mirror of
https://github.com/Z3Prover/z3
synced 2026-05-24 10:59:38 +00:00
Format code with prettier for RCF API implementation
Co-authored-by: NikolajBjorner <3085284+NikolajBjorner@users.noreply.github.com>
This commit is contained in:
parent
b1291041e0
commit
b5b1af3de7
4 changed files with 57 additions and 56 deletions
|
|
@ -1,12 +1,12 @@
|
||||||
/**
|
/**
|
||||||
* Example demonstrating the RCF (Real Closed Field) API in TypeScript.
|
* Example demonstrating the RCF (Real Closed Field) API in TypeScript.
|
||||||
*
|
*
|
||||||
* This example shows how to use RCF numerals to work with:
|
* This example shows how to use RCF numerals to work with:
|
||||||
* - Transcendental numbers (pi, e)
|
* - Transcendental numbers (pi, e)
|
||||||
* - Algebraic numbers (roots of polynomials)
|
* - Algebraic numbers (roots of polynomials)
|
||||||
* - Infinitesimals
|
* - Infinitesimals
|
||||||
* - Exact real arithmetic
|
* - Exact real arithmetic
|
||||||
*
|
*
|
||||||
* Note: This example uses the high-level API for a cleaner interface.
|
* Note: This example uses the high-level API for a cleaner interface.
|
||||||
*/
|
*/
|
||||||
|
|
||||||
|
|
@ -76,9 +76,9 @@ async function rcfRootsExample() {
|
||||||
// Find roots of x^2 - 2 = 0
|
// Find roots of x^2 - 2 = 0
|
||||||
// Polynomial: -2 + 0*x + 1*x^2
|
// Polynomial: -2 + 0*x + 1*x^2
|
||||||
const coeffs = [
|
const coeffs = [
|
||||||
RCFNum(-2), // constant term
|
RCFNum(-2), // constant term
|
||||||
RCFNum(0), // x coefficient
|
RCFNum(0), // x coefficient
|
||||||
RCFNum(1) // x^2 coefficient
|
RCFNum(1), // x^2 coefficient
|
||||||
];
|
];
|
||||||
|
|
||||||
const roots = RCFNum.roots(coeffs);
|
const roots = RCFNum.roots(coeffs);
|
||||||
|
|
|
||||||
|
|
@ -2136,23 +2136,23 @@ describe('high-level', () => {
|
||||||
// x^2 - 2 = 0 has roots ±√2
|
// x^2 - 2 = 0 has roots ±√2
|
||||||
// Polynomial: -2 + 0*x + 1*x^2
|
// Polynomial: -2 + 0*x + 1*x^2
|
||||||
const coeffs = [
|
const coeffs = [
|
||||||
RCFNum(-2), // constant term
|
RCFNum(-2), // constant term
|
||||||
RCFNum(0), // x coefficient
|
RCFNum(0), // x coefficient
|
||||||
RCFNum(1) // x^2 coefficient
|
RCFNum(1), // x^2 coefficient
|
||||||
];
|
];
|
||||||
|
|
||||||
const roots = RCFNum.roots(coeffs);
|
const roots = RCFNum.roots(coeffs);
|
||||||
expect(roots.length).toBe(2);
|
expect(roots.length).toBe(2);
|
||||||
|
|
||||||
// All roots should be algebraic
|
// All roots should be algebraic
|
||||||
roots.forEach(root => {
|
roots.forEach(root => {
|
||||||
expect(root.isAlgebraic()).toBe(true);
|
expect(root.isAlgebraic()).toBe(true);
|
||||||
});
|
});
|
||||||
|
|
||||||
// Check that roots are approximately ±√2
|
// Check that roots are approximately ±√2
|
||||||
const root1Decimal = roots[0].toDecimal(5);
|
const root1Decimal = roots[0].toDecimal(5);
|
||||||
const root2Decimal = roots[1].toDecimal(5);
|
const root2Decimal = roots[1].toDecimal(5);
|
||||||
|
|
||||||
// One should be approximately 1.414 and the other -1.414
|
// One should be approximately 1.414 and the other -1.414
|
||||||
const decimals = [root1Decimal, root2Decimal].sort();
|
const decimals = [root1Decimal, root2Decimal].sort();
|
||||||
expect(decimals[0]).toContain('-1.4');
|
expect(decimals[0]).toContain('-1.4');
|
||||||
|
|
@ -2162,7 +2162,7 @@ describe('high-level', () => {
|
||||||
it('should check isRational predicate', () => {
|
it('should check isRational predicate', () => {
|
||||||
const rational = RCFNum('3/4');
|
const rational = RCFNum('3/4');
|
||||||
const pi = RCFNum.pi();
|
const pi = RCFNum.pi();
|
||||||
|
|
||||||
expect(rational.isRational()).toBe(true);
|
expect(rational.isRational()).toBe(true);
|
||||||
expect(pi.isRational()).toBe(false);
|
expect(pi.isRational()).toBe(false);
|
||||||
});
|
});
|
||||||
|
|
@ -2171,9 +2171,9 @@ describe('high-level', () => {
|
||||||
// x^2 - 2 = 0
|
// x^2 - 2 = 0
|
||||||
const coeffs = [RCFNum(-2), RCFNum(0), RCFNum(1)];
|
const coeffs = [RCFNum(-2), RCFNum(0), RCFNum(1)];
|
||||||
const roots = RCFNum.roots(coeffs);
|
const roots = RCFNum.roots(coeffs);
|
||||||
|
|
||||||
expect(roots[0].isAlgebraic()).toBe(true);
|
expect(roots[0].isAlgebraic()).toBe(true);
|
||||||
|
|
||||||
// Pi is not algebraic
|
// Pi is not algebraic
|
||||||
const pi = RCFNum.pi();
|
const pi = RCFNum.pi();
|
||||||
expect(pi.isAlgebraic()).toBe(false);
|
expect(pi.isAlgebraic()).toBe(false);
|
||||||
|
|
@ -2183,7 +2183,7 @@ describe('high-level', () => {
|
||||||
const pi = RCFNum.pi();
|
const pi = RCFNum.pi();
|
||||||
const e = RCFNum.e();
|
const e = RCFNum.e();
|
||||||
const rational = RCFNum(5);
|
const rational = RCFNum(5);
|
||||||
|
|
||||||
expect(pi.isTranscendental()).toBe(true);
|
expect(pi.isTranscendental()).toBe(true);
|
||||||
expect(e.isTranscendental()).toBe(true);
|
expect(e.isTranscendental()).toBe(true);
|
||||||
expect(rational.isTranscendental()).toBe(false);
|
expect(rational.isTranscendental()).toBe(false);
|
||||||
|
|
@ -2192,7 +2192,7 @@ describe('high-level', () => {
|
||||||
it('should check isInfinitesimal predicate', () => {
|
it('should check isInfinitesimal predicate', () => {
|
||||||
const eps = RCFNum.infinitesimal();
|
const eps = RCFNum.infinitesimal();
|
||||||
const rational = RCFNum(5);
|
const rational = RCFNum(5);
|
||||||
|
|
||||||
expect(eps.isInfinitesimal()).toBe(true);
|
expect(eps.isInfinitesimal()).toBe(true);
|
||||||
expect(rational.isInfinitesimal()).toBe(false);
|
expect(rational.isInfinitesimal()).toBe(false);
|
||||||
});
|
});
|
||||||
|
|
@ -2201,7 +2201,7 @@ describe('high-level', () => {
|
||||||
const pi = RCFNum.pi();
|
const pi = RCFNum.pi();
|
||||||
const compact = pi.toString(true);
|
const compact = pi.toString(true);
|
||||||
const nonCompact = pi.toString(false);
|
const nonCompact = pi.toString(false);
|
||||||
|
|
||||||
// Both should contain 'pi' or similar representation
|
// Both should contain 'pi' or similar representation
|
||||||
expect(compact.length).toBeGreaterThan(0);
|
expect(compact.length).toBeGreaterThan(0);
|
||||||
expect(nonCompact.length).toBeGreaterThan(0);
|
expect(nonCompact.length).toBeGreaterThan(0);
|
||||||
|
|
@ -2211,7 +2211,7 @@ describe('high-level', () => {
|
||||||
const pi = RCFNum.pi();
|
const pi = RCFNum.pi();
|
||||||
const decimal5 = pi.toDecimal(5);
|
const decimal5 = pi.toDecimal(5);
|
||||||
const decimal10 = pi.toDecimal(10);
|
const decimal10 = pi.toDecimal(10);
|
||||||
|
|
||||||
// 10 decimal places should be longer than 5
|
// 10 decimal places should be longer than 5
|
||||||
expect(decimal10.length).toBeGreaterThanOrEqual(decimal5.length);
|
expect(decimal10.length).toBeGreaterThanOrEqual(decimal5.length);
|
||||||
expect(decimal5).toContain('3.14');
|
expect(decimal5).toContain('3.14');
|
||||||
|
|
@ -2221,7 +2221,7 @@ describe('high-level', () => {
|
||||||
it('should work with infinitesimal comparisons', () => {
|
it('should work with infinitesimal comparisons', () => {
|
||||||
const eps = RCFNum.infinitesimal();
|
const eps = RCFNum.infinitesimal();
|
||||||
const tiny = RCFNum('1/1000000');
|
const tiny = RCFNum('1/1000000');
|
||||||
|
|
||||||
// Infinitesimal should be smaller than any positive real
|
// Infinitesimal should be smaller than any positive real
|
||||||
expect(eps.lt(tiny)).toBe(true);
|
expect(eps.lt(tiny)).toBe(true);
|
||||||
});
|
});
|
||||||
|
|
|
||||||
|
|
@ -835,27 +835,24 @@ export function createApi(Z3: Z3Core): Z3HighLevel {
|
||||||
},
|
},
|
||||||
};
|
};
|
||||||
|
|
||||||
const RCFNum = Object.assign(
|
const RCFNum = Object.assign((value: string | number) => new RCFNumImpl(value), {
|
||||||
(value: string | number) => new RCFNumImpl(value),
|
pi: () => new RCFNumImpl(check(Z3.rcf_mk_pi(contextPtr))),
|
||||||
{
|
|
||||||
pi: () => new RCFNumImpl(check(Z3.rcf_mk_pi(contextPtr))),
|
|
||||||
|
|
||||||
e: () => new RCFNumImpl(check(Z3.rcf_mk_e(contextPtr))),
|
e: () => new RCFNumImpl(check(Z3.rcf_mk_e(contextPtr))),
|
||||||
|
|
||||||
infinitesimal: () => new RCFNumImpl(check(Z3.rcf_mk_infinitesimal(contextPtr))),
|
infinitesimal: () => new RCFNumImpl(check(Z3.rcf_mk_infinitesimal(contextPtr))),
|
||||||
|
|
||||||
roots: (coefficients: RCFNum<Name>[]) => {
|
roots: (coefficients: RCFNum<Name>[]) => {
|
||||||
assert(coefficients.length > 0, 'Polynomial coefficients cannot be empty');
|
assert(coefficients.length > 0, 'Polynomial coefficients cannot be empty');
|
||||||
const coeffPtrs = coefficients.map(c => (c as RCFNumImpl).ptr);
|
const coeffPtrs = coefficients.map(c => (c as RCFNumImpl).ptr);
|
||||||
const { rv: numRoots, roots: rootPtrs } = Z3.rcf_mk_roots(contextPtr, coeffPtrs);
|
const { rv: numRoots, roots: rootPtrs } = Z3.rcf_mk_roots(contextPtr, coeffPtrs);
|
||||||
const result: RCFNum<Name>[] = [];
|
const result: RCFNum<Name>[] = [];
|
||||||
for (let i = 0; i < numRoots; i++) {
|
for (let i = 0; i < numRoots; i++) {
|
||||||
result.push(new RCFNumImpl(rootPtrs[i]));
|
result.push(new RCFNumImpl(rootPtrs[i]));
|
||||||
}
|
}
|
||||||
return result;
|
return result;
|
||||||
},
|
},
|
||||||
}
|
}) as RCFNumCreation<Name>;
|
||||||
) as RCFNumCreation<Name>;
|
|
||||||
|
|
||||||
const BitVec = {
|
const BitVec = {
|
||||||
sort<Bits extends number>(bits: Bits): BitVecSort<Bits, Name> {
|
sort<Bits extends number>(bits: Bits): BitVecSort<Bits, Name> {
|
||||||
|
|
@ -1776,7 +1773,11 @@ export function createApi(Z3: Z3Core): Z3HighLevel {
|
||||||
return new FuncDeclImpl(check(Z3.mk_transitive_closure(contextPtr, f.ptr)));
|
return new FuncDeclImpl(check(Z3.mk_transitive_closure(contextPtr, f.ptr)));
|
||||||
}
|
}
|
||||||
|
|
||||||
async function polynomialSubresultants(p: Arith<Name>, q: Arith<Name>, x: Arith<Name>): Promise<AstVector<Name, Arith<Name>>> {
|
async function polynomialSubresultants(
|
||||||
|
p: Arith<Name>,
|
||||||
|
q: Arith<Name>,
|
||||||
|
x: Arith<Name>,
|
||||||
|
): Promise<AstVector<Name, Arith<Name>>> {
|
||||||
const result = await Z3.polynomial_subresultants(contextPtr, p.ast, q.ast, x.ast);
|
const result = await Z3.polynomial_subresultants(contextPtr, p.ast, q.ast, x.ast);
|
||||||
return new AstVectorImpl<ArithImpl>(check(result));
|
return new AstVectorImpl<ArithImpl>(check(result));
|
||||||
}
|
}
|
||||||
|
|
@ -2492,7 +2493,7 @@ export function createApi(Z3: Z3Core): Z3HighLevel {
|
||||||
const key = Z3.stats_get_key(contextPtr, this.ptr, i);
|
const key = Z3.stats_get_key(contextPtr, this.ptr, i);
|
||||||
const isUint = Z3.stats_is_uint(contextPtr, this.ptr, i);
|
const isUint = Z3.stats_is_uint(contextPtr, this.ptr, i);
|
||||||
const isDouble = Z3.stats_is_double(contextPtr, this.ptr, i);
|
const isDouble = Z3.stats_is_double(contextPtr, this.ptr, i);
|
||||||
const value = isUint
|
const value = isUint
|
||||||
? Z3.stats_get_uint_value(contextPtr, this.ptr, i)
|
? Z3.stats_get_uint_value(contextPtr, this.ptr, i)
|
||||||
: Z3.stats_get_double_value(contextPtr, this.ptr, i);
|
: Z3.stats_get_double_value(contextPtr, this.ptr, i);
|
||||||
result.push({
|
result.push({
|
||||||
|
|
|
||||||
|
|
@ -184,12 +184,12 @@ export interface Context<Name extends string = 'main'> {
|
||||||
|
|
||||||
/**
|
/**
|
||||||
* Set the pretty printing mode for ASTs.
|
* Set the pretty printing mode for ASTs.
|
||||||
*
|
*
|
||||||
* @param mode - The print mode to use:
|
* @param mode - The print mode to use:
|
||||||
* - Z3_PRINT_SMTLIB_FULL (0): Print AST nodes in SMTLIB verbose format.
|
* - Z3_PRINT_SMTLIB_FULL (0): Print AST nodes in SMTLIB verbose format.
|
||||||
* - Z3_PRINT_LOW_LEVEL (1): Print AST nodes using a low-level format.
|
* - Z3_PRINT_LOW_LEVEL (1): Print AST nodes using a low-level format.
|
||||||
* - Z3_PRINT_SMTLIB2_COMPLIANT (2): Print AST nodes in SMTLIB 2.x compliant format.
|
* - Z3_PRINT_SMTLIB2_COMPLIANT (2): Print AST nodes in SMTLIB 2.x compliant format.
|
||||||
*
|
*
|
||||||
* @category Functions
|
* @category Functions
|
||||||
*/
|
*/
|
||||||
setPrintMode(mode: Z3_ast_print_mode): void;
|
setPrintMode(mode: Z3_ast_print_mode): void;
|
||||||
|
|
@ -1523,16 +1523,16 @@ export interface Model<Name extends string = 'main'> extends Iterable<FuncDecl<N
|
||||||
export interface StatisticsEntry<Name extends string = 'main'> {
|
export interface StatisticsEntry<Name extends string = 'main'> {
|
||||||
/** @hidden */
|
/** @hidden */
|
||||||
readonly __typename: 'StatisticsEntry';
|
readonly __typename: 'StatisticsEntry';
|
||||||
|
|
||||||
/** The key/name of this statistic */
|
/** The key/name of this statistic */
|
||||||
readonly key: string;
|
readonly key: string;
|
||||||
|
|
||||||
/** The numeric value of this statistic */
|
/** The numeric value of this statistic */
|
||||||
readonly value: number;
|
readonly value: number;
|
||||||
|
|
||||||
/** True if this statistic is stored as an unsigned integer */
|
/** True if this statistic is stored as an unsigned integer */
|
||||||
readonly isUint: boolean;
|
readonly isUint: boolean;
|
||||||
|
|
||||||
/** True if this statistic is stored as a double */
|
/** True if this statistic is stored as a double */
|
||||||
readonly isDouble: boolean;
|
readonly isDouble: boolean;
|
||||||
}
|
}
|
||||||
|
|
@ -1543,8 +1543,8 @@ export interface StatisticsCtor<Name extends string> {
|
||||||
|
|
||||||
/**
|
/**
|
||||||
* Statistics for solver operations
|
* Statistics for solver operations
|
||||||
*
|
*
|
||||||
* Provides access to performance metrics, memory usage, decision counts,
|
* Provides access to performance metrics, memory usage, decision counts,
|
||||||
* and other diagnostic information from solver operations.
|
* and other diagnostic information from solver operations.
|
||||||
*/
|
*/
|
||||||
export interface Statistics<Name extends string = 'main'> extends Iterable<StatisticsEntry<Name>> {
|
export interface Statistics<Name extends string = 'main'> extends Iterable<StatisticsEntry<Name>> {
|
||||||
|
|
@ -1992,26 +1992,26 @@ export interface RatNum<Name extends string = 'main'> extends Arith<Name> {
|
||||||
|
|
||||||
/**
|
/**
|
||||||
* A Real Closed Field (RCF) numeral.
|
* A Real Closed Field (RCF) numeral.
|
||||||
*
|
*
|
||||||
* RCF numerals can represent:
|
* RCF numerals can represent:
|
||||||
* - Rational numbers
|
* - Rational numbers
|
||||||
* - Algebraic numbers (roots of polynomials)
|
* - Algebraic numbers (roots of polynomials)
|
||||||
* - Transcendental extensions (e.g., pi, e)
|
* - Transcendental extensions (e.g., pi, e)
|
||||||
* - Infinitesimal extensions
|
* - Infinitesimal extensions
|
||||||
*
|
*
|
||||||
* ```typescript
|
* ```typescript
|
||||||
* const { RCFNum } = Context('main');
|
* const { RCFNum } = Context('main');
|
||||||
*
|
*
|
||||||
* // Create pi
|
* // Create pi
|
||||||
* const pi = RCFNum.pi();
|
* const pi = RCFNum.pi();
|
||||||
* console.log(pi.toDecimal(10)); // "3.1415926536"
|
* console.log(pi.toDecimal(10)); // "3.1415926536"
|
||||||
*
|
*
|
||||||
* // Create a rational
|
* // Create a rational
|
||||||
* const half = new RCFNum('1/2');
|
* const half = new RCFNum('1/2');
|
||||||
*
|
*
|
||||||
* // Arithmetic
|
* // Arithmetic
|
||||||
* const sum = pi.add(half);
|
* const sum = pi.add(half);
|
||||||
*
|
*
|
||||||
* // Check properties
|
* // Check properties
|
||||||
* console.log(pi.isTranscendental()); // true
|
* console.log(pi.isTranscendental()); // true
|
||||||
* console.log(half.isRational()); // true
|
* console.log(half.isRational()); // true
|
||||||
|
|
@ -2187,9 +2187,9 @@ export interface RCFNumCreation<Name extends string> {
|
||||||
|
|
||||||
/**
|
/**
|
||||||
* Find roots of a polynomial.
|
* Find roots of a polynomial.
|
||||||
*
|
*
|
||||||
* The polynomial is a[n-1]*x^(n-1) + ... + a[1]*x + a[0].
|
* The polynomial is a[n-1]*x^(n-1) + ... + a[1]*x + a[0].
|
||||||
*
|
*
|
||||||
* @param coefficients - Polynomial coefficients (constant term first)
|
* @param coefficients - Polynomial coefficients (constant term first)
|
||||||
* @returns Array of RCF numerals representing the roots
|
* @returns Array of RCF numerals representing the roots
|
||||||
*/
|
*/
|
||||||
|
|
|
||||||
Loading…
Add table
Add a link
Reference in a new issue