[lnkForumImage]
TotalShareware - Download Free Software

Confronta i prezzi di migliaia di prodotti.
Asp Forum
 Home | Login | Register | Search 


 

v.srikrishnan@gmail.com

9/10/2006 6:09:00 PM

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

48 Answers

dblack

9/10/2006 6:29:00 PM

0

Paul Lutus

9/10/2006 6:45:00 PM

0

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|
b.each do |i|
print b[i]
end
puts ""
end

Output:

..........
..........
..........
..........
.....*....
..........
..........
..........
..........

--
Paul Lutus
http://www.ara...

M. Edward (Ed) Borasky

9/10/2006 11:56:00 PM

0

dblack@wobblini.net wrote:
> 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
>
You can only set an arbitrary element of a Matrix when it is created.
Once created, you can't change any of the entries.

Just Another Victim of the Ambient Morality

9/11/2006 2:38:00 AM

0


"M. Edward (Ed) Borasky" <znmeb@cesmail.net> wrote in message
news:4504A5A6.9050900@cesmail.net...
> dblack@wobblini.net wrote:
>> 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.
>>
> 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





Paul Lutus

9/11/2006 3:00:00 AM

0

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

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

--
Paul Lutus
http://www.ara...

Dave Burt

9/11/2006 3:01:00 AM

0

Just Another Victim of the Ambient Morality wrote:
> "M. Edward (Ed) Borasky" <znmeb@cesmail.net> 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

MonkeeSage

9/11/2006 5:50:00 AM

0

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

Paul Lutus

9/11/2006 10:21:00 AM

0

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

William Crawford

9/11/2006 10:49:00 AM

0

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

M. Edward (Ed) Borasky

9/11/2006 12:38:00 PM

0

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.