# Matrix class for ruby?

**URL:** <https://www.ruby-forum.com/t/matrix-class-for-ruby/80114>\
**Category:** Ruby\
**Created:** [December 21, 2006, 1:42pm UTC](https://www.ruby-forum.com/t/matrix-class-for-ruby/80114 "2006-12-21T13:42:20Z")\
**Posts on this page:** 5\
**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:42pm UTC](https://www.ruby-forum.com/t/matrix-class-for-ruby/80114/1 "2006-12-21T13:42:20Z")

</div>

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

---

<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:50pm UTC](https://www.ruby-forum.com/t/matrix-class-for-ruby/80114/2 "2006-12-21T13:50:22Z")

</div>

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

---

<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:26pm UTC](https://www.ruby-forum.com/t/matrix-class-for-ruby/80114/3 "2006-12-21T14:26:24Z")

</div>

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

---

<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, 4:30pm UTC](https://www.ruby-forum.com/t/matrix-class-for-ruby/80114/4 "2006-12-21T16:30:35Z")

</div>

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

> 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

---

<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, 4:54pm UTC](https://www.ruby-forum.com/t/matrix-class-for-ruby/80114/5 "2006-12-21T16:54:39Z")

</div>

Dan Uznanski wrote:

> > 
> 
> 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.blogspot.com/](http://borasky-research.blogspot.com/)

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