矩阵乘法alglib

发布于 2024-10-31 18:41:44 字数 78 浏览 0 评论 0原文

如何将两个矩阵与 AlgLib 相乘

How do I multiply two matrices with AlgLib

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(3

迷鸟归林 2024-11-07 18:41:44

免责声明:我没有使用过 AlgLib;我只是按照文档似乎所说的内容进行操作。我很乐意得到更专家的纠正。

不管怎样,恐怕答案似乎是你需要使用 cmatrixgemmrmatrixgemm (哪一个取决于你的矩阵是实矩阵还是复矩阵),如下所示:

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

其中:

  • mnk 是矩阵的大小(Am code> 乘 kBknCm 乘以 n),
  • 1 是乘积的乘积(如果您碰巧想要 3AB 而不是 AB,请输入 3 代替)
  • A,0,0,0B,0,0,0 组是:矩阵、行偏移、列偏移、运算类型
    • 操作类型为 0 表示按原样使用 A 或 B,1 表示使用转置,2 表示使用共轭转置(当然不能使用 2 表示 rmatrixgemm)< /里>
  • 下一个 0 表示将 0*C 添加到结果中(如果在这里输入 0,则 C 中的初始值将被完全忽略)
  • C 之后的两个 0 是行和列偏移量

您可能会认为这种通用性有点过分了,应该有一个更简单的函数来提供这些默认值。我不会不同意这一点,但据我所知,AlgLib 中没有这样更简单的函数。您可能想编写自己的代码(只需调用 rmatrixgemmcmatrixgemm)。

(为什么有这么多通用性?因为进行有效的矩阵乘法需要相当复杂的代码,并且它本质上与您需要执行更通用的 C=af(A).g(B)+bC< /code> *matrixgemm 执行的操作,有时更通用的操作很有用。)

编辑添加了一些可能有用的注释。

  • 偏移量使您可以使用子矩阵进行操作。能够做到这一点在某些数值算法中很有用。我假设 mnk 是您正在使用的子矩阵的大小;在常见情况下,它们将与数组的维度相同,并且偏移量将为零。
  • 在调用 rmatrixgemm 或 cmatrixgemm 之前,数组本身需要存在并且具有适当的大小。至少,AB 确实如此; C 作为 ref 传递,因此如果输入时为 null,这些函数可能会创建它。
  • 您可能会从 rmatrixgemmcmatrixgemm 的签名中认为 AB 被复制,而 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:

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 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.
可爱暴击 2024-11-07 18:41:44

只是为了确认加雷奇所写的内容:

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}}
梦魇绽荼蘼 2024-11-07 18:41:44

在 vba 中我能够使用这个函数的复杂版本。

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)

附带说明一下,通过将所有 alglib 模块加载到一个 access 数据库中,然后从需要函数的当前数据库设置对该数据库的引用,可以将整套 alglib 函数加载到任何 access 程序中。这使得它对于工作数据库来说非常方便和轻量。

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.

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文