--- title: Gaussian elimination id: 5a23c84252665b21eecc7e77 challengeType: 5 --- ## Description
Write a function to solve \(A.x = b\) using Gaussian elimination then backwards substitution. \(A\) being an \(n \times n\) matrix. Also, \(x\) and \(b\) are \(n\) by 1 vectors. To improve accuracy, please use partial pivoting and scaling.
## Instructions
## Tests
```yml tests: - text: gaussianElimination should be a function. testString: 'assert(typeof gaussianElimination=="function","gaussianElimination should be a function.");' - text: gaussianElimination("+JSON.stringify(tests[0][0])+","+JSON.stringify(tests[0][1])+") should return an array. testString: 'assert(Array.isArray(gaussianElimination(tests[0][0],tests[0][1])),"gaussianElimination("+JSON.stringify(tests[0][0])+","+JSON.stringify(tests[0][1])+") should return an array.");' - text: gaussianElimination("+JSON.stringify(tests[0][0])+","+JSON.stringify(tests[0][1])+") should return "+JSON.stringify(results[0])+". testString: 'assert.deepEqual(gaussianElimination(tests[0][0],tests[0][1]),results[0],"gaussianElimination("+JSON.stringify(tests[0][0])+","+JSON.stringify(tests[0][1])+") should return "+JSON.stringify(results[0])+".");' - text: gaussianElimination("+JSON.stringify(tests[1][0])+","+JSON.stringify(tests[1][1])+") should return "+JSON.stringify(results[1])+". testString: 'assert.deepEqual(gaussianElimination(tests[1][0],tests[1][1]),results[1],"gaussianElimination("+JSON.stringify(tests[1][0])+","+JSON.stringify(tests[1][1])+") should return "+JSON.stringify(results[1])+".");' - text: gaussianElimination("+JSON.stringify(tests[2][0])+","+JSON.stringify(tests[2][1])+") should return "+JSON.stringify(results[2])+". testString: 'assert.deepEqual(gaussianElimination(tests[2][0],tests[2][1]),results[2],"gaussianElimination("+JSON.stringify(tests[2][0])+","+JSON.stringify(tests[2][1])+") should return "+JSON.stringify(results[2])+".");' - text: gaussianElimination("+JSON.stringify(tests[3][0])+","+JSON.stringify(tests[3][1])+") should return "+JSON.stringify(results[3])+". testString: 'assert.deepEqual(gaussianElimination(tests[3][0],tests[3][1]),results[3],"gaussianElimination("+JSON.stringify(tests[3][0])+","+JSON.stringify(tests[3][1])+") should return "+JSON.stringify(results[3])+".");' - text: gaussianElimination("+JSON.stringify(tests[4][0])+","+JSON.stringify(tests[4][1])+") should return "+JSON.stringify(results[4])+". testString: 'assert.deepEqual(gaussianElimination(tests[4][0],tests[4][1]),results[4],"gaussianElimination("+JSON.stringify(tests[4][0])+","+JSON.stringify(tests[4][1])+") should return "+JSON.stringify(results[4])+".");' ```
## Challenge Seed
```js function gaussianElimination (A,b) { // Good luck! } ```
### After Test
```js console.info('after the test'); ```
## Solution
```js function gaussianElimination(A, b) { // Lower Upper Decomposition function ludcmp(A) { // A is a matrix that we want to decompose into Lower and Upper matrices. var d = true var n = A.length var idx = new Array(n) // Output vector with row permutations from partial pivoting var vv = new Array(n) // Scaling information for (var i=0; i max) max = temp } if (max == 0) return // Singular Matrix! vv[i] = 1 / max // Scaling } var Acpy = new Array(n) for (var i=0; i= max) { max = temp jmax = j } } if (i <= jmax) { for (var j=0; j -1) for (var j=ii; j=0; i--) { var sum = b[i] for (var j=i+1; j