@cortex-js/compute-engine 0.33.0 → 0.35.0

This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
Files changed (177) hide show
  1. package/dist/compute-engine.esm.js +16198 -9444
  2. package/dist/compute-engine.min.esm.js +457 -52
  3. package/dist/compute-engine.min.umd.js +457 -52
  4. package/dist/compute-engine.umd.js +16197 -9443
  5. package/dist/math-json.esm.js +2 -2
  6. package/dist/math-json.min.esm.js +2 -2
  7. package/dist/math-json.min.umd.js +2 -2
  8. package/dist/math-json.umd.js +2 -2
  9. package/dist/types/common/ansi-codes.d.ts +1 -1
  10. package/dist/types/common/configuration-change.d.ts +1 -1
  11. package/dist/types/common/fuzzy-string-match.d.ts +1 -1
  12. package/dist/types/common/grapheme-splitter.d.ts +1 -1
  13. package/dist/types/common/interruptible.d.ts +1 -1
  14. package/dist/types/common/one-of.d.ts +1 -1
  15. package/dist/types/common/signals.d.ts +1 -1
  16. package/dist/types/common/type/ast-nodes.d.ts +1 -1
  17. package/dist/types/common/type/boxed-type.d.ts +1 -1
  18. package/dist/types/common/type/lexer.d.ts +1 -1
  19. package/dist/types/common/type/parse.d.ts +1 -1
  20. package/dist/types/common/type/parser.d.ts +1 -1
  21. package/dist/types/common/type/primitive.d.ts +1 -1
  22. package/dist/types/common/type/reduce.d.ts +12 -0
  23. package/dist/types/common/type/serialize.d.ts +1 -1
  24. package/dist/types/common/type/subtype.d.ts +1 -1
  25. package/dist/types/common/type/type-builder.d.ts +1 -1
  26. package/dist/types/common/type/types.d.ts +1 -1
  27. package/dist/types/common/type/utils.d.ts +1 -1
  28. package/dist/types/common/utils.d.ts +1 -1
  29. package/dist/types/compute-engine/assume.d.ts +37 -1
  30. package/dist/types/compute-engine/boxed-expression/abstract-boxed-expression.d.ts +5 -3
  31. package/dist/types/compute-engine/boxed-expression/apply.d.ts +1 -1
  32. package/dist/types/compute-engine/boxed-expression/arithmetic-add.d.ts +1 -1
  33. package/dist/types/compute-engine/boxed-expression/arithmetic-mul-div.d.ts +1 -1
  34. package/dist/types/compute-engine/boxed-expression/arithmetic-power.d.ts +1 -1
  35. package/dist/types/compute-engine/boxed-expression/ascii-math.d.ts +1 -1
  36. package/dist/types/compute-engine/boxed-expression/box.d.ts +1 -1
  37. package/dist/types/compute-engine/boxed-expression/boxed-dictionary.d.ts +1 -1
  38. package/dist/types/compute-engine/boxed-expression/boxed-function.d.ts +2 -2
  39. package/dist/types/compute-engine/boxed-expression/boxed-number.d.ts +1 -1
  40. package/dist/types/compute-engine/boxed-expression/boxed-operator-definition.d.ts +1 -1
  41. package/dist/types/compute-engine/boxed-expression/boxed-patterns.d.ts +1 -1
  42. package/dist/types/compute-engine/boxed-expression/boxed-string.d.ts +1 -1
  43. package/dist/types/compute-engine/boxed-expression/boxed-symbol.d.ts +1 -1
  44. package/dist/types/compute-engine/boxed-expression/boxed-tensor.d.ts +2 -1
  45. package/dist/types/compute-engine/boxed-expression/boxed-value-definition.d.ts +5 -1
  46. package/dist/types/compute-engine/boxed-expression/cache.d.ts +1 -1
  47. package/dist/types/compute-engine/boxed-expression/canonical-utils.d.ts +1 -1
  48. package/dist/types/compute-engine/boxed-expression/canonical.d.ts +1 -1
  49. package/dist/types/compute-engine/boxed-expression/compare.d.ts +1 -1
  50. package/dist/types/compute-engine/boxed-expression/expand.d.ts +1 -1
  51. package/dist/types/compute-engine/boxed-expression/expression-map.d.ts +1 -1
  52. package/dist/types/compute-engine/boxed-expression/factor.d.ts +47 -1
  53. package/dist/types/compute-engine/boxed-expression/flatten.d.ts +1 -1
  54. package/dist/types/compute-engine/boxed-expression/hold.d.ts +1 -1
  55. package/dist/types/compute-engine/boxed-expression/match.d.ts +1 -1
  56. package/dist/types/compute-engine/boxed-expression/negate.d.ts +1 -1
  57. package/dist/types/compute-engine/boxed-expression/numerics.d.ts +1 -1
  58. package/dist/types/compute-engine/boxed-expression/order.d.ts +1 -1
  59. package/dist/types/compute-engine/boxed-expression/polynomials.d.ts +1 -1
  60. package/dist/types/compute-engine/boxed-expression/product.d.ts +1 -1
  61. package/dist/types/compute-engine/boxed-expression/rules.d.ts +1 -1
  62. package/dist/types/compute-engine/boxed-expression/serialize.d.ts +1 -1
  63. package/dist/types/compute-engine/boxed-expression/sgn.d.ts +1 -1
  64. package/dist/types/compute-engine/boxed-expression/simplify.d.ts +1 -1
  65. package/dist/types/compute-engine/boxed-expression/solve-linear-system.d.ts +60 -0
  66. package/dist/types/compute-engine/boxed-expression/solve.d.ts +1 -1
  67. package/dist/types/compute-engine/boxed-expression/terms.d.ts +1 -1
  68. package/dist/types/compute-engine/boxed-expression/trigonometry.d.ts +1 -1
  69. package/dist/types/compute-engine/boxed-expression/utils.d.ts +1 -1
  70. package/dist/types/compute-engine/boxed-expression/validate.d.ts +1 -1
  71. package/dist/types/compute-engine/collection-utils.d.ts +1 -1
  72. package/dist/types/compute-engine/compilation/base-compiler.d.ts +31 -0
  73. package/dist/types/compute-engine/compilation/glsl-target.d.ts +63 -0
  74. package/dist/types/compute-engine/compilation/interval-glsl-target.d.ts +51 -0
  75. package/dist/types/compute-engine/compilation/interval-javascript-target.d.ts +93 -0
  76. package/dist/types/compute-engine/compilation/javascript-target.d.ts +68 -0
  77. package/dist/types/compute-engine/compilation/types.d.ts +140 -0
  78. package/dist/types/compute-engine/cost-function.d.ts +1 -1
  79. package/dist/types/compute-engine/function-utils.d.ts +1 -1
  80. package/dist/types/compute-engine/global-types.d.ts +340 -8
  81. package/dist/types/compute-engine/index.d.ts +188 -3
  82. package/dist/types/compute-engine/interval/arithmetic.d.ts +56 -0
  83. package/dist/types/compute-engine/interval/comparison.d.ts +70 -0
  84. package/dist/types/compute-engine/interval/elementary.d.ts +100 -0
  85. package/dist/types/compute-engine/interval/index.d.ts +93 -0
  86. package/dist/types/compute-engine/interval/trigonometric.d.ts +105 -0
  87. package/dist/types/compute-engine/interval/types.d.ts +50 -0
  88. package/dist/types/compute-engine/interval/util.d.ts +99 -0
  89. package/dist/types/compute-engine/latex-syntax/dictionary/definitions-algebra.d.ts +1 -1
  90. package/dist/types/compute-engine/latex-syntax/dictionary/definitions-arithmetic.d.ts +1 -1
  91. package/dist/types/compute-engine/latex-syntax/dictionary/definitions-calculus.d.ts +1 -1
  92. package/dist/types/compute-engine/latex-syntax/dictionary/definitions-complex.d.ts +1 -1
  93. package/dist/types/compute-engine/latex-syntax/dictionary/definitions-core.d.ts +1 -1
  94. package/dist/types/compute-engine/latex-syntax/dictionary/definitions-linear-algebra.d.ts +1 -1
  95. package/dist/types/compute-engine/latex-syntax/dictionary/definitions-logic.d.ts +1 -1
  96. package/dist/types/compute-engine/latex-syntax/dictionary/definitions-other.d.ts +1 -1
  97. package/dist/types/compute-engine/latex-syntax/dictionary/definitions-relational-operators.d.ts +1 -1
  98. package/dist/types/compute-engine/latex-syntax/dictionary/definitions-sets.d.ts +1 -1
  99. package/dist/types/compute-engine/latex-syntax/dictionary/definitions-statistics.d.ts +1 -1
  100. package/dist/types/compute-engine/latex-syntax/dictionary/definitions-symbols.d.ts +1 -1
  101. package/dist/types/compute-engine/latex-syntax/dictionary/definitions-trigonometry.d.ts +1 -1
  102. package/dist/types/compute-engine/latex-syntax/dictionary/definitions.d.ts +8 -1
  103. package/dist/types/compute-engine/latex-syntax/parse-symbol.d.ts +1 -1
  104. package/dist/types/compute-engine/latex-syntax/parse.d.ts +21 -1
  105. package/dist/types/compute-engine/latex-syntax/serialize-number.d.ts +1 -1
  106. package/dist/types/compute-engine/latex-syntax/serializer-style.d.ts +1 -1
  107. package/dist/types/compute-engine/latex-syntax/serializer.d.ts +1 -1
  108. package/dist/types/compute-engine/latex-syntax/tokenizer.d.ts +1 -1
  109. package/dist/types/compute-engine/latex-syntax/types.d.ts +23 -2
  110. package/dist/types/compute-engine/latex-syntax/utils.d.ts +1 -1
  111. package/dist/types/compute-engine/library/arithmetic.d.ts +1 -1
  112. package/dist/types/compute-engine/library/calculus.d.ts +1 -1
  113. package/dist/types/compute-engine/library/collections.d.ts +1 -1
  114. package/dist/types/compute-engine/library/combinatorics.d.ts +1 -1
  115. package/dist/types/compute-engine/library/complex.d.ts +1 -1
  116. package/dist/types/compute-engine/library/control-structures.d.ts +1 -1
  117. package/dist/types/compute-engine/library/core.d.ts +1 -1
  118. package/dist/types/compute-engine/library/invisible-operator.d.ts +1 -1
  119. package/dist/types/compute-engine/library/library.d.ts +1 -1
  120. package/dist/types/compute-engine/library/linear-algebra.d.ts +1 -1
  121. package/dist/types/compute-engine/library/logic-analysis.d.ts +159 -4
  122. package/dist/types/compute-engine/library/logic-utils.d.ts +1 -1
  123. package/dist/types/compute-engine/library/logic.d.ts +1 -1
  124. package/dist/types/compute-engine/library/number-theory.d.ts +1 -1
  125. package/dist/types/compute-engine/library/polynomials.d.ts +1 -1
  126. package/dist/types/compute-engine/library/random-expression.d.ts +1 -1
  127. package/dist/types/compute-engine/library/relational-operator.d.ts +1 -1
  128. package/dist/types/compute-engine/library/sets.d.ts +1 -1
  129. package/dist/types/compute-engine/library/statistics.d.ts +1 -1
  130. package/dist/types/compute-engine/library/trigonometry.d.ts +1 -1
  131. package/dist/types/compute-engine/library/utils.d.ts +1 -1
  132. package/dist/types/compute-engine/numeric-value/big-numeric-value.d.ts +1 -1
  133. package/dist/types/compute-engine/numeric-value/exact-numeric-value.d.ts +1 -1
  134. package/dist/types/compute-engine/numeric-value/machine-numeric-value.d.ts +1 -1
  135. package/dist/types/compute-engine/numeric-value/types.d.ts +1 -1
  136. package/dist/types/compute-engine/numerics/bigint.d.ts +1 -1
  137. package/dist/types/compute-engine/numerics/expression.d.ts +1 -1
  138. package/dist/types/compute-engine/numerics/interval.d.ts +1 -1
  139. package/dist/types/compute-engine/numerics/monte-carlo.d.ts +1 -1
  140. package/dist/types/compute-engine/numerics/numeric-bigint.d.ts +1 -1
  141. package/dist/types/compute-engine/numerics/numeric-bignum.d.ts +1 -1
  142. package/dist/types/compute-engine/numerics/numeric-complex.d.ts +1 -1
  143. package/dist/types/compute-engine/numerics/numeric.d.ts +1 -1
  144. package/dist/types/compute-engine/numerics/primes.d.ts +1 -1
  145. package/dist/types/compute-engine/numerics/rationals.d.ts +1 -1
  146. package/dist/types/compute-engine/numerics/richardson.d.ts +1 -1
  147. package/dist/types/compute-engine/numerics/special-functions.d.ts +1 -1
  148. package/dist/types/compute-engine/numerics/statistics.d.ts +1 -1
  149. package/dist/types/compute-engine/numerics/strings.d.ts +1 -1
  150. package/dist/types/compute-engine/numerics/types.d.ts +1 -1
  151. package/dist/types/compute-engine/oeis.d.ts +85 -0
  152. package/dist/types/compute-engine/sequence.d.ts +120 -0
  153. package/dist/types/compute-engine/symbolic/antiderivative.d.ts +1 -1
  154. package/dist/types/compute-engine/symbolic/derivative.d.ts +17 -2
  155. package/dist/types/compute-engine/symbolic/distribute.d.ts +1 -1
  156. package/dist/types/compute-engine/symbolic/fu-cost.d.ts +1 -1
  157. package/dist/types/compute-engine/symbolic/fu-transforms.d.ts +1 -1
  158. package/dist/types/compute-engine/symbolic/fu.d.ts +1 -1
  159. package/dist/types/compute-engine/symbolic/simplify-abs.d.ts +1 -1
  160. package/dist/types/compute-engine/symbolic/simplify-divide.d.ts +1 -1
  161. package/dist/types/compute-engine/symbolic/simplify-hyperbolic.d.ts +1 -1
  162. package/dist/types/compute-engine/symbolic/simplify-infinity.d.ts +1 -1
  163. package/dist/types/compute-engine/symbolic/simplify-log.d.ts +1 -1
  164. package/dist/types/compute-engine/symbolic/simplify-power.d.ts +1 -1
  165. package/dist/types/compute-engine/symbolic/simplify-product.d.ts +1 -1
  166. package/dist/types/compute-engine/symbolic/simplify-rules.d.ts +1 -1
  167. package/dist/types/compute-engine/symbolic/simplify-sum.d.ts +1 -1
  168. package/dist/types/compute-engine/symbolic/simplify-trig.d.ts +1 -1
  169. package/dist/types/compute-engine/tensor/tensor-fields.d.ts +1 -1
  170. package/dist/types/compute-engine/tensor/tensors.d.ts +1 -1
  171. package/dist/types/compute-engine/types.d.ts +1 -1
  172. package/dist/types/compute-engine.d.ts +5 -1
  173. package/dist/types/math-json/symbols.d.ts +1 -1
  174. package/dist/types/math-json/types.d.ts +1 -1
  175. package/dist/types/math-json/utils.d.ts +1 -1
  176. package/dist/types/math-json.d.ts +2 -2
  177. package/package.json +2 -2
@@ -1,4 +1,4 @@
1
- /* 0.33.0 */
1
+ /* 0.35.0 */
2
2
  import type { OneOf } from '../common/one-of';
3
3
  import type { Expression, MathJsonNumberObject, MathJsonStringObject, MathJsonFunctionObject, MathJsonSymbolObject, MathJsonSymbol, MathJsonDictionaryObject } from '../math-json';
4
4
  import type { Type, TypeReference, TypeResolver, TypeString } from '../common/type/types';
@@ -506,7 +506,7 @@ export interface BoxedExpression {
506
506
  * Compute Engine or it may return a different value each time it is
507
507
  * evaluated, even if the state of the Compute Engine is the same.
508
508
  *
509
- * As an example, the ["Add", 2, 3]` function expression is pure, but
509
+ * As an example, the `["Add", 2, 3]` function expression is pure, but
510
510
  * the `["Random"]` function expression is not pure.
511
511
  *
512
512
  * For a function expression to be pure, the function itself (its operator)
@@ -1344,9 +1344,25 @@ export interface BoxedExpression {
1344
1344
  * `options.fallback` is set to `false`. If it is set to `false`, the
1345
1345
  * function will throw an error if it cannot be compiled.
1346
1346
  *
1347
+ * **Custom operators**: You can override operators to use function calls
1348
+ * instead of native operators, useful for vector/matrix operations:
1349
+ *
1350
+ * ```javascript
1351
+ * const expr = ce.parse("v + w");
1352
+ * const f = expr.compile({
1353
+ * operators: {
1354
+ * Add: ['add', 11], // Convert + to add()
1355
+ * Multiply: ['mul', 12] // Convert * to mul()
1356
+ * }
1357
+ * });
1358
+ * // Result: add(v, w) instead of v + w
1359
+ * ```
1360
+ *
1347
1361
  */
1348
1362
  compile(options?: {
1349
- to?: 'javascript' | 'wgsl' | 'python' | 'webassembly';
1363
+ to?: string;
1364
+ target?: any;
1365
+ operators?: Partial<Record<MathJsonSymbol, [op: string, prec: number]>> | ((op: MathJsonSymbol) => [op: string, prec: number] | undefined);
1350
1366
  functions?: Record<MathJsonSymbol, JSSource | ((...any: any[]) => any)>;
1351
1367
  vars?: Record<MathJsonSymbol, JSSource>;
1352
1368
  imports?: ((...any: any[]) => any)[];
@@ -1359,15 +1375,27 @@ export interface BoxedExpression {
1359
1375
  * If this is an equation, solve the equation for the variables in vars.
1360
1376
  * Otherwise, solve the equation `this = 0` for the variables in vars.
1361
1377
  *
1378
+ * For univariate equations, returns an array of solutions (roots).
1379
+ * For systems of linear equations (List of Equal expressions), returns
1380
+ * an object mapping variable names to their values.
1381
+ * For non-linear polynomial systems (like xy=6, x+y=5), returns an array
1382
+ * of solution objects (multiple solutions possible).
1362
1383
  *
1363
1384
  * ```javascript
1385
+ * // Univariate equation
1364
1386
  * const expr = ce.parse("x^2 + 2*x + 1 = 0");
1365
- * console.log(expr.solve("x"));
1366
- * ```
1387
+ * console.log(expr.solve("x")); // Returns array of roots
1367
1388
  *
1389
+ * // System of linear equations
1390
+ * const system = ce.parse("\\begin{cases}x+y=70\\\\2x-4y=80\\end{cases}");
1391
+ * console.log(system.solve(["x", "y"])); // Returns { x: 60, y: 10 }
1368
1392
  *
1393
+ * // Non-linear polynomial system (product + sum)
1394
+ * const nonlinear = ce.parse("\\begin{cases}xy=6\\\\x+y=5\\end{cases}");
1395
+ * console.log(nonlinear.solve(["x", "y"])); // Returns [{ x: 2, y: 3 }, { x: 3, y: 2 }]
1396
+ * ```
1369
1397
  */
1370
- solve(vars?: Iterable<string> | string | BoxedExpression | Iterable<BoxedExpression>): null | ReadonlyArray<BoxedExpression>;
1398
+ solve(vars?: Iterable<string> | string | BoxedExpression | Iterable<BoxedExpression>): null | ReadonlyArray<BoxedExpression> | Record<string, BoxedExpression> | Array<Record<string, BoxedExpression>>;
1371
1399
  /**
1372
1400
  * If this expression is a number literal, a string literal or a function
1373
1401
  * literal, return the expression.
@@ -1833,7 +1861,194 @@ export type ValueDefinition = BaseDefinition & {
1833
1861
  neq: (a: BoxedExpression) => boolean | undefined;
1834
1862
  cmp: (a: BoxedExpression) => '=' | '>' | '<' | undefined;
1835
1863
  collection: CollectionHandlers;
1864
+ /**
1865
+ * Custom evaluation handler for subscripted expressions of this symbol.
1866
+ * Called when evaluating `Subscript(symbol, index)`.
1867
+ *
1868
+ * @param subscript - The subscript expression (already evaluated)
1869
+ * @param options - Contains the compute engine and evaluation options
1870
+ * @returns The evaluated result, or `undefined` to fall back to symbolic form
1871
+ */
1872
+ subscriptEvaluate?: (subscript: BoxedExpression, options: {
1873
+ engine: ComputeEngine;
1874
+ numericApproximation?: boolean;
1875
+ }) => BoxedExpression | undefined;
1836
1876
  };
1877
+ /**
1878
+ * Definition for a sequence declared with `ce.declareSequence()`.
1879
+ *
1880
+ * A sequence is defined by base cases and a recurrence relation.
1881
+ *
1882
+ * @example
1883
+ * ```typescript
1884
+ * // Fibonacci sequence
1885
+ * ce.declareSequence('F', {
1886
+ * base: { 0: 0, 1: 1 },
1887
+ * recurrence: 'F_{n-1} + F_{n-2}',
1888
+ * });
1889
+ * ce.parse('F_{10}').evaluate(); // → 55
1890
+ * ```
1891
+ *
1892
+ * @category Definitions
1893
+ */
1894
+ export interface SequenceDefinition {
1895
+ /**
1896
+ * Index variable name for single-index sequences, default 'n'.
1897
+ * For multi-index sequences, use `variables` instead.
1898
+ */
1899
+ variable?: string;
1900
+ /**
1901
+ * Index variable names for multi-index sequences.
1902
+ * Example: `['n', 'k']` for Pascal's triangle `P\_{n,k}`
1903
+ *
1904
+ * If provided, this takes precedence over `variable`.
1905
+ */
1906
+ variables?: string[];
1907
+ /**
1908
+ * Base cases as index → value mapping.
1909
+ *
1910
+ * For single-index sequences, use numeric keys:
1911
+ * ```typescript
1912
+ * base: { 0: 0, 1: 1 } // F_0 = 0, F_1 = 1
1913
+ * ```
1914
+ *
1915
+ * For multi-index sequences, use comma-separated string keys:
1916
+ * ```typescript
1917
+ * base: {
1918
+ * '0,0': 1, // Exact: P_{0,0} = 1
1919
+ * 'n,0': 1, // Pattern: P_{n,0} = 1 for all n
1920
+ * 'n,n': 1, // Pattern: P_{n,n} = 1 (diagonal)
1921
+ * }
1922
+ * ```
1923
+ *
1924
+ * Pattern keys use variable names to match any value. When the same
1925
+ * variable appears multiple times (e.g., 'n,n'), the indices must be equal.
1926
+ */
1927
+ base: Record<number | string, number | BoxedExpression>;
1928
+ /** Recurrence relation as LaTeX string or BoxedExpression */
1929
+ recurrence: string | BoxedExpression;
1930
+ /** Whether to memoize computed values (default: true) */
1931
+ memoize?: boolean;
1932
+ /**
1933
+ * Valid index domain constraints.
1934
+ *
1935
+ * For single-index sequences:
1936
+ * ```typescript
1937
+ * domain: { min: 0, max: 100 }
1938
+ * ```
1939
+ *
1940
+ * For multi-index sequences, use per-variable constraints:
1941
+ * ```typescript
1942
+ * domain: { n: { min: 0 }, k: { min: 0 } }
1943
+ * ```
1944
+ */
1945
+ domain?: {
1946
+ min?: number;
1947
+ max?: number;
1948
+ } | Record<string, {
1949
+ min?: number;
1950
+ max?: number;
1951
+ }>;
1952
+ /**
1953
+ * Constraint expression for multi-index sequences.
1954
+ * The expression should evaluate to a boolean/numeric value.
1955
+ * If it evaluates to false or 0, the subscript is considered out of domain.
1956
+ *
1957
+ * Example: `'k <= n'` for Pascal's triangle (only valid when k ≤ n)
1958
+ */
1959
+ constraints?: string | BoxedExpression;
1960
+ }
1961
+ /**
1962
+ * Status of a sequence definition.
1963
+ * @category Definitions
1964
+ */
1965
+ export interface SequenceStatus {
1966
+ /**
1967
+ * Status of the sequence:
1968
+ * - 'complete': Both base case(s) and recurrence defined
1969
+ * - 'pending': Waiting for base case(s) or recurrence
1970
+ * - 'not-a-sequence': Symbol is not a sequence
1971
+ */
1972
+ status: 'complete' | 'pending' | 'not-a-sequence';
1973
+ /** Whether at least one base case is defined */
1974
+ hasBase: boolean;
1975
+ /** Whether a recurrence relation is defined */
1976
+ hasRecurrence: boolean;
1977
+ /**
1978
+ * Keys of defined base cases.
1979
+ * For single-index: numeric indices (e.g., [0, 1])
1980
+ * For multi-index: string keys including patterns (e.g., ['0,0', 'n,0', 'n,n'])
1981
+ */
1982
+ baseIndices: (number | string)[];
1983
+ /** Index variable name if recurrence is defined (single-index) */
1984
+ variable?: string;
1985
+ /** Index variable names if recurrence is defined (multi-index) */
1986
+ variables?: string[];
1987
+ }
1988
+ /**
1989
+ * Information about a defined sequence for introspection.
1990
+ * @category Definitions
1991
+ */
1992
+ export interface SequenceInfo {
1993
+ /** The sequence name */
1994
+ name: string;
1995
+ /** Index variable name for single-index sequences (e.g., `"n"`) */
1996
+ variable?: string;
1997
+ /** Index variable names for multi-index sequences (e.g., `["n", "k"]`) */
1998
+ variables?: string[];
1999
+ /**
2000
+ * Base case keys.
2001
+ * For single-index: numeric indices
2002
+ * For multi-index: string keys including patterns
2003
+ */
2004
+ baseIndices: (number | string)[];
2005
+ /** Whether memoization is enabled */
2006
+ memoize: boolean;
2007
+ /**
2008
+ * Domain constraints.
2009
+ * For single-index: `{ min?, max? }`
2010
+ * For multi-index: per-variable constraints
2011
+ */
2012
+ domain: {
2013
+ min?: number;
2014
+ max?: number;
2015
+ } | Record<string, {
2016
+ min?: number;
2017
+ max?: number;
2018
+ }>;
2019
+ /** Number of cached values */
2020
+ cacheSize: number;
2021
+ /** Whether this is a multi-index sequence */
2022
+ isMultiIndex: boolean;
2023
+ }
2024
+ /**
2025
+ * Result from an OEIS lookup operation.
2026
+ * @category OEIS
2027
+ */
2028
+ export interface OEISSequenceInfo {
2029
+ /** OEIS sequence ID (e.g., 'A000045') */
2030
+ id: string;
2031
+ /** Sequence name/description */
2032
+ name: string;
2033
+ /** First several terms of the sequence */
2034
+ terms: number[];
2035
+ /** Formula or recurrence (if available) */
2036
+ formula?: string;
2037
+ /** Comments about the sequence */
2038
+ comments?: string[];
2039
+ /** URL to the OEIS page */
2040
+ url: string;
2041
+ }
2042
+ /**
2043
+ * Options for OEIS operations.
2044
+ * @category OEIS
2045
+ */
2046
+ export interface OEISOptions {
2047
+ /** Request timeout in milliseconds (default: 10000) */
2048
+ timeout?: number;
2049
+ /** Maximum number of results to return for lookups (default: 5) */
2050
+ maxResults?: number;
2051
+ }
1837
2052
  /**
1838
2053
  * Definition record for a function.
1839
2054
  * @category Definitions
@@ -2316,6 +2531,14 @@ export interface BoxedValueDefinition extends BoxedBaseDefinition {
2316
2531
  */
2317
2532
  inferredType: boolean;
2318
2533
  type: BoxedType;
2534
+ /**
2535
+ * Custom evaluation handler for subscripted expressions of this symbol.
2536
+ * Called when evaluating `Subscript(symbol, index)`.
2537
+ */
2538
+ subscriptEvaluate?: (subscript: BoxedExpression, options: {
2539
+ engine: ComputeEngine;
2540
+ numericApproximation?: boolean;
2541
+ }) => BoxedExpression | undefined;
2319
2542
  }
2320
2543
  /**
2321
2544
  * An operator definition can have some flags to indicate specific
@@ -2616,7 +2839,7 @@ export type RuleSteps = RuleStep[];
2616
2839
  * into a new expression `replace`.
2617
2840
  *
2618
2841
  * - `x-1` \( \to \) `1-x`
2619
- * - `(x+1)(x-1)` \( \to \) `x^2-1
2842
+ * - `(x+1)(x-1)` \( \to \) `x^2-1`
2620
2843
  *
2621
2844
  * The patterns can be expressed as LaTeX strings or `SemiBoxedExpression`'s.
2622
2845
  * Alternatively, match/replace logic may be specified by a `RuleFunction`, allowing both custom
@@ -2796,6 +3019,8 @@ export interface ComputeEngine extends IBigNum {
2796
3019
  readonly context: EvalContext;
2797
3020
  contextStack: ReadonlyArray<EvalContext>;
2798
3021
  /** @internal */
3022
+ readonly isVerifying: boolean;
3023
+ /** @internal */
2799
3024
  readonly _typeResolver: TypeResolver;
2800
3025
  /** Absolute time beyond which evaluation should not proceed
2801
3026
  * @internal
@@ -2929,6 +3154,11 @@ export interface ComputeEngine extends IBigNum {
2929
3154
  * Use `ce.assign(id, value)` instead.
2930
3155
  * @internal */
2931
3156
  _setSymbolValue(id: MathJsonSymbol, value: BoxedExpression | boolean | number | undefined): void;
3157
+ /**
3158
+ * Set a value directly in the current context's values map.
3159
+ * Used for assumptions so values are properly scoped.
3160
+ * @internal */
3161
+ _setCurrentContextValue(id: MathJsonSymbol, value: BoxedExpression | boolean | number | undefined): void;
2932
3162
  /** A list of the function calls to the current evaluation context */
2933
3163
  trace: ReadonlyArray<string>;
2934
3164
  lookupContext(id: MathJsonSymbol): undefined | EvalContext;
@@ -2953,9 +3183,111 @@ export interface ComputeEngine extends IBigNum {
2953
3183
  [id: MathJsonSymbol]: Type | TypeString | Partial<SymbolDefinition>;
2954
3184
  }, arg2?: Type | TypeString | Partial<SymbolDefinition>, arg3?: Scope): ComputeEngine;
2955
3185
  assume(predicate: BoxedExpression): AssumeResult;
3186
+ /**
3187
+ * Declare a sequence with a recurrence relation.
3188
+ *
3189
+ * @example
3190
+ * ```typescript
3191
+ * // Fibonacci sequence
3192
+ * ce.declareSequence('F', {
3193
+ * base: { 0: 0, 1: 1 },
3194
+ * recurrence: 'F_{n-1} + F_{n-2}',
3195
+ * });
3196
+ * ce.parse('F_{10}').evaluate(); // → 55
3197
+ * ```
3198
+ */
3199
+ declareSequence(name: string, def: SequenceDefinition): ComputeEngine;
3200
+ /**
3201
+ * Get the status of a sequence definition.
3202
+ *
3203
+ * @example
3204
+ * ```typescript
3205
+ * ce.parse('F_0 := 0').evaluate();
3206
+ * ce.getSequenceStatus('F');
3207
+ * // → { status: 'pending', hasBase: true, hasRecurrence: false, baseIndices: [0] }
3208
+ * ```
3209
+ */
3210
+ getSequenceStatus(name: string): SequenceStatus;
3211
+ /**
3212
+ * Get information about a defined sequence.
3213
+ * Returns `undefined` if the symbol is not a sequence.
3214
+ */
3215
+ getSequence(name: string): SequenceInfo | undefined;
3216
+ /**
3217
+ * List all defined sequences.
3218
+ * Returns an array of sequence names.
3219
+ */
3220
+ listSequences(): string[];
3221
+ /**
3222
+ * Check if a symbol is a defined sequence.
3223
+ */
3224
+ isSequence(name: string): boolean;
3225
+ /**
3226
+ * Clear the memoization cache for a sequence.
3227
+ * If no name is provided, clears caches for all sequences.
3228
+ */
3229
+ clearSequenceCache(name?: string): void;
3230
+ /**
3231
+ * Get the memoization cache for a sequence.
3232
+ * Returns a Map of index → value, or `undefined` if not a sequence or memoization is disabled.
3233
+ *
3234
+ * For single-index sequences, keys are numbers.
3235
+ * For multi-index sequences, keys are comma-separated strings (e.g., '5,2').
3236
+ */
3237
+ getSequenceCache(name: string): Map<number | string, BoxedExpression> | undefined;
3238
+ /**
3239
+ * Generate a list of sequence terms from start to end (inclusive).
3240
+ *
3241
+ * @param name - The sequence name
3242
+ * @param start - Starting index (inclusive)
3243
+ * @param end - Ending index (inclusive)
3244
+ * @param step - Step size (default: 1)
3245
+ * @returns Array of BoxedExpressions, or undefined if not a sequence
3246
+ *
3247
+ * @example
3248
+ * ```typescript
3249
+ * ce.declareSequence('F', { base: { 0: 0, 1: 1 }, recurrence: 'F_{n-1} + F_{n-2}' });
3250
+ * ce.getSequenceTerms('F', 0, 10);
3251
+ * // → [0, 1, 1, 2, 3, 5, 8, 13, 21, 34, 55]
3252
+ * ```
3253
+ */
3254
+ getSequenceTerms(name: string, start: number, end: number, step?: number): BoxedExpression[] | undefined;
3255
+ /**
3256
+ * Look up sequences in OEIS by their terms.
3257
+ *
3258
+ * @param terms - Array of sequence terms to search for
3259
+ * @param options - Optional configuration (timeout, maxResults)
3260
+ * @returns Promise resolving to array of matching sequences
3261
+ *
3262
+ * @example
3263
+ * ```typescript
3264
+ * const results = await ce.lookupOEIS([0, 1, 1, 2, 3, 5, 8, 13]);
3265
+ * // → [{ id: 'A000045', name: 'Fibonacci numbers', ... }]
3266
+ * ```
3267
+ */
3268
+ lookupOEIS(terms: (number | BoxedExpression)[], options?: OEISOptions): Promise<OEISSequenceInfo[]>;
3269
+ /**
3270
+ * Check if a defined sequence matches an OEIS sequence.
3271
+ *
3272
+ * @param name - Name of the defined sequence
3273
+ * @param count - Number of terms to check (default: 10)
3274
+ * @param options - Optional configuration
3275
+ * @returns Promise with match results including OEIS matches and generated terms
3276
+ *
3277
+ * @example
3278
+ * ```typescript
3279
+ * ce.declareSequence('F', { base: { 0: 0, 1: 1 }, recurrence: 'F_{n-1} + F_{n-2}' });
3280
+ * const result = await ce.checkSequenceOEIS('F', 10);
3281
+ * // → { matches: [{ id: 'A000045', name: 'Fibonacci numbers', ... }], terms: [0, 1, 1, ...] }
3282
+ * ```
3283
+ */
3284
+ checkSequenceOEIS(name: string, count?: number, options?: OEISOptions): Promise<{
3285
+ matches: OEISSequenceInfo[];
3286
+ terms: number[];
3287
+ }>;
2956
3288
  forget(symbol?: MathJsonSymbol | MathJsonSymbol[]): void;
2957
3289
  ask(pattern: BoxedExpression): BoxedSubstitution[];
2958
- verify(query: BoxedExpression): boolean;
3290
+ verify(query: BoxedExpression): boolean | undefined;
2959
3291
  /** @internal */
2960
3292
  _shouldContinueExecution(): boolean;
2961
3293
  /** @internal */
@@ -1,19 +1,28 @@
1
- /* 0.33.0 */
1
+ /* 0.35.0 */
2
2
  import { Decimal } from 'decimal.js';
3
3
  import { Type, TypeResolver, TypeString } from '../common/type/types';
4
4
  import { BoxedType } from '../common/type/boxed-type';
5
5
  import type { OneOf } from '../common/one-of';
6
6
  import { ConfigurationChangeListener } from '../common/configuration-change';
7
7
  import type { Expression, MathJsonSymbol, MathJsonNumberObject } from '../math-json/types';
8
- import type { ValueDefinition, OperatorDefinition, AngularUnit, AssignValue, AssumeResult, BoxedExpression, BoxedRule, BoxedRuleSet, BoxedSubstitution, CanonicalOptions, SymbolDefinitions, Metadata, Rule, Scope, EvalContext, SemiBoxedExpression, ComputeEngine as IComputeEngine, BoxedDefinition, SymbolDefinition } from './global-types';
8
+ import type { ValueDefinition, OperatorDefinition, AngularUnit, AssignValue, AssumeResult, BoxedExpression, BoxedRule, BoxedRuleSet, BoxedSubstitution, CanonicalOptions, SymbolDefinitions, Metadata, Rule, Scope, EvalContext, SemiBoxedExpression, ComputeEngine as IComputeEngine, BoxedDefinition, SymbolDefinition, SequenceDefinition, SequenceStatus, SequenceInfo, OEISSequenceInfo, OEISOptions } from './global-types';
9
9
  import type { LatexDictionaryEntry, LatexString, LibraryCategory, ParseLatexOptions } from './latex-syntax/types';
10
10
  import { type IndexedLatexDictionary } from './latex-syntax/dictionary/definitions';
11
11
  import type { BigNum, Rational } from './numerics/types';
12
12
  import { ExactNumericValueData, NumericValue, NumericValueData } from './numeric-value/types';
13
13
  import { validatePattern } from './boxed-expression/boxed-patterns';
14
+ import { factor, factorPerfectSquare, factorDifferenceOfSquares, factorQuadratic, factorPolynomial } from './boxed-expression/factor';
14
15
  import './boxed-expression/serialize';
15
16
  export * from './global-types';
16
17
  export { validatePattern };
18
+ export { factor, factorPerfectSquare, factorDifferenceOfSquares, factorQuadratic, factorPolynomial, };
19
+ export type { CompileTarget, CompiledOperators, CompiledFunctions, CompilationOptions, CompiledExecutable, LanguageTarget, TargetSource, CompiledFunction, } from './compilation/types';
20
+ export { JavaScriptTarget } from './compilation/javascript-target';
21
+ export { GLSLTarget } from './compilation/glsl-target';
22
+ export { IntervalJavaScriptTarget } from './compilation/interval-javascript-target';
23
+ export { IntervalGLSLTarget } from './compilation/interval-glsl-target';
24
+ export { BaseCompiler } from './compilation/base-compiler';
25
+ import type { LanguageTarget } from './compilation/types';
17
26
  /**
18
27
  *
19
28
  * To use the Compute Engine, create a `ComputeEngine` instance:
@@ -101,6 +110,8 @@ export declare class ComputeEngine implements IComputeEngine {
101
110
  private _configurationChangeTracker;
102
111
  /** @internal */
103
112
  private _cost?;
113
+ /** @internal Registry of compilation targets */
114
+ private _compilationTargets;
104
115
  /** @internal */
105
116
  private _commonSymbols;
106
117
  /** @internal */
@@ -240,6 +251,43 @@ export declare class ComputeEngine implements IComputeEngine {
240
251
  _reset(): void;
241
252
  /** @internal */
242
253
  listenToConfigurationChange(tracker: ConfigurationChangeListener): () => void;
254
+ /**
255
+ * Register a custom compilation target.
256
+ *
257
+ * This allows you to compile mathematical expressions to different target
258
+ * languages beyond the built-in JavaScript and GLSL targets.
259
+ *
260
+ * @param name - The name of the target (e.g., 'python', 'wgsl', 'matlab')
261
+ * @param target - The LanguageTarget implementation
262
+ *
263
+ * @example
264
+ * ```typescript
265
+ * import { ComputeEngine, GLSLTarget } from '@cortex-js/compute-engine';
266
+ *
267
+ * const ce = new ComputeEngine();
268
+ *
269
+ * // Register a custom target
270
+ * class PythonTarget implements LanguageTarget {
271
+ * // Implementation...
272
+ * }
273
+ *
274
+ * ce.registerCompilationTarget('python', new PythonTarget());
275
+ *
276
+ * // Use the custom target
277
+ * const expr = ce.parse('x^2 + y^2');
278
+ * const code = expr.compile({ to: 'python' });
279
+ * ```
280
+ */
281
+ registerCompilationTarget(name: string, target: LanguageTarget): void;
282
+ /**
283
+ * Get a registered compilation target by name.
284
+ *
285
+ * @param name - The name of the target (e.g., 'javascript', 'glsl')
286
+ * @returns The LanguageTarget implementation, or undefined if not found
287
+ *
288
+ * @internal
289
+ */
290
+ _getCompilationTarget(name: string): LanguageTarget | undefined;
243
291
  get precision(): number;
244
292
  /** The precision, or number of significant digits, of numeric
245
293
  * calculations.
@@ -298,6 +346,35 @@ export declare class ComputeEngine implements IComputeEngine {
298
346
  get recursionLimit(): number;
299
347
  set recursionLimit(t: number);
300
348
  private _recursionLimit;
349
+ /**
350
+ * Flag to prevent infinite recursion in the verify/ask/equality checking cycle.
351
+ *
352
+ * **The Problem:**
353
+ * When verifying equality predicates, a recursion loop can occur:
354
+ * 1. `verify(Equal(x, 0))` evaluates the expression
355
+ * 2. `Equal.evaluate()` calls `eq(x, 0)` to check equality
356
+ * 3. `eq()` calls `ask(['NotEqual', x, 0])` to check assumptions
357
+ * 4. `ask()` calls `verify(NotEqual(x, 0))` as a fallback
358
+ * 5. `verify()` evaluates, calling `eq()` again → infinite loop
359
+ *
360
+ * **The Solution:**
361
+ * - Set `_isVerifying = true` when entering `verify()`
362
+ * - `ask()` skips the `verify()` fallback when `_isVerifying` is true
363
+ * - `Equal/NotEqual` evaluate handlers check this flag to preserve 3-valued
364
+ * logic in verification mode while still returning False/True in normal mode
365
+ *
366
+ * @see verify() in index.ts
367
+ * @see ask() in index.ts
368
+ * @see eq() in compare.ts
369
+ * @see Equal/NotEqual operators in relational-operator.ts
370
+ */
371
+ private _isVerifying;
372
+ /**
373
+ * @internal
374
+ * Indicates whether we're currently inside a verify() call.
375
+ * Used to prevent recursion and to enable 3-valued logic in verification mode.
376
+ */
377
+ get isVerifying(): boolean;
301
378
  get tolerance(): number;
302
379
  /**
303
380
  * Values smaller than the tolerance are considered to be zero for the
@@ -473,6 +550,13 @@ export declare class ComputeEngine implements IComputeEngine {
473
550
  * @internal
474
551
  */
475
552
  _setSymbolValue(id: MathJsonSymbol, value: BoxedExpression | boolean | number | undefined): void;
553
+ /**
554
+ * Set a value directly in the current context's values map.
555
+ * This is used for assumptions so that the value is scoped to the current
556
+ * evaluation context and is automatically removed when the scope is popped.
557
+ * @internal
558
+ */
559
+ _setCurrentContextValue(id: MathJsonSymbol, value: BoxedExpression | boolean | number | undefined): void;
476
560
  /**
477
561
  * Declare a symbol in the current lexical scope: specify their type and
478
562
  * other attributes, including optionally a value.
@@ -491,6 +575,107 @@ export declare class ComputeEngine implements IComputeEngine {
491
575
  declare(symbols: {
492
576
  [id: string]: Type | TypeString | Partial<SymbolDefinition>;
493
577
  }): IComputeEngine;
578
+ /**
579
+ * Declare a sequence with a recurrence relation.
580
+ *
581
+ * @example
582
+ * ```typescript
583
+ * // Fibonacci sequence
584
+ * ce.declareSequence('F', {
585
+ * base: { 0: 0, 1: 1 },
586
+ * recurrence: 'F_{n-1} + F_{n-2}',
587
+ * });
588
+ * ce.parse('F_{10}').evaluate(); // → 55
589
+ * ```
590
+ */
591
+ declareSequence(name: string, def: SequenceDefinition): IComputeEngine;
592
+ /**
593
+ * Get the status of a sequence definition.
594
+ *
595
+ * @example
596
+ * ```typescript
597
+ * ce.parse('F_0 := 0').evaluate();
598
+ * ce.getSequenceStatus('F');
599
+ * // → { status: 'pending', hasBase: true, hasRecurrence: false, baseIndices: [0] }
600
+ * ```
601
+ */
602
+ getSequenceStatus(name: string): SequenceStatus;
603
+ /**
604
+ * Get information about a defined sequence.
605
+ * Returns `undefined` if the symbol is not a sequence.
606
+ */
607
+ getSequence(name: string): SequenceInfo | undefined;
608
+ /**
609
+ * List all defined sequences.
610
+ */
611
+ listSequences(): string[];
612
+ /**
613
+ * Check if a symbol is a defined sequence.
614
+ */
615
+ isSequence(name: string): boolean;
616
+ /**
617
+ * Clear the memoization cache for a sequence.
618
+ * If no name is provided, clears caches for all sequences.
619
+ */
620
+ clearSequenceCache(name?: string): void;
621
+ /**
622
+ * Get the memoization cache for a sequence.
623
+ * Returns a Map of index → value, or `undefined` if not a sequence or memoization is disabled.
624
+ *
625
+ * For single-index sequences, keys are numbers.
626
+ * For multi-index sequences, keys are comma-separated strings (e.g., '5,2').
627
+ */
628
+ getSequenceCache(name: string): Map<number | string, BoxedExpression> | undefined;
629
+ /**
630
+ * Generate a list of sequence terms from start to end (inclusive).
631
+ *
632
+ * @param name - The sequence name
633
+ * @param start - Starting index (inclusive)
634
+ * @param end - Ending index (inclusive)
635
+ * @param step - Step size (default: 1)
636
+ * @returns Array of BoxedExpressions, or undefined if not a sequence
637
+ *
638
+ * @example
639
+ * ```typescript
640
+ * ce.declareSequence('F', { base: { 0: 0, 1: 1 }, recurrence: 'F_{n-1} + F_{n-2}' });
641
+ * ce.getSequenceTerms('F', 0, 10);
642
+ * // → [0, 1, 1, 2, 3, 5, 8, 13, 21, 34, 55]
643
+ * ```
644
+ */
645
+ getSequenceTerms(name: string, start: number, end: number, step?: number): BoxedExpression[] | undefined;
646
+ /**
647
+ * Look up sequences in OEIS by their terms.
648
+ *
649
+ * @param terms - Array of sequence terms to search for
650
+ * @param options - Optional configuration (timeout, maxResults)
651
+ * @returns Promise resolving to array of matching sequences
652
+ *
653
+ * @example
654
+ * ```typescript
655
+ * const results = await ce.lookupOEIS([0, 1, 1, 2, 3, 5, 8, 13]);
656
+ * // → [{ id: 'A000045', name: 'Fibonacci numbers', ... }]
657
+ * ```
658
+ */
659
+ lookupOEIS(terms: (number | BoxedExpression)[], options?: OEISOptions): Promise<OEISSequenceInfo[]>;
660
+ /**
661
+ * Check if a defined sequence matches an OEIS sequence.
662
+ *
663
+ * @param name - Name of the defined sequence
664
+ * @param count - Number of terms to check (default: 10)
665
+ * @param options - Optional configuration
666
+ * @returns Promise with match results including OEIS matches and generated terms
667
+ *
668
+ * @example
669
+ * ```typescript
670
+ * ce.declareSequence('F', { base: { 0: 0, 1: 1 }, recurrence: 'F_{n-1} + F_{n-2}' });
671
+ * const result = await ce.checkSequenceOEIS('F', 10);
672
+ * // → { matches: [{ id: 'A000045', name: 'Fibonacci numbers', ... }], terms: [0, 1, 1, ...] }
673
+ * ```
674
+ */
675
+ checkSequenceOEIS(name: string, count?: number, options?: OEISOptions): Promise<{
676
+ matches: OEISSequenceInfo[];
677
+ terms: number[];
678
+ }>;
494
679
  /**
495
680
  * Return an evaluation context in which the symbol is defined.
496
681
  */
@@ -632,7 +817,7 @@ export declare class ComputeEngine implements IComputeEngine {
632
817
  * Answer a query based on the current assumptions.
633
818
  *
634
819
  */
635
- verify(_query: BoxedExpression): boolean;
820
+ verify(query: BoxedExpression): boolean | undefined;
636
821
  /**
637
822
  * Add an assumption.
638
823
  *