Java实现矩阵的相关运算

1、余子式

    /**
     * 求(h,v)位置的余子式
     * @param matrix
     * @param h
     * @param v
     * @return
     */
    public double[][] confactor(double[][] matrix,int h,int v){
        double[][] result = new double[matrix.length - 1][matrix[0].length - 1];
        for (int i = 0;i < result.length;i++){
            if(i < h - 1){
                for (int j = 0;j < result[i].length;j++){
                    if(j < v - 1){
                        result[i][j] = matrix[i][j];
                    }
                    else {
                        result[i][j] = matrix[i][j + 1];
                    }
                }
            }
            else {
                for (int j = 0;j < result[i].length;j++){
                    if(j < v - 1){
                        result[i][j] = matrix[i + 1][j];
                    }
                    else {
                        result[i][j] = matrix[i + 1][j + 1];
                    }
                }
            }
        }

        return result;
    }

2、行列式

   /**
     * 求行列式
     * @param matrix
     * @return
     */
    public double det(double[][] matrix){
        /*
         * 二维矩阵计算
         */
        if(matrix.length == 2) {
            return matrix[0][0] * matrix[1][1] - matrix[0][1] * matrix[1][0];
        }
        /*
         * 二维以上的矩阵计算
         */
        double result = 0;
        int num = matrix.length;
        double[] nums = new double[num];
        for(int i = 0; i < matrix.length; i++) {
            if(i % 2 == 0) {
                nums[i] = matrix[0][i] * det(confactor(matrix, 1, i+1));
            }else {
                nums[i] = -matrix[0][i] * det(confactor(matrix, 1, i+1));
            }
        }
        for(int i = 0; i < matrix.length; i++) {
            result += nums[i];
        }

        return result;
    }

3、转置

    /**
     * 求矩阵的转置
     * @param matrix
     * @return
     */
    public double[][] transpose(double[][] matrix){
        double[][] result = new double[matrix[0].length][matrix.length];
        for (int i = 0;i < matrix.length;i++){
            for (int j = 0;j < matrix[i].length;j++){
                result[j][i] = matrix[i][j];
            }
        }
        return result;
    }

4、逆

    /**
     * 求矩阵的逆
     * @param matrix
     * @return
     */
    public double[][] inverse(double[][] matrix){
        double[][] result = new double[matrix.length][matrix[0].length];

        double detA = det(matrix);

        for(int i = 0; i < matrix.length; i++) {
            for(int j = 0; j < matrix[0].length; j++) {
                if((i + j) % 2 == 0) {
                    result[i][j] = det(confactor(matrix, i+1, j+1)) / detA;
                }else {
                    result[i][j] = -det(confactor(matrix, i+1, j+1)) / detA;
                }

            }
        }
        result = transpose(result);

        return result;
    }

5、减法

    /**
     * 矩阵的减法
     * @param matrix1
     * @param matrix2
     * @return
     */
    public double[][] subtract(double[][] matrix1,double[][] matrix2){
        int m1 = matrix1.length;
        int n1 = matrix1[0].length;
        int m2 = matrix2.length;
        int n2 = matrix2[0].length;
        if(m1 != m2 || n1 != n2){
            return null;
        }
        else {
            double[][] result = new double[m1][n1];

            for (int i = 0;i < m1;i++){
                for(int j = 0; j < n1;j++){
                    result[i][j] = matrix1[i][j] - matrix2[i][j];
                }
            }

            return result;
        }
    }

6、数乘

    /**
     * 矩阵的数量乘法(数乘)
     * @param matrix
     * @param a
     * @return
     */
    public double[][] scalarMult(double[][] matrix,double a){
        double[][] result = new double[matrix.length][matrix[0].length];

        for (int i = 0;i < matrix.length;i++){
            for (int j = 0;j < matrix[i].length;j++){
                result[i][j] = a * matrix[i][j];
            }
        }

        return result;
    }

7、乘法

    /**
     * 矩阵的乘法
     * @param matrix1
     * @param matrix2
     * @return
     */
    public double[][] vectorMult(double[][] matrix1,double[][] matrix2){
        int m1 = matrix1.length;
        int n1 = matrix1[0].length;
        int m2 = matrix2.length;
        int n2 = matrix2[0].length;

        // 第一个矩阵的列数不等于第二个矩阵的行数时矩阵的乘法无意义
        if(n1 != m2){
            return null;
        }
        else {
            double[][] result = new double[m1][n2];

            for (int i = 0;i < m1;i++){
                for (int j = 0;j < n2;j++){
                    for (int k = 0;k < n1;k++){
                        result[i][j] += matrix1[i][k] * matrix2[k][j];
                    }
                }
            }

            return result;
        }
    }

8、邻接矩阵的入度

    /**
     * 求邻接矩阵的入度
     * @param matrix
     * @return
     */
    public double[] inDegree(double[][] matrix){
        int len = matrix.length;
        double[] result = new double[len];

        for (int i = 0;i < len;i++){
            double degree = 0.0;// 入度
            for (int j = 0;j<matrix[i].length;j++){
                if(matrix[j][i] != 0.0){// 遍历到非0元素则入度+1
                    degree++;
                }
            }
            result[i] = degree;
        }

        return result;
    }

9、邻接矩阵的出度

    /**
     * 求邻接矩阵的出度
     * @param matrix
     * @return
     */
    public double[] outDegree(double[][] matrix){
        int len = matrix.length;
        double[] result = new double[len];

        for (int i = 0;i < len;i++){
            double degree = 0.0;// 出度
            for (int j = 0;j < matrix[i].length;j++){
                if(matrix[i][j] != 0.0){// 遍历到非0元素则出度+1
                    degree++;
                }
            }
            result[i] = degree;
        }

        return result;
    }

10、求矩阵的最大特征值和其对应的特征向量

import java.math.BigDecimal;

/**
 * 求矩阵的最大特征值和对应的特征向量
 */
public class EigenValue {

    // 平均随机一致性指标
    private double[] RI = {
            0.00, 0.00, 0.52, 0.89, 1.12,
            1.26, 1.36, 1.41, 1.46, 1.49,
            1.52, 1.24, 1.56, 1.58, 1.59,
            1.5943,1.6064,1.6133,1.6207,1.6292,
            1.6385,1.6403,1.6462,1.6497,1.6556,
            1.6587,1.6631,1.6670,1.6693,1.6724
    };

    // 随机一致性比率
    private double CR = 0.0;

    // 最大特征值
    private double lamta = 0.0;

    public double[] weight(double[][] matrix) {
        int len = matrix.length;

        double[] w0 = new double[len];// 初始向量Wk

        for (int i = 0; i < len; i++) {
            w0[i] = 1.0 / len;
        }

        double[] w1 = new double[len];// 一般向量W(k+1)

        double[] w2 = new double[len];// W(k+1)的归一化向量

        double sum = 1.0;

        double d = 1.0;

        double delt = 0.00001;// 误差

        while (d > delt) {
            d = 0.0;
            sum = 0;

            // 获取向量
            for (int i = 0; i < len; i++) {
                double temp = 0.0;
                for (int j = 0; j < len; j++) {
                    temp += matrix[i][j] * w0[j];
                }
                w1[i] = temp;
                sum += w1[i];
            }

            // 向量归一化
            for (int i = 0; i < len; i++) {
                w2[i] = w1[i] / sum;

                d = Math.max(Math.abs(w2[i] - w0[i]), d);// 最大差值

                w0[i] = w2[i];// 用于下次迭代
            }
        }

        // 计算矩阵最大特征值lamta,一致性指标CI,随机一致性指标RI
        lamta = 0.0;

        for (int i = 0; i < len; i++) {
            if(w1[i] != 0 && w0[i] != 0) {
                lamta += w1[i] / (len * w0[i]);
            }
        }

        double CI = (lamta - len) / (len - 1);

        if (RI[len - 1] != 0) {
            CR = CI / RI[len - 1];
        }


        // 四舍五入处理
        lamta = round(lamta, 3);
        CI = Math.abs(round(CI, 3));
        CR =  Math.abs(round(CR, 3));

        for (int i = 0; i < len; i++) {
            w0[i] = round(w0[i], 18);
            w1[i] = round(w1[i], 18);
            w2[i] = round(w2[i], 18);
        }

        return w2;
    }

    public double getLamta(){
        return lamta;
    }

    private double round(double value, int scale) {
        if (scale < 0) {
            throw new IllegalArgumentException(
                    "The scale must be a positive integer or zero");
        }
        BigDecimal b = new BigDecimal(Double.toString(value));
        BigDecimal one = new BigDecimal("1");
        return b.divide(one, scale, BigDecimal.ROUND_HALF_UP).doubleValue();
    }

}
  • 3
    点赞
  • 6
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值