![]() |
Blitz Support : |
From: Marc Vinyes (mvinyes_at_[hidden])
Date: 2005-05-07 10:53:19
Thank you very much Derrick Bass and specially Julian C. for you clear and
detailed explanation.
Now I see that I totally misunderstood the meaning of the * operator and I
thought it was the matrix "standard multiplication" (from a mathematical
point of view).
I suggest adding an entry to the FAQ because I think that this issue might
confuse other people in the future...
> Blitz Arrays are intended to perform elementwise math operations and that
> is
> what the operator*() does. You get a 2x1 result Array because that is
> what
> you allocated.
Finally, as my algorithms use the matrix standard multiplication quite
often and I appreciate them to be overloaded (to code fast), I have
decided to use octave's classes or newmat's library.
However I will certainly store your function for furher uses of blitz in
other problems where the standard-*-matrix overloading is less useful or
in a final efficient implementation of the algorithm.
Again, thank you.
Sincerely,
MarC
>The result Array A is filled by marching through Arrays B
> and C in elementwise order and performing multiplication. These are
> row-major Arrays, so we get 1x1=1 and 2x2=4 for the results that are
> stored
> into A. Of course, this is not the semantics you are looking for. What
> you
> want is to loop over the rows of B and the columns of C and perform inner
> products to get the resulting element values of A. You can do this with
> the
> proper use of Array slicing and summation.
>
> void matmatProduct(Array<int,2> A, Array<int,2> B, Array<int,2> C) {
> int rows = B.rows();
> int columns = C.columns();
> assert(B.columns() == C.rows()); // sanity check
> assert(A.rows()==rows && A.columns()==columns); // could reshape A here
> instead
> for (int i=0; i<rows; ++i)
> for (int j=0; j<columns; ++j)
> A(i,j) = sum(B(i,Range::all())*C(Range::all(),j));
> }
>
> Sorry there is no simple way to do this by overloading the standard *
> notation, but this function will give you the result you expected.
>
> Regards, Julian C.
>
> Dr. Julian C. Cummings Office: PB-111
> Caltech/CACR, MC 158-79 Phone: 626-395-2543
> 1200 E. California Blvd. Fax: 626-584-5917
> Pasadena, CA 91125