# Matrix

**URL:** <https://rubytalk.org/t/matrix/30863>\
**Category:** ruby-talk\
**Created:** [10 September 2006 18:10 UTC](https://rubytalk.org/t/matrix/30863 "2006-09-10T18:10:25Z")\
**Posts on this page:** 20\
**Page:** 1

<div class="post-metadata">

**Author:** ![Srikrishnan\_Iyer](https://avatars.discourse-cdn.com/v4/letter/s/eada6e/32.png) [@Srikrishnan\_Iyer](https://rubytalk.org/u/Srikrishnan_Iyer)\
**Post date:** [10 September 2006 18:10 UTC](https://rubytalk.org/t/matrix/30863/1 "2006-09-10T18:10:25Z")

</div>

Hi all,  
a) Is there a way to create a non-square matrix in ruby?  
b) How does one set any arbitrary element of an array?

I searched for these bu there was a post about 3 years back, if  
anything has been done it would be better to use that rather than  
something i should write!  
Thanks  
krishnan

---

<div class="post-metadata">

**Author:** ![David\_A\_Black3](https://avatars.discourse-cdn.com/v4/letter/d/6a8cbe/32.png) [@David\_A\_Black3](https://rubytalk.org/u/David_A_Black3)\
**Post date:** [10 September 2006 18:29 UTC](https://rubytalk.org/t/matrix/30863/2 "2006-09-10T18:29:14Z")

</div>

Hi --

> **···**
>
> On Mon, 11 Sep 2006, v.srikrishnan@gmail.com wrote:
> 
> > Hi all,  
> > a) Is there a way to create a non-square matrix in ruby?  
> > b) How does one set any arbitrary element of an array?
> > 
> > I searched for these bu there was a post about 3 years back, if  
> > anything has been done it would be better to use that rather than  
> > something i should write!
> 
> Check out the Matrix class -- look for matrix.rb in the Ruby  
> distribution.
> 
> David
> 
> --  
> &nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;David A. Black | dblack@wobblini.net  
> Author of "Ruby for Rails" [1] | Ruby/Rails training & consultancy [3]  
> DABlog (DAB's Weblog) [2] | Co-director, Ruby Central, Inc. [4]  
> [1] [Ruby for Rails](http://www.manning.com/black) | [3] [http://www.rubypowerandlight.com](http://www.rubypowerandlight.com)  
> [2] [http://dablog.rubypal.com](http://dablog.rubypal.com) | [4] [http://www.rubycentral.org](http://www.rubycentral.org)

---

<div class="post-metadata">

**Author:** ![Paul\_Lutus](https://avatars.discourse-cdn.com/v4/letter/p/2bfe46/32.png) [@Paul\_Lutus](https://rubytalk.org/u/Paul_Lutus)\
**Post date:** [10 September 2006 18:50 UTC](https://rubytalk.org/t/matrix/30863/3 "2006-09-10T18:50:41Z")

</div>

> Hi all,  
> a) Is there a way to create a non-square matrix in ruby?

Another poster has answered this one.

> b) How does one set any arbitrary element of an array?

Like this:

> **···**
>
> v.srikrishnan@gmail.com wrote:
> 
> -------------------------------------------
> 
> #! /usr/bin/ruby
> 
> size = 9
> 
> # create, preset value
> 
> array = Array.new(size) { Array.new(size) {"."} }
> 
> # change one value
> 
> array[4][4] = '\*'
> 
> # show it
> 
> array.each do |b|  
> &nbsp;&nbsp;&nbsp;b.each do |i|  
> &nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;print b[i]  
> &nbsp;&nbsp;&nbsp;end  
> &nbsp;&nbsp;&nbsp;puts ""  
> end
> 
> Output:
> 
> .........  
> .........  
> .........  
> .........  
> ....\*....  
> .........  
> .........  
> .........  
> .........
> 
> --  
> Paul Lutus  
> [http://www.arachnoid.com](http://www.arachnoid.com)

---

<div class="post-metadata">

**Author:** ![M\_Edward\_Ed\_Borasky1](https://avatars.discourse-cdn.com/v4/letter/m/e9c0ed/32.png) [@M\_Edward\_Ed\_Borasky1](https://rubytalk.org/u/M_Edward_Ed_Borasky1)\
**Post date:** [10 September 2006 23:55 UTC](https://rubytalk.org/t/matrix/30863/4 "2006-09-10T23:55:52Z")

</div>

dblack@wobblini.net wrote:

> Hi --
> 
> > Hi all,  
> > a) Is there a way to create a non-square matrix in ruby?  
> > b) How does one set any arbitrary element of an array?
> > 
> > I searched for these bu there was a post about 3 years back, if  
> > anything has been done it would be better to use that rather than  
> > something i should write!
> 
> Check out the Matrix class -- look for matrix.rb in the Ruby  
> distribution.
> 
> David

You can only set an arbitrary element of a Matrix when it is created.  
Once created, you can't change any of the entries.

> **···**
>
> > On Mon, 11 Sep 2006, v.srikrishnan@gmail.com wrote:

---

<div class="post-metadata">

**Author:** ![Srikrishnan\_Iyer](https://avatars.discourse-cdn.com/v4/letter/s/eada6e/32.png) [@Srikrishnan\_Iyer](https://rubytalk.org/u/Srikrishnan_Iyer)\
**Post date:** [11 September 2006 17:15 UTC](https://rubytalk.org/t/matrix/30863/5 "2006-09-11T17:15:32Z")

</div>

Thanks everyone. I had seen the same answers in the post i mentioned  
(2-3 yrs now i think). i was wondering why these small things had not  
been added. In my humble opinion having these small things in the basic  
Matrix class would be helpful to beginners. I do not say that the basic  
class should have numerical stuff like SVD, LU decomposition or other  
linear algebra stuff etc. but these things would help. Also,if someone  
wants to "freeze" a matrix i think ruby has an operation for it(am  
learning ruby).

Thanks again  
krishnan  
Paul Lutus wrote:

> **···**
>
> > v.srikrishnan@gmail.com wrote:
> > 
> > \> Hi all,  
> > \> a) Is there a way to create a non-square matrix in ruby?
> > 
> > Another poster has answered this one.
> > 
> > \> b) How does one set any arbitrary element of an array?
> > 
> > Like this:
> > 
> > -------------------------------------------
> > 
> > #! /usr/bin/ruby
> > 
> > size = 9
> > 
> > # create, preset value
> > 
> > array = Array.new(size) { Array.new(size) {"."} }
> > 
> > # change one value
> > 
> > array[4][4] = '\*'
> > 
> > # show it
> > 
> > array.each do |b|  
> > &nbsp;&nbsp;&nbsp;b.each do |i|  
> > &nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;print b[i]  
> > &nbsp;&nbsp;&nbsp;end  
> > &nbsp;&nbsp;&nbsp;puts ""  
> > end
> > 
> > Output:
> > 
> > .........  
> > .........  
> > .........  
> > .........  
> > ....\*....  
> > .........  
> > .........  
> > .........  
> > .........
> > 
> > --  
> > Paul Lutus  
> > [http://www.arachnoid.com](http://www.arachnoid.com)

---

<div class="post-metadata">

**Author:** ![Just\_Another\_Victim2](https://avatars.discourse-cdn.com/v4/letter/j/958977/32.png) [@Just\_Another\_Victim2](https://rubytalk.org/u/Just_Another_Victim2)\
**Post date:** [11 September 2006 02:40 UTC](https://rubytalk.org/t/matrix/30863/6 "2006-09-11T02:40:23Z")

</div>

"M. Edward (Ed) Borasky" \<znmeb@cesmail.net\> wrote in message  
news:4504A5A6.9050900@cesmail.net...

> > Hi --
> > 
> > > Hi all,  
> > > a) Is there a way to create a non-square matrix in ruby?  
> > > b) How does one set any arbitrary element of an array?
> > > 
> > > I searched for these bu there was a post about 3 years back, if  
> > > anything has been done it would be better to use that rather than  
> > > something i should write!
> > 
> > Check out the Matrix class -- look for matrix.rb in the Ruby  
> > distribution.
> 
> You can only set an arbitrary element of a Matrix when it is created.  
> Once created, you can't change any of the entries.

&nbsp;&nbsp;&nbsp;&nbsp;The = operator does seem conspicuously missing. If anyone knows why  
this was (obviously) deliberately left out, please say something!

&nbsp;&nbsp;&nbsp;&nbsp;Of course, if this is useful to you, you can always add it, yourself!  
I think it would go something like this...

require 'matrix'

class Matrix # are for kids!  
&nbsp;&nbsp;&nbsp;&nbsp;def = (i, j, v)  
&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;@rows[i][j] = v  
&nbsp;&nbsp;&nbsp;&nbsp;end  
end

> **···**
>
> > dblack@wobblini.net wrote:
> > 
> > > On Mon, 11 Sep 2006, v.srikrishnan@gmail.com wrote:

---

<div class="post-metadata">

**Author:** ![Dave\_Burt2](https://yyz1.discourse-cdn.com/flex029/user_avatar/rubytalk.org/dave_burt2/32/2036_2.png) [@Dave\_Burt2](https://rubytalk.org/u/Dave_Burt2)\
**Post date:** [11 September 2006 02:55 UTC](https://rubytalk.org/t/matrix/30863/7 "2006-09-11T02:55:28Z")

</div>

Just Another Victim of the Ambient Morality wrote:

> > You can only set an arbitrary element of a Matrix when it is created.  
> > Once created, you can't change any of the entries.
> 
> &nbsp;&nbsp;&nbsp;&nbsp;The = operator does seem conspicuously missing. If anyone knows why  
> this was (obviously) deliberately left out, please say something!

It's by design. A Matrix is immutable, like Ruby's other numbers  
(Fixnum, Rational, Complex...)

Consider subclassing rather than modifying the base class for this reason.

class MutableMatrix \< Matrix  
&nbsp;&nbsp;def = ...

Cheers,  
Dave

> **···**
>
> > "M. Edward (Ed) Borasky" \<znmeb@cesmail.net\> wrote

---

<div class="post-metadata">

**Author:** ![Paul\_Lutus](https://avatars.discourse-cdn.com/v4/letter/p/2bfe46/32.png) [@Paul\_Lutus](https://rubytalk.org/u/Paul_Lutus)\
**Post date:** [11 September 2006 03:00 UTC](https://rubytalk.org/t/matrix/30863/8 "2006-09-11T03:00:23Z")

</div>

Just Another Victim of the Ambient Morality wrote:

/ ...

> > You can only set an arbitrary element of a Matrix when it is created.  
> > Once created, you can't change any of the entries.
> 
> &nbsp;&nbsp;&nbsp;&nbsp;The = operator does seem conspicuously missing. If anyone knows why  
> this was (obviously) deliberately left out, please say something!

Matrices are meant to be operated on as units, not by manipulating the  
contents of individual cells. Think of a matrix as a datatype, a complete,  
self-contained entity, one you can create or destroy in its entirety.

BTW, in answer to an earlier question:

> a) Is there a way to create a non-square matrix in ruby?

#! /usr/bin/ruby

require 'matrix'

m = Matrix[[1,2,3],[4,5,6],[7,8,9]]

nsqm = Matrix[[1,2,3],[7,8,9]]

puts m,nsqm

Matrix[[1, 2, 3], [4, 5, 6], [7, 8, 9]]  
Matrix[[1, 2, 3], [7, 8, 9]]

> Of course, if this is useful to you, you can always add it, yourself!  
> I think it would go something like this...
> 
> require 'matrix'
> 
> class Matrix # are for kids!  
> &nbsp;&nbsp;&nbsp;&nbsp;def = (i, j, v)  
> &nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;@rows[i][j] = v  
> &nbsp;&nbsp;&nbsp;&nbsp;end  
> end

Yes, this works as expected. Easy enough to add if you really want it. 🙂

> **···**
>
> --  
> Paul Lutus  
> [http://www.arachnoid.com](http://www.arachnoid.com)

---

<div class="post-metadata">

**Author:** ![Jordan\_Callicoat](https://avatars.discourse-cdn.com/v4/letter/j/9de053/32.png) [@Jordan\_Callicoat](https://rubytalk.org/u/Jordan_Callicoat)\
**Post date:** [11 September 2006 05:50 UTC](https://rubytalk.org/t/matrix/30863/9 "2006-09-11T05:50:29Z")

</div>

Dave Burt wrote:

> It's by design. A Matrix is immutable, like Ruby's other numbers  
> (Fixnum, Rational, Complex...)

Why should a Matrix be immutable? It's not a type like a Fixnum, it's a  
data structure, like an Array or Hash. What is gained by creating a new  
Matrix from a changing data stream, rather than updating an existing  
one?

Regards,  
Jordan

---

<div class="post-metadata">

**Author:** ![Paul\_Lutus](https://avatars.discourse-cdn.com/v4/letter/p/2bfe46/32.png) [@Paul\_Lutus](https://rubytalk.org/u/Paul_Lutus)\
**Post date:** [11 September 2006 10:25 UTC](https://rubytalk.org/t/matrix/30863/10 "2006-09-11T10:25:30Z")

</div>

MonkeeSage wrote:

> Dave Burt wrote:
> 
> > It's by design. A Matrix is immutable, like Ruby's other numbers  
> > (Fixnum, Rational, Complex...)
> 
> Why should a Matrix be immutable? It's not a type like a Fixnum, it's a  
> data structure, like an Array or Hash. What is gained by creating a new  
> Matrix from a changing data stream, rather than updating an existing  
> one?

In mathematics, matrices of various kinds are regarded as a unit, usually  
indivisible. For example, they're used a lot in computer graphics  
processing to hold rotation matrices.

The rotation matrices are loaded with specific values that accomplish  
rotations and translations, and until the computed viewpoint changes, the  
entire matrix is used to process all the image vertices.

Then, when the viewpoint changes, the entire matrix is recomputed, and a new  
immutable matrix is created. The point I am making is that the matrix can't  
function if it is thought of as nine scalar values rather than a 3x3 square  
matrix.

> **···**
>
> --  
> Paul Lutus  
> [http://www.arachnoid.com](http://www.arachnoid.com)

---

<div class="post-metadata">

**Author:** ![William\_Crawford](https://avatars.discourse-cdn.com/v4/letter/w/dbc845/32.png) [@William\_Crawford](https://rubytalk.org/u/William_Crawford)\
**Post date:** [11 September 2006 10:48 UTC](https://rubytalk.org/t/matrix/30863/11 "2006-09-11T10:48:47Z")

</div>

Paul Lutus wrote:

> The point I am making is that the matrix  
> can't  
> function if it is thought of as nine scalar values rather than a 3x3  
> square  
> matrix.

I think the point they are making is that not everyone uses matrixes for  
the same thing. I agree that for most applications, they should be  
treated as immutable. But they are saying that they not only think they  
should be modifyable, but that they need them to be modifyable to be  
useful to them under their circumstances.

> **···**
>
> --  
> Posted via [http://www.ruby-forum.com/\](http://www.ruby-forum.com/%5C).

---

<div class="post-metadata">

**Author:** ![M\_Edward\_Ed\_Borasky1](https://avatars.discourse-cdn.com/v4/letter/m/e9c0ed/32.png) [@M\_Edward\_Ed\_Borasky1](https://rubytalk.org/u/M_Edward_Ed_Borasky1)\
**Post date:** [11 September 2006 12:37 UTC](https://rubytalk.org/t/matrix/30863/12 "2006-09-11T12:37:50Z")

</div>

Paul Lutus wrote:

> MonkeeSage wrote:
> 
> > Dave Burt wrote:
> > 
> > > It's by design. A Matrix is immutable, like Ruby's other numbers  
> > > (Fixnum, Rational, Complex...)
> > 
> > Why should a Matrix be immutable? It's not a type like a Fixnum, it's a  
> > data structure, like an Array or Hash. What is gained by creating a new  
> > Matrix from a changing data stream, rather than updating an existing  
> > one?
> 
> In mathematics, matrices of various kinds are regarded as a unit, usually  
> indivisible. For example, they're used a lot in computer graphics  
> processing to hold rotation matrices.
> 
> The rotation matrices are loaded with specific values that accomplish  
> rotations and translations, and until the computed viewpoint changes, the  
> entire matrix is used to process all the image vertices.
> 
> Then, when the viewpoint changes, the entire matrix is recomputed, and a new  
> immutable matrix is created. The point I am making is that the matrix can't  
> function if it is thought of as nine scalar values rather than a 3x3 square  
> matrix.

The immutability of a Matrix violates Whoever's Law, which states

It is possible to write Fortran programs in any language.

\<ducking\>

Seriously, though, I agree with the poster who said some mathematicians  
want to be able to compute and change elements of a matrix. I happen to  
be one of those, and the subclass trick someone posted is one I'll need  
to use if I use the "Matrix" concept in my Ruby code. Given how slow it  
is, though, that seems unlikely.

---

<div class="post-metadata">

**Author:** ![Dave\_Burt2](https://yyz1.discourse-cdn.com/flex029/user_avatar/rubytalk.org/dave_burt2/32/2036_2.png) [@Dave\_Burt2](https://rubytalk.org/u/Dave_Burt2)\
**Post date:** [11 September 2006 21:35 UTC](https://rubytalk.org/t/matrix/30863/13 "2006-09-11T21:35:40Z")

</div>

William Crawford wrote:

> Paul Lutus wrote:
> 
> > The point I am making is that the matrix  
> > can't  
> > function if it is thought of as nine scalar values rather than a 3x3  
> > square  
> > matrix.
> 
> I think the point they are making is that not everyone uses matrixes for  
> the same thing. I agree that for most applications, they should be  
> treated as immutable. But they are saying that they not only think they  
> should be modifyable, but that they need them to be modifyable to be  
> useful to them under their circumstances.

If you want a matrix-like data structure (rather than a mathematical  
matrix), why not use an array of arrays?

Cheers, Dave

---

<div class="post-metadata">

**Author:** ![Jordan\_Callicoat](https://avatars.discourse-cdn.com/v4/letter/j/9de053/32.png) [@Jordan\_Callicoat](https://rubytalk.org/u/Jordan_Callicoat)\
**Post date:** [12 September 2006 01:35 UTC](https://rubytalk.org/t/matrix/30863/14 "2006-09-12T01:35:37Z")

</div>

Dave Burt wrote:

> If you want a matrix-like data structure (rather than a mathematical  
> matrix), why not use an array of arrays?

A mathematical matrix is just a dimensional arrangement of data into  
"rows" and "columns", thus allowing for certain algorithmic  
relationships to be defined and applied (e.g., canonical  
decompositions). Every matrix (i.e., the actual matrix considered as a  
complex value) is immutable and has a unique identity, just like every  
number -- that's true -- but they can also be permuted or otherwise  
transformed into different matrices. Why should one have to do that  
constructively (i.e., construct a new matrix "from scratch"), rather  
than destructively (i.e., change the existing matrix "in place"). Think  
of it like this: n = 1; n = n + 1 -- even though the number 1 is  
immutable and has a unique identity, the reference to it is not, and  
can thus be destructively altered by assignment with the result that it  
refers to new identity. It would be a pain to have to do o = n + 1, p =  
n + 2 (i.e., creating new references every time the data changes), just  
because 1 is immutable. Why should a matrix be any different? Mabye I'm  
missing something...

---

<div class="post-metadata">

**Author:** ![William\_Crawford](https://avatars.discourse-cdn.com/v4/letter/w/dbc845/32.png) [@William\_Crawford](https://rubytalk.org/u/William_Crawford)\
**Post date:** [12 September 2006 09:35 UTC](https://rubytalk.org/t/matrix/30863/15 "2006-09-12T09:35:19Z")

</div>

Dave Burt wrote:

> If you want a matrix-like data structure (rather than a mathematical  
> matrix), why not use an array of arrays?

Because then I'd have to write all the matrix functions to go along with  
them.

> **···**
>
> --  
> Posted via [http://www.ruby-forum.com/\](http://www.ruby-forum.com/%5C).

---

<div class="post-metadata">

**Author:** ![Rick\_DeNatale1](https://avatars.discourse-cdn.com/v4/letter/r/bbce88/32.png) [@Rick\_DeNatale1](https://rubytalk.org/u/Rick_DeNatale1)\
**Post date:** [12 September 2006 01:52 UTC](https://rubytalk.org/t/matrix/30863/16 "2006-09-12T01:52:28Z")

</div>

Yes, you're missing the distinction between a variable which  
references a sequence of objects over time, and the objects it  
references.

a = 1  
a = 2

Does NOT change 1 to 2.

Fortran II used to let you change 1 to 2:

subroutine a(arg)  
&nbsp;&nbsp;&nbsp;arg = 2  
end subroutine a

call a(1)

But this was considered a bug and was changed in Fortran IV.

> **···**
>
> On 9/11/06, MonkeeSage \<MonkeeSage@gmail.com\> wrote:
> 
> > Dave Burt wrote:  
> > \> If you want a matrix-like data structure (rather than a mathematical  
> > \> matrix), why not use an array of arrays?
> > 
> > A mathematical matrix is just a dimensional arrangement of data into  
> > "rows" and "columns", thus allowing for certain algorithmic  
> > relationships to be defined and applied (e.g., canonical  
> > decompositions). Every matrix (i.e., the actual matrix considered as a  
> > complex value) is immutable and has a unique identity, just like every  
> > number -- that's true -- but they can also be permuted or otherwise  
> > transformed into different matrices. Why should one have to do that  
> > constructively (i.e., construct a new matrix "from scratch"), rather  
> > than destructively (i.e., change the existing matrix "in place"). Think  
> > of it like this: n = 1; n = n + 1 -- even though the number 1 is  
> > immutable and has a unique identity, the reference to it is not, and  
> > can thus be destructively altered by assignment with the result that it  
> > refers to new identity. It would be a pain to have to do o = n + 1, p =  
> > n + 2 (i.e., creating new references every time the data changes), just  
> > because 1 is immutable. Why should a matrix be any different? Mabye I'm  
> > missing something...
> 
> --  
> Rick DeNatale
> 
> My blog on Ruby  
> [http://talklikeaduck.denhaven2.com/](http://talklikeaduck.denhaven2.com/)

---

<div class="post-metadata">

**Author:** ![Ara.T.Howard6](https://avatars.discourse-cdn.com/v4/letter/a/958977/32.png) [@Ara.T.Howard6](https://rubytalk.org/u/Ara.T.Howard6)\
**Post date:** [12 September 2006 01:58 UTC](https://rubytalk.org/t/matrix/30863/17 "2006-09-12T01:58:50Z")

</div>

[http://narray.rubyforge.org/](http://narray.rubyforge.org/)

and it even dovetails with

[http://rb-gsl.rubyforge.org/](http://rb-gsl.rubyforge.org/)

you can go back and forth.

-a

> **···**
>
> On Tue, 12 Sep 2006, MonkeeSage wrote:
> 
> > Dave Burt wrote:
> > 
> > > If you want a matrix-like data structure (rather than a mathematical  
> > > matrix), why not use an array of arrays?
> > 
> > A mathematical matrix is just a dimensional arrangement of data into  
> > "rows" and "columns", thus allowing for certain algorithmic  
> > relationships to be defined and applied (e.g., canonical  
> > decompositions). Every matrix (i.e., the actual matrix considered as a  
> > complex value) is immutable and has a unique identity, just like every  
> > number -- that's true -- but they can also be permuted or otherwise  
> > transformed into different matrices. Why should one have to do that  
> > constructively (i.e., construct a new matrix "from scratch"), rather  
> > than destructively (i.e., change the existing matrix "in place"). Think  
> > of it like this: n = 1; n = n + 1 -- even though the number 1 is  
> > immutable and has a unique identity, the reference to it is not, and  
> > can thus be destructively altered by assignment with the result that it  
> > refers to new identity. It would be a pain to have to do o = n + 1, p =  
> > n + 2 (i.e., creating new references every time the data changes), just  
> > because 1 is immutable. Why should a matrix be any different? Mabye I'm  
> > missing something...
> 
> --  
> in order to be effective truth must penetrate like an arrow - and that is  
> likely to hurt. -- wei wu wei

---

<div class="post-metadata">

**Author:** ![Just\_Another\_Victim2](https://avatars.discourse-cdn.com/v4/letter/j/958977/32.png) [@Just\_Another\_Victim2](https://rubytalk.org/u/Just_Another_Victim2)\
**Post date:** [12 September 2006 02:15 UTC](https://rubytalk.org/t/matrix/30863/18 "2006-09-12T02:15:29Z")

</div>

"MonkeeSage" \<MonkeeSage@gmail.com\> wrote in message  
news:1158024640.933843.82830@i42g2000cwa.googlegroups.com...

> Dave Burt wrote:
> 
> > If you want a matrix-like data structure (rather than a mathematical  
> > matrix), why not use an array of arrays?
> 
> A mathematical matrix is just a dimensional arrangement of data into  
> "rows" and "columns", thus allowing for certain algorithmic  
> relationships to be defined and applied (e.g., canonical  
> decompositions). Every matrix (i.e., the actual matrix considered as a  
> complex value) is immutable and has a unique identity, just like every  
> number -- that's true -- but they can also be permuted or otherwise  
> transformed into different matrices. Why should one have to do that  
> constructively (i.e., construct a new matrix "from scratch"), rather  
> than destructively (i.e., change the existing matrix "in place"). Think  
> of it like this: n = 1; n = n + 1 -- even though the number 1 is  
> immutable and has a unique identity, the reference to it is not, and  
> can thus be destructively altered by assignment with the result that it  
> refers to new identity. It would be a pain to have to do o = n + 1, p =  
> n + 2 (i.e., creating new references every time the data changes), just  
> because 1 is immutable. Why should a matrix be any different? Mabye I'm  
> missing something...

&nbsp;&nbsp;&nbsp;&nbsp;As far as I can tell, matrices are no different. Your example shows  
that 1 is immutable but references to it can be re-assigned, so you can do:

n = 3  
n = n + 1

&nbsp;&nbsp;&nbsp;&nbsp;Matrices are no different and, so, you can do:

n = Matrix.scalar(2, 3)  
n = n + Matrix.I(2)

&nbsp;&nbsp;&nbsp;&nbsp;In both cases, the "number" types are immutable but the references to  
them are not, as you say. They both work exactly the same way. What are  
you trying to say?

---

<div class="post-metadata">

**Author:** ![Dave\_Burt2](https://yyz1.discourse-cdn.com/flex029/user_avatar/rubytalk.org/dave_burt2/32/2036_2.png) [@Dave\_Burt2](https://rubytalk.org/u/Dave_Burt2)\
**Post date:** [12 September 2006 15:05 UTC](https://rubytalk.org/t/matrix/30863/19 "2006-09-12T15:05:39Z")

</div>

William Crawford wrote:

> Dave Burt wrote:
> 
> > If you want a matrix-like data structure (rather than a mathematical  
> > matrix), why not use an array of arrays?
> 
> Because then I'd have to write all the matrix functions to go along with  
> them.

Or NMatrix then:

[http://narray.rubyforge.org/](http://narray.rubyforge.org/)

Cheers,  
Dave

---

<div class="post-metadata">

**Author:** ![Ara.T.Howard6](https://avatars.discourse-cdn.com/v4/letter/a/958977/32.png) [@Ara.T.Howard6](https://rubytalk.org/u/Ara.T.Howard6)\
**Post date:** [12 September 2006 01:59 UTC](https://rubytalk.org/t/matrix/30863/20 "2006-09-12T01:59:51Z")

</div>

but wouldn't it make to days go quickly! nothing beats debuggin to burn up  
the clock... perhaps and RCR?

-a

> **···**
>
> On Tue, 12 Sep 2006, Rick DeNatale wrote:
> 
> > Yes, you're missing the distinction between a variable which  
> > references a sequence of objects over time, and the objects it  
> > references.
> > 
> > a = 1  
> > a = 2
> > 
> > Does NOT change 1 to 2.
> > 
> > Fortran II used to let you change 1 to 2:
> > 
> > subroutine a(arg)  
> > arg = 2  
> > end subroutine a
> > 
> > call a(1)
> > 
> > But this was considered a bug and was changed in Fortran IV.
> 
> --  
> in order to be effective truth must penetrate like an arrow - and that is  
> likely to hurt. -- wei wu wei

[Next page](https://rubytalk.org/t/matrix/30863.md?page=2)
