Velocity Reviews - Computer Hardware Reviews

Velocity Reviews > Newsgroups > Programming > Ruby > Matrix

Reply
Thread Tools

Matrix

 
 
v.srikrishnan@gmail.com
Guest
Posts: n/a
 
      09-10-2006
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

 
Reply With Quote
 
 
 
 
dblack@wobblini.net
Guest
Posts: n/a
 
      09-10-2006
Hi --

On Mon, 11 Sep 2006, http://www.velocityreviews.com/forums/(E-Mail Removed) 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

--
David A. Black | (E-Mail Removed)
Author of "Ruby for Rails" [1] | Ruby/Rails training & consultancy [3]
DABlog (DAB's Weblog) [2] | Co-director, Ruby Central, Inc. [4]
[1] http://www.manning.com/black | [3] http://www.rubypowerandlight.com
[2] http://dablog.rubypal.com | [4] http://www.rubycentral.org

 
Reply With Quote
 
 
 
 
M. Edward (Ed) Borasky
Guest
Posts: n/a
 
      09-10-2006
(E-Mail Removed) wrote:
> Hi --
>
> On Mon, 11 Sep 2006, (E-Mail Removed) 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
>

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

 
Reply With Quote
 
Just Another Victim of the Ambient Morality
Guest
Posts: n/a
 
      09-11-2006

"M. Edward (Ed) Borasky" <(E-Mail Removed)> wrote in message
news:(E-Mail Removed)...
> (E-Mail Removed) wrote:
>> Hi --
>>
>> On Mon, 11 Sep 2006, (E-Mail Removed) 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.
>>

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


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

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!
def []= (i, j, v)
@rows[i][j] = v
end
end





 
Reply With Quote
 
Dave Burt
Guest
Posts: n/a
 
      09-11-2006
Just Another Victim of the Ambient Morality wrote:
> "M. Edward (Ed) Borasky" <(E-Mail Removed)> 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.

>
> 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
def []= ...

Cheers,
Dave
 
Reply With Quote
 
MonkeeSage
Guest
Posts: n/a
 
      09-11-2006
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

 
Reply With Quote
 
William Crawford
Guest
Posts: n/a
 
      09-11-2006
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/.

 
Reply With Quote
 
M. Edward (Ed) Borasky
Guest
Posts: n/a
 
      09-11-2006
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.


 
Reply With Quote
 
v.srikrishnan@gmail.com
Guest
Posts: n/a
 
      09-11-2006
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:
> (E-Mail Removed) 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|
> b.each do |i|
> print b[i]
> end
> puts ""
> end
>
> Output:
>
> .........
> .........
> .........
> .........
> ....*....
> .........
> .........
> .........
> .........
>
> --
> Paul Lutus
> http://www.arachnoid.com


 
Reply With Quote
 
Dave Burt
Guest
Posts: n/a
 
      09-11-2006
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
 
Reply With Quote
 
 
 
Reply

Thread Tools

Posting Rules
You may not post new threads
You may not post replies
You may not post attachments
You may not edit your posts

BB code is On
Smilies are On
[IMG] code is On
HTML code is Off
Trackbacks are On
Pingbacks are On
Refbacks are Off


Similar Threads
Thread Thread Starter Forum Replies Last Post
Re: Matrix operations on character matrix element? Robert Kern Python 0 04-02-2009 12:27 AM
Re: Matrix operations on character matrix element? Terry Reedy Python 0 04-02-2009 12:12 AM
Matrix*Vector and Vector*Matrix Holgerson C++ 3 10-26-2007 07:38 AM
Matrix composed by two matrix lvcargnini VHDL 3 07-05-2006 07:21 AM
Re: Matrix DTS and Matrix 2 DTS? PeterTHX DVD Video 0 08-03-2003 05:46 AM



Advertisments