如何在Android中进行矩阵求逆和乘法?

发布于 2024-11-10 00:17:54 字数 512 浏览 1 评论 0原文

给定 2 个矩阵:

public float[] mRi = new float[16];  
public float[] mR = new float[16];  

的两个读数的输出

  • 这些将是来自SensorManager.getRotationMatrix(mR, x, y, z)
  • SensorManager.getRotationMatrix(mRi, x, y, z) code>

所以会有两个 4x4 矩阵,

我想得到以下方程的结果:

  • ResultMtrix=inverse(mRi)*mR

事实上我知道它是否适用于 invertM()multiplyMM() 但我不知道如何使用矩阵来做到这一点。

你能帮忙吗?

Given 2 matrices:

public float[] mRi = new float[16];  
public float[] mR = new float[16];  

These will be the outputs of two readings from

  • SensorManager.getRotationMatrix(mR, x, y, z) and
  • SensorManager.getRotationMatrix(mRi, x, y, z)

So there will be two 4x4 matrices which,

I want to get the result of the following equation:

  • ResultMtrix=inverse(mRi)*mR

In fact I have the idea whether it works with invertM() and multiplyMM() but I don't have a clue on how to do it with the matrices.

Can you help?

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(2

孤城病女 2024-11-17 00:17:54

嘿伙计,我假设它们是 4x4 矩阵(16 个元素)...您可以使用高斯乔丹消除 http://en.wikipedia.org/wiki/Gauss%E2%80%93Jordan_elimination 用于反演。矩阵乘法的描述随处可见,甚至比矩阵求逆还要多。

Hey guy, I assume they are 4x4 matrix (16 elements)... You can use Gauss-Jordan elimination http://en.wikipedia.org/wiki/Gauss%E2%80%93Jordan_elimination for inversion. Matrix multiplication is described about everywhere, even more than matrix inversion.

南渊 2024-11-17 00:17:54

您描述的矩阵实际上是一维向量,所以我假设您所说的实际上是转置。这种情况下的计算非常简单:

方法

// 1 row * 1 column
public static float scalarMultiplication (float[] m1, float[] m2) {
    if (m1.length != m2.length)
        throw new IllegalArgumentException("Vectors need to have the same length");
    float m = 0;
    for (int i=0; i<m1.length; i++)
        m += (m1[i]*m2[i]);
    return m;
}

// N rows * N columns
public static float[][] vectorMultiplication (float[] m1, float[] m2) {
    if (m1.length != m2.length)
        throw new IllegalArgumentException("Vectors need to have the same length");
    float[][] m = new float[m1.length][m1.length];
    for (int i=0; i<m1.length; i++)
        for (int j=0; j<m1.length; j++)
            m[i][j] = (m1[i]*m2[j]);
    return m;
}

测试

            float[] m1 = new float[16];
            float[] m2 = new float[16];

            for (int i=0; i<m1.length; i++) {
                m1[i]=i;
                m2[i]=i*i;
            }

            System.out.println ("Multiple is " + scalarMultiplication(m1, m2));
            float[][] m = vectorMultiplication(m1, m2);
            for (int i=0; i<m[0].length; i++) {
                for (int j=0; j<m[0].length; j++) {
                    System.out.print (m[i][j] +" ");
                }
                System.out.println();
            }

输出

Multiple is 14400.0
0.0 0.0 0.0 0.0 0.0 0.0 0.0 0.0 0.0 0.0 0.0 0.0 0.0 0.0 0.0 0.0 
0.0 1.0 4.0 9.0 16.0 25.0 36.0 49.0 64.0 81.0 100.0 121.0 144.0 169.0 196.0 225.0 
0.0 2.0 8.0 18.0 32.0 50.0 72.0 98.0 128.0 162.0 200.0 242.0 288.0 338.0 392.0 450.0 
0.0 3.0 12.0 27.0 48.0 75.0 108.0 147.0 192.0 243.0 300.0 363.0 432.0 507.0 588.0 675.0 
0.0 4.0 16.0 36.0 64.0 100.0 144.0 196.0 256.0 324.0 400.0 484.0 576.0 676.0 784.0 900.0 
0.0 5.0 20.0 45.0 80.0 125.0 180.0 245.0 320.0 405.0 500.0 605.0 720.0 845.0 980.0 1125.0 
0.0 6.0 24.0 54.0 96.0 150.0 216.0 294.0 384.0 486.0 600.0 726.0 864.0 1014.0 1176.0 1350.0 
0.0 7.0 28.0 63.0 112.0 175.0 252.0 343.0 448.0 567.0 700.0 847.0 1008.0 1183.0 1372.0 1575.0 
0.0 8.0 32.0 72.0 128.0 200.0 288.0 392.0 512.0 648.0 800.0 968.0 1152.0 1352.0 1568.0 1800.0 
0.0 9.0 36.0 81.0 144.0 225.0 324.0 441.0 576.0 729.0 900.0 1089.0 1296.0 1521.0 1764.0 2025.0 
0.0 10.0 40.0 90.0 160.0 250.0 360.0 490.0 640.0 810.0 1000.0 1210.0 1440.0 1690.0 1960.0 2250.0 
0.0 11.0 44.0 99.0 176.0 275.0 396.0 539.0 704.0 891.0 1100.0 1331.0 1584.0 1859.0 2156.0 2475.0 
0.0 12.0 48.0 108.0 192.0 300.0 432.0 588.0 768.0 972.0 1200.0 1452.0 1728.0 2028.0 2352.0 2700.0 
0.0 13.0 52.0 117.0 208.0 325.0 468.0 637.0 832.0 1053.0 1300.0 1573.0 1872.0 2197.0 2548.0 2925.0 
0.0 14.0 56.0 126.0 224.0 350.0 504.0 686.0 896.0 1134.0 1400.0 1694.0 2016.0 2366.0 2744.0 3150.0 
0.0 15.0 60.0 135.0 240.0 375.0 540.0 735.0 960.0 1215.0 1500.0 1815.0 2160.0 2535.0 2940.0 3375.0 

The matrices you are describing are actually uni-dimensional vectors, so I am assuming that what you call inverse is actually transpose. The calculation in that case is quite simple:

Methods

// 1 row * 1 column
public static float scalarMultiplication (float[] m1, float[] m2) {
    if (m1.length != m2.length)
        throw new IllegalArgumentException("Vectors need to have the same length");
    float m = 0;
    for (int i=0; i<m1.length; i++)
        m += (m1[i]*m2[i]);
    return m;
}

// N rows * N columns
public static float[][] vectorMultiplication (float[] m1, float[] m2) {
    if (m1.length != m2.length)
        throw new IllegalArgumentException("Vectors need to have the same length");
    float[][] m = new float[m1.length][m1.length];
    for (int i=0; i<m1.length; i++)
        for (int j=0; j<m1.length; j++)
            m[i][j] = (m1[i]*m2[j]);
    return m;
}

Test

            float[] m1 = new float[16];
            float[] m2 = new float[16];

            for (int i=0; i<m1.length; i++) {
                m1[i]=i;
                m2[i]=i*i;
            }

            System.out.println ("Multiple is " + scalarMultiplication(m1, m2));
            float[][] m = vectorMultiplication(m1, m2);
            for (int i=0; i<m[0].length; i++) {
                for (int j=0; j<m[0].length; j++) {
                    System.out.print (m[i][j] +" ");
                }
                System.out.println();
            }

Output

Multiple is 14400.0
0.0 0.0 0.0 0.0 0.0 0.0 0.0 0.0 0.0 0.0 0.0 0.0 0.0 0.0 0.0 0.0 
0.0 1.0 4.0 9.0 16.0 25.0 36.0 49.0 64.0 81.0 100.0 121.0 144.0 169.0 196.0 225.0 
0.0 2.0 8.0 18.0 32.0 50.0 72.0 98.0 128.0 162.0 200.0 242.0 288.0 338.0 392.0 450.0 
0.0 3.0 12.0 27.0 48.0 75.0 108.0 147.0 192.0 243.0 300.0 363.0 432.0 507.0 588.0 675.0 
0.0 4.0 16.0 36.0 64.0 100.0 144.0 196.0 256.0 324.0 400.0 484.0 576.0 676.0 784.0 900.0 
0.0 5.0 20.0 45.0 80.0 125.0 180.0 245.0 320.0 405.0 500.0 605.0 720.0 845.0 980.0 1125.0 
0.0 6.0 24.0 54.0 96.0 150.0 216.0 294.0 384.0 486.0 600.0 726.0 864.0 1014.0 1176.0 1350.0 
0.0 7.0 28.0 63.0 112.0 175.0 252.0 343.0 448.0 567.0 700.0 847.0 1008.0 1183.0 1372.0 1575.0 
0.0 8.0 32.0 72.0 128.0 200.0 288.0 392.0 512.0 648.0 800.0 968.0 1152.0 1352.0 1568.0 1800.0 
0.0 9.0 36.0 81.0 144.0 225.0 324.0 441.0 576.0 729.0 900.0 1089.0 1296.0 1521.0 1764.0 2025.0 
0.0 10.0 40.0 90.0 160.0 250.0 360.0 490.0 640.0 810.0 1000.0 1210.0 1440.0 1690.0 1960.0 2250.0 
0.0 11.0 44.0 99.0 176.0 275.0 396.0 539.0 704.0 891.0 1100.0 1331.0 1584.0 1859.0 2156.0 2475.0 
0.0 12.0 48.0 108.0 192.0 300.0 432.0 588.0 768.0 972.0 1200.0 1452.0 1728.0 2028.0 2352.0 2700.0 
0.0 13.0 52.0 117.0 208.0 325.0 468.0 637.0 832.0 1053.0 1300.0 1573.0 1872.0 2197.0 2548.0 2925.0 
0.0 14.0 56.0 126.0 224.0 350.0 504.0 686.0 896.0 1134.0 1400.0 1694.0 2016.0 2366.0 2744.0 3150.0 
0.0 15.0 60.0 135.0 240.0 375.0 540.0 735.0 960.0 1215.0 1500.0 1815.0 2160.0 2535.0 2940.0 3375.0 
~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文