README
Raku-CortexJS
Raku client for the MathLive Cortex-JS Compute Engine.
Installation
Preliminary setup (Node.js and Node packages)
CortexJS starts a Node.js backend process and requires the
@cortex-js/compute-engine package to be available.
macOS (Homebrew):
brew install node
npm install @cortex-js/compute-engineLinux (Debian/Ubuntu):
sudo apt update
sudo apt install -y nodejs npm
npm install @cortex-js/compute-engineWindows (winget, PowerShell):
winget install OpenJS.NodeJS.LTS
npm install @cortex-js/compute-engineVerify:
node --version
npm list @cortex-js/compute-engineRaku package installation
Install this Raku package, "CortexJS", from Zef ecosystem:
zef install CortexJSFrom GitHub:
zef install https://github.com/antononcube/Raku-CortexJS.gitHow it works?
CortexJS::ComputeEngine is a Raku wrapper around a long-lived Node.js backend.
When you call ComputeEngine.new, the wrapper:
Resolves or loads the bridge JavaScript (
ce-bridge.mjs) source.Starts a Node process (
node ...) throughProc::Async.Initializes stream handlers for
stdout(responses) andstderr(diagnostics).Sends an initial
pingrequest to confirm the backend is ready.
During the session, each Raku method call (for example parse-latex, simplify,
evaluate, expand, solve) is converted into a JSON request and written to the
Node process stdin. The bridge executes the operation with
@cortex-js/compute-engine and writes a JSON response back to stdout.
The Raku side matches responses and returns the decoded result to your code.
When done, call .close (or let object destruction trigger cleanup) to stop the
backend process.
Basic usage
Make a new computation engine object and evaluate a LaTeX expression:
use CortexJS;
'e^{i\\pi}' ==> evaluate()Expand expression:
'(a + b)^2' ==> expand()Simplify expression:
my $expr = parse-latex('3x^2 + 2x^2 + x + 5');
say "{to-latex($expr)} = {to-latex(simplify($expr))}";=
Solve a symbolic equation
'x^2 - a x + 1 = 0' ==> solve(vars => 'a')The input can be both a LaTeX string (as above) or in MathJSON format.
Here the LaTeX expression (a - b)^2 is converted to MathJSON using the sub parse-latex:
parse-latex('(a - b)^2').raku # $["Power", ["Add", "a", ["Negate", "b"]], 2]Here the corresponding MathJSON expression is expanded:
["Power", ["Add", "a", ["Negate", "b"]], 2]
==> expand()# [Add [Power a 2] [Power b 2] [Multiply -2 a b]]MathJSON expressions can be converted to LaTeX with to-latex:
_ ==> to-latex()Remark: The "free functions" evaluate, N, simplify, assign, expand, expandAll, factor, and solve
verify (by parsing) is a string input a valid LaTeX code, and if the verification is successful,
then (more or less) the following processing pipeline in applied: $expr ==> parse-latex() ==> &func() ==> to-latex().
Using assignment for repeated expression evaluation:
my $expr = parse-latex("3x^2+4x+2");
for (0, 0.1 ... 1) -> $x {
assign('x', $x);
say "f($x) = {evaluate($expr)}";
}# f(0) = Add Multiply 3 Power x 2 Multiply 4 x 2
# f(0.1) = Add Multiply 3 Power x 2 Multiply 4 x 2
# f(0.2) = Add Multiply 3 Power x 2 Multiply 4 x 2
# f(0.3) = Add Multiply 3 Power x 2 Multiply 4 x 2
# f(0.4) = Add Multiply 3 Power x 2 Multiply 4 x 2
# f(0.5) = Add Multiply 3 Power x 2 Multiply 4 x 2
# f(0.6) = Add Multiply 3 Power x 2 Multiply 4 x 2
# f(0.7) = Add Multiply 3 Power x 2 Multiply 4 x 2
# f(0.8) = Add Multiply 3 Power x 2 Multiply 4 x 2
# f(0.9) = Add Multiply 3 Power x 2 Multiply 4 x 2
# f(1) = Add Multiply 3 Power x 2 Multiply 4 x 2References
[ML1] MathLive.io, Compute Engine.