如何将 RealVector 乘以 RealMatrix?
How to multiply a RealVector by a RealMatrix?
如何将给定的 RealVector
乘以 RealMatrix
?我在两个 类 上都找不到任何 "multiply" 方法,只有 preMultiply
但它似乎不起作用:
// point to translate
final RealVector p = MatrixUtils.createRealVector(new double[] {
3, 4, 5, 1
});
// translation matrix (6, 7, 8)
final RealMatrix m = MatrixUtils.createRealMatrix(new double[][] {
{1, 0, 0, 6},
{0, 1, 0, 7},
{0, 0, 1, 8},
{0, 0, 0, 1}
});
// p2 = m x p
final RealVector p2 = m.preMultiply(p);
// prints {3; 4; 5; 87}
// expected {9; 11; 13; 1}
System.out.println(p2);
请将实际结果与预期结果进行比较。
是否还有一种方法可以将 Vector3D
乘以 4x4 RealMatrix
,其中 w 分量被丢弃? (我不是在寻找自定义实现,而是在寻找库中已经存在的方法)。
preMultiply
不给你m x p
而是p x m
。这适合您的问题,但不适合您的评论 // p2 = m x p
.
要获得您想要的结果,您有两种选择:
使用 RealMatrix#operate(RealVector)
生成 m x p
:
RealVector mxp = m.operate(p);
System.out.println(mxp);
预乘前转置矩阵:
RealVector pxm_t = m.transpose().preMultiply(p);
System.out.println(pxm_t);
结果:
{9; 11; 13; 1}
如何将给定的 RealVector
乘以 RealMatrix
?我在两个 类 上都找不到任何 "multiply" 方法,只有 preMultiply
但它似乎不起作用:
// point to translate
final RealVector p = MatrixUtils.createRealVector(new double[] {
3, 4, 5, 1
});
// translation matrix (6, 7, 8)
final RealMatrix m = MatrixUtils.createRealMatrix(new double[][] {
{1, 0, 0, 6},
{0, 1, 0, 7},
{0, 0, 1, 8},
{0, 0, 0, 1}
});
// p2 = m x p
final RealVector p2 = m.preMultiply(p);
// prints {3; 4; 5; 87}
// expected {9; 11; 13; 1}
System.out.println(p2);
请将实际结果与预期结果进行比较。
是否还有一种方法可以将 Vector3D
乘以 4x4 RealMatrix
,其中 w 分量被丢弃? (我不是在寻找自定义实现,而是在寻找库中已经存在的方法)。
preMultiply
不给你m x p
而是p x m
。这适合您的问题,但不适合您的评论 // p2 = m x p
.
要获得您想要的结果,您有两种选择:
使用
RealMatrix#operate(RealVector)
生成m x p
:RealVector mxp = m.operate(p); System.out.println(mxp);
预乘前转置矩阵:
RealVector pxm_t = m.transpose().preMultiply(p); System.out.println(pxm_t);
结果:
{9; 11; 13; 1}