# HG changeset patch # User Jaroslav Hajek # Date 1259267495 -3600 # Node ID cac3b4e5035b79bf58b7c891dcb49b831b511db6 # Parent 21d81d06b221f6fa307db82f6e7c130e075b5db7 cse in cross diff -r 21d81d06b221 -r cac3b4e5035b scripts/ChangeLog --- a/scripts/ChangeLog Thu Nov 26 21:06:21 2009 +0100 +++ b/scripts/ChangeLog Thu Nov 26 21:31:35 2009 +0100 @@ -1,3 +1,7 @@ +2009-11-26 Jaroslav Hajek + + * linear-algebra/cross.m: Avoid doing indexing twice. + 2009-11-26 Jaroslav Hajek * linear-algebra/normest.m: Randomize initial vector. diff -r 21d81d06b221 -r cac3b4e5035b scripts/linear-algebra/cross.m --- a/scripts/linear-algebra/cross.m Thu Nov 26 21:06:21 2009 +0100 +++ b/scripts/linear-algebra/cross.m Thu Nov 26 21:31:35 2009 +0100 @@ -66,27 +66,26 @@ error ("cross: must have at least one dimension with 3 elements"); endif else - if (size (x) != 3) + if (size (x, dim) != 3) error ("cross: dimension dim must have 3 elements"); endif endif nd = ndims (x); sz = size (x); - idx1 = cell (1, nd); - for i = 1:nd - idx1{i} = 1:sz(i); - endfor - idx2 = idx3 = idx1; + idx2 = idx3 = idx1 = {':'}(ones (1, nd)); idx1(dim) = 1; idx2(dim) = 2; idx3(dim) = 3; if (size_equal (x, y)) - z = cat (dim, - (x(idx2{:}) .* y(idx3{:}) - x(idx3{:}) .* y(idx2{:})), - (x(idx3{:}) .* y(idx1{:}) - x(idx1{:}) .* y(idx3{:})), - (x(idx1{:}) .* y(idx2{:}) - x(idx2{:}) .* y(idx1{:}))); + x1 = x(idx1{:}); + x2 = x(idx2{:}); + x3 = x(idx3{:}); + y1 = y(idx1{:}); + y2 = y(idx2{:}); + y3 = y(idx3{:}); + z = cat (dim, (x2.*y3 - x3.*y2), (x3.*y1 - x1.*y3), (x1.*y2 - x2.*y1)); else error ("cross: x and y must have the same dimensions"); endif