MathGroup Archive 2006

[Date Index] [Thread Index] [Author Index]

Search the Archive

Re: matrix operations -- shared data vs copied

  • To: mathgroup at
  • Subject: [mg65964] Re: matrix operations -- shared data vs copied
  • From: albert <awnl at>
  • Date: Tue, 25 Apr 2006 05:18:30 -0400 (EDT)
  • References: <e2i90l$9df$>
  • Sender: owner-wri-mathgroup at


as has been mentioned by others, I think the crucial thing to understand
what happens is to understand that mathematica is a term rewrite system,
and evaluating an expression means to apply all rules how to rewrite terms
that are defined. You will find explanations on this and how the evaluation
process works in the book or the helpbrowser. 

Bill Rowe wrote:
> One of the best ways and probably the most direct way to answer questions
> like this is to simply try it out and see what happens. For example, I can
> create a simple 3 x 3 matrix named A and set B to be the same as follows:

I absolutly agree on this, and would just like to bring in the function
MemoryInUse[] which will allow you to check whether or not a copy of data
is made or not. When looking at the session I have copied below, you will
realize that there are nontrivial things going on behind the scene: 
1) A=B; does not copy the data, obviously.
2) Only when assigning to A[[50,50]] the data is copied.
3) The copy of the data causes the memory usage to increase much more than
   the generation of the original data. This is because the rhs I was using
   is nonnumeric, and so mathematica has to use a more expensive 

although I'm afraid that this is probably causing even more confusion than
before I hope it nevertheless helps. If you look up all the details of
everything you don't understand in the helpbrowser you will be well
prepaired for using mathematica in an efficient way, I propose :-)


In[1]:= MemoryInUse[]

Out[1]= 1988256

In[2]:= A=Table[Random[],{100},{100}];

In[3]:= MemoryInUse[]

Out[3]= 2071344

In[4]:= B=A;

In[5]:= MemoryInUse[]

Out[5]= 2072288

In[6]:= {Out[3]-Out[1],Out[5]-Out[3]}

Out[6]= {83088, 944}

In[7]:= MemoryInUse[]

Out[7]= 2073496

In[8]:= A[[50,50]]=Infinity;

In[9]:= MemoryInUse[]

Out[9]= 2277128

In[10]:= 2277128-2073496

Out[10]= 203632

In[11]:= Developer`PackedArrayQ[A]

Out[11]= False

In[12]:= Developer`PackedArrayQ[B]

Out[12]= True

  • Prev by Date: NDSolve
  • Next by Date: Re: Troubles in NonlinearFit-Why always failed?
  • Previous by thread: Re: matrix operations -- shared data vs copied
  • Next by thread: Troubles in NonlinearFit-Why always failed?