From bug-octave-request at bevo dot che dot wisc dot edu Thu Feb 18 04:45:10 1999 Subject: Unidentified subject! From: Rolf Fabian To: bug-octave at bevo dot che dot wisc dot edu Date: Thu, 18 Feb 1999 04:45:08 -0600 (CST) Hi, incorrect transposition operator " ' " instead of correct " .' " for complex input to script files (already reported 990216 for 'polyfit') can also be found in scripts 'dot.m' and 'cross.m'. EXAMPLE to reproduce the bugs: :)x=1+[1,1,-1]*i; y=1+[1,1,1]*i; ############################## # (A) preference for columns # ############################## :)prefer_column_vectors = 1; :)dot(x,y),dot(x.',y),dot(x.',y.'),dot(x,y.') ans = 4 - 2i ans = 2 + 4i ans = 4 - 2i ans = 2 + 4i #REM: this is obviously buggy # we get 2 different results depending only on # row/column orientation of input :)cross(x,y),cross(x.',y),cross(x.',y.'),cross(x,y.') ans = -2 - 2i 2 + 2i 0 + 0i #BUG: WHY DO WE GET A ROW AT COLUMN-PREFERENCE? ans = -2 + 2i #BUG: CONJUGATE COMPLEX TO 1st ANSWER ABOVE 2 - 2i 0 + 0i ans = -2 + 2i 2 - 2i 0 + 0i ans = -2 + 2i 2 - 2i 0 + 0i ################################# # (B) no preference for columns # ################################# :)prefer_column_vectors = 0; :)cross(x,y),cross(x.',y),cross(x.',y.'),cross(x,y.') ans = -2 - 2i 2 + 2i 0 + 0i ans = -2 - 2i 2 + 2i 0 + 0i ans = -2 + 2i #BUG: WHY DO WE GET A COLUMN AT ROW-PREFERENCE? 2 - 2i #BUG: CONJUGATE COMPLEX TO OTHER ANSWERS 0 + 0i ans = -2 - 2i 2 + 2i 0 + 0i ####################################### #FIX -> see comments in scripts below # ####################################### #application of these fixes result in unique correct answers to our example :)dot(x,y),dot(x.',y),dot(x.',y.'),dot(x,y.') ans = 2 + 4i ans = 2 + 4i ans = 2 + 4i ans = 2 + 4i :)prefer_column_vectors=0 #we expect rows! :)cross(x,y),cross(x.',y),cross(x.',y.'),cross(x,y.') ans = -2 + 2i 2 - 2i 0 + 0i #ROWS ok ans = -2 + 2i 2 - 2i 0 + 0i ans = -2 + 2i 2 - 2i 0 + 0i ans = -2 + 2i 2 - 2i 0 + 0i :)prefer_column_vectors=1; #we expect columns! :)cross(x,y),cross(x.',y),cross(x.',y.'),cross(x,y.') ans = -2 + 2i #COLUMNS ok 2 - 2i 0 + 0i ans = -2 + 2i 2 - 2i 0 + 0i ans = -2 + 2i 2 - 2i 0 + 0i ans = -2 + 2i 2 - 2i 0 + 0i I suspect even more script to be candidates for the subtle bug inferred by an incorrect usage of " ' " instead of ".' " -operator if only a change in orientation is requested. I assume, authors tend to check their procedures only at real-type input so that this type of bugs are not detected even for a long time, e.g. 'cross' dates back to 1994 ! Wouldn't it be a good idea to implement more complex-input-type tests even for basic routines into the test-suite? Furthermore, I suggest to give an easy-to-find hint in the manual, that " ' " is *NOT* the appropriate operator for simple tranposition operation only. Bye Rolf Fabian 990217 Atmospheric Chemistry and Air Quality Dep. Technical University of Cottbus / FRG ##------------------------------------------ ## usage: dot (x, y) ## ## Computes the dot product of two vectors. ## Author: jwe ## Debug : fabian at tu-cottbus dot de 990217 function z = dot (x, y) if (nargin != 2) usage ("dot (x, y)"); endif if (is_vector (x) && is_vector (y) && length (x) == length (y)) [x_nr, x_nc] = size (x); [y_nr, y_nc] = size (y); if (x_nr == 1) if (y_nr == 1) #z = x * y'; #original line BUGGY for complex y, correct: z = x*y.'; else z = x * y; endif else if (y_nr == 1) z = y * x; else #z = y' * x; #original line BUGGY for complex y, correct: z = y.'* x; endif endif else error ("dot: both arguments must be vectors of the same length"); endif endfunction ##------------------------------------------ ## usage: cross (x, y) ## ## Computes the vector cross product of the two 3-dimensional vectors ## x and y. ## Author: KH ## Created: 15 October 1994 ## Adapted-By: jwe ## Debug : fabian at tu-cottbus dot de 990217 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)]; #**---- REMOVE WHOLE BLOCK OF ORIGINAL LINES, THEY LEAD TO THE 'ORIENTATION BUG' #** x_nr = rows (x); #** y_nr = rows (y); #** #** if ((x_nr == y_nr && x_nr == 1) #** || (x_nr != y_nr && ! prefer_column_vectors)) #**---- REMOVE WHOLE BLOCK OF ORIGINAL LINES if(!prefer_column_vectors)) #FIX fabian at tu-cottbus dot de (MUCH MORE SIMPLE!) #z = z'; #BUGGY original line for complex vectors x,y z = z.'; #FIX fabian at tu-cottbus dot de endif else error ("cross: both x and y must be 3-dimensional vectors"); endif endfunction