@cortex-js/compute-engine 0.4.3 → 0.4.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 (80) hide show
  1. package/README.md +2 -3
  2. package/dist/compute-engine.esm.js +63 -44
  3. package/dist/compute-engine.min.esm.js +2 -2
  4. package/dist/compute-engine.min.js +2 -2
  5. package/dist/math-json.esm.js +23 -16
  6. package/dist/math-json.min.esm.js +2 -2
  7. package/dist/math-json.min.js +2 -2
  8. package/dist/types/common/grapheme-splitter.d.ts +1 -1
  9. package/dist/types/common/signals.d.ts +1 -1
  10. package/dist/types/compute-engine/assume.d.ts +1 -1
  11. package/dist/types/compute-engine/boxed-expression/abstract-boxed-expression.d.ts +3 -3
  12. package/dist/types/compute-engine/boxed-expression/box.d.ts +1 -1
  13. package/dist/types/compute-engine/boxed-expression/boxed-dictionary.d.ts +1 -1
  14. package/dist/types/compute-engine/boxed-expression/boxed-domain.d.ts +1 -1
  15. package/dist/types/compute-engine/boxed-expression/boxed-function-definition.d.ts +1 -1
  16. package/dist/types/compute-engine/boxed-expression/boxed-function.d.ts +1 -1
  17. package/dist/types/compute-engine/boxed-expression/boxed-number.d.ts +1 -1
  18. package/dist/types/compute-engine/boxed-expression/boxed-patterns.d.ts +1 -1
  19. package/dist/types/compute-engine/boxed-expression/boxed-string.d.ts +1 -1
  20. package/dist/types/compute-engine/boxed-expression/boxed-symbol-definition.d.ts +2 -2
  21. package/dist/types/compute-engine/boxed-expression/boxed-symbol.d.ts +3 -3
  22. package/dist/types/compute-engine/boxed-expression/expression-map.d.ts +1 -1
  23. package/dist/types/compute-engine/boxed-expression/order.d.ts +2 -1
  24. package/dist/types/compute-engine/boxed-expression/serialize.d.ts +1 -1
  25. package/dist/types/compute-engine/boxed-expression/utils.d.ts +1 -1
  26. package/dist/types/compute-engine/compute-engine.d.ts +12 -7
  27. package/dist/types/compute-engine/cost-function.d.ts +1 -1
  28. package/dist/types/compute-engine/dictionary/arithmetic-add.d.ts +1 -1
  29. package/dist/types/compute-engine/dictionary/arithmetic-divide.d.ts +1 -1
  30. package/dist/types/compute-engine/dictionary/arithmetic-multiply.d.ts +1 -1
  31. package/dist/types/compute-engine/dictionary/arithmetic-power.d.ts +1 -1
  32. package/dist/types/compute-engine/dictionary/arithmetic.d.ts +1 -1
  33. package/dist/types/compute-engine/dictionary/collections.d.ts +1 -1
  34. package/dist/types/compute-engine/dictionary/core.d.ts +1 -1
  35. package/dist/types/compute-engine/dictionary/dictionary.d.ts +1 -1
  36. package/dist/types/compute-engine/dictionary/logic.d.ts +1 -1
  37. package/dist/types/compute-engine/dictionary/polynomials.d.ts +1 -1
  38. package/dist/types/compute-engine/dictionary/relational-operator.d.ts +1 -1
  39. package/dist/types/compute-engine/dictionary/sets.d.ts +1 -1
  40. package/dist/types/compute-engine/dictionary/trigonometry.d.ts +1 -1
  41. package/dist/types/compute-engine/domain-utils.d.ts +1 -1
  42. package/dist/types/compute-engine/latex-syntax/dictionary/definitions-algebra.d.ts +1 -1
  43. package/dist/types/compute-engine/latex-syntax/dictionary/definitions-arithmetic.d.ts +1 -1
  44. package/dist/types/compute-engine/latex-syntax/dictionary/definitions-calculus.d.ts +1 -1
  45. package/dist/types/compute-engine/latex-syntax/dictionary/definitions-core.d.ts +1 -1
  46. package/dist/types/compute-engine/latex-syntax/dictionary/definitions-inequalities.d.ts +1 -1
  47. package/dist/types/compute-engine/latex-syntax/dictionary/definitions-logic.d.ts +1 -1
  48. package/dist/types/compute-engine/latex-syntax/dictionary/definitions-other.d.ts +1 -1
  49. package/dist/types/compute-engine/latex-syntax/dictionary/definitions-sets.d.ts +1 -1
  50. package/dist/types/compute-engine/latex-syntax/dictionary/definitions-symbols.d.ts +1 -1
  51. package/dist/types/compute-engine/latex-syntax/dictionary/definitions-trigonometry.d.ts +1 -1
  52. package/dist/types/compute-engine/latex-syntax/dictionary/definitions.d.ts +1 -1
  53. package/dist/types/compute-engine/latex-syntax/latex-syntax.d.ts +2 -2
  54. package/dist/types/compute-engine/latex-syntax/parse.d.ts +5 -5
  55. package/dist/types/compute-engine/latex-syntax/public.d.ts +2 -2
  56. package/dist/types/compute-engine/latex-syntax/serialize-number.d.ts +3 -3
  57. package/dist/types/compute-engine/latex-syntax/serializer-style.d.ts +1 -1
  58. package/dist/types/compute-engine/latex-syntax/serializer.d.ts +3 -3
  59. package/dist/types/compute-engine/latex-syntax/tokenizer.d.ts +1 -1
  60. package/dist/types/compute-engine/numerics/numeric-complex.d.ts +1 -1
  61. package/dist/types/compute-engine/numerics/numeric-decimal.d.ts +1 -1
  62. package/dist/types/compute-engine/numerics/numeric.d.ts +1 -1
  63. package/dist/types/compute-engine/numerics/primes.d.ts +1 -1
  64. package/dist/types/compute-engine/public.d.ts +68 -35
  65. package/dist/types/compute-engine/rules.d.ts +1 -1
  66. package/dist/types/compute-engine/simplify-rules.d.ts +1 -1
  67. package/dist/types/compute-engine/symbolic/expand.d.ts +1 -1
  68. package/dist/types/compute-engine/symbolic/flatten.d.ts +1 -1
  69. package/dist/types/compute-engine/symbolic/negate.d.ts +1 -1
  70. package/dist/types/compute-engine/symbolic/polynomials.d.ts +1 -1
  71. package/dist/types/compute-engine/symbolic/product.d.ts +1 -1
  72. package/dist/types/compute-engine/symbolic/sum.d.ts +1 -1
  73. package/dist/types/compute-engine/symbolic/utils.d.ts +1 -1
  74. package/dist/types/compute-engine.d.ts +2 -2
  75. package/dist/types/math-json/math-json-format.d.ts +1 -1
  76. package/dist/types/math-json/utils.d.ts +1 -1
  77. package/dist/types/math-json.d.ts +2 -2
  78. package/package.json +3 -3
  79. package/dist/compute-engine.js +0 -23523
  80. package/dist/math-json.js +0 -6339
package/README.md CHANGED
@@ -5,8 +5,7 @@
5
5
  <h3><strong>Cortex Compute Engine</strong></h3>
6
6
  <h1>Symbolic manipulation and numeric evaluation of MathJSON expressions</h1>
7
7
 
8
- [MathJSON](https://cortexjs.io/math-json/) is a lightweight data interchange
9
- format for mathematical notation, based on JSON.
8
+ [MathJSON](https://cortexjs.io/math-json/) is a lightweight mathematical notation interchange format based on JSON.
10
9
 
11
10
  The Cortex Compute Engine can parse LaTeX to MathJSON, serialize MathJSON to
12
11
  LaTeX, format, simplify and evaluate MathJSON expressions.
@@ -40,7 +39,7 @@ console.log(evaluate(expr));
40
39
 
41
40
  <dl>
42
41
  <dt><a href="https://cortexjs.io/math-json/">MathJSON</a></dt>
43
- <dd>A lightweight data interchange format for mathematical notation</dd>
42
+ <dd>A lightweight mathematical notation interchange format</dd>
44
43
  <dt><a href="https://cortexjs.io/mathlive">MathLive</a> (on <a href="https://github.com/arnog/mathlive">GitHub</a>)</dt>
45
44
  <dd>A Web Component for math input.</dd>
46
45
  <dt><a href="https://cortexjs.io/cortex">Cortex</a> (on <a href="https://github.com/cortex-js/compute-engine/tree/master/src/cortex">GitHub</a>)</dt>
@@ -1,7 +1,4 @@
1
- /** CortexJS Compute Engine 0.4.3 */
2
- const DEFAULT_COMPLEXITY = 100000;
3
- const DEBUG = true;
4
-
1
+ /** CortexJS Compute Engine 0.4.4 */
5
2
  function stringToCodepoints(string) {
6
3
  const result = [];
7
4
  for (let i = 0; i < string.length; i++) {
@@ -843,17 +840,17 @@ function numeratorDenominator(expr) {
843
840
  for (const arg of args) {
844
841
  if (head(arg) === 'Power') {
845
842
  if (head(op(arg, 2)) === 'Negate') {
846
- const a = op(arg, 1) ?? 'Nothing';
847
- const b = op(op(arg, 2), 1) ?? 'Nothing';
843
+ const a = op(arg, 1) ?? 'Missing';
844
+ const b = op(op(arg, 2), 1) ?? 'Missing';
848
845
  denominator.push([POWER, a, b]);
849
846
  }
850
847
  else {
851
848
  const exponentVal = machineValue(op(arg, 2)) ?? NaN;
852
849
  if (exponentVal === -1) {
853
- denominator.push(op(arg, 1) ?? 'Nothing');
850
+ denominator.push(op(arg, 1) ?? 'Missing');
854
851
  }
855
852
  else if (exponentVal < 0) {
856
- denominator.push([POWER, op(arg, 1) ?? 'Nothing', -exponentVal]);
853
+ denominator.push([POWER, op(arg, 1) ?? 'Missing', -exponentVal]);
857
854
  }
858
855
  else {
859
856
  numerator.push(arg);
@@ -871,8 +868,8 @@ function parseRoot(parser) {
871
868
  const base = parser.matchRequiredLatexArgument();
872
869
  if (base === null) {
873
870
  if (degree !== null)
874
- return [ROOT, 'Nothing', degree];
875
- return [SQRT, 'Nothing'];
871
+ return [ROOT, 'Missing', degree];
872
+ return [SQRT, 'Missing'];
876
873
  }
877
874
  if (degree !== null)
878
875
  return [ROOT, base, degree];
@@ -1103,11 +1100,11 @@ function parseFraction(parser) {
1103
1100
  head(op(denom, 1)) === 'PartialDerivative'))) {
1104
1101
  // It's a Leibniz notation partial derivative
1105
1102
  // `∂f(x)/∂x` or `∂^2f(x)/∂x∂y` or `∂/∂x f(x)`
1106
- const degree = op(numer, 3) ?? 'Nothing';
1103
+ const degree = op(numer, 3) ?? 'Missing';
1107
1104
  // Expect: getArg(numer, 2) === 'Nothing' -- no args
1108
1105
  let fn = op(numer, 1);
1109
1106
  if (fn === null || fn === 'Missing') {
1110
- fn = parser.matchExpression() ?? 'Nothing';
1107
+ fn = parser.matchExpression() ?? 'Missing';
1111
1108
  }
1112
1109
  let vars = [];
1113
1110
  if (head(denom) === 'Multiply') {
@@ -1339,7 +1336,7 @@ const DEFINITIONS_ARITHMETIC = [
1339
1336
  name: 'Exp',
1340
1337
  serialize: (serializer, expr) => joinLatex([
1341
1338
  '\\exponentialE^{',
1342
- serializer.serialize(op(expr, 1) ?? 'Nothing'),
1339
+ serializer.serialize(op(expr, 1) ?? 'Missing'),
1343
1340
  '}',
1344
1341
  ]),
1345
1342
  },
@@ -4934,10 +4931,10 @@ class _Parser {
4934
4931
  if (sub)
4935
4932
  this.match('<}>');
4936
4933
  }
4937
- subscripts.push(sub ?? 'Nothing');
4934
+ subscripts.push(sub ?? 'Missing');
4938
4935
  }
4939
4936
  else if (this.match('^'))
4940
- superscripts.push(this.matchRequiredLatexArgument() ?? 'Nothing');
4937
+ superscripts.push(this.matchRequiredLatexArgument() ?? 'Missing');
4941
4938
  this.skipSpace();
4942
4939
  }
4943
4940
  if (superscripts.length === 0 && subscripts.length === 0) {
@@ -5205,13 +5202,13 @@ class _Parser {
5205
5202
  if (tokens.length === 0)
5206
5203
  return [
5207
5204
  'Error',
5208
- 'Nothing',
5205
+ 'Missing',
5209
5206
  { str: 'syntax-error' },
5210
5207
  ['LatexForm', { str: command }],
5211
5208
  ];
5212
5209
  return [
5213
5210
  'Error',
5214
- 'Nothing',
5211
+ 'Missing',
5215
5212
  { str: 'unknown-command' },
5216
5213
  ['LatexForm', { str: `${command}{${tokensToString(tokens)}}` }],
5217
5214
  ];
@@ -5251,7 +5248,7 @@ class _Parser {
5251
5248
  }
5252
5249
  return [
5253
5250
  'Error',
5254
- 'Nothing',
5251
+ 'Missing',
5255
5252
  { str: 'unknown-command' },
5256
5253
  ['LatexForm', { str: `${command}${tokensToString(tokens)}` }],
5257
5254
  ];
@@ -5511,6 +5508,13 @@ function serializeNumber(expr, options) {
5511
5508
  // remove it (legacy format)
5512
5509
  if (/[0-9][nd]$/.test(num))
5513
5510
  num = num.slice(0, -1);
5511
+ // Remove any whitespace
5512
+ num = num.replace(/[\u0009-\u000d\u0020\u00a0]/g, '');
5513
+ // Do we have repeating digits?
5514
+ if (/\([0-9]+\)$/.test(num)) {
5515
+ const [_, body, repeat] = num.match(/(.+)\(([0-9]+)\)$/) ?? [];
5516
+ num = body + repeat.repeat(Math.ceil(options.precision / repeat.length));
5517
+ }
5514
5518
  let sign = '';
5515
5519
  if (num[0] === '-') {
5516
5520
  sign = '-';
@@ -13045,6 +13049,9 @@ class AbstractBoxedExpression {
13045
13049
  }
13046
13050
  /** Object.valueOf(): return a primitive value for the object */
13047
13051
  valueOf() {
13052
+ const [n, d] = this.rationalValue;
13053
+ if (n !== null && d !== null)
13054
+ return [n, d];
13048
13055
  return this.asFloat ?? this.string ?? this.symbol ?? this.toString();
13049
13056
  }
13050
13057
  /** Object.is() */
@@ -13564,6 +13571,7 @@ function hashCode(s) {
13564
13571
  return Math.abs(hash);
13565
13572
  }
13566
13573
 
13574
+ const DEFAULT_COMPLEXITY = 100000;
13567
13575
  /**
13568
13576
  * Sort by higher total degree (sum of degree), if tied, sort by max degree,
13569
13577
  * if tied,
@@ -14162,6 +14170,10 @@ function serializeJsonCanonicalFunction(ce, head, args, metadata) {
14162
14170
  return serializeJsonFunction(ce, 'Subtract', [args[1], args[0].op1], metadata);
14163
14171
  }
14164
14172
  }
14173
+ if (head === 'Divide' &&
14174
+ ce.jsonSerializationOptions.exclude.includes('Divide')) {
14175
+ return serializeJsonFunction(ce, 'Multiply', [args[0], ce._fn('Power', [args[1], ce.NEGATIVE_ONE])], metadata);
14176
+ }
14165
14177
  if (head === 'Multiply' &&
14166
14178
  !ce.jsonSerializationOptions.exclude.includes('Divide')) {
14167
14179
  // Display a product with negative exponents as a division if
@@ -14179,10 +14191,12 @@ function serializeJsonCanonicalFunction(ce, head, args, metadata) {
14179
14191
  const exp = args[1].asSmallInteger;
14180
14192
  if (!exclusions.includes('Square') && exp === 2)
14181
14193
  return serializeJsonFunction(ce, 'Square', [args[0]], metadata);
14182
- if (exp === -1)
14183
- return serializeJsonFunction(ce, 'Divide', [ce.ONE, args[0]], metadata);
14184
- if (exp !== null && exp < 0) {
14185
- return serializeJsonFunction(ce, 'Divide', [ce.ONE, ce.power(args[0], -exp)], metadata);
14194
+ if (!ce.jsonSerializationOptions.exclude.includes('Divide')) {
14195
+ if (exp === -1)
14196
+ return serializeJsonFunction(ce, 'Divide', [ce.ONE, args[0]], metadata);
14197
+ if (exp !== null && exp < 0) {
14198
+ return serializeJsonFunction(ce, 'Divide', [ce.ONE, ce.power(args[0], -exp)], metadata);
14199
+ }
14186
14200
  }
14187
14201
  const [n, d] = args[1].rationalValue;
14188
14202
  if (n === 1) {
@@ -14430,7 +14444,9 @@ function serializeJsonNumber(ce, value, metadata) {
14430
14444
  if (shorthandAllowed)
14431
14445
  return value;
14432
14446
  const num = repeatingDecimal(ce, value.toString());
14433
- return metadata.latex !== num ? { latex: metadata.latex, num } : { num };
14447
+ return metadata.latex.length > 0 && metadata.latex !== num
14448
+ ? { latex: metadata.latex, num }
14449
+ : { num };
14434
14450
  }
14435
14451
  function _escapeJsonString(s) {
14436
14452
  return s;
@@ -15105,12 +15121,8 @@ class BoxedFunction extends AbstractBoxedExpression {
15105
15121
  super(ce, metadata);
15106
15122
  this._head = typeof head === 'string' ? head : head.symbol ?? head;
15107
15123
  this._ops = ops;
15108
- if (typeof this._head === 'string') {
15109
- const def = ce.getFunctionDefinition(this._head, metadata?.wikidata);
15110
- if (def === null)
15111
- throw new Error(`Function \`${this._head}\` is not defined`);
15112
- this._def = def;
15113
- }
15124
+ if (typeof this._head === 'string')
15125
+ this._def = ce.getFunctionDefinition(this._head, metadata?.wikidata);
15114
15126
  this._isCanonical = false;
15115
15127
  // Note: _isPure is computed on demand and cached
15116
15128
  ce._register(this);
@@ -17090,6 +17102,8 @@ class BoxedSymbolDefinitionImpl {
17090
17102
  set value(val) {
17091
17103
  if (this.constant)
17092
17104
  throw new Error(`The value of the constant "${this.name}" cannot be changed`);
17105
+ if (typeof val === 'number')
17106
+ val = this._engine.box(val);
17093
17107
  this._value = val;
17094
17108
  if (val)
17095
17109
  this.setProps(valueToFlags(val));
@@ -20277,7 +20291,7 @@ const CORE_DICTIONARY = [
20277
20291
  /*
20278
20292
  DICTIONARY
20279
20293
  aka Association in Wolfram, Dictionary in Python and Swift, Record in Maple,
20280
- Map Containers in mathlab, Map in Javascript
20294
+ Map Containers in mathlab, Map in JavaScript
20281
20295
  Dictionary("field1", "value1", "field2", "value2"...)
20282
20296
  Need a new atomic 'dict' MathJSON type?
20283
20297
  {{name: 'dict',"field1": "value1", "field2": "value2"}}
@@ -22567,8 +22581,11 @@ class ComputeEngine {
22567
22581
  * Construct a new `ComputeEngine` environment.
22568
22582
  *
22569
22583
  * If no `options.dictionaries` is provided a default set of dictionaries
22570
- * is used. The `ComputeEngine.getDictionaries()` method can be called
22571
- * to access some subset of dictionaries, e.g. for arithmetic, calculus, etc...
22584
+ * is used.
22585
+ *
22586
+ * The `ComputeEngine.getDictionaries()` method can be called
22587
+ * to access a subset of dictionaries, e.g. for arithmetic, calculus, etc...
22588
+ *
22572
22589
  * The order of the dictionaries matter: the definitions from the later ones
22573
22590
  * override the definitions from earlier ones. The first dictionary should
22574
22591
  * be the `'core'` dictionary which include some basic definitions such
@@ -22622,6 +22639,7 @@ class ComputeEngine {
22622
22639
  TranscendentalNumber: null,
22623
22640
  PositiveNumber: null,
22624
22641
  };
22642
+ this._latexDictionary = options?.latexDictionary;
22625
22643
  this._jsonSerializationOptions = {
22626
22644
  exclude: [],
22627
22645
  shorthands: ['function', 'symbol', 'string', 'dictionary', 'number'],
@@ -22832,10 +22850,12 @@ class ComputeEngine {
22832
22850
  return;
22833
22851
  // Set the display precision as requested.
22834
22852
  // It may be less than the effective precision, which is never less than 15
22835
- this.latexSyntax.updateOptions({
22836
- precision: p,
22837
- avoidExponentsInRange: [-6, p],
22838
- });
22853
+ if (this._latexSyntax) {
22854
+ this.latexSyntax.updateOptions({
22855
+ precision: p,
22856
+ avoidExponentsInRange: [-6, p],
22857
+ });
22858
+ }
22839
22859
  this._precision = Math.max(p, Math.floor(MACHINE_PRECISION));
22840
22860
  this._decimal = this._decimal.config({ precision: this._precision });
22841
22861
  // Reset the caches
@@ -22865,7 +22885,8 @@ class ComputeEngine {
22865
22885
  if (f === 'complex' || f === 'machine')
22866
22886
  this._precision = Math.floor(MACHINE_PRECISION);
22867
22887
  // Make sure the display precision is not larger than the computation precision
22868
- if (this.latexSyntax.options.precision > this._precision)
22888
+ if (this._latexSyntax &&
22889
+ this.latexSyntax.options.precision > this._precision)
22869
22890
  this.latexSyntax.updateOptions({ precision: this._precision });
22870
22891
  // Reset the caches: the values in the cache depend on the numeric mode)
22871
22892
  this.purge();
@@ -22933,14 +22954,12 @@ class ComputeEngine {
22933
22954
  return n;
22934
22955
  }
22935
22956
  get latexSyntax() {
22936
- // We'll use this LatexSyntax instance internally, for example to parse
22937
- // rules, etc... Use our own custom error handler, which will throw
22938
- // on any error.
22939
22957
  if (!this._latexSyntax)
22940
22958
  this._latexSyntax = new LatexSyntax({
22959
+ computeEngine: this,
22960
+ dictionary: this._latexDictionary,
22941
22961
  precision: this.precision,
22942
22962
  avoidExponentsInRange: [-6, this.precision],
22943
- computeEngine: this,
22944
22963
  onError: (err) => {
22945
22964
  throw new Error(err[0].message.toString());
22946
22965
  },
@@ -22992,7 +23011,7 @@ class ComputeEngine {
22992
23011
  }
22993
23012
  if (defs)
22994
23013
  return defs[0];
22995
- // If no definition matching the domain of the arguments, return `undefined`
23014
+ // If no matching definition
22996
23015
  return undefined;
22997
23016
  }
22998
23017
  /**
@@ -23505,6 +23524,6 @@ class ComputeEngine {
23505
23524
  }
23506
23525
 
23507
23526
  // This file is the root of the `compute-engine` package
23508
- const version = '0.4.3';
23527
+ const version = '0.4.4';
23509
23528
 
23510
- export { ComputeEngine, DEBUG, DEFAULT_COMPLEXITY, version };
23529
+ export { ComputeEngine, version };