Skip to content
Prev 11981 / 63424 Next

*s behaviour (PR#6633)

This does appear to be a shortcoming in the '*' code.  If I understand 
Berwin's concerns correctly, here is a simpler example with matrix and 
vector, which should generate a warning, but doesn't, and an example with 
two vectors, which does generate a warning:

 > matrix(1:6,2) * 1:4
      [,1] [,2] [,3]
[1,]    1    9    5
[2,]    4   16   12
 > 1:6 * 1:4
[1]  1  4  9 16  5 12
Warning message:
longer object length
         is not a multiple of shorter object length in: 1:6 * 1:4
 >

The operators "+", "-", "/", "%/%", "^", "&", and "|" behave in a similar 
manner (and switching the arguments does not change the behavior).

In S-plus 6.1, examples of this type stop with an error.

As far as I know, it is standard in the S language for "*" to treat both 
its arguments as vectors (dimension attributes are only used to construct 
the dimension attributes of the returned value). So as Berwin notes, the 
matrix * vector case should generate a warning when the lengths of the 
arguments are not multiples.

The problem appears to be in the functions lbinary() in logic.c and 
R_binary() in arithmetic.c -- length checking is not done at all if either 
x or y is an array.

The fix in arithmetic.c is to move the block of code:
from inside the else block of "if (xarray || yarray) {" to after the end of 
the else (so that it always gets executed) (also, as far as I can see the 
first "if" that sets mismatch=0 is redundant and could be deleted).

The fix in logic.c is to move the block of code:
from inside the else block of "if (xarray || yarray) {" to after the end of 
the else (so that it always gets executed).

With these changes (including the deletion of the redundant code) R 1.9.0 
compiled and ran "make tests" with errors only in "running code in 
'reg-tests-3.R'" (which appeared due to not being able to find packages 
MASS and survival) and differences in the internet tests (as expected.)

-- Tony Plate
At Monday 09:57 PM 3/1/2004, berwin@maths.uwa.edu.au wrote: