rbovet
10/28/2003 4:46:00 PM
I decided to use Ruby to write a simple program to help my daughter
with her math homework, which involves solving systems of linear
equations using matrices. It's been a long time since I learned all
this stuff, but I think I still remember the basics of it. But I keep
getting answers that don't make sense to me when I use the
Matrix::inverse function. Can somebody help me out?
Here's a simple program:
#!/usr/bin/env ruby
require 'matrix'
c=Matrix[[1,2],[2,1]]
d=c.inverse
p1=c*d
puts "c is " +c.to_s
puts "d is " +d.to_s
puts "product is "+p1.to_s
And here are the results:
c is Matrix[[1, 2], [2, 1]]
d is Matrix[[-1, 1], [0, -1]]
product is Matrix[[-1, -1], [-2, 1]]
The problem is that any matrix multiplied by its inverse is supposed
to yield the appropriate identity matrix. In this case, the product
is definitely NOT the identity matrix.
I get the same results under Ruby 1.6.7, 1.6.8, and 1.8.0.
I humbly request that somebody illuminate me as to the error(s) of my
ways.
Thanks!
Ray