Clojure matrix multiplication error: "Mismatched vector sizes" (clojure.core.matrix)
Clojure matrix multiplication error: "Mismatched vector sizes" (clojure.core.matrix)
我有一个大小为 [1, 10] 的矩阵和另一个大小为 [1, 50] 的矩阵(它们都是向量 - 一个将被转置),我想将其相乘。我可以在 MATLAB 中执行此操作,但 Clojure library 我正在使用 return 一个错误,表明矢量形状不匹配。
这是 Clojure 代码(失败)
(def A [-0.4300 0.8205 0.3060 0.7011 0.3717 0.3790 0.6332 0.6179 0.5414 0.7277])
(m/shape A)
(def B [1 4.5239 1.0 4.54133 4.17334 1.0 2.3195 1.25481 2.57760 0.999 1.71030 1.167121 0.996 1.0 1.0 1.0 1.0 2.42060 4.53421 1.0 3.81672 2.26177 1.412147 1.13449 4.22844 1.87670 1.42931 4.13310 1.0 3.06024 1.0 0.999 1.02989 8.92018 8.90729 6.60117 2.61610 7.31420 1.0 4.23987 0.999 1.05592 5.31238 1.0 1.0 0.999 7.97549 1.6177 1.0 1.0])
(m/shape B)
(m/mmul (m/transpose A) B)
MATLAB 中的等效运算:
A = [-0.4300 0.8205 0.3060 0.7011 0.3717 0.3790 0.6332 0.6179 0.5414 0.7277];
size(A)
B = [1 4.5239 1.0 4.54133 4.17334 1.0 2.3195 1.25481 2.57760 0.999 1.71030 1.167121 0.996 1.0 1.0 1.0 1.0 2.42060 4.53421 1.0 3.81672 2.26177 1.412147 1.13449 4.22844 1.87670 1.42931 4.13310 1.0 3.06024 1.0 0.999 1.02989 8.92018 8.90729 6.60117 2.61610 7.31420 1.0 4.23987 0.999 1.05592 5.31238 1.0 1.0 0.999 7.97549 1.6177 1.0 1.0];
size(B)
C = A' * B;
size(C)
显然该操作在数学上是可行的 - 它应该 return 一个 [10, 50] 矩阵...我实现计算的方式是否有错误或者这是一个错误?
(let [A [[-0.4300 0.8205 0.3060 0.7011 0.3717 0.3790 0.6332 0.6179 0.5414 0.7277]]
B [[1 4.5239 1.0 4.54133 4.17334 1.0 2.3195 1.25481 2.57760 0.999 1.71030 1.167121 0.996 1.0 1.0 1.0 1.0 2.42060 4.53421 1.0 3.81672 2.26177 1.412147 1.13449 4.22844 1.87670 1.42931 4.13310 1.0 3.06024 1.0 0.999 1.02989 8.92018 8.90729 6.60117 2.61610 7.31420 1.0 4.23987 0.999 1.05592 5.31238 1.0 1.0 0.999 7.97549 1.6177 1.0 1.0]]]
(mmul (transpose A) B))
[2 3 5]
不是矩阵而是向量。
[[2 3 5]]
是一个矩阵。
根据 mmul
文档,根据参数位置,转置隐含在向量中:
Performs matrix multiplication on matrices or vectors. Equivalent to
inner-product when applied to vectors. Will treat a 1D vector roughly as a
1xN matrix (row vector) when it's the first argument, or as an Nx1 matrix
(column vector) when it's the second argument--except that the dimensionality
of the result will be different from what it would be with matrix arguments.
所以,你不应该转置自己。
我有一个大小为 [1, 10] 的矩阵和另一个大小为 [1, 50] 的矩阵(它们都是向量 - 一个将被转置),我想将其相乘。我可以在 MATLAB 中执行此操作,但 Clojure library 我正在使用 return 一个错误,表明矢量形状不匹配。
这是 Clojure 代码(失败)
(def A [-0.4300 0.8205 0.3060 0.7011 0.3717 0.3790 0.6332 0.6179 0.5414 0.7277])
(m/shape A)
(def B [1 4.5239 1.0 4.54133 4.17334 1.0 2.3195 1.25481 2.57760 0.999 1.71030 1.167121 0.996 1.0 1.0 1.0 1.0 2.42060 4.53421 1.0 3.81672 2.26177 1.412147 1.13449 4.22844 1.87670 1.42931 4.13310 1.0 3.06024 1.0 0.999 1.02989 8.92018 8.90729 6.60117 2.61610 7.31420 1.0 4.23987 0.999 1.05592 5.31238 1.0 1.0 0.999 7.97549 1.6177 1.0 1.0])
(m/shape B)
(m/mmul (m/transpose A) B)
MATLAB 中的等效运算:
A = [-0.4300 0.8205 0.3060 0.7011 0.3717 0.3790 0.6332 0.6179 0.5414 0.7277];
size(A)
B = [1 4.5239 1.0 4.54133 4.17334 1.0 2.3195 1.25481 2.57760 0.999 1.71030 1.167121 0.996 1.0 1.0 1.0 1.0 2.42060 4.53421 1.0 3.81672 2.26177 1.412147 1.13449 4.22844 1.87670 1.42931 4.13310 1.0 3.06024 1.0 0.999 1.02989 8.92018 8.90729 6.60117 2.61610 7.31420 1.0 4.23987 0.999 1.05592 5.31238 1.0 1.0 0.999 7.97549 1.6177 1.0 1.0];
size(B)
C = A' * B;
size(C)
显然该操作在数学上是可行的 - 它应该 return 一个 [10, 50] 矩阵...我实现计算的方式是否有错误或者这是一个错误?
(let [A [[-0.4300 0.8205 0.3060 0.7011 0.3717 0.3790 0.6332 0.6179 0.5414 0.7277]]
B [[1 4.5239 1.0 4.54133 4.17334 1.0 2.3195 1.25481 2.57760 0.999 1.71030 1.167121 0.996 1.0 1.0 1.0 1.0 2.42060 4.53421 1.0 3.81672 2.26177 1.412147 1.13449 4.22844 1.87670 1.42931 4.13310 1.0 3.06024 1.0 0.999 1.02989 8.92018 8.90729 6.60117 2.61610 7.31420 1.0 4.23987 0.999 1.05592 5.31238 1.0 1.0 0.999 7.97549 1.6177 1.0 1.0]]]
(mmul (transpose A) B))
[2 3 5]
不是矩阵而是向量。
[[2 3 5]]
是一个矩阵。
根据 mmul
文档,根据参数位置,转置隐含在向量中:
Performs matrix multiplication on matrices or vectors. Equivalent to inner-product when applied to vectors. Will treat a 1D vector roughly as a 1xN matrix (row vector) when it's the first argument, or as an Nx1 matrix (column vector) when it's the second argument--except that the dimensionality of the result will be different from what it would be with matrix arguments.
所以,你不应该转置自己。