The University of Tennessee Modified by Nilson

MATRIZ INVERSA EN VISUAL BASIC 6.0

He visto que muchas personas que programan con Visual Basic preguntan sobre la matriz inversa, me doy cuenta que es un poco difícil encontrarla en Internet. En este documento me gustaría colocar como se hace. 1) Inicia VB6.0 en un proyecto estándar en blanco (figura 1):

Figura 1 2) Coloca un botón de comando en el formulario, que sea de tamaño relativamente pequeño (figura 2) y coloque la propiedad AUTOREDRAW = True del Form1.
Botón Command1

AutoRedraw = True

Figura 2

(Figura 3) *Click en el menú Proyecto *Clic en Agregar Modulo Figura 3 *Si esta ventana aparece.The University of Tennessee Modified by Nilson 3) Agregar un Modulo. pulsar abrir Figura 4 .

Aparecerá la ventana código (figura 6) Figura 6 Copie el siguiente código y péguelo dentro de esta ventana (el tamaño de la letra fue reducido para ahorrar espacio en este documento. NO NECESITA LEERLO): . Doble click Figura 5. haga doble clic en el Module 1 para ingresar a la ventana código de este modulo. En la ventana proyecto.The University of Tennessee Modified by Nilson 4) Introducir código de la matriz inversa en el Module 1.

Exp(-X)) / (Exp(X) + Exp(-X)) End Function Public Function Pi() As Double Pi = PiNumber End Function Public Function Power(ByVal Base As Double. YABS) If V = 0 Then Result = W Else Result = W * Sqr(1 + Square(V / W)) End If AbsComplex = Result End Function . YABS) V = MinReal(XABS.Y = 0 C_Complex = Result End Function Public Function AbsComplex(ByRef Z As Complex) As Double Dim Result As Double Dim W As Double Dim XABS As Double Dim YABS As Double Dim V As Double XABS = Abs(Z.Exp(-X)) / 2 End Function Public Function CosH(ByVal X As Double) As Double CosH = (Exp(X) + Exp(-X)) / 2 End Function Public Function TanH(ByVal X As Double) As Double TanH = (Exp(X) .Y) W = MaxReal(XABS. ByVal Exponent As Double) As Double Power = Base ^ Exponent End Function Public Function Square(ByVal X As Double) As Double Square = X * X End Function Public Function Log10(ByVal X As Double) As Double Log10 = Log(X) / Log(10) End Function Public Function Ceil(ByVal X As Double) As Double Ceil = -Int(-X) End Function Public Function RandomInteger(ByVal X As Long) As Long RandomInteger = Int(Rnd() * X) End Function Public Function Atn2(ByVal Y As Double. ByVal X As Double) As Double If SmallNumber * Abs(Y) < Abs(X) Then If X < 0 Then If Y = 0 Then Atn2 = Pi() Else Atn2 = Atn(Y / X) + Pi() * Sgn(Y) End If Else Atn2 = Atn(Y / X) End If Else Atn2 = Sgn(Y) * Pi() / 2 End If End Function Public Function C_Complex(ByVal X As Double) As Complex Dim Result As Complex Result.The University of Tennessee Modified by Nilson Private Const LUNB As Long = 8# Public Type Complex X As Double Y As Double End Type Public Const MachineEpsilon = 5E-16 Public Const MaxRealNumber = 1E+300 Public Const MinRealNumber = 1E-300 Private Const BigNumber As Double = 1E+70 Private Const SmallNumber As Double = 1E-70 Private Const PiNumber As Double = 3.X * X) If T < SmallNumber Then ArcCos = Atn(BigNumber * Sgn(-X)) + 2 * Atn(1) Else ArcCos = Atn(-X / T) + 2 * Atn(1) End If End Function Public Function SinH(ByVal X As Double) As Double SinH = (Exp(X) . ByVal M2 As Long) As Long If M1 > M2 Then MaxInt = M1 Else MaxInt = M2 End If End Function Public Function MinInt(ByVal M1 As Long. ByVal M2 As Double) As Double If M1 < M2 Then MinReal = M1 Else MinReal = M2 End If End Function Public Function MaxInt(ByVal M1 As Long. ByVal M2 As Long) As Long If M1 < M2 Then MinInt = M1 Else MinInt = M2 End If End Function Public Function ArcSin(ByVal X As Double) As Double Dim T As Double T = Sqr(1 .X * X) If T < SmallNumber Then ArcSin = Atn(BigNumber * Sgn(X)) Else ArcSin = Atn(X / T) End If End Function Public Function ArcCos(ByVal X As Double) As Double Dim T As Double T = Sqr(1 . ByVal M2 As Double) As Double If M1 > M2 Then MaxReal = M1 Else MaxReal = M2 End If End Function Public Function MinReal(ByVal M1 As Double.X = X Result.X) YABS = Abs(Z.14159265358979 Public Function MaxReal(ByVal M1 As Double.

Y + Z2.Y .X Result.Y .X / R Result.X = Z1.X = (A + B * E) / F Result.X + Z2.X B = Z1.Z2. ByVal R As Double) As Complex Dim Result As Complex Result.X = (B + A * E) / F Result.Y = -Z1.Y C_SubR = Result End Function Public Function C_RSub(ByVal R As Double.X D = Z2.X = Z1. ByRef Z1 As Complex) As Complex Dim Result As Complex Result.X Result.Y * Z2.X D = Z2.X = Z1.Y = (B . ByRef Z2 As Complex) As Complex Dim Result As Complex Result.Y Result.The University of Tennessee Modified by Nilson Public Function C_Opposite(ByRef Z As Complex) As Complex Dim Result As Complex Result.A * E) / F Else E=C/D F=D+C*E Result.X = Z1.Y = Z1.Y = Z1.Y Conj = Result End Function Public Function CSqr(ByRef Z As Complex) As Complex Dim Result As Complex Result.Y = 2 * Z.Y CSqr = Result End Function Public Function C_Add(ByRef Z1 As Complex.Y = Z1.X = Z.X .X * R Result.Square(Z.Z1.X + R Result.X .X Result.Y = Z1. ByRef Z2 As Complex) As Complex Dim Result As Complex Result.Y = Z1.Y C = Z2.Y / R C_DivR = Result End Function Public Function C_RDiv(ByVal R As Double.Y C_Opposite = Result End Function Public Function Conj(ByRef Z As Complex) As Complex Dim Result As Complex Result.Y + Z1.R Result.X = Square(Z.X * Z2.Z1.X = R .Y = -Z.Y = -Z.Y = Z1.Z2.X * Z2.X = Z1.Y) Result.Y * R C_MulR = Result End Function Public Function C_Sub(ByRef Z1 As Complex.X = Z1.X Result.X . ByVal R As Double) As Complex Dim Result As Complex Result.Y C_AddR = Result End Function Public Function C_MulR(ByRef Z1 As Complex.Y If Abs(D) < Abs(C) Then E=D/C F=C+D*E Result. ByVal R As Double) As Complex Dim Result As Complex Result. ByRef Z2 As Complex) As Complex Dim Result As Complex Result.X) .Y C_Add = Result End Function Public Function C_Mul(ByRef Z1 As Complex.X = -Z. ByRef Z2 As Complex) As Complex Dim Result As Complex Dim A As Double Dim B As Double Dim C As Double Dim D As Double Dim E As Double Dim F As Double A = Z1.Y = Z1. ByRef Z2 As Complex) As Complex Dim Result As Complex Dim A As Double Dim C As Double Dim D As Double Dim E As Double Dim F As Double A=R C = Z2. ByVal R As Double) As Complex Dim Result As Complex Result.X Result.X C_Mul = Result End Function Public Function C_AddR(ByRef Z1 As Complex.X * Z.Y C_Sub = Result End Function Public Function C_SubR(ByRef Z1 As Complex.X = Z1.Y * Z2.Y C_RSub = Result End Function Public Function C_Div(ByRef Z1 As Complex.Y = (-A + B * E) / F End If C_Div = Result End Function Public Function C_DivR(ByRef Z1 As Complex.

OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY 'THEORY OF LIABILITY. INCIDENTAL. ' '.X <> Z2. 'SPECIAL. LOSS OF USE. 'DATA. ' 'Redistribution and use in source and binary forms.X <> R Or Z1.X Or Z1. _ ByRef Pivots() As Long. ' 'Result: ' True. THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR 'A PARTICULAR PURPOSE ARE DISCLAIMED. False) Then Result = False RMatrixLUInverse = Result Exit Function End If ' ' Solve the equation inv(A)*L = inv(U) for inv(A).LAPACK routine (version 3. Translation from FORTRAN to ' pseudocode. '''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''' 'Routines '''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''' 'Inversion of a matrix given by its LU decomposition.0) -' Univ. ' 'Output parameters: ' A . BUT NOT 'LIMITED TO..X And Z1.X = R And Z1. BUT NOT 'LIMITED TO.Y = 0 C_EqualR = Result End Function Public Function C_NotEqualR(ByRef Z1 As Complex. of California Berkeley.LU decomposition of the matrix (output of RMatrixLU subroutine). ' 'Contributors: ' * Sergey Bochkanov (ALGLIB project). ByRef Z2 As Complex) As Boolean Dim Result As Boolean Result = Z1. ' ' -. PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES. N. ' 'Input parameters: ' A . ' Pivots . Univ. ' Courant Institute. _ ByRef Z2 As Complex) As Boolean Dim Result As Boolean Result = Z1. STRICT LIABILITY. are permitted provided that the following conditions are 'met: ' '.X = A * E / F Result. IN NO EVENT SHALL THE COPYRIGHT 'OWNER OR CONTRIBUTORS BE LIABLE FOR ANY DIRECT. NAG Ltd. INDIRECT. _ ByVal R As Double) As Boolean Dim Result As Boolean Result = Z1. if the matrix is singular. 0.N-1. OR CONSEQUENTIAL DAMAGES (INCLUDING. ' 'THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS '"AS IS" AND ANY EXPRESS OR IMPLIED WARRANTIES. ByVal R As Double) As Boolean Dim Result As Boolean Result = Z1. ' .X = Z2.. EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE. WHETHER IN CONTRACT.Y = -(A / F) End If C_RDiv = Result End Function Public Function C_Equal(ByRef Z1 As Complex.. EXEMPLARY. with or without 'modification. ' 'See subroutines comments for additional copyrights.size of matrix A.Y C_NotEqual = Result End Function Public Function C_EqualR(ByRef Z1 As Complex.Y C_Equal = Result End Function Public Function C_NotEqual(ByRef Z1 As Complex. _ 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 V As Double Dim i_ As Long Result = True ' ' Quick return if possible ' If N = 0# Then RMatrixLUInverse = Result Exit Function End If ReDim WORK(0# To N . ' '. this list of conditions and the following disclaimer listed ' in this license in the documentation and/or other materials ' provided with the distribution. 1992 ' '''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''' Public Function RMatrixLUInverse(ByRef A() As Double. OR TORT '(INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE 'OF THIS SOFTWARE. All rights reserved.Y = Z2. this list of conditions and the following disclaimer. of Tennessee.The University of Tennessee Modified by Nilson If Abs(D) < Abs(C) Then E=D/C F=C+D*E Result.inverse of matrix A.1#) ' ' Form inv(U) ' If Not RMatrixTRInverse(A.Y <> Z2.N-1]. ' N . if the matrix is not singular.Y <> 0 C_NotEqualR = Result End Function '''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''' 'Copyright (c) 1992-2007 The University of Tennessee. OR PROFITS. Argonne National Lab.Redistributions of source code must retain the above copyright ' notice. True.Redistributions in binary form must reproduce the above copyright ' notice. ' Array whose indexes range within [0.X = A / F Result. and Rice University ' February 29.Neither the name of the copyright holders nor the names of its ' contributors may be used to endorse or promote products derived from ' this software without specific prior written permission.table of permutations which were made during the LU decomposition ' (the output of RMatrixLU subroutine). INCLUDING. ' False.Y = -(A * E / F) Else E=C/D F=D+C*E Result.

matrix. ' If J < N . J) Next i_ For i_ = 0# To N .1# To 1# Step -1 JP = Pivots(J) . ' For J = N . ' N . JP) = WORK(i_) Next i_ End If Next J RMatrixLUInverse = Result End Function '''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''' 'Inversion of a general matrix. J) = A(I. N. False) Then Result = False InverseLU = Result Exit Function End If ' ' Solve the equation inv(A)*L = inv(U) for inv(A)..N-1]. J) = 0# Next I ' ' Compute current column of inv(A). _ ByRef Pivots() As Long.V Next I End If Next J ' ' Apply column interchanges. J) A(I. _ 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.1# Step 1 WORK(i_) = A(i_. JP) Next i_ For i_ = 0# To N . J) .1# Step 1 V = 0# For i_ = J + 1# To N . ' For I = J + 1# To N Step 1 WORK(I) = A(I. ' For J = N . ' For I = J + 1# To N .2# To 0# Step -1 JP = Pivots(J) If JP <> J Then For i_ = 0# To N . ' False. i_) * WORK(i_) Next i_ A(I. ' '''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''' Public Function InverseLU(ByRef A() As Double. J) . Pivots) Result = RMatrixLUInverse(A. ' 'See RMatrixLUInverse for 0-based replacement.1# To 0# Step -1 ' ' Copy current column of L to WORK and replace with zeros. ' 'Input parameters: ' A . J) = A(i_.The University of Tennessee Modified by Nilson For J = N .1# Step 1 A(i_. J) A(I. N) RMatrixInverse = Result End Function '''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''' 'Obsolete 1-based subroutine..1# Step 1 WORK(I) = A(I. ' 'Output parameters: ' A . ByVal N As Long) As Boolean Dim Result As Boolean Dim Pivots() As Long Call RMatrixLU(A. 0. i_) * WORK(i_) Next i_ A(I.inverse of matrix A.1# Step 1 V = V + A(I. Pivots. if the matrix is singular. 0.size of matrix A. ' For J = N To 1# Step -1 ' ' Copy current column of L to WORK and replace with zeros. ' ' -.N-1]. True...1# Step 1 A(i_. Array whose indexes range within [0. ' 'Result: ' True.V Next I End If Next J ' ' Apply column interchanges.N-1. J) = A(I. ' Array whose indexes range within [0. J) = 0# Next I ' ' Compute current column of inv(A). N.N-1. if the matrix is not singular. ' If J < N Then JP1 = J + 1# For I = 1# To N Step 1 V = 0# For i_ = JP1 To N Step 1 V = V + A(I.ALGLIB -' Copyright 2005 by Bochkanov Sergey ' '''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''' Public Function RMatrixInverse(ByRef A() As Double.1# Then For I = 0# To N .

' ' -..N-1].Min(M-1. ByVal N As Long) As Boolean Dim Result As Boolean Dim Pivots() As Long Call LUDecomposition(A.. this list of conditions and the following disclaimer listed ' in this license in the documentation and/or other materials ' provided with the distribution. PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES.N). if M<N) matrix. if M>N) matrix. and Rice University ' June 30. ' 'Output parameters: ' A . 1992 ' '''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''' Public Sub RMatrixLU(ByRef A() As Double.blocked or unblocked code ' If N <= 1# Or MinInt(M.matrix A whose indexes range within [0. ' '. _ ByVal M As Long.Redistributions of source code must retain the above copyright ' notice. 0. OR TORT '(INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE 'OF THIS SOFTWARE. All rights reserved. JP) Next i_ For i_ = 1# To N Step 1 A(i_. ' 'Redistribution and use in source and binary forms. BUT NOT 'LIMITED TO.Neither the name of the copyright holders nor the names of its ' contributors may be used to endorse or promote products derived from ' this software without specific prior written permission. OR CONSEQUENTIAL DAMAGES (INCLUDING. N) Inverse = Result End Function '''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''' 'Copyright (c) 1992-2007 The University of Tennessee. INDIRECT. STRICT LIABILITY. ' '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. J) = A(i_. EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.number of columns in matrix A.0) -' Univ..LAPACK routine (version 3.. ' Pivots . ' '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.M-1. ' M . ' ' ( U11 U12 U13 ) ' ( L21 U22 U23 ) ' ( L31 L32 U33 ) ' ( L41 L42 L43 ) ' 'As we can see. EXEMPLARY. LOSS OF USE. Pivots.upper triangular (or upper trapezoid. J) Next i_ For i_ = 1# To N Step 1 A(i_. OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY 'THEORY OF LIABILITY. 'U . N) <= NB Or NB = 1# Then . Argonne National Lab. with or without 'modification. _ ByRef Pivots() As Long) Dim B() As Double Dim T() As Double Dim BP() As Long Dim MinMN As Long Dim I As Long Dim IP As Long Dim J As Long Dim J1 As Long Dim J2 As Long Dim CB As Long Dim NB As Long Dim V As Double Dim i_ As Long Dim i1_ As Long NB = LUNB ' ' Decide what to use . N. matrix U has size Min(M.N-1)]. matrix P(i) is 'a permutation of the identity matrix of size MxM with numbers I and Pivots[I]. WHETHER IN CONTRACT.permutation matrix in compact form (see below). Pivots) Result = InverseLU(A. N=3). ' '''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''' Public Function Inverse(ByRef A() As Double.N-1]. ' N . ' 'See RMatrixInverse for 0-based replacement. of California Berkeley. Univ. 'matrix L .N)xN. ' Courant Institute. ' 'Contributors: ' * Sergey Bochkanov (ALGLIB project). _ ByVal N As Long.. JP) = WORK(i_) Next i_ End If Next J InverseLU = Result End Function '''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''' 'Obsolete 1-based subroutine. ' 'Matrix A is represented as A = P * L * U. ' Array whose indexes range within [0. Translation from FORTRAN to ' pseudocode. ' 'See subroutines comments for additional copyrights. BUT NOT 'LIMITED TO. ' Array whose index ranges within [0. 0. this list of conditions and the following disclaimer.M-1.Redistributions in binary form must reproduce the above copyright ' notice. N.The University of Tennessee Modified by Nilson If JP <> J Then For i_ = 1# To N Step 1 WORK(i_) = A(i_. NAG Ltd..number of rows in matrix A. are permitted provided that the following conditions are 'met: ' '. IN NO EVENT SHALL THE COPYRIGHT 'OWNER OR CONTRIBUTORS BE LIABLE FOR ANY DIRECT. '''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''' 'Global constants '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).lower triangular (or lower trapezoid. ' '. where P is a permutation matrix. ' 'THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS '"AS IS" AND ANY EXPRESS OR IMPLIED WARRANTIES. THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR 'A PARTICULAR PURPOSE ARE DISCLAIMED. of Tennessee. 'SPECIAL. the unit diagonal isn't stored. OR PROFITS.matrices L and U in compact form (see below). INCIDENTAL. 'DATA. ' 'Input parameters: ' A . INCLUDING.

N) J1 = 0# J2 = MinInt(MinMN.1# Step 1 A(J1 + IP.1# Then ' ' Interchange the rest of the matrix. 0# To NB . Left for backward compatibility.J1.1# Then For I = J1 + 1# To J2 Step 1 For J = J1 To I . NB) . 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. i_) Next i_ For i_ = J2 + 1# To N .1#) ReDim T(0# To N .1# Loop End If End Sub '''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''' 'Obsolete 1-based subroutine.. i_) = T(i_) Next i_ End If If J2 < N . ' JP = J For I = J + 1# To M Step 1 . CB.1# Then For I = J2 + 1# To M .1# Step 1 T(i_) = A(J1 + I. apply pivots ' For I = J1 To M . i_) Next i_ Next J Next I End If ' ' Update trailing submatrix ' If J2 < N . Copy columns J1.1# Step 1 A(J1 + IP.1# Step 1 B(I .(0#) For i_ = 0# To CB . BP) For I = J1 To M .1# Step 1 A(J1 + I. i_) Next i_ For i_ = J2 + 1# To N . i_) = A(I. i_) Next i_ For i_ = 0# To J1 . prepare temporary matrix and indices ' ReDim B(0# To M . i_) = A(I. J) For i_ = J2 + 1# To N .J2 of A to B ' 2. i_) = T(i_) Next i_ End If End If Next I ' ' Compute block row of U ' If J2 < N .1# Step 1 T(i_) = A(J1 + I.1# Step 1 A(I.(J1) For i_ = J1 To J2 Step 1 A(I.1#.1# Step 1 i1_ = (J1) . _ ByVal M As Long. i_ + i1_) Next i_ Next I Call RMatrixLU2(B. Copy result back to A ' 4. i_) .1#) ReDim Pivots(0# To MinInt(M. ' First.The University of Tennessee Modified by Nilson ' ' Unblocked code ' Call RMatrixLU2(A. i_ + i1_) Next i_ Next I For I = 0# To CB .J1 + 1# ' ' LU factorization of diagonal and subdiagonal blocks: ' 1.1# Step 1 For J = J1 To J2 Step 1 V = A(I.1# Step 1 A(I. i_) = B(I .1# Step 1 A(J1 + I.J1. J) For i_ = J2 + 1# To N . i_) .J1.1# Step 1 i1_ = (0#) .V * A(J. Copy pivots. N) Step 1 ' ' Find pivot and test for singularity. i_) Next i_ For i_ = 0# To J1 .1# Step 1 V = A(I. M . J1 + NB) . 'See RMatrixLU for 0-based replacement.V * A(J. Pivots) Else ' ' Blocked code. N. i_) = A(J1 + IP. M.1#) MinMN = MinInt(M. i_) = A(J1 + IP. i_) = A(I. _ 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. i_) Next i_ Next J Next I End If ' ' Next step ' J1 = J2 + 1# J2 = MinInt(MinMN.1# ' ' Main cycle ' Do While J1 < MinMN CB = J2 . N) . ' '''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''' Public Sub LUDecomposition(ByRef A() As Double. LU(B) ' 3. if needed ' For i_ = J2 + 1# To N .1# Step 1 IP = BP(I) Pivots(J1 + I) = J1 + IP If BP(I) <> I Then If J1 <> 0# Then ' ' Interchange columns 0:J1-1 ' For i_ = 0# To J1 . _ ByVal N As Long.

J) Next i_ End If End If If J < MinInt(M. Pivots) For I = 1# To M Step 1 For J = 1# To MinMN Step 1 If J > I Then L(I. _ ByVal M As Long. J) = A(I. i_) = A(I. Univ. -ONE. A( J. J) <> 0# Then ' 'Apply the interchange to rows ' . _ 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. _ ByRef L() As Double. N) Then ' 'Update trailing submatrix.1#)) ' ' Quick return if possible ' If M = 0# Or N = 0# Then Exit Sub End If For J = 0# To MinInt(M . 1992 ' '''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''' Private Sub RMatrixLU2(ByRef A() As Double. ' JP = J For I = J + 1# To M . 'CALL DGER( M-J. _ ByVal M As Long.0) -' Univ. LDA ) ' JP = J + 1# For I = J + 1# To M Step 1 s = A(I.1#.1#.s * A(J. A( J+1. i_) . and Rice University ' June 30. 1# To N) Call LUDecomposition(A. J+1 ). 1. J) For i_ = JP To N Step 1 A(I. N-J. N) ReDim L(1# To M. ONE / A( J. i_) = T1(i_) Next i_ End If ' 'Compute elements J+1:M of J-th column. _ ByVal N As Long. ' Courant Institute. N . LDA. N . NAG Ltd. A( J+1.1#)) ReDim T1(0# To MaxInt(M .. of Tennessee. M.1#) Step 1 ' ' Find pivot and test for singularity. i_) Next i_ For i_ = 1# To N Step 1 A(JP. i_) Next i_ Next I End If Next J End Sub '''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''' 'Obsolete 1-based subroutine. 1# To MinMN) ReDim U(1# To MinMN.1#. N.The University of Tennessee Modified by Nilson If Abs(A(I. ' '''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''' Public Sub LUDecompositionUnpacked(ByRef A_() As Double. 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. J)) > Abs(A(JP. J)) Then JP = I End If Next I Pivots(J) = JP If A(JP.1# Step 1 If Abs(A(I. i_) = A(JP. N . ' If J < M Then ' ' CALL DSCAL( M-J.A( J+1. _ ByVal N As Long. i_) Next i_ For i_ = 1# To N Step 1 A(J. J ). J) = 1# End If If J < I Then L(I. Left for backward compatibility. _ 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(0# To MinInt(M . J) = 0# End If If J >= I Then U(I. J) = s * A(i_. J) End If Next J Next I End Sub '''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''' 'Level 2 BLAS version of RMatrixLU ' ' -. Argonne National Lab. J) For i_ = JP To M Step 1 A(i_. J ). J)) > Abs(A(JP. 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 L(I. J+1 ). J ). of California Berkeley. 1 ) ' JP = J + 1# s = 1# / A(J. J) = A(I.LAPACK routine (version 3. _ ByRef U() As Double.

i_) Next i_ For i_ = 0# To N . if the matrix is upper triangular. Translation from FORTRAN to ' pseudocode. ' 'Contributors: ' * Sergey Bochkanov (ALGLIB project). The elements below (above) the 'main diagonal are not changed by the algorithm. PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES. J) = s * A(i_. ' Courant Institute. Inferior: 'InvTriangular(Matr(). NAG Ltd.1# Step 1 T1(i_) = A(J. ' 'Result: ' True. with or without 'modification. OR PROFITS. False) 'Una matriz triangular superior (y tambien triangular inferior) 'Para invertir una matriz triang. i_) Next i_ Next I End If Next J End Sub '''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''' 'Copyright (c) 1992-2007 The University of Tennessee. Univ.1# Step 1 A(JP. J) For i_ = JP To N . LOSS OF USE. OR CONSEQUENTIAL DAMAGES (INCLUDING.. ' 'If the matrix has a unit diagonal. ' IsUpper . ' 'Output parameters: ' A .Redistributions of source code must retain the above copyright ' notice.1# Step 1 s = A(I.N-1]. this list of conditions and the following disclaimer. of California Berkeley. INDIRECT. ' NOUNIT = Not IsUnitTriangular If IsUpper Then . and the diagonal elements are not passed to the algorithm.True. ' 'Input parameters: ' A . i_) . False. 0.True. ' '. this list of conditions and the following disclaimer listed ' in this license in the documentation and/or other materials ' provided with the distribution.1# Step 1 A(J. INCIDENTAL.LAPACK routine (version 3.1#) ' ' Test the input parameters.1# Step 1 A(i_. EXEMPLARY. OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY 'THEORY OF LIABILITY. J) Next i_ End If End If If J < MinInt(M. _ ByVal IsUpper As Boolean. N) . '''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''' '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. nn. All rights reserved. OR TORT '(INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE 'OF THIS SOFTWARE. of Tennessee. WHETHER IN CONTRACT. False) Public Function RMatrixTRInverse(ByRef A() As Double.N-1..inverse matrix (if the problem is not degenerate). ' If J < M Then JP = J + 1# s = 1# / A(J. and after the end of the algorithm. i_) = A(I. Argonne National Lab. ' Array whose indexes range within [0.The University of Tennessee Modified by Nilson If JP <> J Then For i_ = 0# To N . and Rice University ' February 29. ' ' -. BUT NOT 'LIMITED TO. 'DATA.1# Then ' 'Update trailing submatrix. if the matrix has a unit diagonal. _ ByVal N As Long.size of matrix A. ' 'See subroutines comments for additional copyrights. are permitted provided that the following conditions are 'met: ' '. ' N .matrix. i_) = A(JP. EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE. 1992 ' '''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''' 'Invierte una matriz como esta: '2 0 0 '1 3 0 '3 1 4 'Para invertir una matriz triang. BUT NOT 'LIMITED TO. STRICT LIABILITY. ' JP = J + 1# For I = J + 1# To M . i_) Next i_ For i_ = 0# To N . if the matrix is singular.s * A(J. ' '.0) -' Univ. Superior 'InvTriangular(Matr().Neither the name of the copyright holders nor the names of its ' contributors may be used to endorse or promote products derived from ' this software without specific prior written permission. i_) = T1(i_) Next i_ End If ' 'Compute elements J+1:M of J-th column. the inverse matrix also has a unit 'diagonal. ' IsUnitTriangular ' .Redistributions in binary form must reproduce the above copyright ' notice. nn.1# Step 1 A(I. True. ' False. J) For i_ = JP To M . THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR 'A PARTICULAR PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT 'OWNER OR CONTRIBUTORS BE LIABLE FOR ANY DIRECT. _ ByVal IsUnitTriangular As Boolean) As Boolean Dim Result As Boolean Dim NOUNIT As Boolean Dim I As Long Dim J As Long Dim V As Double Dim AJJ As Double Dim T() As Double Dim i_ As Long Result = True ReDim T(0# To N . 'SPECIAL.. the 'inverse matrix replaces the source matrix. INCLUDING. if the matrix is not singular. the inverse matrix will 'also be upper (lower) triangular. ' 'THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS '"AS IS" AND ANY EXPRESS OR IMPLIED WARRANTIES. ' 'Redistribution and use in source and binary forms.

J) = 1# / A(J. J) = AJJ * A(i_.1# Step 1 V = V + A(I. 'See RMatrixTRInverse for 0-based replacement.1# Step 1 If I < J . J) = V + T(I) End If Next I For i_ = 0# To J . J) = AJJ * A(i_. ' '''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''' Public Function InvTriangular(ByRef A() As Double. J) = 0# Then Result = False RMatrixTRInverse = Result Exit Function End If A(J.1# Step 1 If NOUNIT Then If A(J. J) Else AJJ = -1# End If ' ' Compute elements 1:j-1 of j-th column. J) AJJ = -A(J.1# Step 1 If I > J + 1# Then V = 0# For i_ = J + 1# To I .1# Step 1 T(i_) = A(i_. i_) * T(i_) Next i_ Else V = 0# End If If NOUNIT Then A(I. i_) * T(i_) Next i_ Else V = 0# End If If NOUNIT Then A(I.1# Then . I) * T(I) Else A(I.1# Step 1 V = V + A(I. J) Else AJJ = -1# End If ' ' Compute elements 1:j-1 of j-th column. J) Next i_ For I = 1# To J . J) AJJ = -A(J. _ 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. J) = 0# Then Result = False RMatrixTRInverse = Result Exit Function End If A(J. J) = 1# / A(J.1# To 0# Step -1 If NOUNIT Then If A(J.1# For i_ = 1# To JM1 Step 1 T(i_) = A(i_. ' For i_ = J + 1# To N . J) = V + T(I) End If Next I For i_ = J + 1# To N . J) Next i_ For I = J + 1# To N .1# Then ' ' Compute elements j+1:n of j-th column.1# Step 1 If I < J . J) = V + A(I. J) Next i_ End If Next J Else ' ' Compute inverse of lower triangular matrix.1# Step 1 A(i_.1# Step 1 T(i_) = A(i_. ' NOUNIT = Not IsUnitTriangular If IsUpper Then ' ' Compute inverse of upper triangular matrix. ' If J > 1# Then JM1 = J . _ ByVal IsUpper As Boolean.1# Then V = 0# For i_ = I + 1# To J .The University of Tennessee Modified by Nilson ' ' Compute inverse of upper triangular matrix. J) Next i_ End If Next J End If RMatrixTRInverse = Result End Function '''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''' 'Obsolete 1-based subroutine. J) Else AJJ = -1# End If If J < N . I) * T(I) Else A(I. J) = V + A(I.1# Step 1 A(i_. J) Next i_ For I = 0# To J . ' For J = 0# To N . J) = 0# Then Result = False InvTriangular = Result Exit Function End If A(J. J) AJJ = -A(J. ' If J > 0# Then For i_ = 0# To J . _ ByVal N As Long. ' For J = 1# To N Step 1 If NOUNIT Then If A(J. ' For J = N . J) = 1# / A(J.

1# Step 1 V = V + A(I. J) = V + T(I) End If Next I For i_ = JP1 To N Step 1 A(i_. J) = V + A(I.The University of Tennessee Modified by Nilson V = 0# For i_ = I + 1# To JM1 Step 1 V = V + A(I. J) Next i_ For I = J + 1# To N Step 1 If I > J + 1# Then V = 0# For i_ = JP1 To I .J JP1 = J + 1# For i_ = JP1 To N Step 1 T(i_) = A(i_. J) AJJ = -A(J. I) * T(I) Else A(I. I) * T(I) Else A(I. es decir. J) = AJJ * A(i_. i_) * T(i_) Next i_ Else V = 0# End If If NOUNIT Then A(I. J) = V + A(I. J) Else AJJ = -1# End If If J < N Then ' ' Compute elements j+1:n of j-th column. ' NMJ = N . Private Sub Form_Load() End Sub . *Haga doble Clic en el Form 1 para ingresar a la ventana código del Form1 Aparecerá la siguiente ventana (figura 7): Figura 7 *Borre las líneas de código mostradas dentro de esta venta. J) = AJJ * A(i_. i_) * T(i_) Next i_ Else V = 0# End If If NOUNIT Then A(I. J) = V + T(I) End If Next I For i_ = 1# To JM1 Step 1 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) Next i_ End If Next J End If InvTriangular = Result End Function 6) Introducir código dentro del Formulario Form1 *Cierre la ventana código del Module 1.

Chr(32)) End Function Private Sub MostrarMatriz(Matriz() As Double. " & JJ & " ) = ") Next JJ Next II Me. nn) Then Print "Tiene Inversa" Else Print "No tiene inversa" Exit Sub End If Call MostrarMatriz(Matr(). nn) As Double For II = 1 To nn For JJ = 1 To nn Matr(II. False) Print vbCrLf If Inverse(Matr(). True) End Sub Private Sub Form_Load() End Sub . como en la siguiente figura: Figura 8 *Ahora Copie el siguiente código y péguelo dentro de esta ventana (NO NECESITA LEERLO): Private Function Let27(Palabra As String) 'Funcion que da una extensidad 'de 27 caracteres a una palabra Dim NumeroEsp As Integer: NumeroEsp = 27 . SCI As Boolean) Dim II As Integer Dim JJ As Integer Dim EscribirMat As String For II = 1 To UBound(Matriz()) For JJ = 1 To UBound(Matriz()) If SCI = True Then EscribirMat = EscribirMat & Let27(Str(Format(Matriz(II.The University of Tennessee Modified by Nilson La ventana código del Form 1 quedará en blanco. JJ) = InputBox("Elemento ( " & II & " .0000000"))) & vbTab ElseIf SCI = False Then EscribirMat = EscribirMat & Let27(Str(Matriz(II.Cls Print "Matriz a invertir" Call MostrarMatriz(Matr().Len(Palabra) If NumeroEsp < 0 Then Let27 = Left(Palabra. JJ). JJ))) & vbTab End If Next JJ EscribirMat = EscribirMat & vbCrLf Next II Print EscribirMat End Sub Private Sub Command1_Click() Dim II As Integer Dim JJ As Integer Dim Matr() As Double Dim InvNatr() As Double Dim nn As Integer Dim EscribirMat As String nn = Val(InputBox("Tamaño de matriz")) If nn = 0 Then Exit Sub ReDim Matr(nn. "0. 27) Exit Function End If Let27 = Palabra & String(NumeroEsp.

ESPERO QUE LES SIRVA ESTE CÓDIGO. esta debe ser CUADRADA. Y LISTO. * De nuevo aparecerá un cuadro de texto por cada elemento de la matriz. * Pulse el Botón Command1. esto es para introducir cada elemento de la matriz cuadrada. . y en matemáticas en general. * Ahora puede ejecutar la aplicación. NO SE PUEDE SACAR MATRIZ INVERSA A MATRICES QUE NO SEAN CUADRADAS) Figura 10. * La Matriz Inversa es utilizada para resoluciones de ecuaciones lineales. es decir. Pulse el botón Iniciar (figura 9).The University of Tennessee Modified by Nilson 7) Ejecución de la aplicación Matriz Inversa en Visual Basic.0. * Una vez introducidos todos los elementos de la matriz. Puedes modificar el código para mostrar en mejor forma los resultados de la matriz inversa. YA TIENES LA MATRIZ INVERSA EN VB6. Figura 11. de tamaño n x n. el resultado se imprimirá en el formulario. aparecerá un cuadro InputBox solicitando el tamaño de la matriz cuadrada (Para invertir una matriz. Pulse este botón para ejecutar la aplicación Figura 9. para el cálculo de estructuras en Ingeniería Civil (Análisis Matricial).

Sign up to vote on this title
UsefulNot useful