Updated to typed-function 0.6.2, fixing a couple of bugs. Conversion from BigNumber to Complex added.

This commit is contained in:
jos 2015-02-26 21:51:25 +01:00
parent adbf9031b2
commit cdd7bb8a83
12 changed files with 17 additions and 39 deletions

View File

@ -44,11 +44,6 @@ module.exports = function (config) {
var log = typed('log', {
'number': _logNumber,
// FIXME: boolean and null should be converted automatically to number (is this not working because of any?
'boolean | null': function(x) {
return _logNumber(+x);
},
'Complex': function (x) {
return new Complex (
Math.log(Math.sqrt(x.re * x.re + x.im * x.im)),

View File

@ -94,11 +94,6 @@ module.exports = function (config) {
'any, Array | Matrix': function (x, y) {
return collection.deepMap2(x, y, multiply);
},
// FIXME: this should be redundant, should be handled by typed-function
'boolean, boolean': function (x, y) {
return x * y;
}
});

View File

@ -39,12 +39,6 @@ module.exports = function (config) {
* Subtraction of `x` and `y`
*/
var subtract = typed('subtract', {
// FIXME: should not be needed to define subtract for booleans
// TODO: return either number or BigNumber depending on config?
'boolean, boolean': function (x, y) {
return x - y;
},
'number, number': function (x, y) {
return x - y;
},

View File

@ -179,13 +179,12 @@ function create (config) {
return new Complex(x, 0);
}
}, {
// TODO: convert from BigNumber to complex?
// from: 'BigNumber',
// to: 'Complex',
// convert: function (x) {
// return new Complex(x.toNumber(), 0);
// }
//}, {
from: 'BigNumber',
to: 'Complex',
convert: function (x) {
return new Complex(x.toNumber(), 0);
}
}, {
from: 'number',
to: 'string',
convert: function (x) {

View File

@ -51,7 +51,7 @@
],
"dependencies": {
"decimal.js": "^4.0.1",
"typed-function": "^0.6.1"
"typed-function": "^0.6.2"
},
"devDependencies": {
"webpack": "latest",

View File

@ -1396,8 +1396,7 @@ describe('parse', function() {
bigmath.matrix([new BigNumber(0.7), new BigNumber(0.8)]));
});
// TODO: cleanup once decided to not downgrade BigNumber to number
it.skip('should work with complex numbers (downgrades bignumbers to number)', function() {
it('should work with complex numbers (downgrades bignumbers to number)', function() {
assert.deepEqual(bigmath.eval('3i'), new Complex(0, 3));
assert.deepEqual(bigmath.eval('2 + 3i'), new Complex(2, 3));
assert.deepEqual(bigmath.eval('2 * i'), new Complex(0, 2));

View File

@ -58,8 +58,8 @@ describe('add', function() {
});
it('should add mixed complex numbers and BigNumbers', function() {
assert.throws(function () {add(math.complex(3, -4), new BigNumber(2))}, /TypeError: Unexpected type of argument \(expected: Array or Matrix, actual: BigNumber, index: 1\)/);
assert.throws(function () {add(new BigNumber(2), math.complex(3, -4))}, /TypeError: Unexpected type of argument \(expected: Array or Matrix, actual: Complex, index: 1\)/);
assert.deepEqual(add(math.complex(3, -4), new BigNumber(2)), math.complex(5, -4));
assert.deepEqual(add(new BigNumber(2), math.complex(3, -4)), math.complex(5, -4));
});
it('should add two complex numbers', function() {

View File

@ -100,10 +100,8 @@ describe('divide', function() {
});
it('should divide mixed complex numbers and bignumbers', function() {
//assert.deepEqual(divide(math.complex(6, -4), bignumber(2)), math.complex(3, -2));
//assert.deepEqual(divide(bignumber(1), math.complex(2, 4)), math.complex(0.1, -0.2));
assert.throws(function () {divide(math.complex(6, -4), bignumber(2))}, /TypeError: Unexpected type of argument \(expected: Complex or number, actual: BigNumber, index: 1\)/);
assert.throws(function () {divide(bignumber(1), math.complex(2, 4))}, /TypeError: Unexpected type of argument \(expected: BigNumber or number or boolean or null, actual: Complex, index: 1\)/);
assert.deepEqual(divide(math.complex(6, -4), bignumber(2)), math.complex(3, -2));
assert.deepEqual(divide(bignumber(1), math.complex(2, 4)), math.complex(0.1, -0.2));
});
it('should divide units by a number', function() {

View File

@ -117,8 +117,7 @@ describe('multiply', function() {
assert.deepEqual(multiply(2, math.complex(2, 4)), math.complex(4, 8));
});
// TODO: remove this test
it.skip('should multiply mixed complex numbers and big numbers', function() {
it('should multiply mixed complex numbers and big numbers', function() {
assert.deepEqual(multiply(math.complex(6, -4), math.bignumber(2)), math.complex(12, -8));
assert.deepEqual(multiply(math.bignumber(2), math.complex(2, 4)), math.complex(4, 8));
});

View File

@ -121,8 +121,7 @@ describe('pow', function() {
approx.deepEqual(pow(complex(1,1),complex(1,1)), complex('0.2739572538301211 + 0.5837007587586147i'));
});
// TODO: support mixed complex and bignumbers?
it.skip('should exponentiate a complex number to the given bignumber power', function() {
it('should exponentiate a complex number to the given bignumber power', function() {
approx.deepEqual(pow(complex(3, 0), math.bignumber(2)), complex(9, 0));
approx.deepEqual(pow(complex(0, 2), math.bignumber(2)), complex(-4, 0));
});

View File

@ -73,8 +73,8 @@ describe('subtract', function() {
});
it('should throw an error for mixed complex numbers and big numbers', function() {
assert.throws(function () {subtract(math.complex(3, 4), math.bignumber(10))}, /TypeError: Unexpected type of argument \(expected: Matrix or Array, actual: BigNumber, index: 1\)/);
assert.throws(function () {subtract(math.bignumber(10), math.complex(3, 4))}, /TypeError: Unexpected type of argument \(expected: Matrix or Array, actual: Complex, index: 1\)/);
assert.deepEqual(subtract(math.complex(3, 4), math.bignumber(10)), math.complex(-7, 4));
assert.deepEqual(subtract(math.bignumber(10), math.complex(3, 4)), math.complex(7, -4));
});
it('should subtract two quantities of the same unit', function() {

View File

@ -10,7 +10,7 @@ describe('unaryMinus', function() {
assert.equal(math.unaryMinus(false), 0);
});
// FIXME: convert boolean to BigNumber in unaryMinus
// TODO: unary minus should return bignumber on boolean input when configured for bignumber
it.skip('should return bignumber unary minus of a boolean', function () {
var bigmath = math.create({number: 'bignumber'});
assert.deepEqual(bigmath.unaryMinus(true), bigmath.bignumber(-1));