English 中文(简体)
如何在Android中反转和相乘矩阵?
原标题:How to invert and multiply matrices in Android?

给定2个矩阵:

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

这些将是来自

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

所以会有两个4x4矩阵,

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

  • ResultMtrix=inverse(mRi)*mR

事实上,我知道它是否适用于investm()multiplyMM(),但我不知道如何使用矩阵。

你能帮忙吗?

问题回答

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

您所描述的矩阵实际上是一维向量,因此我假设您所称的inverse实际是transpose。这种情况下的计算非常简单:

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 




相关问题
Spring Properties File

Hi have this j2ee web application developed using spring framework. I have a problem with rendering mnessages in nihongo characters from the properties file. I tried converting the file to ascii using ...

Logging a global ID in multiple components

I have a system which contains multiple applications connected together using JMS and Spring Integration. Messages get sent along a chain of applications. [App A] -> [App B] -> [App C] We set a ...

Java Library Size

If I m given two Java Libraries in Jar format, 1 having no bells and whistles, and the other having lots of them that will mostly go unused.... my question is: How will the larger, mostly unused ...

How to get the Array Class for a given Class in Java?

I have a Class variable that holds a certain type and I need to get a variable that holds the corresponding array class. The best I could come up with is this: Class arrayOfFooClass = java.lang....

SQLite , Derby vs file system

I m working on a Java desktop application that reads and writes from/to different files. I think a better solution would be to replace the file system by a SQLite database. How hard is it to migrate ...

热门标签