Boost logo

Ublas :

From: Paul C. Leopardi (leopardi_at_[hidden])
Date: 2006-03-10 17:06:04


Hi Alion,
I'm curious. What result do you expect to see when multiplying matrices of
different size? The usual definition of matrix multplication only defines a
result when an m row x n column matrix A is multiplied by an n row x p column
matrix B, A*B=C, where C has m rows and p columns.

The definition can be generalized by embedding both A and B into q x q
matrices a and b (where q = max (m,n,p)) which contain zeros outside of the
extent of A and B, ie by treating both A and B as upper left hand submatrices
of larger matrices of the same size which have zeros elsewhere. The matrices
a and b are square and large enough to contain each of A and B. The result of
a*b is c which is a q x q matrix, which can then be truncated to include only
the leading nonzero rows and columns. AFAIK, this is rarely done in practice,
and the usual definition is the one implemented in linear algebra packages
and libraries such as uBLAS.

Sorry, my description above may have been confusing. Following is an example
in Matlab.

Best regards, Paul Leopardi

>> A=[1 2;3 4;5 -6];
>> size(A)

ans =

     3 2

>> B=[1 2 3 4;4 3 2 1];
>> size(B)

ans =

     2 4

>> C=A*B

C =

     9 8 7 6
    19 18 17 16
   -19 -8 3 14

>> size(C)

ans =

     3 4

>> B*A
??? Error using ==> mtimes
Inner matrix dimensions must agree.

>> a=zeros(4,4);
>> a(1:3,1:2)=A

a =

     1 2 0 0
     3 4 0 0
     5 -6 0 0
     0 0 0 0

>> b=zeros(4,4);
>> b(1:2,1:4)=B

b =

     1 2 3 4
     4 3 2 1
     0 0 0 0
     0 0 0 0

>> a*b

ans =

     9 8 7 6
    19 18 17 16
   -19 -8 3 14
     0 0 0 0

>> b*a

ans =

    22 -8 0 0
    23 8 0 0
     0 0 0 0
     0 0 0 0

On Saturday 11 March 2006 08:17, Alion Sci wrote:
> I have two matrices, m1 and m2. So long as they are the same size
> (currently both are 2x2) then the following statement works just fine:
>
> cout << prod(m1, m2) << endl;
>
> However, when I change the size of either matrices I get the following
> error at compile time:
>
> Assertion failed in file /usr/include/boost/numeric/ublas/functional.hpp at
> line 1212:
> size1 == size2
> terminate called after throwing an instance of
> 'boost::numeric::ublas::bad_argument'
> what(): bad argument
> Aborted
>
>
> Does anyone know what I am doing wrong?