largerEq updates

This commit is contained in:
rjbaucells 2015-05-03 22:28:35 -04:00
parent 5880adfd8c
commit 5293c22c10
2 changed files with 168 additions and 24 deletions

View File

@ -3,7 +3,14 @@
var nearlyEqual = require('../../util/number').nearlyEqual;
function factory (type, config, load, typed) {
var collection = load(require('../../type/collection'));
var matrix = load(require('../construction/matrix'));
var algorithm03 = load(require('../../type/matrix/util/algorithm03'));
var algorithm07 = load(require('../../type/matrix/util/algorithm07'));
var algorithm11 = load(require('../../type/matrix/util/algorithm11'));
var algorithm12 = load(require('../../type/matrix/util/algorithm12'));
var algorithm13 = load(require('../../type/matrix/util/algorithm13'));
/**
* Test whether value x is larger or equal to y.
@ -32,6 +39,7 @@ function factory (type, config, load, typed) {
* @return {Boolean | Array | Matrix} Returns true when the x is larger or equal to y, else returns false
*/
var largerEq = typed('largerEq', {
'boolean, boolean': function (x, y) {
return x >= y;
},
@ -44,7 +52,7 @@ function factory (type, config, load, typed) {
return x.gte(y);
},
'Complex, Complex': function (x, y) {
'Complex, Complex': function () {
throw new TypeError('No ordering relation is defined for complex numbers');
},
@ -59,12 +67,93 @@ function factory (type, config, load, typed) {
return x >= y;
},
'Array | Matrix, any': function (x, y) {
return collection.deepMap2(x, y, largerEq);
'Matrix, Matrix': function (x, y) {
// result
var c;
// process matrix storage
switch (x.storage()) {
case 'sparse':
switch (y.storage()) {
case 'sparse':
// sparse + sparse
c = algorithm07(x, y, largerEq);
break;
default:
// sparse + dense
c = algorithm03(y, x, largerEq, true);
break;
}
break;
default:
switch (y.storage()) {
case 'sparse':
// dense + sparse
c = algorithm03(x, y, largerEq, false);
break;
default:
// dense + dense
c = algorithm12(x, y, largerEq);
break;
}
break;
}
return c;
},
'any, Array | Matrix': function (x, y) {
return collection.deepMap2(x, y, largerEq);
'Array, Array': function (x, y) {
// use matrix implementation
return largerEq(matrix(x), matrix(y)).valueOf();
},
'Array, Matrix': function (x, y) {
// use matrix implementation
return largerEq(matrix(x), y);
},
'Matrix, Array': function (x, y) {
// use matrix implementation
return largerEq(x, matrix(y));
},
'Matrix, any': function (x, y) {
// result
var c;
// check storage format
switch (x.storage()) {
case 'sparse':
c = algorithm11(x, y, largerEq, false);
break;
default:
c = algorithm13(x, y, largerEq, false);
break;
}
return c;
},
'any, Matrix': function (x, y) {
// result
var c;
// check storage format
switch (y.storage()) {
case 'sparse':
c = algorithm11(y, x, largerEq, true);
break;
default:
c = algorithm13(y, x, largerEq, true);
break;
}
return c;
},
'Array, any': function (x, y) {
// use matrix implementation
return algorithm13(matrix(x), y, largerEq, false).valueOf();
},
'any, Array': function (x, y) {
// use matrix implementation
return algorithm13(matrix(y), x, largerEq, true).valueOf();
}
});

View File

@ -4,6 +4,7 @@ var assert = require('assert'),
bignumber = math.bignumber,
complex = math.complex,
matrix = math.matrix,
sparse = math.sparse,
unit = math.unit,
largerEq = math.largerEq;
@ -67,8 +68,8 @@ describe('largerEq', function() {
assert.equal(largerEq(bignumber(2), 3), false);
assert.equal(largerEq(2, bignumber(2)), true);
assert.throws(function () {largerEq(1/3, bignumber(1).div(3))}, /TypeError: Cannot implicitly convert a number with >15 significant digits to BigNumber/);
assert.throws(function () {largerEq(bignumber(1).div(3), 1/3)}, /TypeError: Cannot implicitly convert a number with >15 significant digits to BigNumber/);
assert.throws(function () {largerEq(1/3, bignumber(1).div(3));}, /TypeError: Cannot implicitly convert a number with >15 significant digits to BigNumber/);
assert.throws(function () {largerEq(bignumber(1).div(3), 1/3);}, /TypeError: Cannot implicitly convert a number with >15 significant digits to BigNumber/);
});
it('should compare mixed booleans and bignumbers', function() {
@ -95,7 +96,7 @@ describe('largerEq', function() {
});
it('should throw an error if comparing a unit with a number', function() {
assert.throws(function () {largerEq(unit('100cm'), 22)});
assert.throws(function () {largerEq(unit('100cm'), 22);});
});
it('should throw an error for two measures of different units', function() {
@ -103,7 +104,7 @@ describe('largerEq', function() {
});
it('should throw an error if comparing a unit with a bignumber', function() {
assert.throws(function () {largerEq(unit('100cm'), bignumber(22))});
assert.throws(function () {largerEq(unit('100cm'), bignumber(22));});
});
it('should perform lexical comparison for 2 strings', function() {
@ -113,31 +114,85 @@ describe('largerEq', function() {
assert.equal(largerEq('abc', 'abd'), false);
});
it('should compare a string an matrix elementwise', function() {
assert.deepEqual(largerEq('B', ['A', 'B', 'C']), [true, true, false]);
assert.deepEqual(largerEq(['A', 'B', 'C'], 'B'), [false, true, true]);
describe('Array', function () {
it('should compare array - scalar', function () {
assert.deepEqual(largerEq('B', ['A', 'B', 'C']), [true, true, false]);
assert.deepEqual(largerEq(['A', 'B', 'C'], 'B'), [false, true, true]);
});
it('should compare array - array', function () {
assert.deepEqual(largerEq([[1, 2, 0], [-1, 0, 2]], [[1, -1, 0], [-1, 1, 0]]), [[true, true, true], [true, false, true]]);
});
it('should compare array - dense matrix', function () {
assert.deepEqual(largerEq([[1, 2, 0], [-1, 0, 2]], matrix([[1, -1, 0], [-1, 1, 0]])), matrix([[true, true, true], [true, false, true]]));
});
it('should compare array - sparse matrix', function () {
assert.deepEqual(largerEq([[1, 2, 0], [-1, 0, 2]], sparse([[1, -1, 0], [-1, 1, 0]])), matrix([[true, true, true], [true, false, true]]));
});
it('should throw an error if arrays have different sizes', function() {
assert.throws(function () {largerEq([1,4,5], [3,4]);});
});
});
it('should perform element-wise comparison for two matrices of the same size', function() {
assert.deepEqual(largerEq([1,4,6], [3,4,5]), [false, true, true]);
assert.deepEqual(largerEq([1,4,6], matrix([3,4,5])), matrix([false, true, true]));
describe('DenseMatrix', function () {
it('should compare dense matrix - scalar', function () {
assert.deepEqual(largerEq('B', matrix(['A', 'B', 'C'])), matrix([true, true, false]));
assert.deepEqual(largerEq(matrix(['A', 'B', 'C']), 'B'), matrix([false, true, true]));
});
it('should compare dense matrix - array', function () {
assert.deepEqual(largerEq(matrix([[1, 2, 0], [-1, 0, 2]]), [[1, -1, 0], [-1, 1, 0]]), matrix([[true, true, true], [true, false, true]]));
});
it('should compare dense matrix - dense matrix', function () {
assert.deepEqual(largerEq(matrix([[1, 2, 0], [-1, 0, 2]]), matrix([[1, -1, 0], [-1, 1, 0]])), matrix([[true, true, true], [true, false, true]]));
});
it('should compare dense matrix - sparse matrix', function () {
assert.deepEqual(largerEq(matrix([[1, 2, 0], [-1, 0, 2]]), sparse([[1, -1, 0], [-1, 1, 0]])), matrix([[true, true, true], [true, false, true]]));
});
});
describe('SparseMatrix', function () {
it('should compare sparse matrix - scalar', function () {
assert.deepEqual(largerEq('B', sparse([['A', 'B'], ['C', 'D']])), matrix([[true, true], [false, false]]));
assert.deepEqual(largerEq(sparse([['A', 'B'], ['C', 'D']]), 'B'), matrix([[false, true], [true, true]]));
});
it('should compare sparse matrix - array', function () {
assert.deepEqual(largerEq(sparse([[1, 2, 0], [-1, 0, 2]]), [[1, -1, 0], [-1, 1, 0]]), matrix([[true, true, true], [true, false, true]]));
});
it('should compare sparse matrix - dense matrix', function () {
assert.deepEqual(largerEq(sparse([[1, 2, 0], [-1, 0, 2]]), matrix([[1, -1, 0], [-1, 1, 0]])), matrix([[true, true, true], [true, false, true]]));
});
it('should compare sparse matrix - sparse matrix', function () {
assert.deepEqual(largerEq(sparse([[1, 2, 0], [-1, 0, 2]]), sparse([[1, -1, 0], [-1, 1, 0]])), matrix([[true, true, true], [true, false, 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);
assert.throws(function () {largerEq(math.bignumber(3), complex(2,4))}, TypeError);
assert.throws(function () {largerEq(complex(2,4), math.bignumber(3))}, TypeError);
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);
assert.throws(function () {largerEq(math.bignumber(3), complex(2,4));}, TypeError);
assert.throws(function () {largerEq(complex(2,4), math.bignumber(3));}, TypeError);
});
it('should throw an error if comparing two matrices of different sizes', function() {
assert.throws(function () {largerEq([1,4,6], [3,4])});
assert.throws(function () {largerEq([1,4,6], [3,4]);});
});
it('should throw an error in case of invalid number of arguments', function() {
assert.throws(function () {largerEq(1)}, /TypeError: Too few arguments/);
assert.throws(function () {largerEq(1, 2, 3)}, /TypeError: Too many arguments/);
assert.throws(function () {largerEq(1);}, /TypeError: Too few arguments/);
assert.throws(function () {largerEq(1, 2, 3);}, /TypeError: Too many arguments/);
});
it('should LaTeX largerEq', function () {