inverse float Matrix - Android java.lang

Android examples for java.lang:Math Matrix

Description

inverse float Matrix

Demo Code

/*/*  www . j a va  2  s. c  o  m*/
 * Taken from android-gl, Making OpenGL Programming in Android Easier
 * URL: https://code.google.com/p/android-gl/source/browse/trunk/AndroidGL/src/edu/union/
 * 
 * License: GNU Lesser General Public License
 * 
 *   This library is free software; you can redistribute it and/or
 *   modify it under the terms of the GNU Lesser General Public
 *   License as published by the Free Software Foundation; either
 *   version 2.1 of the License, or (at your option) any later version.
 *
 *   This library is distributed in the hope that it will be useful,
 *   but WITHOUT ANY WARRANTY; without even the implied warranty of
 *   MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the GNU
 *   Lesser General Public License for more details.
 *
 *   You should have received a copy of the GNU Lesser General Public
 *   License along with this library; if not, write to the Free Software
 *   Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA  02111-1307  USA
 */
//package com.java2s;

public class Main {
    public static void inverse(float[][] in, float[][] out) {
        int st_vrs = 4, st_stolp = 4;
        float[][] old = new float[st_vrs][st_stolp * 2];
        float[][] tmp = new float[st_vrs][st_stolp * 2];

        for (int v = 0; v < st_vrs; v++) {// ones vector
            for (int s = 0; s < st_stolp * 2; s++) {
                if (s - v == st_vrs)
                    old[v][s] = 1;
                if (s < st_stolp)
                    old[v][s] = in[v][s];
            }
        }
        // zeros below the diagonal
        for (int v = 0; v < st_vrs; v++) {
            for (int v1 = 0; v1 < st_vrs; v1++) {
                for (int s = 0; s < st_stolp * 2; s++) {
                    if (v == v1)
                        tmp[v][s] = old[v][s] / old[v][v];
                    else
                        tmp[v1][s] = old[v1][s];
                }
            }
            old = prepisi(tmp);
            for (int v1 = v + 1; v1 < st_vrs; v1++) {
                for (int s = 0; s < st_stolp * 2; s++) {
                    tmp[v1][s] = old[v1][s] - old[v][s] * old[v1][v];
                }
            }
            old = prepisi(tmp);
        }
        // zeros above the diagonal
        for (int s = st_stolp - 1; s > 0; s--) {
            for (int v = s - 1; v >= 0; v--) {
                for (int s1 = 0; s1 < st_stolp * 2; s1++) {
                    tmp[v][s1] = old[v][s1] - old[s][s1] * old[v][s];
                }
            }
            old = prepisi(tmp);
        }
        for (int v = 0; v < st_vrs; v++) {// right part of matrix is inverse
            for (int s = st_stolp; s < st_stolp * 2; s++) {
                out[v][s - st_stolp] = tmp[v][s];
            }
        }
    }

    private static float[][] prepisi(float[][] in) {
        float[][] out = new float[in.length][in[0].length];
        for (int v = 0; v < in.length; v++) {
            for (int s = 0; s < in[0].length; s++) {
                out[v][s] = in[v][s];
            }
        }
        return out;
    }
}

Related Tutorials