# @yaffle/expression

> A library to parse expressions, solve and simplify systems of linear equations, find eigenvalues and eigenvectors

Latest version **0.0.47** (published 2024-11-05) · ISC license · 0 weekly downloads

## Install

```sh
npm install @yaffle/expression
pnpm add @yaffle/expression
yarn add @yaffle/expression
bun add @yaffle/expression
```

## Health

**Score 20/100 (F)** — status: abandoned.

Positive: esm support; no vulnerabilities.

Warnings: low downloads; no types; pre 1.0.

Negative: abandoned; low maintenance score.

## Facts

| | |
|---|---|
| Version | 0.0.47 |
| Published | 2024-11-05 |
| First published | 2019-02-09 |
| Weekly downloads | 0 |
| License | ISC |
| TypeScript types | none |
| Module format | ESM + CommonJS |
| Dependencies | 5 |
| Unpacked size | 842.4 KB |
| Known vulnerabilities | 0 |
| Install scripts | yes |
| GitHub stars | 7 |
| Author | Yaffle |
| Maintainers | yaffle |
| Keywords | math, algebra, parser, expression, matrix, polynomial, factorization, symbolic-computation, cas, complex, eigenvectors, eigenvalues, equation solver, linear algebra, symbolic math, resultant, polynomial GCD, polynomial roots |

## Links

- npm: https://www.npmjs.com/package/@yaffle/expression
- Repository: https://github.com/Yaffle/Expression
- Homepage: https://github.com/Yaffle/Expression#readme
- Issues: https://github.com/Yaffle/Expression/issues
- npm.io page: https://npm.io/package/@yaffle/expression

## Dependencies (5)

- [bigint-gcd](https://npm.io/package/bigint-gcd.md) ^1.0.28
- [seedrandom](https://npm.io/package/seedrandom.md) ^3.0.5
- [js-big-integer](https://npm.io/package/js-big-integer.md) ^3.0.25
- [@yaffle/bigdecimal](https://npm.io/package/@yaffle/bigdecimal.md) ^1.0.27
- [quadraticsievefactorization](https://npm.io/package/quadraticsievefactorization.md) ^1.0.72

## Alternatives

- [@mce/gif](https://npm.io/package/@mce/gif.md) — 2.6K weekly downloads
- [cleanse](https://npm.io/package/cleanse.md) — 173 weekly downloads
- [str](https://npm.io/package/str.md) — 127 weekly downloads
- [naming](https://npm.io/package/naming.md) — 95 weekly downloads
- [tap-telco-api](https://npm.io/package/tap-telco-api.md) — 19 weekly downloads

## Recent versions

- 0.0.47 (latest) — 2024-11-05
- 0.0.46 — 2024-11-05
- 0.0.45 — 2023-08-20
- 0.0.44 — 2023-08-18
- 0.0.43 — 2023-08-18
- 0.0.41 — 2023-06-04
- 0.0.40 — 2023-06-04
- 0.0.38 — 2022-12-19
- 0.0.37 — 2022-07-04
- 0.0.35 — 2022-02-17
- 0.0.34 — 2021-10-27
- 0.0.33 — 2021-10-26
- 0.0.32 — 2021-10-26
- 0.0.31 — 2021-05-09
- 0.0.30 — 2021-05-09
- … 29 more at https://npm.io/package/@yaffle/expression/versions

## README

It is a homemade library for JavaScript.
It can parse expressions, solve and simplify systems of linear equations, find eigenvalues and eigenvectors,
or calculate real roots of polynomials with integer coefficients for a specified accuracy.

Installation
============
`npm install @yaffle/expression`
or
`npm install Yaffle/Expression`

Usage example
=============

example.mjs:
<!-- {% raw %} -->
```javascript

  import {ExpressionParser, Polynomial, Expression} from './node_modules/@yaffle/expression/index.js';

  // Exact polynomial roots can be found for some polynomials:
  var p = Polynomial.toPolynomial(ExpressionParser.parse("10x^5−17x^4−505x^3+1775x^2−249x−630"), ExpressionParser.parse("x"));
  console.log(p.getroots().toString()); // -1/2,5,21/5,(-73^0.5-7)/2,(73^0.5-7)/2

  // Polynomial roots:
  var p = Polynomial.toPolynomial(ExpressionParser.parse("x^5−2x^4−11x^3+26x^2−2x−13"), ExpressionParser.parse("x"));
  console.log(p.getZeros().map(x => x.toString({rounding: {fractionDigits: 20}})).toString()); // -3.41190231035920486644,-0.60930943815581736137,1.07534597839596488553,1.92498144931467217779,3.02088432080438516449

  // parse a matrix from a string:
  var matrix = ExpressionParser.parse('{{1,2,3},{4,5,6},{7,8,9}}').matrix;
  console.log('matrix: ' + matrix.toString()); // matrix: {{1,2,3},{4,5,6},{7,8,9}}

  var eigenvalues = Expression.getEigenvalues(matrix);
  console.log('eigenvalues: ' + eigenvalues.toString()); // eigenvalues: 0,(-3*33^0.5+15)/2,(3*33^0.5+15)/2
  console.log('eigenvalues: ' + eigenvalues.map(x => x.toMathML({rounding: {fractionDigits: 10}}))); // eigenvalues: <mn>0</mn>,<mrow><mo>&minus;</mo><mn>1.1168439698</mn></mrow>,<mn>16.1168439698</mn>

  var eigenvectors = Expression.getEigenvectors(matrix, eigenvalues);
  console.log('eigenvectors: ' + eigenvectors.toString()); // eigenvectors: {{1},{-2},{1}},{{(-3*33^0.5-11)/22},{(-3*33^0.5+11)/44},{1}},{{(3*33^0.5-11)/22},{(3*33^0.5+11)/44},{1}}

  var y = Expression.diagonalize(matrix, eigenvalues, eigenvectors);
  console.log('diagonalization: ' + matrix.toString() + ' = ' + y.T.toString() + " * " + y.L.toString() + " * " + y.T_INVERSED.toString()); // diagonalization: {{1,2,3},{4,5,6},{7,8,9}} = {{1,(-3*33^0.5-11)/22,(3*33^0.5-11)/22},{-2,(-3*33^0.5+11)/44,(3*33^0.5+11)/44},{1,1,1}} * {{0,0,0},{0,(-3*33^0.5+15)/2,0},{0,0,(3*33^0.5+15)/2}} * {{1/6,-1/3,1/6},{(-33^0.5-1)/12,(-33^0.5+3)/18,(-33^0.5+15)/36},{(33^0.5-1)/12,(33^0.5+3)/18,(33^0.5+15)/36}}

  //var y = Expression.getFormaDeJordan(...);

  // Compute the first 100 digits of the square root of 2:
  console.log(ExpressionParser.parse('sqrt(2)').toMathML({rounding: {fractionDigits: 100}})); // <mn>1.4142135623730950488016887242096980785696718753769480731766797379907324784621070388503875343276415727</mn>

  // simplify an expression:
  const simplify = ExpressionParser.parse;
  console.log(simplify('x * y * -x / (x ^ 2)').toString()) // '-y'

  // parsing with substitutions:
  var result = ExpressionParser.parse('A*B', new ExpressionParser.Context(function (id) {
    if (id === 'A') {
      return ExpressionParser.parse('{{1,2},{3,4}}');
    }
    if (id === 'B') {
      return ExpressionParser.parse('{{-4,2},{3,-1}}');
    }
  })).simplify();
  console.log(result.toString());

  // Square root of a matrix:
  console.log(ExpressionParser.parse('{{33,24},{48,57}}**(1/2)').toString()); // {{5,2},{4,7}}

  // Nth-root of a matrix:
  console.log(ExpressionParser.parse('{{33,24},{48,57}}**(1/n)').toString()); // {{(3^(4/n)+2*3^(2/n))/3,(3^(4/n)-3^(2/n))/3},{(2*3^(4/n)-2*3^(2/n))/3,(2*3^(4/n)+3^(2/n))/3}}

  // Nth-power of a matrix:
  console.log(ExpressionParser.parse('{{33,24},{48,57}}**n').toString()); // {{(3^(4*n)+2*3^(2*n))/3,(3^(4*n)-3^(2*n))/3},{(2*3^(4*n)-2*3^(2*n))/3,(2*3^(4*n)+3^(2*n))/3}}

  // Note: toLaTeX is deprecated
  import './node_modules/@yaffle/expression/toLaTeX.js';
  console.log(ExpressionParser.parse('{{1,2},{3,4}}').toLaTeX()); // \begin{pmatrix}1 & 2 \\ 3 & 4\end{pmatrix}

```
<!-- {% endraw %} -->

to run from a webbrowser create example.mjs (see above), example.html, npm install http-server, npx http-server, and open it in a web browser:
====================================================================================================
```html
<meta charset="utf-8" />
<script type="module" src="example.mjs"></script>
```
See the console output.

to run from the node.js create example.mjs (see above), then run:
================================================================
```sh
npm install @yaffle/expression --save
node --experimental-modules example.mjs
```


Types
=====
```
  nthRoot(a, n)
  primeFactor(a)
  Matrix
    .I(size)
    .Zero(rows, cols)
    rows()
    cols()
    e(row, column) - get element
    isSquare()
    map(mapFunction)
    transpose()
    scale(x)
    multiply(b)
    add(b)
    subtract(b)
    augment(b)
    rowReduce(...)
    swapRows(...)
    toRowEchelon(...)
    determinant()
    rank()
    inverse()
    toString()
    pow(n)
    eql()
  Polynomial
    .ZERO
    .of(a0, a1, ...)
    .from(arrayLike)
    .pseudoRemainder(p1, p2)
    .polynomialGCD(p1, p2)
    .resultant(p1, p2)
    .toPolynomial(expression, variable)

    #getDegree()
    #getCoefficient(index)
    #getLeadingCoefficient() - same as p.getCoefficient(p.getDegree())
    #getContent()

    #add(other)
    #multiply(other)
    #scale(coefficient)
    #shift(n)
    #divideAndRemainder(other)
    #modularInverse(m)

    #getroots()
    #getZeros([precision, complex])
    #numberOfRoots(interval)
    #calcAt(point)

    #_exponentiateRoots(n)
    #_scaleRoots(s)
    #_translateRoots(h)

    #factorize() - find some factor of a polynomial with integer coefficients
  ExpressionParser
    parse(string, context)
  Expression
    .ZERO
    .ONE
    .TWO
    .TEN
    .PI
    .E
    .I
    #add
    #subtract
    #multiply
    #divide
    #pow
    #equals
    #toString()
    #toMathML()
    #toLaTeX()
      Expression.Integer
        integer
      Expression.Symbol
        symbol
      Expression.NthRoot
        radicand
      Expression.Matrix
        matrix
      Expression.Polynomial
        polynomial
      Expression.Sin
        argument
      Expression.Cos
        argument
      Expression.Complex
        real
        imaginary
      Expression.ExpressionPolynomialRoot
        root
```

DEMO
====
[demo page](https://yaffle.github.io/Expression/)

Similar projects
================
 * https://coffeequate.readthedocs.io/en/latest/usage/
 * http://algebrite.org - this page contains the list of "JavaScript Computer Algebra Systems"
 * https://github.com/sloisel/numeric
 * https://nerdamer.com/

---
_Source: https://npm.io/package/@yaffle/expression · Machine-readable twin of the npm.io package page. Health data is recomputed on every publish._
