使用Strepe乘以2 double [] [] Matrices [英] Multiply 2 double[][] Matrices Using Streams

查看:104
本文介绍了使用Strepe乘以2 double [] [] Matrices的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我想知道使用流的多个2 double [] [] 数组矩阵的最紧凑和最有效的方法是什么。该方法应遵循如下所示的矩阵乘法规则:
http:// www.mathwarehouse.com/algebra/matrix/multiply-matrix.php

I'm wondering what the most compact and efficient way to multiple 2 double[][] arrays matrices using streams. The approach should follow matrix multiplication rules as illustrated here: http://www.mathwarehouse.com/algebra/matrix/multiply-matrix.php

这是使用for循环的一种方法('this'是第一个matrix'):

Here's one way to do it using for loops ('this' is the first matrix'):

final int nRows = this.getRowDimension();
final int nCols = m.getColumnDimension();
final int nSum = this.getColumnDimension();

final double[][] outData = new double[nRows][nCols];
// Will hold a column of "m".
final double[] mCol = new double[nSum];
final double[][] mData = m.data;

// Multiply.
for (int col = 0; col < nCols; col++) {
    // Copy all elements of column "col" of "m" so that
    // will be in contiguous memory.
    for (int mRow = 0; mRow < nSum; mRow++) {
        mCol[mRow] = mData[mRow][col];
    }

    for (int row = 0; row < nRows; row++) {
        final double[] dataRow = data[row];
        double sum = 0;
        for (int i = 0; i < nSum; i++) {
            sum += dataRow[i]
                    * mCol[i];
        }
        outData[row][col] = sum;
    }
}

该程序应符合以下测试数据:

The procedure should fit the following test data:

    double[][] md1 = { { 4d, 8d }, { 0d, 2d }, { 1d, 6d } };
    double[][] md2 = { { 5d, 2d, 5d, 5d }, { 9d, 4d, 5d, 5d } };

    double[][] md1 = { { 4d, 8d }, { 0d, 2d }, { 1d, 6d } };
    double[][] md2 = { { 5d }, { 9d } };


推荐答案

更紧凑和可读的解决方案是创建一个流在第一个矩阵的行上,将每一行映射到与第二个矩阵列相乘的结果,并将其收集回 double [] []

A more compact and readable solution is to create a Stream over the rows of the first matrix, map each row to the result of multiplying it with the second matrix column and collect that back into a double[][].

public static void main(String[] args) {
    double[][] m1 = { { 4, 8 }, { 0, 2 }, { 1, 6 } };
    double[][] m2 = { { 5, 2 }, { 9, 4 } };

    double[][] result = Arrays.stream(m1).map(r -> 
        IntStream.range(0, m2[0].length).mapToDouble(i -> 
            IntStream.range(0, m2.length).mapToDouble(j -> r[j] * m2[j][i]).sum()
    ).toArray()).toArray(double[][]::new);

    System.out.println(Arrays.deepToString(result));
}

这将计算 m1 * m2 ,结果将在结果中。对于每一行的乘法,我们不能用第二个矩阵的 Arrays.stream 创建一个Stream,因为当我们需要一个Stream时,这将在行上创建一个Stream列。为了抵消这种情况,我们只需回到索引上使用 IntStream

This will calculate m1 * m2 and the result will be in result. For the multiplication of each row, we can't create a Stream with Arrays.stream of the second matrix since this would create a Stream over the rows when we need a Stream over the columns. To counteract that, we simply go back to using an IntStream over the indexes.

这篇关于使用Strepe乘以2 double [] [] Matrices的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆