Skip to content

Commit

Permalink
Merge pull request #440 from rjbaucells/develop
Browse files Browse the repository at this point in the history
Fix for issue #437 (Possible bug in lusolve)
  • Loading branch information
josdejong committed Aug 28, 2015
2 parents 3b80df8 + 06c3cd3 commit 08ccb24
Show file tree
Hide file tree
Showing 5 changed files with 39 additions and 24 deletions.
10 changes: 7 additions & 3 deletions lib/function/algebra/decomposition/lup.js
Expand Up @@ -211,12 +211,16 @@ function factory (type, config, load, typed) {
var u = new DenseMatrix({
data: udata,
size: usize
});
});
// p vector
var pv = [];
for (i = 0, n = p.length; i < n; i++)
pv[p[i]] = i;
// return matrices
return {
L: l,
U: u,
p: p,
p: pv,
toString: function () {
return 'L: ' + this.L.toString() + '\nU: ' + this.U.toString() + '\nP: ' + this.p;
}
Expand Down Expand Up @@ -369,7 +373,7 @@ function factory (type, config, load, typed) {
ptr: uptr,
size: usize
}),
p: pv_oc,
p: pv_co,
toString: function () {
return 'L: ' + this.L.toString() + '\nU: ' + this.U.toString() + '\nP: ' + this.p;
}
Expand Down
2 changes: 1 addition & 1 deletion lib/function/algebra/sparse/cs_ipvec.js
Expand Up @@ -13,7 +13,7 @@ function factory () {
var cs_ipvec = function (p, b, n) {
// vars
var k;
var n = p.length;
var n = b.length;
var x = [];
// check permutation vector was provided, p = null denotes identity
if (p) {
Expand Down
2 changes: 1 addition & 1 deletion lib/type/matrix/FibonacciHeap.js
Expand Up @@ -147,7 +147,7 @@ function factory (type, config, load, typed) {
*/
FibonacciHeap.prototype.remove = function (node) {
// decrease key value
this._minimum = _decreaseKey(this._minimum, node, null);
this._minimum = _decreaseKey(this._minimum, node, -1);
// remove the smallest
this.extractMinimum();
};
Expand Down
19 changes: 6 additions & 13 deletions test/function/algebra/decomposition/lup.test.js
Expand Up @@ -148,7 +148,7 @@ describe('lup', function () {
[0, 0, 0, 4]
]);
// P
assert.deepEqual(r.p, [2, 1, 3, 0]);
assert.deepEqual(r.p, [3, 1, 0, 2]);
// verify
approx.deepEqual(math.multiply(_p(r.p), m).valueOf(), math.multiply(r.L, r.U).valueOf());
});
Expand Down Expand Up @@ -304,7 +304,7 @@ describe('lup', function () {
[0, 0, 0, 4]
]);
// P
assert.deepEqual(r.p, [2, 1, 3, 0]);
assert.deepEqual(r.p, [3, 1, 0, 2]);
// verify
approx.deepEqual(math.multiply(_p(r.p), m).valueOf(), math.multiply(r.L, r.U).valueOf());
});
Expand Down Expand Up @@ -375,21 +375,14 @@ describe('lup', function () {
* Creates a Matrix out of a row permutation vector
*/
function _p(p) {
// identity matrix
var identity = math.eye(p.length);
// array
var data = [];
// loop rows
for (var i = 0, l = p.length; i < l; i++) {
// row
var r = p[i];
// init data row
data[i] = [];
// loop columns
for (var j = 0; j < l; j++) {
if (r === j)
data[i][j] = 1;
else
data[i][j] = 0;
}
// swap row
data[p[i]] = identity._data[i];
}
return data;
}
Expand Down
30 changes: 24 additions & 6 deletions test/function/algebra/solver/lusolve.test.js
Expand Up @@ -220,20 +220,38 @@ describe('lusolve', function () {
approx.deepEqual(x, [[-5/3], [7/3], [-1]]);
});

it('should solve linear system 3 x 3, permutations, matrix', function () {
it('should solve linear system 4 x 4, permutations, matrix - Issue 437', function () {
var m = math.matrix(
[
[1, 2, -1],
[2, 1, 1],
[1, 2, 1]
[ -1, 1, -1, 1],
[ 0, 0, 0, 1],
[ 1, 1, 1, 1],
[ 8, 4, 2, 1]
]);
var b = [4, -2, 2];

var b = [0.1, 0.2, 0.15, 0.1];

var x = math.lusolve(m, b);

approx.deepEqual(x, math.matrix([[-5/3], [7/3], [-1]]));
approx.deepEqual(x, math.matrix([[0.025], [-0.075], [0], [0.2]]));
});

it('should solve linear system 4 x 4, permutations, sparse - Issue 437', function () {
var m = math.sparse(
[
[ -1, 1, -1, 1],
[ 0, 0, 0, 1],
[ 1, 1, 1, 1],
[ 8, 4, 2, 1]
]);

var b = [0.1, 0.2, 0.15, 0.1];

var x = math.lusolve(m, b);

approx.deepEqual(x, math.matrix([[0.025], [-0.075], [0], [0.2]]));
});

it('should solve linear system 3 x 3, permutations, sparse matrix', function () {
var m = math.matrix(
[
Expand Down

0 comments on commit 08ccb24

Please sign in to comment.