danbaron
08-03-2010, 03:04
Problem 4
When a number n, and its inverse (reciprocal), 1/n, are multiplied together, the
product is 1.
Likewise, when a square matrix, A, of dimension m x m, and its inverse, A^-1,
are multiplied together, the product is the m x m identity matrix, I; for which
the values on the main diagonal (the diagonal extending from upper left to lower
right) are 1, and the values not on the main diagonal are 0.
For instance, the 2 x 2 identity matrix is,
1 0
0 1.
The 3 x 3 identity matrix is,
1 0 0
0 1 0
0 0 1.
The following matrix is 9 x 9.
5308 4065 4694 2592 1751 2662 7944 5922 1597
6609 4238 9432 9669 1468 8115 7879 3786 1372
2505 5011 2674 4944 5419 2192 6279 2336 9325
7378 9350 8358 2812 1795 1529 3892 6398 6734
6491 8240 3404 7458 9466 6131 8633 9578 6359
7125 1864 9810 6618 8660 4980 3110 4440 3989
6012 7661 8770 9954 8542 3475 3530 3254 2724
9145 1203 9522 4394 8725 6311 5610 8768 7274
5222 3874 1450 6178 5111 8396 2220 8994 5827
The product of it and its inverse (see the script below), is not equal to the
9 x 9 identity matrix.
What is wrong?
DTB.
'~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~
'file = Lovecraft4.tbasicc
'~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~
Uses "console"
Uses "file"
Uses "math"
'~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~
Global filestring As String = "Lovecraft4.txt"
Global s1, s2, s3 As String
Global outfile As DWord
Global A(9, 9) As Ext
A(1, 1) = 5308, 4065, 4694, 2592, 1751, 2662, 7944, 5922, 1597
A(2, 1) = 6609, 4238, 9432, 9669, 1468, 8115, 7879, 3786, 1372
A(3, 1) = 2505, 5011, 2674, 4944, 5419, 2192, 6279, 2336, 9325
A(4, 1) = 7378, 9350, 8358, 2812, 1795, 1529, 3892, 6398, 6734
A(5, 1) = 6491, 8240, 3404, 7458, 9466, 6131, 8633, 9578, 6359
A(6, 1) = 7125, 1864, 9810, 6618, 8660, 4980, 3110, 4440, 3989
A(7, 1) = 6012, 7661, 8770, 9954, 8542, 3475, 3530, 3254, 2724
A(8, 1) = 9145, 1203, 9522, 4394, 8725, 6311, 5610, 8768, 7274
A(9, 1) = 5222, 3874, 1450, 6178, 5111, 8396, 2220, 8994, 5827
Global B(3, 3) As Ext
B(1, 1) = 1, 2, 3
B(2, 1) = 2, 3, 1
B(3, 1) = 3, 1, 2
Global Ai(9, 9) As Ext
Global Bi(3, 3) As Ext
Global I9(9, 9) As Ext
Global I3(3, 3) As Ext
'~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~
Function TBMain()
Local i, j As Integer
outfile = FILE_Open(filestring, "OUTPUT")
MAT Ai() = INV(A)
MAT I9() = A() * Ai()
MAT Bi() = INV(B)
MAT I3() = B() * Bi()
'------------------------------------------------------
writeoutput("Here is a 3 x 3 test matrix, B, to invert.")
writeoutput("")
For i = 1 To 3
s3 =""
For j = 1 To 3
s1 = Format$(B(i, j), "0")
If j < 3 Then
s2 = s1 & " "
Else
s2 = s1
EndIf
s3 = s3 & s2
Next
writeoutput(s3)
Next
writeoutput("")
'------------------------------------------------------
writeoutput("Here is the computed 3 x 3 identity matrix, I3.")
writeoutput("")
For i = 1 To 3
s3 =""
For j = 1 To 3
s1 = Format$(I3(i, j), "+000.00;-000.00")
If j < 3 Then
s2 = s1 & " "
Else
s2 = s1
EndIf
s3 = s3 & s2
Next
writeoutput(s3)
Next
writeoutput("")
'------------------------------------------------------
writeoutput("Here is the 9 x 9 matrix for this problem, A.")
writeoutput("")
For i = 1 To 9
s3 =""
For j = 1 To 9
s1 = Format$(A(i, j), "0000")
If j < 9 Then
s2 = s1 & " "
Else
s2 = s1
EndIf
s3 = s3 & s2
Next
writeoutput(s3)
Next
writeoutput("")
'------------------------------------------------------
writeoutput("Here is the computed 9 x 9 identity matrix, I9.")
writeoutput("")
For i = 1 To 9
s3 =""
For j = 1 To 9
s1 = Format$(I9(i, j), "+000.00;-000.00")
If j < 9 Then
s2 = s1 & " "
Else
s2 = s1
EndIf
s3 = s3 & s2
Next
writeoutput(s3)
Next
writeoutput("")
'------------------------------------------------------
writeoutput("What happened?")
'------------------------------------------------------
FILE_Close(outfile)
WaitKey
End Function
'~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~
Function writeoutput(s4 As String)
Console_WriteLine(s4)
FILE_LinePrint(outfile, s4)
End Function
'~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~
When a number n, and its inverse (reciprocal), 1/n, are multiplied together, the
product is 1.
Likewise, when a square matrix, A, of dimension m x m, and its inverse, A^-1,
are multiplied together, the product is the m x m identity matrix, I; for which
the values on the main diagonal (the diagonal extending from upper left to lower
right) are 1, and the values not on the main diagonal are 0.
For instance, the 2 x 2 identity matrix is,
1 0
0 1.
The 3 x 3 identity matrix is,
1 0 0
0 1 0
0 0 1.
The following matrix is 9 x 9.
5308 4065 4694 2592 1751 2662 7944 5922 1597
6609 4238 9432 9669 1468 8115 7879 3786 1372
2505 5011 2674 4944 5419 2192 6279 2336 9325
7378 9350 8358 2812 1795 1529 3892 6398 6734
6491 8240 3404 7458 9466 6131 8633 9578 6359
7125 1864 9810 6618 8660 4980 3110 4440 3989
6012 7661 8770 9954 8542 3475 3530 3254 2724
9145 1203 9522 4394 8725 6311 5610 8768 7274
5222 3874 1450 6178 5111 8396 2220 8994 5827
The product of it and its inverse (see the script below), is not equal to the
9 x 9 identity matrix.
What is wrong?
DTB.
'~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~
'file = Lovecraft4.tbasicc
'~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~
Uses "console"
Uses "file"
Uses "math"
'~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~
Global filestring As String = "Lovecraft4.txt"
Global s1, s2, s3 As String
Global outfile As DWord
Global A(9, 9) As Ext
A(1, 1) = 5308, 4065, 4694, 2592, 1751, 2662, 7944, 5922, 1597
A(2, 1) = 6609, 4238, 9432, 9669, 1468, 8115, 7879, 3786, 1372
A(3, 1) = 2505, 5011, 2674, 4944, 5419, 2192, 6279, 2336, 9325
A(4, 1) = 7378, 9350, 8358, 2812, 1795, 1529, 3892, 6398, 6734
A(5, 1) = 6491, 8240, 3404, 7458, 9466, 6131, 8633, 9578, 6359
A(6, 1) = 7125, 1864, 9810, 6618, 8660, 4980, 3110, 4440, 3989
A(7, 1) = 6012, 7661, 8770, 9954, 8542, 3475, 3530, 3254, 2724
A(8, 1) = 9145, 1203, 9522, 4394, 8725, 6311, 5610, 8768, 7274
A(9, 1) = 5222, 3874, 1450, 6178, 5111, 8396, 2220, 8994, 5827
Global B(3, 3) As Ext
B(1, 1) = 1, 2, 3
B(2, 1) = 2, 3, 1
B(3, 1) = 3, 1, 2
Global Ai(9, 9) As Ext
Global Bi(3, 3) As Ext
Global I9(9, 9) As Ext
Global I3(3, 3) As Ext
'~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~
Function TBMain()
Local i, j As Integer
outfile = FILE_Open(filestring, "OUTPUT")
MAT Ai() = INV(A)
MAT I9() = A() * Ai()
MAT Bi() = INV(B)
MAT I3() = B() * Bi()
'------------------------------------------------------
writeoutput("Here is a 3 x 3 test matrix, B, to invert.")
writeoutput("")
For i = 1 To 3
s3 =""
For j = 1 To 3
s1 = Format$(B(i, j), "0")
If j < 3 Then
s2 = s1 & " "
Else
s2 = s1
EndIf
s3 = s3 & s2
Next
writeoutput(s3)
Next
writeoutput("")
'------------------------------------------------------
writeoutput("Here is the computed 3 x 3 identity matrix, I3.")
writeoutput("")
For i = 1 To 3
s3 =""
For j = 1 To 3
s1 = Format$(I3(i, j), "+000.00;-000.00")
If j < 3 Then
s2 = s1 & " "
Else
s2 = s1
EndIf
s3 = s3 & s2
Next
writeoutput(s3)
Next
writeoutput("")
'------------------------------------------------------
writeoutput("Here is the 9 x 9 matrix for this problem, A.")
writeoutput("")
For i = 1 To 9
s3 =""
For j = 1 To 9
s1 = Format$(A(i, j), "0000")
If j < 9 Then
s2 = s1 & " "
Else
s2 = s1
EndIf
s3 = s3 & s2
Next
writeoutput(s3)
Next
writeoutput("")
'------------------------------------------------------
writeoutput("Here is the computed 9 x 9 identity matrix, I9.")
writeoutput("")
For i = 1 To 9
s3 =""
For j = 1 To 9
s1 = Format$(I9(i, j), "+000.00;-000.00")
If j < 9 Then
s2 = s1 & " "
Else
s2 = s1
EndIf
s3 = s3 & s2
Next
writeoutput(s3)
Next
writeoutput("")
'------------------------------------------------------
writeoutput("What happened?")
'------------------------------------------------------
FILE_Close(outfile)
WaitKey
End Function
'~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~
Function writeoutput(s4 As String)
Console_WriteLine(s4)
FILE_LinePrint(outfile, s4)
End Function
'~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~