有 Java 编程相关的问题?

你可以在下面搜索框中键入要查询的问题!

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

iam在使用bloom filter数据结构的可搜索加密算法中工作,并且为了加密此bloom filter,应使用与加密中bloom filter长度相同的大长度矩阵,但当我使用以下矩阵时。它仅在最大长度[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) 个答案