Are these expressions equivalent? (seeing greater errors in vectorized expression)
9 vues (au cours des 30 derniers jours)
Afficher commentaires plus anciens
After converting a loop-based set of expressions to what I thought was a equivalent vectorized one I see a much more rapid accumulation of errors in a simple euler style numerical integration I am performing. This is confusing as the expressions appear to me to be equivalent. Does MATLAB use some reduced precision when computing vectorized expressions (float vs double maybe?). I can't think of any other reasons these two might not be equivalent.
Well, except for the fact that unlike other vectorized expressions, each element of B may be updated more than once here. I shouldn't think this should matter however since we are doing simple addition and the order that elements are updated does not matter.
Details of what the program is doing I don't think are relevant but follow the code snippet below
% loop-based expression
for j=1:size(L,1)
B(L(j,1),3:4)=B(L(j,1),3:4)+F1(j,:)./m.*dt;
B(L(j,2),3:4)=B(L(j,2),3:4)+F2(j,:)./m.*dt;
end
% Vectorized expression
B(L(:,1),3:4)=B(L(:,1),3:4)+F1./m.*dt;
B(L(:,2),3:4)=B(L(:,2),3:4)+F2./m.*dt;
F1 (note: F2=-F1) has two columns and the same number of rows as L.
Notes on what the script does:
This is from a simple toy script that performs a numerical integration of a mass-spring collection. These lines simply update the velocities of the blocks (B(:,3:4) are vx and vy). The spring links (L) columns 1 and 2 indicated which blocks are connected and tie back to that row in B. The force on each block at the end of each link (F1 and F2) is just above this in the script.
I can provide the entire script if needed.
6 commentaires
dpb
le 14 Déc 2015
I'd wager if you take the code snippets above out of the overall script and provide a set of static elements for the variables that you'll find for the single evaluation the results are identical. My guess is that it's owing to the fact it's in an integration routine and it's related to the path by which you get there; hence the comment regarding order possibly being significant (as well as the comment re: possible number of evaluations).
Réponse acceptée
dpb
le 14 Déc 2015
Modifié(e) : dpb
le 15 Déc 2015
Well, it isn't so easy to see; but the two expressions are not the same thing...I was wrong in my conjecture earlier in not having the details of the indexing to be able to tell it is a "many to one" addressing, not just a reordering.
Consider a sample incarnation of the test code; where
>> idx.'
4 4 9 9 2 7 5 7 10 3 6 8 5 7 6 10 4 3 6 4
>>
NB: there are duplicated row indices
>> n=histc(idx,[1:10]).'
n =
0 1 2 4 2 3 3 1 2 2
>>
Consequently, when you run the loop, there are going to be four sequential entries summed into location 4, none at 1, etc., and the magic number of 1 for the 2nd and 8th. Consequently only the those latter two plus the first location (with a trivial zero result) are going to match. You could compute the percentage in error at this point for each trial without anything else as 0.3 correct/0.7 wrong. The alternate construction is identically the same as writing
A_v(idx) = B;
there's only a mapping of the elements of B to the indexed locations in A; since
length(idx)==length(B)
there's not a mismatch of number of elements to be assigned, simply an overwriting of the various given locations multiple times leaving the last one of B in that location specified by idx.
OTOH, sequentially you are building a summation which is why the locations with a discrepancy are always larger for a B uniformly >0. If B were all negative then the observation regarding using plus vis a vis minus as the operation would swap directions; if it were centered with zero mean then it would be random based on the sample.
The "vectorized" solution for this case would be the use of accumarray with the indices vector as the locations.
4 commentaires
dpb
le 15 Déc 2015
"...the two objects on the RHS and LHS of the equation with the same name do not reference the same object in memory"
What makes you think they don't? The problem is that the target for the LHS is multiply-defined. There's nothing in a simple assignment about summing and I don't think there's any other language that would do that for a similar assignment.
Plus de réponses (0)
Voir également
Catégories
En savoir plus sur Matrix Indexing dans Help Center et File Exchange
Community Treasure Hunt
Find the treasures in MATLAB Central and discover how the community can help you!
Start Hunting!