// matrix A
let matrixA = [
[6, -2, -3],
[-2, 5, -2],
[-3, -2, 8],
];
// Generate L and U using matrix A length
let matrixL = Array(matrixA.length)
.fill()
.map(() => Array(matrixA.length).fill(0));
let matrixU = Array(matrixA.length)
.fill()
.map(() => Array(matrixA.length).fill(0));
crout = () => {
// assign matrix A length to a variable
let n = matrixA.length;
// loop in to index
for (i = 0; i < n; i++) {
for (j = 0; j < n; j++) {
// if j is less than i change value of matrixL to 0 with its corresponding indexes
if (j < i) matrixL[j][i] = 0;
else {
// the first column of matrix L is equal to the first column of matrix A
matrixL[j][i] = matrixA[j][i];
// loop the values excluding the first column and top row
for (k = 0; k < i; k++) {
matrixL[j][i] = matrixL[j][i] - matrixL[j][k] * matrixU[k][i];
}
}
}
// loop in to index
for (j = 0; j < n; j++) {
// if j is less than i change value of martixU to 0 with its corresponding indexes
if (j < i) matrixU[i][j] = 0;
// if j is equal to i the change value to 1
// result to a diagonal values of 1
else if (j == i) matrixU[i][j] = 1;
else {
// get index of the first rows of matrixU
matrixU[i][j] = matrixA[i][j] / matrixL[i][i];
// loop to get values excluding the first rows, 1's and 0's
for (k = 0; k < i; k++) {
matrixU[i][j] =
matrixU[i][j] - (matrixL[i][k] * matrixU[k][j]) / matrixL[i][i];
}
}
}
}
// print in console
console.log("matrixA:\n", matrixA, "\n\nmatrixL:\n", matrixL, "\n\nmatrixU:\n", matrixU);
};
crout();
Replies to crout's method javascript
{"html5":"htmlmixed","css":"css","javascript":"javascript","php":"php","python":"python","ruby":"ruby","lua":"text\/x-lua","bash":"text\/x-sh","go":"go","c":"text\/x-csrc","cpp":"text\/x-c++src","diff":"diff","latex":"stex","sql":"sql","xml":"xml","apl":"apl","asterisk":"asterisk","c_loadrunner":"text\/x-csrc","c_mac":"text\/x-csrc","coffeescript":"text\/x-coffeescript","csharp":"text\/x-csharp","d":"d","ecmascript":"javascript","erlang":"erlang","groovy":"text\/x-groovy","haskell":"text\/x-haskell","haxe":"text\/x-haxe","html4strict":"htmlmixed","java":"text\/x-java","java5":"text\/x-java","jquery":"javascript","mirc":"mirc","mysql":"sql","ocaml":"text\/x-ocaml","pascal":"text\/x-pascal","perl":"perl","perl6":"perl","plsql":"sql","properties":"text\/x-properties","q":"text\/x-q","scala":"scala","scheme":"text\/x-scheme","tcl":"text\/x-tcl","vb":"text\/x-vb","verilog":"text\/x-verilog","yaml":"text\/x-yaml","z80":"text\/x-z80"}