如何通过RealMatrix乘以RealVector? [英] How to multiply a RealVector by a RealMatrix?

查看:123
本文介绍了如何通过RealMatrix乘以RealVector?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

如何通过 RealMatrix 乘以给定的 RealVector ?我在两个类上都找不到任何乘法方法,只有 preMultiply 但似乎不起作用:

How can I multiply a given RealVector by a RealMatrix? I can't find any "multiply" method on both classes, only preMultiply but it seems not work:

// 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);

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

Please compare the actual result with the expected result.

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

Is there also a way for multiplying a Vector3D by a 4x4 RealMatrix where the w component is throw away? (I'm looking not for custom implementation but an already existing method in the library).

推荐答案

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

preMultiply does not give you m x p but p x m. This would be suitable for your question but not for your comment // p2 = m x p.

要获得结果你想要你有两个选择:

To get the result you want you have two options:


  1. 使用 RealMatrix #open(RealVector)产生 mxp

RealVector mxp = m.operate(p);    
System.out.println(mxp);    


  • 在预乘之前转置矩阵:

  • Transpose the matrix before pre-multiplying:

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


  • 结果:


    {9; 11; 13; 1}

    {9; 11; 13; 1}

    这篇关于如何通过RealMatrix乘以RealVector?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

    查看全文
    登录 关闭
    扫码关注1秒登录
    发送“验证码”获取 | 15天全站免登陆