How do I multiply two matrices with AlgLib
3 Answers
Disclaimer: I haven't used AlgLib; I'm just going by what the documentation seems to say. I'd be happy to be corrected by someone more expert.
Anyway, I'm afraid the answer seems to be that you need to use cmatrixgemm
or rmatrixgemm
(which one depends on whether your matrices are real or complex), like this:
rmatrixgemm(m,n,k, 1, A,0,0,0, B,0,0,0, 0, C,0,0);
where:
m
,n
,k
are the sizes of the matrices (A
ism
byk
,B
isk
byn
,C
ism
byn
)- the
1
is what to multiply the product by (if you happen to want, say, 3AB instead of AB, put3
there instead) - the
A,0,0,0
andB,0,0,0
groups are: matrix, row offset, column offset, operation type- the operation type is 0 to use A or B as it is, 1 to use the transpose, and 2 to use the conjugate transpose (of course you can't use 2 for
rmatrixgemm
)
- the operation type is 0 to use A or B as it is, 1 to use the transpose, and 2 to use the conjugate transpose (of course you can't use 2 for
- the next
0
says to add 0*C to the result (if you put 0 here then the initial values in C are completely ignored) - the two
0
s after C are a row and column offset
You might perhaps think that that level of generality is overkill, and that there should be a simpler function that provides those defaults. I wouldn't disagree with that, but so far as I can see there's no such simpler function in AlgLib. You might want to write your own (which would just call rmatrixgemm
or cmatrixgemm
).
(Why so much generality? Because doing an efficient matrix multiplication requires quite sophisticated code, and it's essentially the same quite sophisticated code as you need to do the more general C=a.f(A).g(B)+b.C
operation that *matrixgemm
does, and sometimes that more general operation is useful.)
EDITED to add a few more remarks that might be useful.
- The offsets are so that you can do things with submatrices. Being able to do this is useful in some numerical algorithms. I assume
m
,n
,k
are the sizes of the submatrices you're using; in the common case, they'll be the same as the dimensions of your arrays, and the offsets will be zero. - The arrays themselves need to exist and be of appropriate sizes before you call
rmatrixgemm
orcmatrixgemm
. At least,A
andB
certainly do;C
is passed as aref
so it's possible that these functions will create it if it'snull
on entry. - You might think from the signature of
rmatrixgemm
orcmatrixgemm
thatA
andB
get copied whereasC
is passed by reference, but if I'm not totally confused about C#'s semantics they're all effectively passed by (object) reference.

- 19,888
- 1
- 41
- 62
-
4wow... there was no way I could have figured that out by reading the manual ... thanks – Nestor Apr 09 '11 at 21:16
-
1For a less general, simpler function, it's always possible to create a function which uses `rmatrixgemm` internally with hard-coded dummy arguments. – heltonbiker Feb 18 '13 at 13:23
Just to confirm what Garech wrote:
double[,] a = new double[,] {
{1,2,3},
{4,5,6}
};
double[,] b = new double[,] {
{7,8,9,10},
{11,12,13,14},
{15,16,17,18}
};
int m = a.GetLength(0);
int n = b.GetLength(1);
int k = a.GetLength(1);
double[,] c = new double[m,n];
alglib.rmatrixgemm(m, n, k, 1, a, 0,0,0, b,0,0,0, 0, ref c, 0,0);
//c = {{74, 80, 86, 92}, {173, 188, 203, 218}}

- 13,706
- 11
- 78
- 119
In vba I was able to use the complex version of this function.
Alpha.x = 1: Alpha.y = 0
Beta.x = 0: Beta.y = 0
Call CMatrixGEMM(4, 1, 4, Alpha, r, 0, 0, 0, x, 0, 0, 0, Beta, RX, 0, 0)
as a side note the entire set of alglib functions can be loaded into any access program by loading all of the alglib modules into one access database and then setting a reference to that database from the current database where the functions are needed. This makes it very convenient and lightweight to the working database.

- 40,997
- 14
- 121
- 129

- 21
- 3
-
This library is also available for vb6 here: http://www.planet-source-code.com/vb/scripts/ShowCode.asp?txtCodeId=74940&lngWId=1 – Testautomation Oct 21 '13 at 13:50