Java库特征值浮点数

时间:2012-10-31 18:16:47

标签: java android shared-libraries eigenvalue jama

嗨,我有Jama图书馆,但是这个图书馆只能使用Double number ..而且非常慢。对于Android应用..最后我不需要如此高精度的eig decomp ..所以有一些JAva libaray与浮点数.....在语法上与jama类似?因为...我不想重新写 - 我的440行代码谢谢。 EIG。转置,反向等基本线性代数运算..

或存在相同的java库特征值。有线程吗?

2 个答案:

答案 0 :(得分:2)

我知道有一个名为la4j的库,您可能有兴趣研究它。我应该提一下,一般来说,如果你计划进行许多矩阵操作/计算(我自己已经尝试并且达到了死胡同),我认为Java不是一个好的选择,你可能最好不要看Python (NumPy)或C ++(Armadillo)用于此类项目。

答案 1 :(得分:0)

或者你在找这样的东西?

     import java.util.Arrays;

public class Matrix {

protected int rows;

protected int cols;

double[][] values;

public Matrix(int rows, int cols) {
    this.rows = rows;
    this.cols = cols;

    values = new double[rows][cols];

    for (int i = 0; i < rows; i++)
        for (int j = 0; j < cols; j++)
            values[i][j] = 0;

}

public Matrix(int[][] M) {
    this.rows = M.length;
    this.cols = M[0].length;

    values = new double[rows][cols];

    for (int i = 0; i < rows; i++)
        for (int j = 0; j < cols; j++)
            values[i][j] = M[i][j];
}

public Matrix(double[][] M) {
    this.rows = M.length;
    this.cols = M[0].length;

    values = new double[rows][cols];

    for (int i = 0; i < rows; i++)
        for (int j = 0; j < cols; j++)
            values[i][j] = M[i][j];
}

public void setToEye() {
    for (int i = 0; i < rows; i++)
        for (int j = 0; j < cols; j++)
            values[i][j] = (i == j) ? 1 : 0;

}

public static int[] matrixSize(Matrix M) {
    int[] size = new int[2];
    size[0] = M.rows;
    size[1] = M.cols;
    return size;
}

public static double vectMul(double[] A, double[] B) {
    double suma = 0;
    for (int i = 0; i < A.length; i++)
        suma += A[i] * B[i];

    return suma;
}

public static Matrix matrixTranspose(Matrix M) {
    int[] size = matrixSize(M);
    double[][] Mt = new double[size[0]][size[1]];

    for (int i = 0; i < size[0]; i++)
        for (int j = 0; j < size[1]; j++)
            Mt[i][j] = M.getValue(j, i);

    return new Matrix(Mt);
}

public static Matrix matrixMul(Matrix A, Matrix B) {
    int m1 = matrixSize(A)[0];
    int n1 = matrixSize(A)[1];
    int m2 = matrixSize(B)[0];
    int n2 = matrixSize(B)[1];
    double[][] rez;

    if (n1 != m2) {
        System.err.println("Inner matrix dimensions must agree!");
        return null;
    }

    rez = new double[m1][n2];
    for (int i = 0; i < m1; i++)
        for (int j = 0; j < n2; j++)
            rez[i][j] = vectMul(A.getRow(i), B.getColumn(j));

    Matrix r = new Matrix(rez);
    return r;
}

public static Matrix matrixMulWithMod(Matrix A, Matrix B, double mod) {
    int m1 = matrixSize(A)[0];
    int n1 = matrixSize(A)[1];
    int m2 = matrixSize(B)[0];
    int n2 = matrixSize(B)[1];
    double[][] rez;

    if (n1 != m2) {
        System.err.println("Inner matrix dimensions must agree!");
        return null;
    }

    rez = new double[m1][n2];
    for (int i = 0; i < m1; i++)
        for (int j = 0; j < n2; j++)
            rez[i][j] = vectMul(A.getRow(i), B.getColumn(j)) % mod;

    Matrix r = new Matrix(rez);
    return r;
}

public String toString() {
    StringBuilder sb = new StringBuilder();
    for (int i = 0; i < this.rows; i++) {
        sb.append(Arrays.toString(values[i]));
        sb.append('\n');
    }

    String str = sb.toString();
    return str;
}

public double[][] getValues() {
    return values;
}

public void setValues(double[][] values) {
    this.values = values;
}

public void setValues(int[][] values) {
    for (int i = 0; i < rows; i++)
        for (int j = 0; j < cols; j++)
            this.values[i][j] = (double) values[i][j];
}

public double getValue(int row, int col) {
    return values[row][col];
}

public void setValue(int row, int col, double value) {
    values[row][col] = value;
}

public double[] getRow(int row) {
    double[] temp = new double[cols];
    for (int i = 0; i < temp.length; i++)
        temp[i] = values[row][i];
    return temp;

}

public double[] getColumn(int col) {
    double[] temp = new double[rows];
    for (int i = 0; i < temp.length; i++)
        temp[i] = values[i][col];
    return temp;
}

public double[] toDoubleArray() {
    double[] temp = new double[rows * cols];
    for (int i = 0; i < rows; i++)
        for (int j = 0; j < cols; j++)
            temp[i * (rows + 1) + j] = values[i][j];
    return temp;
}

public int[] toIntArray() {
    int[] temp = new int[rows * cols];
    for (int i = 0; i < rows; i++)
        for (int j = 0; j < cols; j++)
            temp[i * (rows + 1) + j] = (int) values[i][j];
    return temp;
}

public int getRowCount() {
    return this.rows;
}

public int getColumnsCount() {
    return this.cols;
}

public static double getMatrixDet(Matrix M) {
    int m = M.getRowCount();
    int n = M.getColumnsCount();

    double D = 0;

    if (m != n) {
        System.err.println("Matrix must be square!");
        System.exit(0);
    }

    if (n > 1) {
        Matrix I = new Matrix(m - 1, n - 1);

        for (int i = 1; i < m; i++)
            for (int j = 1; j < n; j++)
                I.setValue(i - 1, j - 1, M.getValue(i, j));

        D = M.getValue(0, 0) * getMatrixDet(I);
    } else
        D = M.getValue(0, 0);

    // za niz , kopira iz niza a elemente 0:i-1 i+1:n sredi za matrcu 
    Matrix I = new Matrix(m - 1, n - 1);
    for (int i = 1; i < n; i++) {
        I = M.withoutIthRowAndJthCol(i, 0);
        D = D + Math.pow((-1), i) * M.getValue(i, 0) * getMatrixDet(I);
    }
    return D;

}

public Matrix transpose() {
    Matrix temp = new Matrix(this.values);

    for (int i = 0; i < this.rows; i++)
        for (int j = 0; j < this.cols; j++)
            this.values[i][j] = temp.getValue(j, i);

    return this;
}

private Matrix withoutIthRowAndJthCol(int row, int col) {
    Matrix temp = new Matrix(this.rows - 1, this.cols - 1);
    int k = 0, l = 0;
    for (int i = 0; i < this.getRowCount(); i++) {
        if (i == row)
            continue;
        for (int j = 0; j < this.getColumnsCount(); j++) {
            if (j == col)
                continue;

            temp.setValue(k, l, this.values[i][j]);
            l++;
        }
        l %= 2;
        k++;
    }
    return temp;
}

public static Matrix getMatrixAdj(Matrix M) {
    int m = M.getRowCount();
    int n = M.getColumnsCount();

    Matrix A = new Matrix(m, n);

    if (m != n) {
        System.err.println("Matrix must be square!");
        System.exit(0);
    }

    for (int i = 0; i < m; i++)
        for (int j = 0; j < n; j++) {
            A.setValue(i, j, Math.pow((-1), i + j)
                    * getMatrixDet(M.withoutIthRowAndJthCol(i, j)));
        }
    A.transpose();
    return A;
}

public static Matrix matrixDiv(Matrix M, double n) {
    Matrix temp = M;

    for (int i = 0; i < M.getRowCount(); i++)
        for (int j = 0; j < M.getColumnsCount(); j++)
            temp.setValue(i, j, (M.getValue(i, j) / n));

    return temp;
}

public static Matrix getMatrixInv(Matrix M) {
    Matrix I = new Matrix(M.getRowCount(), M.getColumnsCount());

    if (M.getRowCount() != M.getColumnsCount()) {
        System.err.println("Matrix must be square!");
        System.exit(0);
    }
    if (getMatrixDet(M) == 0) {
        System.err.println("Matrix is singular!");
        System.exit(0);
    }
    I = matrixDiv(getMatrixAdj(M), getMatrixDet(M));
    return I;
}
}

如果这是你想要的,欢迎你。