[lnkForumImage]
TotalShareware - Download Free Software

Confronta i prezzi di migliaia di prodotti.
Asp Forum
 Home | Login | Register | Search 


 

Forums >

comp.lang.ruby

Trouble with Matrix::inverse

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
7 Answers

ts

10/28/2003 5:01:00 PM

0

>>>>> "r" == rbovet <rbovet@digitalglobe.com> writes:

r> #!/usr/bin/env ruby
r> require 'matrix'

add this line

require 'mathn'

r> c=Matrix[[1,2],[2,1]]

svg% cat b.rb
#!/usr/bin/ruby
require 'matrix'
require 'mathn'

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
svg%

svg% b.rb
c is Matrix[[1, 2], [2, 1]]
d is Matrix[[-1/3, 2/3], [2/3, -1/3]]
product is Matrix[[1, 0], [0, 1]]
svg%


--

Guy Decoux

Lyle Johnson

10/28/2003 5:27:00 PM

0

rbovet wrote:

> I humbly request that somebody illuminate me as to the error(s) of my
> ways.

Umm, me too. I just worked it out by hand and [[-1, 1], [0, -1]] is not
the inverse of the original matrix. The inverse of [[1, 2], [2, 1]]
should be [[-1/3, 2/3], [2/3, -1/3]].

Lyle Johnson

10/28/2003 6:04:00 PM

0

ts wrote:

> add this line
>
> require 'mathn'

<snip>

Thanks for the reminder, Guy. It was using integer math since the
elements of the matrix are integers.

Ray, as an alternative to Guy's suggestion, you could also do this:

c = Matrix[ [1.0, 2.0], [2.0, 1.0]]
d = c.inverse
p1 = c*d

and get the expected results (using floating point arithmetic instead).

rbovet

10/31/2003 10:20:00 PM

0

Lyle Johnson <lyle@users.sourceforge.net> wrote in message news:<3F9EAFA1.8020705@users.sourceforge.net>...
> ts wrote:
>
> > add this line
> >
> > require 'mathn'
>
> <snip>
>
> Thanks for the reminder, Guy. It was using integer math since the
> elements of the matrix are integers.
>
> Ray, as an alternative to Guy's suggestion, you could also do this:
>
> c = Matrix[ [1.0, 2.0], [2.0, 1.0]]
> d = c.inverse
> p1 = c*d
>
> and get the expected results (using floating point arithmetic instead).


Lyle and Guy,

Thank you both for your rapid responses! It's a delight to get such
fast and informative replies to a question. Putting the values into a
floating point format made clear to me what the problem was, but the
real gem was to find out about the mathn class, which is really cool!
It makes it much more "human-like" in its mathematical prowess.

Ray

The Phantom Piper

7/24/2009 8:02:00 PM

0

On Jul 24, 12:56 pm, "La N" <nilita2004NOS...@yahoo.com> wrote:
>
> I believe Cory and I were in agreement that an
> Israeli kibbutz is an example of true communism.

Except for the Theocratic element, of course...


Defending Semitism, Not Zionism,

The Phantom Piper

Cory Bhreckan

7/24/2009 9:23:00 PM

0

The Phantom Piper wrote:
> On Jul 24, 12:56 pm, "La N" <nilita2004NOS...@yahoo.com> wrote:
>> I believe Cory and I were in agreement that an
>> Israeli kibbutz is an example of true communism.
>
> Except for the Theocratic element, of course...
>
>
> Defending Semitism, Not Zionism,
>
> The Phantom Piper

The point was that *true* communism works very nicely if your population
is 150 people or fewer, few enough so that everyone knows or knows of
each other.

--
"For the stronger we our houses do build,
The less chance we have of being killed." - William Topaz McGonagall

The Phantom Piper

7/25/2009 1:32:00 AM

0

On Jul 24, 2:23 pm, Cory Bhreckan <corybhreckan@nospam_verizon.net>
wrote:
>
> The point was that *true* communism works very nicely
> if your population is 150 people or fewer, few enough so
> that everyone knows or knows of each other.

And a fair point it is. It's when the "caretaker government"
refuses to give up their Mercedes sedans and Black Sea
Dachas that you know it's not actually Communism...


Socially Democratic,

The Phantom Piper