Lu decomposition, JavaScript code

Source: Internet
Author: User

A is a matrix, written here as a one-dimensional array, such as "1", "1,2,3,4" function Getlu (a) {var n = a.length;//Matrix total data number var s = math.sqrt (n);//The Order of the matrix var L = new Arra Y (n), var U = new Array (n), if (Getdet (a)! = 0) {var Allordernotequleszero = true;for (var i = 0; i < s; i++) {if (Getdet (G        Etordermatrix (A, i)) = = 0) {Allordernotequleszero = False;break;}} if (Allordernotequleszero) {for (var i = 0, i < s; i++) {for (var j = 0; J < S; j + +) {if (i = = j) {l[i*s + j] = 1;}            else if (I < j) {L[i*s+j] = 0;}            if (i > J) u[i*s+j] = 0;            U[0*S+J] = A[0*s+j]; L[I*S+0] = a[i*s+0]/u[0*s+0];}}            for (var k = 1, k < S; k++) {for (var j = k; J < S; j + +) {tmp = 0;            for (var m = 0; m < K; m++) {tmp + = l[k*s+m] * U[m*s+j];        } U[k*s+j] = a[k*s+j]-tmp;            } for (var i = k+1; i < S; i++) {tmp = 0;           for (var m = 0; m < K; m++) {     TMP + = l[i*s+m] * U[m*s+k];        } L[i*s+k] = (a[i*s+k]-tmp)/u[k*s+k]; }}}else {alert (' The matrix has 0 in the order of the master style!  ') Console.log (Getlastmatrix (a)), for (var i = 0, i < s; i++) {for (var j = 0; J < S; j + +) {if (J > i) {u[i * s + j] = A[i * s + j]; L[i * s + j] = 0;} else if (j = = i) {U[i * s + j] = A[i * s + j]; L[i * s + j] = 1;} Else{l[i * s + j] = A[i * s + j]; U[i * s + j] = 0;}}} Console.log (L); Console.log (U);} Else{alert (' The matrix is a singular matrix and cannot be decomposed by Lu! ‘);}} function Getdet (a) {var n = total data of a.length;//matrix var s = math.sqrt (n);//The Order of the matrix var sum = 0;if (n = = 1) {return a[0];} for (var i = 0; i < s; i++) {if (i 2 = = 0) {sum + = a[i] * Getdet (Getsubmatrix (A, i))}else{sum-= a[i] * Getdet (GETSUBMA Trix (A, i))}}return sum;}  Gets the number of matrix function Getlastmatrix (a) {var n = a.length;//Matrix after the nth column is compared var s = math.sqrt (n);//The Order of the matrix var p = [];for (var i = 0; I < S; i++) {for (var j = 0; J < S; j + +) {if (i = = j) {P.push (1);} else {p.push (0);}}} for (var i = 0; i < s-1; i++) {a = Getnewmatrix (A, I, p); }console.log (P); return A;} function Getnewmatrix (A, K, p) {var n = Total data number of a.length;//matrix var s = math.sqrt (n);//order of matrix var result = [];var Maxrownum = 0;va R MAXValue = Math.Abs (a[k * s + K]); for (var i = 1; i < S; i++) {if (i >= k && math.abs (A[i * s + K]) > MAXValue) {maxrownum = i; MAXValue = Math.Abs (A[i * s + K]);}} Console.log (' maxrownum: ' + maxrownum);//console.log (' MAXValue: ' + MAXValue '); if (maxrownum! = k) {for (var i = 0; I < s ; i++) {var temp = A[k * s + i];a[k * s + i] = A[maxrownum * s + i]a[maxrownum * s + i] = Temp;var TMP1 = p[k * s + i];p [k * S + i] = P[maxrownum * s + i]p[maxrownum * s + i] = TMP1;} Console.log (a);//console.log (P); if (MAXValue! = 0) {for (var i = k + 1; i < S; i++) {if (A[i * s + K]! = 0) {A[i * s + k ] = parsefloat (A[i * s + K]/a[k * s + K]) for (var j = k + 1; j< s; j + +) {A[i * s + j] = parsefloat (A[i * s + j]-A[i * s + K] * A[k * s + j])}}}}}else{if (MAXValue! = 0) {for (var i = k + 1; i < S; i++) {if (A[i * s + K]! = 0) {a[I * s + K] = parsefloat (A[i * s + K]/a[k * s + k)) for (var j = k + 1; j< s; j + +) {A[i * s + j] = parsefloat (A[K * s + J]-A[i * s + K] * A[i * s + j])}}}}}//console.log (a); return A; A is the original matrix, K is the number of columns in the function Getsubmatrix (A, k) {var n = Total data of the a.length;//matrix var s = math.sqrt (n);//The Order of the Matrix var = []; for ( var i = 1; I < S; i++) {for (var j = 0; J < S; j + +) {if (k! = j) {Result.push (a[i*s +j]);}} return result;} Get the Order Master matrix, K for order function Getordermatrix (A, k) {var n = a.length;//Matrix total data number var s = math.sqrt (n);//The Order of the Matrix var = []; f or (var i = 0; i < s; i++) {for (var j = 0; J < S; j + +) {if (I <= k && J <= k) {Result.push (a[i*s +j]);} }}return result;}

  

Lu decomposition, JavaScript code

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.