当前位置: 首页 > 工具软件 > UJMP > 使用案例 >

Android 矩阵乘法-ujmp-complete-0.3.0.jar

姚善
2023-12-01

1. Android 矩阵乘法

基于ujmp-complete-0.3.0.jar实现矩阵和矩阵之前的相乘

    /**
     * 矩阵X和矩阵系数A相乘
     */
    private void multiplication() {
        Matrix mMatrix_Y = null;
        // 矩阵乘法
        mMatrix_Y = mMatrix_X.mtimes(MATRIX_A);
        Log.d(TAG, "运行结果 Y:" + mMatrix_Y.toString());
    }

运行结果

01-03 17:55:01.172: D/sufadi(12525): 运行结果 Y:   36.3776

2.构建矩阵X

构建自变量X column(列)为 F.length row (行)为0的矩阵

    /**
     * 构建自变量X column(列)为 F.length row (行)为0的矩阵
     */
    private void list2MatrixForX() {
        List<Double> list = new ArrayList<Double>();
        for (int i = 0; i < F.length; i++) {
            list.add(Math.random() * 40);
        }

        mMatrix_X = Matrix.Factory.zeros(1, list.size());
        for (int i = 0; i < list.size(); i++) {
            /** value, row, column */
            mMatrix_X.setAsDouble(list.get(i), 0, i);
        }
        Log.d(TAG, "自变量 X = " + mMatrix_X.toString());
    }

例如格式 1 行 24列 矩阵

01-03 17:55:01.165: D/sufadi(12525): 自变量 X =    18.6310    26.8438    17.6582    23.7252     5.3129     9.4883    24.4888    33.5971     5.3075    23.4335    31.4158    23.8718    17.6305    24.1536     3.3704    26.6275    13.5857     6.5998    30.1050    21.9661     6.3465    38.1481    30.1993    33.9715

3.构建矩阵系数A

构建系数A column(列)为 COLUMNS row (行)为F.length的矩阵

    public static final double[] F = new double[] { 0.1, -0.2, -0.3, 0.3, 0.4, 0.5, 0.2, 0.2, 0.1, -0.1, 0.1, -0.1, 0.1, 0.2, -0.2, 0.3, 0.3, 0.2, 0.1, -0.6, 0.3, 0.4, -0.3, 0.1 };

    public static final int ROW = F.length;

    public static final int COLUMNS = 1;

    public static final Matrix MATRIX_A = Matrix.Factory.zeros(ROW, COLUMNS);

    /**
     * 构建系数A column(列)为 COLUMNS row (行)为F.length的矩阵
     */
    private void initMatrixA() {
        int index = 0;
        for (int i = 0; i < MATRIX_A.getRowCount(); i++) {
            for (int j = 0; j < MATRIX_A.getColumnCount(); j++) {
                MATRIX_A.setAsDouble(F[index], i, j);
                index++;
            }
        }

        Log.d(TAG, "系数 = " + MATRIX_A.toString());
    }

例如格式 1 列 24 行的矩阵

01-03 17:55:01.170: D/sufadi(12525): 系数 =     0.1000
01-03 17:55:01.170: D/sufadi(12525):    -0.2000
01-03 17:55:01.170: D/sufadi(12525):    -0.3000
01-03 17:55:01.170: D/sufadi(12525):     0.3000
01-03 17:55:01.170: D/sufadi(12525):     0.4000
01-03 17:55:01.170: D/sufadi(12525):     0.5000
01-03 17:55:01.170: D/sufadi(12525):     0.2000
01-03 17:55:01.170: D/sufadi(12525):     0.2000
01-03 17:55:01.170: D/sufadi(12525):     0.1000
01-03 17:55:01.170: D/sufadi(12525):    -0.1000
01-03 17:55:01.170: D/sufadi(12525):     0.1000
01-03 17:55:01.170: D/sufadi(12525):    -0.1000
01-03 17:55:01.170: D/sufadi(12525):     0.1000
01-03 17:55:01.170: D/sufadi(12525):     0.2000
01-03 17:55:01.170: D/sufadi(12525):    -0.2000
01-03 17:55:01.170: D/sufadi(12525):     0.3000
01-03 17:55:01.170: D/sufadi(12525):     0.3000
01-03 17:55:01.170: D/sufadi(12525):     0.2000
01-03 17:55:01.170: D/sufadi(12525):     0.1000
01-03 17:55:01.170: D/sufadi(12525):    -0.6000
01-03 17:55:01.170: D/sufadi(12525):     0.3000
01-03 17:55:01.170: D/sufadi(12525):     0.4000
01-03 17:55:01.170: D/sufadi(12525):    -0.3000
01-03 17:55:01.170: D/sufadi(12525):     0.1000
 类似资料: