├── index.js ├── test ├── type │ ├── collection.test.js │ └── ResultSet.test.js ├── expression │ └── keywords.test.js ├── function │ ├── probability │ │ ├── random.test.js │ │ ├── randomInt.test.js │ │ └── pickRandom.test.js │ ├── construction │ │ ├── parser.test.js │ │ ├── chain.test.js │ │ └── index.test.js │ ├── expression │ │ └── parse.test.js │ └── matrix │ │ └── squeeze.test.js ├── error │ └── index.test.js ├── README.md ├── test.min.html └── util │ └── boolean.test.js ├── .travis.yml ├── img ├── favicon.ico ├── favicon.png ├── mathjs.png ├── favicon_16x16.png ├── favicon_75x75.png ├── mathjs_100x30.png ├── mathjs_165x50.png ├── mathjs_330x100.png └── mathjs.txt ├── .gitignore ├── docs ├── chained_operations.md ├── expressions.md ├── reference │ ├── index.md │ └── functions │ │ ├── det.md │ │ ├── flatten.md │ │ ├── trace.md │ │ ├── clone.md │ │ ├── help.md │ │ ├── pickRandom.md │ │ ├── inv.md │ │ ├── forEach.md │ │ ├── transpose.md │ │ ├── csc.md │ │ ├── sec.md │ │ ├── cosh.md │ │ ├── sinh.md │ │ ├── cot.md │ │ ├── dot.md │ │ ├── combinations.md │ │ ├── factorial.md │ │ ├── size.md │ │ ├── sech.md │ │ ├── csch.md │ │ ├── coth.md │ │ ├── abs.md │ │ ├── sqrt.md │ │ ├── acos.md │ │ ├── map.md │ │ ├── sum.md │ │ ├── xgcd.md │ │ ├── asin.md │ │ ├── atan.md │ │ ├── not.md │ │ ├── permutations.md │ │ ├── distribution.md │ │ ├── to.md │ │ ├── gcd.md │ │ ├── bitNot.md │ │ ├── gamma.md │ │ ├── log10.md │ │ ├── ones.md │ │ ├── eval.md │ │ ├── zeros.md │ │ ├── sign.md │ │ ├── filter.md │ │ ├── cube.md │ │ ├── concat.md │ │ ├── prod.md │ │ ├── tan.md │ │ ├── cross.md │ │ ├── unaryPlus.md │ │ ├── eye.md │ │ ├── lcm.md │ │ ├── conj.md │ │ ├── square.md │ │ ├── exp.md │ │ ├── bitXor.md │ │ ├── bitAnd.md │ │ ├── unaryMinus.md │ │ ├── tanh.md │ │ ├── arg.md │ │ ├── dotPow.md │ │ ├── re.md │ │ ├── sin.md │ │ ├── unit.md │ │ ├── cos.md │ │ ├── median.md │ │ ├── im.md │ │ ├── fix.md │ │ ├── parse.md │ │ ├── string.md │ │ ├── nthRoot.md │ │ ├── floor.md │ │ ├── pow.md │ │ ├── resize.md │ │ ├── sort.md │ │ ├── bitOr.md │ │ ├── rightLogShift.md │ │ ├── dotDivide.md │ │ ├── or.md │ │ ├── and.md │ │ ├── bignumber.md │ │ └── squeeze.md ├── expressions │ └── index.md ├── command_line_interface.md └── chaining.md ├── lib ├── expression │ ├── keywords.js │ ├── docs │ │ ├── constants │ │ │ ├── NaN.js │ │ │ ├── false.js │ │ │ ├── null.js │ │ │ ├── true.js │ │ │ ├── version.js │ │ │ ├── LN2.js │ │ │ ├── SQRT2.js │ │ │ ├── LN10.js │ │ │ ├── LOG2E.js │ │ │ ├── SQRT1_2.js │ │ │ ├── LOG10E.js │ │ │ ├── tau.js │ │ │ ├── e.js │ │ │ ├── Infinity.js │ │ │ ├── i.js │ │ │ ├── pi.js │ │ │ └── phi.js │ │ └── function │ │ │ ├── arithmetic │ │ │ ├── abs.js │ │ │ ├── lcm.js │ │ │ ├── pow.js │ │ │ ├── gcd.js │ │ │ ├── xgcd.js │ │ │ ├── dotPow.js │ │ │ ├── unaryPlus.js │ │ │ ├── cube.js │ │ │ ├── add.js │ │ │ ├── subtract.js │ │ │ ├── multiply.js │ │ │ ├── sign.js │ │ │ ├── sqrt.js │ │ │ ├── divide.js │ │ │ ├── fix.js │ │ │ ├── log10.js │ │ │ ├── unaryMinus.js │ │ │ ├── ceil.js │ │ │ ├── exp.js │ │ │ ├── dotDivide.js │ │ │ ├── floor.js │ │ │ ├── square.js │ │ │ ├── dotMultiply.js │ │ │ ├── mod.js │ │ │ ├── nthRoot.js │ │ │ ├── norm.js │ │ │ ├── round.js │ │ │ └── log.js │ │ │ ├── utils │ │ │ ├── import.js │ │ │ ├── typeof.js │ │ │ ├── filter.js │ │ │ ├── format.js │ │ │ ├── forEach.js │ │ │ ├── map.js │ │ │ ├── clone.js │ │ │ ├── print.js │ │ │ └── sort.js │ │ │ ├── units │ │ │ └── to.js │ │ │ ├── trigonometry │ │ │ ├── sinh.js │ │ │ ├── cosh.js │ │ │ ├── asin.js │ │ │ ├── cot.js │ │ │ ├── csc.js │ │ │ ├── sec.js │ │ │ ├── acos.js │ │ │ ├── atan.js │ │ │ ├── tanh.js │ │ │ ├── coth.js │ │ │ ├── csch.js │ │ │ ├── sech.js │ │ │ ├── tan.js │ │ │ ├── sin.js │ │ │ ├── cos.js │ │ │ └── atan2.js │ │ │ ├── expression │ │ │ ├── help.js │ │ │ └── eval.js │ │ │ ├── probability │ │ │ ├── combinations.js │ │ │ ├── pickRandom.js │ │ │ ├── factorial.js │ │ │ ├── permutations.js │ │ │ ├── gamma.js │ │ │ ├── random.js │ │ │ ├── randomInt.js │ │ │ └── distribution.js │ │ │ ├── complex │ │ │ ├── re.js │ │ │ ├── im.js │ │ │ ├── arg.js │ │ │ └── conj.js │ │ │ ├── logical │ │ │ ├── not.js │ │ │ ├── or.js │ │ │ ├── and.js │ │ │ └── xor.js │ │ │ ├── matrix │ │ │ ├── inv.js │ │ │ ├── flatten.js │ │ │ ├── det.js │ │ │ ├── cross.js │ │ │ ├── transpose.js │ │ │ ├── resize.js │ │ │ ├── size.js │ │ │ ├── trace.js │ │ │ ├── squeeze.js │ │ │ ├── dot.js │ │ │ ├── eye.js │ │ │ ├── zeros.js │ │ │ ├── ones.js │ │ │ ├── concat.js │ │ │ ├── range.js │ │ │ ├── subset.js │ │ │ └── diag.js │ │ │ ├── bitwise │ │ │ ├── leftShift.js │ │ │ ├── rightArithShift.js │ │ │ ├── rightLogShift.js │ │ │ ├── bitNot.js │ │ │ ├── bitOr.js │ │ │ ├── bitXor.js │ │ │ └── bitAnd.js │ │ │ ├── construction │ │ │ ├── string.js │ │ │ ├── complex.js │ │ │ ├── unit.js │ │ │ ├── bignumber.js │ │ │ ├── matrix.js │ │ │ ├── boolean.js │ │ │ ├── number.js │ │ │ └── index.js │ │ │ ├── statistics │ │ │ ├── sum.js │ │ │ ├── prod.js │ │ │ ├── median.js │ │ │ ├── mean.js │ │ │ ├── max.js │ │ │ ├── min.js │ │ │ ├── var.js │ │ │ └── std.js │ │ │ └── relational │ │ │ ├── deepEqual.js │ │ │ ├── largerEq.js │ │ │ ├── smallerEq.js │ │ │ ├── compare.js │ │ │ ├── equal.js │ │ │ ├── smaller.js │ │ │ ├── larger.js │ │ │ └── unequal.js │ ├── transform │ │ ├── error.transform.js │ │ ├── subset.transform.js │ │ ├── range.transform.js │ │ └── concat.transform.js │ └── node │ │ └── index.js ├── version.js ├── util │ ├── boolean.js │ ├── index.js │ ├── function.js │ └── types.js ├── error │ ├── index.js │ ├── ArgumentsError.js │ └── UnsupportedTypeError.js ├── function │ ├── probability │ │ └── pickRandom.js │ └── utils │ │ └── clone.js ├── type │ └── ResultSet.js └── header.js ├── .npmignore ├── bower.json ├── examples └── browser │ ├── requirejs_loading.html │ ├── webworkers │ └── worker.js │ └── basic_usage.html ├── misc └── registrations.md ├── CONTRIBUTING.md ├── component.json ├── NOTICE └── ROADMAP.md /index.js: -------------------------------------------------------------------------------- 1 | module.exports = require('./lib/math.js'); 2 | -------------------------------------------------------------------------------- /test/type/collection.test.js: -------------------------------------------------------------------------------- 1 | // TODO: test collection.js -------------------------------------------------------------------------------- /.travis.yml: -------------------------------------------------------------------------------- 1 | language: node_js 2 | node_js: 3 | - 0.10 4 | - 0.11 5 | -------------------------------------------------------------------------------- /img/favicon.ico: -------------------------------------------------------------------------------- https://raw.githubusercontent.com/Wikunia/mathjs/master/img/favicon.ico -------------------------------------------------------------------------------- /img/favicon.png: -------------------------------------------------------------------------------- https://raw.githubusercontent.com/Wikunia/mathjs/master/img/favicon.png -------------------------------------------------------------------------------- /img/mathjs.png: -------------------------------------------------------------------------------- https://raw.githubusercontent.com/Wikunia/mathjs/master/img/mathjs.png -------------------------------------------------------------------------------- /.gitignore: -------------------------------------------------------------------------------- 1 | .idea 2 | .c9 3 | _site 4 | coverage 5 | *swp 6 | node_modules 7 | *.log 8 | -------------------------------------------------------------------------------- /img/favicon_16x16.png: -------------------------------------------------------------------------------- https://raw.githubusercontent.com/Wikunia/mathjs/master/img/favicon_16x16.png -------------------------------------------------------------------------------- /img/favicon_75x75.png: -------------------------------------------------------------------------------- https://raw.githubusercontent.com/Wikunia/mathjs/master/img/favicon_75x75.png -------------------------------------------------------------------------------- /img/mathjs_100x30.png: -------------------------------------------------------------------------------- https://raw.githubusercontent.com/Wikunia/mathjs/master/img/mathjs_100x30.png -------------------------------------------------------------------------------- /img/mathjs_165x50.png: -------------------------------------------------------------------------------- https://raw.githubusercontent.com/Wikunia/mathjs/master/img/mathjs_165x50.png -------------------------------------------------------------------------------- /img/mathjs_330x100.png: -------------------------------------------------------------------------------- https://raw.githubusercontent.com/Wikunia/mathjs/master/img/mathjs_330x100.png -------------------------------------------------------------------------------- /docs/chained_operations.md: -------------------------------------------------------------------------------- 1 | # Chained operations 2 | 3 | This page has been moved to [Chaining](chaining.md). -------------------------------------------------------------------------------- /docs/expressions.md: -------------------------------------------------------------------------------- 1 | # Expressions 2 | 3 | This page has been moved here: [Expressions](expressions/index.md). 4 | -------------------------------------------------------------------------------- /lib/expression/keywords.js: -------------------------------------------------------------------------------- 1 | 'use strict'; 2 | 3 | // Reserved keywords not allowed to use in the parser 4 | module.exports = { 5 | end: true 6 | }; 7 | -------------------------------------------------------------------------------- /lib/version.js: -------------------------------------------------------------------------------- 1 | module.exports = '1.3.0'; 2 | // Note: This file is automatically generated when building math.js. 3 | // Changes made in this file will be overwritten. 4 | -------------------------------------------------------------------------------- /.npmignore: -------------------------------------------------------------------------------- 1 | _site 2 | coverage 3 | img 4 | misc 5 | node_modules 6 | tools 7 | component.json 8 | bower.json 9 | gulpfile.js 10 | .idea 11 | .npmignore 12 | .travis.yml 13 | -------------------------------------------------------------------------------- /docs/reference/index.md: -------------------------------------------------------------------------------- 1 | # Reference 2 | 3 | - [Constants](constants.md) 4 | - [Functions (alphabetical)](functions/alphabetical.md) 5 | - [Functions (categorical)](functions/categorical.md) 6 | - [Units](units.md) 7 | -------------------------------------------------------------------------------- /lib/util/boolean.js: -------------------------------------------------------------------------------- 1 | 'use strict'; 2 | 3 | /** 4 | * Test whether value is a Boolean 5 | * @param {*} value 6 | * @return {Boolean} isBoolean 7 | */ 8 | exports.isBoolean = function(value) { 9 | return (value instanceof Boolean) || (typeof value == 'boolean'); 10 | }; 11 | -------------------------------------------------------------------------------- /lib/expression/docs/constants/NaN.js: -------------------------------------------------------------------------------- 1 | module.exports = { 2 | 'name': 'NaN', 3 | 'category': 'Constants', 4 | 'syntax': [ 5 | 'NaN' 6 | ], 7 | 'description': 'Not a number', 8 | 'examples': [ 9 | 'NaN', 10 | '0 / 0' 11 | ], 12 | 'seealso': [] 13 | }; 14 | -------------------------------------------------------------------------------- /lib/expression/docs/constants/false.js: -------------------------------------------------------------------------------- 1 | module.exports = { 2 | 'name': 'false', 3 | 'category': 'Constants', 4 | 'syntax': [ 5 | 'false' 6 | ], 7 | 'description': 'Boolean value false', 8 | 'examples': [ 9 | 'false' 10 | ], 11 | 'seealso': ['true'] 12 | }; 13 | -------------------------------------------------------------------------------- /lib/expression/docs/constants/null.js: -------------------------------------------------------------------------------- 1 | module.exports = { 2 | 'name': 'null', 3 | 'category': 'Constants', 4 | 'syntax': [ 5 | 'null' 6 | ], 7 | 'description': 'Value null', 8 | 'examples': [ 9 | 'null' 10 | ], 11 | 'seealso': ['true', 'false'] 12 | }; 13 | -------------------------------------------------------------------------------- /lib/expression/docs/constants/true.js: -------------------------------------------------------------------------------- 1 | module.exports = { 2 | 'name': 'true', 3 | 'category': 'Constants', 4 | 'syntax': [ 5 | 'true' 6 | ], 7 | 'description': 'Boolean value true', 8 | 'examples': [ 9 | 'true' 10 | ], 11 | 'seealso': ['false'] 12 | }; 13 | -------------------------------------------------------------------------------- /img/mathjs.txt: -------------------------------------------------------------------------------- 1 | Description of the logo: 2 | 3 | size: 330x100px 4 | font: Source Sans Pro, 96px 5 | 6 | math: 7 | size: 243x100px 8 | color: ffffffff 9 | bgcolor: dc3912ff 10 | 11 | js: 12 | size: 87x100px 13 | color: 202020ff 14 | bgcolor: ffffffff 15 | 16 | -------------------------------------------------------------------------------- /lib/expression/docs/constants/version.js: -------------------------------------------------------------------------------- 1 | module.exports = { 2 | 'name': 'version', 3 | 'category': 'Constants', 4 | 'syntax': [ 5 | 'version' 6 | ], 7 | 'description': 'A string with the version number of math.js', 8 | 'examples': [ 9 | 'version' 10 | ], 11 | 'seealso': [] 12 | }; 13 | -------------------------------------------------------------------------------- /lib/error/index.js: -------------------------------------------------------------------------------- 1 | 'use strict'; 2 | 3 | exports.ArgumentsError = require('./ArgumentsError'); 4 | exports.DimensionError = require('./DimensionError'); 5 | exports.IndexError = require('./IndexError'); 6 | exports.UnsupportedTypeError = require('./UnsupportedTypeError'); 7 | 8 | // TODO: implement an InvalidValueError? 9 | -------------------------------------------------------------------------------- /lib/expression/docs/function/arithmetic/abs.js: -------------------------------------------------------------------------------- 1 | module.exports = { 2 | 'name': 'abs', 3 | 'category': 'Arithmetic', 4 | 'syntax': [ 5 | 'abs(x)' 6 | ], 7 | 'description': 'Compute the absolute value.', 8 | 'examples': [ 9 | 'abs(3.5)', 10 | 'abs(-4.2)' 11 | ], 12 | 'seealso': ['sign'] 13 | }; 14 | -------------------------------------------------------------------------------- /lib/expression/docs/constants/LN2.js: -------------------------------------------------------------------------------- 1 | module.exports = { 2 | 'name': 'LN2', 3 | 'category': 'Constants', 4 | 'syntax': [ 5 | 'LN2' 6 | ], 7 | 'description': 'Returns the natural logarithm of 2, approximately equal to 0.693', 8 | 'examples': [ 9 | 'LN2', 10 | 'log(2)' 11 | ], 12 | 'seealso': [] 13 | }; 14 | -------------------------------------------------------------------------------- /lib/expression/docs/constants/SQRT2.js: -------------------------------------------------------------------------------- 1 | module.exports = { 2 | 'name': 'SQRT2', 3 | 'category': 'Constants', 4 | 'syntax': [ 5 | 'SQRT2' 6 | ], 7 | 'description': 'Returns the square root of 2, approximately equal to 1.414', 8 | 'examples': [ 9 | 'SQRT2', 10 | 'sqrt(2)' 11 | ], 12 | 'seealso': [] 13 | }; 14 | -------------------------------------------------------------------------------- /lib/expression/docs/constants/LN10.js: -------------------------------------------------------------------------------- 1 | module.exports = { 2 | 'name': 'LN10', 3 | 'category': 'Constants', 4 | 'syntax': [ 5 | 'LN10' 6 | ], 7 | 'description': 'Returns the natural logarithm of 10, approximately equal to 2.302', 8 | 'examples': [ 9 | 'LN10', 10 | 'log(10)' 11 | ], 12 | 'seealso': [] 13 | }; 14 | -------------------------------------------------------------------------------- /lib/expression/docs/constants/LOG2E.js: -------------------------------------------------------------------------------- 1 | module.exports = { 2 | 'name': 'LOG2E', 3 | 'category': 'Constants', 4 | 'syntax': [ 5 | 'LOG2E' 6 | ], 7 | 'description': 'Returns the base-2 logarithm of E, approximately equal to 1.442', 8 | 'examples': [ 9 | 'LOG2E', 10 | 'log(e, 2)' 11 | ], 12 | 'seealso': [] 13 | }; 14 | -------------------------------------------------------------------------------- /lib/util/index.js: -------------------------------------------------------------------------------- 1 | 'use strict'; 2 | 3 | exports.array = require('./array'); 4 | exports['boolean'] = require('./boolean'); 5 | exports.number = require('./number'); 6 | exports.bignumber = require('./bignumber'); 7 | exports.object = require('./object'); 8 | exports.string = require('./string'); 9 | exports.types = require('./types'); 10 | -------------------------------------------------------------------------------- /lib/expression/docs/constants/SQRT1_2.js: -------------------------------------------------------------------------------- 1 | module.exports = { 2 | 'name': 'SQRT1_2', 3 | 'category': 'Constants', 4 | 'syntax': [ 5 | 'SQRT1_2' 6 | ], 7 | 'description': 'Returns the square root of 1/2, approximately equal to 0.707', 8 | 'examples': [ 9 | 'SQRT1_2', 10 | 'sqrt(1/2)' 11 | ], 12 | 'seealso': [] 13 | }; 14 | -------------------------------------------------------------------------------- /lib/expression/docs/function/utils/import.js: -------------------------------------------------------------------------------- 1 | module.exports = { 2 | 'name': 'import', 3 | 'category': 'Utils', 4 | 'syntax': [ 5 | 'import(string)' 6 | ], 7 | 'description': 'Import functions from a file.', 8 | 'examples': [ 9 | 'import("numbers")', 10 | 'import("./mylib.js")' 11 | ], 12 | 'seealso': [] 13 | }; 14 | -------------------------------------------------------------------------------- /lib/expression/docs/constants/LOG10E.js: -------------------------------------------------------------------------------- 1 | module.exports = { 2 | 'name': 'LOG10E', 3 | 'category': 'Constants', 4 | 'syntax': [ 5 | 'LOG10E' 6 | ], 7 | 'description': 'Returns the base-10 logarithm of E, approximately equal to 0.434', 8 | 'examples': [ 9 | 'LOG10E', 10 | 'log(e, 10)' 11 | ], 12 | 'seealso': [] 13 | }; 14 | -------------------------------------------------------------------------------- /lib/expression/docs/function/arithmetic/lcm.js: -------------------------------------------------------------------------------- 1 | module.exports = { 2 | 'name': 'lcm', 3 | 'category': 'Arithmetic', 4 | 'syntax': [ 5 | 'lcm(x, y)' 6 | ], 7 | 'description': 'Compute the least common multiple.', 8 | 'examples': [ 9 | 'lcm(4, 6)', 10 | 'lcm(6, 21)', 11 | 'lcm(6, 21, 5)' 12 | ], 13 | 'seealso': [ 'gcd' ] 14 | }; 15 | -------------------------------------------------------------------------------- /test/expression/keywords.test.js: -------------------------------------------------------------------------------- 1 | // test keywords 2 | var assert = require('assert'), 3 | keywords = require('../../lib/expression/keywords'); 4 | 5 | describe('keywords', function() { 6 | 7 | it('should return a map with reserved keywords', function() { 8 | assert.deepEqual(Object.keys(keywords).sort(), ['end'].sort()); 9 | }); 10 | 11 | }); 12 | -------------------------------------------------------------------------------- /lib/expression/docs/function/units/to.js: -------------------------------------------------------------------------------- 1 | module.exports = { 2 | 'name': 'to', 3 | 'category': 'Units', 4 | 'syntax': [ 5 | 'x to unit', 6 | 'to(x, unit)' 7 | ], 8 | 'description': 'Change the unit of a value.', 9 | 'examples': [ 10 | '5 inch to cm', 11 | '3.2kg to g', 12 | '16 bytes in bits' 13 | ], 14 | 'seealso': [] 15 | }; 16 | -------------------------------------------------------------------------------- /lib/expression/docs/constants/tau.js: -------------------------------------------------------------------------------- 1 | module.exports = { 2 | 'name': 'tau', 3 | 'category': 'Constants', 4 | 'syntax': [ 5 | 'tau' 6 | ], 7 | 'description': 'Tau is the ratio constant of a circle\'s circumference to radius, equal to 2 * pi, approximately 6.2832.', 8 | 'examples': [ 9 | 'tau', 10 | '2 * pi' 11 | ], 12 | 'seealso': ['pi'] 13 | }; 14 | -------------------------------------------------------------------------------- /lib/expression/docs/function/trigonometry/sinh.js: -------------------------------------------------------------------------------- 1 | module.exports = { 2 | 'name': 'sinh', 3 | 'category': 'Trigonometry', 4 | 'syntax': [ 5 | 'sinh(x)' 6 | ], 7 | 'description': 'Compute the hyperbolic sine of x in radians.', 8 | 'examples': [ 9 | 'sinh(0.5)' 10 | ], 11 | 'seealso': [ 12 | 'cosh', 13 | 'tanh' 14 | ] 15 | }; 16 | -------------------------------------------------------------------------------- /lib/expression/docs/function/expression/help.js: -------------------------------------------------------------------------------- 1 | module.exports = { 2 | 'name': 'help', 3 | 'category': 'Expression', 4 | 'syntax': [ 5 | 'help(object)', 6 | 'help(string)' 7 | ], 8 | 'description': 'Display documentation on a function or data type.', 9 | 'examples': [ 10 | 'help(sqrt)', 11 | 'help("complex")' 12 | ], 13 | 'seealso': [] 14 | }; 15 | -------------------------------------------------------------------------------- /lib/expression/docs/constants/e.js: -------------------------------------------------------------------------------- 1 | module.exports = { 2 | 'name': 'e', 3 | 'category': 'Constants', 4 | 'syntax': [ 5 | 'e' 6 | ], 7 | 'description': 'Euler\'s number, the base of the natural logarithm. Approximately equal to 2.71828', 8 | 'examples': [ 9 | 'e', 10 | 'e ^ 2', 11 | 'exp(2)', 12 | 'log(e)' 13 | ], 14 | 'seealso': ['exp'] 15 | }; 16 | -------------------------------------------------------------------------------- /lib/expression/docs/function/utils/typeof.js: -------------------------------------------------------------------------------- 1 | module.exports = { 2 | 'name': 'typeof', 3 | 'category': 'Utils', 4 | 'syntax': [ 5 | 'typeof(x)' 6 | ], 7 | 'description': 'Get the type of a variable.', 8 | 'examples': [ 9 | 'typeof(3.5)', 10 | 'typeof(2 - 4i)', 11 | 'typeof(45 deg)', 12 | 'typeof("hello world")' 13 | ], 14 | 'seealso': [] 15 | }; 16 | -------------------------------------------------------------------------------- /bower.json: -------------------------------------------------------------------------------- 1 | { 2 | "name": "mathjs", 3 | "version": "1.3.0", 4 | "main": "./dist/math.min.js", 5 | "ignore": [ 6 | "coverage", 7 | "img", 8 | "misc", 9 | "node_modules", 10 | "test", 11 | "tools", 12 | ".gitignore", 13 | ".npmignore", 14 | ".travis.yml", 15 | "gulpfile.js", 16 | "component.json", 17 | "package.json" 18 | ] 19 | } 20 | -------------------------------------------------------------------------------- /lib/expression/docs/constants/Infinity.js: -------------------------------------------------------------------------------- 1 | module.exports = { 2 | 'name': 'Infinity', 3 | 'category': 'Constants', 4 | 'syntax': [ 5 | 'Infinity' 6 | ], 7 | 'description': 'Infinity, a number which is larger than the maximum number that can be handled by a floating point number.', 8 | 'examples': [ 9 | 'Infinity', 10 | '1 / 0' 11 | ], 12 | 'seealso': [] 13 | }; 14 | -------------------------------------------------------------------------------- /lib/expression/docs/function/arithmetic/pow.js: -------------------------------------------------------------------------------- 1 | module.exports = { 2 | 'name': 'pow', 3 | 'category': 'Operators', 4 | 'syntax': [ 5 | 'x ^ y', 6 | 'pow(x, y)' 7 | ], 8 | 'description': 9 | 'Calculates the power of x to y, x^y.', 10 | 'examples': [ 11 | '2^3 = 8', 12 | '2*2*2', 13 | '1 + e ^ (pi * i)' 14 | ], 15 | 'seealso': [ 'multiply' ] 16 | }; 17 | -------------------------------------------------------------------------------- /lib/expression/docs/function/trigonometry/cosh.js: -------------------------------------------------------------------------------- 1 | module.exports = { 2 | 'name': 'cosh', 3 | 'category': 'Trigonometry', 4 | 'syntax': [ 5 | 'cosh(x)' 6 | ], 7 | 'description': 'Compute the hyperbolic cosine of x in radians.', 8 | 'examples': [ 9 | 'cosh(0.5)' 10 | ], 11 | 'seealso': [ 12 | 'sinh', 13 | 'tanh', 14 | 'coth' 15 | ] 16 | }; 17 | -------------------------------------------------------------------------------- /lib/expression/docs/function/probability/combinations.js: -------------------------------------------------------------------------------- 1 | module.exports = { 2 | 'name': 'combinations', 3 | 'category': 'Probability', 4 | 'syntax': [ 5 | 'combinations(n, k)' 6 | ], 7 | 'description': 'Compute the number of combinations of n items taken k at a time', 8 | 'examples': [ 9 | 'combinations(7, 5)' 10 | ], 11 | 'seealso': ['permutations', 'factorial'] 12 | }; 13 | -------------------------------------------------------------------------------- /lib/expression/docs/function/arithmetic/gcd.js: -------------------------------------------------------------------------------- 1 | module.exports = { 2 | 'name': 'gcd', 3 | 'category': 'Arithmetic', 4 | 'syntax': [ 5 | 'gcd(a, b)', 6 | 'gcd(a, b, c, ...)' 7 | ], 8 | 'description': 'Compute the greatest common divisor.', 9 | 'examples': [ 10 | 'gcd(8, 12)', 11 | 'gcd(-4, 6)', 12 | 'gcd(25, 15, -10)' 13 | ], 14 | 'seealso': [ 'lcm', 'xgcd' ] 15 | }; 16 | -------------------------------------------------------------------------------- /lib/expression/docs/function/arithmetic/xgcd.js: -------------------------------------------------------------------------------- 1 | module.exports = { 2 | 'name': 'xgcd', 3 | 'category': 'Arithmetic', 4 | 'syntax': [ 5 | 'xgcd(a, b)' 6 | ], 7 | 'description': 'Calculate the extended greatest common divisor for two values', 8 | 'examples': [ 9 | 'xgcd(8, 12)', 10 | 'gcd(8, 12)', 11 | 'xgcd(36163, 21199)' 12 | ], 13 | 'seealso': [ 'gcd', 'lcm' ] 14 | }; 15 | -------------------------------------------------------------------------------- /lib/expression/docs/function/trigonometry/asin.js: -------------------------------------------------------------------------------- 1 | module.exports = { 2 | 'name': 'asin', 3 | 'category': 'Trigonometry', 4 | 'syntax': [ 5 | 'asin(x)' 6 | ], 7 | 'description': 'Compute the inverse sine of a value in radians.', 8 | 'examples': [ 9 | 'asin(0.5)', 10 | 'asin(sin(2.3))' 11 | ], 12 | 'seealso': [ 13 | 'sin', 14 | 'acos', 15 | 'atan' 16 | ] 17 | }; 18 | -------------------------------------------------------------------------------- /lib/expression/docs/function/trigonometry/cot.js: -------------------------------------------------------------------------------- 1 | module.exports = { 2 | 'name': 'cot', 3 | 'category': 'Trigonometry', 4 | 'syntax': [ 5 | 'cot(x)' 6 | ], 7 | 'description': 'Compute the cotangent of x in radians. Defined as 1/tan(x)', 8 | 'examples': [ 9 | 'cot(2)', 10 | '1 / tan(2)' 11 | ], 12 | 'seealso': [ 13 | 'sec', 14 | 'csc', 15 | 'tan' 16 | ] 17 | }; 18 | -------------------------------------------------------------------------------- /lib/expression/docs/function/trigonometry/csc.js: -------------------------------------------------------------------------------- 1 | module.exports = { 2 | 'name': 'csc', 3 | 'category': 'Trigonometry', 4 | 'syntax': [ 5 | 'csc(x)' 6 | ], 7 | 'description': 'Compute the cosecant of x in radians. Defined as 1/sin(x)', 8 | 'examples': [ 9 | 'csc(2)', 10 | '1 / sin(2)' 11 | ], 12 | 'seealso': [ 13 | 'sec', 14 | 'cot', 15 | 'sin' 16 | ] 17 | }; 18 | -------------------------------------------------------------------------------- /lib/expression/docs/function/trigonometry/sec.js: -------------------------------------------------------------------------------- 1 | module.exports = { 2 | 'name': 'sec', 3 | 'category': 'Trigonometry', 4 | 'syntax': [ 5 | 'sec(x)' 6 | ], 7 | 'description': 'Compute the secant of x in radians. Defined as 1/cos(x)', 8 | 'examples': [ 9 | 'sec(2)', 10 | '1 / cos(2)' 11 | ], 12 | 'seealso': [ 13 | 'cot', 14 | 'csc', 15 | 'cos' 16 | ] 17 | }; 18 | -------------------------------------------------------------------------------- /lib/expression/docs/constants/i.js: -------------------------------------------------------------------------------- 1 | module.exports = { 2 | 'name': 'i', 3 | 'category': 'Constants', 4 | 'syntax': [ 5 | 'i' 6 | ], 7 | 'description': 'Imaginary unit, defined as i*i=-1. A complex number is described as a + b*i, where a is the real part, and b is the imaginary part.', 8 | 'examples': [ 9 | 'i', 10 | 'i * i', 11 | 'sqrt(-1)' 12 | ], 13 | 'seealso': [] 14 | }; 15 | -------------------------------------------------------------------------------- /lib/expression/docs/function/arithmetic/dotPow.js: -------------------------------------------------------------------------------- 1 | module.exports = { 2 | 'name': 'dotpow', 3 | 'category': 'Operators', 4 | 'syntax': [ 5 | 'x .^ y', 6 | 'dotpow(x, y)' 7 | ], 8 | 'description': 9 | 'Calculates the power of x to y element wise.', 10 | 'examples': [ 11 | 'a = [1, 2, 3; 4, 5, 6]', 12 | 'a .^ 2' 13 | ], 14 | 'seealso': [ 15 | 'pow' 16 | ] 17 | }; 18 | -------------------------------------------------------------------------------- /lib/expression/docs/function/probability/pickRandom.js: -------------------------------------------------------------------------------- 1 | module.exports = { 2 | 'name': 'pickRandom', 3 | 'category': 'Probability', 4 | 'syntax': [ 5 | 'pickRandom(array)' 6 | ], 7 | 'description': 8 | 'Pick a random entry from a given array.', 9 | 'examples': [ 10 | 'pickRandom(0:10)', 11 | 'pickRandom([1, 3, 1, 6])' 12 | ], 13 | 'seealso': ['random', 'randomInt'] 14 | }; 15 | -------------------------------------------------------------------------------- /lib/expression/docs/function/trigonometry/acos.js: -------------------------------------------------------------------------------- 1 | module.exports = { 2 | 'name': 'acos', 3 | 'category': 'Trigonometry', 4 | 'syntax': [ 5 | 'acos(x)' 6 | ], 7 | 'description': 'Compute the inverse cosine of a value in radians.', 8 | 'examples': [ 9 | 'acos(0.5)', 10 | 'acos(cos(2.3))' 11 | ], 12 | 'seealso': [ 13 | 'cos', 14 | 'atan', 15 | 'asin' 16 | ] 17 | }; 18 | -------------------------------------------------------------------------------- /lib/expression/docs/function/trigonometry/atan.js: -------------------------------------------------------------------------------- 1 | module.exports = { 2 | 'name': 'atan', 3 | 'category': 'Trigonometry', 4 | 'syntax': [ 5 | 'atan(x)' 6 | ], 7 | 'description': 'Compute the inverse tangent of a value in radians.', 8 | 'examples': [ 9 | 'atan(0.5)', 10 | 'atan(tan(2.3))' 11 | ], 12 | 'seealso': [ 13 | 'tan', 14 | 'acos', 15 | 'asin' 16 | ] 17 | }; 18 | -------------------------------------------------------------------------------- /lib/expression/docs/constants/pi.js: -------------------------------------------------------------------------------- 1 | module.exports = { 2 | 'name': 'pi', 3 | 'category': 'Constants', 4 | 'syntax': [ 5 | 'pi' 6 | ], 7 | 'description': 'The number pi is a mathematical constant that is the ratio of a circle\'s circumference to its diameter, and is approximately equal to 3.14159', 8 | 'examples': [ 9 | 'pi', 10 | 'sin(pi/2)' 11 | ], 12 | 'seealso': ['tau'] 13 | }; 14 | -------------------------------------------------------------------------------- /lib/expression/docs/function/arithmetic/unaryPlus.js: -------------------------------------------------------------------------------- 1 | module.exports = { 2 | 'name': 'unaryPlus', 3 | 'category': 'Operators', 4 | 'syntax': [ 5 | '+x', 6 | 'unaryPlus(x)' 7 | ], 8 | 'description': 9 | 'Converts booleans and strings to numbers.', 10 | 'examples': [ 11 | '+true', 12 | '+"2"' 13 | ], 14 | 'seealso': [ 15 | 'add', 'subtract', 'unaryMinus' 16 | ] 17 | }; 18 | -------------------------------------------------------------------------------- /lib/expression/docs/function/probability/factorial.js: -------------------------------------------------------------------------------- 1 | module.exports = { 2 | 'name': 'factorial', 3 | 'category': 'Probability', 4 | 'syntax': [ 5 | 'n!', 6 | 'factorial(n)' 7 | ], 8 | 'description': 'Compute the factorial of a value', 9 | 'examples': [ 10 | '5!', 11 | '5 * 4 * 3 * 2 * 1', 12 | '3!' 13 | ], 14 | 'seealso': ['combinations', 'permutations', 'gamma'] 15 | }; 16 | -------------------------------------------------------------------------------- /lib/expression/docs/function/trigonometry/tanh.js: -------------------------------------------------------------------------------- 1 | module.exports = { 2 | 'name': 'tanh', 3 | 'category': 'Trigonometry', 4 | 'syntax': [ 5 | 'tanh(x)' 6 | ], 7 | 'description': 'Compute the hyperbolic tangent of x in radians.', 8 | 'examples': [ 9 | 'tanh(0.5)', 10 | 'sinh(0.5) / cosh(0.5)' 11 | ], 12 | 'seealso': [ 13 | 'sinh', 14 | 'cosh' 15 | ] 16 | }; 17 | -------------------------------------------------------------------------------- /test/function/probability/random.test.js: -------------------------------------------------------------------------------- 1 | var assert = require('assert'), 2 | math = require('../../../index'); 3 | 4 | describe('random', function () { 5 | // Note: random is a convenience function generated by distribution 6 | // it is tested in distribution.test.js 7 | 8 | it('should have a function random', function () { 9 | assert.equal(typeof math.random, 'function'); 10 | }) 11 | }); 12 | -------------------------------------------------------------------------------- /lib/expression/docs/function/expression/eval.js: -------------------------------------------------------------------------------- 1 | module.exports = { 2 | 'name': 'eval', 3 | 'category': 'Expression', 4 | 'syntax': [ 5 | 'eval(expression)', 6 | 'eval([expr1, expr2, expr3, ...])' 7 | ], 8 | 'description': 'Evaluate an expression or an array with expressions.', 9 | 'examples': [ 10 | 'eval("2 + 3")', 11 | 'eval("sqrt(" + 4 + ")")' 12 | ], 13 | 'seealso': [] 14 | }; 15 | -------------------------------------------------------------------------------- /lib/expression/docs/function/trigonometry/coth.js: -------------------------------------------------------------------------------- 1 | module.exports = { 2 | 'name': 'coth', 3 | 'category': 'Trigonometry', 4 | 'syntax': [ 5 | 'coth(x)' 6 | ], 7 | 'description': 'Compute the hyperbolic cotangent of x in radians.', 8 | 'examples': [ 9 | 'coth(2)', 10 | '1 / tanh(2)' 11 | ], 12 | 'seealso': [ 13 | 'sech', 14 | 'csch', 15 | 'tanh' 16 | ] 17 | }; 18 | -------------------------------------------------------------------------------- /lib/expression/docs/function/arithmetic/cube.js: -------------------------------------------------------------------------------- 1 | module.exports = { 2 | 'name': 'cube', 3 | 'category': 'Arithmetic', 4 | 'syntax': [ 5 | 'cube(x)' 6 | ], 7 | 'description': 'Compute the cube of a value. The cube of x is x * x * x.', 8 | 'examples': [ 9 | 'cube(2)', 10 | '2^3', 11 | '2 * 2 * 2' 12 | ], 13 | 'seealso': [ 14 | 'multiply', 15 | 'square', 16 | 'pow' 17 | ] 18 | }; 19 | -------------------------------------------------------------------------------- /lib/expression/docs/function/arithmetic/add.js: -------------------------------------------------------------------------------- 1 | module.exports = { 2 | 'name': 'add', 3 | 'category': 'Operators', 4 | 'syntax': [ 5 | 'x + y', 6 | 'add(x, y)' 7 | ], 8 | 'description': 'Add two values.', 9 | 'examples': [ 10 | 'a = 2.1 + 3.6', 11 | 'a - 3.6', 12 | '3 + 2i', 13 | '"hello" + " world"', 14 | '3 cm + 2 inch' 15 | ], 16 | 'seealso': [ 17 | 'subtract' 18 | ] 19 | }; 20 | -------------------------------------------------------------------------------- /lib/expression/docs/function/utils/filter.js: -------------------------------------------------------------------------------- 1 | module.exports = { 2 | 'name': 'filter', 3 | 'category': 'Utils', 4 | 'syntax': [ 5 | 'filter(x, test)' 6 | ], 7 | 'description': 'Filter items in a matrix.', 8 | 'examples': [ 9 | 'isPositive(x) = x > 0', 10 | 'filter([6, -2, -1, 4, 3], isPositive)', 11 | 'filter([6, -2, 0, 1, 0], x != 0)' 12 | ], 13 | 'seealso': ['sort', 'map', 'forEach'] 14 | }; 15 | -------------------------------------------------------------------------------- /lib/expression/docs/function/utils/format.js: -------------------------------------------------------------------------------- 1 | module.exports = { 2 | 'name': 'format', 3 | 'category': 'Utils', 4 | 'syntax': [ 5 | 'format(value)', 6 | 'format(value, precision)' 7 | ], 8 | 'description': 'Format a value of any type as string.', 9 | 'examples': [ 10 | 'format(2.3)', 11 | 'format(3 - 4i)', 12 | 'format([])', 13 | 'format(pi, 3)' 14 | ], 15 | 'seealso': ['print'] 16 | }; 17 | -------------------------------------------------------------------------------- /test/function/probability/randomInt.test.js: -------------------------------------------------------------------------------- 1 | var assert = require('assert'), 2 | math = require('../../../index'); 3 | 4 | describe('randomInt', function () { 5 | // Note: randomInt is a convenience function generated by distribution 6 | // it is tested in distribution.test.js 7 | 8 | it('should have a function randomInt', function () { 9 | assert.equal(typeof math.randomInt, 'function'); 10 | }) 11 | }); 12 | -------------------------------------------------------------------------------- /lib/expression/docs/function/arithmetic/subtract.js: -------------------------------------------------------------------------------- 1 | module.exports = { 2 | 'name': 'subtract', 3 | 'category': 'Operators', 4 | 'syntax': [ 5 | 'x - y', 6 | 'subtract(x, y)' 7 | ], 8 | 'description': 'subtract two values.', 9 | 'examples': [ 10 | 'a = 5.3 - 2', 11 | 'a + 2', 12 | '2/3 - 1/6', 13 | '2 * 3 - 3', 14 | '2.1 km - 500m' 15 | ], 16 | 'seealso': [ 17 | 'add' 18 | ] 19 | }; 20 | -------------------------------------------------------------------------------- /lib/expression/docs/function/complex/re.js: -------------------------------------------------------------------------------- 1 | module.exports = { 2 | 'name': 're', 3 | 'category': 'Complex', 4 | 'syntax': [ 5 | 're(x)' 6 | ], 7 | 'description': 'Get the real part of a complex number.', 8 | 'examples': [ 9 | 're(2 + 3i)', 10 | 'im(2 + 3i)', 11 | 're(-5.2i)', 12 | 're(2.4)' 13 | ], 14 | 'seealso': [ 15 | 'im', 16 | 'conj', 17 | 'abs', 18 | 'arg' 19 | ] 20 | }; 21 | -------------------------------------------------------------------------------- /lib/expression/docs/function/logical/not.js: -------------------------------------------------------------------------------- 1 | module.exports = { 2 | 'name': 'not', 3 | 'category': 'Logical', 4 | 'syntax': [ 5 | '!x', 6 | 'not x', 7 | 'not(x)' 8 | ], 9 | 'description': 'Logical not. Flips the boolean value of given argument.', 10 | 'examples': [ 11 | '!true', 12 | 'not false', 13 | '!2', 14 | '!0' 15 | ], 16 | 'seealso': [ 17 | 'and', 'or', 'xor' 18 | ] 19 | }; 20 | -------------------------------------------------------------------------------- /lib/expression/docs/function/utils/forEach.js: -------------------------------------------------------------------------------- 1 | module.exports = { 2 | 'name': 'forEach', 3 | 'category': 'Utils', 4 | 'syntax': [ 5 | 'forEach(x, callback)' 6 | ], 7 | 'description': 'Iterates over all elements of a matrix/array, and executes the given callback function.', 8 | 'examples': [ 9 | 'forEach([1, 2, 3], function(val) { console.log(val) })' 10 | ], 11 | 'seealso': ['map', 'sort', 'filter'] 12 | }; 13 | -------------------------------------------------------------------------------- /test/function/probability/pickRandom.test.js: -------------------------------------------------------------------------------- 1 | var assert = require('assert'), 2 | math = require('../../../index'); 3 | 4 | describe('pickRandom', function () { 5 | // Note: pickRandom is a convenience function generated by distribution 6 | // it is tested in distribution.test.js 7 | 8 | it('should have a function pickRandom', function () { 9 | assert.equal(typeof math.pickRandom, 'function'); 10 | }) 11 | }); 12 | -------------------------------------------------------------------------------- /lib/expression/docs/function/complex/im.js: -------------------------------------------------------------------------------- 1 | module.exports = { 2 | 'name': 'im', 3 | 'category': 'Complex', 4 | 'syntax': [ 5 | 'im(x)' 6 | ], 7 | 'description': 'Get the imaginary part of a complex number.', 8 | 'examples': [ 9 | 'im(2 + 3i)', 10 | 're(2 + 3i)', 11 | 'im(-5.2i)', 12 | 'im(2.4)' 13 | ], 14 | 'seealso': [ 15 | 're', 16 | 'conj', 17 | 'abs', 18 | 'arg' 19 | ] 20 | }; 21 | -------------------------------------------------------------------------------- /lib/expression/docs/function/trigonometry/csch.js: -------------------------------------------------------------------------------- 1 | module.exports = { 2 | 'name': 'csch', 3 | 'category': 'Trigonometry', 4 | 'syntax': [ 5 | 'csch(x)' 6 | ], 7 | 'description': 'Compute the hyperbolic cosecant of x in radians. Defined as 1/sinh(x)', 8 | 'examples': [ 9 | 'csch(2)', 10 | '1 / sinh(2)' 11 | ], 12 | 'seealso': [ 13 | 'sech', 14 | 'coth', 15 | 'sinh' 16 | ] 17 | }; 18 | -------------------------------------------------------------------------------- /lib/expression/docs/function/trigonometry/sech.js: -------------------------------------------------------------------------------- 1 | module.exports = { 2 | 'name': 'sech', 3 | 'category': 'Trigonometry', 4 | 'syntax': [ 5 | 'sech(x)' 6 | ], 7 | 'description': 'Compute the hyperbolic secant of x in radians. Defined as 1/cosh(x)', 8 | 'examples': [ 9 | 'sech(2)', 10 | '1 / cosh(2)' 11 | ], 12 | 'seealso': [ 13 | 'coth', 14 | 'csch', 15 | 'cosh' 16 | ] 17 | }; 18 | -------------------------------------------------------------------------------- /test/function/construction/parser.test.js: -------------------------------------------------------------------------------- 1 | var assert = require('assert'), 2 | error = require('../../../lib/error/index'), 3 | Parser = require('../../../lib/expression/Parser'), 4 | math = require('../../../index'); 5 | 6 | describe('parser', function() { 7 | 8 | it('should create a parser', function() { 9 | var parser = math.parser(); 10 | 11 | assert(parser instanceof Parser); 12 | }); 13 | 14 | }); 15 | 16 | -------------------------------------------------------------------------------- /lib/expression/docs/function/arithmetic/multiply.js: -------------------------------------------------------------------------------- 1 | module.exports = { 2 | 'name': 'multiply', 3 | 'category': 'Operators', 4 | 'syntax': [ 5 | 'x * y', 6 | 'multiply(x, y)' 7 | ], 8 | 'description': 'multiply two values.', 9 | 'examples': [ 10 | 'a = 2.1 * 3.4', 11 | 'a / 3.4', 12 | '2 * 3 + 4', 13 | '2 * (3 + 4)', 14 | '3 * 2.1 km' 15 | ], 16 | 'seealso': [ 17 | 'divide' 18 | ] 19 | }; 20 | -------------------------------------------------------------------------------- /lib/expression/docs/function/arithmetic/sign.js: -------------------------------------------------------------------------------- 1 | module.exports = { 2 | 'name': 'sign', 3 | 'category': 'Arithmetic', 4 | 'syntax': [ 5 | 'sign(x)' 6 | ], 7 | 'description': 8 | 'Compute the sign of a value. The sign of a value x is 1 when x>1, -1 when x<0, and 0 when x=0.', 9 | 'examples': [ 10 | 'sign(3.5)', 11 | 'sign(-4.2)', 12 | 'sign(0)' 13 | ], 14 | 'seealso': [ 15 | 'abs' 16 | ] 17 | }; 18 | -------------------------------------------------------------------------------- /lib/expression/docs/function/arithmetic/sqrt.js: -------------------------------------------------------------------------------- 1 | module.exports = { 2 | 'name': 'sqrt', 3 | 'category': 'Arithmetic', 4 | 'syntax': [ 5 | 'sqrt(x)' 6 | ], 7 | 'description': 8 | 'Compute the square root value. If x = y * y, then y is the square root of x.', 9 | 'examples': [ 10 | 'sqrt(25)', 11 | '5 * 5', 12 | 'sqrt(-1)' 13 | ], 14 | 'seealso': [ 15 | 'square', 16 | 'multiply' 17 | ] 18 | }; 19 | -------------------------------------------------------------------------------- /lib/expression/docs/function/trigonometry/tan.js: -------------------------------------------------------------------------------- 1 | module.exports = { 2 | 'name': 'tan', 3 | 'category': 'Trigonometry', 4 | 'syntax': [ 5 | 'tan(x)' 6 | ], 7 | 'description': 'Compute the tangent of x in radians.', 8 | 'examples': [ 9 | 'tan(0.5)', 10 | 'sin(0.5) / cos(0.5)', 11 | 'tan(pi / 4)', 12 | 'tan(45 deg)' 13 | ], 14 | 'seealso': [ 15 | 'atan', 16 | 'sin', 17 | 'cos' 18 | ] 19 | }; 20 | -------------------------------------------------------------------------------- /lib/expression/docs/function/utils/map.js: -------------------------------------------------------------------------------- 1 | module.exports = { 2 | 'name': 'map', 3 | 'category': 'Utils', 4 | 'syntax': [ 5 | 'map(x, callback)' 6 | ], 7 | 'description': 'Create a new matrix or array with the results of the callback function executed on each entry of the matrix/array.', 8 | 'examples': [ 9 | 'map([1, 2, 3], function(val) { return value * value })' 10 | ], 11 | 'seealso': ['filter', 'forEach'] 12 | }; 13 | -------------------------------------------------------------------------------- /lib/expression/docs/function/arithmetic/divide.js: -------------------------------------------------------------------------------- 1 | module.exports = { 2 | 'name': 'divide', 3 | 'category': 'Operators', 4 | 'syntax': [ 5 | 'x / y', 6 | 'divide(x, y)' 7 | ], 8 | 'description': 'Divide two values.', 9 | 'examples': [ 10 | 'a = 2 / 3', 11 | 'a * 3', 12 | '4.5 / 2', 13 | '3 + 4 / 2', 14 | '(3 + 4) / 2', 15 | '18 km / 4.5' 16 | ], 17 | 'seealso': [ 18 | 'multiply' 19 | ] 20 | }; 21 | -------------------------------------------------------------------------------- /lib/expression/docs/function/logical/or.js: -------------------------------------------------------------------------------- 1 | module.exports = { 2 | 'name': 'or', 3 | 'category': 'Logical', 4 | 'syntax': [ 5 | 'x or y', 6 | 'or(x, y)' 7 | ], 8 | 'description': 'Logical or. Test if at least one value is defined with a nonzero/nonempty value.', 9 | 'examples': [ 10 | 'true or false', 11 | 'false or false', 12 | '0 or 4' 13 | ], 14 | 'seealso': [ 15 | 'not', 'and', 'xor' 16 | ] 17 | }; 18 | -------------------------------------------------------------------------------- /lib/expression/docs/function/logical/and.js: -------------------------------------------------------------------------------- 1 | module.exports = { 2 | 'name': 'and', 3 | 'category': 'Logical', 4 | 'syntax': [ 5 | 'x and y', 6 | 'and(x, y)' 7 | ], 8 | 'description': 'Logical and. Test whether two values are both defined with a nonzero/nonempty value.', 9 | 'examples': [ 10 | 'true and false', 11 | 'true and true', 12 | '2 and 4' 13 | ], 14 | 'seealso': [ 15 | 'not', 'or', 'xor' 16 | ] 17 | }; 18 | -------------------------------------------------------------------------------- /lib/expression/docs/function/probability/permutations.js: -------------------------------------------------------------------------------- 1 | module.exports = { 2 | 'name': 'permutations', 3 | 'category': 'Probability', 4 | 'syntax': [ 5 | 'permutations(n)', 6 | 'permutations(n, k)' 7 | ], 8 | 'description': 'Compute the number of permutations of n items taken k at a time', 9 | 'examples': [ 10 | 'permutations(5)', 11 | 'permutations(5, 3)' 12 | ], 13 | 'seealso': ['combinations', 'factorial'] 14 | }; 15 | -------------------------------------------------------------------------------- /test/error/index.test.js: -------------------------------------------------------------------------------- 1 | var assert = require('assert'), 2 | error = require('../../lib/error/index'); 3 | 4 | describe('index.js', function () { 5 | 6 | it('should contain error namespace', function () { 7 | assert.equal(typeof error, 'object'); 8 | assert('ArgumentsError' in error); 9 | assert('DimensionError' in error); 10 | assert('IndexError' in error); 11 | assert('UnsupportedTypeError' in error); 12 | }); 13 | 14 | }); -------------------------------------------------------------------------------- /lib/expression/docs/function/arithmetic/fix.js: -------------------------------------------------------------------------------- 1 | module.exports = { 2 | 'name': 'fix', 3 | 'category': 'Arithmetic', 4 | 'syntax': [ 5 | 'fix(x)' 6 | ], 7 | 'description': 8 | 'Round a value towards zero. If x is complex, both real and imaginary part are rounded towards zero.', 9 | 'examples': [ 10 | 'fix(3.2)', 11 | 'fix(3.8)', 12 | 'fix(-4.2)', 13 | 'fix(-4.8)' 14 | ], 15 | 'seealso': ['ceil', 'floor', 'round'] 16 | }; 17 | -------------------------------------------------------------------------------- /lib/expression/docs/function/arithmetic/log10.js: -------------------------------------------------------------------------------- 1 | module.exports = { 2 | 'name': 'log10', 3 | 'category': 'Arithmetic', 4 | 'syntax': [ 5 | 'log10(x)' 6 | ], 7 | 'description': 'Compute the 10-base logarithm of a value.', 8 | 'examples': [ 9 | 'log10(0.00001)', 10 | 'log10(10000)', 11 | '10 ^ 4', 12 | 'log(10000) / log(10)', 13 | 'log(10000, 10)' 14 | ], 15 | 'seealso': [ 16 | 'exp', 17 | 'log' 18 | ] 19 | }; 20 | -------------------------------------------------------------------------------- /lib/expression/docs/function/arithmetic/unaryMinus.js: -------------------------------------------------------------------------------- 1 | module.exports = { 2 | 'name': 'unaryMinus', 3 | 'category': 'Operators', 4 | 'syntax': [ 5 | '-x', 6 | 'unaryMinus(x)' 7 | ], 8 | 'description': 9 | 'Inverse the sign of a value. Converts booleans and strings to numbers.', 10 | 'examples': [ 11 | '-4.5', 12 | '-(-5.6)', 13 | '-"22"' 14 | ], 15 | 'seealso': [ 16 | 'add', 'subtract', 'unaryPlus' 17 | ] 18 | }; 19 | -------------------------------------------------------------------------------- /lib/expression/docs/function/matrix/inv.js: -------------------------------------------------------------------------------- 1 | module.exports = { 2 | 'name': 'inv', 3 | 'category': 'Matrix', 4 | 'syntax': [ 5 | 'inv(x)' 6 | ], 7 | 'description': 'Calculate the inverse of a matrix', 8 | 'examples': [ 9 | 'inv([1, 2; 3, 4])', 10 | 'inv(4)', 11 | '1 / 4' 12 | ], 13 | 'seealso': [ 14 | 'concat', 'det', 'diag', 'eye', 'ones', 'range', 'size', 'squeeze', 'subset', 'trace', 'transpose', 'zeros' 15 | ] 16 | }; 17 | -------------------------------------------------------------------------------- /lib/expression/docs/function/utils/clone.js: -------------------------------------------------------------------------------- 1 | module.exports = { 2 | 'name': 'clone', 3 | 'category': 'Utils', 4 | 'syntax': [ 5 | 'clone(x)' 6 | ], 7 | 'description': 'Clone a variable. Creates a copy of primitive variables,and a deep copy of matrices', 8 | 'examples': [ 9 | 'clone(3.5)', 10 | 'clone(2 - 4i)', 11 | 'clone(45 deg)', 12 | 'clone([1, 2; 3, 4])', 13 | 'clone("hello world")' 14 | ], 15 | 'seealso': [] 16 | }; 17 | -------------------------------------------------------------------------------- /lib/expression/docs/function/arithmetic/ceil.js: -------------------------------------------------------------------------------- 1 | module.exports = { 2 | 'name': 'ceil', 3 | 'category': 'Arithmetic', 4 | 'syntax': [ 5 | 'ceil(x)' 6 | ], 7 | 'description': 8 | 'Round a value towards plus infinity. If x is complex, both real and imaginary part are rounded towards plus infinity.', 9 | 'examples': [ 10 | 'ceil(3.2)', 11 | 'ceil(3.8)', 12 | 'ceil(-4.2)' 13 | ], 14 | 'seealso': ['floor', 'fix', 'round'] 15 | }; 16 | -------------------------------------------------------------------------------- /lib/expression/docs/function/arithmetic/exp.js: -------------------------------------------------------------------------------- 1 | module.exports = { 2 | 'name': 'exp', 3 | 'category': 'Arithmetic', 4 | 'syntax': [ 5 | 'exp(x)' 6 | ], 7 | 'description': 'Calculate the exponent of a value.', 8 | 'examples': [ 9 | 'exp(1.3)', 10 | 'e ^ 1.3', 11 | 'log(exp(1.3))', 12 | 'x = 2.4', 13 | '(exp(i*x) == cos(x) + i*sin(x)) # Euler\'s formula' 14 | ], 15 | 'seealso': [ 16 | 'pow', 17 | 'log' 18 | ] 19 | }; 20 | -------------------------------------------------------------------------------- /lib/expression/docs/function/bitwise/leftShift.js: -------------------------------------------------------------------------------- 1 | module.exports = { 2 | 'name': 'leftShift', 3 | 'category': 'Bitwise', 4 | 'syntax': [ 5 | 'x << y', 6 | 'leftShift(x, y)' 7 | ], 8 | 'description': 'Bitwise left logical shift of a value x by y number of bits.', 9 | 'examples': [ 10 | '4 << 1', 11 | '8 >> 1' 12 | ], 13 | 'seealso': [ 14 | 'bitAnd', 'bitNot', 'bitOr', 'bitXor', 'rightArithShift', 'rightLogShift' 15 | ] 16 | }; 17 | -------------------------------------------------------------------------------- /lib/expression/docs/function/trigonometry/sin.js: -------------------------------------------------------------------------------- 1 | module.exports = { 2 | 'name': 'sin', 3 | 'category': 'Trigonometry', 4 | 'syntax': [ 5 | 'sin(x)' 6 | ], 7 | 'description': 'Compute the sine of x in radians.', 8 | 'examples': [ 9 | 'sin(2)', 10 | 'sin(pi / 4) ^ 2', 11 | 'sin(90 deg)', 12 | 'sin(30 deg)', 13 | 'sin(0.2)^2 + cos(0.2)^2' 14 | ], 15 | 'seealso': [ 16 | 'asin', 17 | 'cos', 18 | 'tan' 19 | ] 20 | }; 21 | -------------------------------------------------------------------------------- /lib/expression/docs/constants/phi.js: -------------------------------------------------------------------------------- 1 | module.exports = { 2 | 'name': 'phi', 3 | 'category': 'Constants', 4 | 'syntax': [ 5 | 'phi' 6 | ], 7 | 'description': 'Phi is the golden ratio. Two quantities are in the golden ratio if their ratio is the same as the ratio of their sum to the larger of the two quantities. Phi is defined as `(1 + sqrt(5)) / 2` and is approximately 1.618034...', 8 | 'examples': [ 9 | 'tau' 10 | ], 11 | 'seealso': [] 12 | }; 13 | -------------------------------------------------------------------------------- /lib/expression/docs/function/arithmetic/dotDivide.js: -------------------------------------------------------------------------------- 1 | module.exports = { 2 | 'name': 'dotDivide', 3 | 'category': 'Operators', 4 | 'syntax': [ 5 | 'x ./ y', 6 | 'dotDivide(x, y)' 7 | ], 8 | 'description': 'Divide two values element wise.', 9 | 'examples': [ 10 | 'a = [1, 2, 3; 4, 5, 6]', 11 | 'b = [2, 1, 1; 3, 2, 5]', 12 | 'a ./ b' 13 | ], 14 | 'seealso': [ 15 | 'multiply', 16 | 'dotMultiply', 17 | 'divide' 18 | ] 19 | }; 20 | -------------------------------------------------------------------------------- /lib/expression/docs/function/arithmetic/floor.js: -------------------------------------------------------------------------------- 1 | module.exports = { 2 | 'name': 'floor', 3 | 'category': 'Arithmetic', 4 | 'syntax': [ 5 | 'floor(x)' 6 | ], 7 | 'description': 8 | 'Round a value towards minus infinity.If x is complex, both real and imaginary part are rounded towards minus infinity.', 9 | 'examples': [ 10 | 'floor(3.2)', 11 | 'floor(3.8)', 12 | 'floor(-4.2)' 13 | ], 14 | 'seealso': ['ceil', 'fix', 'round'] 15 | }; 16 | -------------------------------------------------------------------------------- /lib/expression/docs/function/arithmetic/square.js: -------------------------------------------------------------------------------- 1 | module.exports = { 2 | 'name': 'square', 3 | 'category': 'Arithmetic', 4 | 'syntax': [ 5 | 'square(x)' 6 | ], 7 | 'description': 8 | 'Compute the square of a value. The square of x is x * x.', 9 | 'examples': [ 10 | 'square(3)', 11 | 'sqrt(9)', 12 | '3^2', 13 | '3 * 3' 14 | ], 15 | 'seealso': [ 16 | 'multiply', 17 | 'pow', 18 | 'sqrt', 19 | 'cube' 20 | ] 21 | }; 22 | -------------------------------------------------------------------------------- /lib/expression/docs/function/matrix/flatten.js: -------------------------------------------------------------------------------- 1 | module.exports = { 2 | 'name': 'flatten', 3 | 'category': 'Matrix', 4 | 'syntax': [ 5 | 'flatten(x)' 6 | ], 7 | 'description': 'Flatten a multi dimensional matrix into a single dimensional matrix.', 8 | 'examples': [ 9 | 'a = [1, 2, 3; 4, 5, 6]', 10 | 'size(a)', 11 | 'b = flatten(a)', 12 | 'size(b)' 13 | ], 14 | 'seealso': [ 15 | 'concat', 'resize', 'size', 'squeeze' 16 | ] 17 | }; 18 | -------------------------------------------------------------------------------- /lib/expression/docs/function/trigonometry/cos.js: -------------------------------------------------------------------------------- 1 | module.exports = { 2 | 'name': 'cos', 3 | 'category': 'Trigonometry', 4 | 'syntax': [ 5 | 'cos(x)' 6 | ], 7 | 'description': 'Compute the cosine of x in radians.', 8 | 'examples': [ 9 | 'cos(2)', 10 | 'cos(pi / 4) ^ 2', 11 | 'cos(180 deg)', 12 | 'cos(60 deg)', 13 | 'sin(0.2)^2 + cos(0.2)^2' 14 | ], 15 | 'seealso': [ 16 | 'acos', 17 | 'sin', 18 | 'tan' 19 | ] 20 | }; 21 | -------------------------------------------------------------------------------- /lib/expression/docs/function/matrix/det.js: -------------------------------------------------------------------------------- 1 | module.exports = { 2 | 'name': 'det', 3 | 'category': 'Matrix', 4 | 'syntax': [ 5 | 'det(x)' 6 | ], 7 | 'description': 'Calculate the determinant of a matrix', 8 | 'examples': [ 9 | 'det([1, 2; 3, 4])', 10 | 'det([-2, 2, 3; -1, 1, 3; 2, 0, -1])' 11 | ], 12 | 'seealso': [ 13 | 'concat', 'diag', 'eye', 'inv', 'ones', 'range', 'size', 'squeeze', 'subset', 'trace', 'transpose', 'zeros' 14 | ] 15 | }; 16 | -------------------------------------------------------------------------------- /lib/expression/docs/function/arithmetic/dotMultiply.js: -------------------------------------------------------------------------------- 1 | module.exports = { 2 | 'name': 'dotMultiply', 3 | 'category': 'Operators', 4 | 'syntax': [ 5 | 'x .* y', 6 | 'dotMultiply(x, y)' 7 | ], 8 | 'description': 'Multiply two values element wise.', 9 | 'examples': [ 10 | 'a = [1, 2, 3; 4, 5, 6]', 11 | 'b = [2, 1, 1; 3, 2, 5]', 12 | 'a .* b' 13 | ], 14 | 'seealso': [ 15 | 'multiply', 16 | 'divide', 17 | 'dotDivide' 18 | ] 19 | }; 20 | -------------------------------------------------------------------------------- /lib/expression/docs/function/matrix/cross.js: -------------------------------------------------------------------------------- 1 | module.exports = { 2 | 'name': 'cross', 3 | 'category': 'Matrix', 4 | 'syntax': [ 5 | 'cross(A, B)' 6 | ], 7 | 'description': 'Calculate the cross product for two vectors in three dimensional space.', 8 | 'examples': [ 9 | 'cross([1, 1, 0], [0, 1, 1])', 10 | 'cross([3, -3, 1], [4, 9, 2])', 11 | 'cross([2, 3, 4], [5, 6, 7])' 12 | ], 13 | 'seealso': [ 14 | 'multiply', 15 | 'dot' 16 | ] 17 | }; 18 | -------------------------------------------------------------------------------- /lib/expression/docs/function/matrix/transpose.js: -------------------------------------------------------------------------------- 1 | module.exports = { 2 | 'name': 'transpose', 3 | 'category': 'Matrix', 4 | 'syntax': [ 5 | 'x\'', 6 | 'transpose(x)' 7 | ], 8 | 'description': 'Transpose a matrix', 9 | 'examples': [ 10 | 'a = [1, 2, 3; 4, 5, 6]', 11 | 'a\'', 12 | 'transpose(a)' 13 | ], 14 | 'seealso': [ 15 | 'concat', 'det', 'diag', 'eye', 'inv', 'ones', 'range', 'size', 'squeeze', 'subset', 'trace', 'zeros' 16 | ] 17 | }; 18 | -------------------------------------------------------------------------------- /lib/expression/docs/function/probability/gamma.js: -------------------------------------------------------------------------------- 1 | module.exports = { 2 | 'name': 'gamma', 3 | 'category': 'Probability', 4 | 'syntax': [ 5 | 'gamma(n)' 6 | ], 7 | 'description': 'Compute the gamma function. For small values, the Lanczos approximation is used, and for large values the extended Stirling approximation.', 8 | 'examples': [ 9 | 'gamma(4)', 10 | '3!', 11 | 'gamma(1/2)', 12 | 'sqrt(pi)' 13 | ], 14 | 'seealso': ['factorial'] 15 | }; 16 | -------------------------------------------------------------------------------- /lib/expression/docs/function/complex/arg.js: -------------------------------------------------------------------------------- 1 | module.exports = { 2 | 'name': 'arg', 3 | 'category': 'Complex', 4 | 'syntax': [ 5 | 'arg(x)' 6 | ], 7 | 'description': 8 | 'Compute the argument of a complex value. If x = a+bi, the argument is computed as atan2(b, a).', 9 | 'examples': [ 10 | 'arg(2 + 2i)', 11 | 'atan2(3, 2)', 12 | 'arg(2 + 3i)' 13 | ], 14 | 'seealso': [ 15 | 're', 16 | 'im', 17 | 'conj', 18 | 'abs' 19 | ] 20 | }; 21 | -------------------------------------------------------------------------------- /lib/expression/docs/function/complex/conj.js: -------------------------------------------------------------------------------- 1 | module.exports = { 2 | 'name': 'conj', 3 | 'category': 'Complex', 4 | 'syntax': [ 5 | 'conj(x)' 6 | ], 7 | 'description': 8 | 'Compute the complex conjugate of a complex value. If x = a+bi, the complex conjugate is a-bi.', 9 | 'examples': [ 10 | 'conj(2 + 3i)', 11 | 'conj(2 - 3i)', 12 | 'conj(-5.2i)' 13 | ], 14 | 'seealso': [ 15 | 're', 16 | 'im', 17 | 'abs', 18 | 'arg' 19 | ] 20 | }; 21 | -------------------------------------------------------------------------------- /lib/expression/docs/function/construction/string.js: -------------------------------------------------------------------------------- 1 | module.exports = { 2 | 'name': 'string', 3 | 'category': 'Type', 4 | 'syntax': [ 5 | '"text"', 6 | 'string(x)' 7 | ], 8 | 'description': 9 | 'Create a string or convert a value to a string', 10 | 'examples': [ 11 | '"Hello World!"', 12 | 'string(4.2)', 13 | 'string(3 + 2i)' 14 | ], 15 | 'seealso': [ 16 | 'bignumber', 'boolean', 'complex', 'index', 'matrix', 'number', 'unit' 17 | ] 18 | }; 19 | -------------------------------------------------------------------------------- /lib/expression/docs/function/bitwise/rightArithShift.js: -------------------------------------------------------------------------------- 1 | module.exports = { 2 | 'name': 'rightArithShift', 3 | 'category': 'Bitwise', 4 | 'syntax': [ 5 | 'x >> y', 6 | 'leftShift(x, y)' 7 | ], 8 | 'description': 'Bitwise right arithmetic shift of a value x by y number of bits.', 9 | 'examples': [ 10 | '8 >> 1', 11 | '4 << 1', 12 | '-12 >> 2' 13 | ], 14 | 'seealso': [ 15 | 'bitAnd', 'bitNot', 'bitOr', 'bitXor', 'leftShift', 'rightLogShift' 16 | ] 17 | }; 18 | -------------------------------------------------------------------------------- /lib/expression/docs/function/bitwise/rightLogShift.js: -------------------------------------------------------------------------------- 1 | module.exports = { 2 | 'name': 'rightLogShift', 3 | 'category': 'Bitwise', 4 | 'syntax': [ 5 | 'x >> y', 6 | 'leftShift(x, y)' 7 | ], 8 | 'description': 'Bitwise right logical shift of a value x by y number of bits.', 9 | 'examples': [ 10 | '8 >>> 1', 11 | '4 << 1', 12 | '-12 >>> 2' 13 | ], 14 | 'seealso': [ 15 | 'bitAnd', 'bitNot', 'bitOr', 'bitXor', 'leftShift', 'rightArithShift' 16 | ] 17 | }; 18 | -------------------------------------------------------------------------------- /lib/expression/docs/function/logical/xor.js: -------------------------------------------------------------------------------- 1 | module.exports = { 2 | 'name': 'xor', 3 | 'category': 'Logical', 4 | 'syntax': [ 5 | 'x or y', 6 | 'or(x, y)' 7 | ], 8 | 'description': 'Logical exclusive or, xor. Test whether one and only one value is defined with a nonzero/nonempty value.', 9 | 'examples': [ 10 | 'true xor false', 11 | 'false xor false', 12 | 'true xor true', 13 | '0 or 4' 14 | ], 15 | 'seealso': [ 16 | 'not', 'and', 'or' 17 | ] 18 | }; 19 | -------------------------------------------------------------------------------- /lib/expression/docs/function/arithmetic/mod.js: -------------------------------------------------------------------------------- 1 | module.exports = { 2 | 'name': 'mod', 3 | 'category': 'Operators', 4 | 'syntax': [ 5 | 'x % y', 6 | 'x mod y', 7 | 'mod(x, y)' 8 | ], 9 | 'description': 10 | 'Calculates the modulus, the remainder of an integer division.', 11 | 'examples': [ 12 | '7 % 3', 13 | '11 % 2', 14 | '10 mod 4', 15 | 'function isOdd(x) = x % 2', 16 | 'isOdd(2)', 17 | 'isOdd(3)' 18 | ], 19 | 'seealso': ['divide'] 20 | }; 21 | -------------------------------------------------------------------------------- /lib/expression/docs/function/construction/complex.js: -------------------------------------------------------------------------------- 1 | module.exports = { 2 | 'name': 'complex', 3 | 'category': 'Type', 4 | 'syntax': [ 5 | 'complex()', 6 | 'complex(re, im)', 7 | 'complex(string)' 8 | ], 9 | 'description': 10 | 'Create a complex number.', 11 | 'examples': [ 12 | 'complex()', 13 | 'complex(2, 3)', 14 | 'complex("7 - 2i")' 15 | ], 16 | 'seealso': [ 17 | 'bignumber', 'boolean', 'index', 'matrix', 'number', 'string', 'unit' 18 | ] 19 | }; 20 | -------------------------------------------------------------------------------- /lib/expression/docs/function/construction/unit.js: -------------------------------------------------------------------------------- 1 | module.exports = { 2 | 'name': 'unit', 3 | 'category': 'Type', 4 | 'syntax': [ 5 | 'value unit', 6 | 'unit(value, unit)', 7 | 'unit(string)' 8 | ], 9 | 'description': 10 | 'Create a unit.', 11 | 'examples': [ 12 | '5.5 mm', 13 | '3 inch', 14 | 'unit(7.1, "kilogram")', 15 | 'unit("23 deg")' 16 | ], 17 | 'seealso': [ 18 | 'bignumber', 'boolean', 'complex', 'index', 'matrix', 'number', 'string' 19 | ] 20 | }; 21 | -------------------------------------------------------------------------------- /lib/expression/transform/error.transform.js: -------------------------------------------------------------------------------- 1 | var DimensionError = require('../../error/DimensionError'); 2 | var IndexError = require('../../error/IndexError'); 3 | 4 | /** 5 | * Transform zero-based indices to one-based indices in errors 6 | * @param {Error} err 7 | * @returns {Error} Returns the transformed error 8 | */ 9 | exports.transform = function (err) { 10 | if (err instanceof IndexError) { 11 | return new IndexError(err.index + 1, err.min + 1, err.max + 1); 12 | } 13 | 14 | return err; 15 | }; 16 | -------------------------------------------------------------------------------- /lib/expression/docs/function/matrix/resize.js: -------------------------------------------------------------------------------- 1 | module.exports = { 2 | 'name': 'resize', 3 | 'category': 'Matrix', 4 | 'syntax': [ 5 | 'resize(x, size)', 6 | 'resize(x, size, defaultValue)' 7 | ], 8 | 'description': 'Resize a matrix.', 9 | 'examples': [ 10 | 'resize([1,2,3,4,5], [3])', 11 | 'resize([1,2,3], [5])', 12 | 'resize([1,2,3], [5], -1)', 13 | 'resize(2, [2, 3])', 14 | 'resize("hello", [8], "!")' 15 | ], 16 | 'seealso': [ 17 | 'size', 'subset', 'squeeze' 18 | ] 19 | }; 20 | -------------------------------------------------------------------------------- /lib/expression/docs/function/matrix/size.js: -------------------------------------------------------------------------------- 1 | module.exports = { 2 | 'name': 'size', 3 | 'category': 'Matrix', 4 | 'syntax': [ 5 | 'size(x)' 6 | ], 7 | 'description': 'Calculate the size of a matrix.', 8 | 'examples': [ 9 | 'size(2.3)', 10 | 'size("hello world")', 11 | 'a = [1, 2; 3, 4; 5, 6]', 12 | 'size(a)', 13 | 'size(1:6)' 14 | ], 15 | 'seealso': [ 16 | 'concat', 'det', 'diag', 'eye', 'inv', 'ones', 'range', 'squeeze', 'subset', 'trace', 'transpose', 'zeros' 17 | ] 18 | }; 19 | -------------------------------------------------------------------------------- /lib/expression/docs/function/matrix/trace.js: -------------------------------------------------------------------------------- 1 | module.exports = { 2 | 'name': 'trace', 3 | 'category': 'Matrix', 4 | 'syntax': [ 5 | 'trace(A)' 6 | ], 7 | 'description': 'Calculate the trace of a matrix: the sum of the elements on the main diagonal of a square matrix.', 8 | 'examples': [ 9 | 'A = [1, 2, 3; -1, 2, 3; 2, 0, 3]', 10 | 'trace(A)' 11 | ], 12 | 'seealso': [ 13 | 'concat', 'det', 'diag', 'eye', 'inv', 'ones', 'range', 'size', 'squeeze', 'subset', 'transpose', 'zeros' 14 | ] 15 | }; 16 | -------------------------------------------------------------------------------- /lib/expression/docs/function/utils/print.js: -------------------------------------------------------------------------------- 1 | module.exports = { 2 | 'name': 'print', 3 | 'category': 'Utils', 4 | 'syntax': [ 5 | 'print(template, values)', 6 | 'print(template, values, precision)' 7 | ], 8 | 'description': 'Interpolate values into a string template.', 9 | 'examples': [ 10 | 'print("Lucy is $age years old", {age: 5})', 11 | 'print("The value of pi is $pi", {pi: pi}, 3)', 12 | 'print("Hello, $user.name!", {user: {name: "John"}})' 13 | ], 14 | 'seealso': ['format'] 15 | }; 16 | -------------------------------------------------------------------------------- /lib/expression/docs/function/probability/random.js: -------------------------------------------------------------------------------- 1 | module.exports = { 2 | 'name': 'random', 3 | 'category': 'Probability', 4 | 'syntax': [ 5 | 'random()', 6 | 'random(max)', 7 | 'random(min, max)', 8 | 'random(size)', 9 | 'random(size, max)', 10 | 'random(size, min, max)' 11 | ], 12 | 'description': 13 | 'Return a random number.', 14 | 'examples': [ 15 | 'random()', 16 | 'random(10, 20)', 17 | 'random([2, 3])' 18 | ], 19 | 'seealso': ['pickRandom', 'randomInt'] 20 | }; 21 | -------------------------------------------------------------------------------- /lib/expression/docs/function/statistics/sum.js: -------------------------------------------------------------------------------- 1 | module.exports = { 2 | 'name': 'sum', 3 | 'category': 'Statistics', 4 | 'syntax': [ 5 | 'sum(a, b, c, ...)', 6 | 'sum(A)' 7 | ], 8 | 'description': 'Compute the sum of all values.', 9 | 'examples': [ 10 | 'sum(2, 3, 4, 1)', 11 | 'sum([2, 3, 4, 1])', 12 | 'sum([2, 5; 4, 3])' 13 | ], 14 | 'seealso': [ 15 | 'max', 16 | 'mean', 17 | 'median', 18 | 'min', 19 | 'prod', 20 | 'std', 21 | 'sum', 22 | 'var' 23 | ] 24 | }; 25 | -------------------------------------------------------------------------------- /lib/expression/docs/function/trigonometry/atan2.js: -------------------------------------------------------------------------------- 1 | module.exports = { 2 | 'name': 'atan2', 3 | 'category': 'Trigonometry', 4 | 'syntax': [ 5 | 'atan2(y, x)' 6 | ], 7 | 'description': 8 | 'Computes the principal value of the arc tangent of y/x in radians.', 9 | 'examples': [ 10 | 'atan2(2, 2) / pi', 11 | 'angle = 60 deg in rad', 12 | 'x = cos(angle)', 13 | 'y = sin(angle)', 14 | 'atan2(y, x)' 15 | ], 16 | 'seealso': [ 17 | 'sin', 18 | 'cos', 19 | 'tan' 20 | ] 21 | }; 22 | -------------------------------------------------------------------------------- /lib/expression/docs/function/statistics/prod.js: -------------------------------------------------------------------------------- 1 | module.exports = { 2 | 'name': 'prod', 3 | 'category': 'Statistics', 4 | 'syntax': [ 5 | 'prod(a, b, c, ...)', 6 | 'prod(A)' 7 | ], 8 | 'description': 'Compute the product of all values.', 9 | 'examples': [ 10 | 'prod(2, 3, 4)', 11 | 'prod([2, 3, 4])', 12 | 'prod([2, 5; 4, 3])' 13 | ], 14 | 'seealso': [ 15 | 'max', 16 | 'mean', 17 | 'min', 18 | 'median', 19 | 'min', 20 | 'std', 21 | 'sum', 22 | 'var' 23 | ] 24 | }; 25 | -------------------------------------------------------------------------------- /examples/browser/requirejs_loading.html: -------------------------------------------------------------------------------- 1 | 2 | 3 | 4 | math.js | require.js loading 5 | 6 | 7 | 8 | 9 | 17 | 18 | 19 | -------------------------------------------------------------------------------- /lib/expression/docs/function/matrix/squeeze.js: -------------------------------------------------------------------------------- 1 | module.exports = { 2 | 'name': 'squeeze', 3 | 'category': 'Matrix', 4 | 'syntax': [ 5 | 'squeeze(x)' 6 | ], 7 | 'description': 'Remove inner and outer singleton dimensions from a matrix.', 8 | 'examples': [ 9 | 'a = zeros(3,2,1)', 10 | 'size(squeeze(a))', 11 | 'b = zeros(1,1,3)', 12 | 'size(squeeze(b))' 13 | ], 14 | 'seealso': [ 15 | 'concat', 'det', 'diag', 'eye', 'inv', 'ones', 'range', 'size', 'subset', 'trace', 'transpose', 'zeros' 16 | ] 17 | }; 18 | -------------------------------------------------------------------------------- /lib/expression/docs/function/probability/randomInt.js: -------------------------------------------------------------------------------- 1 | module.exports = { 2 | 'name': 'randInt', 3 | 'category': 'Probability', 4 | 'syntax': [ 5 | 'randInt()', 6 | 'randInt(max)', 7 | 'randInt(min, max)', 8 | 'randInt(size)', 9 | 'randInt(size, max)', 10 | 'randInt(size, min, max)' 11 | ], 12 | 'description': 13 | 'Return a random integer number', 14 | 'examples': [ 15 | 'randInt()', 16 | 'randInt(10, 20)', 17 | 'randInt([2, 3], 10)' 18 | ], 19 | 'seealso': ['pickRandom', 'random'] 20 | }; -------------------------------------------------------------------------------- /misc/registrations.md: -------------------------------------------------------------------------------- 1 | math.js is registered at the following places: 2 | 3 | # package managers 4 | - npm.js: mathjs (npm publish) 5 | - bower: mathjs (automatically updates via github) 6 | - jam.js: mathjs (jam publish) 7 | 8 | # websites 9 | - http://jster.net (automatically updates via github) 10 | - http://www.jsdb.io 11 | - http://jswiki.org 12 | - http://jspkg.com (manually import a project from github) 13 | - http://cdnjs.com (add a project via a git pull-request) 14 | 15 | # other 16 | - travis-ci (automatically updates via github) 17 | -------------------------------------------------------------------------------- /lib/expression/docs/function/bitwise/bitNot.js: -------------------------------------------------------------------------------- 1 | module.exports = { 2 | 'name': 'bitNot', 3 | 'category': 'Bitwise', 4 | 'syntax': [ 5 | '~x', 6 | 'bitNot(x)' 7 | ], 8 | 'description': 'Bitwise NOT operation. Performs a logical negation on each bit of the given value. Bits that are 0 become 1, and those that are 1 become 0.', 9 | 'examples': [ 10 | '~1', 11 | '~2', 12 | 'bitNot([2, -3, 4])' 13 | ], 14 | 'seealso': [ 15 | 'bitAnd', 'bitOr', 'bitXor', 'leftShift', 'rightArithShift', 'rightLogShift' 16 | ] 17 | }; 18 | -------------------------------------------------------------------------------- /lib/expression/docs/function/construction/bignumber.js: -------------------------------------------------------------------------------- 1 | module.exports = { 2 | 'name': 'bignumber', 3 | 'category': 'Type', 4 | 'syntax': [ 5 | 'bignumber(x)' 6 | ], 7 | 'description': 8 | 'Create a big number from a number or string.', 9 | 'examples': [ 10 | '0.1 + 0.2', 11 | 'bignumber(0.1) + bignumber(0.2)', 12 | 'bignumber("7.2")', 13 | 'bignumber("7.2e500")', 14 | 'bignumber([0.1, 0.2, 0.3])' 15 | ], 16 | 'seealso': [ 17 | 'boolean', 'complex', 'index', 'matrix', 'string', 'unit' 18 | ] 19 | }; 20 | -------------------------------------------------------------------------------- /lib/expression/docs/function/construction/matrix.js: -------------------------------------------------------------------------------- 1 | module.exports = { 2 | 'name': 'matrix', 3 | 'category': 'Type', 4 | 'syntax': [ 5 | '[]', 6 | '[a1, b1, ...; a2, b2, ...]', 7 | 'matrix()', 8 | 'matrix([...])' 9 | ], 10 | 'description': 11 | 'Create a matrix.', 12 | 'examples': [ 13 | '[]', 14 | '[1, 2, 3]', 15 | '[1, 2, 3; 4, 5, 6]', 16 | 'matrix()', 17 | 'matrix([3, 4])' 18 | ], 19 | 'seealso': [ 20 | 'bignumber', 'boolean', 'complex', 'index', 'number', 'string', 'unit' 21 | ] 22 | }; 23 | -------------------------------------------------------------------------------- /lib/expression/docs/function/construction/boolean.js: -------------------------------------------------------------------------------- 1 | module.exports = { 2 | 'name': 'boolean', 3 | 'category': 'Type', 4 | 'syntax': [ 5 | 'x', 6 | 'boolean(x)' 7 | ], 8 | 'description': 9 | 'Convert a string or number into a boolean.', 10 | 'examples': [ 11 | 'boolean(0)', 12 | 'boolean(1)', 13 | 'boolean(3)', 14 | 'boolean("true")', 15 | 'boolean("false")', 16 | 'boolean([1, 0, 1, 1])' 17 | ], 18 | 'seealso': [ 19 | 'bignumber', 'complex', 'index', 'matrix', 'number', 'string', 'unit' 20 | ] 21 | }; 22 | -------------------------------------------------------------------------------- /lib/expression/docs/function/relational/deepEqual.js: -------------------------------------------------------------------------------- 1 | module.exports = { 2 | 'name': 'deepEqual', 3 | 'category': 'Relational', 4 | 'syntax': [ 5 | 'deepEqual(x, y)' 6 | ], 7 | 'description': 8 | 'Check equality of two matrices element wise. Returns true if the size of both matrices is equal and when and each of the elements are equal.', 9 | 'examples': [ 10 | '[1,3,4] == [1,3,4]', 11 | '[1,3,4] == [1,3]' 12 | ], 13 | 'seealso': [ 14 | 'equal', 'unequal', 'smaller', 'larger', 'smallerEq', 'largerEq', 'compare' 15 | ] 16 | }; 17 | -------------------------------------------------------------------------------- /lib/expression/docs/function/utils/sort.js: -------------------------------------------------------------------------------- 1 | module.exports = { 2 | 'name': 'sort', 3 | 'category': 'Utils', 4 | 'syntax': [ 5 | 'sort(x)', 6 | 'sort(x, compare)' 7 | ], 8 | 'description': 'Sort the items in a matrix. Compare can be a string "asc" or "desc", or a custom sort function.', 9 | 'examples': [ 10 | 'sort([5, 10, 1])', 11 | 'sort(["C", "B", "A", "D"])', 12 | 'sortByLength(a, b) = size(a)[1] - size(b)[1]', 13 | 'sort(["Langdon", "Tom", "Sara"], sortByLength)' 14 | ], 15 | 'seealso': ['map', 'filter', 'forEach'] 16 | }; 17 | -------------------------------------------------------------------------------- /lib/expression/docs/function/matrix/dot.js: -------------------------------------------------------------------------------- 1 | module.exports = { 2 | 'name': 'dot', 3 | 'category': 'Matrix', 4 | 'syntax': [ 5 | 'dot(A, B)' 6 | ], 7 | 'description': 'Calculate the dot product of two vectors. ' + 8 | 'The dot product of A = [a1, a2, a3, ..., an] and B = [b1, b2, b3, ..., bn] ' + 9 | 'is defined as dot(A, B) = a1 * b1 + a2 * b2 + a3 * b3 + ... + an * bn', 10 | 'examples': [ 11 | 'dot([2, 4, 1], [2, 2, 3])', 12 | '[2, 4, 1] * [2, 2, 3]' 13 | ], 14 | 'seealso': [ 15 | 'multiply', 16 | 'cross' 17 | ] 18 | }; 19 | -------------------------------------------------------------------------------- /lib/expression/docs/function/relational/largerEq.js: -------------------------------------------------------------------------------- 1 | module.exports = { 2 | 'name': 'largerEq', 3 | 'category': 'Relational', 4 | 'syntax': [ 5 | 'x >= y', 6 | 'largerEq(x, y)' 7 | ], 8 | 'description': 9 | 'Check if value x is larger or equal to y. Returns true if x is larger or equal to y, and false if not.', 10 | 'examples': [ 11 | '2 > 1+1', 12 | '2 >= 1+1', 13 | 'a = 3.2', 14 | 'b = 6-2.8', 15 | '(a > b)' 16 | ], 17 | 'seealso': [ 18 | 'equal', 'unequal', 'smallerEq', 'smaller', 'largerEq', 'compare' 19 | ] 20 | }; 21 | -------------------------------------------------------------------------------- /lib/expression/docs/function/relational/smallerEq.js: -------------------------------------------------------------------------------- 1 | module.exports = { 2 | 'name': 'smallerEq', 3 | 'category': 'Relational', 4 | 'syntax': [ 5 | 'x <= y', 6 | 'smallerEq(x, y)' 7 | ], 8 | 'description': 9 | 'Check if value x is smaller or equal to value y. Returns true if x is smaller than y, and false if not.', 10 | 'examples': [ 11 | '2 < 1+1', 12 | '2 <= 1+1', 13 | 'a = 3.2', 14 | 'b = 6-2.8', 15 | '(a < b)' 16 | ], 17 | 'seealso': [ 18 | 'equal', 'unequal', 'larger', 'smaller', 'largerEq', 'compare' 19 | ] 20 | }; 21 | -------------------------------------------------------------------------------- /lib/expression/docs/function/arithmetic/nthRoot.js: -------------------------------------------------------------------------------- 1 | module.exports = { 2 | 'name': 'nthRoot', 3 | 'category': 'Arithmetic', 4 | 'syntax': [ 5 | 'nthRoot(a)', 6 | 'nthRoot(a, root)' 7 | ], 8 | 'description': 'Calculate the nth root of a value. ' + 9 | 'The principal nth root of a positive real number A, ' + 10 | 'is the positive real solution of the equation "x^root = A".', 11 | 'examples': [ 12 | '4 ^ 3', 13 | 'nthRoot(64, 3)', 14 | 'nthRoot(9, 2)', 15 | 'sqrt(9)' 16 | ], 17 | 'seealso': [ 18 | 'sqrt', 19 | 'pow' 20 | ] 21 | }; -------------------------------------------------------------------------------- /lib/expression/docs/function/relational/compare.js: -------------------------------------------------------------------------------- 1 | module.exports = { 2 | 'name': 'compare', 3 | 'category': 'Relational', 4 | 'syntax': [ 5 | 'compare(x, y)' 6 | ], 7 | 'description': 8 | 'Compare two values. Returns 1 if x is larger than y, -1 if x is smaller than y, and 0 if x and y are equal.', 9 | 'examples': [ 10 | 'compare(2, 3)', 11 | 'compare(3, 2)', 12 | 'compare(2, 2)', 13 | 'compare(5cm, 40mm)', 14 | 'compare(2, [1, 2, 3])' 15 | ], 16 | 'seealso': [ 17 | 'equal', 'unequal', 'smaller', 'smallerEq', 'largerEq' 18 | ] 19 | }; 20 | -------------------------------------------------------------------------------- /lib/expression/docs/function/relational/equal.js: -------------------------------------------------------------------------------- 1 | module.exports = { 2 | 'name': 'equal', 3 | 'category': 'Relational', 4 | 'syntax': [ 5 | 'x == y', 6 | 'equal(x, y)' 7 | ], 8 | 'description': 9 | 'Check equality of two values. Returns true if the values are equal, and false if not.', 10 | 'examples': [ 11 | '2+2 == 3', 12 | '2+2 == 4', 13 | 'a = 3.2', 14 | 'b = 6-2.8', 15 | 'a == b', 16 | '50cm == 0.5m' 17 | ], 18 | 'seealso': [ 19 | 'unequal', 'smaller', 'larger', 'smallerEq', 'largerEq', 'compare', 'deepEqual' 20 | ] 21 | }; 22 | -------------------------------------------------------------------------------- /lib/expression/docs/function/relational/smaller.js: -------------------------------------------------------------------------------- 1 | module.exports = { 2 | 'name': 'smaller', 3 | 'category': 'Relational', 4 | 'syntax': [ 5 | 'x < y', 6 | 'smaller(x, y)' 7 | ], 8 | 'description': 9 | 'Check if value x is smaller than value y. Returns true if x is smaller than y, and false if not.', 10 | 'examples': [ 11 | '2 < 3', 12 | '5 < 2*2', 13 | 'a = 3.3', 14 | 'b = 6-2.8', 15 | '(a < b)', 16 | '5 cm < 2 inch' 17 | ], 18 | 'seealso': [ 19 | 'equal', 'unequal', 'larger', 'smallerEq', 'largerEq', 'compare' 20 | ] 21 | }; 22 | -------------------------------------------------------------------------------- /lib/expression/docs/function/arithmetic/norm.js: -------------------------------------------------------------------------------- 1 | module.exports = { 2 | 'name': 'norm', 3 | 'category': 'Arithmetic', 4 | 'syntax': [ 5 | 'norm(x)', 6 | 'norm(x, p)' 7 | ], 8 | 'description': 'Calculate the norm of a number, vector or matrix.', 9 | 'examples': [ 10 | 'abs(-3.5)', 11 | 'norm(-3.5)', 12 | 'norm(3 - 4i))', 13 | 'norm([1, 2, -3], Infinity)', 14 | 'norm([1, 2, -3], -Infinity)', 15 | 'norm([3, 4], 2)', 16 | 'norm([[1, 2], [3, 4]], 1)', 17 | 'norm([[1, 2], [3, 4]], \'inf\')', 18 | 'norm([[1, 2], [3, 4]], \'fro\')' 19 | ] 20 | }; 21 | -------------------------------------------------------------------------------- /lib/expression/docs/function/relational/larger.js: -------------------------------------------------------------------------------- 1 | module.exports = { 2 | 'name': 'larger', 3 | 'category': 'Relational', 4 | 'syntax': [ 5 | 'x > y', 6 | 'larger(x, y)' 7 | ], 8 | 'description': 9 | 'Check if value x is larger than y. Returns true if x is larger than y, and false if not.', 10 | 'examples': [ 11 | '2 > 3', 12 | '5 > 2*2', 13 | 'a = 3.3', 14 | 'b = 6-2.8', 15 | '(a > b)', 16 | '(b < a)', 17 | '5 cm > 2 inch' 18 | ], 19 | 'seealso': [ 20 | 'equal', 'unequal', 'smaller', 'smallerEq', 'largerEq', 'compare' 21 | ] 22 | }; 23 | -------------------------------------------------------------------------------- /lib/expression/docs/function/construction/number.js: -------------------------------------------------------------------------------- 1 | module.exports = { 2 | 'name': 'number', 3 | 'category': 'Type', 4 | 'syntax': [ 5 | 'x', 6 | 'number(x)' 7 | ], 8 | 'description': 9 | 'Create a number or convert a string or boolean into a number.', 10 | 'examples': [ 11 | '2', 12 | '2e3', 13 | '4.05', 14 | 'number(2)', 15 | 'number("7.2")', 16 | 'number(true)', 17 | 'number([true, false, true, true])', 18 | 'number("52cm", "m")' 19 | ], 20 | 'seealso': [ 21 | 'bignumber', 'boolean', 'complex', 'index', 'matrix', 'string', 'unit' 22 | ] 23 | }; 24 | -------------------------------------------------------------------------------- /lib/expression/docs/function/matrix/eye.js: -------------------------------------------------------------------------------- 1 | module.exports = { 2 | 'name': 'eye', 3 | 'category': 'Matrix', 4 | 'syntax': [ 5 | 'eye(n)', 6 | 'eye(m, n)', 7 | 'eye([m, n])', 8 | 'eye' 9 | ], 10 | 'description': 'Returns the identity matrix with size m-by-n. The matrix has ones on the diagonal and zeros elsewhere.', 11 | 'examples': [ 12 | 'eye(3)', 13 | 'eye(3, 5)', 14 | 'a = [1, 2, 3; 4, 5, 6]', 15 | 'eye(size(a))' 16 | ], 17 | 'seealso': [ 18 | 'concat', 'det', 'diag', 'inv', 'ones', 'range', 'size', 'squeeze', 'subset', 'trace', 'transpose', 'zeros' 19 | ] 20 | }; 21 | -------------------------------------------------------------------------------- /lib/expression/docs/function/relational/unequal.js: -------------------------------------------------------------------------------- 1 | module.exports = { 2 | 'name': 'unequal', 3 | 'category': 'Relational', 4 | 'syntax': [ 5 | 'x != y', 6 | 'unequal(x, y)' 7 | ], 8 | 'description': 9 | 'Check unequality of two values. Returns true if the values are unequal, and false if they are equal.', 10 | 'examples': [ 11 | '2+2 != 3', 12 | '2+2 != 4', 13 | 'a = 3.2', 14 | 'b = 6-2.8', 15 | 'a != b', 16 | '50cm != 0.5m', 17 | '5 cm != 2 inch' 18 | ], 19 | 'seealso': [ 20 | 'equal', 'smaller', 'larger', 'smallerEq', 'largerEq', 'compare', 'deepEqual' 21 | ] 22 | }; 23 | -------------------------------------------------------------------------------- /lib/expression/docs/function/arithmetic/round.js: -------------------------------------------------------------------------------- 1 | module.exports = { 2 | 'name': 'round', 3 | 'category': 'Arithmetic', 4 | 'syntax': [ 5 | 'round(x)', 6 | 'round(x, n)' 7 | ], 8 | 'description': 9 | 'round a value towards the nearest integer.If x is complex, both real and imaginary part are rounded towards the nearest integer. When n is specified, the value is rounded to n decimals.', 10 | 'examples': [ 11 | 'round(3.2)', 12 | 'round(3.8)', 13 | 'round(-4.2)', 14 | 'round(-4.8)', 15 | 'round(pi, 3)', 16 | 'round(123.45678, 2)' 17 | ], 18 | 'seealso': ['ceil', 'floor', 'fix'] 19 | }; 20 | -------------------------------------------------------------------------------- /lib/expression/docs/function/statistics/median.js: -------------------------------------------------------------------------------- 1 | module.exports = { 2 | 'name': 'median', 3 | 'category': 'Statistics', 4 | 'syntax': [ 5 | 'median(a, b, c, ...)', 6 | 'median(A)' 7 | ], 8 | 'description': 'Compute the median of all values. The values are sorted and the middle value is returned. In case of an even number of values, the average of the two middle values is returned.', 9 | 'examples': [ 10 | 'median(5, 2, 7)', 11 | 'median([3, -1, 5, 7])' 12 | ], 13 | 'seealso': [ 14 | 'max', 15 | 'mean', 16 | 'min', 17 | 'prod', 18 | 'std', 19 | 'sum', 20 | 'var' 21 | ] 22 | }; 23 | -------------------------------------------------------------------------------- /lib/expression/docs/function/bitwise/bitOr.js: -------------------------------------------------------------------------------- 1 | module.exports = { 2 | 'name': 'bitOr', 3 | 'category': 'Bitwise', 4 | 'syntax': [ 5 | 'x | y', 6 | 'bitOr(x, y)' 7 | ], 8 | 'description': 'Bitwise OR operation. Performs the logical inclusive OR operation on each pair of corresponding bits of the two given values. The result in each position is 1 if the first bit is 1 or the second bit is 1 or both bits are 1, otherwise, the result is 0.', 9 | 'examples': [ 10 | '5 | 3', 11 | 'bitOr([1, 2, 3], 4)' 12 | ], 13 | 'seealso': [ 14 | 'bitAnd', 'bitNot', 'bitXor', 'leftShift', 'rightArithShift', 'rightLogShift' 15 | ] 16 | }; 17 | -------------------------------------------------------------------------------- /lib/expression/docs/function/matrix/zeros.js: -------------------------------------------------------------------------------- 1 | module.exports = { 2 | 'name': 'zeros', 3 | 'category': 'Matrix', 4 | 'syntax': [ 5 | 'zeros(m)', 6 | 'zeros(m, n)', 7 | 'zeros(m, n, p, ...)', 8 | 'zeros([m])', 9 | 'zeros([m, n])', 10 | 'zeros([m, n, p, ...])', 11 | 'zeros' 12 | ], 13 | 'description': 'Create a matrix containing zeros.', 14 | 'examples': [ 15 | 'zeros(3)', 16 | 'zeros(3, 5)', 17 | 'a = [1, 2, 3; 4, 5, 6]', 18 | 'zeros(size(a))' 19 | ], 20 | 'seealso': [ 21 | 'concat', 'det', 'diag', 'eye', 'inv', 'ones', 'range', 'size', 'squeeze', 'subset', 'trace', 'transpose' 22 | ] 23 | }; 24 | -------------------------------------------------------------------------------- /CONTRIBUTING.md: -------------------------------------------------------------------------------- 1 | ## Contributing 2 | 3 | Contributions to the math.js library are very welcome! We can't do this alone. 4 | You can contribute in different ways: spread the word, report bugs, come up with 5 | ideas and suggestions, and contribute to the code. 6 | 7 | There are a few preferences regarding code contributions: 8 | 9 | - Math.js follows the node.js code style as described 10 | [here](http://nodeguide.com/style.html). 11 | - Send pull requests to the `develop` branch, not the `master` branch. 12 | - Only commit changes done in the source files under `lib`, not to the builds 13 | which are located in the folder `dist`. 14 | 15 | Thanks! 16 | 17 | -------------------------------------------------------------------------------- /lib/expression/docs/function/bitwise/bitXor.js: -------------------------------------------------------------------------------- 1 | module.exports = { 2 | 'name': 'bitXor', 3 | 'category': 'Bitwise', 4 | 'syntax': [ 5 | 'bitXor(x, y)' 6 | ], 7 | 'description': 'Bitwise XOR operation, exclusive OR. Performs the logical exclusive OR operation on each pair of corresponding bits of the two given values. The result in each position is 1 if only the first bit is 1 or only the second bit is 1, but will be 0 if both are 0 or both are 1.', 8 | 'examples': [ 9 | 'bitOr(1, 2)', 10 | 'bitXor([2, 3, 4], 4)' 11 | ], 12 | 'seealso': [ 13 | 'bitAnd', 'bitNot', 'bitOr', 'leftShift', 'rightArithShift', 'rightLogShift' 14 | ] 15 | }; 16 | -------------------------------------------------------------------------------- /lib/expression/docs/function/statistics/mean.js: -------------------------------------------------------------------------------- 1 | module.exports = { 2 | 'name': 'mean', 3 | 'category': 'Statistics', 4 | 'syntax': [ 5 | 'mean(a, b, c, ...)', 6 | 'mean(A)', 7 | 'mean(A, dim)' 8 | ], 9 | 'description': 'Compute the arithmetic mean of a list of values.', 10 | 'examples': [ 11 | 'mean(2, 3, 4, 1)', 12 | 'mean([2, 3, 4, 1])', 13 | 'mean([2, 5; 4, 3])', 14 | 'mean([2, 5; 4, 3], 1)', 15 | 'mean([2, 5; 4, 3], 2)', 16 | 'mean([1.0, 2.7, 3.2, 4.0])' 17 | ], 18 | 'seealso': [ 19 | 'max', 20 | 'median', 21 | 'min', 22 | 'prod', 23 | 'std', 24 | 'sum', 25 | 'var' 26 | ] 27 | }; 28 | -------------------------------------------------------------------------------- /component.json: -------------------------------------------------------------------------------- 1 | { 2 | "name": "mathjs", 3 | "repo": "josdejong/mathjs", 4 | "description": "Math.js is an extensive math library for JavaScript and Node.js. It features a flexible expression parser and offers an integrated solution to work with numbers, big numbers, complex numbers, units, and matrices.", 5 | "version": "1.3.0", 6 | "main": "dist/math.min.js", 7 | "keywords": [ 8 | "math", 9 | "mathematics", 10 | "functions", 11 | "numeric", 12 | "parser", 13 | "expression", 14 | "number", 15 | "bignumber", 16 | "complex", 17 | "matrix", 18 | "unit" 19 | ], 20 | "scripts": [ 21 | "dist/math.min.js" 22 | ] 23 | } 24 | -------------------------------------------------------------------------------- /lib/expression/docs/function/matrix/ones.js: -------------------------------------------------------------------------------- 1 | module.exports = { 2 | 'name': 'ones', 3 | 'category': 'Matrix', 4 | 'syntax': [ 5 | 'ones(m)', 6 | 'ones(m, n)', 7 | 'ones(m, n, p, ...)', 8 | 'ones([m])', 9 | 'ones([m, n])', 10 | 'ones([m, n, p, ...])', 11 | 'ones' 12 | ], 13 | 'description': 'Create a matrix containing ones.', 14 | 'examples': [ 15 | 'ones(3)', 16 | 'ones(3, 5)', 17 | 'ones([2,3]) * 4.5', 18 | 'a = [1, 2, 3; 4, 5, 6]', 19 | 'ones(size(a))' 20 | ], 21 | 'seealso': [ 22 | 'concat', 'det', 'diag', 'eye', 'inv', 'range', 'size', 'squeeze', 'subset', 'trace', 'transpose', 'zeros' 23 | ] 24 | }; 25 | -------------------------------------------------------------------------------- /NOTICE: -------------------------------------------------------------------------------- 1 | math.js 2 | https://github.com/josdejong/mathjs 3 | 4 | Copyright (C) 2013-2015 Jos de Jong 5 | 6 | Licensed under the Apache License, Version 2.0 (the "License"); 7 | you may not use this file except in compliance with the License. 8 | You may obtain a copy of the License at 9 | 10 | http://www.apache.org/licenses/LICENSE-2.0 11 | 12 | Unless required by applicable law or agreed to in writing, software 13 | distributed under the License is distributed on an "AS IS" BASIS, 14 | WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. 15 | See the License for the specific language governing permissions and 16 | limitations under the License. 17 | -------------------------------------------------------------------------------- /lib/expression/docs/function/bitwise/bitAnd.js: -------------------------------------------------------------------------------- 1 | module.exports = { 2 | 'name': 'bitAnd', 3 | 'category': 'Bitwise', 4 | 'syntax': [ 5 | 'x & y', 6 | 'bitAnd(x, y)' 7 | ], 8 | 'description': 'Bitwise AND operation. Performs the logical AND operation on each pair of the corresponding bits of the two given values by multiplying them. If both bits in the compared position are 1, the bit in the resulting binary representation is 1, otherwise, the result is 0', 9 | 'examples': [ 10 | '5 & 3', 11 | 'bitAnd(53, 131)', 12 | '[1, 12, 31] & 42' 13 | ], 14 | 'seealso': [ 15 | 'bitNot', 'bitOr', 'bitXor', 'leftShift', 'rightArithShift', 'rightLogShift' 16 | ] 17 | }; 18 | -------------------------------------------------------------------------------- /lib/expression/docs/function/statistics/max.js: -------------------------------------------------------------------------------- 1 | module.exports = { 2 | 'name': 'max', 3 | 'category': 'Statistics', 4 | 'syntax': [ 5 | 'max(a, b, c, ...)', 6 | 'max(A)', 7 | 'max(A, dim)' 8 | ], 9 | 'description': 'Compute the maximum value of a list of values.', 10 | 'examples': [ 11 | 'max(2, 3, 4, 1)', 12 | 'max([2, 3, 4, 1])', 13 | 'max([2, 5; 4, 3])', 14 | 'max([2, 5; 4, 3], 1)', 15 | 'max([2, 5; 4, 3], 2)', 16 | 'max(2.7, 7.1, -4.5, 2.0, 4.1)', 17 | 'min(2.7, 7.1, -4.5, 2.0, 4.1)' 18 | ], 19 | 'seealso': [ 20 | 'mean', 21 | 'median', 22 | 'min', 23 | 'prod', 24 | 'std', 25 | 'sum', 26 | 'var' 27 | ] 28 | }; 29 | -------------------------------------------------------------------------------- /lib/expression/docs/function/statistics/min.js: -------------------------------------------------------------------------------- 1 | module.exports = { 2 | 'name': 'min', 3 | 'category': 'Statistics', 4 | 'syntax': [ 5 | 'min(a, b, c, ...)', 6 | 'min(A)', 7 | 'min(A, dim)' 8 | ], 9 | 'description': 'Compute the minimum value of a list of values.', 10 | 'examples': [ 11 | 'min(2, 3, 4, 1)', 12 | 'min([2, 3, 4, 1])', 13 | 'min([2, 5; 4, 3])', 14 | 'min([2, 5; 4, 3], 1)', 15 | 'min([2, 5; 4, 3], 2)', 16 | 'min(2.7, 7.1, -4.5, 2.0, 4.1)', 17 | 'max(2.7, 7.1, -4.5, 2.0, 4.1)' 18 | ], 19 | 'seealso': [ 20 | 'max', 21 | 'mean', 22 | 'median', 23 | 'prod', 24 | 'std', 25 | 'sum', 26 | 'var' 27 | ] 28 | }; 29 | -------------------------------------------------------------------------------- /examples/browser/webworkers/worker.js: -------------------------------------------------------------------------------- 1 | importScripts('../../../dist/math.js'); 2 | 3 | // create a parser 4 | var parser = math.parser(); 5 | 6 | self.addEventListener('message', function(event) { 7 | var request = JSON.parse(event.data), 8 | result = null, 9 | err = null; 10 | 11 | try { 12 | // evaluate the expression 13 | result = parser.eval(request.expr); 14 | } 15 | catch (e) { 16 | // return the error 17 | err = e; 18 | } 19 | 20 | // build a response 21 | var response = { 22 | id: request.id, 23 | result: result, 24 | err: err 25 | }; 26 | 27 | // send the response back 28 | self.postMessage(JSON.stringify(response)); 29 | 30 | }, false); -------------------------------------------------------------------------------- /lib/expression/docs/function/arithmetic/log.js: -------------------------------------------------------------------------------- 1 | module.exports = { 2 | 'name': 'log', 3 | 'category': 'Arithmetic', 4 | 'syntax': [ 5 | 'log(x)', 6 | 'log(x, base)' 7 | ], 8 | 'description': 'Compute the logarithm of a value. If no base is provided, the natural logarithm of x is calculated. If base if provided, the logarithm is calculated for the specified base. log(x, base) is defined as log(x) / log(base).', 9 | 'examples': [ 10 | 'log(3.5)', 11 | 'a = log(2.4)', 12 | 'exp(a)', 13 | '10 ^ 4', 14 | 'log(10000, 10)', 15 | 'log(10000) / log(10)', 16 | 'b = log(1024, 2)', 17 | '2 ^ b' 18 | ], 19 | 'seealso': [ 20 | 'exp', 21 | 'log10' 22 | ] 23 | }; -------------------------------------------------------------------------------- /lib/expression/docs/function/matrix/concat.js: -------------------------------------------------------------------------------- 1 | module.exports = { 2 | 'name': 'concat', 3 | 'category': 'Matrix', 4 | 'syntax': [ 5 | 'concat(A, B, C, ...)', 6 | 'concat(A, B, C, ..., dim)' 7 | ], 8 | 'description': 'Concatenate matrices. By default, the matrices are concatenated by the last dimension. The dimension on which to concatenate can be provided as last argument.', 9 | 'examples': [ 10 | 'A = [1, 2; 5, 6]', 11 | 'B = [3, 4; 7, 8]', 12 | 'concat(A, B)', 13 | 'concat(A, B, 1)', 14 | 'concat(A, B, 2)' 15 | ], 16 | 'seealso': [ 17 | 'det', 'diag', 'eye', 'inv', 'ones', 'range', 'size', 'squeeze', 'subset', 'trace', 'transpose', 'zeros' 18 | ] 19 | }; 20 | -------------------------------------------------------------------------------- /lib/expression/node/index.js: -------------------------------------------------------------------------------- 1 | 'use strict'; 2 | 3 | exports.ArrayNode = require('./ArrayNode'); 4 | exports.AssignmentNode = require('./AssignmentNode'); 5 | exports.BlockNode = require('./BlockNode'); 6 | exports.ConditionalNode = require('./ConditionalNode'); 7 | exports.ConstantNode = require('./ConstantNode'); 8 | exports.IndexNode = require('./IndexNode'); 9 | exports.FunctionAssignmentNode = require('./FunctionAssignmentNode'); 10 | exports.FunctionNode = require('./FunctionNode'); 11 | exports.Node = require('./Node'); 12 | exports.OperatorNode = require('./OperatorNode'); 13 | exports.RangeNode = require('./RangeNode'); 14 | exports.SymbolNode = require('./SymbolNode'); 15 | exports.UpdateNode = require('./UpdateNode'); 16 | -------------------------------------------------------------------------------- /lib/expression/docs/function/statistics/var.js: -------------------------------------------------------------------------------- 1 | module.exports = { 2 | 'name': 'var', 3 | 'category': 'Statistics', 4 | 'syntax': [ 5 | 'var(a, b, c, ...)', 6 | 'var(A)', 7 | 'var(A, normalization)' 8 | ], 9 | 'description': 'Compute the variance of all values. Optional parameter normalization can be "unbiased" (default), "uncorrected", or "biased".', 10 | 'examples': [ 11 | 'var(2, 4, 6)', 12 | 'var([2, 4, 6, 8])', 13 | 'var([2, 4, 6, 8], "uncorrected")', 14 | 'var([2, 4, 6, 8], "biased")', 15 | 'var([1, 2, 3; 4, 5, 6])' 16 | ], 17 | 'seealso': [ 18 | 'max', 19 | 'mean', 20 | 'min', 21 | 'median', 22 | 'min', 23 | 'prod', 24 | 'std', 25 | 'sum' 26 | ] 27 | }; 28 | -------------------------------------------------------------------------------- /lib/expression/docs/function/construction/index.js: -------------------------------------------------------------------------------- 1 | module.exports = { 2 | 'name': 'index', 3 | 'category': 'Type', 4 | 'syntax': [ 5 | '[start]', 6 | '[start:end]', 7 | '[start:step:end]', 8 | '[start1, start 2, ...]', 9 | '[start1:end1, start2:end2, ...]', 10 | '[start1:step1:end1, start2:step2:end2, ...]' 11 | ], 12 | 'description': 13 | 'Create an index to get or replace a subset of a matrix', 14 | 'examples': [ 15 | '[]', 16 | '[1, 2, 3]', 17 | 'A = [1, 2, 3; 4, 5, 6]', 18 | 'A[1, :]', 19 | 'A[1, 2] = 50', 20 | 'A[0:2, 0:2] = ones(2, 2)' 21 | ], 22 | 'seealso': [ 23 | 'bignumber', 'boolean', 'complex', 'matrix,', 'number', 'range', 'string', 'unit' 24 | ] 25 | }; 26 | -------------------------------------------------------------------------------- /lib/expression/docs/function/matrix/range.js: -------------------------------------------------------------------------------- 1 | module.exports = { 2 | 'name': 'range', 3 | 'category': 'Type', 4 | 'syntax': [ 5 | 'start:end', 6 | 'start:step:end', 7 | 'range(start, end)', 8 | 'range(start, end, step)', 9 | 'range(string)' 10 | ], 11 | 'description': 12 | 'Create a range. Lower bound of the range is included, upper bound is excluded.', 13 | 'examples': [ 14 | '1:5', 15 | '3:-1:-3', 16 | 'range(3, 7)', 17 | 'range(0, 12, 2)', 18 | 'range("4:10")', 19 | 'a = [1, 2, 3, 4; 5, 6, 7, 8]', 20 | 'a[1:2, 1:2]' 21 | ], 22 | 'seealso': [ 23 | 'concat', 'det', 'diag', 'eye', 'inv', 'ones', 'size', 'squeeze', 'subset', 'trace', 'transpose', 'zeros' 24 | ] 25 | }; 26 | -------------------------------------------------------------------------------- /lib/expression/docs/function/probability/distribution.js: -------------------------------------------------------------------------------- 1 | module.exports = { 2 | 'name': 'distribution', 3 | 'category': 'Probability', 4 | 'syntax': [ 5 | 'distribution(name)', 6 | 'distribution(name, arg1, arg2, ...)' 7 | ], 8 | 'description': 9 | 'Create a distribution object of a specific type. ' + 10 | 'A distribution object contains functions `random([size,] [min,] [max])`, ' + 11 | '`randomInt([size,] [min,] [max])`, and `pickRandom(array)`. ' + 12 | 'Available types of distributions: "uniform", "normal". ' + 13 | 'Note that the function distribution is currently not available via the expression parser.', 14 | 'examples': [ 15 | ], 16 | 'seealso': ['random', 'randomInt'] 17 | }; 18 | -------------------------------------------------------------------------------- /lib/util/function.js: -------------------------------------------------------------------------------- 1 | // function utils 2 | 3 | /* 4 | * Memoize a given function by caching the computed result. 5 | * The cache of a memoized function can be cleared by deleting the `cache` 6 | * property of the function. 7 | * 8 | * @param {function} fn The function to be memoized. Must be a pure function. 9 | * @return {function} Returns the memoized function 10 | */ 11 | exports.memoize = function(fn) { 12 | return function memoize() { 13 | if (typeof memoize.cache !== 'object') { 14 | memoize.cache = {}; 15 | } 16 | 17 | var hash = JSON.stringify(arguments); 18 | if (!(hash in memoize.cache)) { 19 | return memoize.cache[hash] = fn.apply(fn, arguments); 20 | } 21 | return memoize.cache[hash]; 22 | }; 23 | }; 24 | -------------------------------------------------------------------------------- /test/README.md: -------------------------------------------------------------------------------- 1 | ## Tests 2 | 3 | To execute the tests, install `mocha ` and run the following in the root of 4 | the project: 5 | 6 | npm install -g mocha 7 | mocha test --recursive 8 | 9 | It's important to run mocha with the `--recursive` flag, as most tests are 10 | located in nested folders. 11 | 12 | 13 | ## Code coverage 14 | 15 | To test code coverage of the tests, install `istanbul` and run it: 16 | 17 | npm install -g istanbul 18 | istanbul cover _mocha -- test --recursive 19 | 20 | Note the underscore before mocha, and the `--` surrounded by spaces after _mocha. 21 | See also https://github.com/gotwarlost/istanbul/issues/44. 22 | 23 | To see the results, open the generated report in your browser: 24 | 25 | ./coverage/lcov-report/index.html 26 | -------------------------------------------------------------------------------- /test/function/construction/chain.test.js: -------------------------------------------------------------------------------- 1 | var assert = require('assert'), 2 | math = require('../../../index'), 3 | Chain = math.chaining.Chain; 4 | 5 | describe('chain', function() { 6 | 7 | it ('should construct a chain', function () { 8 | assert.ok(math.chain(45) instanceof Chain); 9 | assert.ok(math.chain(math.complex(2, 3)) instanceof Chain); 10 | assert.ok(math.chain() instanceof Chain); 11 | }); 12 | 13 | it ('deprecated select function should still be functional', function () { 14 | assert.ok(math.select(45) instanceof Chain); 15 | assert.ok(math.select(math.complex(2,3)) instanceof Chain); 16 | assert.ok(math.select() instanceof Chain); 17 | assert.strictEqual(math.chaining.Selector, math.chaining.Chain); 18 | }); 19 | 20 | }); -------------------------------------------------------------------------------- /lib/expression/docs/function/statistics/std.js: -------------------------------------------------------------------------------- 1 | module.exports = { 2 | 'name': 'std', 3 | 'category': 'Statistics', 4 | 'syntax': [ 5 | 'std(a, b, c, ...)', 6 | 'std(A)', 7 | 'std(A, normalization)' 8 | ], 9 | 'description': 'Compute the standard deviation of all values, defined as std(A) = sqrt(var(A)). Optional parameter normalization can be "unbiased" (default), "uncorrected", or "biased".', 10 | 'examples': [ 11 | 'std(2, 4, 6)', 12 | 'std([2, 4, 6, 8])', 13 | 'std([2, 4, 6, 8], "uncorrected")', 14 | 'std([2, 4, 6, 8], "biased")', 15 | 'std([1, 2, 3; 4, 5, 6])' 16 | ], 17 | 'seealso': [ 18 | 'max', 19 | 'mean', 20 | 'min', 21 | 'median', 22 | 'min', 23 | 'prod', 24 | 'sum', 25 | 'var' 26 | ] 27 | }; 28 | -------------------------------------------------------------------------------- /test/test.min.html: -------------------------------------------------------------------------------- 1 | 2 | 3 | 4 | mathjs test 5 | 6 | 7 | 8 | 9 |

10 | Use the mathjs library from the console... 11 |

12 | 30 | 31 | -------------------------------------------------------------------------------- /test/util/boolean.test.js: -------------------------------------------------------------------------------- 1 | // test boolean utils 2 | var assert = require('assert'), 3 | approx = require('../../tools/approx'), 4 | boolean = require('../../lib/util/boolean'); 5 | 6 | describe ('boolean', function () { 7 | 8 | it('isBoolean', function() { 9 | assert.equal(boolean.isBoolean(true), true); 10 | assert.equal(boolean.isBoolean(false), true); 11 | assert.equal(boolean.isBoolean(new Boolean(true)), true); 12 | assert.equal(boolean.isBoolean(new Boolean(false)), true); 13 | 14 | assert.equal(boolean.isBoolean('hi'), false); 15 | assert.equal(boolean.isBoolean(23), false); 16 | assert.equal(boolean.isBoolean([]), false); 17 | assert.equal(boolean.isBoolean({}), false); 18 | assert.equal(boolean.isBoolean(new Date()), false); 19 | }); 20 | 21 | }); -------------------------------------------------------------------------------- /lib/expression/docs/function/matrix/subset.js: -------------------------------------------------------------------------------- 1 | module.exports = { 2 | 'name': 'subset', 3 | 'category': 'Matrix', 4 | 'syntax': [ 5 | 'value(index)', 6 | 'value(index) = replacement', 7 | 'subset(value, [index])', 8 | 'subset(value, [index], replacement)' 9 | ], 10 | 'description': 'Get or set a subset of a matrix or string. ' + 11 | 'Indexes are one-based. ' + 12 | 'Both the ranges lower-bound and upper-bound are included.', 13 | 'examples': [ 14 | 'd = [1, 2; 3, 4]', 15 | 'e = []', 16 | 'e[1, 1:2] = [5, 6]', 17 | 'e[2, :] = [7, 8]', 18 | 'f = d * e', 19 | 'f[2, 1]', 20 | 'f[:, 1]' 21 | ], 22 | 'seealso': [ 23 | 'concat', 'det', 'diag', 'eye', 'inv', 'ones', 'range', 'size', 'squeeze', 'trace', 'transpose', 'zeros' 24 | ] 25 | }; 26 | -------------------------------------------------------------------------------- /lib/function/probability/pickRandom.js: -------------------------------------------------------------------------------- 1 | 'use strict'; 2 | 3 | module.exports = function (math) { 4 | var distribution = require('./distribution')(math); 5 | 6 | /** 7 | * Random pick a value from a one dimensional array. 8 | * Array element is picked using a random function with uniform distribution. 9 | * 10 | * Syntax: 11 | * 12 | * math.pickRandom(array) 13 | * 14 | * Examples: 15 | * 16 | * math.pickRandom([3, 6, 12, 2]); // returns one of the values in the array 17 | * 18 | * See also: 19 | * 20 | * random, randomInt 21 | * 22 | * @param {Array} array A one dimensional array 23 | * @return {Number} One of the elements of the provided input array 24 | */ 25 | math.pickRandom = distribution('uniform').pickRandom; 26 | }; 27 | -------------------------------------------------------------------------------- /docs/reference/functions/det.md: -------------------------------------------------------------------------------- 1 | # Function det 2 | 3 | Calculate the determinant of a matrix. 4 | 5 | 6 | ## Syntax 7 | 8 | ```js 9 | math.det(x) 10 | ``` 11 | 12 | ### Parameters 13 | 14 | Parameter | Type | Description 15 | --------- | ---- | ----------- 16 | `x` | Array | Matrix | A matrix 17 | 18 | ### Returns 19 | 20 | Type | Description 21 | ---- | ----------- 22 | Number | The determinant of `x` 23 | 24 | 25 | ## Examples 26 | 27 | ```js 28 | math.det([[1, 2], [3, 4]]); // returns -2 29 | 30 | var A = [ 31 | [-2, 2, 3], 32 | [-1, 1, 3], 33 | [2, 0, -1] 34 | ] 35 | math.det(A); // returns 6 36 | ``` 37 | 38 | 39 | ## See also 40 | 41 | [inv](inv.md) 42 | 43 | 44 | 45 | -------------------------------------------------------------------------------- /lib/util/types.js: -------------------------------------------------------------------------------- 1 | 'use strict'; 2 | 3 | /** 4 | * Determine the type of a variable 5 | * 6 | * type(x) 7 | * 8 | * @param {*} x 9 | * @return {String} type Lower case type, for example 'number', 'string', 10 | * 'array', 'date'. 11 | */ 12 | exports.type = function(x) { 13 | var type = typeof x; 14 | 15 | if (type === 'object') { 16 | if (x === null) return 'null'; 17 | if (x instanceof Boolean) return 'boolean'; 18 | if (x instanceof Number) return 'number'; 19 | if (x instanceof String) return 'string'; 20 | if (Array.isArray(x)) return 'array'; 21 | if (x instanceof Date) return 'date'; 22 | if (x instanceof Function)return 'function'; 23 | if (x instanceof RegExp) return 'regexp'; 24 | } 25 | 26 | return type; 27 | }; 28 | -------------------------------------------------------------------------------- /lib/expression/transform/subset.transform.js: -------------------------------------------------------------------------------- 1 | 'use strict'; 2 | 3 | var errorTransform = require('./error.transform').transform; 4 | var isBoolean = require('../../util/boolean').isBoolean; 5 | var argsToArray = require('../../util/array').argsToArray; 6 | 7 | /** 8 | * Attach a transform function to math.subset 9 | * Adds a property transform containing the transform function. 10 | * 11 | * This transform creates a range which includes the end value 12 | * @param {Object} math 13 | */ 14 | module.exports = function (math) { 15 | var transform = function () { 16 | try { 17 | return math.subset.apply(math, argsToArray(arguments)); 18 | } 19 | catch (err) { 20 | throw errorTransform(err); 21 | } 22 | }; 23 | 24 | math.subset.transform = transform; 25 | 26 | return transform; 27 | }; 28 | -------------------------------------------------------------------------------- /lib/type/ResultSet.js: -------------------------------------------------------------------------------- 1 | 'use strict'; 2 | 3 | /** 4 | * A ResultSet contains a list or results 5 | * @param {Array} entries 6 | * @constructor 7 | */ 8 | function ResultSet(entries) { 9 | if (!(this instanceof ResultSet)) { 10 | throw new SyntaxError('Constructor must be called with the new operator'); 11 | } 12 | 13 | this.entries = entries || []; 14 | } 15 | 16 | /** 17 | * Returns the array with results hold by this ResultSet 18 | * @returns {Array} entries 19 | */ 20 | ResultSet.prototype.valueOf = function () { 21 | return this.entries; 22 | }; 23 | 24 | /** 25 | * Returns the stringified results of the ResultSet 26 | * @returns {String} string 27 | */ 28 | ResultSet.prototype.toString = function () { 29 | return '[' + this.entries.join(', ') + ']'; 30 | }; 31 | 32 | module.exports = ResultSet; 33 | -------------------------------------------------------------------------------- /docs/reference/functions/flatten.md: -------------------------------------------------------------------------------- 1 | # Function flatten 2 | 3 | Flatten a multi dimensional matrix into a single dimensional matrix. 4 | 5 | 6 | ## Syntax 7 | 8 | ```js 9 | math.flatten(x) 10 | ``` 11 | 12 | ### Parameters 13 | 14 | Parameter | Type | Description 15 | --------- | ---- | ----------- 16 | `x` | Matrix | Array | Matrix to be flattened 17 | 18 | ### Returns 19 | 20 | Type | Description 21 | ---- | ----------- 22 | Matrix | Array | Returns the flattened matrix 23 | 24 | 25 | ## Examples 26 | 27 | ```js 28 | math.flatten([[1,2], [3,4]]); // returns [1, 2, 3, 4] 29 | ``` 30 | 31 | 32 | ## See also 33 | 34 | [concat](concat.md), 35 | [resize](resize.md), 36 | [size](size.md), 37 | [squeeze](squeeze.md) 38 | 39 | 40 | 41 | -------------------------------------------------------------------------------- /lib/expression/transform/range.transform.js: -------------------------------------------------------------------------------- 1 | 'use strict'; 2 | 3 | var isBoolean = require('../../util/boolean').isBoolean; 4 | var argsToArray = require('../../util/array').argsToArray; 5 | 6 | /** 7 | * Attach a transform function to math.range 8 | * Adds a property transform containing the transform function. 9 | * 10 | * This transform creates a range which includes the end value 11 | * @param {Object} math 12 | */ 13 | module.exports = function (math) { 14 | var transform = function () { 15 | var args = argsToArray(arguments); 16 | 17 | var lastIndex = args.length - 1; 18 | var last = args[lastIndex]; 19 | if (!isBoolean(last)) { 20 | args.push(true); // append a parameter includeEnd=true 21 | } 22 | 23 | return math.range.apply(math, args); 24 | }; 25 | 26 | math.range.transform = transform; 27 | 28 | return transform; 29 | }; 30 | -------------------------------------------------------------------------------- /docs/reference/functions/trace.md: -------------------------------------------------------------------------------- 1 | # Function trace 2 | 3 | Calculate the trace of a matrix: the sum of the elements on the main 4 | diagonal of a square matrix. 5 | 6 | 7 | ## Syntax 8 | 9 | ```js 10 | math.trace(x) 11 | ``` 12 | 13 | ### Parameters 14 | 15 | Parameter | Type | Description 16 | --------- | ---- | ----------- 17 | `x` | Array | Matrix | A matrix 18 | 19 | ### Returns 20 | 21 | Type | Description 22 | ---- | ----------- 23 | Number | The trace of `x` 24 | 25 | 26 | ## Examples 27 | 28 | ```js 29 | math.trace([[1, 2], [3, 4]]); // returns 5 30 | 31 | var A = [ 32 | [1, 2, 3], 33 | [-1, 2, 3], 34 | [2, 0, 3] 35 | ] 36 | math.trace(A); // returns 6 37 | ``` 38 | 39 | 40 | ## See also 41 | 42 | [diag](diag.md) 43 | 44 | 45 | 46 | -------------------------------------------------------------------------------- /docs/reference/functions/clone.md: -------------------------------------------------------------------------------- 1 | # Function clone 2 | 3 | Clone an object. 4 | 5 | 6 | ## Syntax 7 | 8 | ```js 9 | math.clone(x) 10 | ``` 11 | 12 | ### Parameters 13 | 14 | Parameter | Type | Description 15 | --------- | ---- | ----------- 16 | `x` | * | Object to be cloned 17 | 18 | ### Returns 19 | 20 | Type | Description 21 | ---- | ----------- 22 | * | A clone of object x 23 | 24 | 25 | ## Examples 26 | 27 | ```js 28 | math.clone(3.5); // returns number 3.5 29 | math.clone(2 - 4i); // returns Complex 2 - 4i 30 | math.clone(45 deg); // returns Unit 45 deg 31 | math.clone([[1, 2], [3, 4]]); // returns Array [[1, 2], [3, 4]] 32 | math.clone("hello world"); // returns string "hello world" 33 | ``` 34 | 35 | 36 | 37 | 38 | 39 | -------------------------------------------------------------------------------- /docs/reference/functions/help.md: -------------------------------------------------------------------------------- 1 | # Function help 2 | 3 | Retrieve help on a function or data type. 4 | Help files are retrieved from the documentation in math.expression.docs. 5 | 6 | 7 | ## Syntax 8 | 9 | ```js 10 | math.help(search) 11 | ``` 12 | 13 | ### Parameters 14 | 15 | Parameter | Type | Description 16 | --------- | ---- | ----------- 17 | `search` | function | string | Object | A function or function name for which to get help 18 | 19 | ### Returns 20 | 21 | Type | Description 22 | ---- | ----------- 23 | Help | A help object 24 | 25 | 26 | ## Examples 27 | 28 | ```js 29 | console.log(math.help('sin').toString()); 30 | console.log(math.help(math.add).toString()); 31 | console.log(math.help(math.add).toJSON()); 32 | ``` 33 | 34 | 35 | 36 | 37 | 38 | -------------------------------------------------------------------------------- /lib/expression/docs/function/matrix/diag.js: -------------------------------------------------------------------------------- 1 | module.exports = { 2 | 'name': 'diag', 3 | 'category': 'Matrix', 4 | 'syntax': [ 5 | 'diag(x)', 6 | 'diag(x, k)' 7 | ], 8 | 'description': 'Create a diagonal matrix or retrieve the diagonal of a matrix. When x is a vector, a matrix with the vector values on the diagonal will be returned. When x is a matrix, a vector with the diagonal values of the matrix is returned. When k is provided, the k-th diagonal will be filled in or retrieved, if k is positive, the values are placed on the super diagonal. When k is negative, the values are placed on the sub diagonal.', 9 | 'examples': [ 10 | 'diag(1:3)', 11 | 'diag(1:3, 1)', 12 | 'a = [1, 2, 3; 4, 5, 6; 7, 8, 9]', 13 | 'diag(a)' 14 | ], 15 | 'seealso': [ 16 | 'concat', 'det', 'eye', 'inv', 'ones', 'range', 'size', 'squeeze', 'subset', 'trace', 'transpose', 'zeros' 17 | ] 18 | }; 19 | -------------------------------------------------------------------------------- /test/function/expression/parse.test.js: -------------------------------------------------------------------------------- 1 | // test parse 2 | var assert = require('assert'), 3 | error = require('../../../lib/error/index'), 4 | math = require('../../../index'), 5 | Node = require('../../../lib/expression/node/Node'); 6 | 7 | describe('parse', function() { 8 | 9 | it('should parse an expression', function() { 10 | var node = math.parse('(5+3)/4'); 11 | assert.ok(node instanceof Node); 12 | assert.equal(node.compile(math).eval(), 2); 13 | }); 14 | 15 | it('should parse multiple expressions', function() { 16 | var nodes = math.parse(['2+3', '4+5']); 17 | assert.ok(Array.isArray(nodes)); 18 | assert.equal(nodes.length, 2); 19 | 20 | assert.ok(nodes[0] instanceof Node); 21 | assert.ok(nodes[1] instanceof Node); 22 | assert.equal(nodes[0].compile(math).eval(), 5); 23 | assert.equal(nodes[1].compile(math).eval(), 9); 24 | }); 25 | 26 | }); 27 | -------------------------------------------------------------------------------- /docs/reference/functions/pickRandom.md: -------------------------------------------------------------------------------- 1 | # Function pickRandom 2 | 3 | Random pick a value from a one dimensional array. 4 | Array element is picked using a random function with uniform distribution. 5 | 6 | 7 | ## Syntax 8 | 9 | ```js 10 | math.pickRandom(array) 11 | ``` 12 | 13 | ### Parameters 14 | 15 | Parameter | Type | Description 16 | --------- | ---- | ----------- 17 | `array` | Array | A one dimensional array 18 | 19 | ### Returns 20 | 21 | Type | Description 22 | ---- | ----------- 23 | Number | One of the elements of the provided input array 24 | 25 | 26 | ## Examples 27 | 28 | ```js 29 | math.pickRandom([3, 6, 12, 2]); // returns one of the values in the array 30 | ``` 31 | 32 | 33 | ## See also 34 | 35 | [random](random.md), 36 | [randomInt](randomInt.md) 37 | 38 | 39 | 40 | -------------------------------------------------------------------------------- /docs/reference/functions/inv.md: -------------------------------------------------------------------------------- 1 | # Function inv 2 | 3 | Calculate the inverse of a square matrix. 4 | 5 | 6 | ## Syntax 7 | 8 | ```js 9 | math.inv(x) 10 | ``` 11 | 12 | ### Parameters 13 | 14 | Parameter | Type | Description 15 | --------- | ---- | ----------- 16 | `x` | Number | Complex | Array | Matrix | Matrix to be inversed 17 | 18 | ### Returns 19 | 20 | Type | Description 21 | ---- | ----------- 22 | Number | Complex | Array | Matrix | The inverse of `x`. 23 | 24 | 25 | ## Examples 26 | 27 | ```js 28 | math.inv([[1, 2], [3, 4]]); // returns [[-2, 1], [1.5, -0.5]] 29 | math.inv(4); // returns 0.25 30 | 1 / 4; // returns 0.25 31 | ``` 32 | 33 | 34 | ## See also 35 | 36 | [det](det.md), 37 | [transpose](transpose.md) 38 | 39 | 40 | 41 | -------------------------------------------------------------------------------- /docs/reference/functions/forEach.md: -------------------------------------------------------------------------------- 1 | # Function forEach 2 | 3 | Iterate over all elements of a matrix/array, and executes the given callback function. 4 | 5 | 6 | ## Syntax 7 | 8 | ```js 9 | math.forEach(x, callback) 10 | ``` 11 | 12 | ### Parameters 13 | 14 | Parameter | Type | Description 15 | --------- | ---- | ----------- 16 | `x` | Matrix | Array | The matrix to iterate on. 17 | `callback` | Function | The callback function is invoked with three parameters: the value of the element, the index of the element, and the Matrix/array being traversed. 18 | 19 | ## Examples 20 | 21 | ```js 22 | math.forEach([1, 2, 3], function(value) { 23 | console.log(value); 24 | }); 25 | // outputs 1, 2, 3 26 | ``` 27 | 28 | 29 | ## See also 30 | 31 | [filter](filter.md), 32 | [map](map.md), 33 | [sort](sort.md) 34 | 35 | 36 | 37 | -------------------------------------------------------------------------------- /docs/expressions/index.md: -------------------------------------------------------------------------------- 1 | # Expressions 2 | 3 | Math.js contains a flexible and easy to use expression parser. 4 | The parser supports all data types, functions and constants available in math.js. 5 | 6 | Whilst the math.js library is aimed at JavaScript developers, the expression 7 | parser is aimed at end users: mathematicians, engineers, students, pupils. 8 | The syntax of the expression parser differs from JavaScript and the low level 9 | math.js library. 10 | 11 | This section is divided in the following pages: 12 | 13 | - [Parsing and evaluation](parsing.md) describes how to parse and 14 | evaluate expressions with math.js. 15 | - [Syntax](syntax.md) describes how to write expressions. 16 | - [Expression trees](expression_trees.md) explains how to parse an expression into an 17 | expression tree, and use this to analyse and manipulate the expression. 18 | - [Customization](customization.md) describes how to customize processing and 19 | evaluation of expressions. 20 | -------------------------------------------------------------------------------- /lib/function/utils/clone.js: -------------------------------------------------------------------------------- 1 | 'use strict'; 2 | 3 | module.exports = function (math) { 4 | var util = require('../../util/index'), 5 | object = util.object; 6 | 7 | /** 8 | * Clone an object. 9 | * 10 | * Syntax: 11 | * 12 | * math.clone(x) 13 | * 14 | * Examples: 15 | * 16 | * math.clone(3.5); // returns number 3.5 17 | * math.clone(2 - 4i); // returns Complex 2 - 4i 18 | * math.clone(45 deg); // returns Unit 45 deg 19 | * math.clone([[1, 2], [3, 4]]); // returns Array [[1, 2], [3, 4]] 20 | * math.clone("hello world"); // returns string "hello world" 21 | * 22 | * @param {*} x Object to be cloned 23 | * @return {*} A clone of object x 24 | */ 25 | math.clone = function clone (x) { 26 | if (arguments.length != 1) { 27 | throw new math.error.ArgumentsError('clone', arguments.length, 1); 28 | } 29 | 30 | return object.clone(x); 31 | }; 32 | }; 33 | -------------------------------------------------------------------------------- /docs/reference/functions/transpose.md: -------------------------------------------------------------------------------- 1 | # Function transpose 2 | 3 | Transpose a matrix. All values of the matrix are reflected over its 4 | main diagonal. Only two dimensional matrices are supported. 5 | 6 | 7 | ## Syntax 8 | 9 | ```js 10 | math.transpose(x) 11 | ``` 12 | 13 | ### Parameters 14 | 15 | Parameter | Type | Description 16 | --------- | ---- | ----------- 17 | `x` | Array | Matrix | Matrix to be transposed 18 | 19 | ### Returns 20 | 21 | Type | Description 22 | ---- | ----------- 23 | Array | Matrix | The transposed matrix 24 | 25 | 26 | ## Examples 27 | 28 | ```js 29 | var A = [[1, 2, 3], [4, 5, 6]]; 30 | math.transpose(A); // returns [[1, 4], [2, 5], [3, 6]] 31 | ``` 32 | 33 | 34 | ## See also 35 | 36 | [diag](diag.md), 37 | [inv](inv.md), 38 | [subset](subset.md), 39 | [squeeze](squeeze.md) 40 | 41 | 42 | 43 | -------------------------------------------------------------------------------- /docs/reference/functions/csc.md: -------------------------------------------------------------------------------- 1 | # Function csc 2 | 3 | Calculate the cosecant of a value, defined as `csc(x) = 1/sin(x)`. 4 | 5 | For matrices, the function is evaluated element wise. 6 | 7 | 8 | ## Syntax 9 | 10 | ```js 11 | math.csc(x) 12 | ``` 13 | 14 | ### Parameters 15 | 16 | Parameter | Type | Description 17 | --------- | ---- | ----------- 18 | `x` | Number | Boolean | Complex | Unit | Array | Matrix | null | Function input 19 | 20 | ### Returns 21 | 22 | Type | Description 23 | ---- | ----------- 24 | Number | Complex | Array | Matrix | Cosecant of x 25 | 26 | 27 | ## Examples 28 | 29 | ```js 30 | math.csc(2); // returns Number 1.099750170294617 31 | 1 / math.sin(2); // returns Number 1.099750170294617 32 | ``` 33 | 34 | 35 | ## See also 36 | 37 | [sin](sin.md), 38 | [sec](sec.md), 39 | [cot](cot.md) 40 | 41 | 42 | 43 | -------------------------------------------------------------------------------- /docs/reference/functions/sec.md: -------------------------------------------------------------------------------- 1 | # Function sec 2 | 3 | Calculate the secant of a value, defined as `sec(x) = 1/cos(x)`. 4 | 5 | For matrices, the function is evaluated element wise. 6 | 7 | 8 | ## Syntax 9 | 10 | ```js 11 | math.sec(x) 12 | ``` 13 | 14 | ### Parameters 15 | 16 | Parameter | Type | Description 17 | --------- | ---- | ----------- 18 | `x` | Number | Boolean | Complex | Unit | Array | Matrix | null | Function input 19 | 20 | ### Returns 21 | 22 | Type | Description 23 | ---- | ----------- 24 | Number | Complex | Array | Matrix | Secant of x 25 | 26 | 27 | ## Examples 28 | 29 | ```js 30 | math.sec(2); // returns Number -2.4029979617223822 31 | 1 / math.cos(2); // returns Number -2.4029979617223822 32 | ``` 33 | 34 | 35 | ## See also 36 | 37 | [cos](cos.md), 38 | [csc](csc.md), 39 | [cot](cot.md) 40 | 41 | 42 | 43 | -------------------------------------------------------------------------------- /docs/reference/functions/cosh.md: -------------------------------------------------------------------------------- 1 | # Function cosh 2 | 3 | Calculate the hyperbolic cosine of a value, 4 | defined as `cosh(x) = 1/2 * (exp(x) + exp(-x))`. 5 | 6 | For matrices, the function is evaluated element wise. 7 | 8 | 9 | ## Syntax 10 | 11 | ```js 12 | math.cosh(x) 13 | ``` 14 | 15 | ### Parameters 16 | 17 | Parameter | Type | Description 18 | --------- | ---- | ----------- 19 | `x` | Number | BigNumber | Boolean | Complex | Unit | Array | Matrix | null | Function input 20 | 21 | ### Returns 22 | 23 | Type | Description 24 | ---- | ----------- 25 | Number | BigNumber | Complex | Array | Matrix | Hyperbolic cosine of x 26 | 27 | 28 | ## Examples 29 | 30 | ```js 31 | math.cosh(0.5); // returns Number 1.1276259652063807 32 | ``` 33 | 34 | 35 | ## See also 36 | 37 | [sinh](sinh.md), 38 | [tanh](tanh.md) 39 | 40 | 41 | 42 | -------------------------------------------------------------------------------- /docs/reference/functions/sinh.md: -------------------------------------------------------------------------------- 1 | # Function sinh 2 | 3 | Calculate the hyperbolic sine of a value, 4 | defined as `sinh(x) = 1/2 * (exp(x) - exp(-x))`. 5 | 6 | For matrices, the function is evaluated element wise. 7 | 8 | 9 | ## Syntax 10 | 11 | ```js 12 | math.sinh(x) 13 | ``` 14 | 15 | ### Parameters 16 | 17 | Parameter | Type | Description 18 | --------- | ---- | ----------- 19 | `x` | Number | BigNumber | Boolean | Complex | Unit | Array | Matrix | null | Function input 20 | 21 | ### Returns 22 | 23 | Type | Description 24 | ---- | ----------- 25 | Number | BigNumber | Complex | Array | Matrix | Hyperbolic sine of x 26 | 27 | 28 | ## Examples 29 | 30 | ```js 31 | math.sinh(0.5); // returns Number 0.5210953054937474 32 | ``` 33 | 34 | 35 | ## See also 36 | 37 | [cosh](cosh.md), 38 | [tanh](tanh.md) 39 | 40 | 41 | 42 | -------------------------------------------------------------------------------- /ROADMAP.md: -------------------------------------------------------------------------------- 1 | # Roadmap 2 | 3 | A rough roadmap for math.js. 4 | 5 | 6 | ## Version 1.x 7 | 8 | - Support for derived units (like `km/h`, `kg*m/s2`, etc). 9 | - Improve performance. Rewrite `Matrix` to support typed arrays. 10 | - Change to a modular architecture, split the library into separate modules 11 | like `mathjs-core`, `mathjs-expression`, `mathjs-unit`, `mathjs-matrix`, 12 | `mathjs-complex`, `mathjs-bignumber`, and maybe separate modules 13 | `mathjs-statistics`, `mathjs-numeric`, etc. 14 | - Support for fractions. 15 | - Functions and data types for numeral systems: Bin, Oct, Hex, Dec. 16 | - BigNumber support for all functions and constants (for example trigonometric 17 | functions still miss BigNumber support). 18 | - Full scripting capabilities for the expression parser (for and while loops, 19 | function blocks, etc). 20 | - Implement a more broad set of common functions covering all common 21 | mathematical areas. 22 | 23 | 24 | ## Version 2.x 25 | 26 | - Support for symbolic algebra. 27 | -------------------------------------------------------------------------------- /test/function/construction/index.test.js: -------------------------------------------------------------------------------- 1 | // test index construction 2 | var assert = require('assert'), 3 | error = require('../../../lib/error/index'), 4 | math = require('../../../index'); 5 | 6 | describe('index', function() { 7 | 8 | it('should create an index', function() { 9 | var index = math.index([2,6]); 10 | assert.ok(index instanceof math.type.Index); 11 | assert.deepEqual(index._ranges, [{start:2, end:6, step:1}]); 12 | 13 | var index2 = math.index([0,4], [5,2,-1]); 14 | assert.ok(index2 instanceof math.type.Index); 15 | assert.deepEqual(index2._ranges, [{start:0, end:4, step: 1}, {start:5, end: 2, step:-1}]); 16 | }); 17 | 18 | it('should create an index from bignumbers (downgrades to numbers)', function() { 19 | var index = math.index([math.bignumber(2), math.bignumber(6)], math.bignumber(3)); 20 | assert.ok(index instanceof math.type.Index); 21 | assert.deepEqual(index._ranges, [{start:2, end:6, step:1}, {start: 3, end: 4, step: 1}]); 22 | }); 23 | 24 | }); -------------------------------------------------------------------------------- /docs/reference/functions/cot.md: -------------------------------------------------------------------------------- 1 | # Function cot 2 | 3 | Calculate the cotangent of a value. `cot(x)` is defined as `1 / tan(x)`. 4 | 5 | For matrices, the function is evaluated element wise. 6 | 7 | 8 | ## Syntax 9 | 10 | ```js 11 | math.cot(x) 12 | ``` 13 | 14 | ### Parameters 15 | 16 | Parameter | Type | Description 17 | --------- | ---- | ----------- 18 | `x` | Number | Boolean | Complex | Unit | Array | Matrix | null | Function input 19 | 20 | ### Returns 21 | 22 | Type | Description 23 | ---- | ----------- 24 | Number | Complex | Array | Matrix | Cotangent of x 25 | 26 | 27 | ## Examples 28 | 29 | ```js 30 | math.cot(2); // returns Number -0.45765755436028577 31 | 1 / math.tan(2); // returns Number -0.45765755436028577 32 | ``` 33 | 34 | 35 | ## See also 36 | 37 | [tan](tan.md), 38 | [sec](sec.md), 39 | [csc](csc.md) 40 | 41 | 42 | 43 | -------------------------------------------------------------------------------- /lib/header.js: -------------------------------------------------------------------------------- 1 | /** 2 | * math.js 3 | * https://github.com/josdejong/mathjs 4 | * 5 | * Math.js is an extensive math library for JavaScript and Node.js, 6 | * It features real and complex numbers, units, matrices, a large set of 7 | * mathematical functions, and a flexible expression parser. 8 | * 9 | * @version @@version 10 | * @date @@date 11 | * 12 | * @license 13 | * Copyright (C) 2013-2015 Jos de Jong 14 | * 15 | * Licensed under the Apache License, Version 2.0 (the "License"); you may not 16 | * use this file except in compliance with the License. You may obtain a copy 17 | * of the License at 18 | * 19 | * http://www.apache.org/licenses/LICENSE-2.0 20 | * 21 | * Unless required by applicable law or agreed to in writing, software 22 | * distributed under the License is distributed on an "AS IS" BASIS, WITHOUT 23 | * WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. See the 24 | * License for the specific language governing permissions and limitations under 25 | * the License. 26 | */ 27 | -------------------------------------------------------------------------------- /docs/reference/functions/dot.md: -------------------------------------------------------------------------------- 1 | # Function dot 2 | 3 | Calculate the dot product of two vectors. The dot product of 4 | `A = [a1, a2, a3, ..., an]` and `B = [b1, b2, b3, ..., bn]` is defined as: 5 | 6 | dot(A, B) = a1 * b1 + a2 * b2 + a3 * b3 + ... + an * bn 7 | 8 | 9 | ## Syntax 10 | 11 | ```js 12 | math.dot(x, y) 13 | ``` 14 | 15 | ### Parameters 16 | 17 | Parameter | Type | Description 18 | --------- | ---- | ----------- 19 | `x` | Array | Matrix | First vector 20 | `y` | Array | Matrix | Second vector 21 | 22 | ### Returns 23 | 24 | Type | Description 25 | ---- | ----------- 26 | Number | Returns the dot product of `x` and `y` 27 | 28 | 29 | ## Examples 30 | 31 | ```js 32 | math.dot([2, 4, 1], [2, 2, 3]); // returns Number 15 33 | math.multiply([2, 4, 1], [2, 2, 3]); // returns Number 15 34 | ``` 35 | 36 | 37 | ## See also 38 | 39 | [multiply](multiply.md), 40 | [cross](cross.md) 41 | 42 | 43 | 44 | -------------------------------------------------------------------------------- /docs/reference/functions/combinations.md: -------------------------------------------------------------------------------- 1 | # Function combinations 2 | 3 | Compute the number of ways of picking `k` unordered outcomes from `n` 4 | possibilities. 5 | 6 | Combinations only takes integer arguments. 7 | The following condition must be enforced: k <= n. 8 | 9 | 10 | ## Syntax 11 | 12 | ```js 13 | math.combinations(n, k) 14 | ``` 15 | 16 | ### Parameters 17 | 18 | Parameter | Type | Description 19 | --------- | ---- | ----------- 20 | `n` | Number | BigNumber | Total number of objects in the set 21 | `k` | Number | BigNumber | Number of objects in the subset 22 | 23 | ### Returns 24 | 25 | Type | Description 26 | ---- | ----------- 27 | Number | BigNumber | Number of possible combinations. 28 | 29 | 30 | ## Examples 31 | 32 | ```js 33 | math.combinations(7, 5); // returns 21 34 | ``` 35 | 36 | 37 | ## See also 38 | 39 | [permutations](permutations.md), 40 | [factorial](factorial.md) 41 | 42 | 43 | 44 | -------------------------------------------------------------------------------- /docs/reference/functions/factorial.md: -------------------------------------------------------------------------------- 1 | # Function factorial 2 | 3 | Compute the factorial of a value 4 | 5 | Factorial only supports an integer value as argument. 6 | For matrices, the function is evaluated element wise. 7 | 8 | 9 | ## Syntax 10 | 11 | ```js 12 | math.factorial(n) 13 | ``` 14 | 15 | ### Parameters 16 | 17 | Parameter | Type | Description 18 | --------- | ---- | ----------- 19 | `n` | Number | BigNumber | Array | Matrix | Boolean | null | An integer number 20 | 21 | ### Returns 22 | 23 | Type | Description 24 | ---- | ----------- 25 | Number | BigNumber | Array | Matrix | The factorial of `n` 26 | 27 | 28 | ## Examples 29 | 30 | ```js 31 | math.factorial(5); // returns 120 32 | math.factorial(3); // returns 6 33 | ``` 34 | 35 | 36 | ## See also 37 | 38 | [combinations](combinations.md), 39 | [gamma](gamma.md), 40 | [permutations](permutations.md) 41 | 42 | 43 | 44 | -------------------------------------------------------------------------------- /docs/reference/functions/size.md: -------------------------------------------------------------------------------- 1 | # Function size 2 | 3 | Calculate the size of a matrix or scalar. 4 | 5 | 6 | ## Syntax 7 | 8 | ```js 9 | math.size(x) 10 | ``` 11 | 12 | ### Parameters 13 | 14 | Parameter | Type | Description 15 | --------- | ---- | ----------- 16 | `x` | Boolean | Number | Complex | Unit | String | Array | Matrix | A matrix 17 | 18 | ### Returns 19 | 20 | Type | Description 21 | ---- | ----------- 22 | Array | Matrix | A vector with size of `x`. 23 | 24 | 25 | ## Examples 26 | 27 | ```js 28 | math.size(2.3); // returns [] 29 | math.size('hello world'); // returns [11] 30 | 31 | var A = [[1, 2, 3], [4, 5, 6]]; 32 | math.size(A); // returns [2, 3] 33 | math.size(math.range(1,6)); // returns [5] 34 | ``` 35 | 36 | 37 | ## See also 38 | 39 | [resize](resize.md), 40 | [squeeze](squeeze.md), 41 | [subset](subset.md) 42 | 43 | 44 | 45 | -------------------------------------------------------------------------------- /docs/reference/functions/sech.md: -------------------------------------------------------------------------------- 1 | # Function sech 2 | 3 | Calculate the hyperbolic secant of a value, 4 | defined as `sech(x) = 1 / cosh(x)`. 5 | 6 | For matrices, the function is evaluated element wise. 7 | 8 | 9 | ## Syntax 10 | 11 | ```js 12 | math.sech(x) 13 | ``` 14 | 15 | ### Parameters 16 | 17 | Parameter | Type | Description 18 | --------- | ---- | ----------- 19 | `x` | Number | Boolean | Complex | Unit | Array | Matrix | null | Function input 20 | 21 | ### Returns 22 | 23 | Type | Description 24 | ---- | ----------- 25 | Number | Complex | Array | Matrix | Hyperbolic secant of x 26 | 27 | 28 | ## Examples 29 | 30 | ```js 31 | // sech(x) = 1/ cosh(x) 32 | math.sech(0.5); // returns 0.886818883970074 33 | 1 / math.cosh(0.5); // returns 0.886818883970074 34 | ``` 35 | 36 | 37 | ## See also 38 | 39 | [cosh](cosh.md), 40 | [csch](csch.md), 41 | [coth](coth.md) 42 | 43 | 44 | 45 | -------------------------------------------------------------------------------- /docs/reference/functions/csch.md: -------------------------------------------------------------------------------- 1 | # Function csch 2 | 3 | Calculate the hyperbolic cosecant of a value, 4 | defined as `csch(x) = 1 / sinh(x)`. 5 | 6 | For matrices, the function is evaluated element wise. 7 | 8 | 9 | ## Syntax 10 | 11 | ```js 12 | math.csch(x) 13 | ``` 14 | 15 | ### Parameters 16 | 17 | Parameter | Type | Description 18 | --------- | ---- | ----------- 19 | `x` | Number | Boolean | Complex | Unit | Array | Matrix | null | Function input 20 | 21 | ### Returns 22 | 23 | Type | Description 24 | ---- | ----------- 25 | Number | Complex | Array | Matrix | Hyperbolic cosecant of x 26 | 27 | 28 | ## Examples 29 | 30 | ```js 31 | // csch(x) = 1/ sinh(x) 32 | math.csch(0.5); // returns 1.9190347513349437 33 | 1 / math.sinh(0.5); // returns 1.9190347513349437 34 | ``` 35 | 36 | 37 | ## See also 38 | 39 | [sinh](sinh.md), 40 | [sech](sech.md), 41 | [coth](coth.md) 42 | 43 | 44 | 45 | -------------------------------------------------------------------------------- /docs/reference/functions/coth.md: -------------------------------------------------------------------------------- 1 | # Function coth 2 | 3 | Calculate the hyperbolic cotangent of a value, 4 | defined as `coth(x) = 1 / tanh(x)`. 5 | 6 | For matrices, the function is evaluated element wise. 7 | 8 | 9 | ## Syntax 10 | 11 | ```js 12 | math.coth(x) 13 | ``` 14 | 15 | ### Parameters 16 | 17 | Parameter | Type | Description 18 | --------- | ---- | ----------- 19 | `x` | Number | Boolean | Complex | Unit | Array | Matrix | null | Function input 20 | 21 | ### Returns 22 | 23 | Type | Description 24 | ---- | ----------- 25 | Number | Complex | Array | Matrix | Hyperbolic cotangent of x 26 | 27 | 28 | ## Examples 29 | 30 | ```js 31 | // coth(x) = 1 / tanh(x) 32 | math.coth(2); // returns 1.0373147207275482 33 | 1 / math.tanh(2); // returns 1.0373147207275482 34 | ``` 35 | 36 | 37 | ## See also 38 | 39 | [sinh](sinh.md), 40 | [tanh](tanh.md), 41 | [cosh](cosh.md) 42 | 43 | 44 | 45 | -------------------------------------------------------------------------------- /docs/reference/functions/abs.md: -------------------------------------------------------------------------------- 1 | # Function abs 2 | 3 | Calculate the absolute value of a number. For matrices, the function is 4 | evaluated element wise. 5 | 6 | 7 | ## Syntax 8 | 9 | ```js 10 | math.abs(x) 11 | ``` 12 | 13 | ### Parameters 14 | 15 | Parameter | Type | Description 16 | --------- | ---- | ----------- 17 | `x` | Number | BigNumber | Boolean | Complex | Array | Matrix | null | A number or matrix for which to get the absolute value 18 | 19 | ### Returns 20 | 21 | Type | Description 22 | ---- | ----------- 23 | Number | BigNumber | Complex | Array | Matrix | Absolute value of `x` 24 | 25 | 26 | ## Examples 27 | 28 | ```js 29 | math.abs(3.5); // returns Number 3.5 30 | math.abs(-4.2); // returns Number 4.2 31 | 32 | math.abs([3, -5, -1, 0, 2]); // returns Array [3, 5, 1, 0, 2] 33 | ``` 34 | 35 | 36 | ## See also 37 | 38 | [sign](sign.md) 39 | 40 | 41 | 42 | -------------------------------------------------------------------------------- /docs/reference/functions/sqrt.md: -------------------------------------------------------------------------------- 1 | # Function sqrt 2 | 3 | Calculate the square root of a value. 4 | 5 | For matrices, the function is evaluated element wise. 6 | 7 | 8 | ## Syntax 9 | 10 | ```js 11 | math.sqrt(x) 12 | ``` 13 | 14 | ### Parameters 15 | 16 | Parameter | Type | Description 17 | --------- | ---- | ----------- 18 | `x` | Number | BigNumber | Boolean | Complex | Array | Matrix | null | Value for which to calculate the square root. 19 | 20 | ### Returns 21 | 22 | Type | Description 23 | ---- | ----------- 24 | Number | BigNumber | Complex | Array | Matrix | Returns the square root of `x` 25 | 26 | 27 | ## Examples 28 | 29 | ```js 30 | math.sqrt(25); // returns 5 31 | math.square(5); // returns 25 32 | math.sqrt(-4); // returns Complex -2i 33 | ``` 34 | 35 | 36 | ## See also 37 | 38 | [square](square.md), 39 | [multiply](multiply.md) 40 | 41 | 42 | 43 | -------------------------------------------------------------------------------- /examples/browser/basic_usage.html: -------------------------------------------------------------------------------- 1 | 2 | 3 | 4 | math.js | basic usage 5 | 6 | 7 | 8 | 9 | 35 | 36 | 37 | -------------------------------------------------------------------------------- /docs/reference/functions/acos.md: -------------------------------------------------------------------------------- 1 | # Function acos 2 | 3 | Calculate the inverse cosine of a value. 4 | 5 | For matrices, the function is evaluated element wise. 6 | 7 | 8 | ## Syntax 9 | 10 | ```js 11 | math.acos(x) 12 | ``` 13 | 14 | ### Parameters 15 | 16 | Parameter | Type | Description 17 | --------- | ---- | ----------- 18 | `x` | Number | BigNumber Boolean | Complex | Array | Matrix | null | Function input 19 | 20 | ### Returns 21 | 22 | Type | Description 23 | ---- | ----------- 24 | Number | BigNumber | Complex | Array | Matrix | The arc cosine of x 25 | 26 | 27 | ## Examples 28 | 29 | ```js 30 | math.acos(0.5); // returns Number 1.0471975511965979 31 | math.acos(math.cos(1.5)); // returns Number 1.5 32 | 33 | math.acos(2); // returns Complex 0 + 1.3169578969248166 i 34 | ``` 35 | 36 | 37 | ## See also 38 | 39 | [cos](cos.md), 40 | [atan](atan.md), 41 | [asin](asin.md) 42 | 43 | 44 | 45 | -------------------------------------------------------------------------------- /docs/reference/functions/map.md: -------------------------------------------------------------------------------- 1 | # Function map 2 | 3 | Create a new matrix or array with the results of the callback function executed on 4 | each entry of the matrix/array. 5 | 6 | 7 | ## Syntax 8 | 9 | ```js 10 | math.map(x, callback) 11 | ``` 12 | 13 | ### Parameters 14 | 15 | Parameter | Type | Description 16 | --------- | ---- | ----------- 17 | `x` | Matrix | Array | The matrix to iterate on. 18 | `callback` | Function | The callback method is invoked with three parameters: the value of the element, the index of the element, and the matrix being traversed. 19 | 20 | ### Returns 21 | 22 | Type | Description 23 | ---- | ----------- 24 | Matrix | array | Transformed map of x 25 | 26 | 27 | ## Examples 28 | 29 | ```js 30 | math.map([1, 2, 3], function(value) { 31 | return value * value; 32 | }); // returns [1, 4, 9] 33 | ``` 34 | 35 | 36 | ## See also 37 | 38 | [filter](filter.md), 39 | [forEach](forEach.md), 40 | [sort](sort.md) 41 | 42 | 43 | 44 | -------------------------------------------------------------------------------- /docs/reference/functions/sum.md: -------------------------------------------------------------------------------- 1 | # Function sum 2 | 3 | Compute the sum of a matrix or a list with values. 4 | In case of a (multi dimensional) array or matrix, the sum of all 5 | elements will be calculated. 6 | 7 | 8 | ## Syntax 9 | 10 | ```js 11 | math.sum(a, b, c, ...) 12 | math.sum(A) 13 | ``` 14 | 15 | ### Parameters 16 | 17 | Parameter | Type | Description 18 | --------- | ---- | ----------- 19 | `args` | ... * | A single matrix or or multiple scalar values 20 | 21 | ### Returns 22 | 23 | Type | Description 24 | ---- | ----------- 25 | * | The sum of all values 26 | 27 | 28 | ## Examples 29 | 30 | ```js 31 | math.sum(2, 1, 4, 3); // returns 10 32 | math.sum([2, 1, 4, 3]); // returns 10 33 | math.sum([[2, 5], [4, 3], [1, 7]]); // returns 22 34 | ``` 35 | 36 | 37 | ## See also 38 | 39 | [mean](mean.md), 40 | [median](median.md), 41 | [min](min.md), 42 | [max](max.md), 43 | [prod](prod.md), 44 | [std](std.md), 45 | [var](var.md) 46 | 47 | 48 | 49 | -------------------------------------------------------------------------------- /docs/reference/functions/xgcd.md: -------------------------------------------------------------------------------- 1 | # Function xgcd 2 | 3 | Calculate the extended greatest common divisor for two values. 4 | See http://en.wikipedia.org/wiki/Extended_Euclidean_algorithm. 5 | 6 | 7 | ## Syntax 8 | 9 | ```js 10 | math.xgcd(a, b) 11 | ``` 12 | 13 | ### Parameters 14 | 15 | Parameter | Type | Description 16 | --------- | ---- | ----------- 17 | `a` | Number | BigNumber | Boolean | An integer number 18 | `b` | Number | BigNumber | Boolean | An integer number 19 | 20 | ### Returns 21 | 22 | Type | Description 23 | ---- | ----------- 24 | Array | Returns an array containing 3 integers `[div, m, n]` where `div = gcd(a, b)` and `a*m + b*n = div` 25 | 26 | 27 | ## Examples 28 | 29 | ```js 30 | math.xgcd(8, 12); // returns [4, -1, 1] 31 | math.gcd(8, 12); // returns 4 32 | math.xgcd(36163, 21199); // returns [1247, -7, 12] 33 | ``` 34 | 35 | 36 | ## See also 37 | 38 | [gcd](gcd.md), 39 | [lcm](lcm.md) 40 | 41 | 42 | 43 | -------------------------------------------------------------------------------- /docs/reference/functions/asin.md: -------------------------------------------------------------------------------- 1 | # Function asin 2 | 3 | Calculate the inverse sine of a value. 4 | 5 | For matrices, the function is evaluated element wise. 6 | 7 | 8 | ## Syntax 9 | 10 | ```js 11 | math.asin(x) 12 | ``` 13 | 14 | ### Parameters 15 | 16 | Parameter | Type | Description 17 | --------- | ---- | ----------- 18 | `x` | Number | BigNumber | Boolean | Complex | Array | Matrix | null | Function input 19 | 20 | ### Returns 21 | 22 | Type | Description 23 | ---- | ----------- 24 | Number | BigNumber | Complex | Array | Matrix | The arc sine of x 25 | 26 | 27 | ## Examples 28 | 29 | ```js 30 | math.asin(0.5); // returns Number 0.5235987755982989 31 | math.asin(math.sin(1.5)); // returns Number ~1.5 32 | 33 | math.asin(2); // returns Complex 1.5707963267948966 -1.3169578969248166 i 34 | ``` 35 | 36 | 37 | ## See also 38 | 39 | [sin](sin.md), 40 | [atan](atan.md), 41 | [acos](acos.md) 42 | 43 | 44 | 45 | -------------------------------------------------------------------------------- /docs/reference/functions/atan.md: -------------------------------------------------------------------------------- 1 | # Function atan 2 | 3 | Calculate the inverse tangent of a value. 4 | 5 | For matrices, the function is evaluated element wise. 6 | 7 | 8 | ## Syntax 9 | 10 | ```js 11 | math.atan(x) 12 | ``` 13 | 14 | ### Parameters 15 | 16 | Parameter | Type | Description 17 | --------- | ---- | ----------- 18 | `x` | Number | BigNumber | Boolean | Complex | Array | Matrix | null | Function input 19 | 20 | ### Returns 21 | 22 | Type | Description 23 | ---- | ----------- 24 | Number | BigNumber | Complex | Array | Matrix | The arc tangent of x 25 | 26 | 27 | ## Examples 28 | 29 | ```js 30 | math.atan(0.5); // returns Number 0.4636476090008061 31 | math.atan(math.tan(1.5)); // returns Number 1.5 32 | 33 | math.atan(2); // returns Complex 1.5707963267948966 -1.3169578969248166 i 34 | ``` 35 | 36 | 37 | ## See also 38 | 39 | [tan](tan.md), 40 | [asin](asin.md), 41 | [acos](acos.md) 42 | 43 | 44 | 45 | -------------------------------------------------------------------------------- /docs/reference/functions/not.md: -------------------------------------------------------------------------------- 1 | # Function not 2 | 3 | Logical `not`. Flips boolean value of a given parameter. 4 | For matrices, the function is evaluated element wise. 5 | 6 | 7 | ## Syntax 8 | 9 | ```js 10 | math.not(x) 11 | ``` 12 | 13 | ### Parameters 14 | 15 | Parameter | Type | Description 16 | --------- | ---- | ----------- 17 | `x` | Number | BigNumber | Boolean | Complex | Unit | Array | Matrix | null | First value to check 18 | 19 | ### Returns 20 | 21 | Type | Description 22 | ---- | ----------- 23 | Boolean | Array | Matrix | Returns true when input is a zero or empty value. 24 | 25 | 26 | ## Examples 27 | 28 | ```js 29 | math.not(2); // returns false 30 | math.not(0); // returns true 31 | math.not(true); // returns false 32 | 33 | a = [2, -7, 0]; 34 | math.not(a); // returns [false, false, true] 35 | ``` 36 | 37 | 38 | ## See also 39 | 40 | [and](and.md), 41 | [or](or.md), 42 | [xor](xor.md) 43 | 44 | 45 | 46 | -------------------------------------------------------------------------------- /docs/reference/functions/permutations.md: -------------------------------------------------------------------------------- 1 | # Function permutations 2 | 3 | Compute the number of ways of obtaining an ordered subset of `k` elements 4 | from a set of `n` elements. 5 | 6 | Permutations only takes integer arguments. 7 | The following condition must be enforced: k <= n. 8 | 9 | 10 | ## Syntax 11 | 12 | ```js 13 | math.permutations(n) 14 | math.permutations(n, k) 15 | ``` 16 | 17 | ### Parameters 18 | 19 | Parameter | Type | Description 20 | --------- | ---- | ----------- 21 | `n` | Number | BigNumber | The number of objects in total 22 | `k` | Number | BigNumber | The number of objects in the subset 23 | 24 | ### Returns 25 | 26 | Type | Description 27 | ---- | ----------- 28 | Number | BigNumber | The number of permutations 29 | 30 | 31 | ## Examples 32 | 33 | ```js 34 | math.permutations(5); // 120 35 | math.permutations(5, 3); // 60 36 | ``` 37 | 38 | 39 | ## See also 40 | 41 | [combinations](combinations.md), 42 | [factorial](factorial.md) 43 | 44 | 45 | 46 | -------------------------------------------------------------------------------- /docs/reference/functions/distribution.md: -------------------------------------------------------------------------------- 1 | # Function distribution 2 | 3 | Create a distribution object with a set of random functions for given 4 | random distribution. 5 | 6 | 7 | ## Syntax 8 | 9 | ```js 10 | math.distribution(name) 11 | ``` 12 | 13 | ### Parameters 14 | 15 | Parameter | Type | Description 16 | --------- | ---- | ----------- 17 | `name` | String | Name of a distribution. Choose from 'uniform', 'normal'. 18 | 19 | ### Returns 20 | 21 | Type | Description 22 | ---- | ----------- 23 | Object | Returns a distribution object containing functions: `random([size] [, min] [, max])`, `randomInt([min] [, max])`, `pickRandom(array)` 24 | 25 | 26 | ## Examples 27 | 28 | ```js 29 | var normalDist = math.distribution('normal'); // create a normal distribution 30 | normalDist.random(0, 10); // get a random value between 0 and 10 31 | ``` 32 | 33 | 34 | ## See also 35 | 36 | [random](random.md), 37 | [randomInt](randomInt.md), 38 | [pickRandom](pickRandom.md) 39 | 40 | 41 | 42 | -------------------------------------------------------------------------------- /docs/reference/functions/to.md: -------------------------------------------------------------------------------- 1 | # Function to 2 | 3 | Change the unit of a value. 4 | 5 | For matrices, the function is evaluated element wise. 6 | 7 | 8 | ## Syntax 9 | 10 | ```js 11 | math.to(x, unit) 12 | ``` 13 | 14 | ### Parameters 15 | 16 | Parameter | Type | Description 17 | --------- | ---- | ----------- 18 | `x` | Unit | Array | Matrix | The unit to be converted. 19 | `unit` | Unit | Array | Matrix | New unit. Can be a string like "cm" or a unit without value. 20 | 21 | ### Returns 22 | 23 | Type | Description 24 | ---- | ----------- 25 | Unit | Array | Matrix | value with changed, fixed unit. 26 | 27 | 28 | ## Examples 29 | 30 | ```js 31 | math.to(math.unit('2 inch'), 'cm'); // returns Unit 5.08 cm 32 | math.to(math.unit('2 inch'), math.unit(null, 'cm')); // returns Unit 5.08 cm 33 | math.to(math.unit(16, 'bytes'), 'bits'); // returns Unit 128 bits 34 | ``` 35 | 36 | 37 | ## See also 38 | 39 | [unit](unit.md) 40 | 41 | 42 | 43 | -------------------------------------------------------------------------------- /test/type/ResultSet.test.js: -------------------------------------------------------------------------------- 1 | // test data type ResultSet 2 | 3 | var assert = require('assert'); 4 | var Complex = require('../../lib/type/Complex'); 5 | var ResultSet = require('../../lib/type/ResultSet'); 6 | 7 | describe('ResultSet', function () { 8 | 9 | it('should create a ResultSet without entries', function () { 10 | var r = new ResultSet(); 11 | assert.deepEqual(r, {entries: []}); 12 | }); 13 | 14 | it('should create a ResultSet with entries', function () { 15 | var r = new ResultSet([1,2,3]); 16 | assert.deepEqual(r, {entries: [1,2,3]}); 17 | }); 18 | 19 | it('should throw an error when called without the new operator', function () { 20 | assert.throws(function () {ResultSet([1,2,3]);}); 21 | }); 22 | 23 | it('should return an Array when calling valueOf()', function () { 24 | var r = new ResultSet([1,2,3]); 25 | assert.deepEqual(r.valueOf(), [1,2,3]); 26 | }); 27 | 28 | it('should return a string when calling toString()', function () { 29 | var r = new ResultSet([1,2,3, new Complex(4, 5)]); 30 | assert.deepEqual(r.toString(), '[1, 2, 3, 4 + 5i]'); 31 | }); 32 | 33 | }); -------------------------------------------------------------------------------- /docs/reference/functions/gcd.md: -------------------------------------------------------------------------------- 1 | # Function gcd 2 | 3 | Calculate the greatest common divisor for two or more values or arrays. 4 | 5 | For matrices, the function is evaluated element wise. 6 | 7 | 8 | ## Syntax 9 | 10 | ```js 11 | math.gcd(a, b) 12 | math.gcd(a, b, c, ...) 13 | ``` 14 | 15 | ### Parameters 16 | 17 | Parameter | Type | Description 18 | --------- | ---- | ----------- 19 | `args` | ... Number | BigNumber | Boolean | Array | Matrix | null | Two or more integer numbers 20 | 21 | ### Returns 22 | 23 | Type | Description 24 | ---- | ----------- 25 | Number | BigNumber | Array | Matrix | The greatest common divisor 26 | 27 | 28 | ## Examples 29 | 30 | ```js 31 | math.gcd(8, 12); // returns 4 32 | math.gcd(-4, 6); // returns 2 33 | math.gcd(25, 15, -10); // returns 5 34 | 35 | math.gcd([8, -4], [12, 6]); // returns [4, 2] 36 | ``` 37 | 38 | 39 | ## See also 40 | 41 | [lcm](lcm.md), 42 | [xgcd](xgcd.md) 43 | 44 | 45 | 46 | -------------------------------------------------------------------------------- /docs/reference/functions/bitNot.md: -------------------------------------------------------------------------------- 1 | # Function bitNot 2 | 3 | Bitwise NOT value, `~x`. 4 | For matrices, the function is evaluated element wise. 5 | For units, the function is evaluated on the best prefix base. 6 | 7 | 8 | ## Syntax 9 | 10 | ```js 11 | math.bitNot(x) 12 | ``` 13 | 14 | ### Parameters 15 | 16 | Parameter | Type | Description 17 | --------- | ---- | ----------- 18 | `x` | Number | BigNumber | Boolean | Array | Matrix | null | Value to not 19 | 20 | ### Returns 21 | 22 | Type | Description 23 | ---- | ----------- 24 | Number | BigNumber | Array | Matrix | NOT of `x` 25 | 26 | 27 | ## Examples 28 | 29 | ```js 30 | math.bitNot(1); // returns Number -2 31 | 32 | math.bitNot([2, -3, 4]); // returns Array [-3, 2, 5] 33 | ``` 34 | 35 | 36 | ## See also 37 | 38 | [bitAnd](bitAnd.md), 39 | [bitOr](bitOr.md), 40 | [bitXor](bitXor.md), 41 | [leftShift](leftShift.md), 42 | [rightArithShift](rightArithShift.md), 43 | [rightLogShift](rightLogShift.md) 44 | 45 | 46 | 47 | -------------------------------------------------------------------------------- /docs/reference/functions/gamma.md: -------------------------------------------------------------------------------- 1 | # Function gamma 2 | 3 | Compute the gamma function of a value using Lanczos approximation for 4 | small values, and an extended Stirling approximation for large values. 5 | 6 | For matrices, the function is evaluated element wise. 7 | 8 | 9 | ## Syntax 10 | 11 | ```js 12 | math.gamma(n) 13 | ``` 14 | 15 | ### Parameters 16 | 17 | Parameter | Type | Description 18 | --------- | ---- | ----------- 19 | `n` | Number | Array | Matrix | Boolean | null | An integer number 20 | 21 | ### Returns 22 | 23 | Type | Description 24 | ---- | ----------- 25 | Number | Array | Matrix | The gamma of `n` 26 | 27 | 28 | ## Examples 29 | 30 | ```js 31 | math.gamma(5); // returns 24 32 | math.gamma(-0.5); // returns -3.5449077018110335 33 | math.gamma(math.i); // returns -0.15494982830180973 - 0.49801566811835596i 34 | ``` 35 | 36 | 37 | ## See also 38 | 39 | [combinations](combinations.md), 40 | [factorial](factorial.md), 41 | [permutations](permutations.md) 42 | 43 | 44 | 45 | -------------------------------------------------------------------------------- /docs/reference/functions/log10.md: -------------------------------------------------------------------------------- 1 | # Function log10 2 | 3 | Calculate the 10-base of a value. This is the same as calculating `log(x, 10)`. 4 | 5 | For matrices, the function is evaluated element wise. 6 | 7 | 8 | ## Syntax 9 | 10 | ```js 11 | math.log10(x) 12 | ``` 13 | 14 | ### Parameters 15 | 16 | Parameter | Type | Description 17 | --------- | ---- | ----------- 18 | `x` | Number | BigNumber | Boolean | Complex | Array | Matrix | null | Value for which to calculate the logarithm. 19 | 20 | ### Returns 21 | 22 | Type | Description 23 | ---- | ----------- 24 | Number | BigNumber | Complex | Array | Matrix | Returns the 10-base logarithm of `x` 25 | 26 | 27 | ## Examples 28 | 29 | ```js 30 | math.log10(0.00001); // returns -5 31 | math.log10(10000); // returns 4 32 | math.log(10000) / math.log(10); // returns 4 33 | math.pow(10, 4); // returns 10000 34 | ``` 35 | 36 | 37 | ## See also 38 | 39 | [exp](exp.md), 40 | [log](log.md) 41 | 42 | 43 | 44 | -------------------------------------------------------------------------------- /docs/reference/functions/ones.md: -------------------------------------------------------------------------------- 1 | # Function ones 2 | 3 | Create a matrix filled with ones. The created matrix can have one or 4 | multiple dimensions. 5 | 6 | 7 | ## Syntax 8 | 9 | ```js 10 | math.ones(m) 11 | math.ones(m, n) 12 | math.ones([m, n]) 13 | math.ones([m, n, p, ...]) 14 | ``` 15 | 16 | ### Parameters 17 | 18 | Parameter | Type | Description 19 | --------- | ---- | ----------- 20 | `size` | ...Number | Array | The size of each dimension of the matrix 21 | 22 | ### Returns 23 | 24 | Type | Description 25 | ---- | ----------- 26 | Array | Matrix | Number | A matrix filled with ones 27 | 28 | 29 | ## Examples 30 | 31 | ```js 32 | math.ones(3); // returns [1, 1, 1] 33 | math.ones(3, 2); // returns [[1, 1], [1, 1], [1, 1]] 34 | 35 | var A = [[1, 2, 3], [4, 5, 6]]; 36 | math.zeros(math.size(A)); // returns [[1, 1, 1], [1, 1, 1]] 37 | ``` 38 | 39 | 40 | ## See also 41 | 42 | [zeros](zeros.md), 43 | [eye](eye.md), 44 | [size](size.md), 45 | [range](range.md) 46 | 47 | 48 | 49 | -------------------------------------------------------------------------------- /lib/error/ArgumentsError.js: -------------------------------------------------------------------------------- 1 | 'use strict'; 2 | 3 | /** 4 | * Create a syntax error with the message: 5 | * 'Wrong number of arguments in function ( provided, - expected)' 6 | * @param {String} fn Function name 7 | * @param {Number} count Actual argument count 8 | * @param {Number} min Minimum required argument count 9 | * @param {Number} [max] Maximum required argument count 10 | * @extends Error 11 | */ 12 | function ArgumentsError(fn, count, min, max) { 13 | if (!(this instanceof ArgumentsError)) { 14 | throw new SyntaxError('Constructor must be called with the new operator'); 15 | } 16 | 17 | this.fn = fn; 18 | this.count = count; 19 | this.min = min; 20 | this.max = max; 21 | 22 | this.message = 'Wrong number of arguments in function ' + fn + 23 | ' (' + count + ' provided, ' + 24 | min + ((max != undefined) ? ('-' + max) : '') + ' expected)'; 25 | 26 | this.stack = (new Error()).stack; 27 | } 28 | 29 | ArgumentsError.prototype = new Error(); 30 | ArgumentsError.prototype.constructor = Error; 31 | ArgumentsError.prototype.name = 'ArgumentsError'; 32 | 33 | module.exports = ArgumentsError; 34 | -------------------------------------------------------------------------------- /docs/reference/functions/eval.md: -------------------------------------------------------------------------------- 1 | # Function eval 2 | 3 | Evaluate an expression. 4 | 5 | 6 | ## Syntax 7 | 8 | ```js 9 | math.eval(expr) 10 | math.eval(expr, scope) 11 | math.eval([expr1, expr2, expr3, ...]) 12 | math.eval([expr1, expr2, expr3, ...], scope) 13 | ``` 14 | 15 | ### Parameters 16 | 17 | Parameter | Type | Description 18 | --------- | ---- | ----------- 19 | `expr` | String | String[] | Matrix | The expression to be evaluated 20 | `scope` | Object | Scope to read/write variables 21 | 22 | ### Returns 23 | 24 | Type | Description 25 | ---- | ----------- 26 | * | The result of the expression 27 | 28 | 29 | ## Examples 30 | 31 | ```js 32 | math.eval('(2+3)/4'); // 1.25 33 | math.eval('sqrt(3^2 + 4^2)'); // 5 34 | math.eval('sqrt(-4)'); // 2i 35 | math.eval(['a=3', 'b=4', 'a*b']);, // [3, 4, 12] 36 | 37 | var scope = {a:3, b:4}; 38 | math.eval('a * b', scope); // 12 39 | ``` 40 | 41 | 42 | ## See also 43 | 44 | [parse](parse.md), 45 | [compile](compile.md) 46 | 47 | 48 | 49 | -------------------------------------------------------------------------------- /docs/reference/functions/zeros.md: -------------------------------------------------------------------------------- 1 | # Function zeros 2 | 3 | Create a matrix filled with zeros. The created matrix can have one or 4 | multiple dimensions. 5 | 6 | 7 | ## Syntax 8 | 9 | ```js 10 | math.zeros(m) 11 | math.zeros(m, n) 12 | math.zeros([m, n]) 13 | math.zeros([m, n, p, ...]) 14 | ``` 15 | 16 | ### Parameters 17 | 18 | Parameter | Type | Description 19 | --------- | ---- | ----------- 20 | `size` | ...Number | Array | The size of each dimension of the matrix 21 | 22 | ### Returns 23 | 24 | Type | Description 25 | ---- | ----------- 26 | Array | Matrix | Number | A matrix filled with zeros 27 | 28 | 29 | ## Examples 30 | 31 | ```js 32 | math.zeros(3); // returns [0, 0, 0] 33 | math.zeros(3, 2); // returns [[0, 0], [0, 0], [0, 0]] 34 | 35 | var A = [[1, 2, 3], [4, 5, 6]]; 36 | math.zeros(math.size(A)); // returns [[0, 0, 0], [0, 0, 0]] 37 | ``` 38 | 39 | 40 | ## See also 41 | 42 | [ones](ones.md), 43 | [eye](eye.md), 44 | [size](size.md), 45 | [range](range.md) 46 | 47 | 48 | 49 | -------------------------------------------------------------------------------- /docs/reference/functions/sign.md: -------------------------------------------------------------------------------- 1 | # Function sign 2 | 3 | Compute the sign of a value. The sign of a value x is: 4 | 5 | - 1 when x > 1 6 | - -1 when x < 0 7 | - 0 when x == 0 8 | 9 | For matrices, the function is evaluated element wise. 10 | 11 | 12 | ## Syntax 13 | 14 | ```js 15 | math.sign(x) 16 | ``` 17 | 18 | ### Parameters 19 | 20 | Parameter | Type | Description 21 | --------- | ---- | ----------- 22 | `x` | Number | BigNumber | Boolean | Complex | Array | Matrix | null | The number for which to determine the sign 23 | 24 | ### Returns 25 | 26 | Type | Description 27 | ---- | ----------- 28 | Number | BigNumber | Complex | Array | Matrix | e The sign of `x` 29 | 30 | 31 | ## Examples 32 | 33 | ```js 34 | math.sign(3.5); // returns 1 35 | math.sign(-4.2); // returns -1 36 | math.sign(0); // returns 0 37 | 38 | math.sign([3, 5, -2, 0, 2]); // returns [1, 1, -1, 0, 1] 39 | ``` 40 | 41 | 42 | ## See also 43 | 44 | [abs](abs.md) 45 | 46 | 47 | 48 | -------------------------------------------------------------------------------- /docs/command_line_interface.md: -------------------------------------------------------------------------------- 1 | # Command Line Interface (CLI) 2 | 3 | When math.js is installed globally using npm, its expression parser can be used 4 | from the command line. To install math.js globally: 5 | 6 | npm install -g mathjs 7 | 8 | Normally, a global installation must be run with admin rights (precede the 9 | command with `sudo`). After installation, the application `mathjs` is available 10 | via the command line: 11 | 12 | ```bash 13 | $ mathjs 14 | > 12 / (2.3 + 0.7) 15 | 4 16 | > 5.08 cm to inch 17 | 2 inch 18 | > sin(45 deg) ^ 2 19 | 0.5 20 | > 9 / 3 + 2i 21 | 3 + 2i 22 | > det([-1, 2; 3, 1]) 23 | -7 24 | ``` 25 | 26 | The command line interface can be used to open a prompt, to execute a script, 27 | or to pipe input and output streams: 28 | 29 | ```bash 30 | $ mathjs # Open a command prompt 31 | $ mathjs script.txt # Run a script file, output to console 32 | $ mathjs script.txt > results.txt # Run a script file, output to file 33 | $ cat script.txt | mathjs # Run input stream, output to console 34 | $ cat script.txt | mathjs > results.txt # Run input stream, output to file 35 | ``` 36 | -------------------------------------------------------------------------------- /docs/reference/functions/filter.md: -------------------------------------------------------------------------------- 1 | # Function filter 2 | 3 | Sort the items in a matrix. 4 | 5 | 6 | ## Syntax 7 | 8 | ```js 9 | math.filter(x, test) 10 | ``` 11 | 12 | ### Parameters 13 | 14 | Parameter | Type | Description 15 | --------- | ---- | ----------- 16 | `x` | Matrix | Array | A one dimensional matrix or array to filter 17 | `test` | Function | RegExp | A function or regular expression to test items. When `test` is a function, it must return a boolean. All entries for which `test` returns true are returned. 18 | 19 | ### Returns 20 | 21 | Type | Description 22 | ---- | ----------- 23 | Matrix | Array | Returns the filtered matrix. 24 | 25 | 26 | ## Examples 27 | 28 | ```js 29 | function isPositive (x) { 30 | return x > 0; 31 | } 32 | math.filter([6, -2, -1, 4, 3], isPositive); // returns [6, 4, 3] 33 | 34 | math.filter(["23", "foo", "100", "55", "bar"], /[0-9]+/); // returns ["23", "100", "55"] 35 | ``` 36 | 37 | 38 | ## See also 39 | 40 | [forEach](forEach.md), 41 | [map](map.md), 42 | [sort](sort.md) 43 | 44 | 45 | 46 | -------------------------------------------------------------------------------- /docs/reference/functions/cube.md: -------------------------------------------------------------------------------- 1 | # Function cube 2 | 3 | Compute the cube of a value, `x * x * x`. 4 | For matrices, the function is evaluated element wise. 5 | 6 | 7 | ## Syntax 8 | 9 | ```js 10 | math.cube(x) 11 | ``` 12 | 13 | ### Parameters 14 | 15 | Parameter | Type | Description 16 | --------- | ---- | ----------- 17 | `x` | Number | BigNumber | Boolean | Complex | Array | Matrix | null | Number for which to calculate the cube 18 | 19 | ### Returns 20 | 21 | Type | Description 22 | ---- | ----------- 23 | Number | BigNumber | Complex | Array | Matrix | Cube of x 24 | 25 | 26 | ## Examples 27 | 28 | ```js 29 | math.cube(2); // returns Number 8 30 | math.pow(2, 3); // returns Number 8 31 | math.cube(4); // returns Number 64 32 | 4 * 4 * 4; // returns Number 64 33 | 34 | math.cube([1, 2, 3, 4]); // returns Array [1, 8, 27, 64] 35 | ``` 36 | 37 | 38 | ## See also 39 | 40 | [multiply](multiply.md), 41 | [square](square.md), 42 | [pow](pow.md) 43 | 44 | 45 | 46 | -------------------------------------------------------------------------------- /docs/reference/functions/concat.md: -------------------------------------------------------------------------------- 1 | # Function concat 2 | 3 | Concatenate two or more matrices. 4 | 5 | 6 | ## Syntax 7 | 8 | ```js 9 | math.concat(A, B, C, ...) 10 | math.concat(A, B, C, ..., dim) 11 | ``` 12 | 13 | ### Where 14 | 15 | - `dim: number` is a zero-based dimension over which to concatenate the matrices. 16 | By default the last dimension of the matrices. 17 | 18 | ### Parameters 19 | 20 | Parameter | Type | Description 21 | --------- | ---- | ----------- 22 | `args` | ... Array | Matrix | Two or more matrices 23 | 24 | ### Returns 25 | 26 | Type | Description 27 | ---- | ----------- 28 | Array | Matrix | Concatenated matrix 29 | 30 | 31 | ## Examples 32 | 33 | ```js 34 | var A = [[1, 2], [5, 6]]; 35 | var B = [[3, 4], [7, 8]]; 36 | 37 | math.concat(A, B); // returns [[1, 2, 3, 4], [5, 6, 7, 8]] 38 | math.concat(A, B, 0); // returns [[1, 2], [5, 6], [3, 4], [7, 8]] 39 | ``` 40 | 41 | 42 | ## See also 43 | 44 | [size](size.md), 45 | [squeeze](squeeze.md), 46 | [subset](subset.md), 47 | [transpose](transpose.md) 48 | 49 | 50 | 51 | -------------------------------------------------------------------------------- /docs/reference/functions/prod.md: -------------------------------------------------------------------------------- 1 | # Function prod 2 | 3 | Compute the product of a matrix or a list with values. 4 | In case of a (multi dimensional) array or matrix, the sum of all 5 | elements will be calculated. 6 | 7 | 8 | ## Syntax 9 | 10 | ```js 11 | math.prod(a, b, c, ...) 12 | math.prod(A) 13 | ``` 14 | 15 | ### Parameters 16 | 17 | Parameter | Type | Description 18 | --------- | ---- | ----------- 19 | `args` | ... * | A single matrix or or multiple scalar values 20 | 21 | ### Returns 22 | 23 | Type | Description 24 | ---- | ----------- 25 | * | The product of all values 26 | 27 | 28 | ## Examples 29 | 30 | ```js 31 | math.multiply(2, 3); // returns 6 32 | math.prod(2, 3); // returns 6 33 | math.prod(2, 3, 4); // returns 24 34 | math.prod([2, 3, 4]); // returns 24 35 | math.prod([[2, 5], [4, 3]]); // returns 120 36 | ``` 37 | 38 | 39 | ## See also 40 | 41 | [mean](mean.md), 42 | [median](median.md), 43 | [min](min.md), 44 | [max](max.md), 45 | [sum](sum.md), 46 | [std](std.md), 47 | [var](var.md) 48 | 49 | 50 | 51 | -------------------------------------------------------------------------------- /docs/reference/functions/tan.md: -------------------------------------------------------------------------------- 1 | # Function tan 2 | 3 | Calculate the tangent of a value. `tan(x)` is equal to `sin(x) / cos(x)`. 4 | 5 | For matrices, the function is evaluated element wise. 6 | 7 | 8 | ## Syntax 9 | 10 | ```js 11 | math.tan(x) 12 | ``` 13 | 14 | ### Parameters 15 | 16 | Parameter | Type | Description 17 | --------- | ---- | ----------- 18 | `x` | Number | BigNumber | Boolean | Complex | Unit | Array | Matrix | null | Function input 19 | 20 | ### Returns 21 | 22 | Type | Description 23 | ---- | ----------- 24 | Number | BigNumber | Complex | Array | Matrix | Tangent of x 25 | 26 | 27 | ## Examples 28 | 29 | ```js 30 | math.tan(0.5); // returns Number 0.5463024898437905 31 | math.sin(0.5) / math.cos(0.5); // returns Number 0.5463024898437905 32 | math.tan(math.pi / 4); // returns Number 1 33 | math.tan(math.unit(45, 'deg')); // returns Number 1 34 | ``` 35 | 36 | 37 | ## See also 38 | 39 | [atan](atan.md), 40 | [sin](sin.md), 41 | [cos](cos.md) 42 | 43 | 44 | 45 | -------------------------------------------------------------------------------- /docs/reference/functions/cross.md: -------------------------------------------------------------------------------- 1 | # Function cross 2 | 3 | Calculate the cross product for two vectors in three dimensional space. 4 | The cross product of `A = [a1, a2, a3]` and `B =[b1, b2, b3]` is defined 5 | as: 6 | 7 | cross(A, B) = [ 8 | a2 * b3 - a3 * b2, 9 | a3 * b1 - a1 * b3, 10 | a1 * b2 - a2 * b1 11 | ] 12 | 13 | 14 | ## Syntax 15 | 16 | ```js 17 | math.cross(x, y) 18 | ``` 19 | 20 | ### Parameters 21 | 22 | Parameter | Type | Description 23 | --------- | ---- | ----------- 24 | `x` | Array | Matrix | First vector 25 | `y` | Array | Matrix | Second vector 26 | 27 | ### Returns 28 | 29 | Type | Description 30 | ---- | ----------- 31 | Array | Matrix | Returns the cross product of `x` and `y` 32 | 33 | 34 | ## Examples 35 | 36 | ```js 37 | math.cross([1, 1, 0], [0, 1, 1]); // Returns [1, -1, 1] 38 | math.cross([3, -3, 1], [4, 9, 2]); // Returns [-15, -2, 39] 39 | math.cross([2, 3, 4], [5, 6, 7]); // Returns [-3, 6, -3] 40 | ``` 41 | 42 | 43 | ## See also 44 | 45 | [dot](dot.md), 46 | [multiply](multiply.md) 47 | 48 | 49 | 50 | -------------------------------------------------------------------------------- /docs/reference/functions/unaryPlus.md: -------------------------------------------------------------------------------- 1 | # Function unaryPlus 2 | 3 | Unary plus operation. 4 | Boolean values and strings will be converted to a number, numeric values will be returned as is. 5 | 6 | For matrices, the function is evaluated element wise. 7 | 8 | 9 | ## Syntax 10 | 11 | ```js 12 | math.unaryPlus(x) 13 | ``` 14 | 15 | ### Parameters 16 | 17 | Parameter | Type | Description 18 | --------- | ---- | ----------- 19 | `x` | Number | BigNumber | Boolean | String | Complex | Unit | Array | Matrix | null | Input value 20 | 21 | ### Returns 22 | 23 | Type | Description 24 | ---- | ----------- 25 | Number | BigNumber | Complex | Unit | Array | Matrix | Returns the input value when numeric, converts to a number when input is non-numeric. 26 | 27 | 28 | ## Examples 29 | 30 | ```js 31 | math.unaryPlus(3.5); // returns 3.5 32 | math.unaryPlus(1); // returns 1 33 | ``` 34 | 35 | 36 | ## See also 37 | 38 | [unaryMinus](unaryMinus.md), 39 | [add](add.md), 40 | [subtract](subtract.md) 41 | 42 | 43 | 44 | -------------------------------------------------------------------------------- /docs/chaining.md: -------------------------------------------------------------------------------- 1 | # Chaining 2 | 3 | Math.js supports chaining operations by wrapping a value into a `Chain`. 4 | A chain can be created with the function `math.chain(value)` 5 | (formerly `math.select(value)`). 6 | All functions available in the math namespace can be executed via the chain. 7 | The functions will be executed with the chain's value as first argument, 8 | followed by extra arguments provided by the function call itself. 9 | 10 | ```js 11 | math.chain(3) 12 | .add(4) 13 | .subtract(2) 14 | .done(); // 5 15 | 16 | math.chain( [[1, 2], [3, 4]] ) 17 | .subset(math.index(0, 0), 8) 18 | .multiply(3) 19 | .done(); // [[24, 6], [9, 12]] 20 | ``` 21 | 22 | ### API 23 | 24 | A `Chain` is constructed as: 25 | 26 | ```js 27 | math.chain() 28 | math.chain(value) 29 | ``` 30 | 31 | The `Chain` has all functions available in the `math` namespace, and has 32 | a number of special functions: 33 | 34 | - `done()` 35 | Finalize the chain and return the chain's value. 36 | - `valueOf()` 37 | The same as `done()`, returns the chain's value. 38 | - `toString()` 39 | Executes `math.format(value)` onto the chain's value, returning 40 | a string representation of the value. 41 | 42 | -------------------------------------------------------------------------------- /docs/reference/functions/eye.md: -------------------------------------------------------------------------------- 1 | # Function eye 2 | 3 | Create a 2-dimensional identity matrix with size m x n or n x n. 4 | The matrix has ones on the diagonal and zeros elsewhere. 5 | 6 | 7 | ## Syntax 8 | 9 | ```js 10 | math.eye(n) 11 | math.eye(m, n) 12 | math.eye([m, n]) 13 | ``` 14 | 15 | ### Parameters 16 | 17 | Parameter | Type | Description 18 | --------- | ---- | ----------- 19 | `size` | ...Number | Matrix | Array | The size for the matrix 20 | 21 | ### Returns 22 | 23 | Type | Description 24 | ---- | ----------- 25 | Matrix | Array | Number | A matrix with ones on the diagonal. 26 | 27 | 28 | ## Examples 29 | 30 | ```js 31 | math.eye(3); // returns [[1, 0, 0], [0, 1, 0], [0, 0, 1]] 32 | math.eye(3, 2); // returns [[1, 0], [0, 1], [0, 0]] 33 | 34 | var A = [[1, 2, 3], [4, 5, 6]]; 35 | math.eye(math.size(b)); // returns [[1, 0, 0], [0, 1, 0]] 36 | ``` 37 | 38 | 39 | ## See also 40 | 41 | [diag](diag.md), 42 | [ones](ones.md), 43 | [zeros](zeros.md), 44 | [size](size.md), 45 | [range](range.md) 46 | 47 | 48 | 49 | -------------------------------------------------------------------------------- /docs/reference/functions/lcm.md: -------------------------------------------------------------------------------- 1 | # Function lcm 2 | 3 | Calculate the least common multiple for two or more values or arrays. 4 | 5 | lcm is defined as: 6 | 7 | lcm(a, b) = abs(a * b) / gcd(a, b) 8 | 9 | For matrices, the function is evaluated element wise. 10 | 11 | 12 | ## Syntax 13 | 14 | ```js 15 | math.lcm(a, b) 16 | math.lcm(a, b, c, ...) 17 | ``` 18 | 19 | ### Parameters 20 | 21 | Parameter | Type | Description 22 | --------- | ---- | ----------- 23 | `args` | ... Number | BigNumber | Boolean | Array | Matrix | null | Two or more integer numbers 24 | 25 | ### Returns 26 | 27 | Type | Description 28 | ---- | ----------- 29 | Number | BigNumber | Array | Matrix | The least common multiple 30 | 31 | 32 | ## Examples 33 | 34 | ```js 35 | math.lcm(4, 6); // returns 12 36 | math.lcm(6, 21); // returns 42 37 | math.lcm(6, 21, 5); // returns 210 38 | 39 | math.lcm([4, 6], [6, 21]); // returns [12, 42] 40 | ``` 41 | 42 | 43 | ## See also 44 | 45 | [gcd](gcd.md), 46 | [xgcd](xgcd.md) 47 | 48 | 49 | 50 | -------------------------------------------------------------------------------- /docs/reference/functions/conj.md: -------------------------------------------------------------------------------- 1 | # Function conj 2 | 3 | Compute the complex conjugate of a complex value. 4 | If `x = a+bi`, the complex conjugate of `x` is `a - bi`. 5 | 6 | For matrices, the function is evaluated element wise. 7 | 8 | 9 | ## Syntax 10 | 11 | ```js 12 | math.conj(x) 13 | ``` 14 | 15 | ### Parameters 16 | 17 | Parameter | Type | Description 18 | --------- | ---- | ----------- 19 | `x` | Number | BigNumber | Complex | Array | Matrix | Boolean | null | A complex number or array with complex numbers 20 | 21 | ### Returns 22 | 23 | Type | Description 24 | ---- | ----------- 25 | Number | BigNumber | Complex | Array | Matrix | The complex conjugate of x 26 | 27 | 28 | ## Examples 29 | 30 | ```js 31 | math.conj(math.complex('2 + 3i')); // returns Complex 2 - 3i 32 | math.conj(math.complex('2 - 3i')); // returns Complex 2 + 3i 33 | math.conj(math.complex('-5.2i')); // returns Complex 5.2i 34 | ``` 35 | 36 | 37 | ## See also 38 | 39 | [re](re.md), 40 | [im](im.md), 41 | [arg](arg.md), 42 | [abs](abs.md) 43 | 44 | 45 | 46 | -------------------------------------------------------------------------------- /docs/reference/functions/square.md: -------------------------------------------------------------------------------- 1 | # Function square 2 | 3 | Compute the square of a value, `x * x`. 4 | For matrices, the function is evaluated element wise. 5 | 6 | 7 | ## Syntax 8 | 9 | ```js 10 | math.square(x) 11 | ``` 12 | 13 | ### Parameters 14 | 15 | Parameter | Type | Description 16 | --------- | ---- | ----------- 17 | `x` | Number | BigNumber | Boolean | Complex | Array | Matrix | null | Number for which to calculate the square 18 | 19 | ### Returns 20 | 21 | Type | Description 22 | ---- | ----------- 23 | Number | BigNumber | Complex | Array | Matrix | Squared value 24 | 25 | 26 | ## Examples 27 | 28 | ```js 29 | math.square(2); // returns Number 4 30 | math.square(3); // returns Number 9 31 | math.pow(3, 2); // returns Number 9 32 | math.multiply(3, 3); // returns Number 9 33 | 34 | math.square([1, 2, 3, 4]); // returns Array [1, 4, 9, 16] 35 | ``` 36 | 37 | 38 | ## See also 39 | 40 | [multiply](multiply.md), 41 | [cube](cube.md), 42 | [sqrt](sqrt.md), 43 | [pow](pow.md) 44 | 45 | 46 | 47 | -------------------------------------------------------------------------------- /docs/reference/functions/exp.md: -------------------------------------------------------------------------------- 1 | # Function exp 2 | 3 | Calculate the exponent of a value. 4 | For matrices, the function is evaluated element wise. 5 | 6 | 7 | ## Syntax 8 | 9 | ```js 10 | math.exp(x) 11 | ``` 12 | 13 | ### Parameters 14 | 15 | Parameter | Type | Description 16 | --------- | ---- | ----------- 17 | `x` | Number | BigNumber | Boolean | Complex | Array | Matrix | null | A number or matrix to exponentiate 18 | 19 | ### Returns 20 | 21 | Type | Description 22 | ---- | ----------- 23 | Number | BigNumber | Complex | Array | Matrix | Exponent of `x` 24 | 25 | 26 | ## Examples 27 | 28 | ```js 29 | math.exp(2); // returns Number 7.3890560989306495 30 | math.pow(math.e, 2); // returns Number 7.3890560989306495 31 | math.log(math.exp(2)); // returns Number 2 32 | 33 | math.exp([1, 2, 3]); 34 | // returns Array [ 35 | // 2.718281828459045, 36 | // 7.3890560989306495, 37 | // 20.085536923187668 38 | // ] 39 | ``` 40 | 41 | 42 | ## See also 43 | 44 | [log](log.md), 45 | [pow](pow.md) 46 | 47 | 48 | 49 | -------------------------------------------------------------------------------- /docs/reference/functions/bitXor.md: -------------------------------------------------------------------------------- 1 | # Function bitXor 2 | 3 | Bitwise XOR two values, `x ^ y`. 4 | For matrices, the function is evaluated element wise. 5 | 6 | 7 | ## Syntax 8 | 9 | ```js 10 | math.bitXor(x, y) 11 | ``` 12 | 13 | ### Parameters 14 | 15 | Parameter | Type | Description 16 | --------- | ---- | ----------- 17 | `x` | Number | BigNumber | Boolean | Array | Matrix | null | First value to xor 18 | `y` | Number | BigNumber | Boolean | Array | Matrix | null | Second value to xor 19 | 20 | ### Returns 21 | 22 | Type | Description 23 | ---- | ----------- 24 | Number | BigNumber | Array | Matrix | XOR of `x` and `y` 25 | 26 | 27 | ## Examples 28 | 29 | ```js 30 | math.bitXor(1, 2); // returns Number 3 31 | 32 | math.bitXor([2, 3, 4], 4); // returns Array [6, 7, 0] 33 | ``` 34 | 35 | 36 | ## See also 37 | 38 | [bitAnd](bitAnd.md), 39 | [bitNot](bitNot.md), 40 | [bitOr](bitOr.md), 41 | [leftShift](leftShift.md), 42 | [rightArithShift](rightArithShift.md), 43 | [rightLogShift](rightLogShift.md) 44 | 45 | 46 | 47 | -------------------------------------------------------------------------------- /lib/error/UnsupportedTypeError.js: -------------------------------------------------------------------------------- 1 | 'use strict'; 2 | 3 | /** 4 | * Create a TypeError with message: 5 | * 'Function does not support a parameter of type '; 6 | * @param {String} fn Function name 7 | * @param {*...} [types] The types of the function arguments 8 | * @extends TypeError 9 | */ 10 | function UnsupportedTypeError(fn, types) { 11 | if (!(this instanceof UnsupportedTypeError)) { 12 | throw new SyntaxError('Constructor must be called with the new operator'); 13 | } 14 | 15 | this.fn = fn; 16 | this.types = Array.prototype.splice.call(arguments, 1); 17 | 18 | if (!fn) { 19 | this.message = 'Unsupported type of argument'; 20 | } 21 | else { 22 | if (this.types.length == 0) { 23 | this.message = 'Unsupported type of argument in function ' + fn; 24 | } 25 | else { 26 | this.message = 'Function ' + fn + '(' + this.types.join(', ') + ') not supported'; 27 | } 28 | } 29 | 30 | this.stack = (new Error()).stack; 31 | } 32 | 33 | UnsupportedTypeError.prototype = new TypeError(); 34 | UnsupportedTypeError.prototype.constructor = TypeError; 35 | UnsupportedTypeError.prototype.name = 'UnsupportedTypeError'; 36 | 37 | module.exports = UnsupportedTypeError; 38 | -------------------------------------------------------------------------------- /docs/reference/functions/bitAnd.md: -------------------------------------------------------------------------------- 1 | # Function bitAnd 2 | 3 | Bitwise AND two values, `x & y`. 4 | For matrices, the function is evaluated element wise. 5 | 6 | 7 | ## Syntax 8 | 9 | ```js 10 | math.bitAnd(x, y) 11 | ``` 12 | 13 | ### Parameters 14 | 15 | Parameter | Type | Description 16 | --------- | ---- | ----------- 17 | `x` | Number | BigNumber | Boolean | Array | Matrix | null | First value to and 18 | `y` | Number | BigNumber | Boolean | Array | Matrix | null | Second value to and 19 | 20 | ### Returns 21 | 22 | Type | Description 23 | ---- | ----------- 24 | Number | BigNumber | Array | Matrix | AND of `x` and `y` 25 | 26 | 27 | ## Examples 28 | 29 | ```js 30 | math.bitAnd(53, 131); // returns Number 1 31 | 32 | math.bitAnd([1, 12, 31], 42); // returns Array [0, 8, 10] 33 | ``` 34 | 35 | 36 | ## See also 37 | 38 | [bitNot](bitNot.md), 39 | [bitOr](bitOr.md), 40 | [bitXor](bitXor.md), 41 | [leftShift](leftShift.md), 42 | [rightArithShift](rightArithShift.md), 43 | [rightLogShift](rightLogShift.md) 44 | 45 | 46 | 47 | -------------------------------------------------------------------------------- /docs/reference/functions/unaryMinus.md: -------------------------------------------------------------------------------- 1 | # Function unaryMinus 2 | 3 | Inverse the sign of a value, apply a unary minus operation. 4 | 5 | For matrices, the function is evaluated element wise. Boolean values and 6 | strings will be converted to a number. For complex numbers, both real and 7 | complex value are inverted. 8 | 9 | 10 | ## Syntax 11 | 12 | ```js 13 | math.unaryMinus(x) 14 | ``` 15 | 16 | ### Parameters 17 | 18 | Parameter | Type | Description 19 | --------- | ---- | ----------- 20 | `x` | Number | BigNumber | Boolean | String | Complex | Unit | Array | Matrix | null | Number to be inverted. 21 | 22 | ### Returns 23 | 24 | Type | Description 25 | ---- | ----------- 26 | Number | BigNumber | Complex | Unit | Array | Matrix | Returns the value with inverted sign. 27 | 28 | 29 | ## Examples 30 | 31 | ```js 32 | math.unaryMinus(3.5); // returns -3.5 33 | math.unaryMinus(-4.2); // returns 4.2 34 | ``` 35 | 36 | 37 | ## See also 38 | 39 | [add](add.md), 40 | [subtract](subtract.md), 41 | [unaryPlus](unaryPlus.md) 42 | 43 | 44 | 45 | -------------------------------------------------------------------------------- /docs/reference/functions/tanh.md: -------------------------------------------------------------------------------- 1 | # Function tanh 2 | 3 | Calculate the hyperbolic tangent of a value, 4 | defined as `tanh(x) = (exp(2 * x) - 1) / (exp(2 * x) + 1)`. 5 | 6 | For matrices, the function is evaluated element wise. 7 | 8 | 9 | ## Syntax 10 | 11 | ```js 12 | math.tanh(x) 13 | ``` 14 | 15 | ### Parameters 16 | 17 | Parameter | Type | Description 18 | --------- | ---- | ----------- 19 | `x` | Number | BigNumber | Boolean | Complex | Unit | Array | Matrix | null | Function input 20 | 21 | ### Returns 22 | 23 | Type | Description 24 | ---- | ----------- 25 | Number | BigNumber | Complex | Array | Matrix | Hyperbolic tangent of x 26 | 27 | 28 | ## Examples 29 | 30 | ```js 31 | // tanh(x) = sinh(x) / cosh(x) = 1 / coth(x) 32 | math.tanh(0.5); // returns 0.46211715726000974 33 | math.sinh(0.5) / math.cosh(0.5); // returns 0.46211715726000974 34 | 1 / math.coth(0.5); // returns 0.46211715726000974 35 | ``` 36 | 37 | 38 | ## See also 39 | 40 | [sinh](sinh.md), 41 | [cosh](cosh.md), 42 | [coth](coth.md) 43 | 44 | 45 | 46 | -------------------------------------------------------------------------------- /docs/reference/functions/arg.md: -------------------------------------------------------------------------------- 1 | # Function arg 2 | 3 | Compute the argument of a complex value. 4 | For a complex number `a + bi`, the argument is computed as `atan2(b, a)`. 5 | 6 | For matrices, the function is evaluated element wise. 7 | 8 | 9 | ## Syntax 10 | 11 | ```js 12 | math.arg(x) 13 | ``` 14 | 15 | ### Parameters 16 | 17 | Parameter | Type | Description 18 | --------- | ---- | ----------- 19 | `x` | Number | Complex | Array | Matrix | Boolean | null | A complex number or array with complex numbers 20 | 21 | ### Returns 22 | 23 | Type | Description 24 | ---- | ----------- 25 | Number | Array | Matrix | The argument of x 26 | 27 | 28 | ## Examples 29 | 30 | ```js 31 | var a = math.complex(2, 2); 32 | math.arg(a) / math.pi; // returns Number 0.25 33 | 34 | var b = math.complex('2 + 3i'); 35 | math.arg(b); // returns Number 0.982793723247329 36 | math.atan2(3, 2); // returns Number 0.982793723247329 37 | ``` 38 | 39 | 40 | ## See also 41 | 42 | [re](re.md), 43 | [im](im.md), 44 | [conj](conj.md), 45 | [abs](abs.md) 46 | 47 | 48 | 49 | -------------------------------------------------------------------------------- /docs/reference/functions/dotPow.md: -------------------------------------------------------------------------------- 1 | # Function dotPow 2 | 3 | Calculates the power of x to y element wise. 4 | 5 | 6 | ## Syntax 7 | 8 | ```js 9 | math.dotPow(x, y) 10 | ``` 11 | 12 | ### Parameters 13 | 14 | Parameter | Type | Description 15 | --------- | ---- | ----------- 16 | `x` | Number | BigNumber | Boolean | Complex | Unit | Array | Matrix | null | The base 17 | `y` | Number | BigNumber | Boolean | Complex | Unit | Array | Matrix | null | The exponent 18 | 19 | ### Returns 20 | 21 | Type | Description 22 | ---- | ----------- 23 | Number | BigNumber | Complex | Unit | Array | Matrix | The value of `x` to the power `y` 24 | 25 | 26 | ## Examples 27 | 28 | ```js 29 | math.dotPow(2, 3); // returns Number 8 30 | 31 | var a = [[1, 2], [4, 3]]; 32 | math.dotPow(a, 2); // returns Array [[1, 4], [16, 9]] 33 | math.pow(a, 2); // returns Array [[9, 8], [16, 17]] 34 | ``` 35 | 36 | 37 | ## See also 38 | 39 | [pow](pow.md), 40 | [sqrt](sqrt.md), 41 | [multiply](multiply.md) 42 | 43 | 44 | 45 | -------------------------------------------------------------------------------- /docs/reference/functions/re.md: -------------------------------------------------------------------------------- 1 | # Function re 2 | 3 | Get the real part of a complex number. 4 | For a complex number `a + bi`, the function returns `a`. 5 | 6 | For matrices, the function is evaluated element wise. 7 | 8 | 9 | ## Syntax 10 | 11 | ```js 12 | math.re(x) 13 | ``` 14 | 15 | ### Parameters 16 | 17 | Parameter | Type | Description 18 | --------- | ---- | ----------- 19 | `x` | Number | BigNumber | Complex | Array | Matrix | Boolean | null | A complex number or array with complex numbers 20 | 21 | ### Returns 22 | 23 | Type | Description 24 | ---- | ----------- 25 | Number | BigNumber | Array | Matrix | The real part of x 26 | 27 | 28 | ## Examples 29 | 30 | ```js 31 | var a = math.complex(2, 3); 32 | math.re(a); // returns Number 2 33 | math.im(a); // returns Number 3 34 | 35 | math.re(math.complex('-5.2i')); // returns Number 0 36 | math.re(math.complex(2.4)); // returns Number 2.4 37 | ``` 38 | 39 | 40 | ## See also 41 | 42 | [im](im.md), 43 | [conj](conj.md), 44 | [abs](abs.md), 45 | [arg](arg.md) 46 | 47 | 48 | 49 | -------------------------------------------------------------------------------- /docs/reference/functions/sin.md: -------------------------------------------------------------------------------- 1 | # Function sin 2 | 3 | Calculate the sine of a value. 4 | 5 | For matrices, the function is evaluated element wise. 6 | 7 | 8 | ## Syntax 9 | 10 | ```js 11 | math.sin(x) 12 | ``` 13 | 14 | ### Parameters 15 | 16 | Parameter | Type | Description 17 | --------- | ---- | ----------- 18 | `x` | Number | BigNumber | Boolean | Complex | Unit | Array | Matrix | null | Function input 19 | 20 | ### Returns 21 | 22 | Type | Description 23 | ---- | ----------- 24 | Number | BigNumber | Complex | Array | Matrix | Sine of x 25 | 26 | 27 | ## Examples 28 | 29 | ```js 30 | math.sin(2); // returns Number 0.9092974268256813 31 | math.sin(math.pi / 4); // returns Number 0.7071067811865475 32 | math.sin(math.unit(90, 'deg')); // returns Number 1 33 | math.sin(math.unit(30, 'deg')); // returns Number 0.5 34 | 35 | var angle = 0.2; 36 | math.pow(math.sin(angle), 2) + math.pow(math.cos(angle), 2); // returns Number ~1 37 | ``` 38 | 39 | 40 | ## See also 41 | 42 | [cos](cos.md), 43 | [tan](tan.md) 44 | 45 | 46 | 47 | -------------------------------------------------------------------------------- /docs/reference/functions/unit.md: -------------------------------------------------------------------------------- 1 | # Function unit 2 | 3 | Create a unit. Depending on the passed arguments, the function 4 | will create and return a new math.type.Unit object. 5 | When a matrix is provided, all elements will be converted to units. 6 | 7 | 8 | ## Syntax 9 | 10 | ```js 11 | math.unit(unit : string) 12 | math.unit(value : number, unit : string) 13 | ``` 14 | 15 | ### Parameters 16 | 17 | Parameter | Type | Description 18 | --------- | ---- | ----------- 19 | `args` | * | Array | Matrix | A number and unit. 20 | 21 | ### Returns 22 | 23 | Type | Description 24 | ---- | ----------- 25 | Unit | Array | Matrix | The created unit 26 | 27 | 28 | ## Examples 29 | 30 | ```js 31 | var a = math.unit(5, 'cm'); // returns Unit 50 mm 32 | var b = math.unit('23 kg'); // returns Unit 23 kg 33 | a.to('m'); // returns Unit 0.05 m 34 | ``` 35 | 36 | 37 | ## See also 38 | 39 | [bignumber](bignumber.md), 40 | [boolean](boolean.md), 41 | [complex](complex.md), 42 | [index](index.md), 43 | [matrix](matrix.md), 44 | [number](number.md), 45 | [string](string.md) 46 | 47 | 48 | 49 | -------------------------------------------------------------------------------- /docs/reference/functions/cos.md: -------------------------------------------------------------------------------- 1 | # Function cos 2 | 3 | Calculate the cosine of a value. 4 | 5 | For matrices, the function is evaluated element wise. 6 | 7 | 8 | ## Syntax 9 | 10 | ```js 11 | math.cos(x) 12 | ``` 13 | 14 | ### Parameters 15 | 16 | Parameter | Type | Description 17 | --------- | ---- | ----------- 18 | `x` | Number | BigNumber | Boolean | Complex | Unit | Array | Matrix | null | Function input 19 | 20 | ### Returns 21 | 22 | Type | Description 23 | ---- | ----------- 24 | Number | BigNumber | Complex | Array | Matrix | Cosine of x 25 | 26 | 27 | ## Examples 28 | 29 | ```js 30 | math.cos(2); // returns Number -0.4161468365471422 31 | math.cos(math.pi / 4); // returns Number 0.7071067811865475 32 | math.cos(math.unit(180, 'deg')); // returns Number -1 33 | math.cos(math.unit(60, 'deg')); // returns Number 0.5 34 | 35 | var angle = 0.2; 36 | math.pow(math.sin(angle), 2) + math.pow(math.cos(angle), 2); // returns Number ~1 37 | ``` 38 | 39 | 40 | ## See also 41 | 42 | [cos](cos.md), 43 | [tan](tan.md) 44 | 45 | 46 | 47 | -------------------------------------------------------------------------------- /docs/reference/functions/median.md: -------------------------------------------------------------------------------- 1 | # Function median 2 | 3 | Compute the median of a matrix or a list with values. The values are 4 | sorted and the middle value is returned. In case of an even number of 5 | values, the average of the two middle values is returned. 6 | Supported types of values are: Number, BigNumber, Unit 7 | 8 | In case of a (multi dimensional) array or matrix, the median of all 9 | elements will be calculated. 10 | 11 | 12 | ## Syntax 13 | 14 | ```js 15 | mean.median(a, b, c, ...) 16 | mean.median(A) 17 | ``` 18 | 19 | ### Parameters 20 | 21 | Parameter | Type | Description 22 | --------- | ---- | ----------- 23 | `args` | ... * | A single matrix or or multiple scalar values 24 | 25 | ### Returns 26 | 27 | Type | Description 28 | ---- | ----------- 29 | * | The median 30 | 31 | 32 | ## Examples 33 | 34 | ```js 35 | math.median(5, 2, 7); // returns 5 36 | math.median([3, -1, 5, 7]); // returns 4 37 | ``` 38 | 39 | 40 | ## See also 41 | 42 | [mean](mean.md), 43 | [min](min.md), 44 | [max](max.md), 45 | [sum](sum.md), 46 | [prod](prod.md), 47 | [std](std.md), 48 | [var](var.md) 49 | 50 | 51 | 52 | -------------------------------------------------------------------------------- /docs/reference/functions/im.md: -------------------------------------------------------------------------------- 1 | # Function im 2 | 3 | Get the imaginary part of a complex number. 4 | For a complex number `a + bi`, the function returns `b`. 5 | 6 | For matrices, the function is evaluated element wise. 7 | 8 | 9 | ## Syntax 10 | 11 | ```js 12 | math.im(x) 13 | ``` 14 | 15 | ### Parameters 16 | 17 | Parameter | Type | Description 18 | --------- | ---- | ----------- 19 | `x` | Number | BigNumber | Complex | Array | Matrix | Boolean | null | A complex number or array with complex numbers 20 | 21 | ### Returns 22 | 23 | Type | Description 24 | ---- | ----------- 25 | Number | BigNumber | Array | Matrix | The imaginary part of x 26 | 27 | 28 | ## Examples 29 | 30 | ```js 31 | var a = math.complex(2, 3); 32 | math.re(a); // returns Number 2 33 | math.im(a); // returns Number 3 34 | 35 | math.re(math.complex('-5.2i')); // returns Number -5.2 36 | math.re(math.complex(2.4)); // returns Number 0 37 | ``` 38 | 39 | 40 | ## See also 41 | 42 | [re](re.md), 43 | [conj](conj.md), 44 | [abs](abs.md), 45 | [arg](arg.md) 46 | 47 | 48 | 49 | -------------------------------------------------------------------------------- /docs/reference/functions/fix.md: -------------------------------------------------------------------------------- 1 | # Function fix 2 | 3 | Round a value towards zero. 4 | For matrices, the function is evaluated element wise. 5 | 6 | 7 | ## Syntax 8 | 9 | ```js 10 | math.fix(x) 11 | ``` 12 | 13 | ### Parameters 14 | 15 | Parameter | Type | Description 16 | --------- | ---- | ----------- 17 | `x` | Number | BigNumber | Boolean | Complex | Array | Matrix | null | Number to be rounded 18 | 19 | ### Returns 20 | 21 | Type | Description 22 | ---- | ----------- 23 | Number | BigNumber | Complex | Array | Matrix | Rounded value 24 | 25 | 26 | ## Examples 27 | 28 | ```js 29 | math.fix(3.2); // returns Number 3 30 | math.fix(3.8); // returns Number 3 31 | math.fix(-4.2); // returns Number -4 32 | math.fix(-4.7); // returns Number -4 33 | 34 | var c = math.complex(3.2, -2.7); 35 | math.fix(c); // returns Complex 3 - 2i 36 | 37 | math.fix([3.2, 3.8, -4.7]); // returns Array [3, 3, -4] 38 | ``` 39 | 40 | 41 | ## See also 42 | 43 | [ceil](ceil.md), 44 | [floor](floor.md), 45 | [round](round.md) 46 | 47 | 48 | 49 | -------------------------------------------------------------------------------- /docs/reference/functions/parse.md: -------------------------------------------------------------------------------- 1 | # Function parse 2 | 3 | Parse an expression. Returns a node tree, which can be evaluated by 4 | invoking node.eval(); 5 | 6 | 7 | ## Syntax 8 | 9 | ```js 10 | parse(expr) 11 | parse(expr, options) 12 | parse([expr1, expr2, expr3, ...]) 13 | parse([expr1, expr2, expr3, ...], options) 14 | ``` 15 | 16 | ### Parameters 17 | 18 | Parameter | Type | Description 19 | --------- | ---- | ----------- 20 | `expr` | String | String[] | Matrix | Expression to be parsed 21 | `options` | {nodes: Object} | Available options: - `nodes` a set of custom nodes 22 | 23 | ### Returns 24 | 25 | Type | Description 26 | ---- | ----------- 27 | Node | Node[] | node 28 | 29 | 30 | ## Examples 31 | 32 | ```js 33 | var node = parse('sqrt(3^2 + 4^2)'); 34 | node.compile(math).eval(); // 5 35 | 36 | var scope = {a:3, b:4} 37 | var node = parse('a * b'); // 12 38 | var code = node.compile(math); 39 | code.eval(scope); // 12 40 | scope.a = 5; 41 | code.eval(scope); // 20 42 | 43 | var nodes = math.parse(['a = 3', 'b = 4', 'a * b']); 44 | nodes[2].compile(math).eval(); // 12 45 | ``` 46 | 47 | 48 | 49 | 50 | 51 | -------------------------------------------------------------------------------- /docs/reference/functions/string.md: -------------------------------------------------------------------------------- 1 | # Function string 2 | 3 | Create a string or convert any object into a string. 4 | Elements of Arrays and Matrices are processed element wise. 5 | 6 | 7 | ## Syntax 8 | 9 | ```js 10 | math.string(value) 11 | ``` 12 | 13 | ### Parameters 14 | 15 | Parameter | Type | Description 16 | --------- | ---- | ----------- 17 | `value` | * | Array | Matrix | null | A value to convert to a string 18 | 19 | ### Returns 20 | 21 | Type | Description 22 | ---- | ----------- 23 | String | Array | Matrix | The created string 24 | 25 | 26 | ## Examples 27 | 28 | ```js 29 | math.string(4.2); // returns string '4.2' 30 | math.string(math.complex(3, 2); // returns string '3 + 2i' 31 | 32 | var u = math.unit(5, 'km'); 33 | math.string(u.to('m')); // returns string '5000 m' 34 | 35 | math.string([true, false]); // returns ['true', 'false'] 36 | ``` 37 | 38 | 39 | ## See also 40 | 41 | [bignumber](bignumber.md), 42 | [boolean](boolean.md), 43 | [complex](complex.md), 44 | [index](index.md), 45 | [matrix](matrix.md), 46 | [number](number.md), 47 | [unit](unit.md) 48 | 49 | 50 | 51 | -------------------------------------------------------------------------------- /docs/reference/functions/nthRoot.md: -------------------------------------------------------------------------------- 1 | # Function nthRoot 2 | 3 | Calculate the nth root of a value. 4 | The principal nth root of a positive real number A, is the positive real 5 | solution of the equation 6 | 7 | x^root = A 8 | 9 | For matrices, the function is evaluated element wise. 10 | 11 | 12 | ## Syntax 13 | 14 | ```js 15 | math.nthRoot(a, root) 16 | ``` 17 | 18 | ### Parameters 19 | 20 | Parameter | Type | Description 21 | --------- | ---- | ----------- 22 | `a` | Number | BigNumber | Boolean | Array | Matrix | null | Value for which to calculate the nth root 23 | `root` | Number | BigNumber | Boolean | null | The root. Default value: 2. 24 | 25 | ### Returns 26 | 27 | Type | Description 28 | ---- | ----------- 29 | Number | Complex | Array | Matrix | Returns the nth root of `a` 30 | 31 | 32 | ## Examples 33 | 34 | ```js 35 | math.nthRoot(9, 2); // returns 3, as 3^2 == 9 36 | math.sqrt(9); // returns 3, as 3^2 == 9 37 | math.nthRoot(64, 3); // returns 4, as 4^3 == 64 38 | ``` 39 | 40 | 41 | ## See also 42 | 43 | [sqrt](sqrt.md), 44 | [pow](pow.md) 45 | 46 | 47 | 48 | -------------------------------------------------------------------------------- /test/function/matrix/squeeze.test.js: -------------------------------------------------------------------------------- 1 | // test squeeze 2 | var assert = require('assert'), 3 | error = require('../../../lib/error/index'), 4 | math = require('../../../index'), 5 | squeeze = math.squeeze, 6 | size = math.size, 7 | matrix = math.matrix; 8 | 9 | describe('squeeze', function() { 10 | 11 | it('should squeeze an matrix', function() { 12 | var m = math.ones(matrix([1,3,2])); 13 | assert.deepEqual(size(m), matrix([1,3,2])); 14 | assert.deepEqual(size(m.valueOf()), [1,3,2]); 15 | assert.deepEqual(size(squeeze(m)), matrix([3,2])); 16 | 17 | m = math.ones(matrix([1,1,3])); 18 | assert.deepEqual(size(m), matrix([1,1,3])); 19 | assert.deepEqual(size(squeeze(m)), matrix([3])); 20 | assert.deepEqual(size(squeeze(math.range(1,6))), matrix([5])); 21 | 22 | assert.deepEqual(squeeze(2.3), 2.3); 23 | assert.deepEqual(squeeze(matrix([[5]])), 5); 24 | }); 25 | 26 | it('should squeeze an array', function() { 27 | assert.deepEqual(squeeze([[2,3]]), [2,3]); 28 | }); 29 | 30 | it('should throw an error if called with an invalid number of arguments', function() { 31 | assert.throws(function () {squeeze()}, error.ArgumentsError); 32 | assert.throws(function () {squeeze(1,2)}, error.ArgumentsError); 33 | }); 34 | }); -------------------------------------------------------------------------------- /docs/reference/functions/floor.md: -------------------------------------------------------------------------------- 1 | # Function floor 2 | 3 | Round a value towards minus infinity. 4 | For matrices, the function is evaluated element wise. 5 | 6 | 7 | ## Syntax 8 | 9 | ```js 10 | math.floor(x) 11 | ``` 12 | 13 | ### Parameters 14 | 15 | Parameter | Type | Description 16 | --------- | ---- | ----------- 17 | `x` | Number | BigNumber | Boolean | Complex | Array | Matrix | null | Number to be rounded 18 | 19 | ### Returns 20 | 21 | Type | Description 22 | ---- | ----------- 23 | Number | BigNumber | Complex | Array | Matrix | Rounded value 24 | 25 | 26 | ## Examples 27 | 28 | ```js 29 | math.floor(3.2); // returns Number 3 30 | math.floor(3.8); // returns Number 3 31 | math.floor(-4.2); // returns Number -5 32 | math.floor(-4.7); // returns Number -5 33 | 34 | var c = math.complex(3.2, -2.7); 35 | math.floor(c); // returns Complex 3 - 3i 36 | 37 | math.floor([3.2, 3.8, -4.7]); // returns Array [3, 3, -5] 38 | ``` 39 | 40 | 41 | ## See also 42 | 43 | [ceil](ceil.md), 44 | [fix](fix.md), 45 | [round](round.md) 46 | 47 | 48 | 49 | -------------------------------------------------------------------------------- /docs/reference/functions/pow.md: -------------------------------------------------------------------------------- 1 | # Function pow 2 | 3 | Calculates the power of x to y, `x ^ y`. 4 | Matrix exponentiation is supported for square matrices `x`, and positive 5 | integer exponents `y`. 6 | 7 | 8 | ## Syntax 9 | 10 | ```js 11 | math.pow(x, y) 12 | ``` 13 | 14 | ### Parameters 15 | 16 | Parameter | Type | Description 17 | --------- | ---- | ----------- 18 | `x` | Number | BigNumber | Boolean | Complex | Array | Matrix | null | The base 19 | `y` | Number | BigNumber | Boolean | Complex | null | The exponent 20 | 21 | ### Returns 22 | 23 | Type | Description 24 | ---- | ----------- 25 | Number | BigNumber | Complex | Array | Matrix | The value of `x` to the power `y` 26 | 27 | 28 | ## Examples 29 | 30 | ```js 31 | math.pow(2, 3); // returns Number 8 32 | 33 | var a = math.complex(2, 3); 34 | math.pow(a, 2) // returns Complex -5 + 12i 35 | 36 | var b = [[1, 2], [4, 3]]; 37 | math.pow(b, 2); // returns Array [[9, 8], [16, 17]] 38 | ``` 39 | 40 | 41 | ## See also 42 | 43 | [multiply](multiply.md), 44 | [sqrt](sqrt.md) 45 | 46 | 47 | 48 | -------------------------------------------------------------------------------- /docs/reference/functions/resize.md: -------------------------------------------------------------------------------- 1 | # Function resize 2 | 3 | Resize a matrix 4 | 5 | 6 | ## Syntax 7 | 8 | ```js 9 | math.resize(x, size) 10 | math.resize(x, size, defaultValue) 11 | ``` 12 | 13 | ### Parameters 14 | 15 | Parameter | Type | Description 16 | --------- | ---- | ----------- 17 | `x` | * | Array | Matrix | Matrix to be resized 18 | `size` | Array | Matrix | One dimensional array with numbers 19 | `defaultValue` | Number | String | Zero by default, except in case of a string, in that case defaultValue = ' ' Default value: 0. 20 | 21 | ### Returns 22 | 23 | Type | Description 24 | ---- | ----------- 25 | * | Array | Matrix | A resized clone of matrix `x` 26 | 27 | 28 | ## Examples 29 | 30 | ```js 31 | math.resize([1, 2, 3, 4, 5], [3]); // returns Array [1, 2, 3] 32 | math.resize([1, 2, 3], [5], 0); // returns Array [1, 2, 3, 0, 0] 33 | math.resize(2, [2, 3], 0); // returns Matrix [[2, 0, 0], [0, 0, 0]] 34 | math.resize("hello", [8], "!"); // returns String 'hello!!!' 35 | ``` 36 | 37 | 38 | ## See also 39 | 40 | [size](size.md), 41 | [squeeze](squeeze.md), 42 | [subset](subset.md) 43 | 44 | 45 | 46 | -------------------------------------------------------------------------------- /docs/reference/functions/sort.md: -------------------------------------------------------------------------------- 1 | # Function sort 2 | 3 | Sort the items in a matrix. 4 | 5 | 6 | ## Syntax 7 | 8 | ```js 9 | math.sort(x) 10 | math.sort(x, compare) 11 | ``` 12 | 13 | ### Parameters 14 | 15 | Parameter | Type | Description 16 | --------- | ---- | ----------- 17 | `x` | Matrix | Array | A one dimensional matrix or array to sort 18 | `compare` | Function | 'asc' | 'desc' | An optional comparator function. The function is called as `compare(a, b)`, and must return 1 when a > b, -1 when a < b, and 0 when a == b. Default value: 'asc'. 19 | 20 | ### Returns 21 | 22 | Type | Description 23 | ---- | ----------- 24 | Matrix | Array | Returns the sorted matrix. 25 | 26 | 27 | ## Examples 28 | 29 | ```js 30 | math.sort([5, 10, 1]); // returns [1, 5, 10] 31 | math.sort(['C', 'B', 'A', 'D']); // returns ['A', 'B', 'C', 'D'] 32 | 33 | function sortByLength (a, b) { 34 | return a.length - b.length; 35 | } 36 | math.sort(['Langdon', 'Tom', 'Sara'], sortByLength); // returns ['Tom', 'Sara', 'Langdon'] 37 | ``` 38 | 39 | 40 | ## See also 41 | 42 | [filter](filter.md), 43 | [forEach](forEach.md), 44 | [map](map.md) 45 | 46 | 47 | 48 | -------------------------------------------------------------------------------- /docs/reference/functions/bitOr.md: -------------------------------------------------------------------------------- 1 | # Function bitOr 2 | 3 | Bitwise OR two values, `x | y`. 4 | For matrices, the function is evaluated element wise. 5 | For units, the function is evaluated on the lowest print base. 6 | 7 | 8 | ## Syntax 9 | 10 | ```js 11 | math.bitOr(x, y) 12 | ``` 13 | 14 | ### Parameters 15 | 16 | Parameter | Type | Description 17 | --------- | ---- | ----------- 18 | `x` | Number | BigNumber | Boolean | Array | Matrix | null | First value to or 19 | `y` | Number | BigNumber | Boolean | Array | Matrix | null | Second value to or 20 | 21 | ### Returns 22 | 23 | Type | Description 24 | ---- | ----------- 25 | Number | BigNumber | Array | Matrix | OR of `x` and `y` 26 | 27 | 28 | ## Examples 29 | 30 | ```js 31 | math.bitOr(1, 2); // returns Number 3 32 | 33 | math.bitOr([1, 2, 3], 4); // returns Array [5, 6, 7] 34 | ``` 35 | 36 | 37 | ## See also 38 | 39 | [bitAnd](bitAnd.md), 40 | [bitNot](bitNot.md), 41 | [bitXor](bitXor.md), 42 | [leftShift](leftShift.md), 43 | [rightArithShift](rightArithShift.md), 44 | [rightLogShift](rightLogShift.md) 45 | 46 | 47 | 48 | -------------------------------------------------------------------------------- /docs/reference/functions/rightLogShift.md: -------------------------------------------------------------------------------- 1 | # Function rightLogShift 2 | 3 | Bitwise right logical shift of value x by y number of bits, `x >>> y`. 4 | For matrices, the function is evaluated element wise. 5 | For units, the function is evaluated on the best prefix base. 6 | 7 | 8 | ## Syntax 9 | 10 | ```js 11 | math.rightLogShift(x, y) 12 | ``` 13 | 14 | ### Parameters 15 | 16 | Parameter | Type | Description 17 | --------- | ---- | ----------- 18 | `x` | Number | Boolean | Array | Matrix | null | Value to be shifted 19 | `y` | Number | Boolean | null | Amount of shifts 20 | 21 | ### Returns 22 | 23 | Type | Description 24 | ---- | ----------- 25 | Number | Array | Matrix | `x` zero-filled shifted right `y` times 26 | 27 | 28 | ## Examples 29 | 30 | ```js 31 | math.rightLogShift(4, 2); // returns Number 1 32 | 33 | math.rightLogShift([16, -32, 64], 4); // returns Array [1, 2, 3] 34 | ``` 35 | 36 | 37 | ## See also 38 | 39 | [bitAnd](bitAnd.md), 40 | [bitNot](bitNot.md), 41 | [bitOr](bitOr.md), 42 | [bitXor](bitXor.md), 43 | [leftShift](leftShift.md), 44 | [rightArithShift](rightArithShift.md) 45 | 46 | 47 | 48 | -------------------------------------------------------------------------------- /lib/expression/transform/concat.transform.js: -------------------------------------------------------------------------------- 1 | 'use strict'; 2 | 3 | var BigNumber = require('decimal.js'); 4 | var errorTransform = require('./error.transform').transform; 5 | var isNumber = require('../../util/number').isNumber; 6 | var argsToArray = require('../../util/array').argsToArray; 7 | 8 | /** 9 | * Attach a transform function to math.range 10 | * Adds a property transform containing the transform function. 11 | * 12 | * This transform changed the last `dim` parameter of function concat 13 | * from one-based to zero based 14 | * @param {Object} math 15 | */ 16 | module.exports = function (math) { 17 | var transform = function () { 18 | // copy arguments into an array 19 | var args = argsToArray(arguments); 20 | 21 | // change last argument from one-based to zero-based 22 | var lastIndex = args.length - 1; 23 | var last = args[lastIndex]; 24 | if (isNumber(last)) { 25 | args[lastIndex] = last - 1; 26 | } 27 | else if (last instanceof BigNumber) { 28 | args[lastIndex] = last.minus(1); 29 | } 30 | 31 | try { 32 | return math.concat.apply(math, args); 33 | } 34 | catch (err) { 35 | throw errorTransform(err); 36 | } 37 | }; 38 | 39 | math.concat.transform = transform; 40 | 41 | return transform; 42 | }; 43 | -------------------------------------------------------------------------------- /docs/reference/functions/dotDivide.md: -------------------------------------------------------------------------------- 1 | # Function dotDivide 2 | 3 | Divide two matrices element wise. The function accepts both matrices and 4 | scalar values. 5 | 6 | 7 | ## Syntax 8 | 9 | ```js 10 | math.dotDivide(x, y) 11 | ``` 12 | 13 | ### Parameters 14 | 15 | Parameter | Type | Description 16 | --------- | ---- | ----------- 17 | `x` | Number | BigNumber | Boolean | Complex | Unit | Array | Matrix | null | Numerator 18 | `y` | Number | BigNumber | Boolean | Complex | Unit | Array | Matrix | null | Denominator 19 | 20 | ### Returns 21 | 22 | Type | Description 23 | ---- | ----------- 24 | Number | BigNumber | Complex | Unit | Array | Matrix | Quotient, `x ./ y` 25 | 26 | 27 | ## Examples 28 | 29 | ```js 30 | math.dotDivide(2, 4); // returns 0.5 31 | 32 | a = [[9, 5], [6, 1]]; 33 | b = [[3, 2], [5, 2]]; 34 | 35 | math.dotDivide(a, b); // returns [[3, 2.5], [1.2, 0.5]] 36 | math.divide(a, b); // returns [[1.75, 0.75], [-1.75, 2.25]] 37 | ``` 38 | 39 | 40 | ## See also 41 | 42 | [divide](divide.md), 43 | [multiply](multiply.md), 44 | [dotMultiply](dotMultiply.md) 45 | 46 | 47 | 48 | -------------------------------------------------------------------------------- /docs/reference/functions/or.md: -------------------------------------------------------------------------------- 1 | # Function or 2 | 3 | Logical `or`. Test if at least one value is defined with a nonzero/nonempty value. 4 | For matrices, the function is evaluated element wise. 5 | 6 | 7 | ## Syntax 8 | 9 | ```js 10 | math.or(x, y) 11 | ``` 12 | 13 | ### Parameters 14 | 15 | Parameter | Type | Description 16 | --------- | ---- | ----------- 17 | `x` | Number | BigNumber | Boolean | Complex | Unit | Array | Matrix | null | First value to check 18 | `y` | Number | BigNumber | Boolean | Complex | Unit | Array | Matrix | null | Second value to check 19 | 20 | ### Returns 21 | 22 | Type | Description 23 | ---- | ----------- 24 | Boolean | Array | Matrix | Returns true when one of the inputs is defined with a nonzero/nonempty value. 25 | 26 | 27 | ## Examples 28 | 29 | ```js 30 | math.or(2, 4); // returns true 31 | 32 | a = [2, 5, 0]; 33 | b = [0, 22, 0]; 34 | c = 0; 35 | 36 | math.or(a, b); // returns [true, true, false] 37 | math.or(b, c); // returns [false, true, false] 38 | ``` 39 | 40 | 41 | ## See also 42 | 43 | [and](and.md), 44 | [not](not.md), 45 | [xor](xor.md) 46 | 47 | 48 | 49 | -------------------------------------------------------------------------------- /docs/reference/functions/and.md: -------------------------------------------------------------------------------- 1 | # Function and 2 | 3 | Logical `and`. Test whether two values are both defined with a nonzero/nonempty value. 4 | For matrices, the function is evaluated element wise. 5 | 6 | 7 | ## Syntax 8 | 9 | ```js 10 | math.and(x, y) 11 | ``` 12 | 13 | ### Parameters 14 | 15 | Parameter | Type | Description 16 | --------- | ---- | ----------- 17 | `x` | Number | BigNumber | Boolean | Complex | Unit | Array | Matrix | null | First value to check 18 | `y` | Number | BigNumber | Boolean | Complex | Unit | Array | Matrix | null | Second value to check 19 | 20 | ### Returns 21 | 22 | Type | Description 23 | ---- | ----------- 24 | Boolean | Array | Matrix | Returns true when both inputs are defined with a nonzero/nonempty value. 25 | 26 | 27 | ## Examples 28 | 29 | ```js 30 | math.and(2, 4); // returns true 31 | 32 | a = [2, 0, 0]; 33 | b = [3, 7, 0]; 34 | c = 0; 35 | 36 | math.and(a, b); // returns [true, false, false] 37 | math.and(a, c); // returns [false, false, false] 38 | ``` 39 | 40 | 41 | ## See also 42 | 43 | [not](not.md), 44 | [or](or.md), 45 | [xor](xor.md) 46 | 47 | 48 | 49 | -------------------------------------------------------------------------------- /docs/reference/functions/bignumber.md: -------------------------------------------------------------------------------- 1 | # Function bignumber 2 | 3 | Create a BigNumber, which can store numbers with arbitrary precision. 4 | When a matrix is provided, all elements will be converted to BigNumber. 5 | 6 | 7 | ## Syntax 8 | 9 | ```js 10 | math.bignumber(x) 11 | ``` 12 | 13 | ### Parameters 14 | 15 | Parameter | Type | Description 16 | --------- | ---- | ----------- 17 | `value` | Number | String | Array | Matrix | Boolean | null | Value for the big number, 0 by default. 18 | 19 | ### Returns 20 | 21 | Type | Description 22 | ---- | ----------- 23 | BigNumber | The created bignumber 24 | 25 | 26 | ## Examples 27 | 28 | ```js 29 | 0.1 + 0.2; // returns Number 0.30000000000000004 30 | math.bignumber(0.1) + math.bignumber(0.2); // returns BigNumber 0.3 31 | 32 | 33 | 7.2e500; // returns Number Infinity 34 | math.bignumber('7.2e500'); // returns BigNumber 7.2e500 35 | ``` 36 | 37 | 38 | ## See also 39 | 40 | [boolean](boolean.md), 41 | [complex](complex.md), 42 | [index](index.md), 43 | [matrix](matrix.md), 44 | [string](string.md), 45 | [unit](unit.md) 46 | 47 | 48 | 49 | -------------------------------------------------------------------------------- /docs/reference/functions/squeeze.md: -------------------------------------------------------------------------------- 1 | # Function squeeze 2 | 3 | Squeeze a matrix, remove inner and outer singleton dimensions from a matrix. 4 | 5 | 6 | ## Syntax 7 | 8 | ```js 9 | math.squeeze(x) 10 | ``` 11 | 12 | ### Parameters 13 | 14 | Parameter | Type | Description 15 | --------- | ---- | ----------- 16 | `x` | Matrix | Array | Matrix to be squeezed 17 | 18 | ### Returns 19 | 20 | Type | Description 21 | ---- | ----------- 22 | Matrix | Array | Squeezed matrix 23 | 24 | 25 | ## Examples 26 | 27 | ```js 28 | math.squeeze([3]); // returns 3 29 | math.squeeze([[3]]); // returns 3 30 | 31 | var A = math.zeros(3, 1); // returns [[0], [0], [0]] (size 3x1) 32 | math.squeeze(A); // returns [0, 0, 0] (size 3) 33 | 34 | var B = math.zeros(1, 3); // returns [[0, 0, 0]] (size 1x3) 35 | math.squeeze(B); // returns [0, 0, 0] (size 3) 36 | 37 | // only inner and outer dimensions are removed 38 | var C = math.zeros(2, 1, 3); // returns [[[0, 0, 0]], [[0, 0, 0]]] (size 2x1x3) 39 | math.squeeze(C); // returns [[[0, 0, 0]], [[0, 0, 0]]] (size 2x1x3) 40 | ``` 41 | 42 | 43 | ## See also 44 | 45 | [subset](subset.md) 46 | 47 | 48 | 49 | --------------------------------------------------------------------------------