algebrite

repository·master·Indexed 21 days ago

https://github.com/davidedc/algebrite

A Computer Algebra System in TypeScript and JavaScript for symbolic mathematics. Algebrite provides tools for arbitrary-precision arithmetic, calculus (differentiation and integration), matrix operations, simplification, and expansion. It includes a suite of built-in functions for mathematical operations, Lisp-style list processing, and predicate functions for inspecting symbolic expressions.

Tokens
1.5K
Snippets
7
Records
9
Agent score
27%

What's inside algebrite

  1. Build Algebrite for npm and browser usage

    master

    To create a build suitable for npm and browser environments, follow these steps:

    1. Install dependencies via npm.
    2. Run the bazel build command for npm.
    3. Open index.html to view the result.
    npm install
    bazelisk build npm
  2. Build Algebrite for development or testing

    master

    To build the project for running tests, ensure npm is installed and use bazelisk (the launcher for the bazel build system).

    To perform a thorough clean, use:

    bazelisk clean; rm -rf ./dist/*
    npm install
    bazelisk build algebrite
  3. Use Algebrite for symbolic mathematics

    master

    Algebrite is a JavaScript/TypeScript library for symbolic mathematics. It allows you to perform operations like simplification, expansion, substitution, differentiation, and integration.

    All built-in methods are exposed through a JavaScript interface. Strings are automatically parsed as expressions, and numbers are converted into appropriate representations. Methods return internal 'cons' objects, which you can convert to a human-readable string using the .toString() method.

    var Algebrite = require('algebrite')
    
    // Basic expression evaluation
    Algebrite.run('x + x') // => "2 x"
    
    // Factoring
    Algebrite.factor('10!').toString() // => "2^8 3^4 5^2 7"
    
    // Calculus: Integrals
    Algebrite.eval('integral(x^2)').toString() // => "1/3 x^3"
    
    // Composing operations
    Algebrite.integral(Algebrite.eval('x')).toString() // => "1/2 x^2"
  4. Run Algebrite tests

    master

    Use bazelisk to run the test suite. If you suspect cached results are interfering with your test accuracy, you can bypass the cache.

    # Run all tests
    bazelisk test :all
    
    # Run all tests bypassing the cache
    bazelisk test :all --cache_test_results=no
  5. Manage user-defined symbols and bindings

    master

    Algebrite allows you to define and manage symbols and their bindings within the environment.

    • symbol(name): Creates a symbol.
    • set_binding(symbol, value): Assigns a value to a symbol.
    • get_binding(symbol): Retrieves the current value of a symbol.
    • collectUserSymbols(): Returns a list of symbols defined by the user.
    • usr_symbol(name): Creates a user-defined symbol.
    import { symbol, set_binding, get_binding } from 'algebrite';
    
    const x = symbol('x');
    set_binding(x, 10);
    const value = get_binding(x);
    console.log(value);
  6. Use list processing functions (car, cdr, etc.)

    master

    Algebrite supports Lisp-style list manipulation functions to navigate symbolic structures (often represented as 'cons' cells).

    • car(expr): Returns the first element of a list.
    • cdr(expr): Returns the rest of the list after the first element.
    • caar(expr): Returns the car of the car.
    • cadr(expr): Returns the cdr of the car.
    • caddr(expr): Returns the cdr of the cdr of the car.
    • (And various other combinations like cadadr, cadddr, etc.)
  7. Execute Algebrite symbolic operations

    master

    The Algebrite library exports a default object (often referred to as $) that serves as the primary interface for symbolic mathematics. You can execute built-in mathematical functions by calling them as methods on this object. Most built-in functions are wrappers around exec, allowing you to perform operations like simplification, differentiation, and integration.

    Common built-in functions include:

    • abs, add, multiply, power, sqrt
    • derivative, integral, simplify, expand
    • sin, cos, tan, exp, log
    • factor, gcd, lcm
    • roots, nroots, eigen
    • printlatex, printhuman (for output formatting)
    import algebrite from 'algebrite';
    
    // Example usage of built-in functions
    algebrite.simplify('expand((x + 1)^2)');
    algebrite.derivative('x^2 + 3x', 'x');
    algebrite.integral('1/x', 'x');
  8. Check types and properties of mathematical objects

    master

    The library provides a wide array of predicate functions to inspect the properties of symbolic expressions and atoms. These are useful for conditional logic in mathematical algorithms.

    Numeric and Atom Checks:

    • isNumericAtom(expr): Checks if the expression is a numeric atom.
    • isinteger(expr), isposint(expr), isnegative(expr), isrational(expr)
    • iscomplexnumber(expr), isimaginarynumber(expr)
    • isfraction(expr), isfloating(expr)

    Structural Checks:

    • isadd(expr): Checks if the expression is an addition.
    • ismultiply(expr): Checks if the expression is a multiplication.
    • ispower(expr): Checks if the expression is a power.
    • isfactorial(expr): Checks if the expression is a factorial.
    • iscons(expr): Checks if the expression is a 'cons' cell.
    • issymbol(expr): Checks if the expression is a symbol.
    • isstr(expr): Checks if the expression is a string.
    • istensor(expr): Checks if the expression is a tensor.
  9. Parse and execute Algebrite expressions

    master

    The library provides low-level methods for parsing and executing expressions directly via the zombocom runtime.

    • parse(expression): Parses a string expression into an internal representation.
    • exec(functionName, ...args): Executes a specific built-in function with the provided arguments.
    • run(expression): Executes an expression.
    import { parse, exec } from 'algebrite';
    
    // Using the underlying runtime methods
    const parsed = parse('x + 1');
    const result = exec('add', parsed, 2);
    // Note: The default export $ wraps these into convenient methods.