Blitz logo

Blitz Support :

From: Julian Cummings (cummings_at_[hidden])
Date: 2004-01-06 13:55:35


Hi,

There are (incomplete and not well documented) Matrix classes in blitz
that perform the expected matrix-matrix multiply behavior. The *
operation for Arrays does an elementwise multiplication, which is not
what you intended. Also, you normally need to allocate the array that
receives a result before assigning it. Below is a program using the
blitz Matrix class that does what your original program was supposed to do.

Regards, Julian C.

#include <blitz/matrix.h>

using namespace blitz;

int main(int argc, char* argv[])
{
    Matrix<double> m1(1,100);
    Matrix<double> m2(100,24);

    for (int i=0; i<100; ++i) {
            m1(0,i) = 0;
            for (int j=0; j<24; ++j)
                m2(i,j) = i;
        }

    cout << m1 << endl;
    cout << m2 << endl;

    Matrix<double> m3(1,24);
        m3 = m1 * m2;

    cout << m3 << endl;
}

Google Poster wrote:

>Please forgive my ignorance - I am not a
>mathematician.
>
>I am involved in a project that requires high
>performance math. I have been replacing the old
>legacy routines with blitz++ and have been
>experiencing encouraging results thus far. I have hit
>a problem with matrix multiplication and I am sure the
>root cause is my lack of understand.
>
>I *think*.
>
>A matrix with dimensions A x B can be multiplied by a
>matrix with dimensions B x C to produce a matrix of
>dimensions A x C.
>
>For this code:
>
>// ---------------------------------------------
>#include <blitz/array.h>
>
>using namespace blitz;
>
>int main(int argc, char* argv[])
>{
> double* t = new double[100];
> double* t2 = new double[2400];
>
> Array<double,2> m1( t , shape( 1 , 100 ) ,
>neverDeleteData );
> Array<double,2> m2( t2 , shape( 100 , 24 ) ,
>neverDeleteData ,blitz::ColumnMajorArray<2>() );
>
> blitz::firstIndex iw;
>
> m1 = iw;
> m2 = iw;
>
> cout << m1 << endl;
> cout << m2 << endl;
>
> Array<double,2> m3( m1 * m2 );
>
> cout << m3 << endl;
>}
>// ----------------------------------------
>
>M3 is a matrix of 1 x 1 - I would expect 1 x 24
>matrix.
>
>Any assistance would be gratefully appreciated.
>
>--
>gp
>
>__________________________________
>Do you Yahoo!?
>New Yahoo! Photos - easier uploading and sharing.
>http://photos.yahoo.com/
>_______________________________________________
>Blitz-support mailing list
>Blitz-support_at_[hidden]
>http://www.oonumerics.org/mailman/listinfo.cgi/blitz-support
>
>

-- 
Dr. Julian C. Cummings                       E-mail: cummings_at_[hidden]
California Institute of Technology           Phone:  626-395-2543
1200 E. California Blvd., Mail Code 158-79   Fax:    626-584-5917
Pasadena, CA 91125