From eb9ffdeadf043da0737f8f205aa365b233c2a481 Mon Sep 17 00:00:00 2001 From: josdejong Date: Thu, 17 Oct 2013 14:49:30 +0200 Subject: [PATCH] Removed support for comparing complex numbers --- HISTORY.md | 2 ++ lib/function/arithmetic/larger.js | 25 +++++++--------------- lib/function/arithmetic/largereq.js | 25 +++++++--------------- lib/function/arithmetic/smaller.js | 25 +++++++--------------- lib/function/arithmetic/smallereq.js | 21 ++++++------------ test/function/arithmetic/larger.test.js | 20 ++++++----------- test/function/arithmetic/largereq.test.js | 20 ++++++----------- test/function/arithmetic/smaller.test.js | 20 ++++++----------- test/function/arithmetic/smallereq.test.js | 23 ++++++-------------- test/function/statistics/max.test.js | 22 +++++++++---------- test/function/statistics/min.test.js | 22 +++++++++---------- 11 files changed, 77 insertions(+), 148 deletions(-) diff --git a/HISTORY.md b/HISTORY.md index 03d6f91b2..9b2c3bede 100644 --- a/HISTORY.md +++ b/HISTORY.md @@ -7,6 +7,8 @@ https://github.com/josdejong/mathjs - Implemented statistics function `mean`. Thanks Guillermo Indalecio Fernandez (guillermobox). - Implemented support for multiplying vectors with matrices. +- Removed support for comparing complex numbers in functions `smaller`, + `smallereq`, `larger`, `largereq`. Complex numbers cannot be ordered. - Fixed formatting numbers as scientific notation in some cases returning a zero digit left from the decimal point. (like "0.33333e8" rather than "3.3333e7"). Thanks husayt. diff --git a/lib/function/arithmetic/larger.js b/lib/function/arithmetic/larger.js index f0dcd34da..7b7cee271 100644 --- a/lib/function/arithmetic/larger.js +++ b/lib/function/arithmetic/larger.js @@ -20,8 +20,8 @@ module.exports = function (math) { * For matrices, the function is evaluated element wise. * In case of complex numbers, the absolute values of a and b are compared. * - * @param {Number | Boolean | Complex | Unit | String | Array | Matrix} x - * @param {Number | Boolean | Complex | Unit | String | Array | Matrix} y + * @param {Number | Boolean | Unit | String | Array | Matrix} x + * @param {Number | Boolean | Unit | String | Array | Matrix} y * @return {Boolean | Array | Matrix} res */ math.larger = function larger(x, y) { @@ -29,21 +29,8 @@ module.exports = function (math) { throw new util.error.ArgumentsError('larger', arguments.length, 2); } - if (isNumBool(x)) { - if (isNumBool(y)) { - return x > y; - } - else if (isComplex(y)) { - return x > math.abs(y); - } - } - if (isComplex(x)) { - if (isNumBool(y)) { - return math.abs(x) > y; - } - else if (isComplex(y)) { - return math.abs(x) > math.abs(y); - } + if (isNumBool(x) && isNumBool(y)) { + return x > y; } if ((isUnit(x)) && (isUnit(y))) { @@ -61,6 +48,10 @@ module.exports = function (math) { return collection.deepMap2(x, y, larger); } + if (isComplex(x) || isComplex(y)) { + throw new TypeError('No ordering relation is defined for complex numbers'); + } + if (x.valueOf() !== x || y.valueOf() !== y) { // fallback on the objects primitive values return larger(x.valueOf(), y.valueOf()); diff --git a/lib/function/arithmetic/largereq.js b/lib/function/arithmetic/largereq.js index 7beecd22d..4d8e7eb0b 100644 --- a/lib/function/arithmetic/largereq.js +++ b/lib/function/arithmetic/largereq.js @@ -20,8 +20,8 @@ module.exports = function (math) { * For matrices, the function is evaluated element wise. * In case of complex numbers, the absolute values of a and b are compared. * - * @param {Number | Boolean | Complex | Unit | String | Array | Matrix} x - * @param {Number | Boolean | Complex | Unit | String | Array | Matrix} y + * @param {Number | Boolean | Unit | String | Array | Matrix} x + * @param {Number | Boolean | Unit | String | Array | Matrix} y * @return {Boolean | Array | Matrix} res */ math.largereq = function largereq(x, y) { @@ -29,21 +29,8 @@ module.exports = function (math) { throw new util.error.ArgumentsError('largereq', arguments.length, 2); } - if (isNumBool(x)) { - if (isNumBool(y)) { - return x >= y; - } - else if (isComplex(y)) { - return x >= math.abs(y); - } - } - if (isComplex(x)) { - if (isNumBool(y)) { - return math.abs(x) >= y; - } - else if (isComplex(y)) { - return math.abs(x) >= math.abs(y); - } + if (isNumBool(x) && isNumBool(y)) { + return x >= y; } if ((isUnit(x)) && (isUnit(y))) { @@ -61,6 +48,10 @@ module.exports = function (math) { return collection.deepMap2(x, y, largereq); } + if (isComplex(x) || isComplex(y)) { + throw new TypeError('No ordering relation is defined for complex numbers'); + } + if (x.valueOf() !== x || y.valueOf() !== y) { // fallback on the objects primitive values return largereq(x.valueOf(), y.valueOf()); diff --git a/lib/function/arithmetic/smaller.js b/lib/function/arithmetic/smaller.js index 6056832c1..dfce3c286 100644 --- a/lib/function/arithmetic/smaller.js +++ b/lib/function/arithmetic/smaller.js @@ -20,8 +20,8 @@ module.exports = function (math) { * For matrices, the function is evaluated element wise. * In case of complex numbers, the absolute values of a and b are compared. * - * @param {Number | Boolean | Complex | Unit | String | Array | Matrix} x - * @param {Number | Boolean | Complex | Unit | String | Array | Matrix} y + * @param {Number | Boolean | Unit | String | Array | Matrix} x + * @param {Number | Boolean | Unit | String | Array | Matrix} y * @return {Boolean | Array | Matrix} res */ math.smaller = function smaller(x, y) { @@ -29,21 +29,8 @@ module.exports = function (math) { throw new util.error.ArgumentsError('smaller', arguments.length, 2); } - if (isNumBool(x)) { - if (isNumBool(y)) { - return x < y; - } - else if (isComplex(y)) { - return x < math.abs(y); - } - } - if (isComplex(x)) { - if (isNumBool(y)) { - return math.abs(x) < y; - } - else if (isComplex(y)) { - return math.abs(x) < math.abs(y); - } + if (isNumBool(x) && isNumBool(y)) { + return x < y; } if ((isUnit(x)) && (isUnit(y))) { @@ -61,6 +48,10 @@ module.exports = function (math) { return collection.deepMap2(x, y, smaller); } + if (isComplex(x) || isComplex(y)) { + throw new TypeError('No ordering relation is defined for complex numbers'); + } + if (x.valueOf() !== x || y.valueOf() !== y) { // fallback on the objects primitive values return smaller(x.valueOf(), y.valueOf()); diff --git a/lib/function/arithmetic/smallereq.js b/lib/function/arithmetic/smallereq.js index 22d4ead36..2e3b45a97 100644 --- a/lib/function/arithmetic/smallereq.js +++ b/lib/function/arithmetic/smallereq.js @@ -29,21 +29,8 @@ module.exports = function (math) { throw new util.error.ArgumentsError('smallereq', arguments.length, 2); } - if (isNumBool(x)) { - if (isNumBool(y)) { - return x <= y; - } - else if (isComplex(y)) { - return x <= math.abs(y); - } - } - if (isComplex(x)) { - if (isNumBool(y)) { - return math.abs(x) <= y; - } - else if (isComplex(y)) { - return math.abs(x) <= math.abs(y); - } + if (isNumBool(x) && isNumBool(y)) { + return x <= y; } if ((isUnit(x)) && (isUnit(y))) { @@ -61,6 +48,10 @@ module.exports = function (math) { return collection.deepMap2(x, y, smallereq); } + if (isComplex(x) || isComplex(y)) { + throw new TypeError('No ordering relation is defined for complex numbers'); + } + if (x.valueOf() !== x || y.valueOf() !== y) { // fallback on the objects primitive values return smallereq(x.valueOf(), y.valueOf()); diff --git a/test/function/arithmetic/larger.test.js b/test/function/arithmetic/larger.test.js index a1daba1a8..9898df63e 100644 --- a/test/function/arithmetic/larger.test.js +++ b/test/function/arithmetic/larger.test.js @@ -31,20 +31,6 @@ describe('larger', function() { assert.equal(larger(false, 2), false); }); - it('should compare two complex numbers correctly', function() { - assert.equal(larger(complex(1,1), complex(1,2)), false); - assert.equal(larger(complex(1,1), complex(1,1)), false); - assert.equal(larger(complex(1,1), complex(2,1)), false); - assert.equal(larger(complex(1,6), complex(7,1)), false); - assert.equal(larger(complex(4,1), complex(2,2)), true); - assert.equal(larger(complex(2,0), 3), false); - assert.equal(larger(complex(2,0), 2), false); - assert.equal(larger(complex(2,0), 1), true); - assert.equal(larger(3, complex(2,0)), true); - assert.equal(larger(2, complex(2,0)), false); - assert.equal(larger(1, complex(2,0)), false); - }); - it('should add two measures of the same unit', function() { assert.equal(larger(unit('100cm'), unit('10inch')), true); assert.equal(larger(unit('99cm'), unit('1m')), false); @@ -69,6 +55,12 @@ describe('larger', function() { assert.deepEqual(larger([1,4,6], matrix([3,4,5])), matrix([false, false, true])); }); + it('should throw an error when comparing complex numbers', function() { + assert.throws(function () {larger(complex(1,1), complex(1,2))}, TypeError); + assert.throws(function () {larger(complex(2,1), 3)}, TypeError); + assert.throws(function () {larger(3, complex(2,4))}, TypeError); + }); + it('should throw an error if matrices are different sizes', function() { assert.throws(function () {larger([1,4,6], [3,4])}); }); diff --git a/test/function/arithmetic/largereq.test.js b/test/function/arithmetic/largereq.test.js index 3e47afebc..0bf8f0957 100644 --- a/test/function/arithmetic/largereq.test.js +++ b/test/function/arithmetic/largereq.test.js @@ -33,20 +33,6 @@ describe('largereq', function() { assert.equal(largereq(false, 0), true); }); - it('should compare two complex numbers correctly', function() { - assert.equal(largereq(complex(1,1), complex(1,2)), false); - assert.equal(largereq(complex(1,1), complex(1,1)), true); - assert.equal(largereq(complex(1,1), complex(2,1)), false); - assert.equal(largereq(complex(1,6), complex(7,1)), false); - assert.equal(largereq(complex(4,1), complex(2,2)), true); - assert.equal(largereq(complex(2,0), 3), false); - assert.equal(largereq(complex(2,0), 2), true); - assert.equal(largereq(complex(2,0), 1), true); - assert.equal(largereq(3, complex(2,0)), true); - assert.equal(largereq(2, complex(2,0)), true); - assert.equal(largereq(1, complex(2,0)), false); - }); - it('should compare two units correctly', function() { assert.equal(largereq(unit('100cm'), unit('10inch')), true); assert.equal(largereq(unit('99cm'), unit('1m')), false); @@ -70,6 +56,12 @@ describe('largereq', function() { 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); + }); + it('should throw an error if comparing two matrices of different sizes', function() { assert.throws(function () {largereq([1,4,6], [3,4])}); }); diff --git a/test/function/arithmetic/smaller.test.js b/test/function/arithmetic/smaller.test.js index bed969cbe..2bcef7a8d 100644 --- a/test/function/arithmetic/smaller.test.js +++ b/test/function/arithmetic/smaller.test.js @@ -34,20 +34,6 @@ describe('smaller', function() { assert.equal(smaller(false, 2), true); }); - it('should compare two complex numbers correctly', function() { - assert.equal(smaller(complex(1,1), complex(1,2)), true); - assert.equal(smaller(complex(1,1), complex(1,1)), false); - assert.equal(smaller(complex(1,1), complex(2,1)), true); - assert.equal(smaller(complex(1,6), complex(7,1)), true); - assert.equal(smaller(complex(4,1), complex(2,2)), false); - assert.equal(smaller(complex(2,0), 3), true); - assert.equal(smaller(complex(2,0), 2), false); - assert.equal(smaller(complex(2,0), 1), false); - assert.equal(smaller(3, complex(2,0)), false); - assert.equal(smaller(2, complex(2,0)), false); - assert.equal(smaller(1, complex(2,0)), true); - }); - it('should compare two measures of the same unit correctly', function() { assert.equal(smaller(unit('100cm'), unit('10inch')), false); assert.equal(smaller(unit('99cm'), unit('1m')), true); @@ -71,6 +57,12 @@ describe('smaller', function() { assert.deepEqual(smaller([1,4,6], matrix([3,4,5])), matrix([true, false, false])); }); + it('should throw an error when comparing complex numbers', function() { + assert.throws(function () {smaller(complex(1,1), complex(1,2))}, TypeError); + assert.throws(function () {smaller(complex(2,1), 3)}, TypeError); + assert.throws(function () {smaller(3, complex(2,4))}, TypeError); + }); + it('should throw an error with two matrices of different sizes', function () { assert.throws(function () {smaller([1,4,6], [3,4])}); }); diff --git a/test/function/arithmetic/smallereq.test.js b/test/function/arithmetic/smallereq.test.js index ec04ccb9e..ec18899dd 100644 --- a/test/function/arithmetic/smallereq.test.js +++ b/test/function/arithmetic/smallereq.test.js @@ -35,21 +35,6 @@ describe('smallereq', function() { assert.equal(smallereq(false, 2), true); }); - it('should compare two complex numbers correctly', function() { - assert.equal(smallereq(complex(1,1), complex(1,2)), true); - assert.equal(smallereq(complex(1,1), complex(1,1)), true); - assert.equal(smallereq(complex(2,4), complex(4,2)), true); - assert.equal(smallereq(complex(1,1), complex(2,1)), true); - assert.equal(smallereq(complex(1,6), complex(7,1)), true); - assert.equal(smallereq(complex(4,1), complex(2,2)), false); - assert.equal(smallereq(complex(2,0), 3), true); - assert.equal(smallereq(complex(2,0), 2), true); - assert.equal(smallereq(complex(2,0), 1), false); - assert.equal(smallereq(3, complex(2,0)), false); - assert.equal(smallereq(2, complex(2,0)), true); - assert.equal(smallereq(1, complex(2,0)), 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); @@ -61,7 +46,7 @@ describe('smallereq', function() { assert.throws(function () {smallereq(unit('100cm'), 22)}); }); - it('should perform lexical comprison of two strings', function() { + 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); @@ -73,6 +58,12 @@ describe('smallereq', function() { 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); + }); + it('should throw an error with two matrices of different sizes', function () { assert.throws(function () {smallereq([1,4,6], [3,4])}); }); diff --git a/test/function/statistics/max.test.js b/test/function/statistics/max.test.js index a97576bf4..711d962e6 100644 --- a/test/function/statistics/max.test.js +++ b/test/function/statistics/max.test.js @@ -15,18 +15,6 @@ describe('max', function() { assert.equal(math.max('A', 'C', 'D', 'B'), 'D'); }); - it('should return the max value for complex values', function() { - assert.deepEqual(math.max(math.complex(2,3), math.complex(2,1)), math.complex(2,3)); - assert.deepEqual(math.max(math.complex(2,3), math.complex(2,5)), math.complex(2,5)); - }); - - it('should return the max value for mixed real and complex values', function() { - assert.deepEqual(math.max(math.complex(3,4), 4), math.complex(3,4)); - assert.deepEqual(math.max(math.complex(3,4), 5), math.complex(3,4)); - assert.deepEqual(math.max(5, math.complex(3,4)), 5); - assert.deepEqual(math.max(math.complex(3,4), 6), 6); - }); - it('should return the max element from a vector', function() { assert.equal(math.max(math.matrix([1,3,5,2,-5])), 5); }); @@ -44,6 +32,16 @@ describe('max', function() { ])), math.matrix([ 3, 9, 11])); }); + it('should throw an error when called with complex numbers', function() { + assert.throws(function () {math.max(math.complex(2,3), math.complex(2,1))}, TypeError); + assert.throws(function () {math.max(math.complex(2,3), math.complex(2,5))}, TypeError); + + assert.throws(function () {math.max(math.complex(3,4), 4)}, TypeError); + assert.throws(function () {math.max(math.complex(3,4), 5)}, TypeError); + assert.throws(function () {math.max(5, math.complex(3,4))}, TypeError); + assert.throws(function () {math.max(math.complex(3,4), 6)}, TypeError); + }); + it('should throw an error if called with invalid number of arguments', function() { assert.throws(function() {math.max()}); }); diff --git a/test/function/statistics/min.test.js b/test/function/statistics/min.test.js index 60cadb836..3af5da3be 100644 --- a/test/function/statistics/min.test.js +++ b/test/function/statistics/min.test.js @@ -15,18 +15,6 @@ describe('min', function() { assert.equal(math.min('A', 'C', 'D', 'B'), 'A'); }); - it('should return the min value for complex values', function() { - assert.deepEqual(math.min(math.complex(2,3), math.complex(2,1)), math.complex(2,1)); - assert.deepEqual(math.min(math.complex(2,3), math.complex(2,5)), math.complex(2,3)); - }); - - it('should return the min value for mixed real and complex values', function() { - assert.deepEqual(math.min(math.complex(3,4), 4), 4); - assert.deepEqual(math.min(math.complex(3,4), 5), math.complex(3,4)); - assert.deepEqual(math.min(5, math.complex(3,4)), 5); - assert.deepEqual(math.min(math.complex(3,4), 6), math.complex(3,4)); - }); - it('should return the min element from a vector', function() { assert.equal(math.min([1,3,5,-5,2]), -5); }); @@ -48,6 +36,16 @@ describe('min', function() { ])), math.matrix([-1, 0, 5])); }); + it('should throw an error when called with complex numbers', function() { + assert.throws(function () {math.min(math.complex(2,3), math.complex(2,1))}, TypeError); + assert.throws(function () {math.min(math.complex(2,3), math.complex(2,5))}, TypeError); + + assert.throws(function () {math.min(math.complex(3,4), 4)}, TypeError); + assert.throws(function () {math.min(math.complex(3,4), 5)}, TypeError); + assert.throws(function () {math.min(5, math.complex(3,4))}, TypeError); + assert.throws(function () {math.min(math.complex(3,4), 6)}, TypeError); + }); + it('should throw an error if called with invalid number of arguments', function() { assert.throws(function() {math.min()}); });