Matrix class: How to set a single element?

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-forum.com/.

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

···

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

http://www.ruby-lang.org/ja/man/html/Matrix.html#Matrix.23.5b.5d.3d

Harry

···

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-forum.com/\.

--
A Look into Japanese Ruby List in English

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-forum.com/.

http://www.ruby-lang.org/ja/man/html/Matrix.html#Matrix.23.5b.5d.3d
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-forum.com/\.

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

···

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.

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

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

···

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.23.5b.5d.3d
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)

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-forum.com/\.

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-forum.com/\.

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

Marcio Braga wrote:

Todd Benson 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?!).

···

On Mon, Aug 4, 2008 at 10:24 PM, Marcio Braga <mbraga0001@gmail.com> >> wrote:

--
Posted via http://www.ruby-forum.com/\.