
Security News
Crates.io Implements Trusted Publishing Support
Crates.io adds Trusted Publishing support, enabling secure GitHub Actions-based crate releases without long-lived API tokens.
@cortex-js/compute-engine
Advanced tools
Symbolic computing and numeric evaluations for JavaScript and Node.js
MathJSON is a lightweight mathematical notation interchange format based on JSON.
The Cortex Compute Engine can parse LaTeX to MathJSON, serialize MathJSON to LaTeX or MathASCII, format, simplify and evaluate MathJSON expressions.
Reference documentation and guides at cortexjs.io/compute-engine.
$ npm install --save @cortex-js/compute-engine
import { ComputeEngine } from "@cortex-js/compute-engine";
const ce = new ComputeEngine();
ce.parse("2^{11}-1 \\in \\Z").evaluate().print()
// ➔ "True"
Q How do I build the project?
Build instructions
This project is licensed under the MIT License.
0.27.0 2024-12-02
#217 Correctly parse LaTeX expressions that include a command followed by
a *
such as \\pi*2
.
#217 Correctly calculate the angle of trigonometric expressions with an
expression containing a reference to Pi
, for example \\sin(\\pi^2)
.
The Factorial
function will now time out if the argument is too large. The
timeout is signaled by throwing a CancellationError
.
When specifying exp.toMathJSON({shorthands:[]})
, i.e., not to use shorthands
in the MathJSON, actually avoid using shorthands.
Correctly use custom multiply, plus, etc. for LaTeX serialization.
When comparing two numeric values, the tolerance is now used to determine if
the values are equal. The tolerance can be set with the ce.tolerance
property.
When comparing two expressions with isEqual()
the values are compared
structurally when necessary, or with a stochastic test when the expressions
are too complex to compare structurally.
Correctly serialize nested superscripts, e.g. x^{y^z}
.
The result of evaluating a Hold
expression is now the expression itself.
To prevent evaluation of an expression temporarily, use the Unevaluated
function. The result of evaluating an Unevaluated
expression is its
argument.
The type of a Hold
expression was incorrectly returned as string
. It now
returns the type of its argument.
The statistics function (Mean
, Median
, Variance
, StandardDeviation
,
Kurtosis
, Skewness
, Mode
, Quartiles
and InterQuartileRange
) now
accept as argument either a collection or a sequence of values.
ce.parse("\\mathrm{Mean}([7, 2, 11])").evaluate().print();
// -> 20/3
ce.parse("\\mathrm{Mean}(7, 2, 11)").evaluate().print();
// -> 20/3
The Variance
and StandardDeviation
functions now have variants for
population statistics, PopulationVariance
and PopulationStandardDeviation
.
The default is to use sample statistics.
ce.parse("\\mathrm{PopulationVariance}([7, 2, 11])").evaluate().print();
// -> 13.555
ce.parse("\\mathrm{Variance}([7, 2, 11])").evaluate().print();
// -> 20.333
The statistics function can now be compiled to JavaScript:
const code = ce.parse("\\mathrm{Mean}(7, 2, 11)").compile();
console.log(code());
// -> 13.555
The statistics function calculate either using machine numbers or bignums
depending on the precision. The precision can be set with the precision
property of the Compute Engine.
The argument of compiled function is now optional.
Compiled expressions can now reference external JavaScript functions. For example:
ce.defineFunction('Foo', {
signature: 'number -> number',
evaluate: ([x]) => ce.box(['Add', x, 1]),
});
const fn = ce.box(['Foo', 3]).compile({
functions: { Foo: (x) => x + 1 },
})!;
console.info(fn());
// -> 4
ce.defineFunction('Foo', {
signature: 'number -> number',
evaluate: ([x]) => ce.box(['Add', x, 1]),
});
function foo(x) {
return x + 1;
}
const fn = ce.box(['Foo', 3]).compile({
functions: { Foo: foo },
})!;
console.info(fn());
// -> 4
Additionally, functions can be implicitly imported (in case they are needed by other JavaScript functions):
ce.defineFunction('Foo', {
signature: 'number -> number',
evaluate: ([x]) => ce.box(['Add', x, 1]),
});
function bar(x, y) {
return x + y;
}
function foo(x) {
return bar(x, 1);
}
const fn = ce.box(['Foo', 3]).compile({
functions: { Foo: 'foo' },
imports: [foo, bar],
})!;
console.info(fn());
// -> 4
Compiled expression can now include an arbitrary preamble (JavaScript source) that is executed before the compiled function is executed. This can be used to define additional functions or constants.
ce.defineFunction('Foo', {
signature: 'number -> number',
evaluate: ([x]) => ce.box(['Add', x, 1]),
});
const code = ce.box(['Foo', 3]).compile({
preamble: "function Foo(x) { return x + 1};",
});
The hold
function definition flag has been renamed to lazy
FAQs
Symbolic computing and numeric evaluations for JavaScript and Node.js
The npm package @cortex-js/compute-engine receives a total of 34,218 weekly downloads. As such, @cortex-js/compute-engine popularity was classified as popular.
We found that @cortex-js/compute-engine demonstrated a healthy version release cadence and project activity because the last version was released less than a year ago. It has 1 open source maintainer collaborating on the project.
Did you know?
Socket for GitHub automatically highlights issues in each pull request and monitors the health of all your open source dependencies. Discover the contents of your packages and block harmful activity before you install or update your dependencies.
Security News
Crates.io adds Trusted Publishing support, enabling secure GitHub Actions-based crate releases without long-lived API tokens.
Research
/Security News
Undocumented protestware found in 28 npm packages disrupts UI for Russian-language users visiting Russian and Belarusian domains.
Research
/Security News
North Korean threat actors deploy 67 malicious npm packages using the newly discovered XORIndex malware loader.