@@ -52,15 +52,45 @@ ce.function('Add', [1, 'x'], { form: 'structural' }); // bound, not fully canoni
5252ce .box ([' Add' , 1 , ' x' ], { form: [' Number' , ' Order' ] }); // selective passes
5353```
5454
55+ #### New Free Functions: ` parse() ` , ` simplify() ` , ` evaluate() ` , ` N() ` , ` assign() `
56+
57+ Top-level free functions are now available for the most common operations. They
58+ use a shared ` ComputeEngine ` instance created on first call, so no setup is
59+ required.
60+
61+ ``` ts
62+ import { parse , simplify , evaluate , N , assign } from ' @cortex-js/compute-engine' ;
63+
64+ simplify (' x + x + 1' ); // 2x + 1
65+ evaluate (' 2^{11} - 1' ); // 2047
66+ N (' \\ sqrt{2}' ); // 1.414213562…
67+
68+ assign (' x' , 3 );
69+ evaluate (' x + 2' ); // 5
70+ ```
71+
72+ Except for ` parse() ` (which only accepts a LaTeX string), each function accepts
73+ either a LaTeX string or an existing ` BoxedExpression ` :
74+
75+ ``` ts
76+ const expr = parse (' x + x + 1' );
77+ simplify (expr ); // same as simplify('x + x + 1')
78+ ```
79+
80+ Use ` getDefaultEngine() ` to access the shared engine for configuration
81+ (precision, angular unit, etc.) or to call methods like ` forget() ` .
82+
5583#### ` compile() ` Is Now a Free Function
5684
5785The ` expr.compile() ` method has been replaced by a standalone ` compile() `
58- function with a structured ` CompilationResult ` return type.
86+ function with a structured ` CompilationResult ` return type. It accepts either a
87+ LaTeX string or a ` BoxedExpression ` .
5988
6089``` ts
6190import { compile } from ' @cortex-js/compute-engine' ;
6291
63- const result = compile (ce .parse (' x^2 + 1' ));
92+ // From a LaTeX string
93+ const result = compile (' x^2 + 1' );
6494result .run ({ x: 3 }); // 10
6595result .code ; // generated source
6696result .success ; // true
@@ -79,24 +109,51 @@ Custom compilation targets can be registered and unregistered dynamically via
79109while ` expandAll() ` applies it recursively. Both return ` null ` if the expression
80110cannot be expanded.
81111
112+ Both accept a LaTeX string or a ` BoxedExpression ` , consistent with the other free
113+ functions (` simplify ` , ` evaluate ` , ` N ` ).
114+
82115``` ts
83116import { expand , expandAll } from ' @cortex-js/compute-engine' ;
84117
118+ // From a LaTeX string
119+ expand (' (x+1)^2' ); // x^2 + 2x + 1
120+ expandAll (' (x+1)(x+2) + (a+b)^2' ); // recursive expansion
121+
122+ // From a BoxedExpression
85123const expr = ce .parse (' (x+1)(x+2)' );
86124expand (expr ); // x^2 + 3x + 2
87- expandAll (complexExpr ); // recursive expansion
88125
89126// Returns null when not expandable — use ?? for fallback
90127const result = expand (expr ) ?? expr ;
91128```
92129
130+ #### ` solve() ` Is a Free Function
131+
132+ A new ` solve() ` free function is available for solving equations without
133+ explicitly creating a ` ComputeEngine ` instance. Like the other free functions, it
134+ accepts either a LaTeX string or a ` BoxedExpression ` .
135+
136+ ``` ts
137+ import { solve } from ' @cortex-js/compute-engine' ;
138+
139+ // Solve from LaTeX
140+ solve (' x^2 - 5x + 6 = 0' , ' x' ); // [2, 3]
141+
142+ // Solve from a BoxedExpression
143+ const expr = ce .parse (' x^2 - 5x + 6 = 0' );
144+ solve (expr , ' x' ); // [2, 3]
145+ ```
146+
93147#### ` factor() ` Is a Free Function
94148
95- Polynomial factoring functions are now standalone free functions.
149+ Polynomial factoring functions are now standalone free functions. ` factor() `
150+ accepts a LaTeX string or a ` BoxedExpression ` . The specialized variants
151+ (` factorPolynomial ` , ` factorQuadratic ` , etc.) accept only a ` BoxedExpression ` .
96152
97153``` ts
98154import { factor , factorPolynomial , factorQuadratic } from ' @cortex-js/compute-engine' ;
99155
156+ factor (' (2x)(4y)' ); // 8xy — from LaTeX
100157factor (expr ); // general factoring
101158factorPolynomial (expr ); // polynomial-specific
102159factorQuadratic (expr ); // quadratic-specific
0 commit comments