在调用 rmatrixgemm 或 cmatrixgemm 之前,数组本身需要存在并且具有适当的大小。至少,A 和 B 确实如此; C 作为 ref 传递,因此如果输入时为 null,这些函数可能会创建它。
您可能会从 rmatrixgemm 或 cmatrixgemm 的签名中认为 A 和 B 被复制,而 C< /code> 是通过引用传递的,但如果我对 C# 的语义没有完全困惑的话,它们都是通过(对象)引用有效传递的。
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:
m,n,k are the sizes of the matrices (A is m by k, B is k by n, C is m by n)
the 1 is what to multiply the product by (if you happen to want, say, 3AB instead of AB, put 3 there instead)
the A,0,0,0 and B,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 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 0s 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 or cmatrixgemm. At least, A and B certainly do; C is passed as a ref so it's possible that these functions will create it if it's null on entry.
You might think from the signature of rmatrixgemm or cmatrixgemm that A and B get copied whereas C is passed by reference, but if I'm not totally confused about C#'s semantics they're all effectively passed by (object) reference.
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}}
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}}
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.
发布评论
评论(3)
免责声明:我没有使用过 AlgLib;我只是按照文档似乎所说的内容进行操作。我很乐意得到更专家的纠正。
不管怎样,恐怕答案似乎是你需要使用
cmatrixgemm
或rmatrixgemm
(哪一个取决于你的矩阵是实矩阵还是复矩阵),如下所示:其中:
m
、n
、k
是矩阵的大小(A
是m
code> 乘k
,B
是k
乘n
,C
是m
乘以n
),1
是乘积的乘积(如果您碰巧想要 3AB 而不是 AB,请输入3
代替)A,0,0,0
和B,0,0,0
组是:矩阵、行偏移、列偏移、运算类型rmatrixgemm
)< /里>0
表示将 0*C 添加到结果中(如果在这里输入 0,则 C 中的初始值将被完全忽略)0
是行和列偏移量您可能会认为这种通用性有点过分了,应该有一个更简单的函数来提供这些默认值。我不会不同意这一点,但据我所知,AlgLib 中没有这样更简单的函数。您可能想编写自己的代码(只需调用
rmatrixgemm
或cmatrixgemm
)。(为什么有这么多通用性?因为进行有效的矩阵乘法需要相当复杂的代码,并且它本质上与您需要执行更通用的
C=af(A).g(B)+bC< /code>
*matrixgemm
执行的操作,有时更通用的操作很有用。)编辑添加了一些可能有用的注释。
m
、n
、k
是您正在使用的子矩阵的大小;在常见情况下,它们将与数组的维度相同,并且偏移量将为零。A
和B
确实如此;C
作为ref
传递,因此如果输入时为null
,这些函数可能会创建它。rmatrixgemm
或cmatrixgemm
的签名中认为A
和B
被复制,而C< /code> 是通过引用传递的,但如果我对 C# 的语义没有完全困惑的话,它们都是通过(对象)引用有效传递的。
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
orrmatrixgemm
(which one depends on whether your matrices are real or complex), like this:where:
m
,n
,k
are the sizes of the matrices (A
ism
byk
,B
isk
byn
,C
ism
byn
)1
is what to multiply the product by (if you happen to want, say, 3AB instead of AB, put3
there instead)A,0,0,0
andB,0,0,0
groups are: matrix, row offset, column offset, operation typermatrixgemm
)0
says to add 0*C to the result (if you put 0 here then the initial values in C are completely ignored)0
s after C are a row and column offsetYou 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
orcmatrixgemm
).(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.
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.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.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.只是为了确认加雷奇所写的内容:
Just to confirm what Garech wrote:
在 vba 中我能够使用这个函数的复杂版本。
附带说明一下,通过将所有 alglib 模块加载到一个 access 数据库中,然后从需要函数的当前数据库设置对该数据库的引用,可以将整套 alglib 函数加载到任何 access 程序中。这使得它对于工作数据库来说非常方便和轻量。
In vba I was able to use the complex version of this function.
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.