File inv.bas
'''''''''''''''''''''''''''''''''''''''''''''''''''''''''' '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) '''''''''''''''''''''''''''''''''''''''''''''''''''''''''' 'This routines must be defined by the programmer ' Sub LUDecomposition(ByRef A() As Double, _ ' ByVal M As Long, _ ' ByVal N As Long, _ ' ByRef Pivots() As Long) ' Function InvTriangular(ByRef A() As Double, _ ' ByVal N As Long, _ ' ByVal IsUpper As Boolean, _ ' ByVal IsUnitTriangular As Boolean) As Boolean 'Routines '''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''' 'Inversion of a matrix given by its LU decomposition. ' 'Input parameters: ' A - LU decomposition of the matrix (output of LUDecomposition subroutine). ' Pivots - table of permutations which were made during the LU decomposition ' (the output of LUDecomposition subroutine). ' N - size of matrix A. ' 'Output parameters: ' A - inverse of matrix A. ' Array whose indexes range within [1..N, 1..N]. ' '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 InverseLU(ByRef A() As Double, _ ByRef Pivots() As Long, _ ByVal N As Long) As Boolean Dim Result As Boolean Dim WORK() As Double Dim I As Long Dim IWS As Long Dim J As Long Dim JB As Long Dim JJ As Long Dim JP As Long Dim JP1 As Long Dim V As Double Dim i_ As Long Result = True ' ' Quick return if possible ' If N=0# then InverseLU = Result Exit Function End If ReDim WORK(1# To N) ' ' Form inv(U) ' If Not InvTriangular(A, N, True, False) then Result = False InverseLU = Result Exit Function End If ' ' Solve the equation inv(A)*L = inv(U) for inv(A). ' For J=N To 1# Step -1 ' ' Copy current column of L to WORK and replace with zeros. ' For I=J+1# To N Step 1 WORK(I) = A(I,J) A(I,J) = 0# Next I ' ' Compute current column of inv(A). ' If J<N then JP1 = J+1# For I=1# To N Step 1 V = 0.0 For i_ = JP1 To N Step 1 V = V + A(I,i_)*WORK(i_) Next i_ A(I,J) = A(I,J)-V Next I End If Next J ' ' Apply column interchanges. ' For J=N-1# To 1# Step -1 JP = Pivots(J) If JP<>J then For i_ = 1# To N Step 1 WORK(i_) = A(i_,J) Next i_ For i_ = 1# To N Step 1 A(i_,J) = A(i_,JP) Next i_ For i_ = 1# To N Step 1 A(i_,JP) = WORK(i_) Next i_ End If Next J InverseLU = Result End Function '''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''' 'Inversion of a general matrix. ' 'Input parameters: ' A - matrix. Array whose indexes range within [1..N, 1..N]. ' N - size of matrix A. ' 'Output parameters: ' A - inverse of matrix A. ' Array whose indexes range within [1..N, 1..N]. ' 'Result: ' True, if the matrix is not singular. ' False, if the matrix is singular. ' ' -- ALGLIB -- ' Copyright 2005 by Bochkanov Sergey ' '''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''' Public Function Inverse(ByRef A() As Double, ByVal N As Long) As Boolean Dim Result As Boolean Dim Pivots() As Long Call LUDecomposition(A, N, N, Pivots) Result = InverseLU(A, Pivots, N) Inverse = Result End Function
Còn đây là bằng C# :
File inv.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) ***********************************************************************/ /* This routines must be defined by the programmer: static void ludecomposition(ref double[,] a, int m, int n, ref int[] pivots) static bool invtriangular(ref double[,] a, int n, bool isupper, bool isunittriangular) */ /************************************************************************* Inversion of a matrix given by its LU decomposition. Input parameters: A - LU decomposition of the matrix (output of LUDecomposition subroutine). Pivots - table of permutations which were made during the LU decomposition (the output of LUDecomposition subroutine). N - size of matrix A. Output parameters: A - inverse of matrix A. Array whose indexes range within [1..N, 1..N]. 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 inverselu(ref double[,] a, ref int[] pivots, int n) { bool result = new bool(); double[] work = new double[0]; int i = 0; int iws = 0; int j = 0; int jb = 0; int jj = 0; int jp = 0; int jp1 = 0; double v = 0; int i_ = 0; result = true; // // Quick return if possible // if( n==0 ) { return result; } work = new double[n+1]; // // Form inv(U) // if( !invtriangular(ref a, n, true, false) ) { result = false; return result; } // // Solve the equation inv(A)*L = inv(U) for inv(A). // for(j=n; j>=1; j--) { // // Copy current column of L to WORK and replace with zeros. // for(i=j+1; i<=n; i++) { work[i] = a[i,j]; a[i,j] = 0; } // // Compute current column of inv(A). // if( j<n ) { jp1 = j+1; for(i=1; i<=n; i++) { v = 0.0; for(i_=jp1; i_<=n;i_++) { v += a[i,i_]*work[i_]; } a[i,j] = a[i,j]-v; } } } // // Apply column interchanges. // for(j=n-1; j>=1; j--) { jp = pivots[j]; if( jp!=j ) { for(i_=1; i_<=n;i_++) { work[i_] = a[i_,j]; } for(i_=1; i_<=n;i_++) { a[i_,j] = a[i_,jp]; } for(i_=1; i_<=n;i_++) { a[i_,jp] = work[i_]; } } } return result; } /************************************************************************* Inversion of a general matrix. Input parameters: A - matrix. Array whose indexes range within [1..N, 1..N]. N - size of matrix A. Output parameters: A - inverse of matrix A. Array whose indexes range within [1..N, 1..N]. Result: True, if the matrix is not singular. False, if the matrix is singular. -- ALGLIB -- Copyright 2005 by Bochkanov Sergey *************************************************************************/ public static bool inverse(ref double[,] a, int n) { bool result = new bool(); int[] pivots = new int[0]; ludecomposition(ref a, n, n, ref pivots); result = inverselu(ref a, ref pivots, n); return result; }
C++ :
inv.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" /*----------------------------------------------- This routines must be defined by the programmer: void ludecomposition(ap::real_2d_array& a, int m, int n, ap::integer_1d_array& pivots); bool invtriangular(ap::real_2d_array& a, int n, bool isupper, bool isunittriangular); -----------------------------------------------*/ bool inverselu(ap::real_2d_array& a, const ap::integer_1d_array& pivots, int n); bool inverse(ap::real_2d_array& a, int n); /************************************************************************* Inversion of a matrix given by its LU decomposition. Input parameters: A - LU decomposition of the matrix (output of LUDecomposition subroutine). Pivots - table of permutations which were made during the LU decomposition (the output of LUDecomposition subroutine). N - size of matrix A. Output parameters: A - inverse of matrix A. Array whose indexes range within [1..N, 1..N]. 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 inverselu(ap::real_2d_array& a, const ap::integer_1d_array& pivots, int n) { bool result; ap::real_1d_array work; int i; int iws; int j; int jb; int jj; int jp; int jp1; double v; result = true; // // Quick return if possible // if( n==0 ) { return result; } work.setbounds(1, n); // // Form inv(U) // if( !invtriangular(a, n, true, false) ) { result = false; return result; } // // Solve the equation inv(A)*L = inv(U) for inv(A). // for(j = n; j >= 1; j--) { // // Copy current column of L to WORK and replace with zeros. // for(i = j+1; i <= n; i++) { work(i) = a(i,j); a(i,j) = 0; } // // Compute current column of inv(A). // if( j<n ) { jp1 = j+1; for(i = 1; i <= n; i++) { v = ap::vdotproduct(a.getrow(i, jp1, n), work.getvector(jp1, n)); a(i,j) = a(i,j)-v; } } } // // Apply column interchanges. // for(j = n-1; j >= 1; j--) { jp = pivots(j); if( jp!=j ) { ap::vmove(work.getvector(1, n), a.getcolumn(j, 1, n)); ap::vmove(a.getcolumn(j, 1, n), a.getcolumn(jp, 1, n)); ap::vmove(a.getcolumn(jp, 1, n), work.getvector(1, n)); } } return result; } /************************************************************************* Inversion of a general matrix. Input parameters: A - matrix. Array whose indexes range within [1..N, 1..N]. N - size of matrix A. Output parameters: A - inverse of matrix A. Array whose indexes range within [1..N, 1..N]. Result: True, if the matrix is not singular. False, if the matrix is singular. -- ALGLIB -- Copyright 2005 by Bochkanov Sergey *************************************************************************/ bool inverse(ap::real_2d_array& a, int n) { bool result; ap::integer_1d_array pivots; ludecomposition(a, n, n, pivots); result = inverselu(a, pivots, n); return result; }