PDA

View Full Version : Project Lovecraft Problem 4



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

'~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~

jack
08-03-2010, 04:12
Maple tells me that the inverse is a singular matrix, but I could have made a mistake.

ErosOlmi
08-03-2010, 08:40
Because this thread has not been posted into "Bug Forum" I suppose ... it is not a thinBasic bug, isn't it ? :read:

danbaron
08-03-2010, 08:50
jack, who is stronger, Maple or Mathematica?

Good news Eros, no bug, no bug!

jack
09-03-2010, 04:13
something is wrong with the forum or my computer, when I click on Show unread posts since last visit.
it shows no unread posts, hence I missed your question, which is better Maple or Mathematica?
that's a though question and one I am not qualified to answer, I have Maple 11 and Mathematica 4 but have a preference for the procedural programming of Maple.

Michael Hartlef
09-03-2010, 06:53
Hi jack, yes the show unread posts function does not work relyable. I hope that Eros will switch the forum software soon, so this should be gone.

danbaron
10-03-2010, 09:11
The dimension of a square n x n matrix, is n.

The rank, r, of a square matrix of dimension n, is the number of linear independent row
vectors, or the number of linear independent column vectors.

For a square matrix of dimension n, the number of linearly independent row
vectors, equals the number of linearly independent column vectors.

For a square matrix, "A", of dimension n, the inverse of A, A^-1, if it exists, is
the square matrix of dimension n, such that,

A * A^-1 = I,

where "I" is the square n x n identity matrix; having main diagonal (the diagonal
extending from the top left to lower right) entries equal to 1, and all other
entries equal to 0.

For example, the 4 x 4 identity matrix is,

1 0 0 0
0 1 0 0
0 0 1 0
0 0 0 1.

For a square matrix, A, of dimension n, if r < n, then there is no matrix A^-1
such that, A * A^-1 = I.

When r is less than n, then the square matrix of dimension n, is called,
"singular".

For the problem's square matrix, say, "P", of dimension 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 rank, r, is 8. Therefore, the matrix is singular, and there is no matrix,
P^-1, such that, P * P^-1 = I.

The rank, r, of P, equals 8, instead of 9, because, column vector 1, is a linear
combination of column vectors, 3 and 8. That is,

for i = 1 to 9,

P(i, 1) = (P(i, 3) + P(i, 8 ))/2.

(For a matrix, the first index indicates the row, and the second index indicates
the column.)

---------------------------------------------------------------------------------------

Just for fun, here is one more singular matrix, say "Q".

8444 8970 3086 1798 5806 5363 7244 5017 2366 6982 5104 3849 9971
8080 7832 4931 6388 1090 9904 3844 4894 4129 4720 7439 2169 4140
9161 1639 3793 6112 3388 6151 3943 1371 7987 8410 3921 1024 9874
6645 2527 8525 1972 3414 8435 4784 7930 4839 6369 6995 3388 7603
3677 4290 5801 2415 7226 2454 3956 7123 4666 1263 1937 3361 5835
3249 6422 4518 1519 3234 9178 2041 7757 6070 3202 8859 7958 7549
5450 8624 9949 8472 6975 2137 3437 3013 7550 2249 3751 9532 7745
5249 7928 5115 1285 5994 5487 5093 7217 7445 2481 6323 4675 2156
2919 3220 6894 2077 9107 1385 9429 6283 3721 2954 3611 5478 7780
5067 1865 1209 3453 3287 4762 7069 2571 2604 1208 8830 1025 7932
5935 6155 5413 3994 5108 1656 6334 8837 4328 2957 4429 2432 9806
1059 8482 5083 2431 4796 5612 6122 7468 3677 1836 9350 5504 3468
9642 1269 6463 1309 2065 7451 1581 2321 4184 1075 6428 4095 5987

It is a square matrix of dimension 13, and rank 12. Its rank is not 13, because,
column vector 11 is a linear combination of all of the other column vectors.
That is,

for i = 1 to 13,

Q(i, 11) = Q(i, 2) + Q(i, 3) + Q(i, 6) + Q(i, 7) + Q(i, 9) + Q(i, 13) - Q(i, 1) - Q(i, 4) - Q(i, 5) - Q(i, 8 ) - Q(i, 10) - Q(i, 12).

You can see that, generally, it is difficult to visually determine if a
matrix is singular.

DTB.


'~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~
'file = Lovecraft4a.tbasicc
'~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~
Uses "console"
Uses "file"
Uses "math"
'~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~

%n = 13

Global filestring As String = "Lovecraft4a.txt"
Global s1, s2, s3 As String
Global outfile As DWord

Global Q(%n, %n) As Ext

Q(01, 1) = 8444, 8970, 3086, 1798, 5806, 5363, 7244, 5017, 2366, 6982, 5104, 3849, 9971
Q(02, 1) = 8080, 7832, 4931, 6388, 1090, 9904, 3844, 4894, 4129, 4720, 7439, 2169, 4140
Q(03, 1) = 9161, 1639, 3793, 6112, 3388, 6151, 3943, 1371, 7987, 8410, 3921, 1024, 9874
Q(04, 1) = 6645, 2527, 8525, 1972, 3414, 8435, 4784, 7930, 4839, 6369, 6995, 3388, 7603
Q(05, 1) = 3677, 4290, 5801, 2415, 7226, 2454, 3956, 7123, 4666, 1263, 1937, 3361, 5835
Q(06, 1) = 3249, 6422, 4518, 1519, 3234, 9178, 2041, 7757, 6070, 3202, 8859, 7958, 7549
Q(07, 1) = 5450, 8624, 9949, 8472, 6975, 2137, 3437, 3013, 7550, 2249, 3751, 9532, 7745
Q(08, 1) = 5249, 7928, 5115, 1285, 5994, 5487, 5093, 7217, 7445, 2481, 6323, 4675, 2156
Q(09, 1) = 2919, 3220, 6894, 2077, 9107, 1385, 9429, 6283, 3721, 2954, 3611, 5478, 7780
Q(10, 1) = 5067, 1865, 1209, 3453, 3287, 4762, 7069, 2571, 2604, 1208, 8830, 1025, 7932
Q(11, 1) = 5935, 6155, 5413, 3994, 5108, 1656, 6334, 8837, 4328, 2957, 4429, 2432, 9806
Q(12, 1) = 1059, 8482, 5083, 2431, 4796, 5612, 6122, 7468, 3677, 1836, 9350, 5504, 3468
Q(13, 1) = 9642, 1269, 6463, 1309, 2065, 7451, 1581, 2321, 4184, 1075, 6428, 4095, 5987


Global Qi(%n, %n) As Ext
Global I(%n, %n) As Ext

'~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~

Function TBMain()
Local j, k As Integer

outfile = FILE_Open(filestring, "OUTPUT")

MAT Qi() = INV(Q)
MAT I() = Q() * Qi()

'------------------------------------------------------

writeoutput("Here is the 13 x 13 matrix for this problem, Q.")
writeoutput("")

For j = 1 To %n
s3 =""
For k = 1 To %n
s1 = Format$(Q(j, k), "0000")
If k < %n Then
s2 = s1 & " "
Else
s2 = s1
EndIf
s3 = s3 & s2
Next
writeoutput(s3)
Next
writeoutput("")

'------------------------------------------------------

writeoutput("Here is the computed 13 x 13 identity matrix, I.")
writeoutput("")

For j = 1 To %n
s3 =""
For k = 1 To %n
s1 = Format$(I(j, k), "+000.000;-000.000")
If k < %n Then
s2 = s1 & " "
Else
s2 = s1
EndIf
s3 = s3 & s2
Next
writeoutput(s3)
Next
writeoutput("")

'------------------------------------------------------

FILE_Close(outfile)

Console_writeline("Done. Press a key.")

WaitKey

End Function

'~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~

Function writeoutput(s4 As String)
FILE_LinePrint(outfile, s4)
End Function

'~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~