Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

added diagonal dominance functions, update src #128

Merged
merged 1 commit into from
Oct 9, 2014
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
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