Cortex Compute Engine
Symbolic manipulation and numeric evaluation of MathJSON expressions
MathJSON is a lightweight mathematical notation interchange format based on JSON.
The Cortex Compute Engine can parse LaTeX to MathJSON, serialize MathJSON to
LaTeX, format, simplify and evaluate MathJSON expressions.
Reference documentation and guides at
cortexjs.io/compute-engine.
Using Compute Engine
$ npm install --save @cortex-js/compute-engine
import { parse, evaluate } from '@cortex-js/compute-engine';
const expr = parse('2^{11}-1 \\in \\P');
console.log(expr);
console.log(evaluate(expr));
More
Related Projects
- MathJSON
- A lightweight mathematical notation interchange format
- MathLive (on GitHub)
- A Web Component for math input.
- Cortex (on GitHub)
- A programming language for scientific computing
Support the Project
License
This project is licensed under the MIT License.
0.9.0 2022-11-15
Breaking Changes
- The head of a number expression is always
Number
. Use expr.domain
to be
get more specific info about what kind of number this is.
- By default,
ce.box()
and ce.parse()
return a canonical expression. A flag
can be used if a non-canonical expression is desired.
- The API surface of
BoxedExpression
has been reduced. The properties
machineValue
, bignumValue
, asFloat
, asSmallInteger
, asRational
etc... have been replaced with a single numericValue
property.
parseUnknownSymbol
is now parseUnknownIdentifier
Improvements
-
Support angles in degrees with 30\degree
, 30^\circ
and \ang{30}
.
-
More accurate error expressions, for example if there is a missing closing
delimiter an ["Error", ["ErrorCode", "'expected-closing-delimiter'", "')'"]]
is produced.
-
["Expand"]
handles more cases
-
The trig functions can now have a regular exponent, i.e.\cos^2(x)
in
addition to -1
for inverse, and a combination of \prime
, \doubleprime
and '
for derivatives.
-
ce.assume()
handle more expressions and can be used to define new symbols by
domain or value.
-
Better error message when parsing, e.g. \sqrt(2)
(instead of \sqrt{2}
)
-
Better simplification for square root expressions:
-
Improved evaluation of ["Power"]
expressions, including for negative
arguments and non-integer exponents and complex arguments and exponents.
-
Added Arccot
, Arcoth
, Arcsch
, Arcscc
, Arsech
and Arccsc
-
expr.solve()
returns result for polynomials of order up to 2.
-
The pattern.match()
function now work correctly for commutative functions,
i.e. ce.pattern(['Add', '_a', 'x']).match(ce.parse('x+y')) -> {"_a": "y"}
-
Added ce.let()
and ce.set()
to declare and assign values to identifiers.
-
Preserve exact calculations involving rationals or square root of rationals.
\sqrt{\frac{49}{25}}
-> \frac{7}{5}
-
Addition and multiplication provide more consistent results for evaluate()
and N()
. Evaluate returns an exact result when possible.
- EXACT
- 2 + 5 -> 7
- 2 + 5/7 -> 19/7
- 2 + √2 -> 2 + √2
- 2 + √(5/7) -> 2 + √(5/7)
- 5/7 + 9/11 -> 118/77
- 5/7 + √2 -> 5/7 + √2
- 10/14 + √(18/9) -> 5/7 + √2
- √2 + √5 -> √2 + √5
- √2 + √2 -> 2√2
- sin(2) -> sin(2)
- sin(π/3) -> √3/2
- APPROXIMATE
- 2 + 2.1 -> 4.1
- 2 + √2.1 -> 3.44914
- 5/7 + √2.1 -> 2.16342
- sin(2) + √2.1 -> 2.35844
-
More consistent behavior of the auto
numeric mode: calculations are done
with bignum
and complex
in most cases.
-
JsonSerializationOptions
has a new option to specify the numeric precision
in the MathJSON serialization.
-
Shorthand numbers can now be strings if they do not fit in a float-64:
// Before
["Rational", { "num": "1234567890123456789"}, { "num": "2345678901234567889"}]
// Now
["Rational", "1234567890123456789", "2345678901234567889"]
\sum
is now correctly parsed and evaluated. This includes creating a local
scope with the index and expression value of the sum.
Bugs Fixed
- The parsing and evaluation of log functions could produce unexpected results
- The
\gamma
command now correctly maps to ["Gamma"]
- Fixed numeric evaluation of the
["Gamma"]
function when using bignum
- #57 Substituting
0
(i.e. with expr.subs({})
) did not work.
- #60 Correctly parse multi-char symbols with underscore, i.e.
\mathrm{V_a}
- Parsing a number with repeating decimals and an exponent would drop the
exponent.
- Correct calculation of complex square roots
- Calculations were not always performed as bignum in
"auto"
numeric mode if
the precision was less than 15. Now, if the numeric mode is "auto"
,
calculations are done as bignum or complex numbers.
- If an identifier contained multiple strings of digits, it would not be
rendered to LaTeX correctly, e.g.
V20_20
.
- Correctly return
isReal
for real numbers