Java矩阵计算之EJML

最近的实验中涉及到了矩阵运算。其中EJML(Efficient Java Matrix Library)是矩阵运算java库。
话不多说,上代码:
下列代码中初始化了一个矩阵。然后,获得每行或者每列的最大最小值。

import org.ejml.data.DenseMatrix64F;
import org.ejml.ops.CommonOps;

import java.util.List;

public class MatrixHelper {

    public static void main(String[] args) {
        DenseMatrix64F L = new DenseMatrix64F(3, 3); // 初始化一个矩阵,并进行下面的赋值
        L.set(0, 0, 4.0);
        L.set(0, 1, 13.0);
        L.set(0, 2, -16.0);
        L.set(1, 0, 12.0);
        L.set(1, 1, 37.0);
        L.set(1, 2, -43.0);
        L.set(2, 0, -16.0);
        L.set(2, 1, -43.0);
        L.set(2, 2, 98.0);
        System.out.println("data为:");
        System.out.println(L);

        DenseMatrix64F mu_0 = getRowMax(L); // 获取行最大值
        System.out.println("矩阵每一行的最大值为:" + mu_0);

        DenseMatrix64F mu_1 = getColMax(L); // 获取列最大值
        System.out.println("矩阵每一列的最大值为:" + mu_1);

        DenseMatrix64F mu_2 = getRowMin(L); // 获取行最小值
        System.out.println("矩阵每一行的最小值为:" + mu_2);

        DenseMatrix64F mu_3 = getColMin(L); // 获取列最小值
        System.out.println("矩阵每一列的最小值为:" + mu_3);
    }

    /**
     * max of the data row
     *
     * @param data
     * @return
     */
    public static DenseMatrix64F getRowMax(DenseMatrix64F data) {

        if (data.numCols > 1) {
            // 将矩阵转换为行向量
            DenseMatrix64F[] dataVectors = new DenseMatrix64F[data.numRows];
            CommonOps.rowsToVector(data, dataVectors);

            DenseMatrix64F max = new DenseMatrix64F(dataVectors.length, 1);// initialized to 0
            for (int i = 0; i < dataVectors.length; i++) {
                DenseMatrix64F vec = dataVectors[i];
                max.set(i, CommonOps.elementMax(vec)); // 获取每一行数据的最大值
            }

            return max;
        } else {
            return data;
        }
    }

    /**
     * max of the data col
     *
     * @param data
     * @return
     */
    public static DenseMatrix64F getColMax(DenseMatrix64F data) {

        if (data.numRows > 1) {
            // 将矩阵转换为列向量
            DenseMatrix64F[] dataVectors = new DenseMatrix64F[data.numCols];
            CommonOps.columnsToVector(data, dataVectors);

            DenseMatrix64F max = new DenseMatrix64F(1, dataVectors.length);// initialized to 0
            for (int i = 0; i < dataVectors.length; i++) {
                DenseMatrix64F vec = dataVectors[i];
                max.set(i, CommonOps.elementMax(vec)); // 获取每一列数据的最大值
            }

            return max;
        } else {
            return data;
        }
    }

    /**
     * min of the data row
     *
     * @param data
     * @return
     */
    public static DenseMatrix64F getRowMin(DenseMatrix64F data) {

        if (data.numCols > 1) {
            // 将矩阵转换为行向量
            DenseMatrix64F[] dataVectors = new DenseMatrix64F[data.numRows];
            CommonOps.rowsToVector(data, dataVectors);

            DenseMatrix64F max = new DenseMatrix64F(dataVectors.length, 1);// initialized to 0
            for (int i = 0; i < dataVectors.length; i++) {
                DenseMatrix64F vec = dataVectors[i];
                max.set(i, CommonOps.elementMin(vec)); // 获取每一行数据的最小值
            }

            return max;
        } else {
            return data;
        }
    }

    /**
     * min of the data col
     *
     * @param data
     * @return
     */
    public static DenseMatrix64F getColMin(DenseMatrix64F data) {

        // 将矩阵转换为列向量
        if (data.numRows > 1) {
            DenseMatrix64F[] dataVectors = new DenseMatrix64F[data.numCols];
            CommonOps.columnsToVector(data, dataVectors);

            DenseMatrix64F max = new DenseMatrix64F(1, dataVectors.length);// initialized to 0
            for (int i = 0; i < dataVectors.length; i++) {
                DenseMatrix64F vec = dataVectors[i];
                max.set(i, CommonOps.elementMin(vec)); // 获取每一列数据的最小值
            }

            return max;
        } else {
            return data;
        }
    }

    /**
     * min of the data row
     *
     * @param data
     * @return
     */
    public static double[] getColMax(List<DenseMatrix64F> data) {

        if (data.isEmpty()) {
            return null;
        }

        DenseMatrix64F result = new DenseMatrix64F(data.size(), data.get(0).numCols);
        // 将矩阵转换为行向量
        for (int i = 0; i < data.size(); i++) {

            if (data.get(i) != null) {
                DenseMatrix64F vec = getColMax(data.get(i));

                for (int j = 0; j < vec.numCols; j++) {
                    result.set(i, j, vec.get(j)); // 获取每一行数据的最小值
                }
            }
        }

        return getColMax(result).data;
    }

    /**
     * min of the data col
     *
     * @param data
     * @return
     */
    public static double[] getColMin(List<DenseMatrix64F> data) {

        if (data.isEmpty()) {
            return null;
        }

        DenseMatrix64F result = new DenseMatrix64F(data.size(), data.get(0).numCols);
        // 将矩阵转换为行向量
        for (int i = 0; i < data.size(); i++) {

            if (data.get(i) != null) {
                DenseMatrix64F vec = getColMin(data.get(i));

                for (int j = 0; j < vec.numCols; j++) {
                    result.set(i, j, vec.get(j)); // 获取每一行数据的最小值
                }
            }
        }
        return getColMin(result).data;
    }
}

运行结果:

data为:
Type = dense , numRows = 3 , numCols = 3
 4.000  13.000  -16.000  
12.000  37.000  -43.000  
-16.000  -43.000  98.000  

矩阵每一行的最大值为:Type = dense , numRows = 3 , numCols = 1
13.000  
37.000  
98.000  

矩阵每一列的最大值为:Type = dense , numRows = 1 , numCols = 3
12.000  37.000  98.000  

矩阵每一行的最小值为:Type = dense , numRows = 3 , numCols = 1
-16.000  
-43.000  
-43.000  

矩阵每一列的最小值为:Type = dense , numRows = 1 , numCols = 3
-16.000  -43.000  -43.000  
评论 1
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值