# Re: Bug/feature in matrix multiply

```
David Fanning wrote in message ...
>Mark Fardal (fardal@weka.phast.umass.edu) writes:
>
>> is the following a bug or feature?  I don't understand why changing the
>> type of the array changes the dimensions of the result.  Then again,
>> it's late on Friday, so my brain might just be mush.
>>
>> IDL> junk=fltarr(3)
>> IDL> junk=reform(junk,3,1)
>> IDL> help,junk
>> JUNK            FLOAT     = Array[3, 1]
>> IDL> help,[3.,2.,1.]#junk
>> <Expression>    FLOAT     = Array[1]
>> IDL> help,[3.d0,2.d0,1.d0]#junk
>> <Expression>    DOUBLE    = Array[3, 3]
>
>The latter will make a column vector, which makes more
>sense when multiplied by a row vector. What kind of result
>were you expecting? From my reading of the # operator
>I think the result with the floating array is correct.
>I don't have a clue why the double expression does what
>it does. :-(

Whichever Mark intended, the result bothered me so i have put together
another test for "Matrix multiplication".
The first 4 expresions mathematically are legal,  the latter 4 should be
caught as errors (or do i need to go back to math class!) Two of the latter
are caught as illegal, the other two can only be described as "creative" in
their result and certainly do not exhibit behaviour detailed in the half
page of help given to the subject of matric multiplication.

IDL> print, reform([0,1,0],3,1)##reform([1,2,3],1,3)
2
{1row vector (row column multiplication) 1col vector => ok}

IDL> print, reform([0,1,0],1,3)##reform([1,2,3],3,1)
0           0           0
1           2           3
0           0           0

{1col (rc mult)1row => ok}

IDL> print, reform([0,1,0],1,3)#reform([1,2,3],3,1)
2
{1col (cr mult)1row => ok}

IDL> print, reform([0,1,0],3,1)#reform([1,2,3],1,3)
0           1           0
0           2           0
0           3           0
{1row (cr mult)1col => ok}

IDL> print, reform([0,1,0],1,3)##reform([1,2,3],1,3)
% Operands of matrix multiply have incompatible dimensions: <INT
Array[1, 3]>, <INT       Array[1, 3]>.
% Execution halted at:  \$MAIN\$
IDL> print, reform([0,1,0],1,3)#reform([1,2,3],1,3)
% Operands of matrix multiply have incompatible dimensions: <INT
Array[1, 3]>, <INT       Array[1, 3]>.
% Execution halted at:  \$MAIN\$
{ good: these should not be allowed}

IDL> print, reform([0,1,0],3,1)#reform([1,2,3],3,1)
0           1           0
0           2           0
0           3           0
{1row (cr mult)1row => ILLEGAL!} it would appear IDL has transposed the
second (row) vector to give a legal input.

IDL> print, reform([0,1,0],3,1)##reform([1,2,3],3,1)
0           0           0
1           2           3
0           0           0
{1row (rc mult)1row => ILLEGAL!} again IDL has transposed the second (row)
vector to give a legal input. }

This was on Idl 5.2, does anyone want to check whether the behaviour is the
same with older versions?
Martin

Martin Downing
Research Physicist,
Grampian Orthopaedic Clinical Research Unit,
Woodend Hospital, Grampian Healthcare Trust,
Eday Road, Aberdeen, Scotland. AB15 6LS
mailto:m.downing@abdn.ac.uk

```