简体   繁体   English

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

[英]How to invert and multiply matrices in Android?

Given 2 matrices:给定2个矩阵:

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(mR, x, y, z)
  • SensorManager.getRotationMatrix(mRi, x, y, z)

So there will be two 4x4 matrices which,所以会有两个 4x4 矩阵,

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.事实上,我知道它是否适用于invertM()multiplyMM()但我不知道如何使用矩阵来做到这一点。

Can you help?你能帮我吗?

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.嘿家伙,我假设它们是 4x4 矩阵(16 个元素)......您可以使用 Gauss-Jordan 消除http://en.wikipedia.org/wiki/Gauss%E2%80%93Jordan_elimination进行反转。 Matrix multiplication is described about everywhere , even more than matrix inversion.矩阵乘法到处都有描述,甚至比矩阵求逆还要多。

The matrices you are describing are actually uni-dimensional vectors , so I am assuming that what you call inverse is actually transpose .您描述的矩阵实际上是一维向量,所以我假设您所说的inverse实际上是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 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 

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM