npm package discovery and stats viewer.

Discover Tips

  • General search

    [free text search, go nuts!]

  • Package details

    pkg:[package-name]

  • User packages

    @[username]

Sponsor

Optimize Toolset

I’ve always been into building performant and accessible sites, but lately I’ve been taking it extremely seriously. So much so that I’ve been building a tool to help me optimize and monitor the sites that I build to make sure that I’m making an attempt to offer the best experience to those who visit them. If you’re into performant, accessible and SEO friendly sites, you might like it too! You can check it out at Optimize Toolset.

About

Hi, 👋, I’m Ryan Hefner  and I built this site for me, and you! The goal of this site was to provide an easy way for me to check the stats on my npm packages, both for prioritizing issues and updates, and to give me a little kick in the pants to keep up on stuff.

As I was building it, I realized that I was actually using the tool to build the tool, and figured I might as well put this out there and hopefully others will find it to be a fast and useful way to search and browse npm packages as I have.

If you’re interested in other things I’m working on, follow me on Twitter or check out the open source projects I’ve been publishing on GitHub.

I am also working on a Twitter bot for this site to tweet the most popular, newest, random packages from npm. Please follow that account now and it will start sending out packages soon–ish.

Open Software & Tools

This site wouldn’t be possible without the immense generosity and tireless efforts from the people who make contributions to the world and share their work via open source initiatives. Thank you 🙏

© 2024 – Pkg Stats / Ryan Hefner

symbolic-math

v0.9.103

Published

Computer Algebra System in TypeScript

Downloads

103

Readme

npm version

symbolic-math is a Javascript (Typescript) library for symbolic mathematics.

Example

import { assert } from "chai";
import {
    Cons,
    create_script_context,
    create_sym, ExtensionEnv,
    is_rat,
    items_to_cons,
    LambdaExpr,
    Native,
    native_sym,
    one,
    ScriptContext,
    ScriptContextOptions,
    SyntaxKind,
    U
} from "symbolic-math";
import { assert_one_value_execute } from "./assert_one_value_execute";

describe("example", function () {
    it("Geometric Algebra", function () {
        const lines: string[] = [
            `G30=algebra([1,1,1],["i","j","k"])`,
            `e1=G30[1]`,
            `e2=G30[2]`,
            `e3=G30[3]`,
            `grad(s) = d(s,x) * e1 + d(s,y) * e2 + d(s,z) * e3`,
            `div(v) = d(v|e1,x) + d(v|e2,y) + d(v|e3,z)`,
            `curl(v) = (d(v|e3,y)-d(v|e2,z))*e1+(d(v|e1,z)-d(v|e3,x))*e2+(d(v|e2,x)-d(v|e1,y))*e3`,
            `ddrv(v,a) = (a|e1)*d(v,x)+(a|e2)*d(v,y)+(a|e3)*d(v,z)`,
            `A = Ax * e1 + Ay * e2 + Az * e3`,
            `B = Bx * e1 + By * e2 + Bz * e3`,
            `C = Cx * e1 + Cy * e2 + Cz * e3`,
            `cross(A,B)`,
            `A|B`,
            `A^B`
        ];
        const sourcetText = lines.join('\n');
        const options: ScriptContextOptions = {
            syntaxKind: SyntaxKind.Native,
            useCaretForExponentiation: false,
            useDefinitions: false
        };
        const context = create_script_context(options);
        const { values } = context.executeScript(sourcetText);
        assert.strictEqual(context.renderAsInfix(values[0]), "Ay*Bz*i-Az*By*i-Ax*Bz*j+Az*Bx*j+Ax*By*k-Ay*Bx*k");
        assert.strictEqual(context.renderAsInfix(values[1]), "Ax*Bx+Ay*By+Az*Bz");
        assert.strictEqual(context.renderAsInfix(values[2]), "Ax*By*i^j-Ay*Bx*i^j+Ax*Bz*i^k-Az*Bx*i^k+Ay*Bz*j^k-Az*By*j^k");
        context.release();
    });
});

const VERSIN = create_sym('versin');
const negOne = one.neg();

/**
 * Registers an implementation of the versin function with the environment.
 */
export function define_versin($: ScriptContext): ScriptContext {
    // If we also want to control the name as it appears in the script
    const match: U = items_to_cons(VERSIN);   // TODO 
    return $.defineFunction(match, versin_lambda);
}

const versin_lambda: LambdaExpr = (argList: Cons, $: ExtensionEnv) => {
    const x = argList.head;
    // versin(n*pi) = 1 - (-1)**n
    const pi = native_sym(Native.PI);
    const n = $.evaluate(Native.divide, x, pi);
    if (is_rat(n) && n.isInteger()) {
        return $.subtract(one, $.power(negOne, n));
    }
    else {
        // versin(x) = 1 - cos(x)
        return $.subtract(one, $.evaluate(Native.cosine, x));
    }
    // If not expanding.
    // return cons(VERSIN, argList);
};

describe("versin", function () {
    it("versin(x)", function () {
        const lines: string[] = [
            `versin(x)`
        ];
        const rootContext = create_script_context({
        });
        const context = define_versin(rootContext);
        const value = assert_one_value_execute(lines.join('\n'), context);
        assert.strictEqual(context.renderAsInfix(value), "1-cos(x)");
        context.release();
    });
    it("versin(pi)", function () {
        const lines: string[] = [
            `pi=tau(1)/2`,
            `versin(pi)`
        ];
        const rootContext = create_script_context({
        });
        const context = define_versin(rootContext);
        const value = assert_one_value_execute(lines.join('\n'), context);
        assert.strictEqual(context.renderAsInfix(value), "2");
        context.release();
    });
    it("versin(2*pi)", function () {
        const lines: string[] = [
            `pi=tau(1)/2`,
            `versin(2*pi)`
        ];
        const rootContext = create_script_context({
        });
        const context = define_versin(rootContext);
        const value = assert_one_value_execute(lines.join('\n'), context);
        assert.strictEqual(context.renderAsInfix(value), "0");
        context.release();
    });
});

Features

  • arbitrary-precision arithmetic
  • complex quantities
  • geometric algebra
  • trigonometric functions
  • special functions
  • simplification
  • expansion
  • substitution
  • factoring
  • symbolic and numeric roots
  • units of measure
  • hyperreal numbers
  • matrices
  • derivatives and gradients
  • tensors
  • booleans
  • integrals
  • multi-integrals
  • Native, Python, and Scheme syntax
  • open for extension

Getting Started

Please take a look at the tutorial file.

Contributing

Please take a look at the contributing file.

References

symbolic-math is a fork of Algebrite by Davide Della Casa. The fork adds Geometric Algebra, S.I. Units of Measure, and changes the way that expressions are matched and transformed.

Algebrite started as a port of the EigenMath CAS by George Weigt to TypeScript. Another fork of EigenMath: SMIB by Philippe Billet.

Another CAS of similar nature is SymPy made in Python.

Three other Javascript CAS are

  • javascript-cas by Anthony Foster supporting "differentiation, complex numbers, sums, vectors (dot products, cross products, gradient/curl etc)"
  • Coffeequate by Matthew Alger supporting "quadratic and linear equations, simplification of most algebraic expressions, uncertainties propagation, substitutions, variables, constants, and symbolic constants".
  • Algebra.js by Nicole White which among other things can build and solve equations via a "chainable" API.