Velocity Reviews - Computer Hardware Reviews

Velocity Reviews > Newsgroups > Programming > Ruby > Matrix: Need help to understand this behavior

Reply
Thread Tools

Matrix: Need help to understand this behavior

 
 
Marcio Braga
Guest
Posts: n/a
 
      08-17-2008
a=[1]
b=a # make matrix "b" equal matrix "a", but expected 2
separated matrices
b[0]=2*b[0] # I want to change matrix "b" (and not matrix "a")
p a # print matrix "a" and see the "issue"

The expected value in the matrix "a" should be 1, but instead it is 2.

Why ?

Thank you.
Marcio
--
Posted via http://www.ruby-forum.com/.

 
Reply With Quote
 
 
 
 
Frantisek ZACEK
Guest
Posts: n/a
 
      08-17-2008
On Sun, Aug 17, 2008 at 3:55 PM, Marcio Braga <(E-Mail Removed)> wrote:
> a=[1]
> b=a # make matrix "b" equal matrix "a", but expected 2
> separated matrices
> b[0]=2*b[0] # I want to change matrix "b" (and not matrix "a")
> p a # print matrix "a" and see the "issue"
>
> The expected value in the matrix "a" should be 1, but instead it is 2.
>
> Why ?
>
> Thank you.
> Marcio



Typical Ruby behavior.

In Ruby, variables reference objects rather than contain values.
Therefore, when you write b = a, b references the same object as a does.
Therefore, when you modify b, you modify a.
See the obejct_ids in your code:

$>cat mat.rb
#!/usr/bin/env ruby -wKU

a = [1]
b = a
puts a.object_id
puts b.object_id
b[0] = 2 * b[0]
p a
p b
$>ruby mat.rb
284610
284610
[2]
[2]
$>

See ? same objects.

To do what you want, you need to duplicate your array rather than just
assign it :
$>cat dup_mat.rb
#!/usr/bin/env ruby -wKU

a = [1]
b = a.dup
puts a.object_id
puts b.object_id
b[0] = 2 * b[0]
p a
p b
$>ruby dup_mat.rb
284540
284530
[1]
[2]
$>

Hope it helped.

--
Frantisek ZACEK (zacek_f) -- SRS 2008

 
Reply With Quote
 
 
 
 
Marcio Braga
Guest
Posts: n/a
 
      08-17-2008
Just to complement, as a contrast, the code below works as expected, or
in other words, the content of matrix "a" is not changed when you change
the content of matrix "b".

b=[]
a=[1]
b[0]=a[0] # explicitly set a single element and not the entire
matrix
b[0]=2*b[0]
p a

Marcio


Marcio Braga wrote:
> a=[1]
> b=a # make matrix "b" equal matrix "a", but expected 2
> separated matrices
> b[0]=2*b[0] # I want to change matrix "b" (and not matrix "a")
> p a # print matrix "a" and see the "issue"
>
> The expected value in the matrix "a" should be 1, but instead it is 2.
>
> Why ?
>
> Thank you.
> Marcio


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

 
Reply With Quote
 
Marcio Braga
Guest
Posts: n/a
 
      08-17-2008
Thank you Zacek. I understand your point and that help very much.

Have a nice day !!
Marcio


Frantisek ZACEK wrote:
> On Sun, Aug 17, 2008 at 3:55 PM, Marcio Braga <(E-Mail Removed)>
> wrote:
>> Thank you.
>> Marcio

>
>
> Typical Ruby behavior.
>
> .....
>
> Hope it helped.

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

 
Reply With Quote
 
Frantisek ZACEK
Guest
Posts: n/a
 
      08-17-2008
On Sun, Aug 17, 2008 at 4:22 PM, Marcio Braga <(E-Mail Removed)> wrote:
> Just to complement, as a contrast, the code below works as expected, or
> in other words, the content of matrix "a" is not changed when you change
> the content of matrix "b".
>
> b=[]
> a=[1]
> b[0]=a[0] # explicitly set a single element and not the entire
> matrix
> b[0]=2*b[0]
> p a
>
> Marcio
>
>


Here you create explicitely two objects therefore : no problem whereas
on the first snippet, you only have one.


--
Frantisek ZACEK (zacek_f) -- SRS 2008

 
Reply With Quote
 
Marcio Braga
Guest
Posts: n/a
 
      08-17-2008
Zacek and all: how to duplicate a matrix that has elements that are also
matrix ?
For example:

a = [[1]]
b = a.dup
puts a.object_id
puts b.object_id
puts a[0].object_id # the same object
puts b[0].object_id # the same object
b[0][0] = 2 * b[0][0]
p a
p b

So, matrix "a" and "b" are different objects but each internal matrix
element continue to be the same object. Do I need to duplicate each one
?

Thank you
Marcio
--
Posted via http://www.ruby-forum.com/.

 
Reply With Quote
 
Frantisek ZACEK
Guest
Posts: n/a
 
      08-17-2008
On Sun, Aug 17, 2008 at 4:53 PM, Marcio Braga <(E-Mail Removed)> wrote:
> Zacek and all: how to duplicate a matrix that has elements that are also
> matrix ?
> For example:
>
> a = [[1]]
> b = a.dup
> puts a.object_id
> puts b.object_id
> puts a[0].object_id # the same object
> puts b[0].object_id # the same object
> b[0][0] = 2 * b[0][0]
> p a
> p b
>
> So, matrix "a" and "b" are different objects but each internal matrix
> element continue to be the same object. Do I need to duplicate each one
> ?
>
> Thank you
> Marcio
> --
> Posted via http://www.ruby-forum.com/.
>
>



I don't think there is a simple way to achieve that.
I would suggest:
a=[[1]]
b=a.dup.colllect { |e| e.dup }

--
Frantisek ZACEK (zacek_f) -- SRS 2008

 
Reply With Quote
 
Todd Benson
Guest
Posts: n/a
 
      08-17-2008
On Sun, Aug 17, 2008 at 9:53 AM, Marcio Braga <(E-Mail Removed)> wrote:
> Zacek and all: how to duplicate a matrix that has elements that are also
> matrix ?
> For example:
>
> a = [[1]]
> b = a.dup
> puts a.object_id
> puts b.object_id
> puts a[0].object_id # the same object
> puts b[0].object_id # the same object
> b[0][0] = 2 * b[0][0]
> p a
> p b
>
> So, matrix "a" and "b" are different objects but each internal matrix
> element continue to be the same object. Do I need to duplicate each one
> ?


It's a Matrix, right? (some of this stolen from a previous thread)...

require 'matrix'
class Matrix
def []=(i, j, n)
@rows[i][j] = n
end
end

m = Matrix[['a', 'b'], ['c', 'd']]
k = m.clone
k[0, 0] = 'e'
puts k.to_a
puts m.to_a

Todd

 
Reply With Quote
 
Marcio Braga
Guest
Posts: n/a
 
      08-18-2008
> I don't think there is a simple way to achieve that.
> I would suggest:
> a=[[1]]
> b=a.dup.colllect { |e| e.dup }


Ok.
So I need to create "my own" function to do that (just in case
a=[[[1]]])

Thank you
--
Posted via http://www.ruby-forum.com/.

 
Reply With Quote
 
Marcio Braga
Guest
Posts: n/a
 
      08-18-2008
> It's a Matrix, right? (some of this stolen from a previous thread)...
>
> require 'matrix'
> class Matrix
> def []=(i, j, n)
> @rows[i][j] = n
> end
> end
>
> m = Matrix[['a', 'b'], ['c', 'd']]
> k = m.clone
> k[0, 0] = 'e'
> puts k.to_a
> puts m.to_a
>
> Todd


At first I was not considering to handle them using Matrix class (just
Array class) but thank you to call attention to this clone method of
Matrix.

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

 
Reply With Quote
 
 
 
Reply

Thread Tools

Posting Rules
You may not post new threads
You may not post replies
You may not post attachments
You may not edit your posts

BB code is On
Smilies are On
[IMG] code is On
HTML code is Off
Trackbacks are On
Pingbacks are On
Refbacks are Off


Similar Threads
Thread Thread Starter Forum Replies Last Post
unable to understand constructor behavior neo C++ 4 12-30-2006 12:00 AM
QuickSort . I don't understand this behavior camotito C Programming 2 04-24-2006 08:45 PM
Read all of this to understand how it works. then check around on otherRead all of this to understand how it works. then check around on other thelisa martin Computer Support 2 08-18-2005 06:40 AM
i do not understand this behavior: url tacked onto "localhost" Timo ASP .Net 1 03-03-2005 07:55 PM
help me understand this cin/fstream behavior Danny Anderson C++ 5 01-26-2004 02:35 AM



Advertisments