1- # API Transition Guide
1+ # Migration Guide to Compute Engine 0.50.0
22
33This guide covers the breaking changes introduced in the latest architecture
4- revision of ` @cortex-js/compute-engine ` . Each section shows the old API, the
5- new API, and a brief rationale.
4+ revision of ` @cortex-js/compute-engine ` . Each section shows the old API, the new
5+ API, and a brief rationale.
66
77---
88
@@ -67,23 +67,22 @@ type FormOption =
6767## 2. Role-Specific Properties Moved to Role Interfaces
6868
6969Properties that were previously available on all ` Expression ` instances
70- (returning ` null ` or ` undefined ` when not applicable) have been removed from
71- the base interface. They are now only accessible after narrowing with a type
72- guard.
70+ (returning ` null ` or ` undefined ` when not applicable) have been removed from the
71+ base interface. They are now only accessible after narrowing with a type guard.
7372
7473### Removed from ` Expression `
7574
76- | Property | Access via |
77- | :-------------------| :-------------------------------------------------|
78- | ` .symbol ` | ` isSymbol(expr) ` then ` expr.symbol ` |
79- | ` .string ` | ` isString(expr) ` then ` expr.string ` |
80- | ` .ops ` | ` isFunction(expr) ` then ` expr.ops ` |
81- | ` .nops ` | ` isFunction(expr) ` then ` expr.nops ` |
82- | ` .op1 ` /` .op2 ` /` .op3 ` | ` isFunction(expr) ` then ` expr.op1 ` etc. |
75+ | Property | Access via |
76+ | :---------------------- | :-------------------------------------------------- |
77+ | ` .symbol ` | ` isSymbol(expr) ` then ` expr.symbol ` |
78+ | ` .string ` | ` isString(expr) ` then ` expr.string ` |
79+ | ` .ops ` | ` isFunction(expr) ` then ` expr.ops ` |
80+ | ` .nops ` | ` isFunction(expr) ` then ` expr.nops ` |
81+ | ` .op1 ` /` .op2 ` /` .op3 ` | ` isFunction(expr) ` then ` expr.op1 ` etc. |
8382| ` .isFunctionExpression ` | ` isFunction(expr) ` then ` expr.isFunctionExpression ` |
84- | ` .numericValue ` | ` isNumber(expr) ` then ` expr.numericValue ` |
85- | ` .isNumberLiteral ` | ` isNumber(expr) ` then ` expr.isNumberLiteral ` |
86- | ` .tensor ` | ` isTensor(expr) ` then ` expr.tensor ` |
83+ | ` .numericValue ` | ` isNumber(expr) ` then ` expr.numericValue ` |
84+ | ` .isNumberLiteral ` | ` isNumber(expr) ` then ` expr.isNumberLiteral ` |
85+ | ` .tensor ` | ` isTensor(expr) ` then ` expr.tensor ` |
8786
8887### Before
8988
@@ -120,16 +119,18 @@ if (sym(expr) === 'Pi') {
120119
121120See Section 6 for the full list of type guards and role interfaces.
122121
123- ** Note:** The ` sym() ` helper combines ` isSymbol() ` check with symbol name access,
124- making simple symbol comparisons more concise.
122+ ** Note:** The ` sym() ` helper combines ` isSymbol() ` check with symbol name
123+ access, making simple symbol comparisons more concise.
125124
126125### Still on ` Expression `
127126
128127- ` .re ` / ` .im ` — typed ` number ` , return ` NaN ` when not applicable
129128- ` .shape ` — typed ` number[] ` , returns ` [] ` for scalars
130129- ` .operator ` — returns the operator name for all expression types
131- - All arithmetic methods (` .add() ` , ` .mul() ` , etc.) — work symbolically on all expressions
132- - All numeric predicates (` .isPositive ` , ` .isInteger ` , etc.) — meaningful with assumptions
130+ - All arithmetic methods (` .add() ` , ` .mul() ` , etc.) — work symbolically on all
131+ expressions
132+ - All numeric predicates (` .isPositive ` , ` .isInteger ` , etc.) — meaningful with
133+ assumptions
133134
134135---
135136
@@ -213,16 +214,16 @@ console.log(expanded.latex); // "x^2+3x+2"
213214```
214215
215216> ** Note** : ` expand() ` returns ` null ` if the expression cannot be expanded.
216- > Handle this with ` expand(expr) ?? expr ` if you want the original expression
217- > as a fallback.
217+ > Handle this with ` expand(expr) ?? expr ` if you want the original expression as
218+ > a fallback.
218219
219220---
220221
221222## 5. Library System
222223
223- The constructor now accepts a ` libraries ` option for controlling which
224- libraries are loaded. Libraries declare their dependencies explicitly and are
225- loaded in topological order.
224+ The constructor now accepts a ` libraries ` option for controlling which libraries
225+ are loaded. Libraries declare their dependencies explicitly and are loaded in
226+ topological order.
226227
227228### Before
228229
@@ -362,25 +363,25 @@ const name = sym(expr); // string | undefined
362363
363364### Role Interfaces
364365
365- | Guard | Narrows to |
366- | :-------------------| :------------------------------------------- |
367- | ` isNumber ` | ` Expression & NumberLiteralInterface ` |
368- | ` isSymbol ` | ` Expression & SymbolInterface ` |
369- | ` isFunction ` | ` Expression & FunctionInterface ` |
370- | ` isString ` | ` Expression & StringInterface ` |
371- | ` isTensor ` | ` Expression & TensorInterface ` |
372- | ` isDictionary ` | ` Expression & DictionaryInterface ` |
373- | ` isCollection ` | ` Expression & CollectionInterface ` |
366+ | Guard | Narrows to |
367+ | :-------------------- | :---------------------------------------- |
368+ | ` isNumber ` | ` Expression & NumberLiteralInterface ` |
369+ | ` isSymbol ` | ` Expression & SymbolInterface ` |
370+ | ` isFunction ` | ` Expression & FunctionInterface ` |
371+ | ` isString ` | ` Expression & StringInterface ` |
372+ | ` isTensor ` | ` Expression & TensorInterface ` |
373+ | ` isDictionary ` | ` Expression & DictionaryInterface ` |
374+ | ` isCollection ` | ` Expression & CollectionInterface ` |
374375| ` isIndexedCollection ` | ` Expression & IndexedCollectionInterface ` |
375- | ` isExpression ` | ` Expression ` (from ` unknown ` ) |
376+ | ` isExpression ` | ` Expression ` (from ` unknown ` ) |
376377
377378---
378379
379380## 7. Compilation Targets
380381
381382Custom compilation targets can now be registered and unregistered dynamically.
382- Built-in targets (` 'javascript' ` , ` 'glsl' ` , ` 'python' ` , ` 'interval-javascript ' ` ,
383- ` 'interval-glsl' ` ) are pre-registered.
383+ Built-in targets (` 'javascript' ` , ` 'glsl' ` , 'wgsl' , ` 'python ' ` ,
384+ ` 'interval-javascript' ` , ` 'interval- glsl'` ) are pre-registered.
384385
385386### Before
386387
@@ -470,8 +471,8 @@ expr.simplify({ rules: otherRules });
470471
471472## 9. Subpath Exports
472473
473- ` Expression ` now refers to the compute-engine runtime expression type.
474- The MathJSON type has been renamed to ` MathJsonExpression ` .
474+ ` Expression ` now refers to the compute-engine runtime expression type. The
475+ MathJSON type has been renamed to ` MathJsonExpression ` .
475476
476477### Before
477478
@@ -495,21 +496,21 @@ import { MathJsonExpression } from '@cortex-js/compute-engine/math-json';
495496
496497## 10. Removed Properties
497498
498- The following properties have been removed from the ` Expression ` base
499- interface. They are now only available on the corresponding role interfaces,
500- accessed via type guards.
501-
502- | Removed Property | Type Guard → Interface |
503- | :---------------------------- | :-------------------------------------------------- |
504- | ` expr.numericValue ` | ` isNumber() ` → ` NumberLiteralInterface ` |
505- | ` expr.isNumberLiteral ` | ` isNumber() ` → ` NumberLiteralInterface ` |
506- | ` expr.symbol ` | ` isSymbol() ` → ` SymbolInterface ` |
507- | ` expr.string ` | ` isString() ` → ` StringInterface ` |
508- | ` expr.isFunctionExpression ` | ` isFunction() ` → ` FunctionInterface ` |
509- | ` expr.ops ` | ` isFunction() ` → ` FunctionInterface ` |
510- | ` expr.nops ` | ` isFunction() ` → ` FunctionInterface ` |
511- | ` expr.op1 ` / ` op2 ` / ` op3 ` | ` isFunction() ` → ` FunctionInterface ` |
512- | ` expr.tensor ` | ` isTensor() ` → ` TensorInterface ` |
499+ The following properties have been removed from the ` Expression ` base interface.
500+ They are now only available on the corresponding role interfaces, accessed via
501+ type guards.
502+
503+ | Removed Property | Type Guard → Interface |
504+ | :-------------------------- | :-------------------------------------- |
505+ | ` expr.numericValue ` | ` isNumber() ` → ` NumberLiteralInterface ` |
506+ | ` expr.isNumberLiteral ` | ` isNumber() ` → ` NumberLiteralInterface ` |
507+ | ` expr.symbol ` | ` isSymbol() ` → ` SymbolInterface ` |
508+ | ` expr.string ` | ` isString() ` → ` StringInterface ` |
509+ | ` expr.isFunctionExpression ` | ` isFunction() ` → ` FunctionInterface ` |
510+ | ` expr.ops ` | ` isFunction() ` → ` FunctionInterface ` |
511+ | ` expr.nops ` | ` isFunction() ` → ` FunctionInterface ` |
512+ | ` expr.op1 ` / ` op2 ` / ` op3 ` | ` isFunction() ` → ` FunctionInterface ` |
513+ | ` expr.tensor ` | ` isTensor() ` → ` TensorInterface ` |
513514
514515Accessing these properties without first narrowing with a type guard is now a
515516TypeScript compile error.
@@ -531,6 +532,7 @@ if (isSymbol(expr)) {
531532### Pattern: Checking Multiple Expression Types
532533
533534** Before:**
535+
534536``` ts
535537if (expr .symbol !== null ) {
536538 return expr .symbol ;
@@ -542,6 +544,7 @@ if (expr.symbol !== null) {
542544```
543545
544546** After:**
547+
545548``` ts
546549import { isSymbol , isNumber , isFunction } from ' @cortex-js/compute-engine' ;
547550
@@ -557,6 +560,7 @@ if (isSymbol(expr)) {
557560### Pattern: Processing Function Arguments
558561
559562** Before:**
563+
560564``` ts
561565if (expr .ops ) {
562566 for (const arg of expr .ops ) {
@@ -566,6 +570,7 @@ if (expr.ops) {
566570```
567571
568572** After:**
573+
569574``` ts
570575import { isFunction } from ' @cortex-js/compute-engine' ;
571576
@@ -579,11 +584,13 @@ if (isFunction(expr)) {
579584### Pattern: Safe Numeric Value Access
580585
581586** Before:**
587+
582588``` ts
583589const value = expr .numericValue ?? 0 ; // Default to 0 if not a number
584590```
585591
586592** After:**
593+
587594``` ts
588595import { isNumber } from ' @cortex-js/compute-engine' ;
589596
@@ -593,11 +600,13 @@ const value = isNumber(expr) ? expr.numericValue : 0;
593600### Pattern: Symbol Name Extraction
594601
595602** Before:**
603+
596604``` ts
597605const name = expr .symbol || ' unknown' ;
598606```
599607
600608** After:**
609+
601610``` ts
602611import { sym } from ' @cortex-js/compute-engine' ;
603612
@@ -607,11 +616,13 @@ const name = sym(expr) ?? 'unknown';
607616### Pattern: Working with Decomposition Results
608617
609618** Before:**
619+
610620``` ts
611621const [P, L, U] = luDecomposition .ops ; // Unsafe - ops might be null
612622```
613623
614624** After:**
625+
615626``` ts
616627import { isFunction } from ' @cortex-js/compute-engine' ;
617628
@@ -639,17 +650,14 @@ ce.box(json, { canonical: false });
639650
640651// New
641652import {
642- ComputeEngine ,
653+ getDefaultEngine ,
643654 compile ,
644655 expand ,
645656 isFunction ,
646657} from ' @cortex-js/compute-engine' ;
647658
648- const ce = new ComputeEngine ();
649- const expr = ce .parse (' x^2 + 1' );
650-
651- // New free function calls
652- expand (expr );
659+ const expr = parse (' x^2 + 1' );
660+ expand (expr ); // or expand("x^2 + 1")
653661compile (expr );
654- ce .box (json , { form: ' raw' });
662+ getDefaultEngine () .box (json , { form: ' raw' });
655663```
0 commit comments