# Matrices, Gtk et al

**URL:** <https://www.ruby-forum.com/t/matrices-gtk-et-al/55714>\
**Category:** Ruby\
**Created:** [March 23, 2006, 5:34am UTC](https://www.ruby-forum.com/t/matrices-gtk-et-al/55714 "2006-03-23T05:34:00Z")\
**Posts on this page:** 4\
**Page:** 1

<div class="post-metadata">

**Author:** ![Robert\_Love](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/r/d26b3c/32.png) [@Robert\_Love](https://www.ruby-forum.com/u/Robert_Love)\
**Post date:** [March 23, 2006, 5:34am UTC](https://www.ruby-forum.com/t/matrices-gtk-et-al/55714/1 "2006-03-23T05:34:00Z")

</div>

I have something working but it sure seems clumsy and I’m hoping for  
pointers to make it better. My app is a ruby & Gtk thing to do some  
calculations. I want to have a standard 3x3 matrix of floating point  
numbers as well some 3x1 vectors. I’m using a 9 element array today for  
the 3x3. I also have a series of 9 Gtk.Entry fields that is even more  
clumsy than the floating point numbers.

How do I build a function that, when called by the Calc button callback,  
fetches the contents of the 9 numbers and returns it as a 3x3 matrix?  
From what I’ve read, ruby doesn’t have real two dimensional arrays. So  
am I stuck doing everything in a manner that doesn’t map well to my math  
problem domain?

All advice appreciated.

---

<div class="post-metadata">

**Author:** ![Robert\_Love](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/r/d26b3c/32.png) [@Robert\_Love](https://www.ruby-forum.com/u/Robert_Love)\
**Post date:** [March 23, 2006, 11:17pm UTC](https://www.ruby-forum.com/t/matrices-gtk-et-al/55714/2 "2006-03-23T23:17:48Z")

</div>

Robert Love wrote:

> am I stuck doing everything in a manner that doesn’t map well to my math  
> problem domain?
> 
> All advice appreciated

Try checking out the Matrix standard library:

[http://ruby-doc.org/stdlib/libdoc/matrix/rdoc/index.html](http://ruby-doc.org/stdlib/libdoc/matrix/rdoc/index.html)

-Justin

---

<div class="post-metadata">

**Author:** ![Robert\_Love](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/r/d26b3c/32.png) [@Robert\_Love](https://www.ruby-forum.com/u/Robert_Love)\
**Post date:** [March 24, 2006, 5:14am UTC](https://www.ruby-forum.com/t/matrices-gtk-et-al/55714/3 "2006-03-24T05:14:07Z")

</div>

In [removed\_email\_address@domain.invalid](mailto:removed_email_address@domain.invalid) Justin C. wrote:

> > 3x3 matrix? From what I’ve read, ruby doesn’t have real two  
> > dimensional arrays. So am I stuck doing everything in a manner that  
> > doesn’t map well to my math problem domain?
> > 
> > All advice appreciated
> 
> Try checking out the Matrix standard library:
> 
> [http://ruby-doc.org/stdlib/libdoc/matrix/rdoc/index.html](http://ruby-doc.org/stdlib/libdoc/matrix/rdoc/index.html)

Thanks, I will look at. I assume that it is for floating numbers only?  
I can’t have a 3x3 matrix of Gtk.Entry fields so a user can input this?

---

<div class="post-metadata">

**Author:** ![Robert\_Love](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/r/d26b3c/32.png) [@Robert\_Love](https://www.ruby-forum.com/u/Robert_Love)\
**Post date:** [March 24, 2006, 5:52am UTC](https://www.ruby-forum.com/t/matrices-gtk-et-al/55714/4 "2006-03-24T05:52:05Z")

</div>

> How do I build a function that, when called by the Calc button callback,  
> fetches the contents of the 9 numbers and returns it as a 3x3 matrix?  
> From what I’ve read, ruby doesn’t have real two dimensional arrays. So  
> am I stuck doing everything in a manner that doesn’t map well to my math  
> problem domain?

You can do 2d arrays in Ruby as an array of arrays. (Very much like  
many other languages.)

There’s also the enumerator module that can help build a 3x3 array out  
of a 9-element sequence. In irb:

> require ‘enumerator’  
> =\> true

> a = (1…9).to\_a  
> =\> [1, 2, 3, 4, 5, 6, 7, 8, 9]

> b = []  
> =\> []

> a.each\_slice(3) { |x| b \<\< x }  
> =\> nil

> b  
> =\> [[1, 2, 3], [4, 5, 6], [7, 8, 9]]

> b[1][2]  
> =\> 6

You can use Matrix to store things other than Float, but most of the  
methods on Matrix only work with and are intended for Float. So  
putting things other than Float into Matrix turns it into an array of  
arrays; might as well skip Matrix.
