sans_amour
Thống kê
- Nhóm: Thành viên
- Bài viết: 5
- Lượt xem: 1708
- Danh hiệu: Lính mới
- Tuổi: 37 tuổi
- Ngày sinh: Tháng bảy 17, 1986
-
Giới tính
Bí mật
-
Đến từ
Hà Tĩnh
- Website URL http://tvphp.net
Trong chủ đề: GraphCalc 4.0.1
06-10-2007 - 15:07
Merci Magus ;
Trong chủ đề: Nghịch đảo ma trận bằng các ngôn ngữ
24-08-2007 - 01:00
lu.bas:
lu.cs
lu.cpp
Anh em cố mà nghiên cứu nhá :">
'''''''''''''''''''''''''''''''''''''''''''''''''''''''''' 'This code is generated by the AlgoPascal translator ' 'This code is distributed under the ALGLIB license ' (see http://www.alglib.net/copyrules.php for details) '''''''''''''''''''''''''''''''''''''''''''''''''''''''''' 'Routines '''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''' 'LU decomposition of a general matrix of size MxN ' 'The subroutine calculates the LU decomposition of a rectangular general 'matrix with partial pivoting (with row permutations). ' 'Input parameters: ' A - matrix A whose indexes range within [1..M, 1..N]. ' M - number of rows in matrix A. ' N - number of columns in matrix A. ' 'Output parameters: ' A - matrices L and U in compact form (see below). ' Array whose indexes range within [1..M, 1..N]. ' Pivots - permutation matrix in compact form (see below). ' Array whose index ranges within [1..Min(M,N)]. ' 'Matrix A is represented as A = P * L * U, where P is a permutation matrix, 'matrix L - lower triangular (or lower trapezoid, if M>N) matrix, 'U - upper triangular (or upper trapezoid, if M<N) matrix. ' 'Let M be equal to 4 and N be equal to 3: ' ' ( 1 ) ( U11 U12 U13 ) 'A = P1 * P2 * P3 * ( L21 1 ) * ( U22 U23 ) ' ( L31 L32 1 ) ( U33 ) ' ( L41 L42 L43 ) ' 'Matrix L has size MxMin(M,N), matrix U has size Min(M,N)xN, matrix P(i) is 'a permutation of the identity matrix of size MxM with numbers I and Pivots[I]. ' 'The algorithm returns array Pivots and the following matrix which replaces 'matrix A and contains matrices L and U in compact form (the example applies 'to M=4, N=3). ' ' ( U11 U12 U13 ) ' ( L21 U22 U23 ) ' ( L31 L32 U33 ) ' ( L41 L42 L43 ) ' 'As we can see, the unit diagonal isn't stored. ' ' -- LAPACK routine (version 3.0) -- ' Univ. of Tennessee, Univ. of California Berkeley, NAG Ltd., ' Courant Institute, Argonne National Lab, and Rice University ' June 30, 1992 ' '''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''' Public Sub LUDecomposition(ByRef A() As Double, _ ByVal M As Long, _ ByVal N As Long, _ ByRef Pivots() As Long) Dim I As Long Dim J As Long Dim JP As Long Dim T1() As Double Dim s As Double Dim i_ As Long ReDim Pivots(1# To MinInt(M, N)) ReDim T1(1# To MaxInt(M, N)) ' ' Quick return if possible ' If M=0# Or N=0# then Exit Sub End If For J=1# To MinInt(M, N) Step 1 ' ' Find pivot and test for singularity. ' JP = J For I=J+1# To M Step 1 If Abs(A(I,J))>Abs(A(JP,J)) then JP = I End If Next I Pivots(J) = JP If A(JP,J)<>0# then ' 'Apply the interchange to rows ' If JP<>J then For i_ = 1# To N Step 1 T1(i_) = A(J,i_) Next i_ For i_ = 1# To N Step 1 A(J,i_) = A(JP,i_) Next i_ For i_ = 1# To N Step 1 A(JP,i_) = T1(i_) Next i_ End If ' 'Compute elements J+1:M of J-th column. ' If J<M then ' ' CALL DSCAL( M-J, ONE / A( J, J ), A( J+1, J ), 1 ) ' JP = J+1# S = 1#/A(J,J) For i_ = JP To M Step 1 A(i_,J) = S*A(i_,J) Next i_ End If End If If J<MinInt(M, N) then ' 'Update trailing submatrix. 'CALL DGER( M-J, N-J, -ONE, A( J+1, J ), 1, A( J, J+1 ), LDA,A( J+1, J+1 ), LDA ) ' JP = J+1# For I=J+1# To M Step 1 S = A(I,J) For i_ = JP To N Step 1 A(I,i_) = A(I,i_) - S*A(J,i_) Next i_ Next I End If Next J End Sub '''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''' 'LU decomposition of a general matrix of size MxN ' 'It uses LUDecomposition. L and U are not output in compact form, but as 'separate general matrices filled up by zero elements in their 'corresponding positions. ' 'This subroutine described here only serves the purpose to show 'how the result of ComplexLUDecomposition subroutine could be unpacked. ' ' -- ALGLIB -- ' Copyright 2005 by Bochkanov Sergey ' '''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''' Public Sub LUDecompositionUnpacked(ByRef A_() As Double, _ ByVal M As Long, _ ByVal N As Long, _ ByRef L() As Double, _ ByRef U() As Double, _ ByRef Pivots() As Long) Dim A() As Double Dim I As Long Dim J As Long Dim MinMN As Long A = A_ If M=0# Or N=0# then Exit Sub End If MinMN = MinInt(M, N) ReDim L(1# To M, 1# To MinMN) ReDim U(1# To MinMN, 1# To N) Call LUDecomposition(A, M, N, Pivots) For I=1# To M Step 1 For J=1# To MinMN Step 1 If J>I then L(I,J) = 0# End If If J=I then L(I,J) = 1# End If If J<I then L(I,J) = A(I,J) End If Next J Next I For I=1# To MinMN Step 1 For J=1# To N Step 1 If J<I then U(I,J) = 0# End If If J>=I then U(I,J) = A(I,J) End If Next J Next I End Sub
lu.cs
/*********************************************************************** This code is generated by the AlgoPascal translator This code is distributed under the ALGLIB license (see http://www.alglib.net/copyrules.php for details) ***********************************************************************/ /************************************************************************* LU decomposition of a general matrix of size MxN The subroutine calculates the LU decomposition of a rectangular general matrix with partial pivoting (with row permutations). Input parameters: A - matrix A whose indexes range within [1..M, 1..N]. M - number of rows in matrix A. N - number of columns in matrix A. Output parameters: A - matrices L and U in compact form (see below). Array whose indexes range within [1..M, 1..N]. Pivots - permutation matrix in compact form (see below). Array whose index ranges within [1..Min(M,N)]. Matrix A is represented as A = P * L * U, where P is a permutation matrix, matrix L - lower triangular (or lower trapezoid, if M>N) matrix, U - upper triangular (or upper trapezoid, if M<N) matrix. Let M be equal to 4 and N be equal to 3: ( 1 ) ( U11 U12 U13 ) A = P1 * P2 * P3 * ( L21 1 ) * ( U22 U23 ) ( L31 L32 1 ) ( U33 ) ( L41 L42 L43 ) Matrix L has size MxMin(M,N), matrix U has size Min(M,N)xN, matrix P(i) is a permutation of the identity matrix of size MxM with numbers I and Pivots[I]. The algorithm returns array Pivots and the following matrix which replaces matrix A and contains matrices L and U in compact form (the example applies to M=4, N=3). ( U11 U12 U13 ) ( L21 U22 U23 ) ( L31 L32 U33 ) ( L41 L42 L43 ) As we can see, the unit diagonal isn't stored. -- LAPACK routine (version 3.0) -- Univ. of Tennessee, Univ. of California Berkeley, NAG Ltd., Courant Institute, Argonne National Lab, and Rice University June 30, 1992 *************************************************************************/ public static void ludecomposition(ref double[,] a, int m, int n, ref int[] pivots) { int i = 0; int j = 0; int jp = 0; double[] t1 = new double[0]; double s = 0; int i_ = 0; pivots = new int[Math.Min(m, n)+1]; t1 = new double[Math.Max(m, n)+1]; System.Diagnostics.Debug.Assert(m>=0 & n>=0, "Error in LUDecomposition: incorrect function arguments"); // // Quick return if possible // if( m==0 | n==0 ) { return; } for(j=1; j<=Math.Min(m, n); j++) { // // Find pivot and test for singularity. // jp = j; for(i=j+1; i<=m; i++) { if( Math.Abs(a[i,j])>Math.Abs(a[jp,j]) ) { jp = i; } } pivots[j] = jp; if( a[jp,j]!=0 ) { // //Apply the interchange to rows // if( jp!=j ) { for(i_=1; i_<=n;i_++) { t1[i_] = a[j,i_]; } for(i_=1; i_<=n;i_++) { a[j,i_] = a[jp,i_]; } for(i_=1; i_<=n;i_++) { a[jp,i_] = t1[i_]; } } // //Compute elements J+1:M of J-th column. // if( j<m ) { // // CALL DSCAL( M-J, ONE / A( J, J ), A( J+1, J ), 1 ) // jp = j+1; s = 1/a[j,j]; for(i_=jp; i_<=m;i_++) { a[i_,j] = s*a[i_,j]; } } } if( j<Math.Min(m, n) ) { // //Update trailing submatrix. //CALL DGER( M-J, N-J, -ONE, A( J+1, J ), 1, A( J, J+1 ), LDA,A( J+1, J+1 ), LDA ) // jp = j+1; for(i=j+1; i<=m; i++) { s = a[i,j]; for(i_=jp; i_<=n;i_++) { a[i,i_] = a[i,i_] - s*a[j,i_]; } } } } } /************************************************************************* LU decomposition of a general matrix of size MxN It uses LUDecomposition. L and U are not output in compact form, but as separate general matrices filled up by zero elements in their corresponding positions. This subroutine described here only serves the purpose to show how the result of ComplexLUDecomposition subroutine could be unpacked. -- ALGLIB -- Copyright 2005 by Bochkanov Sergey *************************************************************************/ public static void ludecompositionunpacked(double[,] a, int m, int n, ref double[,] l, ref double[,] u, ref int[] pivots) { int i = 0; int j = 0; int minmn = 0; a = (double[,])a.Clone(); if( m==0 | n==0 ) { return; } minmn = Math.Min(m, n); l = new double[m+1, minmn+1]; u = new double[minmn+1, n+1]; ludecomposition(ref a, m, n, ref pivots); for(i=1; i<=m; i++) { for(j=1; j<=minmn; j++) { if( j>i ) { l[i,j] = 0; } if( j==i ) { l[i,j] = 1; } if( j<i ) { l[i,j] = a[i,j]; } } } for(i=1; i<=minmn; i++) { for(j=1; j<=n; j++) { if( j<i ) { u[i,j] = 0; } if( j>=i ) { u[i,j] = a[i,j]; } } } }
lu.cpp
/*********************************************************************** This code is generated by the AlgoPascal translator This code is distributed under the ALGLIB license (see http://www.alglib.net/copyrules.php for details) ***********************************************************************/ #include "ap.h" void ludecomposition(ap::real_2d_array& a, int m, int n, ap::integer_1d_array& pivots); void ludecompositionunpacked(ap::real_2d_array a, int m, int n, ap::real_2d_array& l, ap::real_2d_array& u, ap::integer_1d_array& pivots); /************************************************************************* LU decomposition of a general matrix of size MxN The subroutine calculates the LU decomposition of a rectangular general matrix with partial pivoting (with row permutations). Input parameters: A - matrix A whose indexes range within [1..M, 1..N]. M - number of rows in matrix A. N - number of columns in matrix A. Output parameters: A - matrices L and U in compact form (see below). Array whose indexes range within [1..M, 1..N]. Pivots - permutation matrix in compact form (see below). Array whose index ranges within [1..Min(M,N)]. Matrix A is represented as A = P * L * U, where P is a permutation matrix, matrix L - lower triangular (or lower trapezoid, if M>N) matrix, U - upper triangular (or upper trapezoid, if M<N) matrix. Let M be equal to 4 and N be equal to 3: ( 1 ) ( U11 U12 U13 ) A = P1 * P2 * P3 * ( L21 1 ) * ( U22 U23 ) ( L31 L32 1 ) ( U33 ) ( L41 L42 L43 ) Matrix L has size MxMin(M,N), matrix U has size Min(M,N)xN, matrix P(i) is a permutation of the identity matrix of size MxM with numbers I and Pivots[I]. The algorithm returns array Pivots and the following matrix which replaces matrix A and contains matrices L and U in compact form (the example applies to M=4, N=3). ( U11 U12 U13 ) ( L21 U22 U23 ) ( L31 L32 U33 ) ( L41 L42 L43 ) As we can see, the unit diagonal isn't stored. -- LAPACK routine (version 3.0) -- Univ. of Tennessee, Univ. of California Berkeley, NAG Ltd., Courant Institute, Argonne National Lab, and Rice University June 30, 1992 *************************************************************************/ void ludecomposition(ap::real_2d_array& a, int m, int n, ap::integer_1d_array& pivots) { int i; int j; int jp; ap::real_1d_array t1; double s; pivots.setbounds(1, ap::minint(m, n)); t1.setbounds(1, ap::maxint(m, n)); ap::ap_error::make_assertion(m>=0&&n>=0); // // Quick return if possible // if( m==0||n==0 ) { return; } for(j = 1; j <= ap::minint(m, n); j++) { // // Find pivot and test for singularity. // jp = j; for(i = j+1; i <= m; i++) { if( fabs(a(i,j))>fabs(a(jp,j)) ) { jp = i; } } pivots(j) = jp; if( a(jp,j)!=0 ) { // //Apply the interchange to rows // if( jp!=j ) { ap::vmove(t1.getvector(1, n), a.getrow(j, 1, n)); ap::vmove(a.getrow(j, 1, n), a.getrow(jp, 1, n)); ap::vmove(a.getrow(jp, 1, n), t1.getvector(1, n)); } // //Compute elements J+1:M of J-th column. // if( j<m ) { // // CALL DSCAL( M-J, ONE / A( J, J ), A( J+1, J ), 1 ) // jp = j+1; s = 1/a(j,j); ap::vmul(a.getcolumn(j, jp, m), s); } } if( j<ap::minint(m, n) ) { // //Update trailing submatrix. //CALL DGER( M-J, N-J, -ONE, A( J+1, J ), 1, A( J, J+1 ), LDA,A( J+1, J+1 ), LDA ) // jp = j+1; for(i = j+1; i <= m; i++) { s = a(i,j); ap::vsub(a.getrow(i, jp, n), a.getrow(j, jp, n), s); } } } } /************************************************************************* LU decomposition of a general matrix of size MxN It uses LUDecomposition. L and U are not output in compact form, but as separate general matrices filled up by zero elements in their corresponding positions. This subroutine described here only serves the purpose to show how the result of ComplexLUDecomposition subroutine could be unpacked. -- ALGLIB -- Copyright 2005 by Bochkanov Sergey *************************************************************************/ void ludecompositionunpacked(ap::real_2d_array a, int m, int n, ap::real_2d_array& l, ap::real_2d_array& u, ap::integer_1d_array& pivots) { int i; int j; int minmn; if( m==0||n==0 ) { return; } minmn = ap::minint(m, n); l.setbounds(1, m, 1, minmn); u.setbounds(1, minmn, 1, n); ludecomposition(a, m, n, pivots); for(i = 1; i <= m; i++) { for(j = 1; j <= minmn; j++) { if( j>i ) { l(i,j) = 0; } if( j==i ) { l(i,j) = 1; } if( j<i ) { l(i,j) = a(i,j); } } } for(i = 1; i <= minmn; i++) { for(j = 1; j <= n; j++) { if( j<i ) { u(i,j) = 0; } if( j>=i ) { u(i,j) = a(i,j); } } } }
Anh em cố mà nghiên cứu nhá :">
Trong chủ đề: Nghịch đảo ma trận bằng các ngôn ngữ
24-08-2007 - 00:57
invtriangular.bas
invtriangular.cs
invtriangular.cpp
'''''''''''''''''''''''''''''''''''''''''''''''''''''''''' 'This code is generated by the AlgoPascal translator ' 'This code is distributed under the ALGLIB license ' (see http://www.alglib.net/copyrules.php for details) '''''''''''''''''''''''''''''''''''''''''''''''''''''''''' 'Routines '''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''' 'Triangular matrix inversion ' 'The subroutine inverts the following types of matrices: ' * upper triangular ' * upper triangular with unit diagonal ' * lower triangular ' * lower triangular with unit diagonal ' 'In case of an upper (lower) triangular matrix, the inverse matrix will 'also be upper (lower) triangular, and after the end of the algorithm, the 'inverse matrix replaces the source matrix. The elements below (above) the 'main diagonal are not changed by the algorithm. ' 'If the matrix has a unit diagonal, the inverse matrix also has a unit 'diagonal, and the diagonal elements are not passed to the algorithm. ' 'Input parameters: ' A - matrix. ' Array whose indexes range within [1..N, 1..N]. ' N - size of matrix A. ' IsUpper - True, if the matrix is upper triangular. ' IsUnitTriangular ' - True, if the matrix has a unit diagonal. ' 'Output parameters: ' A - inverse matrix (if the problem is not degenerate). ' 'Result: ' True, if the matrix is not singular. ' False, if the matrix is singular. ' ' -- LAPACK routine (version 3.0) -- ' Univ. of Tennessee, Univ. of California Berkeley, NAG Ltd., ' Courant Institute, Argonne National Lab, and Rice University ' February 29, 1992 ' '''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''' Public Function InvTriangular(ByRef A() As Double, _ ByVal N As Long, _ ByVal IsUpper As Boolean, _ ByVal IsUnitTriangular As Boolean) As Boolean Dim Result As Boolean Dim NOUNIT As Boolean Dim I As Long Dim J As Long Dim NMJ As Long Dim JM1 As Long Dim JP1 As Long Dim V As Double Dim AJJ As Double Dim T() As Double Dim i_ As Long Result = True ReDim T(1# To N) ' ' Test the input parameters. ' NOUNIT = Not IsUnitTriangular If IsUpper then ' ' Compute inverse of upper triangular matrix. ' For J=1# To N Step 1 If NOUNIT then If A(J,J)=0# then Result = False InvTriangular = Result Exit Function End If A(J,J) = 1#/A(J,J) AJJ = -A(J,J) Else AJJ = -1# End If ' ' Compute elements 1:j-1 of j-th column. ' If J>1# then JM1 = J-1# For i_ = 1# To JM1 Step 1 T(i_) = A(i_,J) Next i_ For I=1# To J-1# Step 1 If I<J-1# then V = 0.0 For i_ = I+1# To JM1 Step 1 V = V + A(I,i_)*T(i_) Next i_ Else V = 0# End If If NOUNIT then A(I,J) = V+A(I,I)*T(I) Else A(I,J) = V+T(I) End If Next I For i_ = 1# To JM1 Step 1 A(i_,J) = AJJ*A(i_,J) Next i_ End If Next J Else ' ' Compute inverse of lower triangular matrix. ' For J=N To 1# Step -1 If NOUNIT then If A(J,J)=0# then Result = False InvTriangular = Result Exit Function End If A(J,J) = 1#/A(J,J) AJJ = -A(J,J) Else AJJ = -1# End If If J<N then ' ' Compute elements j+1:n of j-th column. ' NMJ = N-J JP1 = J+1# For i_ = JP1 To N Step 1 T(i_) = A(i_,J) Next i_ For I=J+1# To N Step 1 If I>J+1# then V = 0.0 For i_ = JP1 To I-1# Step 1 V = V + A(I,i_)*T(i_) Next i_ Else V = 0# End If If NOUNIT then A(I,J) = V+A(I,I)*T(I) Else A(I,J) = V+T(I) End If Next I For i_ = JP1 To N Step 1 A(i_,J) = AJJ*A(i_,J) Next i_ End If Next J End If InvTriangular = Result End Function
invtriangular.cs
/*********************************************************************** This code is generated by the AlgoPascal translator This code is distributed under the ALGLIB license (see http://www.alglib.net/copyrules.php for details) ***********************************************************************/ /************************************************************************* Triangular matrix inversion The subroutine inverts the following types of matrices: * upper triangular * upper triangular with unit diagonal * lower triangular * lower triangular with unit diagonal In case of an upper (lower) triangular matrix, the inverse matrix will also be upper (lower) triangular, and after the end of the algorithm, the inverse matrix replaces the source matrix. The elements below (above) the main diagonal are not changed by the algorithm. If the matrix has a unit diagonal, the inverse matrix also has a unit diagonal, and the diagonal elements are not passed to the algorithm. Input parameters: A - matrix. Array whose indexes range within [1..N, 1..N]. N - size of matrix A. IsUpper - True, if the matrix is upper triangular. IsUnitTriangular - True, if the matrix has a unit diagonal. Output parameters: A - inverse matrix (if the problem is not degenerate). Result: True, if the matrix is not singular. False, if the matrix is singular. -- LAPACK routine (version 3.0) -- Univ. of Tennessee, Univ. of California Berkeley, NAG Ltd., Courant Institute, Argonne National Lab, and Rice University February 29, 1992 *************************************************************************/ public static bool invtriangular(ref double[,] a, int n, bool isupper, bool isunittriangular) { bool result = new bool(); bool nounit = new bool(); int i = 0; int j = 0; int nmj = 0; int jm1 = 0; int jp1 = 0; double v = 0; double ajj = 0; double[] t = new double[0]; int i_ = 0; result = true; t = new double[n+1]; // // Test the input parameters. // nounit = !isunittriangular; if( isupper ) { // // Compute inverse of upper triangular matrix. // for(j=1; j<=n; j++) { if( nounit ) { if( a[j,j]==0 ) { result = false; return result; } a[j,j] = 1/a[j,j]; ajj = -a[j,j]; } else { ajj = -1; } // // Compute elements 1:j-1 of j-th column. // if( j>1 ) { jm1 = j-1; for(i_=1; i_<=jm1;i_++) { t[i_] = a[i_,j]; } for(i=1; i<=j-1; i++) { if( i<j-1 ) { v = 0.0; for(i_=i+1; i_<=jm1;i_++) { v += a[i,i_]*t[i_]; } } else { v = 0; } if( nounit ) { a[i,j] = v+a[i,i]*t[i]; } else { a[i,j] = v+t[i]; } } for(i_=1; i_<=jm1;i_++) { a[i_,j] = ajj*a[i_,j]; } } } } else { // // Compute inverse of lower triangular matrix. // for(j=n; j>=1; j--) { if( nounit ) { if( a[j,j]==0 ) { result = false; return result; } a[j,j] = 1/a[j,j]; ajj = -a[j,j]; } else { ajj = -1; } if( j<n ) { // // Compute elements j+1:n of j-th column. // nmj = n-j; jp1 = j+1; for(i_=jp1; i_<=n;i_++) { t[i_] = a[i_,j]; } for(i=j+1; i<=n; i++) { if( i>j+1 ) { v = 0.0; for(i_=jp1; i_<=i-1;i_++) { v += a[i,i_]*t[i_]; } } else { v = 0; } if( nounit ) { a[i,j] = v+a[i,i]*t[i]; } else { a[i,j] = v+t[i]; } } for(i_=jp1; i_<=n;i_++) { a[i_,j] = ajj*a[i_,j]; } } } } return result; }
invtriangular.cpp
/*********************************************************************** This code is generated by the AlgoPascal translator This code is distributed under the ALGLIB license (see http://www.alglib.net/copyrules.php for details) ***********************************************************************/ #include "ap.h" bool invtriangular(ap::real_2d_array& a, int n, bool isupper, bool isunittriangular); /************************************************************************* Triangular matrix inversion The subroutine inverts the following types of matrices: * upper triangular * upper triangular with unit diagonal * lower triangular * lower triangular with unit diagonal In case of an upper (lower) triangular matrix, the inverse matrix will also be upper (lower) triangular, and after the end of the algorithm, the inverse matrix replaces the source matrix. The elements below (above) the main diagonal are not changed by the algorithm. If the matrix has a unit diagonal, the inverse matrix also has a unit diagonal, and the diagonal elements are not passed to the algorithm. Input parameters: A - matrix. Array whose indexes range within [1..N, 1..N]. N - size of matrix A. IsUpper - True, if the matrix is upper triangular. IsUnitTriangular - True, if the matrix has a unit diagonal. Output parameters: A - inverse matrix (if the problem is not degenerate). Result: True, if the matrix is not singular. False, if the matrix is singular. -- LAPACK routine (version 3.0) -- Univ. of Tennessee, Univ. of California Berkeley, NAG Ltd., Courant Institute, Argonne National Lab, and Rice University February 29, 1992 *************************************************************************/ bool invtriangular(ap::real_2d_array& a, int n, bool isupper, bool isunittriangular) { bool result; bool nounit; int i; int j; int nmj; int jm1; int jp1; double v; double ajj; ap::real_1d_array t; result = true; t.setbounds(1, n); // // Test the input parameters. // nounit = !isunittriangular; if( isupper ) { // // Compute inverse of upper triangular matrix. // for(j = 1; j <= n; j++) { if( nounit ) { if( a(j,j)==0 ) { result = false; return result; } a(j,j) = 1/a(j,j); ajj = -a(j,j); } else { ajj = -1; } // // Compute elements 1:j-1 of j-th column. // if( j>1 ) { jm1 = j-1; ap::vmove(t.getvector(1, jm1), a.getcolumn(j, 1, jm1)); for(i = 1; i <= j-1; i++) { if( i<j-1 ) { v = ap::vdotproduct(a.getrow(i, i+1, jm1), t.getvector(i+1, jm1)); } else { v = 0; } if( nounit ) { a(i,j) = v+a(i,i)*t(i); } else { a(i,j) = v+t(i); } } ap::vmul(a.getcolumn(j, 1, jm1), ajj); } } } else { // // Compute inverse of lower triangular matrix. // for(j = n; j >= 1; j--) { if( nounit ) { if( a(j,j)==0 ) { result = false; return result; } a(j,j) = 1/a(j,j); ajj = -a(j,j); } else { ajj = -1; } if( j<n ) { // // Compute elements j+1:n of j-th column. // nmj = n-j; jp1 = j+1; ap::vmove(t.getvector(jp1, n), a.getcolumn(j, jp1, n)); for(i = j+1; i <= n; i++) { if( i>j+1 ) { v = ap::vdotproduct(a.getrow(i, jp1, i-1), t.getvector(jp1, i-1)); } else { v = 0; } if( nounit ) { a(i,j) = v+a(i,i)*t(i); } else { a(i,j) = v+t(i); } } ap::vmul(a.getcolumn(j, jp1, n), ajj); } } } return result; }
- Diễn đàn Toán học
- → Đang xem trang cá nhân: Bài viết: sans_amour