Re: matrix operations -- shared data vs copied
- To: mathgroup at smc.vnet.net
- Subject: [mg65931] Re: [mg65917] matrix operations -- shared data vs copied
- From: "Szabolcs Horvát" <szhorvat at gmail.com>
- Date: Mon, 24 Apr 2006 06:01:26 -0400 (EDT)
- References: <200604231017.GAA11827@smc.vnet.net>
- Sender: owner-wri-mathgroup at wolfram.com
On 4/23/06, robert.dodier at gmail.com <robert.dodier at gmail.com> wrote: > Hello, > > I'm trying to get a handle on what Mathematica does with some > matrix operations. I wasn't able to figure it out by web or newsgroups > search so maybe you can help. > > Suppose A is a matrix. Consider these operations. > > (1) B = A > > (2) myfunction[A] > > (3) B = (some way of extracting column 1 of A) > > (4) B = (some way of pasting A onto another matrix) > > The question of interest is whether modifying elements of A also > modify B after operations (1), (3), and (4), and whether modifications > of the formal argument within myfunction also modify the actual > argument (namely A). > > In some math languages, B = A and myfunction[A] actually create > copies of A, likewise row/column extraction and pasting matrices > together. But that's not the only way to do it; I'm trying to figure > out where Mathematica stands on this point. > > Thanks for any pointers or other information. > > Robert Dodier > > I may not understand you question completely, but I hope this information will help. In Mathematica you can check the information associated with symbols by using "?" A and B do not share the same data, but you may get always get the value of A when you evaluate B: If you assign A to B and evaluate B, you get A. In[1]:= B=A Out[1]= A In[2]:= B Out[2]= A Now, if you assign a value to A and evaluate B again you get 5, because Mathematica first applies the rule "replace B by A" and then "replace A by 5" In[3]:= A=5 Out[3]= 5 In[4]:= B Out[4]= 5 You can check the definition of B by In[5]:= ?B Global`B B=A Now, if you Remove[A,B] and start again, but you assign a value to A _first_, and A to B after, then the outcome depends on whether you use "=" or ":=" for assigment: In[1]:= A=5 Out[1]= 5 If you use B = A then first A is evaluated (replaced by 5), and then 5 is assigned to B, so the value of B will be independent of A. If you use B := A then the symbol A is assinged to B, and B will always evaluate to the value of A. When Mathematica evaluates an expression, it looks up the definitions associated with the symbols in the expression and applies them until no more definitions are associated with the result. You should read Principles of Mathematica -> Evaluation of Expressions in the Mathematica Book (or help browser). Szabolcs Horvat
- References:
- matrix operations -- shared data vs copied
- From: robert.dodier@gmail.com
- matrix operations -- shared data vs copied