Removed support for comparing complex numbers

This commit is contained in:
josdejong 2013-10-17 14:49:30 +02:00
parent c3d5edf4bb
commit eb9ffdeadf
11 changed files with 77 additions and 148 deletions

View File

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

View File

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

View File

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

View File

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

View File

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

View File

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

View File

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

View File

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

View File

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

View File

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

View File

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