[lnkForumImage]
TotalShareware - Download Free Software

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


 

Forums >

comp.lang.ruby

Matrix class: How to set a single element ?

Marcio Braga

8/5/2008 1:19:00 AM

simple code:

require 'matrix'
m1 = Matrix[[1,2],[3,4]]

Now I need to change element (0,0) from 1 to 5. I expected something
like the command below should work:

m1[0][0] = 5

... but the error message is '[]': wrong number of arguments (1 for 2)
(ArgumentError).

Could someone help me on that ?
Thank you
Marcio
--
Posted via http://www.ruby-....

10 Answers

M. Edward (Ed) Borasky

8/5/2008 1:37:00 AM

0

On Tue, 2008-08-05 at 10:18 +0900, Marcio Braga wrote:
> simple code:
>
> require 'matrix'
> m1 = Matrix[[1,2],[3,4]]
>
> Now I need to change element (0,0) from 1 to 5. I expected something
> like the command below should work:
>
> m1[0][0] = 5
>
> ... but the error message is '[]': wrong number of arguments (1 for 2)
> (ArgumentError).
>
> Could someone help me on that ?
> Thank you
> Marcio
IIRC a Matrix (and a Vector) is an immutable object -- you can't change
elements individually. You have to copy the Matrix to a
(two-dimensional) Array, change the Array element, and then convert the
Array back to a Matrix.

This is the only violation I've encountered of the rule that you can
write FORTRAN programs in any language. :)
--
M. Edward (Ed) Borasky
ruby-perspectives.blogspot.com

"A mathematician is a machine for turning coffee into theorems." --
Alfréd Rényi via Paul ErdÅ?s


Marcio Braga

8/5/2008 2:12:00 AM

0

Thank you.

So, I need something ugly like this:

require 'matrix'
m1=Matrix[[1,2],[3,4]]
m2=*m1
m2[0][0]=5
m1=Matrix[*m2]

uh ... Any clue why such limitation ?
Thank you.
--
Posted via http://www.ruby-....

M. Edward (Ed) Borasky

8/5/2008 3:00:00 AM

0

On Tue, 2008-08-05 at 11:12 +0900, Marcio Braga wrote:
> Thank you.
>
> So, I need something ugly like this:
>
> require 'matrix'
> m1=Matrix[[1,2],[3,4]]
> m2=*m1
> m2[0][0]=5
> m1=Matrix[*m2]
>
> uh ... Any clue why such limitation ?
> Thank you.
Someone explained it to me once, and when I complained about it, they
said it wasn't going to get changed. But, if you're dealing with
floating point matrices, you're going to use C or FORTRAN anyhow. If you
must have Ruby, just build an interface layer to C or FORTRAN
libraries. ;)
--
M. Edward (Ed) Borasky
ruby-perspectives.blogspot.com

"A mathematician is a machine for turning coffee into theorems." --
Alfréd Rényi via Paul ErdÅ?s


Harry Kakueki

8/5/2008 3:10:00 AM

0

On Tue, Aug 5, 2008 at 10:18 AM, Marcio Braga <mbraga0001@gmail.com> wrote:
> simple code:
>
> require 'matrix'
> m1 = Matrix[[1,2],[3,4]]
>
> Now I need to change element (0,0) from 1 to 5. I expected something
> like the command below should work:
>
> m1[0][0] = 5
>
> ... but the error message is '[]': wrong number of arguments (1 for 2)
> (ArgumentError).
>
> Could someone help me on that ?
> Thank you
> Marcio
> --
> Posted via http://www.ruby-....
>
>

http://www.ruby-lang.org/ja/man/html/Matrix.html#Matrix.2...

Harry

--
A Look into Japanese Ruby List in English
http://www.kakueki.com/ruby...

Marcio Braga

8/5/2008 3:25:00 AM

0

> http://www.ruby-lang.org/ja/man/html/Matrix.html#Matrix.2...
> Harry

The example of that page:

m=Matrix[[11,12],[21,22]]
m[1,1]=-1

... gives me the following error message:

undefined method '[]=' for Matrix[[11, 12], [21, 22]]:Matrix
(NoMethodError)


--
Posted via http://www.ruby-....

Todd Benson

8/5/2008 3:31:00 AM

0

On Mon, Aug 4, 2008 at 10:24 PM, Marcio Braga <mbraga0001@gmail.com> wrote:
>> http://www.ruby-lang.org/ja/man/html/Matrix.html#Matrix.2...
>> Harry
>
> The example of that page:
>
> m=Matrix[[11,12],[21,22]]
> m[1,1]=-1
>
> ... gives me the following error message:
>
> undefined method '[]=' for Matrix[[11, 12], [21, 22]]:Matrix
> (NoMethodError)

You have to open the class and define the method as in the example...

class Matrix
def []=(i, j, x)
@rows[i][j] = x
end
end

Todd

Marcio Braga

8/5/2008 3:41:00 AM

0

M. Edward (Ed) Borasky wrote:
> ... But, if you're dealing with
> floating point matrices, you're going to use C or FORTRAN anyhow.

Hum, Ruby (with just the standard array class) seems to work well with
my floating point matrices (3D obj models in OpenGL). And with the
extended Matrix class I'm (was) trying to make the code more elegant and
easy to understand. (no plans to learn C)

--
Posted via http://www.ruby-....

Marcio Braga

8/5/2008 3:46:00 AM

0

Todd Benson wrote:
> On Mon, Aug 4, 2008 at 10:24 PM, Marcio Braga <mbraga0001@gmail.com>
> wrote:
>> undefined method '[]=' for Matrix[[11, 12], [21, 22]]:Matrix
>> (NoMethodError)
>
> You have to open the class and define the method as in the example...
>
> class Matrix
> def []=(i, j, x)
> @rows[i][j] = x
> end
> end
>
> Todd

Perfect! Thank you.

Quick comment: Why this method is not already pre-defined in the Matrix
class ?
--
Posted via http://www.ruby-....

mdiam

8/5/2008 11:46:00 AM

0

Redirect to fortan just because you need some float usage
is a bad idea : it's a true argument to make switching
to piton...

But yes, if you have to manipulate matrix with billion
of elements, then you should do some fortan (... ruby binding) !

Some month ago I also had such a need.
As mentioned above, I had to reopen the Matrix class and
add several methods:

class Matrix
def dump(firstLine = "")
str = ""
if firstLine != ""
str << firstLine << "\n"
end
for i in 0...self.row_size
space = ""
for j in 0...self.column_size
str << space << self[i,j].to_s
space = " "
end
str << "\n"
end
return str
end
# La classe Matrix est immutable, or je veux pouvoir écrire :
# m[i,j] = v
#
def []=(i, j, v)
@rows[i][j] = v
end
# Il n'y a même pas de constructeur pour une matrice
rectangulaire : bouhhh
# Le prefixe "self." permet de déclarer une méthode de classe
def self.create(nbRows, nbCols, value)
return Matrix.rows(Array.new(nbRows, Array.new(nbCols,
value)))
end
end

The Podman

8/5/2008 1:34:00 PM

0

Marcio Braga wrote:
> Todd Benson wrote:
>> On Mon, Aug 4, 2008 at 10:24 PM, Marcio Braga <mbraga0001@gmail.com>
>> wrote:
>>> undefined method '[]=' for Matrix[[11, 12], [21, 22]]:Matrix
>>> (NoMethodError)
>>
>> You have to open the class and define the method as in the example...
>>
>> class Matrix
>> def []=(i, j, x)
>> @rows[i][j] = x
>> end
>> end
>>
>> Todd
>
> Perfect! Thank you.
>
> Quick comment: Why this method is not already pre-defined in the Matrix
> class ?

Matrix and Vector are, in my opinion, pretty poor classes. You can't
even divide a Vector by a scalar quantity (you have to v*1/a instead.
:/). There's a lot of common things they don't support and lots of the
methods do things in strange ways (at least they look strange to me..).
The initilization of Vectors is pretty odd. Lots of pointless method
hopping (along with passing a symbol....why?!).
--
Posted via http://www.ruby-....