<rss xmlns:itunes="http://www.itunes.com/dtds/podcast-1.0.dtd" version="2.0">
  <channel>
    <title>MaplePrimes - answers and comments on Question, Strange behavior when copying a vector</title>
    <link>http://www.mapleprimes.com/questions/127451-Strange-Behavior-When-Copying-A-Vector</link>
    <language>en-us</language>
    <copyright>2026 Maplesoft, A Division of Waterloo Maple Inc.</copyright>
    <generator>Maplesoft Document System</generator>
    <lastBuildDate>Fri, 12 Jun 2026 00:01:25 GMT</lastBuildDate>
    <pubDate>Fri, 12 Jun 2026 00:01:25 GMT</pubDate>
    <itunes:subtitle />
    <itunes:summary />
    <description>The latest answers and comments added to the Question, Strange behavior when copying a vector</description>
    <image>
      <url>http://www.mapleprimes.com/images/mapleprimeswhite.jpg</url>
      <title>MaplePrimes - answers and comments on Question, Strange behavior when copying a vector</title>
      <link>http://www.mapleprimes.com/questions/127451-Strange-Behavior-When-Copying-A-Vector</link>
    </image>
    <item>
      <title>Dim light</title>
      <link>http://www.mapleprimes.com/questions/127451-Strange-Behavior-When-Copying-A-Vector?ref=Feed:MaplePrimes:Strange behavior when copying a vector:Comments#answer127454</link>
      <itunes:summary>&lt;p&gt;Your first example is not surprising as in fact (as you are suspecting) a and b point to the same location in memory (try addressof(a); addressof(b);)&lt;/p&gt;
&lt;p&gt;In your second example you can again try addressof(a); addressof(b);&lt;/p&gt;
&lt;p&gt;I also think it is interesting that after assigning y:=2, only the y in a is immediately evaluated to 2.&lt;/p&gt;
&lt;p&gt;The evaluation of y in b is done if you just do b[2];&lt;/p&gt;
&lt;p&gt;On the vector itself you can do eval~(b);&lt;/p&gt;</itunes:summary>
      <description>&lt;p&gt;Your first example is not surprising as in fact (as you are suspecting) a and b point to the same location in memory (try addressof(a); addressof(b);)&lt;/p&gt;
&lt;p&gt;In your second example you can again try addressof(a); addressof(b);&lt;/p&gt;
&lt;p&gt;I also think it is interesting that after assigning y:=2, only the y in a is immediately evaluated to 2.&lt;/p&gt;
&lt;p&gt;The evaluation of y in b is done if you just do b[2];&lt;/p&gt;
&lt;p&gt;On the vector itself you can do eval~(b);&lt;/p&gt;</description>
      <guid>127454</guid>
      <pubDate>Mon, 07 Nov 2011 04:58:57 Z</pubDate>
      <itunes:author>Preben Alsholm</itunes:author>
      <author>Preben Alsholm</author>
    </item>
    <item>
      <title>correct</title>
      <link>http://www.mapleprimes.com/questions/127451-Strange-Behavior-When-Copying-A-Vector?ref=Feed:MaplePrimes:Strange behavior when copying a vector:Comments#answer127456</link>
      <itunes:summary>&lt;p&gt;In your first example , you have not copied the Vector assigned to `a`, when you assign that also to `b`. All that you've accomplished is to make the value of `b` be the same object.&lt;/p&gt;
&lt;p&gt;This behaviour, in the first example, should not seem mysterious. The most natural way to get `b` to be assigned a copy of the Vector assigned to `a` is to use the &lt;a href="http://www.maplesoft.com/support/help/Maple/view.aspx?path=copy"&gt;copy&lt;/a&gt; command (a top-level command, not part of any package, which knows how to copy various sorts of object).&lt;/p&gt;
&lt;p&gt;Note that there is a flip-side to this copying business, which is related to uniqueness. A Vector is a so-called mutable data structure, which means that its entries can be changed &lt;em&gt;in-place&lt;/em&gt;. This is related, in some respects, to the fact there can be more than one distinct length-2 Vector with the entries x and y. Changes to one such Vector does not change the other.&lt;/p&gt;
&lt;pre&gt;&amp;gt;  restart:

&amp;gt; V1:=Vector([x,y]):

&amp;gt; V2:=copy(V1):

&amp;gt; {V1,V2}; # This surprises some people, but it shouldn't.

                                 /[x]  [x]\ 
                                { [ ], [ ] }
                                 \[y]  [y]/ 

&amp;gt; evalb(V1=V2); # This surprises some people, but it shouldn't.

                                    false

&amp;gt; V1[2]:=17:

&amp;gt; V2[2];
                                      y
&lt;/pre&gt;
&lt;p&gt;So, some people are surprised that mere assignment does not make distinct copies. And some people are surprised that separate instances are recognized as being distinct.&lt;/p&gt;
&lt;p&gt;You can contrast all that you've seen here for &lt;em&gt;rtables&lt;/em&gt; (of which a Vector is one kind, the other two being Matrix and Array), with the behaviour of &lt;em&gt;lists&lt;/em&gt; and &lt;em&gt;tables&lt;/em&gt; (the latter of which also form the data structure used for lowercase array, matrix, and vector). How do these differ, you may ask? Well, for &lt;em&gt;lists&lt;/em&gt; mutability is faked, which is much discusses as the reason why doing list-entry assignment is highly inefficient and therefore quite dubious. When you assign into just an entry of a list then the maple kernel actually goes and replaces the whole structure with a new object behind the scenes. And &lt;em&gt;tables&lt;/em&gt; have &lt;a href="http://www.maplesoft.com/support/help/Maple/view.aspx?path=last_name_eval"&gt;last-name-eval&lt;/a&gt;, which makes them behave differently especially when passed as arguments to a procedure.&lt;/p&gt;
&lt;p&gt;Which brings us to your second example. The ArrayTools:-Copy behaviour is a little unusual. But the broader patterns of behaviour are motivated in part to efficiency needs and to argument passing for procedures. I'll try and explain:&lt;/p&gt;
&lt;p&gt;Suppose you create a length-2 Vector V with entries x and y, where x and y are not yet assigned. And then you subsequently assign a value to x, let's say a value of 5.&lt;/p&gt;
&lt;pre&gt;&amp;gt; restart:

&amp;gt; V := Vector([x,y]):

&amp;gt; x := 5:
&lt;/pre&gt;
&lt;p&gt;The Vector V still has entries of x and y. If you access V[1], then you get the fully evaluated result of 5. But the entry of V is still just x. When you cause V to be printed, then really you're just seeing how the `print/rtable` command sees V when in turn it has to access the entries. You can still see the stored entries of V using `lprint`.&lt;/p&gt;
&lt;pre&gt;&amp;gt; V[1];

                                  5

&amp;gt; V;

                                     [5]
                                     [ ]
                                     [y]

&amp;gt; lprint(V);
Vector[column](2, {1 = x, 2 = y}, datatype = anything, storage = rectangular, order = Fortran_order, shape = [])
&lt;/pre&gt;
&lt;p&gt;Now, what would happen if we passed V as a argument to a procedure. Would we want all entries of V to be evaluated fully (as happens for some other structures...), or not?&lt;/p&gt;
&lt;p&gt;Suppose we have an enormous Vector V, and the procedure will only access a small number of the entries a small number of times. In this scenario, we would very much want the entries of V to &lt;strong&gt;not&lt;/strong&gt; all be fully evaluated, as far as what is seen inside the procedure. We'd want behaviour like at the top-level, outside the procedure, where evaluation only occurs upon individual entry access.&lt;/p&gt;
&lt;p&gt;This is, in fact, the default behaviour to how rtables (eg. Vectors) are passed.&lt;/p&gt;
&lt;pre&gt;&amp;gt; f:=proc(S)
&amp;gt;    lprint(S);
&amp;gt;    S[1];
&amp;gt; end proc:

&amp;gt; f(V);
Vector[column](2, {1 = x, 2 = y}, datatype = anything, storage = rectangular, order = Fortran_order, shape = [])

                                      5
&lt;/pre&gt;
&lt;p&gt;Now suppose that all entries of V will be accessed by the algorithm within the procedure. and that this will happen many times repeatedly. In this scenario, we'd very much prefer it if the entries of V &lt;strong&gt;have&lt;/strong&gt; already been fully evaluated already, inside whatever is assigned to that formal parameter of the procedure. Because then each subsequent access would be fast and not require the full evaluation and resolution. There is a (rarely mentioned) command to handle this (somewhat uncommon) situation, called &lt;a href="http://www.maplesoft.com/support/help/Maple/view.aspx?path=rtable_eval"&gt;rtable_eval&lt;/a&gt;. Since these situations relate to efficiency, it's important to be able to resolve the values of the rtable, inplace, without necessitating extra memory allocation that is entailed with fully copying and replacement of the structure.&lt;/p&gt;
&lt;pre&gt;&amp;gt; restart:

&amp;gt; V := Vector([x,y]):

&amp;gt; y := 13:

&amp;gt; lprint(V);
Vector[column](2, {1 = x, 2 = y}, datatype = anything, storage = rectangular, order = Fortran_order, shape = [])

&amp;gt; f := proc(S)
&amp;gt;    rtable_eval(S,inplace);
&amp;gt;    lprint(S);
&amp;gt; end proc:

&amp;gt; f(V);
Vector[column](2, {1 = x, 2 = 13}, datatype = anything, storage = rectangular, order = Fortran_order, shape = [])

&amp;gt; lprint(V); # different!
Vector[column](2, {1 = x, 2 = 13}, datatype = anything, storage = rectangular, order = Fortran_order, shape = [])
&lt;/pre&gt;
&lt;p&gt;All the same fundamental issues were also present for the older (now deprecated) table-based array, matrix, and vector objects. But since these had last-name-eval they faced the problems slightly differently. You can still see the workarounds, though. For example, several `linalg` package commands would accept a matrix argument A and immediately make an assignment to the local B of the form B:=eval(A). Sometimes that was done so that an inplace algorithm could act on local copy B, but sometimes it was done because the entries of last-name-eval matrix A were not all fully evaluated like gets done for other arguments to the procedure.&lt;/p&gt;
&lt;p&gt;Now, the ArrayTools:-Copy command is interesting. It is not a kernel builtin, but it is a compiled routine. It appears to have some unusual behaviour, and I'd label your example as an anomaly. I'd stick with the top-level `copy` command instead, if I were you and I wanted to easily create a distinct copy of a Vector/Matrix/Array. Or assign the entries of the new Vector by some other means, if desired.&lt;/p&gt;
&lt;pre&gt;&amp;gt; restart:
&amp;gt; a := Vector([x,y]):
&amp;gt; b := copy(a):
&amp;gt; b;

                                     [x]
                                     [ ]
                                     [y]

&amp;gt; y:=13:
&amp;gt; a;

                                    [x ]
                                    [  ]
                                    [13]

&amp;gt; b;

                                    [x ]
                                    [  ]
                                    [13]

&amp;gt; restart:
&amp;gt; a := Vector([x,y]):
&amp;gt; b := Vector([0,0]):
&amp;gt; b[1],b[2]:=a[1],a[2]:
&amp;gt; b;

                                     [x]
                                     [ ]
                                     [y]

&amp;gt; y:=13:
&amp;gt; a;

                                    [x ]
                                    [  ]
                                    [13]

&amp;gt; b;

                                    [x ]
                                    [  ]
                                    [13]

&amp;gt; restart:
&amp;gt; a := Vector([x,y]):
&amp;gt; b := Vector([0,0]):
&amp;gt; b[1..2]:=a[1..2]:
&amp;gt; b;

                                     [x]
                                     [ ]
                                     [y]

&amp;gt; y:=13:
&amp;gt; a;

                                    [x ]
                                    [  ]
                                    [13]

&amp;gt; b;

                                    [x ]
                                    [  ]
                                    [13]
&lt;/pre&gt;
&lt;!--break--&gt;
&lt;p&gt;acer&lt;/p&gt;</itunes:summary>
      <description>&lt;p&gt;In your first example , you have not copied the Vector assigned to `a`, when you assign that also to `b`. All that you've accomplished is to make the value of `b` be the same object.&lt;/p&gt;
&lt;p&gt;This behaviour, in the first example, should not seem mysterious. The most natural way to get `b` to be assigned a copy of the Vector assigned to `a` is to use the &lt;a href="http://www.maplesoft.com/support/help/Maple/view.aspx?path=copy"&gt;copy&lt;/a&gt; command (a top-level command, not part of any package, which knows how to copy various sorts of object).&lt;/p&gt;
&lt;p&gt;Note that there is a flip-side to this copying business, which is related to uniqueness. A Vector is a so-called mutable data structure, which means that its entries can be changed &lt;em&gt;in-place&lt;/em&gt;. This is related, in some respects, to the fact there can be more than one distinct length-2 Vector with the entries x and y. Changes to one such Vector does not change the other.&lt;/p&gt;
&lt;pre&gt;&amp;gt;  restart:

&amp;gt; V1:=Vector([x,y]):

&amp;gt; V2:=copy(V1):

&amp;gt; {V1,V2}; # This surprises some people, but it shouldn't.

                                 /[x]  [x]\ 
                                { [ ], [ ] }
                                 \[y]  [y]/ 

&amp;gt; evalb(V1=V2); # This surprises some people, but it shouldn't.

                                    false

&amp;gt; V1[2]:=17:

&amp;gt; V2[2];
                                      y
&lt;/pre&gt;
&lt;p&gt;So, some people are surprised that mere assignment does not make distinct copies. And some people are surprised that separate instances are recognized as being distinct.&lt;/p&gt;
&lt;p&gt;You can contrast all that you've seen here for &lt;em&gt;rtables&lt;/em&gt; (of which a Vector is one kind, the other two being Matrix and Array), with the behaviour of &lt;em&gt;lists&lt;/em&gt; and &lt;em&gt;tables&lt;/em&gt; (the latter of which also form the data structure used for lowercase array, matrix, and vector). How do these differ, you may ask? Well, for &lt;em&gt;lists&lt;/em&gt; mutability is faked, which is much discusses as the reason why doing list-entry assignment is highly inefficient and therefore quite dubious. When you assign into just an entry of a list then the maple kernel actually goes and replaces the whole structure with a new object behind the scenes. And &lt;em&gt;tables&lt;/em&gt; have &lt;a href="http://www.maplesoft.com/support/help/Maple/view.aspx?path=last_name_eval"&gt;last-name-eval&lt;/a&gt;, which makes them behave differently especially when passed as arguments to a procedure.&lt;/p&gt;
&lt;p&gt;Which brings us to your second example. The ArrayTools:-Copy behaviour is a little unusual. But the broader patterns of behaviour are motivated in part to efficiency needs and to argument passing for procedures. I'll try and explain:&lt;/p&gt;
&lt;p&gt;Suppose you create a length-2 Vector V with entries x and y, where x and y are not yet assigned. And then you subsequently assign a value to x, let's say a value of 5.&lt;/p&gt;
&lt;pre&gt;&amp;gt; restart:

&amp;gt; V := Vector([x,y]):

&amp;gt; x := 5:
&lt;/pre&gt;
&lt;p&gt;The Vector V still has entries of x and y. If you access V[1], then you get the fully evaluated result of 5. But the entry of V is still just x. When you cause V to be printed, then really you're just seeing how the `print/rtable` command sees V when in turn it has to access the entries. You can still see the stored entries of V using `lprint`.&lt;/p&gt;
&lt;pre&gt;&amp;gt; V[1];

                                  5

&amp;gt; V;

                                     [5]
                                     [ ]
                                     [y]

&amp;gt; lprint(V);
Vector[column](2, {1 = x, 2 = y}, datatype = anything, storage = rectangular, order = Fortran_order, shape = [])
&lt;/pre&gt;
&lt;p&gt;Now, what would happen if we passed V as a argument to a procedure. Would we want all entries of V to be evaluated fully (as happens for some other structures...), or not?&lt;/p&gt;
&lt;p&gt;Suppose we have an enormous Vector V, and the procedure will only access a small number of the entries a small number of times. In this scenario, we would very much want the entries of V to &lt;strong&gt;not&lt;/strong&gt; all be fully evaluated, as far as what is seen inside the procedure. We'd want behaviour like at the top-level, outside the procedure, where evaluation only occurs upon individual entry access.&lt;/p&gt;
&lt;p&gt;This is, in fact, the default behaviour to how rtables (eg. Vectors) are passed.&lt;/p&gt;
&lt;pre&gt;&amp;gt; f:=proc(S)
&amp;gt;    lprint(S);
&amp;gt;    S[1];
&amp;gt; end proc:

&amp;gt; f(V);
Vector[column](2, {1 = x, 2 = y}, datatype = anything, storage = rectangular, order = Fortran_order, shape = [])

                                      5
&lt;/pre&gt;
&lt;p&gt;Now suppose that all entries of V will be accessed by the algorithm within the procedure. and that this will happen many times repeatedly. In this scenario, we'd very much prefer it if the entries of V &lt;strong&gt;have&lt;/strong&gt; already been fully evaluated already, inside whatever is assigned to that formal parameter of the procedure. Because then each subsequent access would be fast and not require the full evaluation and resolution. There is a (rarely mentioned) command to handle this (somewhat uncommon) situation, called &lt;a href="http://www.maplesoft.com/support/help/Maple/view.aspx?path=rtable_eval"&gt;rtable_eval&lt;/a&gt;. Since these situations relate to efficiency, it's important to be able to resolve the values of the rtable, inplace, without necessitating extra memory allocation that is entailed with fully copying and replacement of the structure.&lt;/p&gt;
&lt;pre&gt;&amp;gt; restart:

&amp;gt; V := Vector([x,y]):

&amp;gt; y := 13:

&amp;gt; lprint(V);
Vector[column](2, {1 = x, 2 = y}, datatype = anything, storage = rectangular, order = Fortran_order, shape = [])

&amp;gt; f := proc(S)
&amp;gt;    rtable_eval(S,inplace);
&amp;gt;    lprint(S);
&amp;gt; end proc:

&amp;gt; f(V);
Vector[column](2, {1 = x, 2 = 13}, datatype = anything, storage = rectangular, order = Fortran_order, shape = [])

&amp;gt; lprint(V); # different!
Vector[column](2, {1 = x, 2 = 13}, datatype = anything, storage = rectangular, order = Fortran_order, shape = [])
&lt;/pre&gt;
&lt;p&gt;All the same fundamental issues were also present for the older (now deprecated) table-based array, matrix, and vector objects. But since these had last-name-eval they faced the problems slightly differently. You can still see the workarounds, though. For example, several `linalg` package commands would accept a matrix argument A and immediately make an assignment to the local B of the form B:=eval(A). Sometimes that was done so that an inplace algorithm could act on local copy B, but sometimes it was done because the entries of last-name-eval matrix A were not all fully evaluated like gets done for other arguments to the procedure.&lt;/p&gt;
&lt;p&gt;Now, the ArrayTools:-Copy command is interesting. It is not a kernel builtin, but it is a compiled routine. It appears to have some unusual behaviour, and I'd label your example as an anomaly. I'd stick with the top-level `copy` command instead, if I were you and I wanted to easily create a distinct copy of a Vector/Matrix/Array. Or assign the entries of the new Vector by some other means, if desired.&lt;/p&gt;
&lt;pre&gt;&amp;gt; restart:
&amp;gt; a := Vector([x,y]):
&amp;gt; b := copy(a):
&amp;gt; b;

                                     [x]
                                     [ ]
                                     [y]

&amp;gt; y:=13:
&amp;gt; a;

                                    [x ]
                                    [  ]
                                    [13]

&amp;gt; b;

                                    [x ]
                                    [  ]
                                    [13]

&amp;gt; restart:
&amp;gt; a := Vector([x,y]):
&amp;gt; b := Vector([0,0]):
&amp;gt; b[1],b[2]:=a[1],a[2]:
&amp;gt; b;

                                     [x]
                                     [ ]
                                     [y]

&amp;gt; y:=13:
&amp;gt; a;

                                    [x ]
                                    [  ]
                                    [13]

&amp;gt; b;

                                    [x ]
                                    [  ]
                                    [13]

&amp;gt; restart:
&amp;gt; a := Vector([x,y]):
&amp;gt; b := Vector([0,0]):
&amp;gt; b[1..2]:=a[1..2]:
&amp;gt; b;

                                     [x]
                                     [ ]
                                     [y]

&amp;gt; y:=13:
&amp;gt; a;

                                    [x ]
                                    [  ]
                                    [13]

&amp;gt; b;

                                    [x ]
                                    [  ]
                                    [13]
&lt;/pre&gt;
&lt;!--break--&gt;
&lt;p&gt;acer&lt;/p&gt;</description>
      <guid>127456</guid>
      <pubDate>Mon, 07 Nov 2011 07:32:01 Z</pubDate>
      <itunes:author>acer</itunes:author>
      <author>acer</author>
    </item>
  </channel>
</rss>