-3

Hi I have the Jama librarry, but this libraray work only with Double numbers..and its very slow. for Android app ..and finally i dont need so high precision of eig decomp..so is there some JAva libaray with float num.....similar in syntax with jama? becouse...I dont want to re-- write again my 440 rows code thanks. eig. Transpose, inverse and so basic Linear algebra operations..

or exist same java library eigenvalues. with threads?

  • Post code lest you want to be ridiculed. No, seriously, post code please and form up your question. – Shark Oct 31 '12 at 18:18
  • What makes you think that float calculations would be faster than double calculations? Your second question is difficult to understand. – assylias Oct 31 '12 at 18:19
  • becous the few methods witch i compute before are in float...and in double it takes too seconds- time so i can convert float to double or, re write my other methods to float precisions.. – Luke Numerati Oct 31 '12 at 18:22
  • float to double conversions will take a minimal amount of time vs. eigenvalue calculation. – assylias Oct 31 '12 at 18:23
  • See for example: http://stackoverflow.com/questions/4584637/double-or-float-which-is-faster – assylias Oct 31 '12 at 18:32

2 Answers2

2

I know of one library called la4j, you might be interested in looking into that. I should mention that, generally, I don't think Java is a good choice if you're planning on doing many matrix manipulations/calculations (I myself have tried and hit a dead-end), you might be better off looking into Python (NumPy) or C++ (Armadillo) for such projects.

Glorfindel
  • 21,988
  • 13
  • 81
  • 109
arshajii
  • 127,459
  • 24
  • 238
  • 287
  • I know it; but I use modern mathematic tools(and threads) so i reduce...many operation, but i know the c++ is fastest..and python – Luke Numerati Oct 31 '12 at 18:30
0

Or are you looking for something like this?

     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;
}
}

if this is what you wanted, you're welcome.

Shark
  • 6,513
  • 3
  • 28
  • 50
  • My college eclipse workspace folder. Used it for cryptography and Hill's cipher so it's actually usable. I mean, he did say 'basic linear algebra' and this one can calculate the inverse matrix as well. – Shark Oct 31 '12 at 18:26