# Matrix class for ruby? -example

**URL:** <https://www.ruby-forum.com/t/matrix-class-for-ruby-example/80113>\
**Category:** Ruby\
**Created:** [December 21, 2006, 1:40pm UTC](https://www.ruby-forum.com/t/matrix-class-for-ruby-example/80113 "2006-12-21T13:40:20Z")\
**Posts on this page:** 6\
**Page:** 1

<div class="post-metadata">

**Author:** ![seepee](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/s/bbe5ce/32.png) [@seepee](https://www.ruby-forum.com/u/seepee)\
**Post date:** [December 21, 2006, 1:40pm UTC](https://www.ruby-forum.com/t/matrix-class-for-ruby-example/80113/1 "2006-12-21T13:40:20Z")

</div>

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

??

---

<div class="post-metadata">

**Author:** ![seepee](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/s/bbe5ce/32.png) [@seepee](https://www.ruby-forum.com/u/seepee)\
**Post date:** [December 21, 2006, 2:06pm UTC](https://www.ruby-forum.com/t/matrix-class-for-ruby-example/80113/2 "2006-12-21T14:06:22Z")

</div>

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

---

<div class="post-metadata">

**Author:** ![seepee](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/s/bbe5ce/32.png) [@seepee](https://www.ruby-forum.com/u/seepee)\
**Post date:** [December 21, 2006, 2:22pm UTC](https://www.ruby-forum.com/t/matrix-class-for-ruby-example/80113/3 "2006-12-21T14:22:23Z")

</div>

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

---

<div class="post-metadata">

**Author:** ![seepee](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/s/bbe5ce/32.png) [@seepee](https://www.ruby-forum.com/u/seepee)\
**Post date:** [December 21, 2006, 2:30pm UTC](https://www.ruby-forum.com/t/matrix-class-for-ruby-example/80113/4 "2006-12-21T14:30:24Z")

</div>

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

---

<div class="post-metadata">

**Author:** ![seepee](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/s/bbe5ce/32.png) [@seepee](https://www.ruby-forum.com/u/seepee)\
**Post date:** [December 22, 2006, 3:39pm UTC](https://www.ruby-forum.com/t/matrix-class-for-ruby-example/80113/5 "2006-12-22T15:39:40Z")

</div>

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

---

<div class="post-metadata">

**Author:** ![seepee](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/s/bbe5ce/32.png) [@seepee](https://www.ruby-forum.com/u/seepee)\
**Post date:** [December 22, 2006, 6:10pm UTC](https://www.ruby-forum.com/t/matrix-class-for-ruby-example/80113/6 "2006-12-22T18:10:01Z")

</div>

On 12/21/06, seepee [removed\_email\_address@domain.invalid](mailto:removed_email_address@domain.invalid) 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
