Skip to content

Commit

Permalink
Merge pull request #128 from StDako/master
Browse files Browse the repository at this point in the history
added diagonal dominance functions, update src
  • Loading branch information
Dakkers committed Oct 9, 2014
2 parents a3b3287 + 94adcc1 commit dd2066c
Show file tree
Hide file tree
Showing 4 changed files with 380 additions and 22 deletions.
140 changes: 138 additions & 2 deletions lib/numbers/matrix.js
Original file line number Diff line number Diff line change
Expand Up @@ -779,7 +779,7 @@ matrix.reverseCols = function(M) {
* @return {Array} matrix
*/
matrix.zeros = function(n,m) {
var M = [];
var M = new Array(n);
if (n < 1 || m < 1) {
throw new Error('The matrix dimensions must be positive integers.');
}
Expand All @@ -790,7 +790,7 @@ matrix.zeros = function(n,m) {
for (var j=0; j<m; j++) {
empty[j] = 0;
}
M.push(empty);
M[i] = empty;
}
return M;
};
Expand Down Expand Up @@ -1153,4 +1153,140 @@ matrix.isLowerBand = function(M,p) {
cnt++;
}
return result;
};

/**
* Determines if a matrix is (weak) row diagonally-dominant.
*
* @param {Array} matrix
* @return {Boolean} true if so, false otherwise.
*/
matrix.isRowDD = function(M) {
var n = M.length;
if (n !== M[0].length) {
throw new Error("The given matrix must be square.");
} else if (M[0][0] === undefined) {
throw new Error("Input must be a matrix.");
}

for (var i=0; i<n; i++) {
var row = M[i],
diag = row[i],
sum = 0,
j;

for (j=0; j<i; j++) {
sum += Math.abs(row[j]);
}
for (j=i+1; j<n; j++) {
sum += Math.abs(row[j]);
}

if (Math.abs(diag) < sum) {
return false;
}
}
return true;
};

/**
* Determines if a matrix is strictly row diagonally-dominant.
*
* @param {Array} matrix
* @return {Boolean} true if so, false otherwise.
*/
matrix.isStrictlyRowDD = function(M) {
var n = M.length;
if (n !== M[0].length) {
throw new Error("The given matrix must be square.");
} else if (M[0][0] === undefined) {
throw new Error("Input must be a matrix.");
}

for (var i=0; i<n; i++) {
var row = M[i],
diag = row[i],
sum = 0,
j;

for (j=0; j<i; j++) {
sum += Math.abs(row[j]);
}
for (j=i+1; j<n; j++) {
sum += Math.abs(row[j]);
}

if (Math.abs(diag) <= sum) {
return false;
}
}
return true;
};

/**
* Determines if a matrix is (weak) column diagonally-dominant.
*
* @param {Array} matrix
* @return {Boolean} true if so, false otherwise.
*/
matrix.isColumnDD = function(M) {
var n = M.length;
if (n !== M[0].length) {
throw new Error("The given matrix must be square.");
} else if (M[0][0] === undefined) {
throw new Error("Input must be a matrix.");
}

for (var i=0; i<n; i++) {
var col = matrix.getCol(M,i),
diag = col[i],
sum = 0,
j;

for (j=0; j<i; j++) {
sum += Math.abs(col[j]);
}
for (j=i+1; j<n; j++) {
sum += Math.abs(col[j]);
}

if (Math.abs(diag) < sum) {
return false;
}
}
return true;
};

/**
* Determines if a matrix is strictly column diagonally-dominant.
*
* @param {Array} matrix
* @return {Boolean} true if so, false otherwise.
*/
matrix.isStrictlyColumnDD = function(M) {
var n = M.length;
if (n !== M[0].length) {
throw new Error("The given matrix must be square.");
} else if (M[0][0] === undefined) {
throw new Error("Input cannot be a vector.");
}

for (var i=0; i<n; i++) {
var col = matrix.getCol(M,i),
diag = col[i],
sum = 0,
j;

for (j=0; j<i; j++) {
sum += Math.abs(col[j]);
}
for (j=i+1; j<n; j++) {
sum += Math.abs(col[j]);
}

if (Math.abs(diag) <= sum) {
return false;
}
}
return true;
};
Loading

0 comments on commit dd2066c

Please sign in to comment.