Ask a Question related to Ruby, Design and Development.
-
rbovet #1
Trouble with Matrix::inverse
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
rbovet Guest
-
inverse of distribute into layers
hi everybody.i am trying to edit a flash template with flash cs4.the problem is that there are some pictures in there that are hidden ( dont ask how... -
Inverse of model(s)UnderLoc?
I'd really like a function that returns the point in the image plane to which a point on a 3D object will be projected. For instance, given a point... -
Is there an inverse of ParagraphFormat
Hi, I'm trying to build a forum and want to allow users to edit their message boxes (textarea boxes). I use the ParagraphFormat whenever I... -
Math::MatrixReal->{inverse,det}() complexity?
Hi folks, Does anyone know anything about the complexity of the methods ->inverse() and ->det() available in Math::MatrixReal? I ran a... -
Camera control become inverse :(
Hi there! I would like to use camera motion to interact with my 3D model instead of using drag Model fonction. My script work just fine but it... -
ts #2
Re: Trouble with Matrix::inverse
>>>>> "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
ts Guest
-
Lyle Johnson #3
Re: Trouble with Matrix::inverse
rbovet wrote:
Umm, me too. I just worked it out by hand and [[-1, 1], [0, -1]] is not> I humbly request that somebody illuminate me as to the error(s) of my
> ways.
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 Guest
-
Lyle Johnson #4
Re: Trouble with Matrix::inverse
ts wrote:
<snip>> add this line
>
> require 'mathn'
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 Johnson Guest
-
rbovet #5
Re: Trouble with Matrix::inverse
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
rbovet Guest



Reply With Quote

