如何通过RealMatrix乘以RealVector?

如何通过RealMatrix乘以给定的RealMatrix ? 我在两个类上找不到任何“乘法”方法,只有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 = mxp final RealVector p2 = m.preMultiply(p); // prints {3; 4; 5; 87} // expected {9; 11; 13; 1} System.out.println(p2); 

请将实际结果与预期结果进行比较。

是否还有一种方法可以将Vector3D乘以4×4 RealMatrix ,其中w组件被丢弃? (我不是在寻找自定义实现,而是在库中已经存在的方法)。

preMultiply不会给你mxp而是pxm 。 这适用于您的问题,但不适用于您的评论// p2 = mxp

要获得您想要的结果,您有两个选择:

  1. 使用生成mxp RealMatrix#operate(RealVector)

     RealVector mxp = m.operate(p); System.out.println(mxp); 
  2. 在预乘之前转置矩阵:

     RealVector pxm_t = m.transpose().preMultiply(p); System.out.println(pxm_t); 

结果:

{9; 11; 13; 1}