From ced18ecfca69a9facba9aa55096b81b6e158d445 Mon Sep 17 00:00:00 2001 From: jos Date: Mon, 9 Jun 2014 14:07:21 +0200 Subject: [PATCH] Implemented functions dotEqual and dotUnequal, renamed functions smallereq to smallerEq and largereq to largerEq (all functions are now camelcase) --- HISTORY.md | 13 +- docs/datatypes/numbers.md | 2 +- docs/functions.md | 14 +- docs/reference/functions/compare.md | 4 +- .../functions/{dotdivide.md => dotDivide.md} | 10 +- docs/reference/functions/dotEqual.md | 49 ++++++ .../{dotmultiply.md => dotMultiply.md} | 10 +- .../functions/{dotpow.md => dotPow.md} | 8 +- docs/reference/functions/dotUnequal.md | 49 ++++++ docs/reference/functions/equal.md | 10 +- docs/reference/functions/larger.md | 4 +- .../functions/{largereq.md => largerEq.md} | 8 +- docs/reference/functions/smaller.md | 4 +- .../functions/{smallereq.md => smallerEq.md} | 8 +- docs/reference/functions/unary.md | 45 ------ .../{unaryminus.md => unaryMinus.md} | 10 +- .../functions/{unaryplus.md => unaryPlus.md} | 10 +- docs/reference/functions/unequal.md | 10 +- .../docs/function/arithmetic/compare.js | 2 +- .../arithmetic/{dotdivide.js => dotDivide.js} | 6 +- .../{dotmultiply.js => dotMultiply.js} | 6 +- .../arithmetic/{dotpow.js => dotPow.js} | 0 .../docs/function/arithmetic/equal.js | 2 +- .../docs/function/arithmetic/larger.js | 2 +- .../arithmetic/{largereq.js => largerEq.js} | 6 +- .../docs/function/arithmetic/smaller.js | 2 +- .../arithmetic/{smallereq.js => smallerEq.js} | 6 +- .../{unaryminus.js => unaryMinus.js} | 6 +- .../arithmetic/{unaryplus.js => unaryPlus.js} | 6 +- .../docs/function/arithmetic/unequal.js | 2 +- lib/expression/docs/index.js | 15 +- lib/expression/parse.js | 41 +++-- lib/function/arithmetic/compare.js | 2 +- .../arithmetic/{dotdivide.js => dotDivide.js} | 14 +- lib/function/arithmetic/dotEqual.js | 40 +++++ .../{dotmultiply.js => dotMultiply.js} | 14 +- .../arithmetic/{dotpow.js => dotPow.js} | 12 +- lib/function/arithmetic/dotUnequal.js | 40 +++++ lib/function/arithmetic/equal.js | 41 ++++- lib/function/arithmetic/larger.js | 2 +- .../arithmetic/{largereq.js => largerEq.js} | 27 ++-- lib/function/arithmetic/smaller.js | 2 +- .../arithmetic/{smallereq.js => smallerEq.js} | 27 ++-- .../{unaryminus.js => unaryMinus.js} | 20 +-- .../arithmetic/{unaryplus.js => unaryPlus.js} | 16 +- lib/function/arithmetic/unequal.js | 42 ++++- lib/function/matrix/det.js | 4 +- lib/function/matrix/inv.js | 6 +- lib/function/probability/distribution.js | 2 +- lib/math.js | 16 +- lib/util/latex.js | 7 +- test/deprecated.test.js | 14 ++ test/expression/node/OperatorNode.test.js | 4 +- test/expression/parse.test.js | 24 ++- test/function/arithmetic/dotdivide.test.js | 68 ++++----- test/function/arithmetic/dotequal.test.js | 40 +++++ test/function/arithmetic/dotmultiply.test.js | 78 +++++----- test/function/arithmetic/dotpow.test.js | 122 +++++++-------- test/function/arithmetic/dotunequal.test.js | 41 +++++ test/function/arithmetic/equal.test.js | 30 +++- test/function/arithmetic/largereq.test.js | 136 ++++++++--------- test/function/arithmetic/smallereq.test.js | 144 +++++++++--------- test/function/arithmetic/unaryminus.test.js | 50 +++--- test/function/arithmetic/unaryplus.test.js | 50 +++--- test/function/arithmetic/unequal.test.js | 31 +++- test/function/complex/arg.test.js | 4 +- tools/validate.js | 4 +- 67 files changed, 966 insertions(+), 578 deletions(-) rename docs/reference/functions/{dotdivide.md => dotDivide.md} (84%) create mode 100644 docs/reference/functions/dotEqual.md rename docs/reference/functions/{dotmultiply.md => dotMultiply.md} (84%) rename docs/reference/functions/{dotpow.md => dotPow.md} (85%) create mode 100644 docs/reference/functions/dotUnequal.md rename docs/reference/functions/{largereq.md => largerEq.md} (91%) rename docs/reference/functions/{smallereq.md => smallerEq.md} (91%) delete mode 100644 docs/reference/functions/unary.md rename docs/reference/functions/{unaryminus.md => unaryMinus.md} (85%) rename docs/reference/functions/{unaryplus.md => unaryPlus.md} (85%) rename lib/expression/docs/function/arithmetic/{dotdivide.js => dotDivide.js} (81%) rename lib/expression/docs/function/arithmetic/{dotmultiply.js => dotMultiply.js} (81%) rename lib/expression/docs/function/arithmetic/{dotpow.js => dotPow.js} (100%) rename lib/expression/docs/function/arithmetic/{largereq.js => largerEq.js} (75%) rename lib/expression/docs/function/arithmetic/{smallereq.js => smallerEq.js} (75%) rename lib/expression/docs/function/arithmetic/{unaryminus.js => unaryMinus.js} (76%) rename lib/expression/docs/function/arithmetic/{unaryplus.js => unaryPlus.js} (72%) rename lib/function/arithmetic/{dotdivide.js => dotDivide.js} (72%) create mode 100644 lib/function/arithmetic/dotEqual.js rename lib/function/arithmetic/{dotmultiply.js => dotMultiply.js} (74%) rename lib/function/arithmetic/{dotpow.js => dotPow.js} (75%) create mode 100644 lib/function/arithmetic/dotUnequal.js rename lib/function/arithmetic/{largereq.js => largerEq.js} (80%) rename lib/function/arithmetic/{smallereq.js => smallerEq.js} (80%) rename lib/function/arithmetic/{unaryminus.js => unaryMinus.js} (77%) rename lib/function/arithmetic/{unaryplus.js => unaryPlus.js} (81%) create mode 100644 test/function/arithmetic/dotequal.test.js create mode 100644 test/function/arithmetic/dotunequal.test.js diff --git a/HISTORY.md b/HISTORY.md index 618e63e79..db69e2c6f 100644 --- a/HISTORY.md +++ b/HISTORY.md @@ -4,10 +4,15 @@ https://github.com/josdejong/mathjs ## not yet released, version 0.23.0 -- Renamed functions `edivide`, `emultiply`, and `epow` to `dotdivide`, - `dotmultiply`, and `dotpow` respectively. -- Renamed function `unary` to `unaryminus` and added support for strings. -- Implemented function `unaryplus` and unary plus operator. +- 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. +- Implemented function `unaryPlus` and unary plus operator. +- Matrix comparison of `equal` and `unequal` are now deep comparisons, + element wise comparisons can be done with the new explicit functions + `dotEqual` and `dotUnequal`. - 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`). diff --git a/docs/datatypes/numbers.md b/docs/datatypes/numbers.md index c6895efa9..a43c67a08 100644 --- a/docs/datatypes/numbers.md +++ b/docs/datatypes/numbers.md @@ -97,4 +97,4 @@ console.log(math.equal(3e-20, 3.1e-20)); // true ``` The available comparison functions are: `compare`, `equal`, `larger`, -`largereq`, `smaller`, `smallereq`, `unequal`. +`largerEq`, `smaller`, `smallerEq`, `unequal`. diff --git a/docs/functions.md b/docs/functions.md index 2fd105736..6a2feb17b 100644 --- a/docs/functions.md +++ b/docs/functions.md @@ -29,16 +29,18 @@ math.add('hello ', 'world!'); // String 'hello world!' - math.compare(x, y) - math.cube(x) - math.divide(x, y) -- math.dotdivide(x, y) -- math.dotmultiply(x, y) -- math.dotpow(x, y) +- math.dotDivide(x, y) +- math.dotEqual(x, y) +- math.dotMultiply(x, y) +- math.dotPow(x, y) +- math.dotUnequal(x, y) - math.equal(x) - math.exp(x) - math.fix(x) - math.floor(x) - math.gcd(a, b, c, ...) - math.larger(x, y) -- math.largereq(x, y) +- math.largerEq(x, y) - math.lcm(a, b, c, ...) - math.log(x [, base]) - math.log10(x) @@ -49,11 +51,11 @@ math.add('hello ', 'world!'); // String 'hello world!' - math.round(x [, n]) - math.sign() - math.smaller(x, y) -- math.smallereq(x, y) +- math.smallerEq(x, y) - math.subtract(x, y) - math.sqrt(x) - math.square(x) -- math.unaryminus(x) +- math.unaryMinus(x) - math.unequal(x) - math.xgcd(a, b) diff --git a/docs/reference/functions/compare.md b/docs/reference/functions/compare.md index e88119a1a..72ed50c37 100644 --- a/docs/reference/functions/compare.md +++ b/docs/reference/functions/compare.md @@ -51,9 +51,9 @@ math.compare(2, [1, 2, 3]); // returns [1, 0, -1] [equal](equal.md), [unequal](unequal.md), [smaller](smaller.md), -[smallereq](smallereq.md), +[smallerEq](smallerEq.md), [larger](larger.md), -[largereq](largereq.md) +[largerEq](largerEq.md) diff --git a/docs/reference/functions/dotdivide.md b/docs/reference/functions/dotDivide.md similarity index 84% rename from docs/reference/functions/dotdivide.md rename to docs/reference/functions/dotDivide.md index 494e723c1..c77ab8f5c 100644 --- a/docs/reference/functions/dotdivide.md +++ b/docs/reference/functions/dotDivide.md @@ -1,4 +1,4 @@ -# Function dotdivide +# Function dotDivide Divide two matrices element wise. The function accepts both matrices and scalar values. @@ -7,7 +7,7 @@ scalar values. ## Syntax ```js -math.dotdivide(x, y) +math.dotDivide(x, y) ``` ### Parameters @@ -29,12 +29,12 @@ Number | BigNumber | Complex | Unit | Array | Matrix | ```js var math = mathjs(); -math.dotdivide(2, 4); // returns 0.5 +math.dotDivide(2, 4); // returns 0.5 a = [[9, 5], [6, 1]]; b = [[3, 2], [5, 2]]; -math.dotdivide(a, b); // returns [[3, 2.5], [1.2, 0.5]] +math.dotDivide(a, b); // returns [[3, 2.5], [1.2, 0.5]] math.divide(a, b); // returns [[1.75, 0.75], [-1.75, 2.25]] ``` @@ -43,7 +43,7 @@ math.divide(a, b); // returns [[1.75, 0.75], [-1.75, 2.25]] [divide](divide.md), [multiply](multiply.md), -[dotmultiply](dotmultiply.md) +[dotMultiply](dotMultiply.md) diff --git a/docs/reference/functions/dotEqual.md b/docs/reference/functions/dotEqual.md new file mode 100644 index 000000000..7982d61e5 --- /dev/null +++ b/docs/reference/functions/dotEqual.md @@ -0,0 +1,49 @@ +# Function dotEqual + +Compare two matrices element wise. The function accepts both matrices and +scalar values. + + +## Syntax + +```js +math.dotEqual(x, y) +``` + +### Parameters + +Parameter | Type | Description +--------- | ---- | ----------- +`x` | Number | BigNumber | Boolean | Complex | Unit | Array | Matrix | First matrix to compare +`y` | Number | BigNumber | Boolean | Complex | Unit | Array | Matrix | Second matrix to compare + +### Returns + +Type | Description +---- | ----------- +Number | BigNumber | Complex | Unit | Array | Matrix | Returns a matrix containing boolean results of the element wise comparisons. + + +## Examples + +```js +var math = mathjs(); + +math.dotEqual(2, 4); // returns false + +a = [2, 5, 1]; +b = [2, 7, 1]; + +math.dotEqual(a, b); // returns [true, false, true] +math.equal(a, b); // returns false +``` + + +## See also + +[equal](equal.md), +[unequal](unequal.md), +[dotuneuqal](dotuneuqal.md) + + + diff --git a/docs/reference/functions/dotmultiply.md b/docs/reference/functions/dotMultiply.md similarity index 84% rename from docs/reference/functions/dotmultiply.md rename to docs/reference/functions/dotMultiply.md index cd76d56ae..39366ce2c 100644 --- a/docs/reference/functions/dotmultiply.md +++ b/docs/reference/functions/dotMultiply.md @@ -1,4 +1,4 @@ -# Function dotmultiply +# Function dotMultiply Multiply two matrices element wise. The function accepts both matrices and scalar values. @@ -7,7 +7,7 @@ scalar values. ## Syntax ```js -math.dotmultiply(x, y) +math.dotMultiply(x, y) ``` ### Parameters @@ -29,12 +29,12 @@ Number | BigNumber | Complex | Unit | Array | Matrix | ```js var math = mathjs(); -math.dotmultiply(2, 4); // returns 8 +math.dotMultiply(2, 4); // returns 8 a = [[9, 5], [6, 1]]; b = [[3, 2], [5, 2]]; -math.dotmultiply(a, b); // returns [[27, 10], [30, 2]] +math.dotMultiply(a, b); // returns [[27, 10], [30, 2]] math.multiply(a, b); // returns [[52, 28], [23, 14]] ``` @@ -43,7 +43,7 @@ math.multiply(a, b); // returns [[52, 28], [23, 14]] [multiply](multiply.md), [divide](divide.md), -[dotdivide](dotdivide.md) +[dotDivide](dotDivide.md) diff --git a/docs/reference/functions/dotpow.md b/docs/reference/functions/dotPow.md similarity index 85% rename from docs/reference/functions/dotpow.md rename to docs/reference/functions/dotPow.md index edb59a8fe..478258ab3 100644 --- a/docs/reference/functions/dotpow.md +++ b/docs/reference/functions/dotPow.md @@ -1,4 +1,4 @@ -# Function dotpow +# Function dotPow Calculates the power of x to y element wise. @@ -6,7 +6,7 @@ Calculates the power of x to y element wise. ## Syntax ```js -math.dotpow(x, y) +math.dotPow(x, y) ``` ### Parameters @@ -28,10 +28,10 @@ Number | BigNumber | Complex | Unit | Array | Matrix | ```js var math = mathjs(); -math.dotpow(2, 3); // returns Number 8 +math.dotPow(2, 3); // returns Number 8 var a = [[1, 2], [4, 3]]; -math.dotpow(a, 2); // returns Array [[1, 4], [16, 9]] +math.dotPow(a, 2); // returns Array [[1, 4], [16, 9]] math.pow(a, 2); // returns Array [[9, 8], [16, 17]] ``` diff --git a/docs/reference/functions/dotUnequal.md b/docs/reference/functions/dotUnequal.md new file mode 100644 index 000000000..befcf8746 --- /dev/null +++ b/docs/reference/functions/dotUnequal.md @@ -0,0 +1,49 @@ +# Function dotUnequal + +Test element wise whether two matrices are unequal. +The function accepts both matrices and scalar values. + + +## Syntax + +```js +math.dotUnequal(x, y) +``` + +### Parameters + +Parameter | Type | Description +--------- | ---- | ----------- +`x` | Number | BigNumber | Boolean | Complex | Unit | Array | Matrix | First matrix to compare +`y` | Number | BigNumber | Boolean | Complex | Unit | Array | Matrix | Second matrix to compare + +### Returns + +Type | Description +---- | ----------- +Number | BigNumber | Complex | Unit | Array | Matrix | Returns a matrix containing boolean results of the element wise comparisons. + + +## Examples + +```js +var math = mathjs(); + +math.dotUnequal(2, 4); // returns true + +a = [2, 5, 1]; +b = [2, 7, 1]; + +math.dotUnequal(a, b); // returns [false, true, false] +math.unequal(a, b); // returns true +``` + + +## See also + +[equal](equal.md), +[unequal](unequal.md), +[dotEqual](dotEqual.md) + + + diff --git a/docs/reference/functions/equal.md b/docs/reference/functions/equal.md index 99d04ca77..6262a9d19 100644 --- a/docs/reference/functions/equal.md +++ b/docs/reference/functions/equal.md @@ -6,7 +6,8 @@ The function tests whether the relative difference between x and y is smaller than the configured epsilon. The function cannot be used to compare values smaller than approximately 2.22e-16. -For matrices, the function is evaluated element wise. +For matrices, the function tests whether the size and all elements of the +matrices are equal (a deep comparison). In case of complex numbers, x.re must equal y.re, and x.im must equal y.im. @@ -48,10 +49,11 @@ math.equal(a, b); // returns true [unequal](unequal.md), [smaller](smaller.md), -[smallereq](smallereq.md), +[smallerEq](smallerEq.md), [larger](larger.md), -[largereq](largereq.md), -[compare](compare.md) +[largerEq](largerEq.md), +[compare](compare.md), +[dotEqual](dotEqual.md) diff --git a/docs/reference/functions/larger.md b/docs/reference/functions/larger.md index 88f675339..cbc8e093e 100644 --- a/docs/reference/functions/larger.md +++ b/docs/reference/functions/larger.md @@ -48,8 +48,8 @@ math.larger(a, b); // returns false [equal](equal.md), [unequal](unequal.md), [smaller](smaller.md), -[smallereq](smallereq.md), -[largereq](largereq.md), +[smallerEq](smallerEq.md), +[largerEq](largerEq.md), [compare](compare.md) diff --git a/docs/reference/functions/largereq.md b/docs/reference/functions/largerEq.md similarity index 91% rename from docs/reference/functions/largereq.md rename to docs/reference/functions/largerEq.md index 10dd2f305..809f17080 100644 --- a/docs/reference/functions/largereq.md +++ b/docs/reference/functions/largerEq.md @@ -1,4 +1,4 @@ -# Function largereq +# Function largerEq Test whether value x is larger or equal to y. @@ -12,7 +12,7 @@ For matrices, the function is evaluated element wise. ## Syntax ```js -math.largereq(x, y) +math.largerEq(x, y) ``` ### Parameters @@ -35,7 +35,7 @@ Boolean | Array | Matrix | Returns true when the x is larger or equal var math = mathjs(); math.larger(2, 1 + 1); // returns false -math.largereq(2, 1 + 1); // returns true +math.largerEq(2, 1 + 1); // returns true ``` @@ -44,7 +44,7 @@ math.largereq(2, 1 + 1); // returns true [equal](equal.md), [unequal](unequal.md), [smaller](smaller.md), -[smallereq](smallereq.md), +[smallerEq](smallerEq.md), [larger](larger.md), [compare](compare.md) diff --git a/docs/reference/functions/smaller.md b/docs/reference/functions/smaller.md index 3f1caf943..84370e2e2 100644 --- a/docs/reference/functions/smaller.md +++ b/docs/reference/functions/smaller.md @@ -47,9 +47,9 @@ math.smaller(a, b); // returns true [equal](equal.md), [unequal](unequal.md), -[smallereq](smallereq.md), +[smallerEq](smallerEq.md), [larger](larger.md), -[largereq](largereq.md), +[largerEq](largerEq.md), [compare](compare.md) diff --git a/docs/reference/functions/smallereq.md b/docs/reference/functions/smallerEq.md similarity index 91% rename from docs/reference/functions/smallereq.md rename to docs/reference/functions/smallerEq.md index a9d57ef16..aa6f9394a 100644 --- a/docs/reference/functions/smallereq.md +++ b/docs/reference/functions/smallerEq.md @@ -1,4 +1,4 @@ -# Function smallereq +# Function smallerEq Test whether value x is smaller or equal to y. @@ -11,7 +11,7 @@ For matrices, the function is evaluated element wise. ## Syntax ```js -math.smallereq(x, y) +math.smallerEq(x, y) ``` ### Parameters @@ -34,7 +34,7 @@ Boolean | Array | Matrix | Returns true when the x is smaller than y, var math = mathjs(); math.smaller(1 + 2, 3); // returns false -math.smallereq(1 + 2, 3); // returns true +math.smallerEq(1 + 2, 3); // returns true ``` @@ -44,7 +44,7 @@ math.smallereq(1 + 2, 3); // returns true [unequal](unequal.md), [smaller](smaller.md), [larger](larger.md), -[largereq](largereq.md), +[largerEq](largerEq.md), [compare](compare.md) diff --git a/docs/reference/functions/unary.md b/docs/reference/functions/unary.md deleted file mode 100644 index 1e9c3039a..000000000 --- a/docs/reference/functions/unary.md +++ /dev/null @@ -1,45 +0,0 @@ -# Function unary - -Inverse the sign of a value, apply a unary minus operation. - -For matrices, the function is evaluated element wise. Boolean values will -be converted to a number. For complex numbers, both real and complex -value are inverted. - - -## Syntax - -```js -math.unary(x) -``` - -### Parameters - -Parameter | Type | Description ---------- | ---- | ----------- -`x` | Number | BigNumber | Boolean | Complex | Unit | Array | Matrix | Number to be inverted. - -### Returns - -Type | Description ----- | ----------- -Number | BigNumber | Complex | Unit | Array | Matrix | Returns the value with inverted sign. - - -## Examples - -```js -var math = mathjs(); - -math.unary(3.5); // returns -3.5 -math.unary(-4.2); // returns 4.2 -``` - - -## See also - -[add](add.md), -[subtract](subtract.md) - - - diff --git a/docs/reference/functions/unaryminus.md b/docs/reference/functions/unaryMinus.md similarity index 85% rename from docs/reference/functions/unaryminus.md rename to docs/reference/functions/unaryMinus.md index c44bf7000..320fb49dc 100644 --- a/docs/reference/functions/unaryminus.md +++ b/docs/reference/functions/unaryMinus.md @@ -1,4 +1,4 @@ -# Function unaryminus +# Function unaryMinus Inverse the sign of a value, apply a unary minus operation. @@ -10,7 +10,7 @@ complex value are inverted. ## Syntax ```js -math.unaryminus(x) +math.unaryMinus(x) ``` ### Parameters @@ -31,8 +31,8 @@ Number | BigNumber | Complex | Unit | Array | Matrix | ```js var math = mathjs(); -math.unaryminus(3.5); // returns -3.5 -math.unaryminus(-4.2); // returns 4.2 +math.unaryMinus(3.5); // returns -3.5 +math.unaryMinus(-4.2); // returns 4.2 ``` @@ -40,7 +40,7 @@ math.unaryminus(-4.2); // returns 4.2 [add](add.md), [subtract](subtract.md), -[unaryplus](unaryplus.md) +[unaryPlus](unaryPlus.md) diff --git a/docs/reference/functions/unaryplus.md b/docs/reference/functions/unaryPlus.md similarity index 85% rename from docs/reference/functions/unaryplus.md rename to docs/reference/functions/unaryPlus.md index 58ef180cc..55d1261d2 100644 --- a/docs/reference/functions/unaryplus.md +++ b/docs/reference/functions/unaryPlus.md @@ -1,4 +1,4 @@ -# Function unaryplus +# Function unaryPlus Unary plus operation. Boolean values and strings will be converted to a number, numeric values will be returned as is. @@ -9,7 +9,7 @@ For matrices, the function is evaluated element wise. ## Syntax ```js -math.unaryplus(x) +math.unaryPlus(x) ``` ### Parameters @@ -30,14 +30,14 @@ Number | BigNumber | Complex | Unit | Array | Matrix | ```js var math = mathjs(); -math.unaryplus(3.5); // returns 3.5 -math.unaryplus(1); // returns 1 +math.unaryPlus(3.5); // returns 3.5 +math.unaryPlus(1); // returns 1 ``` ## See also -[unaryminus](unaryminus.md), +[unaryMinus](unaryMinus.md), [add](add.md), [subtract](subtract.md) diff --git a/docs/reference/functions/unequal.md b/docs/reference/functions/unequal.md index 491cbfea7..37d8af2f6 100644 --- a/docs/reference/functions/unequal.md +++ b/docs/reference/functions/unequal.md @@ -6,7 +6,8 @@ The function tests whether the relative difference between x and y is larger than the configured epsilon. The function cannot be used to compare values smaller than approximately 2.22e-16. -For matrices, the function is evaluated element wise. +For matrices, the function tests whether the size or any of the elements of the +matrices are unequal (a deep comparison). In case of complex numbers, x.re must unequal y.re, or x.im must unequal y.im. @@ -48,10 +49,11 @@ math.unequal(a, b); // returns false [equal](equal.md), [smaller](smaller.md), -[smallereq](smallereq.md), +[smallerEq](smallerEq.md), [larger](larger.md), -[largereq](largereq.md), -[compare](compare.md) +[largerEq](largerEq.md), +[compare](compare.md), +[dotUnequal](dotUnequal.md) diff --git a/lib/expression/docs/function/arithmetic/compare.js b/lib/expression/docs/function/arithmetic/compare.js index ff5434cba..a6388d712 100644 --- a/lib/expression/docs/function/arithmetic/compare.js +++ b/lib/expression/docs/function/arithmetic/compare.js @@ -14,6 +14,6 @@ module.exports = { 'compare(2, [1, 2, 3])' ], 'seealso': [ - 'equal', 'unequal', 'smaller', 'smallereq', 'largereq' + 'equal', 'unequal', 'smaller', 'smallerEq', 'largerEq' ] }; diff --git a/lib/expression/docs/function/arithmetic/dotdivide.js b/lib/expression/docs/function/arithmetic/dotDivide.js similarity index 81% rename from lib/expression/docs/function/arithmetic/dotdivide.js rename to lib/expression/docs/function/arithmetic/dotDivide.js index d4e316887..8ffd0688e 100644 --- a/lib/expression/docs/function/arithmetic/dotdivide.js +++ b/lib/expression/docs/function/arithmetic/dotDivide.js @@ -1,9 +1,9 @@ module.exports = { - 'name': 'dotdivide', + 'name': 'dotDivide', 'category': 'Operators', 'syntax': [ 'x ./ y', - 'dotdivide(x, y)' + 'dotDivide(x, y)' ], 'description': 'Divide two values element wise.', 'examples': [ @@ -13,7 +13,7 @@ module.exports = { ], 'seealso': [ 'multiply', - 'dotmultiply', + 'dotMultiply', 'divide' ] }; diff --git a/lib/expression/docs/function/arithmetic/dotmultiply.js b/lib/expression/docs/function/arithmetic/dotMultiply.js similarity index 81% rename from lib/expression/docs/function/arithmetic/dotmultiply.js rename to lib/expression/docs/function/arithmetic/dotMultiply.js index e09ab3a2c..35854790a 100644 --- a/lib/expression/docs/function/arithmetic/dotmultiply.js +++ b/lib/expression/docs/function/arithmetic/dotMultiply.js @@ -1,9 +1,9 @@ module.exports = { - 'name': 'dotmultiply', + 'name': 'dotMultiply', 'category': 'Operators', 'syntax': [ 'x .* y', - 'dotmultiply(x, y)' + 'dotMultiply(x, y)' ], 'description': 'Multiply two values element wise.', 'examples': [ @@ -14,6 +14,6 @@ module.exports = { 'seealso': [ 'multiply', 'divide', - 'dotdivide' + 'dotDivide' ] }; diff --git a/lib/expression/docs/function/arithmetic/dotpow.js b/lib/expression/docs/function/arithmetic/dotPow.js similarity index 100% rename from lib/expression/docs/function/arithmetic/dotpow.js rename to lib/expression/docs/function/arithmetic/dotPow.js diff --git a/lib/expression/docs/function/arithmetic/equal.js b/lib/expression/docs/function/arithmetic/equal.js index 752f69d23..a1cba1bcf 100644 --- a/lib/expression/docs/function/arithmetic/equal.js +++ b/lib/expression/docs/function/arithmetic/equal.js @@ -16,6 +16,6 @@ module.exports = { '50cm == 0.5m' ], 'seealso': [ - 'unequal', 'smaller', 'larger', 'smallereq', 'largereq', 'compare' + 'unequal', 'smaller', 'larger', 'smallerEq', 'largerEq', 'compare' ] }; diff --git a/lib/expression/docs/function/arithmetic/larger.js b/lib/expression/docs/function/arithmetic/larger.js index 752edad73..18870aefc 100644 --- a/lib/expression/docs/function/arithmetic/larger.js +++ b/lib/expression/docs/function/arithmetic/larger.js @@ -17,6 +17,6 @@ module.exports = { '5 cm > 2 inch' ], 'seealso': [ - 'equal', 'unequal', 'smaller', 'smallereq', 'largereq', 'compare' + 'equal', 'unequal', 'smaller', 'smallerEq', 'largerEq', 'compare' ] }; diff --git a/lib/expression/docs/function/arithmetic/largereq.js b/lib/expression/docs/function/arithmetic/largerEq.js similarity index 75% rename from lib/expression/docs/function/arithmetic/largereq.js rename to lib/expression/docs/function/arithmetic/largerEq.js index 790c945f9..5aa1322c1 100644 --- a/lib/expression/docs/function/arithmetic/largereq.js +++ b/lib/expression/docs/function/arithmetic/largerEq.js @@ -1,9 +1,9 @@ module.exports = { - 'name': 'largereq', + 'name': 'largerEq', 'category': 'Operators', 'syntax': [ 'x >= y', - 'largereq(x, y)' + 'largerEq(x, y)' ], 'description': 'Check if value x is larger or equal to y. Returns true if x is larger or equal to y, and false if not.', @@ -15,6 +15,6 @@ module.exports = { '(a > b)' ], 'seealso': [ - 'equal', 'unequal', 'smallereq', 'smaller', 'largereq', 'compare' + 'equal', 'unequal', 'smallerEq', 'smaller', 'largerEq', 'compare' ] }; diff --git a/lib/expression/docs/function/arithmetic/smaller.js b/lib/expression/docs/function/arithmetic/smaller.js index 529a099df..902876127 100644 --- a/lib/expression/docs/function/arithmetic/smaller.js +++ b/lib/expression/docs/function/arithmetic/smaller.js @@ -16,6 +16,6 @@ module.exports = { '5 cm < 2 inch' ], 'seealso': [ - 'equal', 'unequal', 'larger', 'smallereq', 'largereq', 'compare' + 'equal', 'unequal', 'larger', 'smallerEq', 'largerEq', 'compare' ] }; diff --git a/lib/expression/docs/function/arithmetic/smallereq.js b/lib/expression/docs/function/arithmetic/smallerEq.js similarity index 75% rename from lib/expression/docs/function/arithmetic/smallereq.js rename to lib/expression/docs/function/arithmetic/smallerEq.js index 57848b89d..2cbca30b2 100644 --- a/lib/expression/docs/function/arithmetic/smallereq.js +++ b/lib/expression/docs/function/arithmetic/smallerEq.js @@ -1,9 +1,9 @@ module.exports = { - 'name': 'smallereq', + 'name': 'smallerEq', 'category': 'Operators', 'syntax': [ 'x <= y', - 'smallereq(x, y)' + 'smallerEq(x, y)' ], 'description': 'Check if value x is smaller or equal to value y. Returns true if x is smaller than y, and false if not.', @@ -15,6 +15,6 @@ module.exports = { '(a < b)' ], 'seealso': [ - 'equal', 'unequal', 'larger', 'smaller', 'largereq', 'compare' + 'equal', 'unequal', 'larger', 'smaller', 'largerEq', 'compare' ] }; diff --git a/lib/expression/docs/function/arithmetic/unaryminus.js b/lib/expression/docs/function/arithmetic/unaryMinus.js similarity index 76% rename from lib/expression/docs/function/arithmetic/unaryminus.js rename to lib/expression/docs/function/arithmetic/unaryMinus.js index a0b1d81a3..bb4cf5cd2 100644 --- a/lib/expression/docs/function/arithmetic/unaryminus.js +++ b/lib/expression/docs/function/arithmetic/unaryMinus.js @@ -1,9 +1,9 @@ module.exports = { - 'name': 'unaryminus', + 'name': 'unaryMinus', 'category': 'Operators', 'syntax': [ '-x', - 'unaryminus(x)' + 'unaryMinus(x)' ], 'description': 'Inverse the sign of a value. Converts booleans and strings to numbers.', @@ -13,6 +13,6 @@ module.exports = { '-"22"' ], 'seealso': [ - 'add', 'subtract', 'unaryplus' + 'add', 'subtract', 'unaryPlus' ] }; diff --git a/lib/expression/docs/function/arithmetic/unaryplus.js b/lib/expression/docs/function/arithmetic/unaryPlus.js similarity index 72% rename from lib/expression/docs/function/arithmetic/unaryplus.js rename to lib/expression/docs/function/arithmetic/unaryPlus.js index b77ef6069..24532ac9c 100644 --- a/lib/expression/docs/function/arithmetic/unaryplus.js +++ b/lib/expression/docs/function/arithmetic/unaryPlus.js @@ -1,9 +1,9 @@ module.exports = { - 'name': 'unaryplus', + 'name': 'unaryPlus', 'category': 'Operators', 'syntax': [ '+x', - 'unaryplus(x)' + 'unaryPlus(x)' ], 'description': 'Converts booleans and strings to numbers.', @@ -12,6 +12,6 @@ module.exports = { '+"2"' ], 'seealso': [ - 'add', 'subtract', 'unaryminus' + 'add', 'subtract', 'unaryMinus' ] }; diff --git a/lib/expression/docs/function/arithmetic/unequal.js b/lib/expression/docs/function/arithmetic/unequal.js index 30c42d34d..9f3391841 100644 --- a/lib/expression/docs/function/arithmetic/unequal.js +++ b/lib/expression/docs/function/arithmetic/unequal.js @@ -17,6 +17,6 @@ module.exports = { '5 cm != 2 inch' ], 'seealso': [ - 'equal', 'smaller', 'larger', 'smallereq', 'largereq', 'compare' + 'equal', 'smaller', 'larger', 'smallerEq', 'largerEq', 'compare' ] }; diff --git a/lib/expression/docs/index.js b/lib/expression/docs/index.js index 7a930adb8..a4214484d 100644 --- a/lib/expression/docs/index.js +++ b/lib/expression/docs/index.js @@ -15,6 +15,7 @@ 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'); // functions - arithmetic @@ -24,16 +25,16 @@ 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.dotdivide = require('./function/arithmetic/dotdivide'); -exports.dotmultiply = require('./function/arithmetic/dotmultiply'); -exports.dotpow = require('./function/arithmetic/dotpow'); +exports.dotDivide = require('./function/arithmetic/dotDivide'); +exports.dotMultiply = require('./function/arithmetic/dotMultiply'); +exports.dotPow = require('./function/arithmetic/dotPow'); exports['equal'] = require('./function/arithmetic/equal'); exports.exp = require('./function/arithmetic/exp'); exports.fix = require('./function/arithmetic/fix'); exports.floor = require('./function/arithmetic/floor'); exports.gcd = require('./function/arithmetic/gcd'); exports.larger = require('./function/arithmetic/larger'); -exports.largereq = require('./function/arithmetic/largereq'); +exports.largerEq = require('./function/arithmetic/largerEq'); exports.lcm = require('./function/arithmetic/lcm'); exports.log = require('./function/arithmetic/log'); exports.log10 = require('./function/arithmetic/log10'); @@ -44,12 +45,12 @@ exports.pow = require('./function/arithmetic/pow'); exports.round = require('./function/arithmetic/round'); exports.sign = require('./function/arithmetic/sign'); exports.smaller = require('./function/arithmetic/smaller'); -exports.smallereq = require('./function/arithmetic/smallereq'); +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.unaryminus = require('./function/arithmetic/unaryminus'); -exports.unaryplus = require('./function/arithmetic/unaryplus'); +exports.unaryMinus = require('./function/arithmetic/unaryMinus'); +exports.unaryPlus = require('./function/arithmetic/unaryPlus'); exports.unequal = require('./function/arithmetic/unequal'); exports.xgcd = require('./function/arithmetic/xgcd'); diff --git a/lib/expression/parse.js b/lib/expression/parse.js index c11111ce5..c4322b66e 100644 --- a/lib/expression/parse.js +++ b/lib/expression/parse.js @@ -122,7 +122,9 @@ var DELIMITERS = { '?': true, '==': true, + '.==': true, '!=': true, + '.!=': true, '<': true, '>': true, '<=': true, @@ -167,11 +169,17 @@ function next() { /** * Preview the next character from the expression. + * @param {Number} [num=1] Optional number of next characters to preview * @return {String} cNext * @private */ -function nextPreview() { - return expression.charAt(index + 1); +function nextPreview(num) { + if (num === undefined) { + return expression.charAt(index + 1); + } + else { + return expression.substr(index + 1, num); + } } /** @@ -203,9 +211,22 @@ function getToken() { return; } + // TODO: make this checking for operators of 3, 2, and 1 characters less stupid + + // check for delimiters consisting of 3 characters + var c3 = c + nextPreview(2); + if (c3.length == 3 && DELIMITERS[c3]) { + token_type = TOKENTYPE.DELIMITER; + token = c3; + next(); + next(); + next(); + return; + } + // check for delimiters consisting of 2 characters var c2 = c + nextPreview(); - if (DELIMITERS[c2]) { + if (c2.length == 2 && DELIMITERS[c2]) { token_type = TOKENTYPE.DELIMITER; token = c2; next(); @@ -627,11 +648,13 @@ function parseComparison () { operators = { '==': 'equal', + '.==': 'dotEqual', '!=': 'unequal', + '.!=': 'dotUnequal', '<': 'smaller', '>': 'larger', - '<=': 'smallereq', - '>=': 'largereq' + '<=': 'smallerEq', + '>=': 'largerEq' }; while (token in operators) { name = token; @@ -719,9 +742,9 @@ function parseMultiplyDivide () { operators = { '*': 'multiply', - '.*': 'dotmultiply', + '.*': 'dotMultiply', '/': 'divide', - './': 'dotdivide', + './': 'dotDivide', '%': 'mod', 'mod': 'mod' }; @@ -770,7 +793,7 @@ function parseUnary () { if (token == '-' || token == '+') { name = token; - fn = name == '+' ? 'unaryplus' : 'unaryminus'; + fn = name == '+' ? 'unaryPlus' : 'unaryMinus'; getToken(); params = [parseUnary()]; @@ -793,7 +816,7 @@ function parsePow () { if (token == '^' || token == '.^') { name = token; - fn = (name == '^') ? 'pow' : 'dotpow'; + fn = (name == '^') ? 'pow' : 'dotPow'; getToken(); params = [node, parseUnary()]; // Go back to unary, we can have '2^-3' diff --git a/lib/function/arithmetic/compare.js b/lib/function/arithmetic/compare.js index 2a5f8df97..5528f5eb1 100644 --- a/lib/function/arithmetic/compare.js +++ b/lib/function/arithmetic/compare.js @@ -43,7 +43,7 @@ module.exports = function (math, config) { * * See also: * - * equal, unequal, smaller, smallereq, larger, largereq + * equal, unequal, smaller, smallerEq, larger, largerEq * * @param {Number | BigNumber | Boolean | Unit | String | Array | Matrix} x First value to compare * @param {Number | BigNumber | Boolean | Unit | String | Array | Matrix} y Second value to compare diff --git a/lib/function/arithmetic/dotdivide.js b/lib/function/arithmetic/dotDivide.js similarity index 72% rename from lib/function/arithmetic/dotdivide.js rename to lib/function/arithmetic/dotDivide.js index 4b0c42aa2..5693a8802 100644 --- a/lib/function/arithmetic/dotdivide.js +++ b/lib/function/arithmetic/dotDivide.js @@ -7,31 +7,31 @@ module.exports = function (math) { * * Syntax: * - * math.dotdivide(x, y) + * math.dotDivide(x, y) * * Examples: * * var math = mathjs(); * - * math.dotdivide(2, 4); // returns 0.5 + * math.dotDivide(2, 4); // returns 0.5 * * a = [[9, 5], [6, 1]]; * b = [[3, 2], [5, 2]]; * - * math.dotdivide(a, b); // returns [[3, 2.5], [1.2, 0.5]] + * math.dotDivide(a, b); // returns [[3, 2.5], [1.2, 0.5]] * math.divide(a, b); // returns [[1.75, 0.75], [-1.75, 2.25]] * * See also: * - * divide, multiply, dotmultiply + * divide, multiply, dotMultiply * * @param {Number | BigNumber | Boolean | Complex | Unit | Array | Matrix} x Numerator * @param {Number | BigNumber | Boolean | Complex | Unit | Array | Matrix} y Denominator * @return {Number | BigNumber | Complex | Unit | Array | Matrix} Quotient, `x ./ y` */ - math.dotdivide = function dotdivide(x, y) { + math.dotDivide = function dotDivide(x, y) { if (arguments.length != 2) { - throw new math.error.ArgumentsError('dotdivide', arguments.length, 2); + throw new math.error.ArgumentsError('dotDivide', arguments.length, 2); } return collection.deepMap2(x, y, math.divide); @@ -39,6 +39,6 @@ module.exports = function (math) { // TODO: deprecated since version 0.23.0, clean up some day math.edivide = function () { - throw new Error('Function edivide is renamed to dotdivide'); + throw new Error('Function edivide is renamed to dotDivide'); } }; diff --git a/lib/function/arithmetic/dotEqual.js b/lib/function/arithmetic/dotEqual.js new file mode 100644 index 000000000..9bd60b336 --- /dev/null +++ b/lib/function/arithmetic/dotEqual.js @@ -0,0 +1,40 @@ +module.exports = function (math) { + var collection = require('../../type/collection'); + + /** + * Compare two matrices element wise. The function accepts both matrices and + * scalar values. + * + * Syntax: + * + * math.dotEqual(x, y) + * + * Examples: + * + * var math = mathjs(); + * + * math.dotEqual(2, 4); // returns false + * + * a = [2, 5, 1]; + * b = [2, 7, 1]; + * + * math.dotEqual(a, b); // returns [true, false, true] + * math.equal(a, b); // returns false + * + * See also: + * + * equal, unequal, dotuneuqal + * + * @param {Number | BigNumber | Boolean | Complex | Unit | Array | Matrix} x First matrix to compare + * @param {Number | BigNumber | Boolean | Complex | Unit | Array | Matrix} y Second matrix to compare + * @return {Number | BigNumber | Complex | Unit | Array | Matrix} + * Returns a matrix containing boolean results of the element wise comparisons. + */ + math.dotEqual = function dotEqual(x, y) { + if (arguments.length != 2) { + throw new math.error.ArgumentsError('dotEqual', arguments.length, 2); + } + + return collection.deepMap2(x, y, math.equal); + }; +}; diff --git a/lib/function/arithmetic/dotmultiply.js b/lib/function/arithmetic/dotMultiply.js similarity index 74% rename from lib/function/arithmetic/dotmultiply.js rename to lib/function/arithmetic/dotMultiply.js index 052350733..7710d7065 100644 --- a/lib/function/arithmetic/dotmultiply.js +++ b/lib/function/arithmetic/dotMultiply.js @@ -8,31 +8,31 @@ module.exports = function (math) { * * Syntax: * - * math.dotmultiply(x, y) + * math.dotMultiply(x, y) * * Examples: * * var math = mathjs(); * - * math.dotmultiply(2, 4); // returns 8 + * math.dotMultiply(2, 4); // returns 8 * * a = [[9, 5], [6, 1]]; * b = [[3, 2], [5, 2]]; * - * math.dotmultiply(a, b); // returns [[27, 10], [30, 2]] + * math.dotMultiply(a, b); // returns [[27, 10], [30, 2]] * math.multiply(a, b); // returns [[52, 28], [23, 14]] * * See also: * - * multiply, divide, dotdivide + * multiply, divide, dotDivide * * @param {Number | BigNumber | Boolean | Complex | Unit | Array | Matrix} x Left hand value * @param {Number | BigNumber | Boolean | Complex | Unit | Array | Matrix} y Right hand value * @return {Number | BigNumber | Complex | Unit | Array | Matrix} Multiplication of `x` and `y` */ - math.dotmultiply = function dotmultiply(x, y) { + math.dotMultiply = function dotMultiply(x, y) { if (arguments.length != 2) { - throw new math.error.ArgumentsError('dotmultiply', arguments.length, 2); + throw new math.error.ArgumentsError('dotMultiply', arguments.length, 2); } return collection.deepMap2(x, y, math.multiply); @@ -40,6 +40,6 @@ module.exports = function (math) { // TODO: deprecated since version 0.23.0, clean up some day math.emultiply = function () { - throw new Error('Function emultiply is renamed to dotmultiply'); + throw new Error('Function emultiply is renamed to dotMultiply'); } }; diff --git a/lib/function/arithmetic/dotpow.js b/lib/function/arithmetic/dotPow.js similarity index 75% rename from lib/function/arithmetic/dotpow.js rename to lib/function/arithmetic/dotPow.js index 1288cc1a1..060b1aae8 100644 --- a/lib/function/arithmetic/dotpow.js +++ b/lib/function/arithmetic/dotPow.js @@ -7,16 +7,16 @@ module.exports = function (math) { * * Syntax: * - * math.dotpow(x, y) + * math.dotPow(x, y) * * Examples: * * var math = mathjs(); * - * math.dotpow(2, 3); // returns Number 8 + * math.dotPow(2, 3); // returns Number 8 * * var a = [[1, 2], [4, 3]]; - * math.dotpow(a, 2); // returns Array [[1, 4], [16, 9]] + * math.dotPow(a, 2); // returns Array [[1, 4], [16, 9]] * math.pow(a, 2); // returns Array [[9, 8], [16, 17]] * * See also: @@ -27,9 +27,9 @@ module.exports = function (math) { * @param {Number | BigNumber | Boolean | Complex | Unit | Array | Matrix} y The exponent * @return {Number | BigNumber | Complex | Unit | Array | Matrix} The value of `x` to the power `y` */ - math.dotpow = function dotpow(x, y) { + math.dotPow = function dotPow(x, y) { if (arguments.length != 2) { - throw new math.error.ArgumentsError('dotpow', arguments.length, 2); + throw new math.error.ArgumentsError('dotPow', arguments.length, 2); } return collection.deepMap2(x, y, math.pow); @@ -37,6 +37,6 @@ module.exports = function (math) { // TODO: deprecated since version 0.23.0, clean up some day math.epow = function () { - throw new Error('Function epow is renamed to dotpow'); + throw new Error('Function epow is renamed to dotPow'); } }; diff --git a/lib/function/arithmetic/dotUnequal.js b/lib/function/arithmetic/dotUnequal.js new file mode 100644 index 000000000..0d9f96f43 --- /dev/null +++ b/lib/function/arithmetic/dotUnequal.js @@ -0,0 +1,40 @@ +module.exports = function (math) { + var collection = require('../../type/collection'); + + /** + * Test element wise whether two matrices are unequal. + * The function accepts both matrices and scalar values. + * + * Syntax: + * + * math.dotUnequal(x, y) + * + * Examples: + * + * var math = mathjs(); + * + * math.dotUnequal(2, 4); // returns true + * + * a = [2, 5, 1]; + * b = [2, 7, 1]; + * + * math.dotUnequal(a, b); // returns [false, true, false] + * math.unequal(a, b); // returns true + * + * See also: + * + * equal, unequal, dotEqual + * + * @param {Number | BigNumber | Boolean | Complex | Unit | Array | Matrix} x First matrix to compare + * @param {Number | BigNumber | Boolean | Complex | Unit | Array | Matrix} y Second matrix to compare + * @return {Number | BigNumber | Complex | Unit | Array | Matrix} + * Returns a matrix containing boolean results of the element wise comparisons. + */ + math.dotUnequal = function dotUnequal(x, y) { + if (arguments.length != 2) { + throw new math.error.ArgumentsError('dotUnequal', arguments.length, 2); + } + + return collection.deepMap2(x, y, math.unequal); + }; +}; diff --git a/lib/function/arithmetic/equal.js b/lib/function/arithmetic/equal.js index 593dbc8cf..8615f8975 100644 --- a/lib/function/arithmetic/equal.js +++ b/lib/function/arithmetic/equal.js @@ -21,7 +21,8 @@ module.exports = function (math, config) { * smaller than the configured epsilon. The function cannot be used to * compare values smaller than approximately 2.22e-16. * - * For matrices, the function is evaluated element wise. + * For matrices, the function tests whether the size and all elements of the + * matrices are equal (a deep comparison). * In case of complex numbers, x.re must equal y.re, and x.im must equal y.im. * * Syntax: @@ -41,7 +42,7 @@ module.exports = function (math, config) { * * See also: * - * unequal, smaller, smallereq, larger, largereq, compare + * unequal, smaller, smallerEq, larger, largerEq, compare, dotEqual * * @param {Number | BigNumber | Boolean | Complex | Unit | String | Array | Matrix} x First value to compare * @param {Number | BigNumber | Boolean | Complex | Unit | String | Array | Matrix} y Second value to compare @@ -111,11 +112,11 @@ module.exports = function (math, config) { } if (isCollection(x) || isCollection(y)) { - return collection.deepMap2(x, y, equal); + return deepEqual(x.valueOf(), y.valueOf()); } // Note: test strings after testing collections, - // else we can't compare a string with a matrix + // else we can accidentally compare a stringified array with a string if (isString(x) || isString(y)) { return x == y; } @@ -129,4 +130,36 @@ module.exports = function (math, config) { throw new math.error.UnsupportedTypeError('equal', math['typeof'](x), math['typeof'](y)); }; + + /** + * Test whether two arrays have the same size and all elements are equal + * @param {Array | *} x + * @param {Array | *} y + * @return {boolean} Returns true if both arrays are deep equal + */ + function deepEqual(x, y) { + if (isArray(x)) { + if (isArray(y)) { + var len = x.length; + if (len !== y.length) return false; + + for (var i = 0; i < len; i++) { + if (!deepEqual(x[i], y[i])) return false; + } + + return true; + } + else { + return false; + } + } + else { + if (isArray(y)) { + return false; + } + else { + return math.equal(x, y); + } + } + } }; diff --git a/lib/function/arithmetic/larger.js b/lib/function/arithmetic/larger.js index 4467a0b25..c510282cd 100644 --- a/lib/function/arithmetic/larger.js +++ b/lib/function/arithmetic/larger.js @@ -40,7 +40,7 @@ module.exports = function (math, config) { * * See also: * - * equal, unequal, smaller, smallereq, largereq, compare + * equal, unequal, smaller, smallerEq, largerEq, compare * * @param {Number | BigNumber | Boolean | Unit | String | Array | Matrix} x First value to compare * @param {Number | BigNumber | Boolean | Unit | String | Array | Matrix} y Second value to compare diff --git a/lib/function/arithmetic/largereq.js b/lib/function/arithmetic/largerEq.js similarity index 80% rename from lib/function/arithmetic/largereq.js rename to lib/function/arithmetic/largerEq.js index ad69e4365..184d9eb7d 100644 --- a/lib/function/arithmetic/largereq.js +++ b/lib/function/arithmetic/largerEq.js @@ -25,26 +25,26 @@ module.exports = function (math, config) { * * Syntax: * - * math.largereq(x, y) + * math.largerEq(x, y) * * Examples: * * var math = mathjs(); * * math.larger(2, 1 + 1); // returns false - * math.largereq(2, 1 + 1); // returns true + * math.largerEq(2, 1 + 1); // returns true * * See also: * - * equal, unequal, smaller, smallereq, larger, compare + * equal, unequal, smaller, smallerEq, larger, compare * * @param {Number | BigNumber | Boolean | Unit | String | Array | Matrix} x First value to compare * @param {Number | BigNumber | Boolean | Unit | String | Array | Matrix} y Second value to compare * @return {Boolean | Array | Matrix} Returns true when the x is larger or equal to y, else returns false */ - math.largereq = function largereq(x, y) { + math.largerEq = function largerEq(x, y) { if (arguments.length != 2) { - throw new math.error.ArgumentsError('largereq', arguments.length, 2); + throw new math.error.ArgumentsError('largerEq', arguments.length, 2); } if (isNumber(x) && isNumber(y)) { @@ -65,7 +65,7 @@ module.exports = function (math, config) { } // downgrade to Number - return largereq(x.toNumber(), y); + return largerEq(x.toNumber(), y); } if (y instanceof BigNumber) { // try to convert to big number @@ -81,7 +81,7 @@ module.exports = function (math, config) { } // downgrade to Number - return largereq(x, y.toNumber()); + return largerEq(x, y.toNumber()); } if ((isUnit(x)) && (isUnit(y))) { @@ -92,7 +92,7 @@ module.exports = function (math, config) { } if (isCollection(x) || isCollection(y)) { - return collection.deepMap2(x, y, largereq); + return collection.deepMap2(x, y, largerEq); } // Note: test strings after testing collections, @@ -102,16 +102,21 @@ module.exports = function (math, config) { } if (isBoolean(x)) { - return largereq(+x, y); + return largerEq(+x, y); } if (isBoolean(y)) { - return largereq(x, +y); + return largerEq(x, +y); } if (isComplex(x) || isComplex(y)) { throw new TypeError('No ordering relation is defined for complex numbers'); } - throw new math.error.UnsupportedTypeError('largereq', math['typeof'](x), math['typeof'](y)); + throw new math.error.UnsupportedTypeError('largerEq', math['typeof'](x), math['typeof'](y)); }; + + // TODO: deprecated since version 0.23.0, cleanup some day + math.largereq = function () { + throw new Error('Function largereq is renamed to largerEq'); + } }; diff --git a/lib/function/arithmetic/smaller.js b/lib/function/arithmetic/smaller.js index 2e40cc1eb..977ccee20 100644 --- a/lib/function/arithmetic/smaller.js +++ b/lib/function/arithmetic/smaller.js @@ -40,7 +40,7 @@ module.exports = function (math, config) { * * See also: * - * equal, unequal, smallereq, larger, largereq, compare + * equal, unequal, smallerEq, larger, largerEq, compare * * @param {Number | BigNumber | Boolean | Unit | String | Array | Matrix} x First value to compare * @param {Number | BigNumber | Boolean | Unit | String | Array | Matrix} y Second value to compare diff --git a/lib/function/arithmetic/smallereq.js b/lib/function/arithmetic/smallerEq.js similarity index 80% rename from lib/function/arithmetic/smallereq.js rename to lib/function/arithmetic/smallerEq.js index bb448cce9..28e157a03 100644 --- a/lib/function/arithmetic/smallereq.js +++ b/lib/function/arithmetic/smallerEq.js @@ -24,26 +24,26 @@ module.exports = function (math, config) { * * Syntax: * - * math.smallereq(x, y) + * math.smallerEq(x, y) * * Examples: * * var math = mathjs(); * * math.smaller(1 + 2, 3); // returns false - * math.smallereq(1 + 2, 3); // returns true + * math.smallerEq(1 + 2, 3); // returns true * * See also: * - * equal, unequal, smaller, larger, largereq, compare + * equal, unequal, smaller, larger, largerEq, compare * * @param {Number | BigNumber | Boolean | Unit | String | Array | Matrix} x First value to compare * @param {Number | BigNumber | Boolean | Unit | String | Array | Matrix} y Second value to compare * @return {Boolean | Array | Matrix} Returns true when the x is smaller than y, else returns false */ - math.smallereq = function smallereq(x, y) { + math.smallerEq = function smallerEq(x, y) { if (arguments.length != 2) { - throw new math.error.ArgumentsError('smallereq', arguments.length, 2); + throw new math.error.ArgumentsError('smallerEq', arguments.length, 2); } if (isNumber(x) && isNumber(y)) { @@ -64,7 +64,7 @@ module.exports = function (math, config) { } // downgrade to Number - return smallereq(x.toNumber(), y); + return smallerEq(x.toNumber(), y); } if (y instanceof BigNumber) { // try to convert to big number @@ -80,7 +80,7 @@ module.exports = function (math, config) { } // downgrade to Number - return smallereq(x, y.toNumber()); + return smallerEq(x, y.toNumber()); } if ((isUnit(x)) && (isUnit(y))) { @@ -91,7 +91,7 @@ module.exports = function (math, config) { } if (isCollection(x) || isCollection(y)) { - return collection.deepMap2(x, y, smallereq); + return collection.deepMap2(x, y, smallerEq); } // Note: test strings after testing collections, @@ -101,16 +101,21 @@ module.exports = function (math, config) { } if (isBoolean(x)) { - return smallereq(+x, y); + return smallerEq(+x, y); } if (isBoolean(y)) { - return smallereq(x, +y); + return smallerEq(x, +y); } if (isComplex(x) || isComplex(y)) { throw new TypeError('No ordering relation is defined for complex numbers'); } - throw new math.error.UnsupportedTypeError('smallereq', math['typeof'](x), math['typeof'](y)); + throw new math.error.UnsupportedTypeError('smallerEq', math['typeof'](x), math['typeof'](y)); }; + + // TODO: deprecated since version 0.23.0, cleanup some day + math.smallereq = function () { + throw new Error('Function smallereq is renamed to smallerEq'); + } }; diff --git a/lib/function/arithmetic/unaryminus.js b/lib/function/arithmetic/unaryMinus.js similarity index 77% rename from lib/function/arithmetic/unaryminus.js rename to lib/function/arithmetic/unaryMinus.js index 4e52726a5..d5277302d 100644 --- a/lib/function/arithmetic/unaryminus.js +++ b/lib/function/arithmetic/unaryMinus.js @@ -22,25 +22,25 @@ module.exports = function (math, config) { * * Syntax: * - * math.unaryminus(x) + * math.unaryMinus(x) * * Examples: * * var math = mathjs(); * - * math.unaryminus(3.5); // returns -3.5 - * math.unaryminus(-4.2); // returns 4.2 + * math.unaryMinus(3.5); // returns -3.5 + * math.unaryMinus(-4.2); // returns 4.2 * * See also: * - * add, subtract, unaryplus + * add, subtract, unaryPlus * * @param {Number | BigNumber | Boolean | String | Complex | Unit | Array | Matrix} x Number to be inverted. * @return {Number | BigNumber | Complex | Unit | Array | Matrix} Returns the value with inverted sign. */ - math.unaryminus = function unaryminus(x) { + math.unaryMinus = function unaryMinus(x) { if (arguments.length != 1) { - throw new math.error.ArgumentsError('unaryminus', arguments.length, 1); + throw new math.error.ArgumentsError('unaryMinus', arguments.length, 1); } if (isNumber(x)) { @@ -65,7 +65,7 @@ module.exports = function (math, config) { } if (isCollection(x)) { - return collection.deepMap(x, unaryminus); + return collection.deepMap(x, unaryMinus); } if (isBoolean(x) || isString(x)) { @@ -73,11 +73,11 @@ module.exports = function (math, config) { return (config.number == 'bignumber') ? new BigNumber(-x): -x; } - throw new math.error.UnsupportedTypeError('unaryminus', math['typeof'](x)); + throw new math.error.UnsupportedTypeError('unaryMinus', math['typeof'](x)); }; - // TODO: function unary is renamed to unaryminus since version 0.23.0. Cleanup some day + // TODO: function unary is renamed to unaryMinus since version 0.23.0. Cleanup some day math.unary = function unary() { - throw new Error('Function unary is deprecated. Use unaryminus instead.'); + throw new Error('Function unary is deprecated. Use unaryMinus instead.'); } }; diff --git a/lib/function/arithmetic/unaryplus.js b/lib/function/arithmetic/unaryPlus.js similarity index 81% rename from lib/function/arithmetic/unaryplus.js rename to lib/function/arithmetic/unaryPlus.js index 3ad1df823..3c37029fe 100644 --- a/lib/function/arithmetic/unaryplus.js +++ b/lib/function/arithmetic/unaryPlus.js @@ -21,27 +21,27 @@ module.exports = function (math, config) { * * Syntax: * - * math.unaryplus(x) + * math.unaryPlus(x) * * Examples: * * var math = mathjs(); * - * math.unaryplus(3.5); // returns 3.5 - * math.unaryplus(1); // returns 1 + * math.unaryPlus(3.5); // returns 3.5 + * math.unaryPlus(1); // returns 1 * * See also: * - * unaryminus, add, subtract + * unaryMinus, add, subtract * * @param {Number | BigNumber | Boolean | String | Complex | Unit | Array | Matrix} x * Input value * @return {Number | BigNumber | Complex | Unit | Array | Matrix} * Returns the input value when numeric, converts to a number when input is non-numeric. */ - math.unaryplus = function unaryplus(x) { + math.unaryPlus = function unaryPlus(x) { if (arguments.length != 1) { - throw new math.error.ArgumentsError('unaryplus', arguments.length, 1); + throw new math.error.ArgumentsError('unaryPlus', arguments.length, 1); } if (isNumber(x)) { @@ -61,7 +61,7 @@ module.exports = function (math, config) { } if (isCollection(x)) { - return collection.deepMap(x, unaryplus); + return collection.deepMap(x, unaryPlus); } if (isBoolean(x) || isString(x)) { @@ -69,6 +69,6 @@ module.exports = function (math, config) { return (config.number == 'bignumber') ? new BigNumber(+x): +x; } - throw new math.error.UnsupportedTypeError('unaryplus', math['typeof'](x)); + throw new math.error.UnsupportedTypeError('unaryPlus', math['typeof'](x)); }; }; diff --git a/lib/function/arithmetic/unequal.js b/lib/function/arithmetic/unequal.js index e48c27060..baeb6f2a3 100644 --- a/lib/function/arithmetic/unequal.js +++ b/lib/function/arithmetic/unequal.js @@ -21,7 +21,8 @@ module.exports = function (math, config) { * larger than the configured epsilon. The function cannot be used to compare * values smaller than approximately 2.22e-16. * - * For matrices, the function is evaluated element wise. + * For matrices, the function tests whether the size or any of the elements of the + * matrices are unequal (a deep comparison). * In case of complex numbers, x.re must unequal y.re, or x.im must unequal y.im. * * Syntax: @@ -41,7 +42,7 @@ module.exports = function (math, config) { * * See also: * - * equal, smaller, smallereq, larger, largereq, compare + * equal, smaller, smallerEq, larger, largerEq, compare, dotUnequal * * @param {Number | BigNumber | Boolean | Complex | Unit | String | Array | Matrix} x First value to compare * @param {Number | BigNumber | Boolean | Complex | Unit | String | Array | Matrix} y Second value to compare @@ -111,11 +112,11 @@ module.exports = function (math, config) { } if (isCollection(x) || isCollection(y)) { - return collection.deepMap2(x, y, unequal); + return deepUnequal(x.valueOf(), y.valueOf()); } // Note: test strings after testing collections, - // else we can't compare a string with a matrix + // else we can accidentally compare a stringified array with a string if (isString(x) || isString(y)) { return x != y; } @@ -129,4 +130,37 @@ module.exports = function (math, config) { throw new math.error.UnsupportedTypeError('unequal', math['typeof'](x), math['typeof'](y)); }; + + /** + * Test whether two arrays are not equal: have unequal size or elements + * @param {Array | *} x + * @param {Array | *} y + * @return {boolean} Returns true if size is not equal or when any of the + * elements is unequal + */ + function deepUnequal(x, y) { + if (isArray(x)) { + if (isArray(y)) { + var len = x.length; + if (len !== y.length) return true; + + for (var i = 0; i < len; i++) { + if (deepUnequal(x[i], y[i])) return true; + } + + return false; + } + else { + return true; + } + } + else { + if (isArray(y)) { + return true; + } + else { + return math.unequal(x, y); + } + } + } }; diff --git a/lib/function/matrix/det.js b/lib/function/matrix/det.js index b29f68b5a..f6e7583a7 100644 --- a/lib/function/matrix/det.js +++ b/lib/function/matrix/det.js @@ -122,7 +122,7 @@ module.exports = function (math) { for (i = 0; i < matrix.length; i++) { mu[i] = new Array(matrix.length); - mu[i][i] = math.unaryminus(sum); + mu[i][i] = math.unaryMinus(sum); for (j = 0; j < i; j++) { mu[i][j] = 0; // TODO: make bignumber 0 in case of bignumber computation @@ -146,7 +146,7 @@ module.exports = function (math) { } if (rows % 2 == 0) { - return math.unaryminus(fa[0][0]); + return math.unaryMinus(fa[0][0]); } else { return fa[0][0]; } diff --git a/lib/function/matrix/inv.js b/lib/function/matrix/inv.js index 1d8130bf1..e493ca11e 100644 --- a/lib/function/matrix/inv.js +++ b/lib/function/matrix/inv.js @@ -110,10 +110,10 @@ module.exports = function (math) { return [ [ math.divide(matrix[1][1], d), - math.divide(math.unaryminus(matrix[0][1]), d) + math.divide(math.unaryMinus(matrix[0][1]), d) ], [ - math.divide(math.unaryminus(matrix[1][0]), d), + math.divide(math.unaryMinus(matrix[1][0]), d), math.divide(matrix[0][0], d) ] ]; @@ -161,7 +161,7 @@ module.exports = function (math) { if(r != c) { // eliminate value at column c and row r if (Ar[c] != 0) { - f = math.divide(math.unaryminus(Ar[c]), Ac[c]); + f = math.divide(math.unaryMinus(Ar[c]), Ac[c]); // add (f * row c) to row r to eliminate the value // at column c diff --git a/lib/function/probability/distribution.js b/lib/function/probability/distribution.js index 99f6297bf..148f59844 100644 --- a/lib/function/probability/distribution.js +++ b/lib/function/probability/distribution.js @@ -1,4 +1,4 @@ -module.exports = function (math, config) { +module.exports = function (math) { var Matrix = require('../../type/Matrix'), array = require('../../util/array'), collection = require('../../type/collection'), diff --git a/lib/math.js b/lib/math.js index 1546ad94b..b2d4b14f3 100644 --- a/lib/math.js +++ b/lib/math.js @@ -167,16 +167,18 @@ function mathjs (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/dotdivide.js')(math, _config); - require('./function/arithmetic/dotmultiply.js')(math, _config); - require('./function/arithmetic/dotpow.js')(math, _config); + require('./function/arithmetic/dotDivide.js')(math, _config); + require('./function/arithmetic/dotEqual.js')(math, _config); + require('./function/arithmetic/dotMultiply.js')(math, _config); + require('./function/arithmetic/dotPow.js')(math, _config); + require('./function/arithmetic/dotUnequal.js')(math, _config); require('./function/arithmetic/equal.js')(math, _config); require('./function/arithmetic/exp.js')(math, _config); require('./function/arithmetic/fix.js')(math, _config); require('./function/arithmetic/floor.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/largerEq.js')(math, _config); require('./function/arithmetic/lcm.js')(math, _config); require('./function/arithmetic/log.js')(math, _config); require('./function/arithmetic/log10.js')(math, _config); @@ -187,12 +189,12 @@ function mathjs (config) { require('./function/arithmetic/round.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/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/unaryminus.js')(math, _config); - require('./function/arithmetic/unaryplus.js')(math, _config); + require('./function/arithmetic/unaryMinus.js')(math, _config); + require('./function/arithmetic/unaryPlus.js')(math, _config); require('./function/arithmetic/unequal.js')(math, _config); require('./function/arithmetic/xgcd.js')(math, _config); diff --git a/lib/util/latex.js b/lib/util/latex.js index 025f9346d..f9634c22c 100644 --- a/lib/util/latex.js +++ b/lib/util/latex.js @@ -97,7 +97,8 @@ var functions = { lcm: false, sign: false, xgcd: false, - unaryminus: false, + unaryMinus: false, + unaryPlus: false, // complex complex: false, @@ -306,7 +307,7 @@ exports.toParams = function(that) { op = '>'; break; - case 'largereq': + case 'largerEq': op = '>='; break; @@ -314,7 +315,7 @@ exports.toParams = function(that) { op = '<'; break; - case 'smallereq': + case 'smallerEq': op = '<='; break; diff --git a/test/deprecated.test.js b/test/deprecated.test.js index 7bebead0b..333661214 100644 --- a/test/deprecated.test.js +++ b/test/deprecated.test.js @@ -95,4 +95,18 @@ describe('deprecated stuff', function() { }, /is renamed/); }); + it ('should throw an error when using deprecated function smallereq', function () { + var math = mathjs(); + assert.throws(function () { + new math.smallereq(2, 3); + }, /is renamed/); + }); + + it ('should throw an error when using deprecated function largereq', function () { + var math = mathjs(); + assert.throws(function () { + new math.largereq(2, 3); + }, /is renamed/); + }); + }); diff --git a/test/expression/node/OperatorNode.test.js b/test/expression/node/OperatorNode.test.js index 7d0f014b8..6d2fc7aff 100644 --- a/test/expression/node/OperatorNode.test.js +++ b/test/expression/node/OperatorNode.test.js @@ -86,7 +86,7 @@ describe('OperatorNode', function() { it ('should stringify a OperatorNode with unary minus', function () { var a = new ConstantNode('number', '2'); - var n = new OperatorNode('-', 'unaryminus', [a]); + var n = new OperatorNode('-', 'unaryMinus', [a]); assert.equal(n.toString(), '-2'); }); @@ -137,7 +137,7 @@ describe('OperatorNode', function() { it ('should LaTeX a OperatorNode with unary minus', function () { var a = new ConstantNode('number', '2'); - var n = new OperatorNode('-', 'unaryminus', [a]); + var n = new OperatorNode('-', 'unaryMinus', [a]); assert.equal(n.toTex(), '-2'); }); diff --git a/test/expression/parse.test.js b/test/expression/parse.test.js index 77fcff236..125eb1e30 100644 --- a/test/expression/parse.test.js +++ b/test/expression/parse.test.js @@ -68,7 +68,7 @@ describe('parse', function() { it('should give informative syntax errors', function() { assert.throws(function () {parse('sin pi').compile(math).eval()}, /First parameter in Unit constructor must be a number/); - assert.throws(function () {parse('2 +')}, /Unexpected end of expression \(char 5\)/); + assert.throws(function () {parse('2 +')}, /Unexpected end of expression \(char 4\)/); assert.throws(function () {parse('2 ~ 3')}, /Syntax error in part "~ 3" \(char 3\)/); assert.throws(function () {parse('2 + 3\n3 +\n-4')}, /Value expected \(char 10\)/); }); @@ -605,8 +605,15 @@ describe('parse', function() { }); it('should parse ==', function() { - assert.equal(parseAndEval('2 == 3'), false); - assert.equal(parseAndEval('2 == 2'), true); + assert.strictEqual(parseAndEval('2 == 3'), false); + assert.strictEqual(parseAndEval('2 == 2'), true); + assert.strictEqual(parseAndEval('[2,3] == [2,4]'), false); + }); + + it('should parse .==', function() { + assert.equal(parseAndEval('2 .== 3'), false); + assert.equal(parseAndEval('2 .== 2'), true); + assert.deepEqual(parseAndEval('[2,3] .== [2,4]'), new Matrix([true, false])); }); it('should parse >', function() { @@ -704,8 +711,15 @@ describe('parse', function() { }); it('should parse !=', function() { - assert.equal(parseAndEval('2 != 3'), true); - assert.equal(parseAndEval('2 != 2'), false); + assert.strictEqual(parseAndEval('2 != 3'), true); + assert.strictEqual(parseAndEval('2 != 2'), false); + assert.strictEqual(parseAndEval('[2,3] != [2,4]'), true); + }); + + it('should parse .!=', function() { + assert.equal(parseAndEval('2 .!= 3'), true); + assert.equal(parseAndEval('2 .!= 2'), false); + assert.deepEqual(parseAndEval('[2,3] .!= [2,4]'), new Matrix([false, true])); }); it('should parse contitional expression a ? b : c', function() { diff --git a/test/function/arithmetic/dotdivide.test.js b/test/function/arithmetic/dotdivide.test.js index cbe5a008f..947e829ab 100644 --- a/test/function/arithmetic/dotdivide.test.js +++ b/test/function/arithmetic/dotdivide.test.js @@ -1,67 +1,67 @@ -// test dotdivide (element-wise divide) +// test dotDivide (element-wise divide) var assert = require('assert'), math = require('../../../index')(), error = require('../../../lib/error/index'), approx = require('../../../tools/approx'), - dotdivide = math.dotdivide, + dotDivide = math.dotDivide, complex = math.complex; -describe('dotdivide', function() { +describe('dotDivide', function() { it('should divide two numbers', function() { - assert.equal(dotdivide(4, 2), 2); - assert.equal(dotdivide(-4, 2), -2); - assert.equal(dotdivide(4, -2), -2); - assert.equal(dotdivide(-4, -2), 2); - assert.equal(dotdivide(4, 0), Infinity); - assert.equal(dotdivide(0, -5), 0); - assert.ok(isNaN(dotdivide(0, 0))); + assert.equal(dotDivide(4, 2), 2); + assert.equal(dotDivide(-4, 2), -2); + assert.equal(dotDivide(4, -2), -2); + assert.equal(dotDivide(-4, -2), 2); + assert.equal(dotDivide(4, 0), Infinity); + assert.equal(dotDivide(0, -5), 0); + assert.ok(isNaN(dotDivide(0, 0))); }); it('should divide booleans', function() { - assert.equal(dotdivide(true, true), 1); - assert.equal(dotdivide(true, false), Infinity); - assert.equal(dotdivide(false, true), 0); - assert.ok(isNaN(dotdivide(false, false))); + assert.equal(dotDivide(true, true), 1); + assert.equal(dotDivide(true, false), Infinity); + assert.equal(dotDivide(false, true), 0); + assert.ok(isNaN(dotDivide(false, false))); }); it('should add mixed numbers and booleans', function() { - assert.equal(dotdivide(2, true), 2); - assert.equal(dotdivide(2, false), Infinity); - approx.equal(dotdivide(true, 2), 0.5); - assert.equal(dotdivide(false, 2), 0); + assert.equal(dotDivide(2, true), 2); + assert.equal(dotDivide(2, false), Infinity); + approx.equal(dotDivide(true, 2), 0.5); + assert.equal(dotDivide(false, 2), 0); }); it('should throw an error if there\'s wrong number of arguments', function() { - assert.throws(function () {dotdivide(2,3,4); }); - assert.throws(function () {dotdivide(2); }); + assert.throws(function () {dotDivide(2,3,4); }); + assert.throws(function () {dotDivide(2); }); }); it('should divide two complex numbers', function() { - approx.deepEqual(dotdivide(complex('2+3i'), 2), complex('1+1.5i')); - approx.deepEqual(dotdivide(complex('2+3i'), complex('4i')), complex('0.75 - 0.5i')); - approx.deepEqual(dotdivide(complex('2i'), complex('4i')), 0.5); - approx.deepEqual(dotdivide(4, complex('1+2i')), complex('0.8 - 1.6i')); + approx.deepEqual(dotDivide(complex('2+3i'), 2), complex('1+1.5i')); + approx.deepEqual(dotDivide(complex('2+3i'), complex('4i')), complex('0.75 - 0.5i')); + approx.deepEqual(dotDivide(complex('2i'), complex('4i')), 0.5); + approx.deepEqual(dotDivide(4, complex('1+2i')), complex('0.8 - 1.6i')); }); it('should divide a unit by a number', function() { - assert.equal(dotdivide(math.unit('5 m'), 10).toString(), '500 mm'); + assert.equal(dotDivide(math.unit('5 m'), 10).toString(), '500 mm'); }); it('should throw an error if dividing a number by a unit', function() { - assert.throws(function () {dotdivide(10, math.unit('5 m')).toString()}); + assert.throws(function () {dotDivide(10, math.unit('5 m')).toString()}); }); it('should divide all the elements of a matrix by one number', function() { - assert.deepEqual(dotdivide([2,4,6], 2), [1,2,3]); + assert.deepEqual(dotDivide([2,4,6], 2), [1,2,3]); var a = math.matrix([[1,2],[3,4]]); - assert.deepEqual(dotdivide(a, 2), math.matrix([[0.5,1],[1.5,2]])); - assert.deepEqual(dotdivide(a.valueOf(), 2), [[0.5,1],[1.5,2]]); - assert.deepEqual(dotdivide([], 2), []); - assert.deepEqual(dotdivide([], 2), []); + assert.deepEqual(dotDivide(a, 2), math.matrix([[0.5,1],[1.5,2]])); + assert.deepEqual(dotDivide(a.valueOf(), 2), [[0.5,1],[1.5,2]]); + assert.deepEqual(dotDivide([], 2), []); + assert.deepEqual(dotDivide([], 2), []); }); it('should divide 1 over a matrix element-wise', function() { - approx.deepEqual(math.format(dotdivide(1, [ + approx.deepEqual(math.format(dotDivide(1, [ [ 1, 4, 7], [ 3, 0, 5], [-1, 9, 11] @@ -75,11 +75,11 @@ describe('dotdivide', function() { it('should perform matrix element-wise matrix division', function() { a = math.matrix([[1,2],[3,4]]); b = math.matrix([[5,6],[7,8]]); - assert.deepEqual(dotdivide(a, b), math.matrix([[1/5, 2/6], [3/7,4/8]])); + assert.deepEqual(dotDivide(a, b), math.matrix([[1/5, 2/6], [3/7,4/8]])); }); it('should throw an error when dividing element-wise by a matrix with differing size', function() { - assert.throws(function () {dotdivide(a, [[1]])}); + assert.throws(function () {dotDivide(a, [[1]])}); }); }); diff --git a/test/function/arithmetic/dotequal.test.js b/test/function/arithmetic/dotequal.test.js new file mode 100644 index 000000000..3d91c703f --- /dev/null +++ b/test/function/arithmetic/dotequal.test.js @@ -0,0 +1,40 @@ +// test equal +var assert = require('assert'), + mathjs = require('../../../index'), + math = mathjs(), + error = require('../../../lib/error/index'), + bignumber = math.bignumber, + complex = math.complex, + matrix = math.matrix, + unit = math.unit, + dotEqual = math.dotEqual; + +describe('dotEqual', function() { + + it('should compare two scalars correctly', function() { + assert.equal(dotEqual(2, 3), false); + assert.equal(dotEqual(2, 2), true); + assert.equal(dotEqual(0, 0), true); + assert.equal(dotEqual(-2, 2), false); + }); + + it('should compare a string an matrix elementwise', function() { + assert.deepEqual(dotEqual('B', ['A', 'B', 'C']), [false, true, false]); + assert.deepEqual(dotEqual(['A', 'B', 'C'], 'B'), [false, true, false]); + }); + + it('should compare two matrices element wise', function() { + assert.deepEqual(dotEqual([1,4,5], [3,4,5]), [false, true, true]); + assert.deepEqual(dotEqual([1,4,5], matrix([3,4,5])), matrix([false, true, true])); + }); + + it('should throw an error if matrices have different sizes', function() { + assert.throws(function () {dotEqual([1,4,5], [3,4])}); + }); + + it('should throw an error in case of invalid number of arguments', function() { + assert.throws(function () {dotEqual(1)}, error.ArgumentsError); + assert.throws(function () {dotEqual(1, 2, 3)}, error.ArgumentsError); + }); + +}); diff --git a/test/function/arithmetic/dotmultiply.test.js b/test/function/arithmetic/dotmultiply.test.js index fc046d750..5155f781c 100644 --- a/test/function/arithmetic/dotmultiply.test.js +++ b/test/function/arithmetic/dotmultiply.test.js @@ -1,9 +1,9 @@ -// test dotmultiply (element-wise multiply) +// test dotMultiply (element-wise multiply) var assert = require('assert'), math = require('../../../index')(), approx = require('../../../tools/approx'), error = require('../../../lib/error/index'), - dotmultiply = math.dotmultiply, + dotMultiply = math.dotMultiply, divide = math.divide, matrix = math.matrix, complex = math.complex, @@ -11,53 +11,53 @@ var assert = require('assert'), i = math.i, unit = math.unit; -describe('dotmultiply', function() { +describe('dotMultiply', function() { it('should multiply 2 numbers', function() { // number - approx.equal(dotmultiply(2, 3), 6); - approx.equal(dotmultiply(-2, 3), -6); - approx.equal(dotmultiply(-2, -3), 6); - approx.equal(dotmultiply(5, 0), 0); - approx.equal(dotmultiply(0, 5), 0); + approx.equal(dotMultiply(2, 3), 6); + approx.equal(dotMultiply(-2, 3), -6); + approx.equal(dotMultiply(-2, -3), 6); + approx.equal(dotMultiply(5, 0), 0); + approx.equal(dotMultiply(0, 5), 0); }); it('should multiply booleans', function() { - assert.equal(dotmultiply(true, true), 1); - assert.equal(dotmultiply(true, false), 0); - assert.equal(dotmultiply(false, true), 0); - assert.equal(dotmultiply(false, false), 0); + assert.equal(dotMultiply(true, true), 1); + assert.equal(dotMultiply(true, false), 0); + assert.equal(dotMultiply(false, true), 0); + assert.equal(dotMultiply(false, false), 0); }); it('should multiply mixed numbers and booleans', function() { - assert.equal(dotmultiply(2, true), 2); - assert.equal(dotmultiply(2, false), 0); - assert.equal(dotmultiply(true, 2), 2); - assert.equal(dotmultiply(false, 2), 0); + assert.equal(dotMultiply(2, true), 2); + assert.equal(dotMultiply(2, false), 0); + assert.equal(dotMultiply(true, 2), 2); + assert.equal(dotMultiply(false, 2), 0); }); it('should multiply 2 complex numbers', function() { // complex - approx.deepEqual(dotmultiply(complex(2, 3), 2), complex(4, 6)); - approx.deepEqual(dotmultiply(complex(2, -3), 2), complex(4, -6)); - approx.deepEqual(dotmultiply(complex(0, 1), complex(2, 3)), complex(-3, 2)); - approx.deepEqual(dotmultiply(complex(2, 3), complex(2, 3)), complex(-5, 12)); - approx.deepEqual(dotmultiply(2, complex(2, 3)), complex(4, 6)); + approx.deepEqual(dotMultiply(complex(2, 3), 2), complex(4, 6)); + approx.deepEqual(dotMultiply(complex(2, -3), 2), complex(4, -6)); + approx.deepEqual(dotMultiply(complex(0, 1), complex(2, 3)), complex(-3, 2)); + approx.deepEqual(dotMultiply(complex(2, 3), complex(2, 3)), complex(-5, 12)); + approx.deepEqual(dotMultiply(2, complex(2, 3)), complex(4, 6)); approx.deepEqual(divide(complex(-5, 12), complex(2, 3)), complex(2, 3)); }); it('should multiply a unit by a number', function() { // unit - assert.equal(dotmultiply(2, unit('5 mm')).toString(), '10 mm'); - assert.equal(dotmultiply(2, unit('5 mm')).toString(), '10 mm'); - assert.equal(dotmultiply(unit('5 mm'), 2).toString(), '10 mm'); - assert.equal(dotmultiply(unit('5 mm'), 0).toString(), '0 m'); + assert.equal(dotMultiply(2, unit('5 mm')).toString(), '10 mm'); + assert.equal(dotMultiply(2, unit('5 mm')).toString(), '10 mm'); + assert.equal(dotMultiply(unit('5 mm'), 2).toString(), '10 mm'); + assert.equal(dotMultiply(unit('5 mm'), 0).toString(), '0 m'); }); it('should throw an error with strings', function() { // string - assert.throws(function () {dotmultiply("hello", "world")}); - assert.throws(function () {dotmultiply("hello", 2)}); + assert.throws(function () {dotMultiply("hello", "world")}); + assert.throws(function () {dotMultiply("hello", 2)}); }); var a = matrix([[1,2],[3,4]]); @@ -67,27 +67,27 @@ describe('dotmultiply', function() { it('should multiply a all elements in a matrix by a number', function() { // matrix, array, range - approx.deepEqual(dotmultiply(a, 3), matrix([[3,6],[9,12]])); - approx.deepEqual(dotmultiply(3, a), matrix([[3,6],[9,12]])); + approx.deepEqual(dotMultiply(a, 3), matrix([[3,6],[9,12]])); + approx.deepEqual(dotMultiply(3, a), matrix([[3,6],[9,12]])); }); it('should perform element-wise matrix multiplication', function() { - approx.deepEqual(dotmultiply(a, b), matrix([[5,12],[21,32]])); - approx.deepEqual(dotmultiply([[1,2],[3,4]], [[5,6],[7,8]]), [[5,12],[21,32]]); - approx.deepEqual(dotmultiply([1,2,3,4], 2), [2, 4, 6, 8]); + approx.deepEqual(dotMultiply(a, b), matrix([[5,12],[21,32]])); + approx.deepEqual(dotMultiply([[1,2],[3,4]], [[5,6],[7,8]]), [[5,12],[21,32]]); + approx.deepEqual(dotMultiply([1,2,3,4], 2), [2, 4, 6, 8]); }); it('should throw an error if matrices are of different sizes', function() { - assert.throws(function () {dotmultiply(a, c)}); - assert.throws(function () {dotmultiply(d, a)}); - assert.throws(function () {dotmultiply(d, b)}); - assert.throws(function () {dotmultiply(d, c)}); - assert.throws(function () {dotmultiply(c, b)}); + assert.throws(function () {dotMultiply(a, c)}); + assert.throws(function () {dotMultiply(d, a)}); + assert.throws(function () {dotMultiply(d, b)}); + assert.throws(function () {dotMultiply(d, c)}); + assert.throws(function () {dotMultiply(c, b)}); }); it('should throw an error in case of invalid number of arguments', function() { - assert.throws(function () {dotmultiply(1)}, error.ArgumentsError); - assert.throws(function () {dotmultiply(1, 2, 3)}, error.ArgumentsError); + assert.throws(function () {dotMultiply(1)}, error.ArgumentsError); + assert.throws(function () {dotMultiply(1, 2, 3)}, error.ArgumentsError); }); }); diff --git a/test/function/arithmetic/dotpow.test.js b/test/function/arithmetic/dotpow.test.js index 274ed2462..fc66957fe 100644 --- a/test/function/arithmetic/dotpow.test.js +++ b/test/function/arithmetic/dotpow.test.js @@ -7,95 +7,95 @@ var assert = require('assert'), matrix = math.matrix, unit = math.unit, range = math.range, - dotpow = math.dotpow; + dotPow = math.dotPow; -describe('dotpow', function() { +describe('dotPow', function() { it('should elevate a number to the given power', function() { - approx.deepEqual(dotpow(2,3), 8); - approx.deepEqual(dotpow(2,4), 16); - approx.deepEqual(dotpow(-2,2), 4); - approx.deepEqual(dotpow(3,3), 27); - approx.deepEqual(dotpow(3,-2), 0.111111111111111); - approx.deepEqual(dotpow(-3,-2), 0.111111111111111); - approx.deepEqual(dotpow(3,-3), 0.0370370370370370); - approx.deepEqual(dotpow(-3,-3), -0.0370370370370370); - approx.deepEqual(dotpow(2,1.5), 2.82842712474619); - approx.deepEqual(dotpow(-2,1.5), complex(0, -2.82842712474619)); + approx.deepEqual(dotPow(2,3), 8); + approx.deepEqual(dotPow(2,4), 16); + approx.deepEqual(dotPow(-2,2), 4); + approx.deepEqual(dotPow(3,3), 27); + approx.deepEqual(dotPow(3,-2), 0.111111111111111); + approx.deepEqual(dotPow(-3,-2), 0.111111111111111); + approx.deepEqual(dotPow(3,-3), 0.0370370370370370); + approx.deepEqual(dotPow(-3,-3), -0.0370370370370370); + approx.deepEqual(dotPow(2,1.5), 2.82842712474619); + approx.deepEqual(dotPow(-2,1.5), complex(0, -2.82842712474619)); }); it('should elevate booleans to the given power', function() { - assert.equal(dotpow(true, true), 1); - assert.equal(dotpow(true, false), 1); - assert.equal(dotpow(false, true), 0); - assert.equal(dotpow(false, false), 1); + assert.equal(dotPow(true, true), 1); + assert.equal(dotPow(true, false), 1); + assert.equal(dotPow(false, true), 0); + assert.equal(dotPow(false, false), 1); }); it('should add mixed numbers and booleans', function() { - assert.equal(dotpow(2, true), 2); - assert.equal(dotpow(2, false), 1); - assert.equal(dotpow(true, 2), 1); - assert.equal(dotpow(false, 2), 0); + assert.equal(dotPow(2, true), 2); + assert.equal(dotPow(2, false), 1); + assert.equal(dotPow(true, 2), 1); + assert.equal(dotPow(false, 2), 0); }); it('should throw an error in case of invalid number of arguments', function() { - assert.throws(function () {dotpow(1)}, error.ArgumentsError); - assert.throws(function () {dotpow(1, 2, 3)}, error.ArgumentsError); + assert.throws(function () {dotPow(1)}, error.ArgumentsError); + assert.throws(function () {dotPow(1, 2, 3)}, error.ArgumentsError); }); it('should elevate a complex number to the given power', function() { - approx.deepEqual(dotpow(complex(-1,-1),complex(-1,-1)), complex('-0.0284750589322119 + 0.0606697332231795i')); - approx.deepEqual(dotpow(complex(-1,-1),complex(-1,1)), complex('-6.7536199239765713 + 3.1697803027015614i')); - approx.deepEqual(dotpow(complex(-1,-1),complex(0,-1)), complex('0.0891447921553914 - 0.0321946742909677i')); - approx.deepEqual(dotpow(complex(-1,-1),complex(0,1)), complex('9.92340022667813 + 3.58383962127501i')); - approx.deepEqual(dotpow(complex(-1,-1),complex(1,-1)), complex('-0.1213394664463591 - 0.0569501178644237i')); - approx.deepEqual(dotpow(complex(-1,-1),complex(1,1)), complex('-6.3395606054031211 - 13.5072398479531426i')); - approx.deepEqual(dotpow(complex(-1,1),complex(-1,-1)), complex('-6.7536199239765713 - 3.1697803027015614i')); - approx.deepEqual(dotpow(complex(-1,1),complex(-1,1)), complex('-0.0284750589322119 - 0.0606697332231795i')); - approx.deepEqual(dotpow(complex(-1,1),complex(0,-1)), complex('9.92340022667813 - 3.58383962127501i')); - approx.deepEqual(dotpow(complex(-1,1),complex(0,1)), complex('0.0891447921553914 + 0.0321946742909677i')); - approx.deepEqual(dotpow(complex(-1,1),complex(1,-1)), complex('-6.3395606054031211 + 13.5072398479531426i')); - approx.deepEqual(dotpow(complex(-1,1),complex(1,1)), complex('-0.1213394664463591 + 0.0569501178644237i')); + approx.deepEqual(dotPow(complex(-1,-1),complex(-1,-1)), complex('-0.0284750589322119 + 0.0606697332231795i')); + approx.deepEqual(dotPow(complex(-1,-1),complex(-1,1)), complex('-6.7536199239765713 + 3.1697803027015614i')); + approx.deepEqual(dotPow(complex(-1,-1),complex(0,-1)), complex('0.0891447921553914 - 0.0321946742909677i')); + approx.deepEqual(dotPow(complex(-1,-1),complex(0,1)), complex('9.92340022667813 + 3.58383962127501i')); + approx.deepEqual(dotPow(complex(-1,-1),complex(1,-1)), complex('-0.1213394664463591 - 0.0569501178644237i')); + approx.deepEqual(dotPow(complex(-1,-1),complex(1,1)), complex('-6.3395606054031211 - 13.5072398479531426i')); + approx.deepEqual(dotPow(complex(-1,1),complex(-1,-1)), complex('-6.7536199239765713 - 3.1697803027015614i')); + approx.deepEqual(dotPow(complex(-1,1),complex(-1,1)), complex('-0.0284750589322119 - 0.0606697332231795i')); + approx.deepEqual(dotPow(complex(-1,1),complex(0,-1)), complex('9.92340022667813 - 3.58383962127501i')); + approx.deepEqual(dotPow(complex(-1,1),complex(0,1)), complex('0.0891447921553914 + 0.0321946742909677i')); + approx.deepEqual(dotPow(complex(-1,1),complex(1,-1)), complex('-6.3395606054031211 + 13.5072398479531426i')); + approx.deepEqual(dotPow(complex(-1,1),complex(1,1)), complex('-0.1213394664463591 + 0.0569501178644237i')); - approx.deepEqual(dotpow(complex(0,-1),complex(-1,-1)), complex('0.000000000000000 + 0.207879576350762i')); - approx.deepEqual(dotpow(complex(0,-1),complex(-1,1)), complex('0.000000000000000 + 4.810477380965351i')); - approx.deepEqual(dotpow(complex(0,-1),complex(1,-1)), complex('0.000000000000000 - 0.207879576350762i')); - approx.deepEqual(dotpow(complex(0,-1),complex(1,1)), complex('0.000000000000000 - 4.810477380965351i')); - approx.deepEqual(dotpow(complex(0,1),complex(-1,-1)), complex('0.000000000000000 - 4.810477380965351i')); - approx.deepEqual(dotpow(complex(0,1),complex(-1,1)), complex('0.000000000000000 - 0.207879576350762i')); - approx.deepEqual(dotpow(complex(0,1),complex(1,-1)), complex('0.000000000000000 + 4.810477380965351i')); - approx.deepEqual(dotpow(complex(0,1),complex(1,1)), complex('0.000000000000000 + 0.207879576350762i')); + approx.deepEqual(dotPow(complex(0,-1),complex(-1,-1)), complex('0.000000000000000 + 0.207879576350762i')); + approx.deepEqual(dotPow(complex(0,-1),complex(-1,1)), complex('0.000000000000000 + 4.810477380965351i')); + approx.deepEqual(dotPow(complex(0,-1),complex(1,-1)), complex('0.000000000000000 - 0.207879576350762i')); + approx.deepEqual(dotPow(complex(0,-1),complex(1,1)), complex('0.000000000000000 - 4.810477380965351i')); + approx.deepEqual(dotPow(complex(0,1),complex(-1,-1)), complex('0.000000000000000 - 4.810477380965351i')); + approx.deepEqual(dotPow(complex(0,1),complex(-1,1)), complex('0.000000000000000 - 0.207879576350762i')); + approx.deepEqual(dotPow(complex(0,1),complex(1,-1)), complex('0.000000000000000 + 4.810477380965351i')); + approx.deepEqual(dotPow(complex(0,1),complex(1,1)), complex('0.000000000000000 + 0.207879576350762i')); - approx.deepEqual(dotpow(complex(1,-1),complex(-1,-1)), complex('0.2918503793793073 + 0.1369786269150605i')); - approx.deepEqual(dotpow(complex(1,-1),complex(-1,1)), complex('0.6589325864505904 + 1.4039396486303144i')); - approx.deepEqual(dotpow(complex(1,-1),complex(0,-1)), complex('0.428829006294368 - 0.154871752464247i')); - approx.deepEqual(dotpow(complex(1,-1),complex(0,1)), complex('2.062872235080905 + 0.745007062179724i')); - approx.deepEqual(dotpow(complex(1,-1),complex(1,-1)), complex('0.2739572538301211 - 0.5837007587586147i')); - approx.deepEqual(dotpow(complex(1,-1),complex(1,1)), complex('2.8078792972606288 - 1.3178651729011805i')); - approx.deepEqual(dotpow(complex(1,1),complex(-1,-1)), complex('0.6589325864505904 - 1.4039396486303144i')); - approx.deepEqual(dotpow(complex(1,1),complex(-1,1)), complex('0.2918503793793073 - 0.1369786269150605i')); - approx.deepEqual(dotpow(complex(1,1),complex(0,-1)), complex('2.062872235080905 - 0.745007062179724i')); - approx.deepEqual(dotpow(complex(1,1),complex(0,1)), complex('0.428829006294368 + 0.154871752464247i')); - approx.deepEqual(dotpow(complex(1,1),complex(1,-1)), complex('2.8078792972606288 + 1.3178651729011805i')); - approx.deepEqual(dotpow(complex(1,1),complex(1,1)), complex('0.2739572538301211 + 0.5837007587586147i')); + approx.deepEqual(dotPow(complex(1,-1),complex(-1,-1)), complex('0.2918503793793073 + 0.1369786269150605i')); + approx.deepEqual(dotPow(complex(1,-1),complex(-1,1)), complex('0.6589325864505904 + 1.4039396486303144i')); + approx.deepEqual(dotPow(complex(1,-1),complex(0,-1)), complex('0.428829006294368 - 0.154871752464247i')); + approx.deepEqual(dotPow(complex(1,-1),complex(0,1)), complex('2.062872235080905 + 0.745007062179724i')); + approx.deepEqual(dotPow(complex(1,-1),complex(1,-1)), complex('0.2739572538301211 - 0.5837007587586147i')); + approx.deepEqual(dotPow(complex(1,-1),complex(1,1)), complex('2.8078792972606288 - 1.3178651729011805i')); + approx.deepEqual(dotPow(complex(1,1),complex(-1,-1)), complex('0.6589325864505904 - 1.4039396486303144i')); + approx.deepEqual(dotPow(complex(1,1),complex(-1,1)), complex('0.2918503793793073 - 0.1369786269150605i')); + approx.deepEqual(dotPow(complex(1,1),complex(0,-1)), complex('2.062872235080905 - 0.745007062179724i')); + approx.deepEqual(dotPow(complex(1,1),complex(0,1)), complex('0.428829006294368 + 0.154871752464247i')); + approx.deepEqual(dotPow(complex(1,1),complex(1,-1)), complex('2.8078792972606288 + 1.3178651729011805i')); + approx.deepEqual(dotPow(complex(1,1),complex(1,1)), complex('0.2739572538301211 + 0.5837007587586147i')); }); it('should throw an error with units', function() { - assert.throws(function () {dotpow(unit('5cm'))}); + assert.throws(function () {dotPow(unit('5cm'))}); }); it('should throw an error with strings', function() { - assert.throws(function () {dotpow('text')}); + assert.throws(function () {dotPow('text')}); }); it('should elevate each element in a matrix to the given power', function() { var a = [[1,2],[3,4]]; var res = [[1,4],[9,16]]; - approx.deepEqual(dotpow(a, 2), res); - approx.deepEqual(dotpow(a, 2.5), [[1,5.65685424949238], [15.58845726811990, 32]]); - approx.deepEqual(dotpow(3, [2,3]), [9,27]); - approx.deepEqual(dotpow(matrix(a), 2), matrix(res)); - approx.deepEqual(dotpow([[1,2,3],[4,5,6]],2), [[1,4,9],[16,25,36]]); + approx.deepEqual(dotPow(a, 2), res); + approx.deepEqual(dotPow(a, 2.5), [[1,5.65685424949238], [15.58845726811990, 32]]); + approx.deepEqual(dotPow(3, [2,3]), [9,27]); + approx.deepEqual(dotPow(matrix(a), 2), matrix(res)); + approx.deepEqual(dotPow([[1,2,3],[4,5,6]],2), [[1,4,9],[16,25,36]]); }); }); \ No newline at end of file diff --git a/test/function/arithmetic/dotunequal.test.js b/test/function/arithmetic/dotunequal.test.js new file mode 100644 index 000000000..713ff13ff --- /dev/null +++ b/test/function/arithmetic/dotunequal.test.js @@ -0,0 +1,41 @@ +// test equal +var assert = require('assert'), + mathjs = require('../../../index'), + math = mathjs(), + error = require('../../../lib/error/index'), + bignumber = math.bignumber, + complex = math.complex, + matrix = math.matrix, + unit = math.unit, + dotUnequal = math.dotUnequal; + +describe('dotUnequal', function() { + + it('should compare two scalars correctly', function() { + assert.equal(dotUnequal(2, 3), true); + assert.equal(dotUnequal(2, 2), false); + assert.equal(dotUnequal(0, 0), false); + assert.equal(dotUnequal(-2, 2), true); + assert.equal(dotUnequal(true, 1), false); + }); + + it('should compare a string an matrix elementwise', function() { + assert.deepEqual(dotUnequal('B', ['A', 'B', 'C']), [true, false, true]); + assert.deepEqual(dotUnequal(['A', 'B', 'C'], 'B'), [true, false, true]); + }); + + it('should compare two matrices element wise', function() { + assert.deepEqual(dotUnequal([1,4,5], [3,4,5]), [true, false, false]); + assert.deepEqual(dotUnequal([1,4,5], matrix([3,4,5])), matrix([true, false, false])); + }); + + it('should throw an error if matrices have different sizes', function() { + assert.throws(function () {dotUnequal([1,4,5], [3,4])}); + }); + + it('should throw an error in case of invalid number of arguments', function() { + assert.throws(function () {dotUnequal(1)}, error.ArgumentsError); + assert.throws(function () {dotUnequal(1, 2, 3)}, error.ArgumentsError); + }); + +}); diff --git a/test/function/arithmetic/equal.test.js b/test/function/arithmetic/equal.test.js index 01852a54c..849ea15c0 100644 --- a/test/function/arithmetic/equal.test.js +++ b/test/function/arithmetic/equal.test.js @@ -130,18 +130,32 @@ describe('equal', function() { assert.equal(equal('hello', 'hello'), true); }); - it('should compare a string an matrix elementwise', function() { - assert.deepEqual(equal('B', ['A', 'B', 'C']), [false, true, false]); - assert.deepEqual(equal(['A', 'B', 'C'], 'B'), [false, true, false]); + it('should compare a string an matrix', function() { + assert.deepEqual(equal('B', ['A', 'B', 'C']), false); + assert.deepEqual(equal(['A', 'B', 'C'], 'B'), false); }); - it('should compare two matrices correctly', function() { - assert.deepEqual(equal([1,4,5], [3,4,5]), [false, true, true]); - assert.deepEqual(equal([1,4,5], matrix([3,4,5])), matrix([false, true, true])); + it('should compare two matrices (deepEqual)', function() { + assert.deepEqual(equal([1,4,5], [3,4,5]), false); + assert.deepEqual(equal([1,4,5], [1,4,5]), true); + assert.deepEqual(equal([1,4,5], [1,4]), false); + assert.deepEqual(equal([1,4], [1,4,5]), false); + assert.deepEqual(equal([1,4,5], matrix([3,4,5])), false); + assert.deepEqual(equal([1,4,5], matrix([1,4,5])), true); + assert.deepEqual(equal(matrix([1,4,5]), matrix([1,4,5])), true); + + assert.deepEqual(equal(matrix([[1,2], [3,4]]), matrix([[1,2], [3,4]])), true); + assert.deepEqual(equal(matrix([[1,2], [3,4]]), matrix([[1,2], [3,5]])), false); + assert.deepEqual(equal(matrix([[1,2], [3,4]]), matrix([[1,2], [3,4], [5,6]])), false); + assert.deepEqual(equal(matrix([[1,2], [3,4], [5,6]]), matrix([[1,2], [3,4]])), false); }); - it('should throw an error if matrices have different sizes', function() { - assert.throws(function () {equal([1,4,5], [3,4])}); + it('should compare two matrices with mixed types', function() { + assert.deepEqual(equal([1,4,5], [true,4,5]), true); + assert.deepEqual(equal([2,3], [2, bignumber(3)]), true); + assert.deepEqual(equal([2,3], [2, bignumber(4)]), false); + assert.deepEqual(equal([complex(2,3),3], [complex(2,3),3]), true); + assert.deepEqual(equal([complex(2,3),3], [complex(2,4),3]), false); }); it('should throw an error in case of invalid number of arguments', function() { diff --git a/test/function/arithmetic/largereq.test.js b/test/function/arithmetic/largereq.test.js index 57a4e7f1a..c802c8c07 100644 --- a/test/function/arithmetic/largereq.test.js +++ b/test/function/arithmetic/largereq.test.js @@ -1,4 +1,4 @@ -// test largereq +// test largerEq var assert = require('assert'), mathjs = require('../../../index') math = mathjs(), @@ -7,132 +7,132 @@ var assert = require('assert'), complex = math.complex, matrix = math.matrix, unit = math.unit, - largereq = math.largereq; + largerEq = math.largerEq; -describe('largereq', function() { +describe('largerEq', function() { it('should compare two numbers correctly', function() { - assert.equal(largereq(2, 3), false); - assert.equal(largereq(2, 2), true); - assert.equal(largereq(2, 1), true); - assert.equal(largereq(0, 0), true); - assert.equal(largereq(-2, 2), false); - assert.equal(largereq(-2, -3), true); - assert.equal(largereq(-3, -2), false); + assert.equal(largerEq(2, 3), false); + assert.equal(largerEq(2, 2), true); + assert.equal(largerEq(2, 1), true); + assert.equal(largerEq(0, 0), true); + assert.equal(largerEq(-2, 2), false); + assert.equal(largerEq(-2, -3), true); + assert.equal(largerEq(-3, -2), false); }); it('should compare two floating point numbers correctly', function() { // Infinity - assert.equal(largereq(Number.POSITIVE_INFINITY, Number.POSITIVE_INFINITY), true); - assert.equal(largereq(Number.NEGATIVE_INFINITY, Number.NEGATIVE_INFINITY), true); - assert.equal(largereq(Number.POSITIVE_INFINITY, Number.NEGATIVE_INFINITY), true); - assert.equal(largereq(Number.NEGATIVE_INFINITY, Number.POSITIVE_INFINITY), false); - assert.equal(largereq(Number.POSITIVE_INFINITY, 2.0), true); - assert.equal(largereq(2.0, Number.POSITIVE_INFINITY), false); - assert.equal(largereq(Number.NEGATIVE_INFINITY, 2.0), false); - assert.equal(largereq(2.0, Number.NEGATIVE_INFINITY), true); + assert.equal(largerEq(Number.POSITIVE_INFINITY, Number.POSITIVE_INFINITY), true); + assert.equal(largerEq(Number.NEGATIVE_INFINITY, Number.NEGATIVE_INFINITY), true); + assert.equal(largerEq(Number.POSITIVE_INFINITY, Number.NEGATIVE_INFINITY), true); + assert.equal(largerEq(Number.NEGATIVE_INFINITY, Number.POSITIVE_INFINITY), false); + assert.equal(largerEq(Number.POSITIVE_INFINITY, 2.0), true); + assert.equal(largerEq(2.0, Number.POSITIVE_INFINITY), false); + assert.equal(largerEq(Number.NEGATIVE_INFINITY, 2.0), false); + assert.equal(largerEq(2.0, Number.NEGATIVE_INFINITY), true); // floating point numbers - assert.equal(largereq(0.3 - 0.2, 0.1), true); + assert.equal(largerEq(0.3 - 0.2, 0.1), true); }); it('should compare two booleans', function() { - assert.equal(largereq(true, true), true); - assert.equal(largereq(true, false), true); - assert.equal(largereq(false, true), false); - assert.equal(largereq(false, false), true); + assert.equal(largerEq(true, true), true); + assert.equal(largerEq(true, false), true); + assert.equal(largerEq(false, true), false); + assert.equal(largerEq(false, false), true); }); it('should compare mixed numbers and booleans', function() { - assert.equal(largereq(2, true), true); - assert.equal(largereq(0, true), false); - assert.equal(largereq(true, 2), false); - assert.equal(largereq(true, 1), true); - assert.equal(largereq(false, 0), true); + assert.equal(largerEq(2, true), true); + assert.equal(largerEq(0, true), false); + assert.equal(largerEq(true, 2), false); + assert.equal(largerEq(true, 1), true); + assert.equal(largerEq(false, 0), true); }); it('should compare bignumbers', function() { - assert.equal(largereq(bignumber(2), bignumber(3)), false); - assert.equal(largereq(bignumber(2), bignumber(2)), true); - assert.equal(largereq(bignumber(3), bignumber(2)), true); - assert.equal(largereq(bignumber(0), bignumber(0)), true); - assert.equal(largereq(bignumber(-2), bignumber(2)), false); + assert.equal(largerEq(bignumber(2), bignumber(3)), false); + assert.equal(largerEq(bignumber(2), bignumber(2)), true); + assert.equal(largerEq(bignumber(3), bignumber(2)), true); + assert.equal(largerEq(bignumber(0), bignumber(0)), true); + assert.equal(largerEq(bignumber(-2), bignumber(2)), false); }); it('should compare mixed numbers and bignumbers', function() { - assert.equal(largereq(bignumber(2), 3), false); - assert.equal(largereq(2, bignumber(2)), true); + assert.equal(largerEq(bignumber(2), 3), false); + assert.equal(largerEq(2, bignumber(2)), true); - assert.equal(largereq(1/3, bignumber(1).div(3)), true); - assert.equal(largereq(bignumber(1).div(3), 1/3), true); + assert.equal(largerEq(1/3, bignumber(1).div(3)), true); + assert.equal(largerEq(bignumber(1).div(3), 1/3), true); }); it('should compare mixed booleans and bignumbers', function() { - assert.equal(largereq(bignumber(0.1), true), false); - assert.equal(largereq(bignumber(1), true), true); - assert.equal(largereq(bignumber(1), false), true); - assert.equal(largereq(false, bignumber(0)), true); - assert.equal(largereq(true, bignumber(0)), true); - assert.equal(largereq(true, bignumber(1)), true); + assert.equal(largerEq(bignumber(0.1), true), false); + assert.equal(largerEq(bignumber(1), true), true); + assert.equal(largerEq(bignumber(1), false), true); + assert.equal(largerEq(false, bignumber(0)), true); + assert.equal(largerEq(true, bignumber(0)), true); + assert.equal(largerEq(true, bignumber(1)), true); }); it('should compare two units correctly', function() { - assert.equal(largereq(unit('100cm'), unit('10inch')), true); - assert.equal(largereq(unit('99cm'), unit('1m')), false); - //assert.equal(largereq(unit('100cm'), unit('1m')), true); // dangerous, round-off errors - assert.equal(largereq(unit('101cm'), unit('1m')), true); + assert.equal(largerEq(unit('100cm'), unit('10inch')), true); + assert.equal(largerEq(unit('99cm'), unit('1m')), false); + //assert.equal(largerEq(unit('100cm'), unit('1m')), true); // dangerous, round-off errors + assert.equal(largerEq(unit('101cm'), unit('1m')), true); }); it('should apply configuration option epsilon', function() { var mymath = mathjs(); - assert.equal(mymath.largereq(1, 1.01), false); + assert.equal(mymath.largerEq(1, 1.01), false); mymath.config({epsilon: 1e-2}); - assert.equal(mymath.largereq(1, 1.01), true); + assert.equal(mymath.largerEq(1, 1.01), true); }); it('should throw an error if comparing a unit with a number', function() { - assert.throws(function () {largereq(unit('100cm'), 22)}); + assert.throws(function () {largerEq(unit('100cm'), 22)}); }); it('should throw an error for two measures of different units', function() { - assert.throws(function () {largereq(math.unit(5, 'km'), math.unit(100, 'gram')); }); + assert.throws(function () {largerEq(math.unit(5, 'km'), math.unit(100, 'gram')); }); }); it('should throw an error if comparing a unit with a bignumber', function() { - assert.throws(function () {largereq(unit('100cm'), bignumber(22))}); + assert.throws(function () {largerEq(unit('100cm'), bignumber(22))}); }); it('should perform lexical comparison for 2 strings', function() { - assert.equal(largereq('0', 0), true); - assert.equal(largereq('abd', 'abc'), true); - assert.equal(largereq('abc', 'abc'), true); - assert.equal(largereq('abc', 'abd'), false); + assert.equal(largerEq('0', 0), true); + assert.equal(largerEq('abd', 'abc'), true); + assert.equal(largerEq('abc', 'abc'), true); + assert.equal(largerEq('abc', 'abd'), false); }); it('should compare a string an matrix elementwise', function() { - assert.deepEqual(largereq('B', ['A', 'B', 'C']), [true, true, false]); - assert.deepEqual(largereq(['A', 'B', 'C'], 'B'), [false, true, true]); + assert.deepEqual(largerEq('B', ['A', 'B', 'C']), [true, true, false]); + assert.deepEqual(largerEq(['A', 'B', 'C'], 'B'), [false, true, true]); }); it('should perform element-wise comparison for two matrices of the same size', function() { - assert.deepEqual(largereq([1,4,6], [3,4,5]), [false, true, true]); - assert.deepEqual(largereq([1,4,6], matrix([3,4,5])), matrix([false, true, true])); + assert.deepEqual(largerEq([1,4,6], [3,4,5]), [false, true, true]); + assert.deepEqual(largerEq([1,4,6], matrix([3,4,5])), matrix([false, true, true])); }); it('should throw an error when comparing complex numbers', function() { - assert.throws(function () {largereq(complex(1,1), complex(1,2))}, TypeError); - assert.throws(function () {largereq(complex(2,1), 3)}, TypeError); - assert.throws(function () {largereq(3, complex(2,4))}, TypeError); - assert.throws(function () {largereq(math.bignumber(3), complex(2,4))}, TypeError); - assert.throws(function () {largereq(complex(2,4), math.bignumber(3))}, TypeError); + assert.throws(function () {largerEq(complex(1,1), complex(1,2))}, TypeError); + assert.throws(function () {largerEq(complex(2,1), 3)}, TypeError); + assert.throws(function () {largerEq(3, complex(2,4))}, TypeError); + assert.throws(function () {largerEq(math.bignumber(3), complex(2,4))}, TypeError); + assert.throws(function () {largerEq(complex(2,4), math.bignumber(3))}, TypeError); }); it('should throw an error if comparing two matrices of different sizes', function() { - assert.throws(function () {largereq([1,4,6], [3,4])}); + assert.throws(function () {largerEq([1,4,6], [3,4])}); }); it('should throw an error in case of invalid number of arguments', function() { - assert.throws(function () {largereq(1)}, error.ArgumentsError); - assert.throws(function () {largereq(1, 2, 3)}, error.ArgumentsError); + assert.throws(function () {largerEq(1)}, error.ArgumentsError); + assert.throws(function () {largerEq(1, 2, 3)}, error.ArgumentsError); }); }); \ No newline at end of file diff --git a/test/function/arithmetic/smallereq.test.js b/test/function/arithmetic/smallereq.test.js index f8b67e7bc..bf4cf34a5 100644 --- a/test/function/arithmetic/smallereq.test.js +++ b/test/function/arithmetic/smallereq.test.js @@ -7,137 +7,137 @@ var assert = require('assert'), complex = math.complex, matrix = math.matrix, unit = math.unit, - smallereq = math.smallereq; + smallerEq = math.smallerEq; -describe('smallereq', function() { +describe('smallerEq', function() { it('should compare two numbers correctly', function() { - assert.equal(smallereq(2, 3), true); - assert.equal(smallereq(2, 2), true); - assert.equal(smallereq(2, 1), false); - assert.equal(smallereq(0, 0), true); - assert.equal(smallereq(-2, 2), true); - assert.equal(smallereq(-2, -3), false); - assert.equal(smallereq(-2, -2), true); - assert.equal(smallereq(-3, -2), true); + assert.equal(smallerEq(2, 3), true); + assert.equal(smallerEq(2, 2), true); + assert.equal(smallerEq(2, 1), false); + assert.equal(smallerEq(0, 0), true); + assert.equal(smallerEq(-2, 2), true); + assert.equal(smallerEq(-2, -3), false); + assert.equal(smallerEq(-2, -2), true); + assert.equal(smallerEq(-3, -2), true); }); it('should compare two floating point numbers correctly', function() { // Infinity - assert.equal(smallereq(Number.POSITIVE_INFINITY, Number.POSITIVE_INFINITY), true); - assert.equal(smallereq(Number.NEGATIVE_INFINITY, Number.NEGATIVE_INFINITY), true); - assert.equal(smallereq(Number.POSITIVE_INFINITY, Number.NEGATIVE_INFINITY), false); - assert.equal(smallereq(Number.NEGATIVE_INFINITY, Number.POSITIVE_INFINITY), true); - assert.equal(smallereq(Number.POSITIVE_INFINITY, 2.0), false); - assert.equal(smallereq(2.0, Number.POSITIVE_INFINITY), true); - assert.equal(smallereq(Number.NEGATIVE_INFINITY, 2.0), true); - assert.equal(smallereq(2.0, Number.NEGATIVE_INFINITY), false); + assert.equal(smallerEq(Number.POSITIVE_INFINITY, Number.POSITIVE_INFINITY), true); + assert.equal(smallerEq(Number.NEGATIVE_INFINITY, Number.NEGATIVE_INFINITY), true); + assert.equal(smallerEq(Number.POSITIVE_INFINITY, Number.NEGATIVE_INFINITY), false); + assert.equal(smallerEq(Number.NEGATIVE_INFINITY, Number.POSITIVE_INFINITY), true); + assert.equal(smallerEq(Number.POSITIVE_INFINITY, 2.0), false); + assert.equal(smallerEq(2.0, Number.POSITIVE_INFINITY), true); + assert.equal(smallerEq(Number.NEGATIVE_INFINITY, 2.0), true); + assert.equal(smallerEq(2.0, Number.NEGATIVE_INFINITY), false); // floating point numbers - assert.equal(smallereq(0.3 - 0.2, 0.1), true); + assert.equal(smallerEq(0.3 - 0.2, 0.1), true); }); it('should compare two booleans', function() { - assert.equal(smallereq(true, true), true); - assert.equal(smallereq(true, false), false); - assert.equal(smallereq(false, true), true); - assert.equal(smallereq(false, false), true); + assert.equal(smallerEq(true, true), true); + assert.equal(smallerEq(true, false), false); + assert.equal(smallerEq(false, true), true); + assert.equal(smallerEq(false, false), true); }); it('should compare mixed numbers and booleans', function() { - assert.equal(smallereq(2, true), false); - assert.equal(smallereq(1, true), true); - assert.equal(smallereq(0, true), true); - assert.equal(smallereq(true, 2), true); - assert.equal(smallereq(true, 1), true); - assert.equal(smallereq(false, 2), true); + assert.equal(smallerEq(2, true), false); + assert.equal(smallerEq(1, true), true); + assert.equal(smallerEq(0, true), true); + assert.equal(smallerEq(true, 2), true); + assert.equal(smallerEq(true, 1), true); + assert.equal(smallerEq(false, 2), true); }); it('should compare bignumbers', function() { - assert.deepEqual(smallereq(bignumber(2), bignumber(3)), true); - assert.deepEqual(smallereq(bignumber(2), bignumber(2)), true); - assert.deepEqual(smallereq(bignumber(3), bignumber(2)), false); - assert.deepEqual(smallereq(bignumber(0), bignumber(0)), true); - assert.deepEqual(smallereq(bignumber(-2), bignumber(2)), true); + assert.deepEqual(smallerEq(bignumber(2), bignumber(3)), true); + assert.deepEqual(smallerEq(bignumber(2), bignumber(2)), true); + assert.deepEqual(smallerEq(bignumber(3), bignumber(2)), false); + assert.deepEqual(smallerEq(bignumber(0), bignumber(0)), true); + assert.deepEqual(smallerEq(bignumber(-2), bignumber(2)), true); }); it('should compare mixed numbers and bignumbers', function() { - assert.deepEqual(smallereq(bignumber(2), 3), true); - assert.deepEqual(smallereq(2, bignumber(2)), true); + assert.deepEqual(smallerEq(bignumber(2), 3), true); + assert.deepEqual(smallerEq(2, bignumber(2)), true); - assert.equal(smallereq(1/3, bignumber(1).div(3)), true); - assert.equal(smallereq(bignumber(1).div(3), 1/3), true); + assert.equal(smallerEq(1/3, bignumber(1).div(3)), true); + assert.equal(smallerEq(bignumber(1).div(3), 1/3), true); }); it('should compare mixed booleans and bignumbers', function() { - assert.deepEqual(smallereq(bignumber(0.1), true), true); - assert.deepEqual(smallereq(bignumber(1), true), true); - assert.deepEqual(smallereq(bignumber(1), false), false); - assert.deepEqual(smallereq(bignumber(0), false), true); - assert.deepEqual(smallereq(false, bignumber(0)), true); - assert.deepEqual(smallereq(true, bignumber(0)), false); - assert.deepEqual(smallereq(true, bignumber(1)), true); + assert.deepEqual(smallerEq(bignumber(0.1), true), true); + assert.deepEqual(smallerEq(bignumber(1), true), true); + assert.deepEqual(smallerEq(bignumber(1), false), false); + assert.deepEqual(smallerEq(bignumber(0), false), true); + assert.deepEqual(smallerEq(false, bignumber(0)), true); + assert.deepEqual(smallerEq(true, bignumber(0)), false); + assert.deepEqual(smallerEq(true, bignumber(1)), true); }); it('should compare two measures of the same unit correctly', function() { - assert.equal(smallereq(unit('100cm'), unit('10inch')), false); - assert.equal(smallereq(unit('99cm'), unit('1m')), true); - //assert.equal(smallereq(unit('100cm'), unit('1m')), true); // dangerous, round-off errors - assert.equal(smallereq(unit('101cm'), unit('1m')), false); + assert.equal(smallerEq(unit('100cm'), unit('10inch')), false); + assert.equal(smallerEq(unit('99cm'), unit('1m')), true); + //assert.equal(smallerEq(unit('100cm'), unit('1m')), true); // dangerous, round-off errors + assert.equal(smallerEq(unit('101cm'), unit('1m')), false); }); it('should apply configuration option epsilon', function() { var mymath = mathjs(); - assert.equal(mymath.smallereq(1.01, 1), false); + assert.equal(mymath.smallerEq(1.01, 1), false); mymath.config({epsilon: 1e-2}); - assert.equal(mymath.smallereq(1.01, 1), true); + assert.equal(mymath.smallerEq(1.01, 1), true); }); it('should throw an error if comparing a unit with a number', function() { - assert.throws(function () {smallereq(unit('100cm'), 22)}); - assert.throws(function () {smallereq(22, unit('100cm'))}); + assert.throws(function () {smallerEq(unit('100cm'), 22)}); + assert.throws(function () {smallerEq(22, unit('100cm'))}); }); it('should throw an error for two measures of different units', function() { - assert.throws(function () {smallereq(math.unit(5, 'km'), math.unit(100, 'gram'));}); + assert.throws(function () {smallerEq(math.unit(5, 'km'), math.unit(100, 'gram'));}); }); it('should throw an error if comparing a unit with a bignumber', function() { - assert.throws(function () {smallereq(unit('100cm'), bignumber(22))}); - assert.throws(function () {smallereq(bignumber(22), unit('100cm'))}); + assert.throws(function () {smallerEq(unit('100cm'), bignumber(22))}); + assert.throws(function () {smallerEq(bignumber(22), unit('100cm'))}); }); it('should perform lexical comparison of two strings', function() { - assert.equal(smallereq('0', 0), true); - assert.equal(smallereq('abd', 'abc'), false); - assert.equal(smallereq('abc', 'abc'), true); - assert.equal(smallereq('abc', 'abd'), true); + assert.equal(smallerEq('0', 0), true); + assert.equal(smallerEq('abd', 'abc'), false); + assert.equal(smallerEq('abc', 'abc'), true); + assert.equal(smallerEq('abc', 'abd'), true); }); it('should compare a string an matrix elementwise', function() { - assert.deepEqual(smallereq('B', ['A', 'B', 'C']), [false, true, true]); - assert.deepEqual(smallereq(['A', 'B', 'C'], 'B'), [true, true, false]); + assert.deepEqual(smallerEq('B', ['A', 'B', 'C']), [false, true, true]); + assert.deepEqual(smallerEq(['A', 'B', 'C'], 'B'), [true, true, false]); }); it('should perform element-wise comparison on two matrices', function() { - assert.deepEqual(smallereq([1,4,6], [3,4,5]), [true, true, false]); - assert.deepEqual(smallereq([1,4,6], matrix([3,4,5])), matrix([true, true, false])); + assert.deepEqual(smallerEq([1,4,6], [3,4,5]), [true, true, false]); + assert.deepEqual(smallerEq([1,4,6], matrix([3,4,5])), matrix([true, true, false])); }); it('should throw an error when comparing complex numbers', function() { - assert.throws(function () {smallereq(complex(1,1), complex(1,2))}, TypeError); - assert.throws(function () {smallereq(complex(2,1), 3)}, TypeError); - assert.throws(function () {smallereq(3, complex(2,4))}, TypeError); - assert.throws(function () {smallereq(math.bignumber(3), complex(2,4))}, TypeError); - assert.throws(function () {smallereq(complex(2,4), math.bignumber(3))}, TypeError); + assert.throws(function () {smallerEq(complex(1,1), complex(1,2))}, TypeError); + assert.throws(function () {smallerEq(complex(2,1), 3)}, TypeError); + assert.throws(function () {smallerEq(3, complex(2,4))}, TypeError); + assert.throws(function () {smallerEq(math.bignumber(3), complex(2,4))}, TypeError); + assert.throws(function () {smallerEq(complex(2,4), math.bignumber(3))}, TypeError); }); it('should throw an error with two matrices of different sizes', function () { - assert.throws(function () {smallereq([1,4,6], [3,4])}); + assert.throws(function () {smallerEq([1,4,6], [3,4])}); }); it('should throw an error in case of invalid number of arguments', function() { - assert.throws(function () {smallereq(1)}, error.ArgumentsError); - assert.throws(function () {smallereq(1, 2, 3)}, error.ArgumentsError); + assert.throws(function () {smallerEq(1)}, error.ArgumentsError); + assert.throws(function () {smallerEq(1, 2, 3)}, error.ArgumentsError); }); }); \ No newline at end of file diff --git a/test/function/arithmetic/unaryminus.test.js b/test/function/arithmetic/unaryminus.test.js index c2057498a..0c7a629ca 100644 --- a/test/function/arithmetic/unaryminus.test.js +++ b/test/function/arithmetic/unaryminus.test.js @@ -5,68 +5,68 @@ var assert = require('assert'), error = require('../../../lib/error/index'), bignumber = math.bignumber; -describe('unaryminus', function() { +describe('unaryMinus', function() { it('should return unary minus of a boolean', function () { - assert.equal(math.unaryminus(true), -1); - assert.equal(math.unaryminus(false), 0); + assert.equal(math.unaryMinus(true), -1); + assert.equal(math.unaryMinus(false), 0); }); it('should return bignumber unary minus of a boolean', function () { var bigmath = mathjs({number: 'bignumber'}); - assert.deepEqual(bigmath.unaryminus(true), bigmath.bignumber(-1)); - assert.deepEqual(bigmath.unaryminus(false), bigmath.bignumber(-0)); + assert.deepEqual(bigmath.unaryMinus(true), bigmath.bignumber(-1)); + assert.deepEqual(bigmath.unaryMinus(false), bigmath.bignumber(-0)); }); it('should return unary minus on a string', function() { - assert.equal(math.unaryminus('2'), -2); - assert.equal(math.unaryminus('-2'), 2); + assert.equal(math.unaryMinus('2'), -2); + assert.equal(math.unaryMinus('-2'), 2); }); it('should return bignumber unary minus on a string', function() { var bigmath = mathjs({number: 'bignumber'}); - assert.deepEqual(bigmath.unaryminus('2'), bigmath.bignumber(-2)); - assert.deepEqual(bigmath.unaryminus('-2'), bigmath.bignumber(2)); + assert.deepEqual(bigmath.unaryMinus('2'), bigmath.bignumber(-2)); + assert.deepEqual(bigmath.unaryMinus('-2'), bigmath.bignumber(2)); }); it('should perform unary minus of a number', function() { - assert.deepEqual(math.unaryminus(2), -2); - assert.deepEqual(math.unaryminus(-2), 2); - assert.deepEqual(math.unaryminus(0), 0); + assert.deepEqual(math.unaryMinus(2), -2); + assert.deepEqual(math.unaryMinus(-2), 2); + assert.deepEqual(math.unaryMinus(0), 0); }); it('should perform unary minus of a big number', function() { - assert.deepEqual(math.unaryminus(bignumber(2)), bignumber(-2)); - assert.deepEqual(math.unaryminus(bignumber(-2)), bignumber(2)); - assert.deepEqual(math.unaryminus(bignumber(0)).valueOf(), bignumber(0).valueOf()); + assert.deepEqual(math.unaryMinus(bignumber(2)), bignumber(-2)); + assert.deepEqual(math.unaryMinus(bignumber(-2)), bignumber(2)); + assert.deepEqual(math.unaryMinus(bignumber(0)).valueOf(), bignumber(0).valueOf()); }); it('should perform unary minus of a complex number', function() { - assert.equal(math.unaryminus(math.complex(3, 2)), '-3 - 2i'); - assert.equal(math.unaryminus(math.complex(3, -2)), '-3 + 2i'); - assert.equal(math.unaryminus(math.complex(-3, 2)), '3 - 2i'); - assert.equal(math.unaryminus(math.complex(-3, -2)), '3 + 2i'); + assert.equal(math.unaryMinus(math.complex(3, 2)), '-3 - 2i'); + assert.equal(math.unaryMinus(math.complex(3, -2)), '-3 + 2i'); + assert.equal(math.unaryMinus(math.complex(-3, 2)), '3 - 2i'); + assert.equal(math.unaryMinus(math.complex(-3, -2)), '3 + 2i'); }); it('should perform unary minus of a unit', function() { - assert.equal(math.unaryminus(math.unit(5, 'km')).toString(), '-5 km'); + assert.equal(math.unaryMinus(math.unit(5, 'km')).toString(), '-5 km'); }); it('should perform element-wise unary minus on a matrix', function() { a2 = math.matrix([[1,2],[3,4]]); - var a7 = math.unaryminus(a2); + var a7 = math.unaryMinus(a2); assert.ok(a7 instanceof math.type.Matrix); assert.deepEqual(a7.size(), [2,2]); assert.deepEqual(a7.valueOf(), [[-1,-2],[-3,-4]]); - assert.deepEqual(math.unaryminus([[1,2],[3,4]]), [[-1,-2],[-3,-4]]); + assert.deepEqual(math.unaryMinus([[1,2],[3,4]]), [[-1,-2],[-3,-4]]); }); it('should throw an error in case of invalid number of arguments', function() { - assert.throws(function () {math.unaryminus()}, error.ArgumentsError); - assert.throws(function () {math.unaryminus(1, 2)}, error.ArgumentsError); + assert.throws(function () {math.unaryMinus()}, error.ArgumentsError); + assert.throws(function () {math.unaryMinus(1, 2)}, error.ArgumentsError); }); it('should throw an error in case of invalid type of argument', function() { - assert.throws(function () {math.unaryminus(new Date())}, error.UnsupportedTypeError); + assert.throws(function () {math.unaryMinus(new Date())}, error.UnsupportedTypeError); }); }); \ No newline at end of file diff --git a/test/function/arithmetic/unaryplus.test.js b/test/function/arithmetic/unaryplus.test.js index 50835811b..cc42c8ca1 100644 --- a/test/function/arithmetic/unaryplus.test.js +++ b/test/function/arithmetic/unaryplus.test.js @@ -5,68 +5,68 @@ var assert = require('assert'), error = require('../../../lib/error/index'), bignumber = math.bignumber; -describe('unaryplus', function() { +describe('unaryPlus', function() { it('should return unary plus of a boolean', function () { - assert.equal(math.unaryplus(true), 1); - assert.equal(math.unaryplus(false), 0); + assert.equal(math.unaryPlus(true), 1); + assert.equal(math.unaryPlus(false), 0); }); it('should return bignumber unary plus of a boolean', function () { var bigmath = mathjs({number: 'bignumber'}); - assert.deepEqual(bigmath.unaryplus(true), bigmath.bignumber(1)); - assert.deepEqual(bigmath.unaryplus(false), bigmath.bignumber(0)); + assert.deepEqual(bigmath.unaryPlus(true), bigmath.bignumber(1)); + assert.deepEqual(bigmath.unaryPlus(false), bigmath.bignumber(0)); }); it('should return unary plus on a string', function() { - assert.equal(math.unaryplus('2'), 2); - assert.equal(math.unaryplus('-2'), -2); + assert.equal(math.unaryPlus('2'), 2); + assert.equal(math.unaryPlus('-2'), -2); }); it('should return bignumber unary plus on a string', function() { var bigmath = mathjs({number: 'bignumber'}); - assert.deepEqual(bigmath.unaryplus('2'), bigmath.bignumber(2)); - assert.deepEqual(bigmath.unaryplus('-2'), bigmath.bignumber(-2)); + assert.deepEqual(bigmath.unaryPlus('2'), bigmath.bignumber(2)); + assert.deepEqual(bigmath.unaryPlus('-2'), bigmath.bignumber(-2)); }); it('should perform unary plus of a number', function() { - assert.deepEqual(math.unaryplus(2), 2); - assert.deepEqual(math.unaryplus(-2), -2); - assert.deepEqual(math.unaryplus(0), 0); + assert.deepEqual(math.unaryPlus(2), 2); + assert.deepEqual(math.unaryPlus(-2), -2); + assert.deepEqual(math.unaryPlus(0), 0); }); it('should perform unary plus of a big number', function() { - assert.deepEqual(math.unaryplus(bignumber(2)), bignumber(2)); - assert.deepEqual(math.unaryplus(bignumber(-2)), bignumber(-2)); - assert.deepEqual(math.unaryplus(bignumber(0)).valueOf(), bignumber(0).valueOf()); + assert.deepEqual(math.unaryPlus(bignumber(2)), bignumber(2)); + assert.deepEqual(math.unaryPlus(bignumber(-2)), bignumber(-2)); + assert.deepEqual(math.unaryPlus(bignumber(0)).valueOf(), bignumber(0).valueOf()); }); it('should perform unary plus of a complex number', function() { - assert.equal(math.unaryplus(math.complex(3, 2)), '3 + 2i'); - assert.equal(math.unaryplus(math.complex(3, -2)), '3 - 2i'); - assert.equal(math.unaryplus(math.complex(-3, 2)), '-3 + 2i'); - assert.equal(math.unaryplus(math.complex(-3, -2)), '-3 - 2i'); + assert.equal(math.unaryPlus(math.complex(3, 2)), '3 + 2i'); + assert.equal(math.unaryPlus(math.complex(3, -2)), '3 - 2i'); + assert.equal(math.unaryPlus(math.complex(-3, 2)), '-3 + 2i'); + assert.equal(math.unaryPlus(math.complex(-3, -2)), '-3 - 2i'); }); it('should perform unary plus of a unit', function() { - assert.equal(math.unaryplus(math.unit(5, 'km')).toString(), '5 km'); + assert.equal(math.unaryPlus(math.unit(5, 'km')).toString(), '5 km'); }); it('should perform element-wise unary plus on a matrix', function() { a2 = math.matrix([[1,2],[3,4]]); - var a7 = math.unaryplus(a2); + var a7 = math.unaryPlus(a2); assert.ok(a7 instanceof math.type.Matrix); assert.deepEqual(a7.size(), [2,2]); assert.deepEqual(a7.valueOf(), [[1,2],[3,4]]); - assert.deepEqual(math.unaryplus([[1,2],[3,4]]), [[1,2],[3,4]]); + assert.deepEqual(math.unaryPlus([[1,2],[3,4]]), [[1,2],[3,4]]); }); it('should throw an error in case of invalid number of arguments', function() { - assert.throws(function () {math.unaryplus()}, error.ArgumentsError); - assert.throws(function () {math.unaryplus(1, 2)}, error.ArgumentsError); + assert.throws(function () {math.unaryPlus()}, error.ArgumentsError); + assert.throws(function () {math.unaryPlus(1, 2)}, error.ArgumentsError); }); it('should throw an error in case of invalid type of argument', function() { - assert.throws(function () {math.unaryplus(new Date())}, error.UnsupportedTypeError); + assert.throws(function () {math.unaryPlus(new Date())}, error.UnsupportedTypeError); }); }); \ No newline at end of file diff --git a/test/function/arithmetic/unequal.test.js b/test/function/arithmetic/unequal.test.js index 9792666ae..f5df9738f 100644 --- a/test/function/arithmetic/unequal.test.js +++ b/test/function/arithmetic/unequal.test.js @@ -16,6 +16,7 @@ describe('unequal', function() { assert.equal(unequal(2, 2), false); assert.equal(unequal(0, 0), false); assert.equal(unequal(-2, 2), true); + assert.equal(unequal(true, 1), false); }); it('should compare two floating point numbers correctly', function() { @@ -134,18 +135,32 @@ describe('unequal', function() { assert.equal(unequal('hello', 'hello'), false); }); - it('should compare a string an matrix elementwise', function() { - assert.deepEqual(unequal('B', ['A', 'B', 'C']), [true, false, true]); - assert.deepEqual(unequal(['A', 'B', 'C'], 'B'), [true, false, true]); + it('should compare a string an matrix', function() { + assert.deepEqual(unequal('B', ['A', 'B', 'C']), true); + assert.deepEqual(unequal(['A', 'B', 'C'], 'B'), true); }); - it('should perform element-wise comparison of two matrices of the same size', function() { - assert.deepEqual(unequal([1,4,5], [3,4,5]), [true, false, false]); - assert.deepEqual(unequal([1,4,5], matrix([3,4,5])), matrix([true, false, false])); + it('should compare two matrices (deepEqual)', function() { + assert.deepEqual(unequal([1,4,5], [3,4,5]), true); + assert.deepEqual(unequal([1,4,5], [1,4,5]), false); + assert.deepEqual(unequal([1,4,5], [1,4]), true); + assert.deepEqual(unequal([1,4], [1,4,5]), true); + assert.deepEqual(unequal([1,4,5], matrix([3,4,5])), true); + assert.deepEqual(unequal([1,4,5], matrix([1,4,5])), false); + assert.deepEqual(unequal(matrix([1,4,5]), matrix([1,4,5])), false); + + assert.deepEqual(unequal(matrix([[1,2], [3,4]]), matrix([[1,2], [3,4]])), false); + assert.deepEqual(unequal(matrix([[1,2], [3,4]]), matrix([[1,2], [3,5]])), true); + assert.deepEqual(unequal(matrix([[1,2], [3,4]]), matrix([[1,2], [3,4], [5,6]])), true); + assert.deepEqual(unequal(matrix([[1,2], [3,4], [5,6]]), matrix([[1,2], [3,4]])), true); }); - it('should throw an error when comparing two matrices of different sizes', function() { - assert.throws(function () {unequal([1,4,5], [3,4])}); + it('should compare two matrices with mixed types', function() { + assert.deepEqual(unequal([1,4,5], [true,4,5]), false); + assert.deepEqual(unequal([2,3], [2, bignumber(3)]), false); + assert.deepEqual(unequal([2,3], [2, bignumber(4)]), true); + assert.deepEqual(unequal([complex(2,3),3], [complex(2,3),3]), false); + assert.deepEqual(unequal([complex(2,3),3], [complex(2,4),3]), true); }); it('should throw an error in case of invalid number of arguments', function() { diff --git a/test/function/complex/arg.test.js b/test/function/complex/arg.test.js index 515e509e0..12c820ec2 100644 --- a/test/function/complex/arg.test.js +++ b/test/function/complex/arg.test.js @@ -36,12 +36,12 @@ describe('arg', function() { it('should calculate the argument for each element in a matrix', function() { assert.deepEqual(math.divide(arg([ - math.i, math.unaryminus(math.i), math.add(1,math.i) + math.i, math.unaryMinus(math.i), math.add(1,math.i) ]), math.pi), [ 0.5, -0.5, 0.25 ]); assert.deepEqual(math.matrix(math.divide(arg([ - math.i, math.unaryminus(math.i), math.add(1,math.i) + math.i, math.unaryMinus(math.i), math.add(1,math.i) ]), math.pi)).valueOf(), [ 0.5, -0.5, 0.25 ]); diff --git a/tools/validate.js b/tools/validate.js index f2b062845..ff7216ca4 100644 --- a/tools/validate.js +++ b/tools/validate.js @@ -6,7 +6,9 @@ var gutil = require('gulp-util'), prop; // names to ignore -var ignore = ['workspace', 'compile', 'parse', 'parser', 'select', 'unary', 'print', 'config', 'in']; +var ignore = ['compile', 'parse', 'parser', 'select', 'print', 'config', 'in', + 'workspace', 'unary', 'edivide', 'emultiply', 'epow' +]; // test whether all functions are documented var undocumentedCount = 0;