0.9.103 • Published 1 year ago

symbolic-math v0.9.103

Weekly downloads
-
License
MIT
Repository
github
Last release
1 year ago

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.
0.9.101

1 year ago

0.9.100

1 year ago

0.9.96

1 year ago

0.9.97

1 year ago

0.9.98

1 year ago

0.9.99

1 year ago

0.9.92

1 year ago

0.9.93

1 year ago

0.9.94

1 year ago

0.9.95

1 year ago

0.9.103

1 year ago

0.9.102

1 year ago

0.9.90

1 year ago

0.9.91

1 year ago

0.9.89

1 year ago

0.9.85

1 year ago

0.9.86

1 year ago

0.9.87

1 year ago

0.9.88

1 year ago

0.9.81

1 year ago

0.9.82

1 year ago

0.9.83

1 year ago

0.9.84

1 year ago

0.9.80

1 year ago

0.9.78

1 year ago

0.9.79

1 year ago

0.9.74

1 year ago

0.9.75

1 year ago

0.9.76

1 year ago

0.9.77

1 year ago

0.9.70

1 year ago

0.9.71

1 year ago

0.9.72

1 year ago

0.9.73

1 year ago

0.9.67

1 year ago

0.9.68

1 year ago

0.9.69

1 year ago

0.9.63

1 year ago

0.9.64

1 year ago

0.9.65

1 year ago

0.9.66

1 year ago

0.9.61

1 year ago

0.9.62

1 year ago

0.9.60

1 year ago

0.9.58

2 years ago

0.9.59

2 years ago

0.9.56

2 years ago

0.9.57

2 years ago

0.9.52

2 years ago

0.9.53

2 years ago

0.9.54

2 years ago

0.9.55

2 years ago

0.9.50

2 years ago

0.9.51

2 years ago

0.9.45

2 years ago

0.9.46

2 years ago

0.9.47

2 years ago

0.9.48

2 years ago

0.9.41

2 years ago

0.9.42

2 years ago

0.9.43

2 years ago

0.9.44

2 years ago

0.9.49

2 years ago

0.9.40

2 years ago

0.9.34

2 years ago

0.9.35

2 years ago

0.9.36

2 years ago

0.9.37

2 years ago

0.9.32

2 years ago

0.9.33

2 years ago

0.9.38

2 years ago

0.9.39

2 years ago

0.9.31

2 years ago

0.9.30

2 years ago

0.9.24

2 years ago

0.9.25

2 years ago

0.9.26

2 years ago

0.9.27

2 years ago

0.9.28

2 years ago

0.9.29

2 years ago

0.9.23

2 years ago

0.9.22

2 years ago

0.9.21

2 years ago

0.9.20

2 years ago

0.9.19

2 years ago

0.9.18

2 years ago

0.9.17

2 years ago

0.9.16

2 years ago

0.9.15

2 years ago

0.9.14

2 years ago

0.9.13

2 years ago

0.9.12

2 years ago

0.9.11

2 years ago

0.9.10

2 years ago

0.9.9

2 years ago

0.9.8

2 years ago

0.9.7

2 years ago

0.9.6

2 years ago

0.9.5

2 years ago

0.9.4

2 years ago

0.9.3

2 years ago

0.9.2

2 years ago

0.9.1

2 years ago

0.9.0

2 years ago