Documente Academic
Documente Profesional
Documente Cultură
picOutput.Cls
epsilon = 0.00001
factor = 20#
'
'Read matrix A for input as a string
matrixA = txtMatrixA
'
'Extract the matrix rows and individual elements using subroutine ExtractMatrix
'Then print the matrix in the output field to confirm matrix entry
Call ExtractMatrix(matrixA, A, nA, mA)
Call PrintMatrixA(A, nA, mA)
'Making sure matrix A is a square matrix
If nA <> mA Then
picOutput.Print "Matrix A must be a square matrix to calculate";
If index = 1 Then
picOutput.Print " the inverse matrix."
ElseIf index = 2 Then
picOutput.Print " the determinant."
Else
picOutput.Print " x from A*x = B."
End If
Exit Sub
End If
'
'Read Matrix B if needed, otherwise load identity matrix
If index = 1 Or index = 2 Then
If index = 1 Then
nB = nA: mB = mA
Else
nB = nA: mB = 1
End If
For i = 1 To nA
For j = 1 To mB
If i = j Then
B(i, j) = 1#
Else
B(i, j) = 0#
End If
Next j
Next i
Else
matrixB = txtMatrixB
Call ExtractMatrix(matrixB, B, nB, mB)
Call PrintMatrixB(B, nB, mB)
End If
' Check that matrices are compatible
If nA <> nB Then
picOutput.Cls
picOutput.Print "Matrices A and B must have the same number of"
picOutput.Print "rows in order to solve for the linear system Ax=B."
Exit Sub
End If
'Create the Order vector and load default values of aMax(i)
For i = 1 To nA
Order(i) = i
aMax(i) = 1#
Next i
'Augmenting Matrix A
For i = 1 To nA
For j = 1 To mB
A(i, j + nA) = B(i, j)
Next j
Next i
'Scaling
For i = 1 To nA
S(i) = 0#
For j = 1 To nA
S(i) = S(i) + Abs(A(i, j))
Next j
Next i
scaleFlag = False
For i = 1 To (nA - 1)
For j = (i + 1) To (nA + mB)
If S(i) > factor * S(j) Or S(j) > factor * S(i) Then
scaleFlag = True
End If
Next j
Next i
If scaleFlag Then
For i = 1 To nA
aMax(i) = 0.0000000001
For j = 1 To (nA + mB)
If (A(i, j) > aMax(i)) Then
aMax(i) = A(i, j)
End If
Next j
Next i
For i = 1 To nA
For j = 1 To (nA + mB)
A(i, j) = A(i, j) / aMax(i)
Next j
Next i
End If
'Elimination procedure applied to rows 2 to n to fill with
'zeros lower triangular part of matrix
nExchanges = 0
For k = 1 To (nA - 1)
'Pivoting implemented in the next If statement
For i = (k + 1) To nA
If Abs(A(Order(i), k)) > Abs(A(Order(k), k)) Then
nExchanges = nExchanges + 1
Otemp = Order(i)
Order(i) = Order(k)
Order(k) = Otemp
End If
Next i
For i = (k + 1) To nA
For j = (k + 1) To (nA + mB)
A(Order(i), j) = A(Order(i), j) - A(Order(i), k) * A(Order(k), j
) / A(Order(k), k)
Next j
Next i
Next k
'Calculate and print determinant
detA = 1#
For i = 1 To nA
detA = detA * A(Order(i), i) * aMax(i)
Next i
detA = (-1) ^ nExchanges * detA
If index = 2 Then
picOutput.Cls
picOutput.Print
picOutput.Print "Determinant of matrix A = ", detA
Exit Sub
End If
'If the determinant is small, then the matrix is singular
If Abs(detA) < epsilon Then
picOutput.Print "Matrix is singular."
If index = 3 Then
picOutput.Print " No solution is possible."
ElseIf index = 1 Then
picOutput.Print " Inverse does not exist."
End If
Exit Sub
End If
'Calculating solutions
For j = 1 To mB
x(Order(nA), j) = A(Order(nA), j + nA) / A(Order(nA), nA)
For i = (nA - 1) To 1 Step -1
Sum = 0#
For k = (i + 1) To nA
Sum = Sum + A(Order(i), k) * x(Order(k), j)
Next k
x(Order(i), j) = (A(Order(i), j + nA) - Sum) / A(Order(i), i)
Next i
Next j
'Print result in output field
picOutput.Cls
picOutput.Print
If index = 1 Or index = 3 Then
If index = 1 Then
picOutput.Print "Inverse of Matrix A:"
ElseIf index = 3 Then
picOutput.Print "Solution to the linear system: A*x = B. ------> x
:"
End If
For i = 1 To nA
For j = 1 To mB
picOutput.Print x(Order(i), j),
Next j
picOutput.Print
Next i
End If
End Sub
'===========================================================================
Private Sub cmdInfo_Click()
'***************************************************************************
'This subroutine puts together a message showing the way to enter the matrix
'elements into the input field, and shows that message using a message box
'***************************************************************************
Dim myMessage As String
myMessage = "Enter a matrix by using brackets to separate each row."
myMessage = myMessage & " Elements within a row are separated by blank space
s only."
myMessage = myMessage & " You can enter the entire matrix in a single line:
"
myMessage = myMessage & " [[a11 a12 ... a1m] [a21 a22 ... a2m] ... [an1 an2
... anm]] <return>"
myMessage = myMessage & " or using one line per row:
"
myMessage = myMessage & " [[a11 a12 ... a1m] <return>"
myMessage = myMessage & " [a21 a22 ... a2m] <return>"
myMessage = myMessage & "
. .
. "
myMessage = myMessage & "
[an1 an2 ... anm]] <return>"
MsgBox myMessage
End Sub
'===========================================================================
Private Sub cmdClearOutputField_Click()
'***************************************************************************
'This subroutine simple clears the output field
'***************************************************************************
picOutput.Cls
picMatrixA.Cls
picMatrixB.Cls
End Sub
'===========================================================================
Private Sub cmdFrobenius_Click()
'***************************************************************************
'This subroutine reads a Double matrix, A, and calculates its Frobenius norm,
'which is the square root of the sum of squares of all the matrix elements.
'***************************************************************************
'Declaration of variables
Dim matrixA As String
Dim A(1 To 10, 1 To 10) As Double
Dim norm As Double
Dim nA As Integer, mA As Integer
Dim i As Integer, j As Integer
'
'Read matrix input as a string
matrixA = txtMatrixA
'
'Extract the matrix rows and individual elements using subroutine ExtractMatrix
'Then print the matrix in the output field to confirm matrix entry
Call ExtractMatrix(matrixA, A, nA, mA)
Else
picOutput.Print "Matrix C = A - B:"
End If
Call PrintMatrix(C, nA, mA)
End Sub
'===========================================================================
Private Sub cmdMultiplyAB_Click()
Dim index As Boolean
index = True
Call MultiplyTwoMatrices(index)
End Sub
'===========================================================================
Private Sub cmdMultiplyBA_Click()
Dim index As Boolean
index = False
Call MultiplyTwoMatrices(index)
End Sub
'===========================================================================
Private Sub MultiplyTwoMatrices(index As Boolean)
'*****************************************************************************
' This subroutine adds two matrices A and B
' If index = True, then calculate A * B.
' If index = False, then calculate B * A.
'*****************************************************************************
'
'Declaration of variables
Dim matrixA As String, matrixB As String
Dim A(1 To 10, 1 To 10) As Double
Dim B(1 To 10, 1 To 10) As Double
Dim C(1 To 10, 1 To 10) As Double
Dim nA As Integer, mA As Integer
Dim nB As Integer, mB As Integer
Dim i As Integer, j As Integer, k As Integer
'
'Read matrices A and B for input as a string
matrixA = txtMatrixA
matrixB = txtMatrixB
'
'Extract the matrix rows and individual elements using subroutine ExtractMatrix
'Then print the matrix in the output field to confirm matrix entry
Call ExtractMatrix(matrixA, A, nA, mA)
Call PrintMatrixA(A, nA, mA)
Call ExtractMatrix(matrixB, B, nB, mB)
Call PrintMatrixB(B, nB, mB)
'
' Check that matrices are compatible
If index Then
If mA <> nB Then
picOutput.Cls
picOutput.Print "Matrices A and B are not compatible for"
picOutput.Print "multiplication A*B."
Exit Sub
End If
Else
If mB <> nA Then
picOutput.Cls
+ A(i, k) * B(k, j)
+ B(i, k) * A(k, j)
* B:"
* A:"
End Sub
'===========================================================================
Private Sub ExtractMatrix(MatrixString As String, m() As Double, _
nRows As Integer, nColumns As Integer)
'***************************************************************************
'
'This subroutine reads a matrix when entered in the form:
'
' [[a11 a12 ... a1m] [a21 a22 ... a2m] ... [an1 an2 ... anm]].
'
'The subroutine takes the input from a text box called "txtInput",
'which should haveits "multi-line" property modified to "True".
'
'The subroutine locates the position of brackets,
'determines if the brackets are paired or if the expression is empty,
'and separates the elements of the matrix into a set of real numbers.
'
'The subroutine can be called from any other subroutine, by using:
'
'
Call ReadMatrix(M(),nRows,nColumns)
'
'It will return the matrix, M(i,j) As Double, the number of rows, nRows As
numberOfElements = numberOfElements + 1
Call GetValueAndTrim(myRow, myRowValues(numberOfElements), checkFlag)
Loop
numberOfElements = numberOfElements - 1
End Sub
'===========================================================================
Private Sub GetValueAndTrim(myString As String, myValue As Variant, _
myFlag As Boolean)
'*************************************************************************
'This subroutine takes the string "myString" made of a series of numbers
'separated by spaces and assigns the value of the first number to the
'variant variable "myValue". Then, the subroutine trims the first number
'off the original string and replaces the trimmed string into "myString."
'If a value is recovered then the boolean flag variable "myFlag" is set as
'True. If the input string "myString" is empty, then the boolean flag
'variable "myFlag" is set to false.
'**************************************************************************
Dim
Dim
Dim
Dim
Dim
Dim
myStringLength As Integer
myValueStringLength As Integer
charactersToTheRight As Integer
i As Integer, j As Integer
startChar As Integer, endChar As Integer
leftOverChars As Integer
myString = Trim(myString)
If myString <> "" Then
myFlag = True
myStringLength = Len(myString)
If myStringLength = 1 Then
myValue = Val(myString)
myString = ""
Else
For i = 1 To myStringLength
If Mid(myString, i, 1) <> " " Then
startChar = i
Exit For
End If
Next i
endChar = myStringLength
For j = startChar To myStringLength
If Mid(myString, j, 1) = " " Then
endChar = j - 1
Exit For
End If
Next j
myValueStringLength = endChar - startChar + 1
myValue = Val(Mid(myString, startChar, myValueStringLength))
leftOverChars = myStringLength - myValueStringLength
myString = Right(myString, leftOverChars)
End If
Else
myFlag = False
End If
End Sub
'===========================================================================
Private Sub PrintMatrix(Matrix() As Double, n As Integer, m As Integer)
'*************************************************************************
' This subroutine prints a Matrix
'**************************************************************************
Dim i As Integer, j As Integer
picOutput.Print "Matrix has "; n; " rows and "; m; " columns:"
picOutput.Print
For i = 1 To n
For j = 1 To m
picOutput.Print Matrix(i, j),
Next j
picOutput.Print
Next i
End Sub
'===========================================================================
Private Sub Form_Load()
End Sub