如何在java中找到大长度矩阵的逆?

时间:2018-02-28 23:03:59

标签: java

iam使用可搜索的加密算法,使用布隆过滤器数据结构并加密此布隆过滤器应使用加密时使用相同长度的布隆过滤器的大长度矩阵,但是当我使用以下矩阵时。它只能以最大长度运行[10] [10]。任何人都可以帮助我开发这个实现,使其运行时间很长。

public class Matrix {
   //double[][] value1={{0,92,0.15,0.7},{0.33,1,0.3},{0.24,0.39,0.03}};
    //double [][] value2={{0.57,0.7,0.87},{0.77,0.51,0.83},{0.27,0,0.87}};
    //int []s={1,0,1,1,1,0,1,0,0,1};

    public int columns;
    public int rows;
    public double[][] matrix;

    public Matrix(double[][] matr) {
        this.matrix = matr;
        this.rows = matr.length;
        this.columns = matr[0].length;
    }

    public Matrix(int a, int b) {
        this.rows = a;
        this.columns = b;
        matrix = new double[rows][columns];
    }
    public int size(){
        return columns;
    }
    //}
   // Matrix M1=new Matrix(value1);
    //Matrix M2=new Matrix(value2);

    public static Matrix cofactor(Matrix M) {
        Matrix cofactorOfM = new Matrix(M.getNrows(),M.getNcols());
        for (int i = 0; i < M.getNrows(); i++) {
            for (int j = 0; j < M.getNcols(); j++) {
                cofactorOfM.setValueAt(i, j, changeSign(i) * changeSign(j) * determinant(createSubMatrix(M, i, j)));
            }
        }
        return cofactorOfM;
    }

    public static Matrix transpose(Matrix M) {
        Matrix transposedMatrix = new Matrix(M.getNrows(), M.getNcols());
        for (int i = 0; i < M.getNrows(); i++) {
            for (int j = 0; j < M.getNcols(); j++) {
                transposedMatrix.setValueAt(j, i, M.getValueAt(i, j));
            }
        }
        return transposedMatrix;
    }

    public static double determinant(Matrix M)  {

        if(M.size()==1){
           return M.getValueAt(0, 0);
        }
        if(M.size()==2){
            return(M.getValueAt(0, 0)*(M.getValueAt(1, 1)))-((M.getValueAt(0, 1)*(M.getValueAt(1, 0))));
        }

        double sum = 0.0;
        for (int i = 0; i < M.getNcols(); i++) {
            sum += changeSign(i) * M.getValueAt(0, i) * determinant(createSubMatrix(M, 0, i));

        }
        return sum;
    }

    public static Matrix createSubMatrix(Matrix M, int excluding_row, int excluding_col) {
        Matrix mat = new Matrix(M.getNrows()-1,M.getNcols()-1);

        int r = -1;
        for (int i = 0; i < M.getNrows(); i++) {
            if (i == excluding_row) {
                continue;
            }
            r++;
            int c = -1;
            for (int j = 0; j <M.getNcols(); j++) {
                if (j == excluding_col) {
                    continue;
                }
                mat.setValueAt(r, ++c, M.getValueAt(i, j));
            }
        }
        return mat;
    }

    public static Matrix inverse(Matrix M) {
        return (transpose(cofactor(M)).multiplyByConstant(1.0 / determinant(M)));
    }

    public static int changeSign(int i) {
        if (i % 2 == 0) {
            return 1;
        }
        return -1;
    }

    public double getValueAt(int i, int j) {
        return this.matrix[i][j];
    }

    public void setValueAt(int i, int j, double val) {
        this.matrix[i][j] = val;
    }

    public Matrix multiplyByConstant(double constant) {
        Matrix mat = new Matrix(rows, columns);
        for (int i = 0; i < rows; i++) {
            for (int j = 0; j < columns; j++) {
                mat.setValueAt(i, j, matrix[i][j] * constant);
            }
        }
        return mat;

    }
    public int getNrows() {
        return rows;
    }

    public void setNrows(int nrows) {
        this.rows = nrows;
    }

    public int getNcols() {
        return columns;
    }

    public void setNcols(int ncols) {
        this.columns = ncols;
    }
}

0 个答案:

没有答案