{"id":17052522,"url":"https://github.com/yaffle/expression","last_synced_at":"2025-04-12T16:41:59.073Z","repository":{"id":66142836,"uuid":"164311883","full_name":"Yaffle/Expression","owner":"Yaffle","description":"A library to parse expressions, solve and simplify systems of linear equations, find eigenvalues and eigenvectors","archived":false,"fork":false,"pushed_at":"2024-11-05T22:27:22.000Z","size":1813,"stargazers_count":6,"open_issues_count":4,"forks_count":1,"subscribers_count":2,"default_branch":"master","last_synced_at":"2025-04-06T22:41:13.638Z","etag":null,"topics":["expression","math","matrix","parser","polynomial","symbolic-computation"],"latest_commit_sha":null,"homepage":null,"language":"JavaScript","has_issues":true,"has_wiki":null,"has_pages":null,"mirror_url":null,"source_name":null,"license":"mit","status":null,"scm":"git","pull_requests_enabled":true,"icon_url":"https://github.com/Yaffle.png","metadata":{"files":{"readme":"README.md","changelog":null,"contributing":null,"funding":null,"license":"LICENSE","code_of_conduct":null,"threat_model":null,"audit":null,"citation":null,"codeowners":null,"security":null,"support":null,"governance":null,"roadmap":null,"authors":null,"dei":null,"publiccode":null,"codemeta":null}},"created_at":"2019-01-06T13:54:11.000Z","updated_at":"2024-11-05T22:27:26.000Z","dependencies_parsed_at":"2024-10-14T10:09:48.469Z","dependency_job_id":"94e875dc-3b58-4596-b2d2-2cdbf1ac1069","html_url":"https://github.com/Yaffle/Expression","commit_stats":null,"previous_names":[],"tags_count":8,"template":false,"template_full_name":null,"repository_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/repositories/Yaffle%2FExpression","tags_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/repositories/Yaffle%2FExpression/tags","releases_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/repositories/Yaffle%2FExpression/releases","manifests_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/repositories/Yaffle%2FExpression/manifests","owner_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/owners/Yaffle","download_url":"https://codeload.github.com/Yaffle/Expression/tar.gz/refs/heads/master","host":{"name":"GitHub","url":"https://github.com","kind":"github","repositories_count":248599210,"owners_count":21131239,"icon_url":"https://github.com/github.png","version":null,"created_at":"2022-05-30T11:31:42.601Z","updated_at":"2022-07-04T15:15:14.044Z","host_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub","repositories_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/repositories","repository_names_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/repository_names","owners_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/owners"}},"keywords":["expression","math","matrix","parser","polynomial","symbolic-computation"],"created_at":"2024-10-14T10:09:42.255Z","updated_at":"2025-04-12T16:41:59.045Z","avatar_url":"https://github.com/Yaffle.png","language":"JavaScript","funding_links":[],"categories":[],"sub_categories":[],"readme":"\nIt is a homemade library for JavaScript.\nIt can parse expressions, solve and simplify systems of linear equations, find eigenvalues and eigenvectors,\nor calculate real roots of polynomials with integer coefficients for a specified accuracy.\n\nInstallation\n============\n`npm install @yaffle/expression`\nor\n`npm install Yaffle/Expression`\n\nUsage example\n=============\n\nexample.mjs:\n\u003c!-- {% raw %} --\u003e\n```javascript\n\n  import {ExpressionParser, Polynomial, Expression} from './node_modules/@yaffle/expression/index.js';\n\n  // Exact polynomial roots can be found for some polynomials:\n  var p = Polynomial.toPolynomial(ExpressionParser.parse(\"10x^5−17x^4−505x^3+1775x^2−249x−630\"), ExpressionParser.parse(\"x\"));\n  console.log(p.getroots().toString()); // -1/2,5,21/5,(-73^0.5-7)/2,(73^0.5-7)/2\n\n  // Polynomial roots:\n  var p = Polynomial.toPolynomial(ExpressionParser.parse(\"x^5−2x^4−11x^3+26x^2−2x−13\"), ExpressionParser.parse(\"x\"));\n  console.log(p.getZeros().map(x =\u003e x.toString({rounding: {fractionDigits: 20}})).toString()); // -3.41190231035920486644,-0.60930943815581736137,1.07534597839596488553,1.92498144931467217779,3.02088432080438516449\n\n  // parse a matrix from a string:\n  var matrix = ExpressionParser.parse('{{1,2,3},{4,5,6},{7,8,9}}').matrix;\n  console.log('matrix: ' + matrix.toString()); // matrix: {{1,2,3},{4,5,6},{7,8,9}}\n\n  var eigenvalues = Expression.getEigenvalues(matrix);\n  console.log('eigenvalues: ' + eigenvalues.toString()); // eigenvalues: 0,(-3*33^0.5+15)/2,(3*33^0.5+15)/2\n  console.log('eigenvalues: ' + eigenvalues.map(x =\u003e x.toMathML({rounding: {fractionDigits: 10}}))); // eigenvalues: \u003cmn\u003e0\u003c/mn\u003e,\u003cmrow\u003e\u003cmo\u003e\u0026minus;\u003c/mo\u003e\u003cmn\u003e1.1168439698\u003c/mn\u003e\u003c/mrow\u003e,\u003cmn\u003e16.1168439698\u003c/mn\u003e\n\n  var eigenvectors = Expression.getEigenvectors(matrix, eigenvalues);\n  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}}\n\n  var y = Expression.diagonalize(matrix, eigenvalues, eigenvectors);\n  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}}\n\n  //var y = Expression.getFormaDeJordan(...);\n\n  // Compute the first 100 digits of the square root of 2:\n  console.log(ExpressionParser.parse('sqrt(2)').toMathML({rounding: {fractionDigits: 100}})); // \u003cmn\u003e1.4142135623730950488016887242096980785696718753769480731766797379907324784621070388503875343276415727\u003c/mn\u003e\n\n  // simplify an expression:\n  const simplify = ExpressionParser.parse;\n  console.log(simplify('x * y * -x / (x ^ 2)').toString()) // '-y'\n\n  // parsing with substitutions:\n  var result = ExpressionParser.parse('A*B', new ExpressionParser.Context(function (id) {\n    if (id === 'A') {\n      return ExpressionParser.parse('{{1,2},{3,4}}');\n    }\n    if (id === 'B') {\n      return ExpressionParser.parse('{{-4,2},{3,-1}}');\n    }\n  })).simplify();\n  console.log(result.toString());\n\n  // Square root of a matrix:\n  console.log(ExpressionParser.parse('{{33,24},{48,57}}**(1/2)').toString()); // {{5,2},{4,7}}\n\n  // Nth-root of a matrix:\n  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}}\n\n  // Nth-power of a matrix:\n  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}}\n\n  // Note: toLaTeX is deprecated\n  import './node_modules/@yaffle/expression/toLaTeX.js';\n  console.log(ExpressionParser.parse('{{1,2},{3,4}}').toLaTeX()); // \\begin{pmatrix}1 \u0026 2 \\\\ 3 \u0026 4\\end{pmatrix}\n\n```\n\u003c!-- {% endraw %} --\u003e\n\nto 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:\n====================================================================================================\n```html\n\u003cmeta charset=\"utf-8\" /\u003e\n\u003cscript type=\"module\" src=\"example.mjs\"\u003e\u003c/script\u003e\n```\nSee the console output.\n\nto run from the node.js create example.mjs (see above), then run:\n================================================================\n```sh\nnpm install @yaffle/expression --save\nnode --experimental-modules example.mjs\n```\n\n\nTypes\n=====\n```\n  nthRoot(a, n)\n  primeFactor(a)\n  Matrix\n    .I(size)\n    .Zero(rows, cols)\n    rows()\n    cols()\n    e(row, column) - get element\n    isSquare()\n    map(mapFunction)\n    transpose()\n    scale(x)\n    multiply(b)\n    add(b)\n    subtract(b)\n    augment(b)\n    rowReduce(...)\n    swapRows(...)\n    toRowEchelon(...)\n    determinant()\n    rank()\n    inverse()\n    toString()\n    pow(n)\n    eql()\n  Polynomial\n    .ZERO\n    .of(a0, a1, ...)\n    .from(arrayLike)\n    .pseudoRemainder(p1, p2)\n    .polynomialGCD(p1, p2)\n    .resultant(p1, p2)\n    .toPolynomial(expression, variable)\n\n    #getDegree()\n    #getCoefficient(index)\n    #getLeadingCoefficient() - same as p.getCoefficient(p.getDegree())\n    #getContent()\n\n    #add(other)\n    #multiply(other)\n    #scale(coefficient)\n    #shift(n)\n    #divideAndRemainder(other)\n    #modularInverse(m)\n\n    #getroots()\n    #getZeros([precision, complex])\n    #numberOfRoots(interval)\n    #calcAt(point)\n\n    #_exponentiateRoots(n)\n    #_scaleRoots(s)\n    #_translateRoots(h)\n\n    #factorize() - find some factor of a polynomial with integer coefficients\n  ExpressionParser\n    parse(string, context)\n  Expression\n    .ZERO\n    .ONE\n    .TWO\n    .TEN\n    .PI\n    .E\n    .I\n    #add\n    #subtract\n    #multiply\n    #divide\n    #pow\n    #equals\n    #toString()\n    #toMathML()\n    #toLaTeX()\n      Expression.Integer\n        integer\n      Expression.Symbol\n        symbol\n      Expression.NthRoot\n        radicand\n      Expression.Matrix\n        matrix\n      Expression.Polynomial\n        polynomial\n      Expression.Sin\n        argument\n      Expression.Cos\n        argument\n      Expression.Complex\n        real\n        imaginary\n      Expression.ExpressionPolynomialRoot\n        root\n```\n\nDEMO\n====\n[demo page](https://yaffle.github.io/Expression/)\n\nSimilar projects\n================\n * https://coffeequate.readthedocs.io/en/latest/usage/\n * http://algebrite.org - this page contains the list of \"JavaScript Computer Algebra Systems\"\n * https://github.com/sloisel/numeric\n * https://nerdamer.com/\n","project_url":"https://awesome.ecosyste.ms/api/v1/projects/github.com%2Fyaffle%2Fexpression","html_url":"https://awesome.ecosyste.ms/projects/github.com%2Fyaffle%2Fexpression","lists_url":"https://awesome.ecosyste.ms/api/v1/projects/github.com%2Fyaffle%2Fexpression/lists"}