Comparing version 0.22.0 to 0.23.0
@@ -175,2 +175,3 @@ #!/usr/bin/env node | ||
var res = parser.eval(expr); | ||
parser.set('ans', res); // TODO: in case of multi line input, set ans as the last of the expressions | ||
if (!Array.isArray(res) || res.length) { | ||
@@ -177,0 +178,0 @@ // TODO: how to distinguish array output from multi-line output? |
@@ -17,2 +17,4 @@ # Constants | ||
- math.tau | ||
- math.phi, golden ratio | ||
- math.version, version number of math.js | ||
@@ -22,4 +24,4 @@ Example usage: | ||
```js | ||
math.sin(math.pi / 4); // 0.70711 | ||
math.i * math.i; // -1 | ||
math.sin(math.pi / 4); // 0.70711 | ||
math.multiply(math.i, math.i); // -1 | ||
``` |
@@ -100,2 +100,2 @@ # Numbers | ||
The available comparison functions are: `compare`, `equal`, `larger`, | ||
`largereq`, `smaller`, `smallereq`, `unequal`. | ||
`largerEq`, `smaller`, `smallerEq`, `unequal`. |
@@ -93,3 +93,3 @@ # Units | ||
Volume | m3, litre (l, L), cc, cuin, cuft, cuyd, teaspoon, tablespoon | ||
Liquid | volume minim (min), fluiddram (fldr), fluidounce (fldz), gill (gi), cup (cp), pint (pt), quart (qt), gallon (gal), beerbarrel (bbl), oilbarrel (obl), hogshead | ||
Liquid | volume minim (min), fluiddram (fldr), fluidounce (fldz), gill (gi), cup (cp), pint (pt), quart (qt), gallon (gal), beerbarrel (bbl), oilbarrel (obl), hogshead, drop (gtt) | ||
Angles | rad, deg, grad, cycle | ||
@@ -96,0 +96,0 @@ Time | second (s), seconds, minute, minutes, hour (h), hours, day, days |
@@ -169,4 +169,4 @@ # Expressions | ||
var node = math.parse('sqrt(x/x+1)'); | ||
node.toString(); // returns 'ans = sqrt((x / x) + 1)' | ||
node.toTex(); // returns '{ans}={\sqrt{{\frac{x}{x}}+{1}}}' | ||
node.toString(); // returns 'sqrt((x / x) + 1)' | ||
node.toTex(); // returns '\sqrt{{\frac{x}{x}}+{1}}' | ||
``` | ||
@@ -289,3 +289,5 @@ | ||
`+` | Add | `x + y` | Left to right | `4 + 5` | `9` | ||
`+` | Unary plus | `+y` | None | `+"4"` | `4` | ||
`-` | Subtract | `x - y` | Left to right | `7 - 3` | `4` | ||
`-` | Unary minus | `-y` | None | `-4` | `-4` | ||
`*` | Multiply | `x * y` | Left to right | `2 * 3` | `6` | ||
@@ -298,3 +300,2 @@ `.*` | Element-wise multiply | `x .* y` | Left to right | `[1,2,3] .* [1,2,3]` | `[1,4,9]` | ||
`.^` | Element-wise power | `x .^ y` | Right to left | `[2,3] .^ [3,3]` | `[9,27]` | ||
`-` | Unary | `-y` | None | `-4` | `-4` | ||
`'` | Transpose | `y'` | None | `[[1,2],[3,4]]'` | `[[1,3],[2,4]]` | ||
@@ -321,3 +322,3 @@ `!` | Factorial | `y!` | None | `5!` | `120` | ||
`^`, `.^` | Exponentiation | ||
`-` | Unary | ||
`+`, `-` | Unary plus, unary minus | ||
`x unit` | Unit | ||
@@ -324,0 +325,0 @@ `*`, `/`, `.*`, `./`, `%`, `mod` | Multiply, divide, modulus |
@@ -29,9 +29,7 @@ # Functions | ||
- math.ceil(x) | ||
- math.compare(x, y) | ||
- math.cube(x) | ||
- math.divide(x, y) | ||
- math.edivide(x, y) | ||
- math.emultiply(x, y) | ||
- math.epow(x, y) | ||
- math.equal(x) | ||
- math.dotDivide(x, y) | ||
- math.dotMultiply(x, y) | ||
- math.dotPow(x, y) | ||
- math.exp(x) | ||
@@ -41,4 +39,2 @@ - math.fix(x) | ||
- math.gcd(a, b, c, ...) | ||
- math.larger(x, y) | ||
- math.largereq(x, y) | ||
- math.lcm(a, b, c, ...) | ||
@@ -53,11 +49,19 @@ - math.log(x [, base]) | ||
- math.sign() | ||
- math.smaller(x, y) | ||
- math.smallereq(x, y) | ||
- math.subtract(x, y) | ||
- math.sqrt(x) | ||
- math.square(x) | ||
- math.unary(x) | ||
- math.unequal(x) | ||
- math.unaryMinus(x) | ||
- math.xgcd(a, b) | ||
## Comparison | ||
- math.compare(x, y) | ||
- math.deepEqual(x, y) | ||
- math.equal(x, y) | ||
- math.larger(x, y) | ||
- math.largerEq(x, y) | ||
- math.smaller(x, y) | ||
- math.smallerEq(x, y) | ||
- math.unequal(x) | ||
## Complex | ||
@@ -64,0 +68,0 @@ |
@@ -54,7 +54,7 @@ # Function compare | ||
[smaller](smaller.md), | ||
[smallereq](smallereq.md), | ||
[smallerEq](smallerEq.md), | ||
[larger](larger.md), | ||
[largereq](largereq.md) | ||
[largerEq](largerEq.md) | ||
<!-- Note: This file is automatically generated from source code comments. Changes made in this file will be overridden. --> |
@@ -44,2 +44,8 @@ # Function equal | ||
math.equal(a, b); // returns true | ||
var c = [2, 5, 1]; | ||
var d = [2, 7, 1]; | ||
math.equal(c, d); // returns [true, false, true] | ||
math.deepEqual(c, d); // returns false | ||
``` | ||
@@ -52,8 +58,9 @@ | ||
[smaller](smaller.md), | ||
[smallereq](smallereq.md), | ||
[smallerEq](smallerEq.md), | ||
[larger](larger.md), | ||
[largereq](largereq.md), | ||
[compare](compare.md) | ||
[largerEq](largerEq.md), | ||
[compare](compare.md), | ||
[deepEqual](deepEqual.md) | ||
<!-- Note: This file is automatically generated from source code comments. Changes made in this file will be overridden. --> |
@@ -5,11 +5,7 @@ # Function ifElse | ||
In case of a matrix or array, the test is done element wise, the | ||
true and false part can be either a matrix/array with the same size | ||
of the condition, or a scalar value. | ||
## Syntax | ||
```js | ||
math.ifElse(condition, trueExpr, falseExpr | ||
math.ifElse(condition, trueExpr, falseExpr) | ||
``` | ||
@@ -21,3 +17,3 @@ | ||
--------- | ---- | ----------- | ||
`condition` | Number | Boolean | String | Complex | BigNumber | Unit | Matrix | Array | The conditional expression | ||
`condition` | Number | Boolean | String | Complex | BigNumber | Unit | The conditional expression | ||
`trueExpr` | * | The true expression | ||
@@ -39,3 +35,2 @@ `falseExpr` | * | The false expression | ||
math.ifElse(true, 'yes', 'no'); // returns 'yes' | ||
math.ifElse([4, 6, 0, -1], true, false); // returns [true, true, false, true] | ||
``` | ||
@@ -42,0 +37,0 @@ |
@@ -51,4 +51,4 @@ # Function larger | ||
[smaller](smaller.md), | ||
[smallereq](smallereq.md), | ||
[largereq](largereq.md), | ||
[smallerEq](smallerEq.md), | ||
[largerEq](largerEq.md), | ||
[compare](compare.md) | ||
@@ -55,0 +55,0 @@ |
@@ -9,5 +9,8 @@ # Function random | ||
```js | ||
math.random() // generate a random number between 0 and 1 | ||
math.random(max) // generate a random number between 0 and max | ||
math.random(min, max) // generate a random number between min and max | ||
math.random() // generate a random number between 0 and 1 | ||
math.random(max) // generate a random number between 0 and max | ||
math.random(min, max) // generate a random number between min and max | ||
math.random(size) // generate a matrix with random numbers between 0 and 1 | ||
math.random(size, max) // generate a matrix with random numbers between 0 and max | ||
math.random(size, min, max) // generate a matrix with random numbers between min and max | ||
``` | ||
@@ -19,2 +22,3 @@ | ||
--------- | ---- | ----------- | ||
`size` | Number | If provided, an array with `size` number of random values is returned | ||
`min` | Number | Minimum boundary for the random value | ||
@@ -27,3 +31,3 @@ `max` | Number | Maximum boundary for the random value | ||
---- | ----------- | ||
Number | A random number | ||
Number | Array | Matrix | A random number | ||
@@ -39,7 +43,13 @@ | ||
math.random(30, 40); // returns a random number between 30 and 40 | ||
math.random([2, 3]); // returns a 2x3 matrix with random numbers between 0 and 1 | ||
``` | ||
## See also | ||
[randomInt](randomInt.md), | ||
[pickRandom](pickRandom.md), | ||
[distribution](distribution.md) | ||
<!-- Note: This file is automatically generated from source code comments. Changes made in this file will be overridden. --> |
@@ -50,5 +50,5 @@ # Function smaller | ||
[unequal](unequal.md), | ||
[smallereq](smallereq.md), | ||
[smallerEq](smallerEq.md), | ||
[larger](larger.md), | ||
[largereq](largereq.md), | ||
[largerEq](largerEq.md), | ||
[compare](compare.md) | ||
@@ -55,0 +55,0 @@ |
@@ -44,2 +44,8 @@ # Function unequal | ||
math.unequal(a, b); // returns false | ||
var c = [2, 5, 1]; | ||
var d = [2, 7, 1]; | ||
math.unequal(c, d); // returns [false, true, false] | ||
math.deepEqual(c, d); // returns false | ||
``` | ||
@@ -51,6 +57,7 @@ | ||
[equal](equal.md), | ||
[deepEqual](deepEqual.md), | ||
[smaller](smaller.md), | ||
[smallereq](smallereq.md), | ||
[smallerEq](smallerEq.md), | ||
[larger](larger.md), | ||
[largereq](largereq.md), | ||
[largerEq](largerEq.md), | ||
[compare](compare.md) | ||
@@ -57,0 +64,0 @@ |
@@ -88,3 +88,3 @@ /** | ||
var node1 = math.parse('sqrt(3^2 + 4^2)'); | ||
print(node1.toString()); // "ans = sqrt((3 ^ 2) + (4 ^ 2))" | ||
print(node1.toString()); // "sqrt((3 ^ 2) + (4 ^ 2))" | ||
@@ -101,3 +101,3 @@ // compile the node | ||
var code2 = node2.compile(math); | ||
print(node2.toString()); // "ans = x ^ a" | ||
print(node2.toString()); // "x ^ a" | ||
var scope = { | ||
@@ -104,0 +104,0 @@ x: 3, |
@@ -5,2 +5,35 @@ # math.js history | ||
## 2014-06-10, version 0.23.0 | ||
- Renamed some functions (everything now has a logical, camel case name): | ||
- Renamed functions `edivide`, `emultiply`, and `epow` to `dotDivide`, | ||
`dotMultiply`, and `dotPow` respectively. | ||
- Renamed functions `smallereq` and `largereq` to `smallerEq` and `largerEq`. | ||
- Renamed function `unary` to `unaryMinus` and added support for strings. | ||
- `end` is now a reserved keyword which cannot be used as function or symbol | ||
name in the expression parser, and is not allowed in the scope against which | ||
an expression is evaluated. | ||
- Implemented function `unaryPlus` and unary plus operator. | ||
- Implemented function `deepEqual` for matrix comparisons. | ||
- Added constant `phi`, the golden ratio (`phi = 1.618...`). | ||
- Added constant `version`, returning the version number of math.js as string. | ||
- Added unit `drop` (`gtt`). | ||
- Fixed not being able to load math.js using AMD/require.js. | ||
- Changed signature of `math.parse(expr, nodes)` to `math.parse(expr, options)` | ||
where `options: {nodes: Object.<String, Node>}` | ||
- Removed matrix support from conditional function `ifElse`. | ||
- Removed automatic assignment of expression results to variable `ans`. | ||
This functionality can be restored by pre- or postprocessing every evaluation, | ||
something like: | ||
```js | ||
function evalWithAns (expr, scope) { | ||
var ans = math.eval(expr, scope); | ||
if (scope) { | ||
scope.ans = ans; | ||
} | ||
return ans; | ||
} | ||
``` | ||
## 2014-05-22, version 0.22.0 | ||
@@ -15,2 +48,4 @@ | ||
arrays). | ||
- Fixed random functions not accepting Matrix as input, and always returning | ||
a Matrix as output. | ||
@@ -17,0 +52,0 @@ |
module.exports = function (math) { | ||
var Complex = require('./type/Complex'); | ||
math.version = require('./version'); | ||
math.pi = Math.PI; | ||
math.e = Math.E; | ||
math.tau = Math.PI * 2; | ||
math.phi = 1.61803398874989484820458683436563811772030917980576286213545; // golden ratio, (1+sqrt(5))/2 | ||
math.i = new Complex(0, 1); | ||
@@ -8,0 +11,0 @@ |
@@ -5,3 +5,3 @@ module.exports = { | ||
'syntax': [ | ||
'pi' | ||
'tau' | ||
], | ||
@@ -8,0 +8,0 @@ 'description': 'Tau is the ratio constant of a circle\'s circumference to radius, equal to 2 * pi, approximately 6.2832.', |
@@ -10,4 +10,4 @@ module.exports = { | ||
'examples': [ | ||
'2.1 + 3.6', | ||
'ans - 3.6', | ||
'a = 2.1 + 3.6', | ||
'a - 3.6', | ||
'3 + 2i', | ||
@@ -14,0 +14,0 @@ '"hello" + " world"', |
@@ -10,4 +10,4 @@ module.exports = { | ||
'examples': [ | ||
'2 / 3', | ||
'ans * 3', | ||
'a = 2 / 3', | ||
'a * 3', | ||
'4.5 / 2', | ||
@@ -14,0 +14,0 @@ '3 + 4 / 2', |
@@ -10,4 +10,4 @@ module.exports = { | ||
'examples': [ | ||
'2.1 * 3.4', | ||
'ans / 3.4', | ||
'a = 2.1 * 3.4', | ||
'a / 3.4', | ||
'2 * 3 + 4', | ||
@@ -14,0 +14,0 @@ '2 * (3 + 4)', |
@@ -10,4 +10,4 @@ module.exports = { | ||
'examples': [ | ||
'5.3 - 2', | ||
'ans + 2', | ||
'a = 5.3 - 2', | ||
'a + 2', | ||
'2/3 - 1/6', | ||
@@ -14,0 +14,0 @@ '2 * 3 - 3', |
@@ -10,6 +10,5 @@ module.exports = { | ||
'ifElse(10 > 0, 1, 0)', | ||
'ifElse("", true, false)', | ||
'ifElse([4, 6, 0, -1], true, false)' | ||
'ifElse("", true, false)' | ||
], | ||
'seealso': [] | ||
}; |
@@ -14,5 +14,7 @@ // constants | ||
exports.PI = require('./constants/pi'); | ||
exports.phi = require('./constants/phi'); | ||
exports.SQRT1_2 = require('./constants/SQRT1_2'); | ||
exports.SQRT2 = require('./constants/SQRT2'); | ||
exports.tau = require('./constants/tau'); | ||
exports.version = require('./constants/version'); | ||
exports['true'] = require('./constants/true'); | ||
@@ -24,9 +26,7 @@ | ||
exports.ceil = require('./function/arithmetic/ceil'); | ||
exports.compare = require('./function/arithmetic/compare'); | ||
exports.cube = require('./function/arithmetic/cube'); | ||
exports.divide = require('./function/arithmetic/divide'); | ||
exports.edivide = require('./function/arithmetic/edivide'); | ||
exports.emultiply = require('./function/arithmetic/emultiply'); | ||
exports.epow = require('./function/arithmetic/epow'); | ||
exports['equal'] = require('./function/arithmetic/equal'); | ||
exports.dotDivide = require('./function/arithmetic/dotDivide'); | ||
exports.dotMultiply = require('./function/arithmetic/dotMultiply'); | ||
exports.dotPow = require('./function/arithmetic/dotPow'); | ||
exports.exp = require('./function/arithmetic/exp'); | ||
@@ -36,4 +36,2 @@ exports.fix = require('./function/arithmetic/fix'); | ||
exports.gcd = require('./function/arithmetic/gcd'); | ||
exports.larger = require('./function/arithmetic/larger'); | ||
exports.largereq = require('./function/arithmetic/largereq'); | ||
exports.lcm = require('./function/arithmetic/lcm'); | ||
@@ -48,11 +46,19 @@ exports.log = require('./function/arithmetic/log'); | ||
exports.sign = require('./function/arithmetic/sign'); | ||
exports.smaller = require('./function/arithmetic/smaller'); | ||
exports.smallereq = require('./function/arithmetic/smallereq'); | ||
exports.sqrt = require('./function/arithmetic/sqrt'); | ||
exports.square = require('./function/arithmetic/square'); | ||
exports.subtract = require('./function/arithmetic/subtract'); | ||
exports.unary = require('./function/arithmetic/unary'); | ||
exports.unequal = require('./function/arithmetic/unequal'); | ||
exports.unaryMinus = require('./function/arithmetic/unaryMinus'); | ||
exports.unaryPlus = require('./function/arithmetic/unaryPlus'); | ||
exports.xgcd = require('./function/arithmetic/xgcd'); | ||
// functions - comparison | ||
exports.compare = require('./function/comparison/compare'); | ||
exports.deepEqual = require('./function/comparison/deepEqual'); | ||
exports['equal'] = require('./function/comparison/equal'); | ||
exports.larger = require('./function/comparison/larger'); | ||
exports.largerEq = require('./function/comparison/largerEq'); | ||
exports.smaller = require('./function/comparison/smaller'); | ||
exports.smallerEq = require('./function/comparison/smallerEq'); | ||
exports.unequal = require('./function/comparison/unequal'); | ||
// functions - complex | ||
@@ -59,0 +65,0 @@ exports.arg = require('./function/complex/arg'); |
var Node = require('./Node'), | ||
ArrayNode = require('./ArrayNode'), | ||
keywords = require('../keywords'), | ||
latex = require('../../util/latex'), | ||
@@ -21,4 +23,5 @@ isString = require('../../util/string').isString; | ||
// validate input | ||
if (!isString(name)) throw new TypeError('String expected for parameter "name"'); | ||
if (!(expr instanceof Node)) throw new TypeError('Node expected for parameter "expr"'); | ||
if (!isString(name)) throw new TypeError('String expected for parameter "name"'); | ||
if (!(expr instanceof Node)) throw new TypeError('Node expected for parameter "expr"'); | ||
if (name in keywords) throw new Error('Illegal symbol name, "' + name + '" is a reserved keyword'); | ||
@@ -25,0 +28,0 @@ this.name = name; |
var Node = require('./Node'), | ||
keywords = require('../keywords'), | ||
latex = require('../../util/latex'), | ||
@@ -24,2 +25,3 @@ isString = require('../../util/string').isString; | ||
if (!(expr instanceof Node)) throw new TypeError('Node expected for parameter "expr"'); | ||
if (name in keywords) throw new Error('Illegal function name, "' + name + '" is a reserved keyword'); | ||
@@ -26,0 +28,0 @@ this.name = name; |
@@ -1,2 +0,3 @@ | ||
var error = require('../../error/index'); | ||
var error = require('../../error/index'), | ||
keywords = require('../keywords'); | ||
@@ -40,3 +41,4 @@ /** | ||
math: math, | ||
error: error | ||
error: error, | ||
validateScope: validateScope | ||
}; | ||
@@ -55,2 +57,3 @@ | ||
' try {' + | ||
' if (scope) defs.validateScope(scope);' + | ||
' scope = scope || {};' + | ||
@@ -158,4 +161,19 @@ ' return ' + code + ';' + | ||
return object instanceof Node; | ||
}; | ||
/** | ||
* Validate the symbol names of a scope. | ||
* Throws an error when the scope contains an illegal symbol. | ||
* @param {Object} scope | ||
*/ | ||
function validateScope (scope) { | ||
for (var symbol in scope) { | ||
if (scope.hasOwnProperty(symbol)) { | ||
if (symbol in keywords) { | ||
throw new Error('Scope contains an illegal symbol, "' + symbol + '" is a reserved keyword'); | ||
} | ||
} | ||
} | ||
} | ||
module.exports = Node; |
@@ -37,5 +37,5 @@ var util = require('../util/index'), | ||
* parse(expr) | ||
* parse(expr, nodes) | ||
* parse(expr, options) | ||
* parse([expr1, expr2, expr3, ...]) | ||
* parse([expr1, expr2, expr3, ...], nodes) | ||
* parse([expr1, expr2, expr3, ...], options) | ||
* | ||
@@ -58,7 +58,8 @@ * Example: | ||
* @param {String | String[] | Matrix} expr | ||
* @param {Object<String, Node>} [nodes] A set of custom nodes | ||
* @param {{nodes: Object<String, Node>}} [options] Available options: | ||
* - `nodes` a set of custom nodes | ||
* @return {Node | Node[]} node | ||
* @throws {Error} | ||
*/ | ||
function parse (expr, nodes) { | ||
function parse (expr, options) { | ||
if (arguments.length != 1 && arguments.length != 2) { | ||
@@ -69,3 +70,3 @@ throw new ArgumentsError('parse', arguments.length, 1, 2); | ||
// pass extra nodes | ||
extra_nodes = (type(nodes) === 'object') ? nodes : {}; | ||
extra_nodes = (options && options.nodes) ? options.nodes : {}; | ||
@@ -209,5 +210,7 @@ if (isString(expr)) { | ||
// TODO: make this checking for operators of 3, 2, and 1 characters less stupid | ||
// check for delimiters consisting of 2 characters | ||
var c2 = c + nextPreview(); | ||
if (DELIMITERS[c2]) { | ||
if (c2.length == 2 && DELIMITERS[c2]) { | ||
token_type = TOKENTYPE.DELIMITER; | ||
@@ -318,24 +321,2 @@ token = c2; | ||
/** | ||
* Check if a given name is valid | ||
* if not, an error is thrown | ||
* @param {String} name | ||
* @return {boolean} valid | ||
* @private | ||
*/ | ||
/** TODO: check for valid symbol name | ||
function isValidSymbolName (name) { | ||
for (var i = 0, iMax = name.length; i < iMax; i++) { | ||
var c = name.charAt(i); | ||
//var valid = (isAlpha(c) || (i > 0 && isDigit(c))); // TODO: allow digits in symbol name | ||
var valid = (isAlpha(c)); | ||
if (!valid) { | ||
return false; | ||
} | ||
} | ||
return true; | ||
} | ||
*/ | ||
/** | ||
* checks if the given char c is a letter (upper or lower case) | ||
@@ -420,3 +401,3 @@ * or underscore | ||
if (token != '\n' && token != ';') { | ||
node = parseAns(); | ||
node = parseFunctionAssignment(); | ||
} | ||
@@ -436,3 +417,3 @@ | ||
if (token != '\n' && token != ';' && token != '') { | ||
node = parseAns(); | ||
node = parseFunctionAssignment(); | ||
@@ -452,17 +433,2 @@ visible = (token != ';'); | ||
/** | ||
* Parse assignment of ans. | ||
* Ans is assigned when the expression itself is no variable or function | ||
* assignment | ||
* @return {Node} node | ||
* @private | ||
*/ | ||
function parseAns () { | ||
var expression = parseFunctionAssignment(); | ||
// create a variable definition for ans | ||
var name = 'ans'; | ||
return new AssignmentNode(name, expression); | ||
} | ||
/** | ||
* Parse a function assignment like "function f(a,b) = a*b" | ||
@@ -656,4 +622,4 @@ * @return {Node} node | ||
'>': 'larger', | ||
'<=': 'smallereq', | ||
'>=': 'largereq' | ||
'<=': 'smallerEq', | ||
'>=': 'largerEq' | ||
}; | ||
@@ -746,5 +712,5 @@ while (token in operators) { | ||
'*': 'multiply', | ||
'.*': 'emultiply', | ||
'.*': 'dotMultiply', | ||
'/': 'divide', | ||
'./': 'edivide', | ||
'./': 'dotDivide', | ||
'%': 'mod', | ||
@@ -789,3 +755,3 @@ 'mod': 'mod' | ||
/** | ||
* Unary minus | ||
* Unary plus and minus | ||
* @return {Node} node | ||
@@ -797,5 +763,5 @@ * @private | ||
if (token == '-') { | ||
if (token == '-' || token == '+') { | ||
name = token; | ||
fn = 'unary'; | ||
fn = name == '+' ? 'unaryPlus' : 'unaryMinus'; | ||
getToken(); | ||
@@ -823,3 +789,3 @@ params = [parseUnary()]; | ||
name = token; | ||
fn = (name == '^') ? 'pow' : 'epow'; | ||
fn = (name == '^') ? 'pow' : 'dotPow'; | ||
@@ -826,0 +792,0 @@ getToken(); |
@@ -125,6 +125,6 @@ module.exports = function (math) { | ||
mu[i] = new Array(matrix.length); | ||
mu[i][i] = math.unary(sum); | ||
mu[i][i] = math.unaryMinus(sum); | ||
for (j = 0; j < i; j++) { | ||
mu[i][j] = 0; | ||
mu[i][j] = 0; // TODO: make bignumber 0 in case of bignumber computation | ||
} | ||
@@ -150,3 +150,3 @@ | ||
if (rows % 2 == 0) { | ||
return math.unary(fa[0][0]); | ||
return math.unaryMinus(fa[0][0]); | ||
} else { | ||
@@ -153,0 +153,0 @@ return fa[0][0]; |
@@ -113,6 +113,6 @@ module.exports = function (math) { | ||
math.divide(matrix[1][1], d), | ||
math.divide(math.unary(matrix[0][1]), d) | ||
math.divide(math.unaryMinus(matrix[0][1]), d) | ||
], | ||
[ | ||
math.divide(math.unary(matrix[1][0]), d), | ||
math.divide(math.unaryMinus(matrix[1][0]), d), | ||
math.divide(matrix[0][0], d) | ||
@@ -148,2 +148,3 @@ ] | ||
if (r == rows || A[r][c] == 0) { | ||
// TODO: in case of zero det, just return a matrix wih Infinity values? (like octave) | ||
throw Error('Cannot calculate inverse, determinant is zero'); | ||
@@ -165,3 +166,3 @@ } | ||
if (Ar[c] != 0) { | ||
f = math.divide(math.unary(Ar[c]), Ac[c]); | ||
f = math.divide(math.unaryMinus(Ar[c]), Ac[c]); | ||
@@ -168,0 +169,0 @@ // add (f * row c) to row r to eliminate the value |
@@ -1,7 +0,2 @@ | ||
module.exports = function (math, config) { | ||
var Matrix = require('../../type/Matrix'), | ||
collection = require('../../type/collection'); | ||
// TODO: implement BigNumber support for random | ||
module.exports = function (math) { | ||
/** | ||
@@ -12,5 +7,8 @@ * Return a random number between `min` and `max` using a uniform distribution. | ||
* | ||
* math.random() // generate a random number between 0 and 1 | ||
* math.random(max) // generate a random number between 0 and max | ||
* math.random(min, max) // generate a random number between min and max | ||
* math.random() // generate a random number between 0 and 1 | ||
* math.random(max) // generate a random number between 0 and max | ||
* math.random(min, max) // generate a random number between min and max | ||
* math.random(size) // generate a matrix with random numbers between 0 and 1 | ||
* math.random(size, max) // generate a matrix with random numbers between 0 and max | ||
* math.random(size, min, max) // generate a matrix with random numbers between min and max | ||
* | ||
@@ -24,174 +22,14 @@ * Examples: | ||
* math.random(30, 40); // returns a random number between 30 and 40 | ||
* math.random([2, 3]); // returns a 2x3 matrix with random numbers between 0 and 1 | ||
* | ||
* See also: | ||
* | ||
* randomInt, pickRandom, distribution | ||
* | ||
* @param {Number} [size] If provided, an array with `size` number of random values is returned | ||
* @param {Number} [min] Minimum boundary for the random value | ||
* @param {Number} [max] Maximum boundary for the random value | ||
* @return {Number} A random number | ||
* @return {Number | Array | Matrix} A random number | ||
*/ | ||
// TODO: improve structure of random.js, split it in one file per function | ||
// Each distribution is a function that takes no argument and when called returns | ||
// a number between 0 and 1. | ||
var distributions = { | ||
uniform: function() { | ||
return Math.random; | ||
}, | ||
// Implementation of normal distribution using Box-Muller transform | ||
// ref : http://en.wikipedia.org/wiki/Box%E2%80%93Muller_transform | ||
// We take : mean = 0.5, standard deviation = 1/6 | ||
// so that 99.7% values are in [0, 1]. | ||
normal: function() { | ||
return function() { | ||
var u1, u2, | ||
picked = -1; | ||
// We reject values outside of the interval [0, 1] | ||
// TODO: check if it is ok to do that? | ||
while (picked < 0 || picked > 1) { | ||
u1 = Math.random(); | ||
u2 = Math.random(); | ||
picked = 1/6 * Math.pow(-2 * Math.log(u1), 0.5) * Math.cos(2 * Math.PI * u2) + 0.5; | ||
} | ||
return picked; | ||
} | ||
} | ||
}; | ||
/** | ||
* Create a distribution object. | ||
* @param {String} name Name of a distribution. | ||
* Choose from 'uniform', 'normal'. | ||
* @return {Object} distribution A distribution object containing functions: | ||
* random([size, min, max]) | ||
* randomInt([min, max]) | ||
* pickRandom(array) | ||
*/ | ||
math.distribution = function(name) { | ||
if (!distributions.hasOwnProperty(name)) | ||
throw new Error('unknown distribution ' + name); | ||
var args = Array.prototype.slice.call(arguments, 1), | ||
distribution = distributions[name].apply(this, args); | ||
return (function(distribution) { | ||
// This is the public API for all distributions | ||
var randFunctions = { | ||
random: function(arg1, arg2, arg3) { | ||
var size, min, max; | ||
if (arguments.length > 3) { | ||
throw new math.error.ArgumentsError('random', arguments.length, 0, 3); | ||
// `random(max)` or `random(size)` | ||
} else if (arguments.length === 1) { | ||
if (Array.isArray(arg1)) | ||
size = arg1; | ||
else | ||
max = arg1; | ||
// `random(min, max)` or `random(size, max)` | ||
} else if (arguments.length === 2) { | ||
if (Array.isArray(arg1)) | ||
size = arg1; | ||
else { | ||
min = arg1; | ||
max = arg2; | ||
} | ||
// `random(size, min, max)` | ||
} else { | ||
size = arg1; | ||
min = arg2; | ||
max = arg3; | ||
} | ||
if (max === undefined) max = 1; | ||
if (min === undefined) min = 0; | ||
if (size !== undefined) { | ||
var res = _randomDataForMatrix(size, min, max, _random); | ||
return (config.matrix === 'array') ? res : new Matrix(res); | ||
} | ||
else return _random(min, max); | ||
}, | ||
randomInt: function(arg1, arg2, arg3) { | ||
var size, min, max; | ||
if (arguments.length > 3 || arguments.length < 1) | ||
throw new math.error.ArgumentsError('randomInt', arguments.length, 1, 3); | ||
// `randomInt(max)` | ||
else if (arguments.length === 1) max = arg1; | ||
// `randomInt(min, max)` or `randomInt(size, max)` | ||
else if (arguments.length === 2) { | ||
if (Object.prototype.toString.call(arg1) === '[object Array]') | ||
size = arg1; | ||
else { | ||
min = arg1; | ||
max = arg2; | ||
} | ||
// `randomInt(size, min, max)` | ||
} else { | ||
size = arg1; | ||
min = arg2; | ||
max = arg3; | ||
} | ||
if (min === undefined) min = 0; | ||
if (size !== undefined) { | ||
var res = _randomDataForMatrix(size, min, max, _randomInt); | ||
return (config.matrix === 'array') ? res : new Matrix(res); | ||
} | ||
else return _randomInt(min, max); | ||
}, | ||
pickRandom: function(possibles) { | ||
if (arguments.length !== 1) { | ||
throw new math.error.ArgumentsError('pickRandom', arguments.length, 1); | ||
} | ||
if (!Array.isArray(possibles)) { | ||
throw new math.error.UnsupportedTypeError('pickRandom', math['typeof'](possibles)); | ||
} | ||
// TODO: add support for matrices | ||
return possibles[Math.floor(Math.random() * possibles.length)]; | ||
} | ||
}; | ||
var _random = function(min, max) { | ||
return min + distribution() * (max - min); | ||
}; | ||
var _randomInt = function(min, max) { | ||
return Math.floor(min + distribution() * (max - min)); | ||
}; | ||
// This is a function for generating a random matrix recursively. | ||
var _randomDataForMatrix = function(size, min, max, randFunc) { | ||
var data = [], length, i; | ||
size = size.slice(0); | ||
if (size.length > 1) { | ||
for (i = 0, length = size.shift(); i < length; i++) | ||
data.push(_randomDataForMatrix(size, min, max, randFunc)); | ||
} else { | ||
for (i = 0, length = size.shift(); i < length; i++) | ||
data.push(randFunc(min, max)); | ||
} | ||
return data; | ||
}; | ||
return randFunctions; | ||
})(distribution); | ||
}; | ||
// Default random functions use uniform distribution | ||
// TODO: put random functions in separate files? | ||
var uniformRandFunctions = math.distribution('uniform'); | ||
math.random = uniformRandFunctions.random; | ||
math.randomInt = uniformRandFunctions.randomInt; | ||
math.pickRandom = uniformRandFunctions.pickRandom; | ||
math.random = math.distribution('uniform').random; | ||
}; |
@@ -10,3 +10,2 @@ module.exports = function (math) { | ||
deepEqual = util.object.deepEqual, | ||
isNumber = util.number.isNumber, | ||
@@ -16,4 +15,3 @@ isString = util.string.isString, | ||
isComplex = Complex.isComplex, | ||
isUnit = Unit.isUnit, | ||
isCollection = collection.isCollection; | ||
isUnit = Unit.isUnit; | ||
@@ -23,9 +21,5 @@ /** | ||
* | ||
* In case of a matrix or array, the test is done element wise, the | ||
* true and false part can be either a matrix/array with the same size | ||
* of the condition, or a scalar value. | ||
* | ||
* Syntax: | ||
* | ||
* math.ifElse(condition, trueExpr, falseExpr | ||
* math.ifElse(condition, trueExpr, falseExpr) | ||
* | ||
@@ -37,5 +31,4 @@ * Examples: | ||
* math.ifElse(true, 'yes', 'no'); // returns 'yes' | ||
* math.ifElse([4, 6, 0, -1], true, false); // returns [true, true, false, true] | ||
* | ||
* @param {Number | Boolean | String | Complex | BigNumber | Unit | Matrix | Array} condition | ||
* @param {Number | Boolean | String | Complex | BigNumber | Unit} condition | ||
* The conditional expression | ||
@@ -75,61 +68,4 @@ * @param {*} trueExpr The true expression | ||
if (isCollection(condition)) { | ||
return _ifElseCollection(condition, trueExpr, falseExpr); | ||
} | ||
throw new math.error.UnsupportedTypeError('ifElse', math['typeof'](condition)); | ||
}; | ||
/** | ||
* Execute the if-else condition element wise | ||
* @param {Matrix | Array} condition | ||
* @param {*} trueExpr | ||
* @param {*} falseExpr | ||
* @returns {*} | ||
* @private | ||
*/ | ||
function _ifElseCollection(condition, trueExpr, falseExpr) { | ||
var asMatrix = (condition instanceof Matrix) || | ||
(trueExpr instanceof Matrix) || | ||
(falseExpr instanceof Matrix); | ||
// change an array into a matrix | ||
if (!(condition instanceof Matrix)) condition = new Matrix(condition); | ||
// change the true expression into a matrix and check whether the size | ||
// matches with the condition matrix | ||
if (isCollection(trueExpr)) { | ||
if (!(trueExpr instanceof Matrix)) trueExpr = new Matrix(trueExpr); | ||
if (!deepEqual(condition.size(), trueExpr.size())) { | ||
throw new RangeError('Dimension mismatch ([' + | ||
condition.size().join(', ') + '] != [' + | ||
trueExpr.size().join(', ') | ||
+ '])'); | ||
throw new math.error.DimensionError(condition.size(), trueExpr.size()); | ||
} | ||
} | ||
// change the false expression into a matrix and check whether the size | ||
// matches with the condition matrix | ||
if (isCollection(falseExpr)) { | ||
if (!(falseExpr instanceof Matrix)) falseExpr = new Matrix(falseExpr); | ||
if (!deepEqual(condition.size(), falseExpr.size())) { | ||
throw new math.error.DimensionError(condition.size(), falseExpr.size()); | ||
} | ||
} | ||
// do the actual conditional test element wise | ||
var trueIsMatrix = trueExpr instanceof Matrix, | ||
falseIsMatrix = falseExpr instanceof Matrix; | ||
var result = condition.map(function (value, index) { | ||
return math.ifElse(value, | ||
trueIsMatrix ? trueExpr.get(index) : trueExpr, | ||
falseIsMatrix ? falseExpr.get(index) : falseExpr | ||
); | ||
}); | ||
return asMatrix ? result : result.valueOf(); | ||
} | ||
}; |
@@ -167,9 +167,7 @@ var object = require('./util/object'); | ||
require('./function/arithmetic/ceil.js')(math, _config); | ||
require('./function/arithmetic/compare.js')(math, _config); | ||
require('./function/arithmetic/cube.js')(math, _config); | ||
require('./function/arithmetic/divide.js')(math, _config); | ||
require('./function/arithmetic/edivide.js')(math, _config); | ||
require('./function/arithmetic/emultiply.js')(math, _config); | ||
require('./function/arithmetic/epow.js')(math, _config); | ||
require('./function/arithmetic/equal.js')(math, _config); | ||
require('./function/arithmetic/dotDivide.js')(math, _config); | ||
require('./function/arithmetic/dotMultiply.js')(math, _config); | ||
require('./function/arithmetic/dotPow.js')(math, _config); | ||
require('./function/arithmetic/exp.js')(math, _config); | ||
@@ -179,4 +177,2 @@ require('./function/arithmetic/fix.js')(math, _config); | ||
require('./function/arithmetic/gcd.js')(math, _config); | ||
require('./function/arithmetic/larger.js')(math, _config); | ||
require('./function/arithmetic/largereq.js')(math, _config); | ||
require('./function/arithmetic/lcm.js')(math, _config); | ||
@@ -191,11 +187,19 @@ require('./function/arithmetic/log.js')(math, _config); | ||
require('./function/arithmetic/sign.js')(math, _config); | ||
require('./function/arithmetic/smaller.js')(math, _config); | ||
require('./function/arithmetic/smallereq.js')(math, _config); | ||
require('./function/arithmetic/sqrt.js')(math, _config); | ||
require('./function/arithmetic/square.js')(math, _config); | ||
require('./function/arithmetic/subtract.js')(math, _config); | ||
require('./function/arithmetic/unary.js')(math, _config); | ||
require('./function/arithmetic/unequal.js')(math, _config); | ||
require('./function/arithmetic/unaryMinus.js')(math, _config); | ||
require('./function/arithmetic/unaryPlus.js')(math, _config); | ||
require('./function/arithmetic/xgcd.js')(math, _config); | ||
// functions - comparison | ||
require('./function/comparison/compare.js')(math, _config); | ||
require('./function/comparison/deepEqual.js')(math, _config); | ||
require('./function/comparison/equal.js')(math, _config); | ||
require('./function/comparison/larger.js')(math, _config); | ||
require('./function/comparison/largerEq.js')(math, _config); | ||
require('./function/comparison/smaller.js')(math, _config); | ||
require('./function/comparison/smallerEq.js')(math, _config); | ||
require('./function/comparison/unequal.js')(math, _config); | ||
// functions - complex | ||
@@ -235,4 +239,7 @@ require('./function/complex/arg.js')(math, _config); | ||
// functions - probability | ||
require('./function/probability/distribution.js')(math, _config); | ||
require('./function/probability/factorial.js')(math, _config); | ||
require('./function/probability/random.js')(math, _config); | ||
require('./function/probability/randomInt.js')(math, _config); | ||
require('./function/probability/pickRandom.js')(math, _config); | ||
require('./function/probability/permutations.js')(math, _config); | ||
@@ -239,0 +246,0 @@ require('./function/probability/combinations.js')(math, _config); |
@@ -675,2 +675,4 @@ var util = require('../util/index'), | ||
//{name: 'cup', base: BASE_UNITS.VOLUME, prefixes: PREFIXES.NONE, value: 0.000240, offset: 0}, // 240 mL // not possible, we have already another cup | ||
drop: {name: 'drop', base: BASE_UNITS.VOLUME, prefixes: PREFIXES.NONE, value: 5e-8, offset: 0}, // 0.05 mL = 5e-8 m3 | ||
gtt: {name: 'gtt', base: BASE_UNITS.VOLUME, prefixes: PREFIXES.NONE, value: 5e-8, offset: 0}, // 0.05 mL = 5e-8 m3 | ||
@@ -809,2 +811,3 @@ // Liquid volume | ||
hogsheads: 'hogshead', | ||
gtts: 'gtt', | ||
@@ -811,0 +814,0 @@ grams: 'gram', |
@@ -100,3 +100,4 @@ var ArrayNode = require('../expression/node/ArrayNode'), | ||
xgcd: false, | ||
unary: false, | ||
unaryMinus: false, | ||
unaryPlus: false, | ||
@@ -310,3 +311,3 @@ // complex | ||
case 'largereq': | ||
case 'largerEq': | ||
op = '>='; | ||
@@ -319,3 +320,3 @@ break; | ||
case 'smallereq': | ||
case 'smallerEq': | ||
op = '<='; | ||
@@ -322,0 +323,0 @@ break; |
@@ -122,3 +122,3 @@ /** | ||
if (options !== undefined) { | ||
if (options) { | ||
// determine notation from options | ||
@@ -125,0 +125,0 @@ if (options.notation) { |
{ | ||
"name": "mathjs", | ||
"version": "0.22.0", | ||
"version": "0.23.0", | ||
"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.", | ||
@@ -44,3 +44,3 @@ "author": "Jos de Jong <wjosdejong@gmail.com> (https://github.com/josdejong)", | ||
"dependencies": { | ||
"decimal.js": "~2.0.1" | ||
"decimal.js": "~3.0.0" | ||
}, | ||
@@ -47,0 +47,0 @@ "devDependencies": { |
Sorry, the diff of this file is too big to display
Sorry, the diff of this file is not supported yet
Sorry, the diff of this file is too big to display
Sorry, the diff of this file is not supported yet
License Policy Violation
LicenseThis package is not allowed per your license policy. Review the package's license to ensure compliance.
Found 1 instance in 1 package
License Policy Violation
LicenseThis package is not allowed per your license policy. Review the package's license to ensure compliance.
Found 1 instance in 1 package
Uses eval
Supply chain riskPackage uses dynamic code execution (e.g., eval()), which is a dangerous practice. This can prevent the code from running in certain environments and increases the risk that the code may contain exploits or malicious behavior.
Found 1 instance in 1 package
Long strings
Supply chain riskContains long string literals, which may be a sign of obfuscated or packed code.
Found 1 instance in 1 package
Minified code
QualityThis package contains minified code. This may be harmless in some cases where minified code is included in packaged libraries, however packages on npm should not minify code.
Found 1 instance in 1 package
1986814
420
41666
1
3
2
+ Addeddecimal.js@3.0.1(transitive)
- Removeddecimal.js@2.0.3(transitive)
Updateddecimal.js@~3.0.0