nerdamer

a symbolic math expression evaluator for javascript

https://github.com/jiggzson/nerdamer

Science Score: 44.0%

This score indicates how likely this project is to be science-related based on various indicators:

  • CITATION.cff file
    Found CITATION.cff file
  • codemeta.json file
    Found codemeta.json file
  • .zenodo.json file
    Found .zenodo.json file
  • DOI references
  • Academic publication links
  • Committers with academic emails
  • Institutional organization owner
  • JOSS paper metadata
  • Scientific vocabulary similarity
    Low similarity (13.0%) to scientific vocabulary

Keywords

algebra calculus differentiation equation expressions fraction fractions integration javascript latex math solve-equations solver substitutions symbolic
Last synced: 4 months ago · JSON representation ·

Repository

a symbolic math expression evaluator for javascript

Basic Info
  • Host: GitHub
  • Owner: jiggzson
  • License: mit
  • Language: JavaScript
  • Default Branch: master
  • Homepage: http://www.nerdamer.com
  • Size: 12.2 MB
Statistics
  • Stars: 531
  • Watchers: 25
  • Forks: 82
  • Open Issues: 81
  • Releases: 34
Topics
algebra calculus differentiation equation expressions fraction fractions integration javascript latex math solve-equations solver substitutions symbolic
Created over 12 years ago · Last pushed 9 months ago
Metadata Files
Readme Contributing License Code of conduct Citation

README.md

Build Status

Nerdamer

As of version 0.5.0, the library is split into the core and optional add-ons which can be loaded after the core has been loaded.

UPDATE

Nerdamer is currently being ported to TypeScript. This will have substantial breaking changes. A full list of changes will be published with the official release. You can track the TypeScript port here


Note: The Together-science project decided to pick up the banner where Nerdamer left off. See Nerdamer-Prime.

Getting started with Nerdamer

Load the library in your html page

html <!-- assuming you've saved the file in the root of course --> <!-- This the core and the only file needed if all you'll be doing is evaluating expresssions --> <script src="nerdamer.core.js"></script> <!-- LOAD ADD-ONS. These files contain extended functions. See documentation --> <!-- again assuming you've saved the files in root --> <script src="Algebra.js"></script> <script src="Calculus.js"></script> <script src="Solve.js"></script> <script src="Extra.js"></script> Or import everything html <script src="all.min.js"></script> <!-- assuming you've saved the file in the root --> If you're using node.js install it using npm i nerdamer and then

javascript // const cannot be used since nerdamer gets modified when other modules are loaded var nerdamer = require('nerdamer'); // Load additional modules. These are not required. require('nerdamer/Algebra'); require('nerdamer/Calculus'); require('nerdamer/Solve'); require('nerdamer/Extra'); Or do a single import to import everything javascript const nerdamer = require("nerdamer/all.min")

Some functions have dependencies from other add-ons.

You can see nerdamer in action at http://nerdamer.com/demo

For full documentation go to http://nerdamer.com/documentation

All operations are done using the 'nerdamer' object.

To add an expression just add it to the nerdamer object which will return a Expression object.

```javascript
var e = nerdamer('x^2+2(cos(x)+xx)'); console.log(e.text());

//result: //2cos(x)+3x^2 It is also possible to use `nerdamer` functions directly within the need for string manipulation of the input. The input will be parsed and the output will of type `Expression`. For example: javascript var ans = nerdamer.expand('(x-1)^5'); console.log(ans.text()); // -1-10x^2-5x^4+10x^3+5x+x^5

var sol = nerdamer.solve('x^2-4', 'x'); console.log(sol.text()) // [2,-2] ```

You can also pass in an object with known values as the second parameter.

```javascript
var e = nerdamer('x^2+2(cos(x)+xx)',{x:6}); console.log(e.text());

//result: //108+2*cos(6) ```

As you can see only the substitution is performed. To evaluate the result just call evaluate. Note that evaluate returns a text string or a number not an object.

```javascript
var e = nerdamer('x^2+2(cos(x)+xx)',{x:6}).evaluate(); console.log(e.text());

//result: //109.9203405733006 ```
To get back the text as a fraction, call the text method and pass in the string 'fractions'.

```javascript
var e = nerdamer('x^2+2(cos(x)+xx)',{x:6}).evaluate(); console.log(e.text('fractions'));

//result: //429607273/3908351 You can get your expression back as LaTeX by calling the toTeX method javascript
var LaTeX = nerdamer('x^2+2(cos(x)+xx)',{x:0.25}).toTeX(); console.log(LaTeX);

//result: //2 \cdot \mathrm{cos}\left(\frac{1}{4}\right)+\frac{3}{16} ```

To have numbers returned as decimals pass in the string 'decimals' to the toTeX method

```javascript
var LaTeX = nerdamer('x^2+2(cos(x)+xx)',{x:0.25}).toTeX('decimal'); console.log(LaTeX);

//result: //2 \cdot \mathrm{cos}\left(0.25\right)+0.1875 ```

Alternatively you can pass an object containing known values into evaluate method instead. The values passed in don't have to be number they can be another expression if needed.

```javascript
var e = nerdamer('x^2+2(cos(x)+xx)',{x:'x^2+1'}); console.log(e.text());

//result: //2cos(1+x^2)+3(1+x^2)^2 ```

Every time you parse an expression it's stored in nerdamer. To get a list of all the expressions you just call nerdamer.expressions().

```javascript
var knownValues = {x:'x^2+1'}; nerdamer('x^2+2(cos(x)+xx)').evaluate(knownValues); nerdamer('sin(x)^2+cos(x)^2').evaluate(knownValues);

console.log(nerdamer.expressions());

//result: //[ 46.692712758272776, 1 ] ```

You can request it as an object as well by passing in true. This can be convenient in some situations as the numbering starts at 1;

```javascript
var knownValues = {x:'x^2+1'}; nerdamer('x^2+2(cos(x)+xx)', knownValues ); nerdamer('sin(x)^2+cos(x)^2', knownValues );

console.log(nerdamer.expressions(true));

//{ '1': '2cos(1+x^(2))+3(1+x^(2))^(2)', //'2': 'cos(1+x^(2))^(2)+sin(1+x^(2))^(2)' } ```

Functions aren't always immediately parsed to numbers. For example

javascript var result = nerdamer('cos(x)',{x:6}); console.log(result.text()); //cos(6) will only subsitute out the variable name. To change this behaviour numer should be passed in as the 3rd argument.

javascript var result = nerdamer('cos(x)',{x:6}, 'numer'); console.log(result.text()); //0.960170286650366 or alternatively

javascript var result = nerdamer('cos(x)').evaluate({x:6}); console.log(result.text()); //0.960170286650366 The difference however is that the first option directly substitutes the variables while the second first evaluates the expression and then makes the substitutions. This library utilizes native javascript functions as much as possible. As a result it inherits whatever rounding errors they possess. One major change with version 0.6.0 however, is dealing with floating point issues.

javascript var result = nerdamer('sqrt(x)*sqrt(x)-2', {x: 2}); console.log(result.text()); //0 The above expample now returns zero whereas in previous version the result would be 4.440892098500626e-16. Same goes for 0.1+0.2.

An expression can be replaced directly by passing in the index of which expression to override. For example

javascript nerdamer('cos(x)',{x:6}, 'numer'); nerdamer('sin(x)+y',{x:6}, null, 1); console.log(nerdamer.expressions()); //[ 'sin(6)+y' ]

If multiple modifier options need to be passed into nerdamer you can do so using an array. For example ...

javascript var e = nerdamer('cos(x)+(y-x)^2', {x:7}, ['expand', 'numer']); console.log(e.text()); //-14*y+y^2+49.7539022543433

If you need the code as LaTeX you can pass in true as the second parameter when requesting the expressions.

```javascript
nerdamer('x^2+2(cos(x)+xx)'); nerdamer('sin(x)^0.25+cos(x)^0.5' ); var asObject = true; var asLaTeX = true; console.log(nerdamer.expressions(asObject, asLaTeX));

/{ '1': '2 \cdot \mathrm{cos}\left(x\right)+3 \cdot x^{2}', '2': '\sqrt{\mathrm{cos}\left(x\right)}+\mathrm{sin}\left(x\right)^{\frac{1}{4}}' }/ ```

You can specify a particular location when adding an expression, which is specified with the third parameter.

```javascript nerdamer('x^2+2(cos(x)+xx)'); nerdamer('sin(x)^0.25+cos(x)^0.5' ); nerdamer('expr-override', undefined, 2 ); var asObject = false; var asLaTeX = true; console.log(nerdamer.expressions(asObject, asLaTeX));

/* [ '2 \cdot \mathrm{cos}\left(x\right)+3 \cdot x^{2}', '\sqrt{\mathrm{cos}\left(x\right)}+\mathrm{sin}\left(x\right)^{\frac{1}{4}}', 'expr-override' ] */ ```

Here's an example of reserved variable and function names.

```javascript var reserved = nerdamer.reserved(); console.log(reserved); //result: /* csc, sec, cot, erf, fact, mod, GCD, QGCD, LCM, pow, PI, E, cos, sin, tan, acos, asin, atan, sinh, cosh, tanh, asinh, acosh, atanh, exp, min, max, floor, ceil, round, vector, matrix, parens, sqrt, log, expand, abs, invert, transpose, dot */

//or as an array

var reserved = nerdamer.reserved(true); console.log(reserved); //result: /* [ 'csc', 'sec', 'cot', 'erf', 'fact', 'mod', 'GCD', 'QGCD', 'LCM', 'pow', 'PI', 'E', 'cos', 'sin', 'tan', 'acos', 'asin', 'atan', 'sinh', 'cosh', 'tanh', 'asinh', 'acosh', 'atanh', 'exp', 'min', 'max', 'floor', 'ceil', 'round', 'vector', 'matrix', 'parens', 'sqrt', 'log', 'expand', 'abs', 'invert', 'transpose', 'dot' ] */ ```

Most math functions are passed in as part of the expression. If you want to differentiate for instance you just use the function diff which is located in the Calculus add-on as of version 0.5.0

```javascript
var e = nerdamer('diff(x^2+2(cos(x)+xx),x)');

console.log(e.text());

//result: //-2sin(x)+6x ```

Nerdamer can also handle runtime functions. To do this use the method setFunction. The runtime functions do have symbolic capabilities and support for imaginary numbers. The setfunction method is used as follows:

nerdamer.setFunction( functionname, parameterarray, function_body )

For Example:

```javascript
//generate some points var f = function(x) { return 5*x-1; } console.log(f(1)); //4 console.log(f(2)); //9 - value to be found console.log(f(7)); //34

nerdamer.setFunction('interpolate',['y0','x0','y1','x1','x'],'y0+(y1-y0)*((x-x0)/(x1-x0))') var answer = nerdamer('interpolate(4,1,34,7,2)').evaluate();

console.log(answer);

//result: 9 ```

Custom functions alternatively be set in following manner.

javascript nerdamer('hyp(a, b) := sqrt(a^2 + b^2) '); var result = nerdamer('hyp(3, 4)').evaluate().text(); console.log(result); //result: 5

If you need to add a constant use the setConstant method

javascript nerdamer.setConstant( 'g', 9.81); var weight = nerdamer('100*g').text(); console.log(weight); //result: //981

To delete just set it to delete

javascript nerdamer.setConstant( 'g', 9.81); var weight = nerdamer('100*g').text(); console.log(weight); //981 nerdamer.setConstant( 'g', 'delete'); var weight = nerdamer('100*g').text(); console.log(weight); //100*g

You also have the option of exporting your function to a javascript function which can be useful if you need some filtering from user input. Do keep in mind that the parameters are sorted alphabetically for more than one parameter. To use it add the expression to nerdamer and use the buildFunction method.

```javascript
var f = nerdamer('x^2+5').buildFunction(); console.log(f(9));

//result: //86 ```
If you have a particular order in which you need the parameters to be set, then you pass in an array with the variables in the order in which you want them for instance:

javascript var f = nerdamer('z+x^2+y').buildFunction(['y', 'x', 'z']); console.log(f(9,2,1)); //result //14

Every time you add an expression to nerdamer it's stored. To list the expressions currently in nerdamer call the 'expressions' method. To delete an expression use the 'clear' method and pass in the expression you want to delete. To clear everything pass in the string 'all'.

```javascript
nerdamer('nRT/v'); nerdamer('mc^2'); nerdamer('Gm1m2/d^2');

nerdamer.clear(2);

console.log(nerdamer.expressions(true));

//result: //{ '1': 'RTnv^(-1)', '2': 'Gd^(-2)m1m2' }

nerdamer.clear('all'); console.log(nerdamer.expressions(true)); //result: //{} ```

If you need go get the variables of an expression use the variables method. This method can be called after nerdamer was provided an expression. For example

javascript var variables = nerdamer('csc(x*cos(y))-no_boring_x').variables(); console.log(variables); //result: //[ 'no_boring_x', 'x', 'y' ]

The order in which the variables appear require a little bit of knowledge of how nerdamer organizes symbols. For the sake of simplicity we'll just assume that there is no particular order


Using the solver

To solve equations first load Solve.js. Just remember that Solve also required Algebra.js and Calculus.js to be loaded. You can then solve equations using nerdamer. Important: State the variable for which you are trying to solve. javascript var sol = nerdamer.solveEquations('x^3+8=x^2+6','x'); console.log(sol.toString()); //1+i,-i+1,-1

Notice that we use toString rather than text as this returns a javascript array.

You can also solve an expression javascript var e = nerdamer.solveEquations('x^2+4-y', 'y'); console.log(e[0].text()); //4+x^2

You can also solve multivariate equations javascript var sol = nerdamer.solveEquations('x^2+8+y=x+6','x'); console.log(sol.toString()); //0.5*((-4*y-7)^0.5+1),0.5*(-(-4*y-7)^0.5+1) You can do up to 3rd order polynomials for multivariate polynomials

Additionally you can try for equations containing functions. This is more of a hit or miss approach unlike single variable polynomials (which uses Mr. David Binner's Jenkins-Traub port - http://www.akiti.ca/PolyRootRe.html) but it's there if you want to give it a try.

javascript var sol = nerdamer.solveEquations('cos(x)+cos(3*x)=1','x'); console.log(sol.toString()); //5.7981235959208695,0.4850617112587174 To solve a system of linear equations pass them in as an array. For example

javascript var sol = nerdamer.solveEquations(['x+y=1', '2*x=6', '4*z+y=6']); console.log(sol); //[ [ 'x', 3 ], [ 'y', -2 ], [ 'z', 2 ] ] In version 0.7.2 and up the solver can additionally be used in the following way javascript //first parse the equation var x = nerdamer('x^2+2=y-7*a'); //You can make substitutions to the equation x = x.evaluate({a: 'x^2-3'}); console.log(x.toString()); //2+x^2=-7*x^2+21+y var solutions = x.solveFor('x'); console.log(solutions.toString()); //(1/16)*sqrt(32*y+608),(-1/16)*sqrt(32*y+608)

Owner

  • Name: Martin Donk
  • Login: jiggzson
  • Kind: user

Citation (CITATION.cff)

cff-version: 1.2.0
title: "Nerdamer"
message: "If you use this software, please cite it as below.""
version: 1.1.3
date-released: 2021-11-01
url: "https://nerdamer.com"
license: "MIT License"
authors:
  - family-names: Donk
    given-names: Martin

GitHub Events

Total
  • Watch event: 20
  • Push event: 1
  • Fork event: 2
Last Year
  • Watch event: 20
  • Push event: 1
  • Fork event: 2

Committers

Last synced: 8 months ago

All Time
  • Total Commits: 890
  • Total Committers: 29
  • Avg Commits per committer: 30.69
  • Development Distribution Score (DDS): 0.197
Past Year
  • Commits: 1
  • Committers: 1
  • Avg Commits per committer: 1.0
  • Development Distribution Score (DDS): 0.0
Top Committers
Name Email Commits
Martin Donk j****n@g****m 715
jiggzson@gmail.com m****n@m****e 60
Martin Donk m****k@r****m 15
tecosaur 2****r 13
Douglas Moura D****a 11
Brosnan Yuen b****n@g****m 11
Grant Sander g****3@g****m 9
ljacqu l****u 7
Hovhannes Menejan s****o@g****m 7
phcreery p****y@g****m 6
Happypig375 h****g@o****m 5
Viktor v****9@y****u 4
Brosnan Yuen b****n 4
Vojtěch Jelínek v****j@g****m 3
Kevin Li k****8@g****m 3
Adrian Leonhard a****d@g****m 3
mdonk m****k@g****m 2
menzelths m****s@y****e 1
iuliia y****i@g****m 1
Mert m****t@t****m 1
iamlearning j****g@a****m 1
jiggzson@gmail.com 1
Yosuke Matsusaka y****a@g****m 1
N00bie-to-Github j****n@a****m 1
Muhammad Abdelsalam e****0@g****m 1
kungfooman l****5@g****m 1
P.L. Lucas s****i@g****m 1
Matvey Safronov d****2@g****m 1
Amy Y 5****3 1
Committer Domains (Top 20 + Academic)

Issues and Pull Requests

Last synced: 5 months ago

All Time
  • Total issues: 140
  • Total pull requests: 25
  • Average time to close issues: 4 months
  • Average time to close pull requests: 29 days
  • Total issue authors: 48
  • Total pull request authors: 9
  • Average comments per issue: 3.24
  • Average comments per pull request: 0.48
  • Merged pull requests: 18
  • Bot issues: 0
  • Bot pull requests: 0
Past Year
  • Issues: 1
  • Pull requests: 0
  • Average time to close issues: N/A
  • Average time to close pull requests: N/A
  • Issue authors: 1
  • Pull request authors: 0
  • Average comments per issue: 0.0
  • Average comments per pull request: 0
  • Merged pull requests: 0
  • Bot issues: 0
  • Bot pull requests: 0
Top Authors
Issue Authors
  • szoshi (9)
  • gunnarmein-ts (6)
  • Happypig375 (4)
  • SomebodyLikeEveryBody (4)
  • markhats (3)
  • Saikedo (3)
  • Matt0017 (3)
  • michaelcheers (3)
  • mbassenne (3)
  • MAGNETO903 (3)
  • Ephraim-Bryski (3)
  • BOISSARD (2)
  • Anonymus0999 (2)
  • roy77 (2)
  • Jeff-Aporta (2)
Pull Request Authors
  • jiggzson (4)
  • phcreery (2)
  • amydevs (2)
  • mugabe (1)
  • mert574 (1)
  • vojtechjelinek (1)
  • myrriad (1)
  • zopieux (1)
  • cdonnelly1992 (1)
Top Labels
Issue Labels
bug (5) active (2) enhancement (2) help wanted (1) moved (1)
Pull Request Labels

Packages

  • Total packages: 2
  • Total downloads:
    • npm 10,149 last-month
  • Total dependent packages: 19
    (may contain duplicates)
  • Total dependent repositories: 124
    (may contain duplicates)
  • Total versions: 40
  • Total maintainers: 3
npmjs.org: nerdamer

javascript light-weight symbolic math expression evaluator

  • Versions: 37
  • Dependent Packages: 17
  • Dependent Repositories: 123
  • Downloads: 10,130 Last month
Rankings
Dependent repos count: 1.3%
Dependent packages count: 1.4%
Downloads: 1.6%
Average: 2.1%
Stargazers count: 3.1%
Forks count: 3.3%
Maintainers (2)
Last synced: 5 months ago
npmjs.org: nerdamer-fork

javascript light-weight symbolic math expression evaluator

  • Versions: 3
  • Dependent Packages: 2
  • Dependent Repositories: 1
  • Downloads: 19 Last month
Rankings
Stargazers count: 3.1%
Forks count: 3.3%
Dependent packages count: 8.9%
Dependent repos count: 10.4%
Average: 13.3%
Downloads: 40.9%
Maintainers (1)
Last synced: 5 months ago

Dependencies

package.json npm
  • gulp ^4.0.2 development
  • gulp-cli ^2.3.0 development
  • gulp-concat ^2.6.1 development
  • gulp-uglify ^3.0.2 development
  • gulp-watch ^5.0.1 development
  • jasmine ^2.99.0 development