Matrix multiplier

How do I multiply two matrices with AlgLib

+4
source share
3 answers

Disclaimer: I have not used AlgLib; I just understand what the documentation says. I would be glad if someone else had corrected me.

In any case, I'm afraid the answer seems like you need to use cmatrixgemm or rmatrixgemm (which depends on whether your matrices are real or complex), for example:

 rmatrixgemm(m,n,k, 1, A,0,0,0, B,0,0,0, 0, C,0,0); 

Where:

  • m , n , k - matrix sizes ( A - m by k , B - k by n , C is m by n )
  • 1 is that to multiply the product by (if you want, say, 3AB instead of AB, put 3 there)
  • groups A,0,0,0 and B,0,0,0 are: matrix, row offset, column offset, type of operation
    • the type of operation is 0 to use A or B as is, 1 to use transpose and 2 to use a transponder pairing (of course, you cannot use 2 for rmatrixgemm )
  • the next 0 says add 0 * C to the result (if you put 0 here, then the initial values ​​in C are completely ignored)
  • two 0 after C - row and column offset

Perhaps you might think that this level of generality is redundant, and that there should be a simpler function providing these default values. I would not agree with this, but as far as I can see, AlgLib does not have such a simpler function. You might want to write your own (which just calls rmatrixgemm or cmatrixgemm ).

(Why is there so much generality? Since effective matrix multiplication requires quite complex code, and it is essentially the same rather complicated code as for the more general operation C=af(A).g(B)+bC , which *matrixgemm does , and sometimes a larger general operation is useful.)

EDITED add some more comments that may be helpful.

  • Offsets are that you can do something with sub-matrices. The ability to do this is useful in some numerical algorithms. I assume that m , n , k are the sizes of the submatrices you use; in general, they will be the same as the sizes of your arrays, and the offsets will be zero.
  • When rmatrixgemm or cmatrixgemm the arrays themselves must exist and be sized accordingly. At least A and B course; C is passed as ref , so it is possible that these functions will create it if it is null on entry.
  • You can think from the signature of rmatrixgemm or cmatrixgemm that A and B get copy, while C is passed by reference, but if I'm not completely confused by the C # semantics, they are all effectively passed by reference (object).
+8
source

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}} 
+3
source

In vba, I managed to use a 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 an additional note, the entire set of alglib functions can be loaded into any access program by loading all alglib modules into one access database and then linking to this database from the current database where functions are needed. This makes it very convenient and easy for a working database.

0
source

Source: https://habr.com/ru/post/1347384/


All Articles