Skip to content

Commit 24eb8f8

Browse files
committed
feat: lenient LaTeX parser
1 parent 3f67e02 commit 24eb8f8

13 files changed

Lines changed: 731 additions & 168 deletions

File tree

CHANGELOG.md

Lines changed: 46 additions & 97 deletions
Original file line numberDiff line numberDiff line change
@@ -61,112 +61,61 @@ ce.function('Add', [1, 'x'], { form: 'structural' }); // bound, not fully canoni
6161
ce.box(['Add', 1, 'x'], { form: ['Number', 'Order'] }); // selective passes
6262
```
6363

64-
#### New Free Functions: `parse()`, `simplify()`, `evaluate()`, `N()`, `assign()`
65-
66-
Top-level free functions are now available for the most common operations. They
67-
use a shared `ComputeEngine` instance created on first call, so no setup is
68-
required.
64+
#### New Free Functions
65+
66+
Top-level free functions are now available for common operations and use a
67+
shared `ComputeEngine` instance created on first call.
68+
69+
| Function | Purpose |
70+
| :-- | :-- |
71+
| `getDefaultEngine()` | Return the shared default `ComputeEngine` instance. |
72+
| `parse(latex)` | Parse a LaTeX string into an `Expression`. |
73+
| `simplify(exprOrLatex)` | Simplify an expression or LaTeX input. |
74+
| `evaluate(exprOrLatex)` | Evaluate an expression or LaTeX input symbolically. |
75+
| `N(exprOrLatex)` | Numerically evaluate an expression or LaTeX input. |
76+
| `assign(id, value)` / `assign(record)` | Assign one symbol value or many at once. |
77+
| `expand(exprOrLatex)` | Expand distributively at the top level (`Expression \| null`). |
78+
| `expandAll(exprOrLatex)` | Expand distributively recursively (`Expression \| null`). |
79+
| `solve(exprOrLatex, vars?)` | Solve equations/systems (returns solve result variants). |
80+
| `factor(exprOrLatex)` | Factor an expression. |
81+
| `compile(exprOrLatex, options?)` | Compile to a target language with `CompilationResult`. |
6982

7083
```ts
71-
import { parse, simplify, evaluate, N, assign } from '@cortex-js/compute-engine';
72-
73-
simplify('x + x + 1'); // 2x + 1
74-
evaluate('2^{11} - 1'); // 2047
75-
N('\\sqrt{2}'); // 1.414213562…
84+
import {
85+
getDefaultEngine,
86+
parse,
87+
simplify,
88+
evaluate,
89+
N,
90+
assign,
91+
expand,
92+
expandAll,
93+
solve,
94+
factor,
95+
compile,
96+
} from '@cortex-js/compute-engine';
7697

7798
assign('x', 3);
78-
evaluate('x + 2'); // 5
79-
```
8099

81-
Except for `parse()` (which only accepts a LaTeX string), each function accepts
82-
either a LaTeX string or an existing `Expression`:
83-
84-
```ts
85-
const expr = parse('x + x + 1');
86-
simplify(expr); // same as simplify('x + x + 1')
100+
const expr = parse('x^2 - 5x + 6');
101+
solve(expr, 'x'); // [2, 3]
102+
factor('(2x)(4y)'); // 8xy
103+
compile('x^2 + 1').run({ x: 3 }); // 10
87104
```
88105

89-
Use `getDefaultEngine()` to access the shared engine for configuration
90-
(precision, angular unit, etc.) or to call methods like `forget()`.
91-
92-
#### `compile()` Is Now a Free Function
93-
94-
The `expr.compile()` method has been replaced by a standalone `compile()`
95-
function with a structured `CompilationResult` return type. It accepts either a
96-
LaTeX string or an `Expression`.
97-
98-
```ts
99-
import { compile } from '@cortex-js/compute-engine';
106+
Except for `parse()`, `assign()`, and `getDefaultEngine()`, these free
107+
functions accept either a LaTeX string or an existing `Expression`.
100108

101-
// From a LaTeX string
102-
const result = compile('x^2 + 1');
103-
result.run({ x: 3 }); // 10
104-
result.code; // generated source
105-
result.success; // true
106-
result.target; // 'javascript'
107-
108-
// Target a different language
109-
compile(expr, { to: 'python' });
110-
```
111-
112-
Custom compilation targets can be registered and unregistered dynamically via
113-
`ce.registerCompilationTarget()` and `ce.unregisterCompilationTarget()`.
114-
115-
#### `expand()` and `expandAll()` Are Now Public Free Functions
116-
117-
`expand()` applies the distributive law at the top level of the expression,
118-
while `expandAll()` applies it recursively. Both return `null` if the expression
119-
cannot be expanded.
120-
121-
Both accept a LaTeX string or an `Expression`, consistent with the other free
122-
functions (`simplify`, `evaluate`, `N`).
123-
124-
```ts
125-
import { expand, expandAll } from '@cortex-js/compute-engine';
109+
#### Free Function Notes
126110

127-
// From a LaTeX string
128-
expand('(x+1)^2'); // x^2 + 2x + 1
129-
expandAll('(x+1)(x+2) + (a+b)^2'); // recursive expansion
130-
131-
// From an Expression
132-
const expr = ce.parse('(x+1)(x+2)');
133-
expand(expr); // x^2 + 3x + 2
134-
135-
// Returns null when not expandable — use ?? for fallback
136-
const result = expand(expr) ?? expr;
137-
```
138-
139-
#### `solve()` Is a Free Function
140-
141-
A new `solve()` free function is available for solving equations without
142-
explicitly creating a `ComputeEngine` instance. Like the other free functions,
143-
it accepts either a LaTeX string or an `Expression`.
144-
145-
```ts
146-
import { solve } from '@cortex-js/compute-engine';
147-
148-
// Solve from LaTeX
149-
solve('x^2 - 5x + 6 = 0', 'x'); // [2, 3]
150-
151-
// Solve from an Expression
152-
const expr = ce.parse('x^2 - 5x + 6 = 0');
153-
solve(expr, 'x'); // [2, 3]
154-
```
155-
156-
#### `factor()` Is a Free Function
157-
158-
Polynomial factoring functions are now standalone free functions. `factor()`
159-
accepts a LaTeX string or an `Expression`. The specialized variants
160-
(`factorPolynomial`, `factorQuadratic`, etc.) accept only an `Expression`.
161-
162-
```ts
163-
import { factor, factorPolynomial, factorQuadratic } from '@cortex-js/compute-engine';
164-
165-
factor('(2x)(4y)'); // 8xy — from LaTeX
166-
factor(expr); // general factoring
167-
factorPolynomial(expr); // polynomial-specific
168-
factorQuadratic(expr); // quadratic-specific
169-
```
111+
- `compile()` is now a top-level entry point returning `CompilationResult`.
112+
Custom compilation targets are managed with
113+
`ce.registerCompilationTarget()` and `ce.unregisterCompilationTarget()`.
114+
- `expand()` and `expandAll()` return `null` when an expression is not
115+
expandable.
116+
- `solve()` is available as a top-level wrapper over equation/system solving.
117+
- `factor()` is the top-level factoring entry point. Specialized helpers such
118+
as `factorPolynomial()` and `factorQuadratic()` remain expression-only APIs.
170119

171120
#### `trigSimplify()` Method Removed
172121

LENIENT_PARSER.md

Lines changed: 0 additions & 44 deletions
This file was deleted.

0 commit comments

Comments
 (0)