@cortex-js/compute-engine 0.12.2 → 0.12.4

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 (90) hide show
  1. package/README.md +2 -2
  2. package/dist/compute-engine.esm.js +24348 -28196
  3. package/dist/compute-engine.js +24523 -0
  4. package/dist/compute-engine.min.esm.js +24506 -2
  5. package/dist/compute-engine.min.js +24519 -2
  6. package/dist/math-json.esm.js +256 -185
  7. package/dist/math-json.js +283 -0
  8. package/dist/math-json.min.esm.js +258 -2
  9. package/dist/math-json.min.js +283 -2
  10. package/dist/types/common/grapheme-splitter.d.ts +1 -1
  11. package/dist/types/common/signals.d.ts +1 -1
  12. package/dist/types/common/utils.d.ts +1 -1
  13. package/dist/types/compute-engine/assume.d.ts +1 -1
  14. package/dist/types/compute-engine/boxed-expression/abstract-boxed-expression.d.ts +3 -1
  15. package/dist/types/compute-engine/boxed-expression/box.d.ts +1 -1
  16. package/dist/types/compute-engine/boxed-expression/boxed-dictionary.d.ts +1 -1
  17. package/dist/types/compute-engine/boxed-expression/boxed-domain.d.ts +3 -2
  18. package/dist/types/compute-engine/boxed-expression/boxed-function-definition.d.ts +1 -1
  19. package/dist/types/compute-engine/boxed-expression/boxed-function.d.ts +3 -2
  20. package/dist/types/compute-engine/boxed-expression/boxed-number.d.ts +2 -2
  21. package/dist/types/compute-engine/boxed-expression/boxed-patterns.d.ts +1 -1
  22. package/dist/types/compute-engine/boxed-expression/boxed-string.d.ts +1 -1
  23. package/dist/types/compute-engine/boxed-expression/boxed-symbol-definition.d.ts +1 -1
  24. package/dist/types/compute-engine/boxed-expression/boxed-symbol.d.ts +1 -1
  25. package/dist/types/compute-engine/boxed-expression/expression-map.d.ts +1 -1
  26. package/dist/types/compute-engine/boxed-expression/order.d.ts +1 -1
  27. package/dist/types/compute-engine/boxed-expression/serialize.d.ts +1 -1
  28. package/dist/types/compute-engine/boxed-expression/utils.d.ts +1 -1
  29. package/dist/types/compute-engine/boxed-expression/validate.d.ts +1 -1
  30. package/dist/types/compute-engine/compute-engine.d.ts +1 -2
  31. package/dist/types/compute-engine/cost-function.d.ts +1 -1
  32. package/dist/types/compute-engine/domain-utils.d.ts +1 -1
  33. package/dist/types/compute-engine/latex-syntax/dictionary/definitions-algebra.d.ts +1 -1
  34. package/dist/types/compute-engine/latex-syntax/dictionary/definitions-arithmetic.d.ts +1 -1
  35. package/dist/types/compute-engine/latex-syntax/dictionary/definitions-calculus.d.ts +1 -1
  36. package/dist/types/compute-engine/latex-syntax/dictionary/definitions-core.d.ts +1 -1
  37. package/dist/types/compute-engine/latex-syntax/dictionary/definitions-inequalities.d.ts +1 -1
  38. package/dist/types/compute-engine/latex-syntax/dictionary/definitions-logic.d.ts +1 -1
  39. package/dist/types/compute-engine/latex-syntax/dictionary/definitions-other.d.ts +1 -1
  40. package/dist/types/compute-engine/latex-syntax/dictionary/definitions-sets.d.ts +1 -1
  41. package/dist/types/compute-engine/latex-syntax/dictionary/definitions-symbols.d.ts +2 -1
  42. package/dist/types/compute-engine/latex-syntax/dictionary/definitions-trigonometry.d.ts +1 -1
  43. package/dist/types/compute-engine/latex-syntax/dictionary/definitions.d.ts +25 -16
  44. package/dist/types/compute-engine/latex-syntax/latex-syntax.d.ts +1 -1
  45. package/dist/types/compute-engine/latex-syntax/parse-identifier.d.ts +15 -0
  46. package/dist/types/compute-engine/latex-syntax/parse.d.ts +28 -33
  47. package/dist/types/compute-engine/latex-syntax/public.d.ts +62 -65
  48. package/dist/types/compute-engine/latex-syntax/serialize-number.d.ts +1 -1
  49. package/dist/types/compute-engine/latex-syntax/serializer-style.d.ts +1 -1
  50. package/dist/types/compute-engine/latex-syntax/serializer.d.ts +10 -6
  51. package/dist/types/compute-engine/latex-syntax/tokenizer.d.ts +2 -1
  52. package/dist/types/compute-engine/library/arithmetic-add.d.ts +3 -2
  53. package/dist/types/compute-engine/library/arithmetic-divide.d.ts +1 -1
  54. package/dist/types/compute-engine/library/arithmetic-multiply.d.ts +2 -2
  55. package/dist/types/compute-engine/library/arithmetic-power.d.ts +1 -1
  56. package/dist/types/compute-engine/library/arithmetic.d.ts +1 -1
  57. package/dist/types/compute-engine/library/calculus.d.ts +1 -1
  58. package/dist/types/compute-engine/library/collections.d.ts +1 -1
  59. package/dist/types/compute-engine/library/core.d.ts +1 -1
  60. package/dist/types/compute-engine/library/domains.d.ts +1 -1
  61. package/dist/types/compute-engine/library/library.d.ts +1 -1
  62. package/dist/types/compute-engine/library/logic.d.ts +1 -1
  63. package/dist/types/compute-engine/library/polynomials.d.ts +1 -1
  64. package/dist/types/compute-engine/library/random-expression.d.ts +1 -1
  65. package/dist/types/compute-engine/library/relational-operator.d.ts +1 -1
  66. package/dist/types/compute-engine/library/sets.d.ts +1 -1
  67. package/dist/types/compute-engine/library/trigonometry.d.ts +1 -1
  68. package/dist/types/compute-engine/library/utils.d.ts +1 -1
  69. package/dist/types/compute-engine/numerics/numeric-bigint.d.ts +1 -1
  70. package/dist/types/compute-engine/numerics/numeric-bignum.d.ts +1 -1
  71. package/dist/types/compute-engine/numerics/numeric-complex.d.ts +1 -1
  72. package/dist/types/compute-engine/numerics/numeric.d.ts +1 -1
  73. package/dist/types/compute-engine/numerics/primes.d.ts +1 -1
  74. package/dist/types/compute-engine/numerics/rationals.d.ts +1 -1
  75. package/dist/types/compute-engine/public.d.ts +17 -7
  76. package/dist/types/compute-engine/rules.d.ts +1 -1
  77. package/dist/types/compute-engine/simplify-rules.d.ts +1 -1
  78. package/dist/types/compute-engine/solve.d.ts +1 -1
  79. package/dist/types/compute-engine/symbolic/expand.d.ts +18 -10
  80. package/dist/types/compute-engine/symbolic/flatten.d.ts +1 -1
  81. package/dist/types/compute-engine/symbolic/negate.d.ts +1 -1
  82. package/dist/types/compute-engine/symbolic/polynomials.d.ts +1 -1
  83. package/dist/types/compute-engine/symbolic/product.d.ts +4 -1
  84. package/dist/types/compute-engine/symbolic/sum.d.ts +2 -1
  85. package/dist/types/compute-engine/symbolic/utils.d.ts +1 -1
  86. package/dist/types/compute-engine.d.ts +2 -2
  87. package/dist/types/math-json/math-json-format.d.ts +1 -1
  88. package/dist/types/math-json/utils.d.ts +4 -1
  89. package/dist/types/math-json.d.ts +2 -2
  90. package/package.json +5 -14
@@ -1,4 +1,4 @@
1
- /* 0.12.2 */
1
+ /* 0.12.4 */
2
2
  * The most important classes are {@link ComputeEngine} and
3
3
  * {@link BoxedExpression}.
4
4
  *
@@ -36,7 +36,7 @@ export type Metadata = {
36
36
  */
37
37
  export type NumericMode = 'auto' | 'machine' | 'bignum' | 'complex';
38
38
  /** Options for `BoxedExpression.simplify()` */
39
- export type SimplifyOptions = EvaluateOptions & {
39
+ export type SimplifyOptions = {
40
40
  recursive?: boolean;
41
41
  rules?: BoxedRuleSet;
42
42
  };
@@ -126,9 +126,9 @@ export type BoxedRule = [
126
126
  export type BoxedRuleSet = Set<BoxedRule>;
127
127
  export type DomainCompatibility = 'covariant' | 'contravariant' | 'bivariant' | 'invariant';
128
128
  /** A domain constructor is the head of a domain expression. */
129
- export type DomainConstructor = 'Error' | 'Matrix' | 'SquareMatrix' | 'Vector' | 'Function' | 'List' | 'Dictionary' | 'Tuple' | 'Range' | 'Interval' | 'Intersection' | 'Union' | 'Maybe' | 'Sequence' | 'Head' | 'Symbol' | 'Value' | 'Covariant' | 'Contravariant' | 'Bivariant' | 'Invariant';
129
+ export type DomainConstructor = 'InvalidDomain' | 'Matrix' | 'SquareMatrix' | 'Vector' | 'Function' | 'List' | 'Dictionary' | 'Tuple' | 'Range' | 'Interval' | 'Intersection' | 'Union' | 'Maybe' | 'Sequence' | 'Head' | 'Symbol' | 'Value' | 'Covariant' | 'Contravariant' | 'Bivariant' | 'Invariant';
130
130
  export type DomainLiteral = string;
131
- export type DomainExpression<T = SemiBoxedExpression> = DomainLiteral | [DomainConstructor, ...(string | T | DomainExpression<T>)[]] | ['Error', T] | ['Error', T, T] | ['Union', ...DomainExpression<T>[]] | ['Intersection', ...DomainExpression<T>[]] | ['Matrix', DomainExpression<T>, T, T] | ['SquareMatrix', DomainExpression<T>, T] | ['Vector', DomainExpression<T>, T] | ['List', DomainExpression<T>] | ['Dictionary', DomainExpression<T>] | ['Tuple', ...DomainExpression<T>[]] | ['Maybe', DomainExpression<T>] | ['Sequence', DomainExpression<T>] | ['Range'] | ['Range', T] | ['Range', T, T] | ['Range', T, T, T] | ['Interval', T, T] | ['Interval', ['Open', T], T] | ['Interval', T, ['Open', T]] | ['Interval', ['Open', T], ['Open', T]] | ['Value', T] | ['Head', string] | ['Symbol', string] | ['Covariant', DomainExpression<T>] | ['Contravariant', DomainExpression<T>] | ['Bivariant', DomainExpression<T>] | ['Invariant', DomainExpression<T>] | ['Function', ...DomainExpression<T>[]];
131
+ export type DomainExpression<T = SemiBoxedExpression> = DomainLiteral | [DomainConstructor, ...(string | T | DomainExpression<T>)[]] | ['InvalidDomain', T] | ['Union', ...DomainExpression<T>[]] | ['Intersection', ...DomainExpression<T>[]] | ['Matrix', DomainExpression<T>, T, T] | ['SquareMatrix', DomainExpression<T>, T] | ['Vector', DomainExpression<T>, T] | ['List', DomainExpression<T>] | ['Dictionary', DomainExpression<T>] | ['Tuple', ...DomainExpression<T>[]] | ['Maybe', DomainExpression<T>] | ['Sequence', DomainExpression<T>] | ['Range'] | ['Range', T] | ['Range', T, T] | ['Range', T, T, T] | ['Interval', T, T] | ['Interval', ['Open', T], T] | ['Interval', T, ['Open', T]] | ['Interval', ['Open', T], ['Open', T]] | ['Value', T] | ['Head', string] | ['Symbol', string] | ['Covariant', DomainExpression<T>] | ['Contravariant', DomainExpression<T>] | ['Bivariant', DomainExpression<T>] | ['Invariant', DomainExpression<T>] | ['Function', ...DomainExpression<T>[]];
132
132
  export interface BoxedDomain extends BoxedExpression {
133
133
  is(s: BoxedDomain): boolean;
134
134
  /** True if a valid domain, and compatible with `dom` */
@@ -247,7 +247,10 @@ export interface BoxedExpression {
247
247
  * @category Primitive Methods
248
248
  */
249
249
  valueOf(): number | string | boolean;
250
- /** From `Object.toString()`, return a LaTeX representation of the expression.
250
+ /** From `Object.toString()`, return a string representation of the
251
+ * expression. This string is suitable to be output to the console
252
+ * for debugging, for example. To get a LaTeX representation of the
253
+ * expression, use `expr.latex`.
251
254
  *
252
255
  * Used when coercing a `BoxedExpression` to a `String`.
253
256
  *
@@ -281,6 +284,9 @@ export interface BoxedExpression {
281
284
  *
282
285
  */
283
286
  readonly json: Expression;
287
+ /** For debugging, the raw MathJSON representation of this expression without decanonicalization.
288
+ * @internal */
289
+ readonly rawJson: Expression;
284
290
  /**
285
291
  * The scope in which this expression has been defined.
286
292
  * Is null when the expression is not canonical.
@@ -838,7 +844,9 @@ export interface BoxedExpression {
838
844
  * The result is in canonical form.
839
845
  *
840
846
  */
841
- evaluate(options?: EvaluateOptions): BoxedExpression;
847
+ evaluate(ids?: {
848
+ [id: string]: SemiBoxedExpression | null | undefined;
849
+ }): BoxedExpression;
842
850
  /** Return a numeric approximation of the canonical form of this expression.
843
851
  *
844
852
  * Any necessary calculations, including on decimal numbers (non-integers),
@@ -1731,7 +1739,9 @@ export interface IComputeEngine {
1731
1739
  pushScope(identifiers?: Readonly<IdTable> | Readonly<IdTable>[], scope?: Partial<Scope>): void;
1732
1740
  popScope(): void;
1733
1741
  /** Assign a value to an identifier in the current scope. Use `null` to reset the identifier to no value */
1734
- set(identifiers: Substitution<SemiBoxedExpression | null | undefined>): void;
1742
+ set(ids: {
1743
+ [id: string]: SemiBoxedExpression | null | undefined;
1744
+ }): void;
1735
1745
  /** Declare identifiers (specify their domain without necessarily assigning them a value in the current scope) */
1736
1746
  let(identifiers: IdTable): void;
1737
1747
  /**
@@ -1,4 +1,4 @@
1
- /* 0.12.2 */
1
+ /* 0.12.4 */
2
2
  /**
3
3
  * Go through all the rules in the rule set, and for all the rules that match
4
4
  * return the rhs of the rule applied to `expr`.
@@ -1,4 +1,4 @@
1
- /* 0.12.2 */
1
+ /* 0.12.4 */
2
2
  /**
3
3
  * A set of simplification rules.
4
4
  *
@@ -1,4 +1,4 @@
1
- /* 0.12.2 */
1
+ /* 0.12.4 */
2
2
  export declare const UNIVARIATE_ROOTS: Rule[];
3
3
  /**
4
4
  * Expression is a function of a single variable (`x`) or an Equality
@@ -1,11 +1,19 @@
1
- /* 0.12.2 */
2
- /**
3
- * Return the expansion of ['Multiply', lhs, rhs]
4
- * - lhs = 'a + b', rhs = '2'
5
- * -> '2a + 2b'
6
- * - lhs = 'a + b', rhs = 'a + c'
7
- * -> 'a^2 + ac + ab + bc'
1
+ /* 0.12.4 */
2
+ export declare function distribute(expr: BoxedExpression[]): BoxedExpression;
3
+ /** Use the multinomial theorem (https://en.wikipedia.org/wiki/Multinomial_theorem) to expand the expression.
4
+ * The expression must be a power of a sum of terms.
5
+ * The power must be a positive integer.
6
+ * - expr = '(a + b)^2'
7
+ * -> 'a^2 + 2ab + b^2'
8
+ * - expr = '(a + b)^3'
9
+ * -> 'a^3 + 3a^2b + 3ab^2 + b^3'
8
10
  */
9
- export declare function expand2(lhs: BoxedExpression, rhs: BoxedExpression): BoxedExpression;
10
- export declare function expandN(expr: BoxedExpression, n: number): BoxedExpression;
11
- export declare function expand(expr: BoxedExpression): BoxedExpression;
11
+ export declare function expandMultinomial(expr: BoxedExpression): BoxedExpression | null;
12
+ /** Apply the distributive law if the expression is a product of sums.
13
+ * For example, a(b + c) = ab + ac
14
+ * Expand the expression if it is a power of a sum.
15
+ * Expand the terms of the expression if it is a sum or negate.
16
+ * If the expression is a fraction, expand the numerator.
17
+ * Return null if the expression cannot be expanded.
18
+ */
19
+ export declare function expand(expr: BoxedExpression | undefined): BoxedExpression | null;
@@ -1,4 +1,4 @@
1
- /* 0.12.2 */
1
+ /* 0.12.4 */
2
2
  /**
3
3
  * Flatten the arguments.
4
4
  * If `expr` was canonical, the result it canonical.
@@ -1,4 +1,4 @@
1
- /* 0.12.2 */
1
+ /* 0.12.4 */
2
2
  /**
3
3
  * Distribute `Negate` (multiply by -1) if expr is a number literal, an
4
4
  * addition or multiplication or another `Negate`.
@@ -1,4 +1,4 @@
1
- /* 0.12.2 */
1
+ /* 0.12.4 */
2
2
  /**
3
3
  * Coefficient of a univariate (single variable) polynomial.
4
4
  *
@@ -1,4 +1,4 @@
1
- /* 0.12.2 */
1
+ /* 0.12.4 */
2
2
  /**
3
3
  * Group terms in a product by common term.
4
4
  *
@@ -16,6 +16,9 @@
16
16
  *
17
17
  */
18
18
  export declare class Product {
19
+ readonly options?: {
20
+ canonical?: boolean;
21
+ };
19
22
  engine: IComputeEngine;
20
23
  private _sign;
21
24
  private _rational;
@@ -1,4 +1,4 @@
1
- /* 0.12.2 */
1
+ /* 0.12.4 */
2
2
  export declare class Sum {
3
3
  private engine;
4
4
  private _isCanonical;
@@ -27,6 +27,7 @@ export declare class Sum {
27
27
  * -> [['x', [3, 1]], ['y', [1, 5]]]
28
28
  */
29
29
  addTerm(term: BoxedExpression, c?: Rational): void;
30
+ toString(): string;
30
31
  terms(mode: 'expression' | 'numeric'): BoxedExpression[];
31
32
  asExpression(mode: 'expression' | 'numeric'): BoxedExpression;
32
33
  }
@@ -1,4 +1,4 @@
1
- /* 0.12.2 */
1
+ /* 0.12.4 */
2
2
  import Decimal from 'decimal.js';
3
3
  import { BoxedExpression, Rational } from '../public';
4
4
  /**
@@ -1,3 +1,3 @@
1
- /* 0.12.2 */
2
- export declare const version = "0.12.2";
1
+ /* 0.12.4 */
2
+ export declare const version = "0.12.4";
3
3
  export { ComputeEngine } from './compute-engine/compute-engine';
@@ -1,4 +1,4 @@
1
- /* 0.12.2 */
1
+ /* 0.12.4 */
2
2
  /** A human readable string to annotate this expression, since JSON does not
3
3
  * allow comments in its encoding */
4
4
  comment?: string;
@@ -1,4 +1,5 @@
1
- /* 0.12.2 */
1
+ /* 0.12.4 */
2
+ export declare const MISSING: Expression;
2
3
  export declare function isNumberExpression(expr: Expression | null): expr is number | string | MathJsonNumber;
3
4
  export declare function isNumberObject(expr: Expression | null): expr is MathJsonNumber;
4
5
  export declare function isSymbolObject(expr: Expression | null): expr is MathJsonSymbol;
@@ -6,6 +7,8 @@ export declare function isStringObject(expr: Expression | null): expr is MathJso
6
7
  export declare function isFunctionObject(expr: Expression | null): expr is MathJsonFunction;
7
8
  export declare function isDictionaryObject(expr: Expression): expr is MathJsonNumber;
8
9
  export declare function isValidIdentifier(s: string): boolean;
10
+ export declare const ONLY_EMOJIS: RegExp;
11
+ export declare function validateIdentifier(s: unknown): 'valid' | 'not-a-string' | 'empty-string' | 'expected-nfc' | 'unexpected-mixed-emoji' | 'unexpected-bidi-marker' | 'unexpected-script' | 'invalid-first-char' | 'invalid-char';
9
12
  /** If expr is a string literal, return it.
10
13
  *
11
14
  * A string literal is a JSON string that begins and ends with
@@ -1,3 +1,3 @@
1
- /* 0.12.2 */
1
+ /* 0.12.4 */
2
2
  export { isSymbolObject, isStringObject, isFunctionObject, isDictionaryObject, stringValue as getStringValue, head, headName, symbol, applyRecursively, mapArgs, op, nops, dictionary as getDictionary, } from './math-json/utils';
3
- export declare const version = "0.12.2";
3
+ export declare const version = "0.12.4";
package/package.json CHANGED
@@ -1,5 +1,5 @@
1
1
  {
2
- "version": "0.12.2",
2
+ "version": "0.12.4",
3
3
  "scripts": {
4
4
  "build": "bash ./scripts/build.sh",
5
5
  "clean": "bash ./scripts/clean.sh",
@@ -48,35 +48,26 @@
48
48
  "devDependencies": {
49
49
  "@cortex-js/prettier-config": "^1.1.1",
50
50
  "@jest/console": "^29.0.3",
51
- "@rollup/plugin-commonjs": "^22.0.2",
52
- "@rollup/plugin-node-resolve": "^14.0.1",
53
51
  "@types/jest": "^29.0.0",
54
- "@types/node": "^18.7.16",
52
+ "@types/node": "^20.3.2",
55
53
  "@typescript-eslint/eslint-plugin": "^5.36.2",
56
54
  "@typescript-eslint/parser": "^5.36.2",
57
55
  "@typescript-eslint/typescript-estree": "^5.36.2",
58
56
  "chalk": "^5.0.1",
59
57
  "check-node-version": "^4.2.1",
60
- "child_process": "^1.0.2",
61
- "chokidar": "^3.5.3",
58
+ "esbuild": "^0.18.10",
62
59
  "eslint": "^8.23.0",
63
60
  "eslint-config-prettier": "^8.5.0",
64
61
  "eslint-plugin-import": "^2.26.0",
65
62
  "eslint-plugin-no-unsanitized": "^4.0.1",
66
63
  "eslint-plugin-prettier": "^4.2.1",
67
- "estrella": "^1.4.1",
68
64
  "jest": "^29.0.3",
69
65
  "jest-silent-reporter": "^0.5.0",
70
- "open": "^8.4.0",
66
+ "open": "^9.1.0",
71
67
  "prettier": "^2.7.1",
72
- "rollup": "^2.79.0",
73
- "rollup-plugin-copy": "^3.4.0",
74
- "rollup-plugin-eslint": "^7.0.0",
75
- "rollup-plugin-terser": "^7.0.2",
76
- "rollup-plugin-typescript2": "latest",
77
68
  "serve-http": "^1.0.7",
78
69
  "ts-jest": "^29.0.0",
79
- "typescript": "^4.8.3",
70
+ "typescript": "^5.1.5",
80
71
  "update-notifier": "^6.0.2"
81
72
  },
82
73
  "prettier": "@cortex-js/prettier-config",