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.
···
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
--
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
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.
···
On Tue, 2008-08-05 at 11:12 +0900, Marcio Braga 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)
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
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?!).
···
On Mon, Aug 4, 2008 at 10:24 PM, Marcio Braga <mbraga0001@gmail.com> >> wrote: