Forum: Ruby Matrix class for ruby? -example

Announcement (2017-05-07): www.ruby-forum.com is now read-only since I unfortunately do not have the time to support and maintain the forum any more. Please see rubyonrails.org/community and ruby-lang.org/en/community for other Rails- und Ruby-related community platforms.
A2cf2d2cd16c5030bff8c123b843fd89?d=identicon&s=25 seepee (Guest)
on 2006-12-21 13:40
(Received via mailing list)
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

??
461b1da26b6b198ef014235928576d81?d=identicon&s=25 Jakub Hegenbart (Guest)
on 2006-12-21 14:06
(Received via mailing list)
>> 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
Ea24c17719a975fb38c107a60f4b3802?d=identicon&s=25 Vincent Fourmond (Guest)
on 2006-12-21 14:22
(Received via mailing list)
seepee wrote:
> 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
3327593586837f472a611fc08836506b?d=identicon&s=25 Julien Gaugaz (Guest)
on 2006-12-21 14:30
(Received via mailing list)
seepee wrote:
>>
>         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
993d0f1436f1af2fa2a0de3188eb1d72?d=identicon&s=25 Jose Augusto (Guest)
on 2006-12-22 15:39
(Received via mailing list)
Hi,

Just a small correction... it is the number of *lines* from the left
matrix
that must
match the number of *columns* from the right matrix.

Kind Regards

J. Augusto
A4a4095ff08bd0fced3c3fddbeac743a?d=identicon&s=25 Cameron McBride (Guest)
on 2006-12-22 18:10
(Received via mailing list)
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
This topic is locked and can not be replied to.