Facebook
From cjoyales, 3 Years ago, written in JavaScript.
This paste is a reply to crout's method javascript from cjoyales - view diff
Embed
Download Paste or View Raw
Hits: 179
  1. startCroutMethod = (A, B) => {
  2.   // matrix A
  3.   let matrixA = A;
  4.   let matrixB = B;
  5.  
  6.   // Generate L and U using matrix A length
  7.   let matrixL = Array(matrixA.length)
  8.     .fill()
  9.     .map(() => Array(matrixA.length).fill(0));
  10.   let matrixU = Array(matrixA.length)
  11.     .fill()
  12.     .map(() => Array(matrixA.length).fill(0));
  13.   let Y = Array(matrixA.length).fill(0);
  14.   let X = Array(matrixA.length).fill(0);
  15.   // .map(() => Array(3));
  16.  
  17.   crout = () => {
  18.     // assign matrix A length to a variable
  19.     let n = matrixA.length;
  20.  
  21.     // loop in to index
  22.     for (i = 0; i < n; i++) {
  23.       // solve for L
  24.       for (j = 0; j < n; j++) {
  25.         // if j is less than i change value of matrixL to 0 with its corresponding indexes
  26.         if (j < i) matrixL[j][i] = 0;
  27.         else {
  28.           // the first column of matrix L is equal to the first column of matrix A
  29.           matrixL[j][i] = matrixA[j][i];
  30.  
  31.           //  loop the values excluding the first column and top row
  32.           for (k = 0; k < i; k++) {
  33.             matrixL[j][i] = matrixL[j][i] - matrixL[j][k] * matrixU[k][i];
  34.           }
  35.         }
  36.       }
  37.       // loop in to index to solve for U
  38.       for (j = 0; j < n; j++) {
  39.         // if j is less than i change value of martixU to 0 with its corresponding indexes
  40.         if (j < i) matrixU[i][j] = 0;
  41.         // if j is equal to i the change value to 1
  42.         //  result to a diagonal values of 1
  43.         else if (j == i) matrixU[i][j] = 1;
  44.         else {
  45.           // get index of the first rows of matrixU
  46.           matrixU[i][j] = matrixA[i][j] / matrixL[i][i];
  47.  
  48.           // loop to get values excluding the first rows, 1's and 0's
  49.           for (k = 0; k < i; k++) {
  50.             matrixU[i][j] =
  51.               matrixU[i][j] - (matrixL[i][k] * matrixU[k][j]) / matrixL[i][i];
  52.           }
  53.         }
  54.       }
  55.     }
  56.  
  57.     //forward subtitution method
  58.     // let Ly = B
  59.     for (let i = 0; i < n; i++) {
  60.       let sum = 0;
  61.       for (let j = 0; j <= i; j++) {
  62.         // substitue values
  63.         sum += matrixL[i][j] * Y[j];
  64.       }
  65.  
  66.       // solve for Y
  67.       // store answer to array
  68.       Y[i] = (matrixB[i] - sum) / matrixL[i][i];
  69.  
  70.       // round to 2 decimal place
  71.       // NOTE: floating points in javascript have accuracy problems when rounding off
  72.       Y[i] = Math.round((Y[i] + Number.EPSILON) * 100) / 100;
  73.     }
  74.  
  75.     // let Ux = y
  76.     for (let i = n - 1; i >= 0; i--) {
  77.       let sum = 0;
  78.  
  79.       for (let p = n - 1; p >= i; p--) {
  80.         // substitue values
  81.         sum += matrixU[i][p] * X[p];
  82.       }
  83.  
  84.       // solve for X
  85.       // store answer to array
  86.       X[i] = (Y[i] - sum) / matrixU[i][i];
  87.  
  88.       // round to 2 decimal place
  89.       X[i] = Math.round((X[i] + Number.EPSILON) * 100) / 100;
  90.     }
  91.  
  92.     //  print in console
  93.     console.log(
  94.       "matrixA:\n",
  95.       matrixA,
  96.       "\n\nmatrixL:\n",
  97.       matrixL,
  98.       "\n\nmatrixU:\n",
  99.       matrixU,
  100.       "\n\nmatrixB:\n",
  101.       matrixB,
  102.       "\n\n-------- Using Forward Substitution ----------",
  103.       "\n\nY:\n",
  104.       Y,
  105.       "\n\nX:\n",
  106.       X
  107.     );
  108.   };
  109.   crout();
  110. };
  111.  
  112. /**
  113.  *
  114.  * startCroutMethod(matrix A, matrix B)
  115.  * startCroutMethod takes 2 parameters
  116.  * first parameters is matrix A
  117.  * and the second parameter is matrix B
  118.  *
  119.  * sample:
  120.  * startCroutMethod([
  121.  * [5, 4, 1],
  122.  * [10, 9, 4],
  123.  * [10, 13, 15],
  124.  * ],
  125.  * [3.4, 8.8, 19.2]
  126.  * );
  127.  *
  128.  *
  129.  * to run in node cli
  130.  * node -e "require('./index').init(matrixA,matrixB)"
  131.  * node -e "require('./index').init([[5,4,1],[10,9,4],[10,13,15]],[3.4,8.8,19.2])"
  132.  *
  133.  *
  134.  */
  135.  
  136. module.exports.init = function (a, b) {
  137.   startCroutMethod(a, b);
  138. };
  139.  

Replies to Re: crout's method javascript rss

Title Name Language When
Re: Re: crout's method javascript cjoyales javascript 3 Years ago.