0.9.103 • Published 2 years ago

symbolic-math v0.9.103

Weekly downloads
-
License
MIT
Repository
github
Last release
2 years 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

2 years ago

0.9.100

2 years ago

0.9.96

2 years ago

0.9.97

2 years ago

0.9.98

2 years ago

0.9.99

2 years ago

0.9.92

2 years ago

0.9.93

2 years ago

0.9.94

2 years ago

0.9.95

2 years ago

0.9.103

2 years ago

0.9.102

2 years ago

0.9.90

2 years ago

0.9.91

2 years ago

0.9.89

2 years ago

0.9.85

2 years ago

0.9.86

2 years ago

0.9.87

2 years ago

0.9.88

2 years ago

0.9.81

2 years ago

0.9.82

2 years ago

0.9.83

2 years ago

0.9.84

2 years ago

0.9.80

2 years ago

0.9.78

2 years ago

0.9.79

2 years ago

0.9.74

2 years ago

0.9.75

2 years ago

0.9.76

2 years ago

0.9.77

2 years ago

0.9.70

2 years ago

0.9.71

2 years ago

0.9.72

2 years ago

0.9.73

2 years ago

0.9.67

2 years ago

0.9.68

2 years ago

0.9.69

2 years ago

0.9.63

2 years ago

0.9.64

2 years ago

0.9.65

2 years ago

0.9.66

2 years ago

0.9.61

2 years ago

0.9.62

2 years ago

0.9.60

2 years ago

0.9.58

3 years ago

0.9.59

3 years ago

0.9.56

3 years ago

0.9.57

3 years ago

0.9.52

3 years ago

0.9.53

3 years ago

0.9.54

3 years ago

0.9.55

3 years ago

0.9.50

3 years ago

0.9.51

3 years ago

0.9.45

3 years ago

0.9.46

3 years ago

0.9.47

3 years ago

0.9.48

3 years ago

0.9.41

3 years ago

0.9.42

3 years ago

0.9.43

3 years ago

0.9.44

3 years ago

0.9.49

3 years ago

0.9.40

3 years ago

0.9.34

3 years ago

0.9.35

3 years ago

0.9.36

3 years ago

0.9.37

3 years ago

0.9.32

3 years ago

0.9.33

3 years ago

0.9.38

3 years ago

0.9.39

3 years ago

0.9.31

3 years ago

0.9.30

3 years ago

0.9.24

3 years ago

0.9.25

3 years ago

0.9.26

3 years ago

0.9.27

3 years ago

0.9.28

3 years ago

0.9.29

3 years ago

0.9.23

3 years ago

0.9.22

3 years ago

0.9.21

3 years ago

0.9.20

3 years ago

0.9.19

3 years ago

0.9.18

3 years ago

0.9.17

3 years ago

0.9.16

3 years ago

0.9.15

3 years ago

0.9.14

3 years ago

0.9.13

3 years ago

0.9.12

3 years ago

0.9.11

3 years ago

0.9.10

3 years ago

0.9.9

3 years ago

0.9.8

3 years ago

0.9.7

3 years ago

0.9.6

3 years ago

0.9.5

3 years ago

0.9.4

3 years ago

0.9.3

3 years ago

0.9.2

3 years ago

0.9.1

3 years ago

0.9.0

3 years ago