ASKSAGE: Sage Q&A Forum - Individual question feedhttp://ask.sagemath.org/questions/Q&A Forum for SageenCopyright Sage, 2010. Some rights reserved under creative commons license.Fri, 19 Oct 2012 08:19:16 -0500Copy vectorshttp://ask.sagemath.org/question/9445/copy-vectors/Hello everyone,
I have the following problem:
I have two vectors v = vector(QQ, [1,1]), w = vector(QQ, [3,3]).
I want to copy the coefficients of w into v. I can't set v = w
directly, beacuse v is used as a value in some dictionaries.
I could loop over the list() of the vectors, but I was wondering if
there is some *better* way to copy all of the entries.
Example from comment:
> l = {1: v}; v.set(1,2) Now l == {1: (1, 2)}, but v = copy(w) does not change lFri, 19 Oct 2012 02:25:33 -0500http://ask.sagemath.org/question/9445/copy-vectors/Answer by kcrisman for <p>Hello everyone,</p>
<p>I have the following problem:
I have two vectors v = vector(QQ, [1,1]), w = vector(QQ, [3,3]).
I want to copy the coefficients of w into v. I can't set v = w
directly, beacuse v is used as a value in some dictionaries.
I could loop over the list() of the vectors, but I was wondering if
there is some <em>better</em> way to copy all of the entries.</p>
<p>Example from comment:</p>
<blockquote>
<p>l = {1: v}; v.set(1,2) Now l == {1: (1, 2)}, but v = copy(w) does not change l</p>
</blockquote>
http://ask.sagemath.org/question/9445/copy-vectors/?answer=14165#post-id-14165Responding to updated issue: To some extent this is a [Python issue](http://stackoverflow.com/questions/4557223/python-basic-data-references-list-of-same-reference), even for lists. `v = int(22); L= [v]; L; v= int(23); L` gives 22 both times. `v.set()` works because you didn't actually change the reference the variable `v` has, if I understand correctly.
The following does what you want, and isn't quite the loop you mentioned, though it's close. I don't know if there is a way to avoid the Python thing (feature, really) above.
sage: for x in w.iteritems(): v.set(*x)
....:
Fri, 19 Oct 2012 05:16:38 -0500http://ask.sagemath.org/question/9445/copy-vectors/?answer=14165#post-id-14165Comment by benjaminfjones for <p>Responding to updated issue: To some extent this is a <a href="http://stackoverflow.com/questions/4557223/python-basic-data-references-list-of-same-reference">Python issue</a>, even for lists. <code>v = int(22); L= [v]; L; v= int(23); L</code> gives 22 both times. <code>v.set()</code> works because you didn't actually change the reference the variable <code>v</code> has, if I understand correctly.</p>
<p>The following does what you want, and isn't quite the loop you mentioned, though it's close. I don't know if there is a way to avoid the Python thing (feature, really) above.</p>
<pre><code>sage: for x in w.iteritems(): v.set(*x)
....:
</code></pre>
http://ask.sagemath.org/question/9445/copy-vectors/?comment=18843#post-id-18843Assignment by reference is definitely an intentional design decision in python. Fri, 19 Oct 2012 08:19:16 -0500http://ask.sagemath.org/question/9445/copy-vectors/?comment=18843#post-id-18843Answer by kcrisman for <p>Hello everyone,</p>
<p>I have the following problem:
I have two vectors v = vector(QQ, [1,1]), w = vector(QQ, [3,3]).
I want to copy the coefficients of w into v. I can't set v = w
directly, beacuse v is used as a value in some dictionaries.
I could loop over the list() of the vectors, but I was wondering if
there is some <em>better</em> way to copy all of the entries.</p>
<p>Example from comment:</p>
<blockquote>
<p>l = {1: v}; v.set(1,2) Now l == {1: (1, 2)}, but v = copy(w) does not change l</p>
</blockquote>
http://ask.sagemath.org/question/9445/copy-vectors/?answer=14163#post-id-14163Do you need this?
sage: v = vector(QQ, [1,1])
sage: w = vector(QQ, [3,3])
sage: v; w
(1, 1)
(3, 3)
sage: v = copy(w)
sage: v; w
(3, 3)
(3, 3)
sage: w = 22
sage: v; w
(3, 3)
22
Fri, 19 Oct 2012 02:34:39 -0500http://ask.sagemath.org/question/9445/copy-vectors/?answer=14163#post-id-14163Comment by Mathmon for <p>Do you need this?</p>
<pre><code>sage: v = vector(QQ, [1,1])
sage: w = vector(QQ, [3,3])
sage: v; w
(1, 1)
(3, 3)
sage: v = copy(w)
sage: v; w
(3, 3)
(3, 3)
sage: w = 22
sage: v; w
(3, 3)
22
</code></pre>
http://ask.sagemath.org/question/9445/copy-vectors/?comment=18845#post-id-18845Well, as an example: l = {1: v}; v.set(1,2) Now l == {1: (1, 2)}, but v = copy(w) does not change l...Fri, 19 Oct 2012 02:41:31 -0500http://ask.sagemath.org/question/9445/copy-vectors/?comment=18845#post-id-18845Answer by benjaminfjones for <p>Hello everyone,</p>
<p>I have the following problem:
I have two vectors v = vector(QQ, [1,1]), w = vector(QQ, [3,3]).
I want to copy the coefficients of w into v. I can't set v = w
directly, beacuse v is used as a value in some dictionaries.
I could loop over the list() of the vectors, but I was wondering if
there is some <em>better</em> way to copy all of the entries.</p>
<p>Example from comment:</p>
<blockquote>
<p>l = {1: v}; v.set(1,2) Now l == {1: (1, 2)}, but v = copy(w) does not change l</p>
</blockquote>
http://ask.sagemath.org/question/9445/copy-vectors/?answer=14166#post-id-14166Responding to @Mathmon's comment to @kcrisman's first (?) answer: that's because "v" is a reference (pointer) to a vector object somewhere in memory. The statement `v = copy(w)` creates a new vector in memory with a copy of `w`s content and then assigns v as a reference to that new vector.
In the dictionary, `I[1]` is another reference (to the location in memory of the value associated with the key `1`. So, for example if you want to update the vector value in `I` you could do something like:
sage: I = { 1: vector(QQ, (1,2)) }
sage: a = I[1]
sage: a.set(0,3)
sage: I
{1: (3, 2)}
to reassign the reference `I[1]` use it's name (not a secondary reference to the same location):
sage: w = vector(QQ, (4,5))
sage: I[1] = copy(w)
sage: I
{1: (4, 5)}
Fri, 19 Oct 2012 08:17:31 -0500http://ask.sagemath.org/question/9445/copy-vectors/?answer=14166#post-id-14166