Cross Product Results

classic Classic list List threaded Threaded
3 messages Options
Reply | Threaded
Open this post in threaded view
|

Cross Product Results

Craig Earls-2
I noticed an interesting artifact that is probably very old.
The cross product defin in OCTAVE-SHAR/m/linear-algebra/cross.m
return singular matrix errors if parallel vectors are used. Since
crossing vectors to find if they are parallel is not so uncommon,
this is inconvenient.

Is the tricky matrix determinant stuff done for speed, to keep all
the addressing out of the interpreter?
-----------------------------------------------------------------
Craig P Earls                                 [hidden email]
LT US Navy, MIT Ocean Engineering             [hidden email]
-----------------------------------------------------------------


Reply | Threaded
Open this post in threaded view
|

Cross Product Results

John W. Eaton-6
On 18-Sep-1997, Craig Earls <[hidden email]> wrote:

| I noticed an interesting artifact that is probably very old.
| The cross product defin in OCTAVE-SHAR/m/linear-algebra/cross.m
| return singular matrix errors if parallel vectors are used. Since
| crossing vectors to find if they are parallel is not so uncommon,
| this is inconvenient.
|
| Is the tricky matrix determinant stuff done for speed, to keep all
| the addressing out of the interpreter?

I'm not sure why Kurt wrote it using the determinant.  I don't see why
it can't be written as

function z = cross (x, y)

  if (nargin != 2)
    usage ("cross (x, y)");
  endif

  if (length (x) == 3 && length (y) == 3)

    z = [x(2)*y(3) - x(3)*y(2); x(3)*y(1) - x(1)*y(3); x(1)*y(2) - x(2)*y(1)];

    x_nr = rows (x);
    y_nr = rows (y);

    if ((x_nr == y_nr && x_nr == 1)
        || (x_nr != y_nr && ! prefer_column_vectors))
      z = z';
    endif

  else
    error ("cross: both x and y must be 3-dimensional vectors");
  endif

endfunction

(I believe I got the signs right).

Thanks,

jwe


Reply | Threaded
Open this post in threaded view
|

Re: Cross Product Results

Mario Storti-4

Sometimes it is useful to perform the  cross product on a large number
of pair of vectors. So a and b may be matrices of N x 3 and:

c = cross(a,b)

is N x 3 matrix where each row is the dot product of the corresponding
rows in a and b. The code may be "vectorized" in the row dimension:

> c(:,1)=a(:,2).*b(:,3)-a(:,3).*b(:,2);
> c(:,2)=a(:,3).*b(:,1)-a(:,1).*b(:,3);
> c(:,3)=a(:,1).*b(:,2)-a(:,2).*b(:,1);

This   is  useful  when  working  with  finite   element   meshes,  3D
representation of surfaces,  etc... For instance, if  a and  b are the
corresponding sides of a large  set of triangles,  then c is normal to
the triangles and |c| is the area of the triangles.

We  could  check  the  dimensions of the  arrays   since there are  no
possible confusion:

> if a and b are 3x1
>      compute cross product as column vectors
> else if a and b are Nx3
>      compute cross product as row vectors
> elseif
>      error
> endif

Note that we can't consider a and b  of the form  3xN since then there
is an ambiguity when the matrix is 3x3.

I will write such a version and post it to octave-sources in the case
that someone else finds it useful. Send comments or suggestions.

Cheers,

Mario

%%%%%%<>%%%%%%<>%%%%%%<>%%%%%%<>%%%%%%<>%%%%%%<>%%%%%%<>%%%%%
Mario Alberto Storti               | Fax: (54)(42) 55.09.44 |
Grupo de Tecnologia Mecanica       | Tel: (54)(42) 55.91.75 |
INTEC, Guemes 3450 - 3000 Santa Fe | Home: Gob. Vera 3161   |
Argentina                          |       (54)(42) 55.00.23|
Reply: [hidden email]  |                        |
                http://venus.unl.edu.ar/gtm-eng.html        |