如何将给定的RealVectorRealMatrix相乘?我在两个类上都找不到任何“乘”方法,只有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}

    07-28 00:07