Tell us what’s happening:
I tried to complete the project and I got this error message and no response to this request // running tests
4. luDecomposition([[11, 9, 24, 2], [1, 5, 2, 6], [3, 17, 18, 1], [2, 5, 7, 1]]) should return [[[1, 0, 0, 0], [0.2727272727272727, 1, 0, 0], [0.09090909090909091, 0.2875, 1, 0], [0.18181818181818182, 0.23124999999999996, 0.0035971223021580693, 1]], [[11, 9, 24, 2], [0, 14.545454545454547, 11.454545454545455, 0.4545454545454546], [0, 0, -3.4749999999999996, 5.6875], [0, 0, 0, 0.510791366
Your code so far
function luDecomposition(A) {
const n = A.length;
const L = Array.from({ length: n }, () => Array(n).fill(0));
const U = Array.from({ length: n }, () => Array(n).fill(0));
const P = Array.from({ length: n }, () => Array(n).fill(0));
// Initialize the permutation matrix
for (let i = 0; i < n; i++) {
P[i][i] = 1;
}
for (let i = 0; i < n; i++) {
// Pivoting
let maxIndex = i;
for (let j = i + 1; j < n; j++) {
if (Math.abs(A[j][i]) > Math.abs(A[maxIndex][i])) {
maxIndex = j;
}
}
if (i !== maxIndex) {
[A[i], A[maxIndex]] = [A[maxIndex], A[i]];
[P[i], P[maxIndex]] = [P[maxIndex], P[i]];
[L[i], L[maxIndex]] = [L[maxIndex], L[i]]; // Swap rows of L to maintain consistency
}
// Decomposition
for (let j = i; j < n; j++) {
let sum = 0;
for (let k = 0; k < i; k++) {
sum += L[i][k] * U[k][j];
}
U[i][j] = A[i][j] - sum;
}
for (let j = i + 1; j < n; j++) {
let sum = 0;
for (let k = 0; k < i; k++) {
sum += L[j][k] * U[k][i];
}
L[j][i] = (A[j][i] - sum) / U[i][i];
}
L[i][i] = 1;
}
return [L, U, P];
}
// Example Tests:
const result1 = luDecomposition([[1, 3, 5], [2, 4, 7], [1, 1, 0]]);
console.log('L:', result1[0]);
console.log('U:', result1[1]);
console.log('P:', result1[2]);
const result2 = luDecomposition([[11, 9, 24, 2], [1, 5, 2, 6], [3, 17, 18, 1], [2, 5, 7, 1]]);
console.log('L:', result2[0]);
console.log('U:', result2[1]);
console.log('P:', result2[2]);
const result3 = luDecomposition([[1, 1, 1], [4, 3, -1], [3, 5, 3]]);
console.log('L:', result3[0]);
console.log('U:', result3[1]);
console.log('P:', result3[2]);
const result4 = luDecomposition([[1, -2, 3], [2, -5, 12], [0, 2, -10]]);
console.log('L:', result4[0]);
console.log('U:', result4[1]);
console.log('P:', result4[2]);
Your browser information:
User Agent is: Mozilla/5.0 (Windows NT 10.0; Win64; x64) AppleWebKit/537.36 (KHTML, like Gecko) Chrome/131.0.0.0 Safari/537.36 Edg/131.0.0.0
Challenge Information:
Rosetta Code Challenges - LU decomposition