diff --git a/docs/expressions.md b/docs/expressions.md index a72b8f434..85e85d40e 100644 --- a/docs/expressions.md +++ b/docs/expressions.md @@ -293,7 +293,7 @@ Operator | Name | Syntax | Associativity | Example `%`, `mod` | Modulus | `x % y` | Left to right | `8 % 3` | `2` `^` | Power | `x ^ y` | Right to left | `2 ^ 3` | `8` `.^` | Element-wise power | `x .^ y` | Right to left | `[2,3] .^ [3,3]` | `[9,27]` -`-` | Unary | `-y` | None | `-4` | `-4` +`-` | Unary minus | `-y` | None | `-4` | `-4` `'` | Transpose | `y'` | None | `[[1,2],[3,4]]'` | `[[1,3],[2,4]]` `!` | Factorial | `y!` | None | `5!` | `120` `=` | Assignment | `x = y` | Right to left | `a = 5` | `5` @@ -315,7 +315,7 @@ Operators | Description `'` | Matrix transpose `!` | Factorial `^`, `.^` | Exponentiation -`-` | Unary +`-` | Unary minus `x unit` | Unit `*`, `/`, `.*`, `./`, `%`, `mod` | Multiply, divide, modulus `+`, `-` | Add, subtract diff --git a/docs/functions.md b/docs/functions.md index bfcbbc680..c82ec9f8e 100644 --- a/docs/functions.md +++ b/docs/functions.md @@ -53,7 +53,7 @@ math.add('hello ', 'world!'); // String 'hello world!' - math.subtract(x, y) - math.sqrt(x) - math.square(x) -- math.unary(x) +- math.unaryminus(x) - math.unequal(x) - math.xgcd(a, b) diff --git a/lib/expression/docs/constants/phi.js b/lib/expression/docs/constants/phi.js new file mode 100644 index 000000000..ce49e97ac --- /dev/null +++ b/lib/expression/docs/constants/phi.js @@ -0,0 +1,12 @@ +module.exports = { + 'name': 'phi', + 'category': 'Constants', + 'syntax': [ + 'phi' + ], + 'description': 'Phi is the golden ratio. Two quantities are in the golden ratio if their ratio is the same as the ratio of their sum to the larger of the two quantities. Phi is defined as `(1 + sqrt(5)) / 2` and is approximately 1.618034...', + 'examples': [ + 'tau' + ], + 'seealso': [] +}; diff --git a/lib/expression/docs/constants/tau.js b/lib/expression/docs/constants/tau.js index 76e3f254b..1bf0f8a28 100644 --- a/lib/expression/docs/constants/tau.js +++ b/lib/expression/docs/constants/tau.js @@ -2,7 +2,7 @@ module.exports = { 'name': 'tau', 'category': 'Constants', 'syntax': [ - 'pi' + 'tau' ], 'description': 'Tau is the ratio constant of a circle\'s circumference to radius, equal to 2 * pi, approximately 6.2832.', 'examples': [ diff --git a/lib/expression/docs/function/arithmetic/unary.js b/lib/expression/docs/function/arithmetic/unaryminus.js similarity index 83% rename from lib/expression/docs/function/arithmetic/unary.js rename to lib/expression/docs/function/arithmetic/unaryminus.js index 964b3acb6..7225c3c9e 100644 --- a/lib/expression/docs/function/arithmetic/unary.js +++ b/lib/expression/docs/function/arithmetic/unaryminus.js @@ -1,9 +1,9 @@ module.exports = { - 'name': 'unary', + 'name': 'unaryminus', 'category': 'Operators', 'syntax': [ '-x', - 'unary(x)' + 'unaryminus(x)' ], 'description': 'Inverse the sign of a value.', diff --git a/lib/expression/docs/index.js b/lib/expression/docs/index.js index 381af273d..d5b36b0fa 100644 --- a/lib/expression/docs/index.js +++ b/lib/expression/docs/index.js @@ -11,6 +11,7 @@ exports.LOG10E = require('./constants/LOG10E'); exports.NaN = require('./constants/NaN'); exports.pi = require('./constants/pi'); exports.PI = require('./constants/pi'); +exports.phi = require('./constants/phi'); exports.SQRT1_2 = require('./constants/SQRT1_2'); exports.SQRT2 = require('./constants/SQRT2'); exports.tau = require('./constants/tau'); @@ -47,7 +48,7 @@ exports.smallereq = require('./function/arithmetic/smallereq'); exports.sqrt = require('./function/arithmetic/sqrt'); exports.square = require('./function/arithmetic/square'); exports.subtract = require('./function/arithmetic/subtract'); -exports.unary = require('./function/arithmetic/unary'); +exports.unaryminus = require('./function/arithmetic/unaryminus'); 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 fab5bfd74..c3523f36c 100644 --- a/lib/expression/parse.js +++ b/lib/expression/parse.js @@ -785,7 +785,7 @@ function parseUnary () { if (token == '-') { name = token; - fn = 'unary'; + fn = 'unaryminus'; getToken(); params = [parseUnary()]; diff --git a/lib/function/arithmetic/unary.js b/lib/function/arithmetic/unaryminus.js similarity index 71% rename from lib/function/arithmetic/unary.js rename to lib/function/arithmetic/unaryminus.js index d21c662af..7b4ab0835 100644 --- a/lib/function/arithmetic/unary.js +++ b/lib/function/arithmetic/unaryminus.js @@ -21,14 +21,14 @@ module.exports = function (math) { * * Syntax: * - * math.unary(x) + * math.unaryminus(x) * * Examples: * * var math = mathjs(); * - * math.unary(3.5); // returns -3.5 - * math.unary(-4.2); // returns 4.2 + * math.unaryminus(3.5); // returns -3.5 + * math.unaryminus(-4.2); // returns 4.2 * * See also: * @@ -37,9 +37,9 @@ module.exports = function (math) { * @param {Number | BigNumber | Boolean | Complex | Unit | Array | Matrix} x Number to be inverted. * @return {Number | BigNumber | Complex | Unit | Array | Matrix} Returns the value with inverted sign. */ - math.unary = function unary(x) { + math.unaryminus = function unaryminus(x) { if (arguments.length != 1) { - throw new math.error.ArgumentsError('unary', arguments.length, 1); + throw new math.error.ArgumentsError('unaryminus', arguments.length, 1); } if (isNumber(x)) { @@ -64,13 +64,18 @@ module.exports = function (math) { } if (isCollection(x)) { - return collection.deepMap(x, unary); + return collection.deepMap(x, unaryminus); } if (isBoolean(x)) { return -x; } - throw new math.error.UnsupportedTypeError('unary', math['typeof'](x)); + throw new math.error.UnsupportedTypeError('unaryminus', math['typeof'](x)); }; + + // TODO: function unary is renamed to unaryminus since version 0.22.1. Cleanup some day + math.unary = function unary() { + throw new Error('Function unary is deprecated. Use unaryminus instead.'); + } }; diff --git a/lib/function/matrix/det.js b/lib/function/matrix/det.js index 7afa78f9f..b29f68b5a 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.unary(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.unary(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 2069b6cc5..1d8130bf1 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.unary(matrix[0][1]), d) + math.divide(math.unaryminus(matrix[0][1]), d) ], [ - math.divide(math.unary(matrix[1][0]), d), + math.divide(math.unaryminus(matrix[1][0]), d), math.divide(matrix[0][0], d) ] ]; @@ -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.unary(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/math.js b/lib/math.js index 38b910421..209395e36 100644 --- a/lib/math.js +++ b/lib/math.js @@ -191,7 +191,7 @@ function mathjs (config) { require('./function/arithmetic/sqrt.js')(math, _config); require('./function/arithmetic/square.js')(math, _config); require('./function/arithmetic/subtract.js')(math, _config); - require('./function/arithmetic/unary.js')(math, _config); + require('./function/arithmetic/unaryminus.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 cbbca350b..025f9346d 100644 --- a/lib/util/latex.js +++ b/lib/util/latex.js @@ -97,7 +97,7 @@ var functions = { lcm: false, sign: false, xgcd: false, - unary: false, + unaryminus: false, // complex complex: false, diff --git a/test/contstants.test.js b/test/contstants.test.js index af2265ed3..7aaefdd6f 100644 --- a/test/contstants.test.js +++ b/test/contstants.test.js @@ -8,17 +8,18 @@ describe('constants', function() { approx.equal(math.pi, 3.14159265358979); approx.equal(math.sin(math.pi / 2), 1); approx.equal(math.PI, math.pi); - approx.equal(math.eval('pi'), 3.14159265358979); }); it('should have tau', function() { approx.equal(math.tau, 6.28318530717959); - approx.equal(math.eval('tau'), 6.28318530717959); }); + it('should have phi, golden ratio', function() { + approx.equal(math.phi, 1.61803398874989484820458683436563811772030917980576286213545); + }); + it('should have euler constant', function() { approx.equal(math.e, 2.71828182845905); - approx.equal(math.eval('e'), 2.71828182845905); assert.equal(math.round(math.add(1,math.pow(math.e, math.multiply(math.pi, math.i))), 5), 0); assert.equal(math.round(math.eval('1+e^(pi*i)'), 5), 0); }); diff --git a/test/expression/node/OperatorNode.test.js b/test/expression/node/OperatorNode.test.js index 6f4135af6..7d0f014b8 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('-', 'unary', [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('-', 'unary', [a]); + var n = new OperatorNode('-', 'unaryminus', [a]); assert.equal(n.toTex(), '-2'); }); diff --git a/test/function/arithmetic/unary.test.js b/test/function/arithmetic/unary.test.js deleted file mode 100644 index 7b9402894..000000000 --- a/test/function/arithmetic/unary.test.js +++ /dev/null @@ -1,54 +0,0 @@ -// test unary minus -var assert = require('assert'), - math = require('../../../index')(), - error = require('../../../lib/error/index'), - bignumber = math.bignumber; - -describe('unaryminus', function() { - it('should perform unary minus of a boolean', function () { - assert.equal(math.unary(true), -1); - assert.equal(math.unary(false), 0); - }); - - it('should perform unary minus of a number', function() { - assert.deepEqual(math.unary(2), -2); - assert.deepEqual(math.unary(-2), 2); - assert.deepEqual(math.unary(0), 0); - }); - - it('should perform unary minus of a big number', function() { - assert.deepEqual(math.unary(bignumber(2)), bignumber(-2)); - assert.deepEqual(math.unary(bignumber(-2)), bignumber(2)); - assert.deepEqual(math.unary(bignumber(0)).valueOf(), bignumber(0).valueOf()); - }); - - it('should perform unary minus of a complex number', function() { - assert.equal(math.unary(math.complex(3, 2)), '-3 - 2i'); - assert.equal(math.unary(math.complex(3, -2)), '-3 + 2i'); - assert.equal(math.unary(math.complex(-3, 2)), '3 - 2i'); - assert.equal(math.unary(math.complex(-3, -2)), '3 + 2i'); - }); - - it('should perform unary minus of a unit', function() { - assert.equal(math.unary(math.unit(5, 'km')).toString(), '-5 km'); - }); - - it('should throw an error when used with a string', function() { - assert.throws(function () {math.unary('hello'); }); - }); - - it('should perform element-wise unary minus on a matrix', function() { - a2 = math.matrix([[1,2],[3,4]]); - var a7 = math.unary(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.unary([[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.unary()}, error.ArgumentsError); - assert.throws(function () {math.unary(1, 2)}, error.ArgumentsError); - }); - -}); \ No newline at end of file diff --git a/test/function/arithmetic/unaryminus.test.js b/test/function/arithmetic/unaryminus.test.js new file mode 100644 index 000000000..7ffd5f6c9 --- /dev/null +++ b/test/function/arithmetic/unaryminus.test.js @@ -0,0 +1,54 @@ +// test unary minus +var assert = require('assert'), + math = require('../../../index')(), + error = require('../../../lib/error/index'), + bignumber = math.bignumber; + +describe('unaryminus', function() { + it('should perform unary minus of a boolean', function () { + assert.equal(math.unaryminus(true), -1); + assert.equal(math.unaryminus(false), 0); + }); + + 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); + }); + + 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()); + }); + + 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'); + }); + + it('should perform unary minus of a unit', function() { + assert.equal(math.unaryminus(math.unit(5, 'km')).toString(), '-5 km'); + }); + + it('should throw an error when used with a string', function() { + assert.throws(function () {math.unaryminus('hello'); }); + }); + + it('should perform element-wise unary minus on a matrix', function() { + a2 = math.matrix([[1,2],[3,4]]); + 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]]); + }); + + 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); + }); + +}); \ No newline at end of file diff --git a/test/function/complex/arg.test.js b/test/function/complex/arg.test.js index b164975ab..515e509e0 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.unary(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.unary(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 ]);