Skip to content

Commit 08edbe4

Browse files
committed
fix: added declare free function
1 parent aa07a1d commit 08edbe4

4 files changed

Lines changed: 23 additions & 0 deletions

File tree

CHANGELOG.md

Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -73,6 +73,9 @@
7373
and `Expression`. This means you can pass numbers, MathJSON objects, or tuple
7474
arrays directly — e.g., `evaluate(["Add", 1, 2])` or
7575
`simplify(["Power", "x", 2])`.
76+
- Added `declare()` free function to declare symbols without instantiating a
77+
`ComputeEngine` explicitly — e.g., `declare('x', 'integer')` or
78+
`declare({ x: 'integer', y: 'real' })`.
7679

7780
### Units and Quantities
7881

src/compute-engine.ts

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -38,6 +38,7 @@ export {
3838
simplify,
3939
evaluate,
4040
N,
41+
declare,
4142
assign,
4243
expand,
4344
expandAll,

src/compute-engine/free-functions.ts

Lines changed: 18 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -2,8 +2,10 @@ import type {
22
Expression,
33
ExpressionInput,
44
AssignValue,
5+
SymbolDefinition,
56
IComputeEngine,
67
} from './global-types';
8+
import type { Type, TypeString } from '../common/type/types';
79
import type { LatexString } from './latex-syntax/types';
810
import { isExpression } from './boxed-expression/type-guards';
911
import {
@@ -58,6 +60,22 @@ export function N(expr: LatexString | ExpressionInput): Expression {
5860
return toExpression(expr).N();
5961
}
6062

63+
export function declare(
64+
id: string,
65+
def: Type | TypeString | Partial<SymbolDefinition>
66+
): void;
67+
export function declare(symbols: {
68+
[id: string]: Type | TypeString | Partial<SymbolDefinition>;
69+
}): void;
70+
export function declare(
71+
arg1:
72+
| string
73+
| { [id: string]: Type | TypeString | Partial<SymbolDefinition> },
74+
arg2?: Type | TypeString | Partial<SymbolDefinition>
75+
): void {
76+
getDefaultEngine().declare(arg1 as any, arg2 as any);
77+
}
78+
6179
export function assign(id: string, value: AssignValue): void;
6280
export function assign(ids: { [id: string]: AssignValue }): void;
6381
export function assign(

src/compute-engine/index.ts

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -175,6 +175,7 @@ export {
175175
simplify,
176176
evaluate,
177177
N,
178+
declare,
178179
assign,
179180
expand,
180181
expandAll,

0 commit comments

Comments
 (0)