[lnkForumImage]
TotalShareware - Download Free Software

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


 

Forums >

comp.lang.ruby

matrix problem

Bu Mihai

7/12/2007 1:30:00 PM

i have a matrix :remember=Matrix[ [] ]
and this line remember[0,0]=3
why do i have this error at that line: undefined method `[]=' for
Matrix[[]]:Matrix (NoMethodError)

and how do i set values for my matrix only with one row

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

2 Answers

Stefan Rusterholz

7/12/2007 2:07:00 PM

0

Bulhac Mihai wrote:
> i have a matrix :remember=Matrix[ [] ]
> and this line remember[0,0]=3
> why do i have this error at that line: undefined method `[]=' for
> Matrix[[]]:Matrix (NoMethodError)
>
> and how do i set values for my matrix only with one row

Matrix is immutable, so there is no method []= to assign values.
[]= is the method called in remember[0,0]=3, it is just syntax sugar for
remember.[]=(0,0,3).

Maybe you want nested arrays instead.

Regards
Stefan

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

Todd Benson

7/12/2007 2:18:00 PM

0

On 7/12/07, Bulhac Mihai <mihai.bulhac@yahoo.com> wrote:
> i have a matrix :remember=Matrix[ [] ]
> and this line remember[0,0]=3
> why do i have this error at that line: undefined method `[]=' for
> Matrix[[]]:Matrix (NoMethodError)
>
> and how do i set values for my matrix only with one row

Hmm. It appears Matrix objects are supposed to be immutable. Use
arrays instead and convert to Matrix for matrix-type functions, so ...

irb> require 'matrix'
=> true
irb> require 'mathn' #need this for correct determinant calculations
=> true
irb> a = [[1, 2], [3, 4]]
=> [[1, 2], [3, 4]]
irb> a[0][0] = 3
=> [[3, 2], [3, 4]]
irb> m = Matrix[*a]
=> Matrix[[3, 2], [3, 4]]
irb> m.transpose
=> Matrix[[3, 3], [2, 4]]
irb> m * m
=> Matrix[[15, 14], [21, 22]]
irb> m_float = m.map {|i| i.to_f}
=> Matrix[[3.0, 2.0], [3.0, 4.0]]
irb> m_inv = m_float.inverse
=> Matrix[[0.666666666666667, -0.333333333333333], [-0.5, 0.5]]
irb> m.to_a
=> [[0.666666666666667, -0.333333333333333], [-0.5, 0.5]]

hth,
Todd