Renamed function unary to unaryminus. Documented and tested constant phi.

This commit is contained in:
jos 2014-06-08 20:12:07 +02:00
parent acec9f95af
commit abf1b02ee9
17 changed files with 102 additions and 83 deletions

View File

@ -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

View File

@ -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)

View File

@ -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': []
};

View File

@ -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': [

View File

@ -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.',

View File

@ -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');

View File

@ -785,7 +785,7 @@ function parseUnary () {
if (token == '-') {
name = token;
fn = 'unary';
fn = 'unaryminus';
getToken();
params = [parseUnary()];

View File

@ -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.');
}
};

View File

@ -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];
}

View File

@ -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

View File

@ -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);

View File

@ -97,7 +97,7 @@ var functions = {
lcm: false,
sign: false,
xgcd: false,
unary: false,
unaryminus: false,
// complex
complex: false,

View File

@ -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);
});

View File

@ -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');
});

View File

@ -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);
});
});

View File

@ -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);
});
});

View File

@ -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
]);