help-octave
[Top][All Lists]
Advanced

[Date Prev][Date Next][Thread Prev][Thread Next][Date Index][Thread Index]

Cross Product Results


From: John W. Eaton
Subject: Cross Product Results
Date: Fri, 19 Sep 1997 17:02:05 -0500

On 18-Sep-1997, Craig Earls <address@hidden> 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 via email to

[Prev in Thread] Current Thread [Next in Thread]