[lnkForumImage]
TotalShareware - Download Free Software

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


 

Forums >

comp.lang.ruby

Matrix class for ruby?

seepee

12/21/2006 7:13:00 AM

Does anyone know of a functional Matrix math class for ruby that could
multiply, add and substract 3x2 or larger matrices.

The matrix.rb that is in Ruby API cannot multiply anything larger than 2x2
matrices, which makes it useless for 2D or 3D graphics (for instance).

SP
9 Answers

seepee

12/21/2006 7:18:00 AM

0

On Thu, 21 Dec 2006 09:12:47 +0200, seepee wrote:

> Does anyone know of a functional Matrix math class for ruby that could
> multiply, add and substract 3x2 or larger matrices.
>
> The matrix.rb that is in Ruby API cannot multiply anything larger than 2x2
> matrices, which makes it useless for 2D or 3D graphics (for instance).
>
> SP

Problem:
go to terminal, and type "irb" or "irb1.8"

>require 'matrix' #or require >rubylibrarypath/matrix.rb
>p Matrix[ [25, 93, 33], [0, -1, 66] ] * Matrix[ [1, 53, 33], [1, -21, 77]

ExceptionForMatrix::ErrDimensionMismatch: Matrix dimension mismatch
from /usr/lib/ruby/1.8/matrix.rb:466:in `*' from (irb):4

??

Jakub Hegenbart

12/21/2006 12:46:00 PM

0

>> require 'matrix' #or require >rubylibrarypath/matrix.rb
>> p Matrix[ [25, 93, 33], [0, -1, 66] ] * Matrix[ [1, 53, 33], [1, -21, 77]
>
> ExceptionForMatrix::ErrDimensionMismatch: Matrix dimension mismatch
> from /usr/lib/ruby/1.8/matrix.rb:466:in `*' from (irb):4
>
> ??

How exactly would you multiply such two matrices?

Jakub

Nicolas Desprès

12/21/2006 12:50:00 PM

0

On 12/21/06, seepee <see@pee.org> wrote:
> Does anyone know of a functional Matrix math class for ruby that could
> multiply, add and substract 3x2 or larger matrices.
>
> The matrix.rb that is in Ruby API cannot multiply anything larger than 2x2
> matrices, which makes it useless for 2D or 3D graphics (for instance).
>

Are you sure?

$ irb
irb(main):001:0> Matrix[[1,2,3],[1,2,3],[1,2,3]] *
Matrix[[1,2,3],[1,2,3],[1,2,3]]
=> Matrix[[6, 12, 18], [6, 12, 18], [6, 12, 18]]
irb(main):002:0> VERSION
=> 1.8.4
$

--
Nicolas Desprès

Vincent Fourmond

12/21/2006 1:22:00 PM

0

seepee wrote:
> On Thu, 21 Dec 2006 09:12:47 +0200, seepee wrote:
>
>> Does anyone know of a functional Matrix math class for ruby that could
>> multiply, add and substract 3x2 or larger matrices.
>>
>> The matrix.rb that is in Ruby API cannot multiply anything larger than 2x2
>> matrices, which makes it useless for 2D or 3D graphics (for instance).
>>
>> SP
>
> Problem:
> go to terminal, and type "irb" or "irb1.8"
>
>> require 'matrix' #or require >rubylibrarypath/matrix.rb
>> p Matrix[ [25, 93, 33], [0, -1, 66] ] * Matrix[ [1, 53, 33], [1, -21, 77]

Err... Have you done matrix computations for a long time ? You need
that the number of *columns* from the left matrix matches the number of
*lines* from the right matrix. If you try to multiply two 3x2 matrices,
it is rather encouraging that Matrix refuses it...

Try transposing your matrix first, if that is what you need. Are you
dealing with transformation matrices (which are not real matrices) ?

Vincent

--
Vincent Fourmond, PhD student
http://vincent.fourmon...

Vincent Fourmond

12/21/2006 1:24:00 PM

0

seepee wrote:
> Does anyone know of a functional Matrix math class for ruby that could
> multiply, add and substract 3x2 or larger matrices.
>
> The matrix.rb that is in Ruby API cannot multiply anything larger than 2x2
> matrices, which makes it useless for 2D or 3D graphics (for instance).

OK, I understand now: you want graphical transformations. They are
*not* real matrices, but the combination of a real matrix and a
translation vector. So you need special rules to combine them (although
for additions and substractions, standard matrix multiplications should
work). I don't know any package doing that in Ruby -- but if you
consider writing one, that would definitely be an interesting addition.

Cheers !

Vince

--
Vincent Fourmond, PhD student
http://vincent.fourmon...

Julien Gaugaz

12/21/2006 1:29:00 PM

0

seepee wrote:
> On Thu, 21 Dec 2006 09:12:47 +0200, seepee wrote:
>
>
>> Does anyone know of a functional Matrix math class for ruby that could
>> multiply, add and substract 3x2 or larger matrices.
>>
>> The matrix.rb that is in Ruby API cannot multiply anything larger than 2x2
>> matrices, which makes it useless for 2D or 3D graphics (for instance).
>>
>> SP
>>
>
> Problem:
> go to terminal, and type "irb" or "irb1.8"
>
>
>> require 'matrix' #or require >rubylibrarypath/matrix.rb
>> p Matrix[ [25, 93, 33], [0, -1, 66] ] * Matrix[ [1, 53, 33], [1, -21, 77]
>>
>
> ExceptionForMatrix::ErrDimensionMismatch: Matrix dimension mismatch
> from /usr/lib/ruby/1.8/matrix.rb:466:in `*' from (irb):4
>
> ??
>
Yes, it's because you cannot multiply a 2x3 matrix with another 2x3
matrix.... try:

Matrix[ [25, 93, 33], [0, -1, 66] ] * Matrix[ [1, 53, 33], [1, -21, 77]].transpose


Julien


Dan Uznanski

12/21/2006 3:30:00 PM

0


On Dec 21, 2006, at 6:24 AM, Vincent Fourmond wrote:

> seepee wrote:
>> Does anyone know of a functional Matrix math class for ruby that could
>> multiply, add and substract 3x2 or larger matrices.
>>
>> The matrix.rb that is in Ruby API cannot multiply anything larger
>> than 2x2
>> matrices, which makes it useless for 2D or 3D graphics (for instance).
>
> OK, I understand now: you want graphical transformations. They are
> *not* real matrices, but the combination of a real matrix and a
> translation vector. So you need special rules to combine them (although
> for additions and substractions, standard matrix multiplications should
> work). I don't know any package doing that in Ruby -- but if you
> consider writing one, that would definitely be an interesting addition.
>
An RST matrix has an implicit 0 0 0 1 row along the bottom. Nothing
more complicated than that.

Actually when dealing with projection matrices, this 'implicit' bottom
row gets used to define the perspective value of the camera.

That said, matrix.rb is very shoddy - it's missing several important
vector operations (like, oh, -Vector and Vector/Numeric), and it uses
Vector*Matrix instead of the correct Matrix*Vector.

Dan


M. Edward (Ed) Borasky

12/21/2006 3:52:00 PM

0

Dan Uznanski wrote:
>
> On Dec 21, 2006, at 6:24 AM, Vincent Fourmond wrote:
>
>> seepee wrote:
>>> Does anyone know of a functional Matrix math class for ruby that could
>>> multiply, add and substract 3x2 or larger matrices.
>>>
>>> The matrix.rb that is in Ruby API cannot multiply anything larger
>>> than 2x2
>>> matrices, which makes it useless for 2D or 3D graphics (for instance).
>>
>> OK, I understand now: you want graphical transformations. They are
>> *not* real matrices, but the combination of a real matrix and a
>> translation vector. So you need special rules to combine them (although
>> for additions and substractions, standard matrix multiplications should
>> work). I don't know any package doing that in Ruby -- but if you
>> consider writing one, that would definitely be an interesting addition.
>>
> An RST matrix has an implicit 0 0 0 1 row along the bottom. Nothing
> more complicated than that.
>
> Actually when dealing with projection matrices, this 'implicit' bottom
> row gets used to define the perspective value of the camera.
>
> That said, matrix.rb is very shoddy - it's missing several important
> vector operations (like, oh, -Vector and Vector/Numeric), and it uses
> Vector*Matrix instead of the correct Matrix*Vector.
>
> Dan
>
>
>
I wouldn't call it "shoddy". My biggest complaint with Matrix is that a
Matrix is immutable -- if you want to set elements, you have to do so in
an Array and then create a new Matrix from the Array.

The whole rational / complex / matrix / mathn collection looks to me
like an attempt to provide "high-school algebra" constructs in Ruby that
work the way you'd expect them to work. For example, you can get the
*exact* inverse of a non-singular matrix with rational elements, and I
suppose, though I haven't tried it, the same thing for a non-singular
matrix with complex rational elements. For *small* one-off calculations
and calculations where high speed isn't required, they do just that.

However, if what you want to do is, say, lots of graphical
transformations at high speed using floating point arithmetic, like you
might in a video game, you want NArray.

--
M. Edward (Ed) Borasky, FBG, AB, PTA, PGS, MS, MNLP, NST, ACMC(P)
http://borasky-research.blo...

If God had meant for carrots to be eaten cooked, He would have given rabbits fire.


Cameron McBride

12/22/2006 5:08:00 PM

0

On 12/21/06, seepee <see@pee.org> wrote:
> On Thu, 21 Dec 2006 09:12:47 +0200, seepee wrote:
>
> > Does anyone know of a functional Matrix math class for ruby that could
> > multiply, add and substract 3x2 or larger matrices.
> >
> > The matrix.rb that is in Ruby API cannot multiply anything larger than 2x2
> > matrices, which makes it useless for 2D or 3D graphics (for instance).
> >
> > SP

others in this thread have corrected the logical problem.

but let me point out a typical additional response - the included
Matrix library in ruby is NOT FAST. There are other libs that do this
on the C level, such as NArray, that will allow much better
performance for some graphical uses.

Cameron