Path: news.mathworks.com!not-for-mail
From: <HIDDEN>
Newsgroups: comp.soft-sys.matlab
Subject: Re: Order of operations in a vectorized function
Date: Tue, 3 Aug 2010 19:17:06 +0000 (UTC)
Organization: The MathWorks, Inc.
Lines: 21
Message-ID: <i39pvi$7g8$1@fred.mathworks.com>
References: <i39kf9$bt5$1@fred.mathworks.com>
Reply-To: <HIDDEN>
NNTP-Posting-Host: webapp-05-blr.mathworks.com
Content-Type: text/plain; charset=UTF-8; format=flowed
Content-Transfer-Encoding: 8bit
X-Trace: fred.mathworks.com 1280863026 7688 172.30.248.35 (3 Aug 2010 19:17:06 GMT)
X-Complaints-To: news@mathworks.com
NNTP-Posting-Date: Tue, 3 Aug 2010 19:17:06 +0000 (UTC)
X-Newsreader: MATLAB Central Newsreader 1187260
Xref: news.mathworks.com comp.soft-sys.matlab:658708

"Vladimir " <something@or.another> wrote in message <i39kf9$bt5$1@fred.mathworks.com>...
> I was wondering if there is any documentation on the internal workings of vectorized operations. Specifically, I have programmed an iterative solver for a system of PDEs, and the next iterations values are found using a line much like this one:
> 
> grid(:,:)=(-alpha.*(grid(:,:)+grid(:,:))-gamma.*(grid(:,:)+grid(:,:))-2*beta.*x_dd_zeta_etha)./(-2*(alpha+gamma));
> 
> all are matrices of the same size. I tried running it both like this, which I hoped would be a good replacement for running looped Gauss-Seidel iteration, and in Jacobi form - when the new values for grid are calculated based specifically on the previous iterations values. The convergence speed was different, but strangely enough, it was faster for the Jacobi version. Then I realized that for my code to really be GS iteration, it needs to calculate each value using the newest values available, but I have no way of knowing if that is the case, because .* and .^'s aren't transparent to the user. 
> Help? 
- - - - - - - - - - -
  In the "vectorized" form you have written, it doesn't look like Gauss-Seidel iteration to me.  For each element of grid, matlab will use the old values in grid for all its computations.  It accomplishes this by temporarily storing two copies of the grid matrix in separate buffers, the old values and the new values.  The computation on the right takes from the old value matrix and puts the results into the new value matrix.  Imagine that you wrote "temp_grid" on the left side and after the computation was finished for all elements you then wrote grid = temp_grid to copy the new results back into grid.  That would be equivalent to the vectorized action.

  Note that this is general different from the results you would get if you put it in nested for-loops like this:

 for i1 = 1:n
  for i2 = 1:n
   grid(i1,i2) = the above expression in grid(i1,i2) ;
  end
 end

  A lot of beginners in matlab make the mistake of not distinguishing between these two different ways of proceeding.

Roger Stafford