【问题标题】:multiply RealMatrix with Complex matrix将 RealMatrix 与复数矩阵相乘
【发布时间】:2013-05-18 20:33:19
【问题描述】:

我对 Java 还是很陌生,我面临一个问题,我相信它可以很容易地掌握。

我正在生成一个链接到 Apache - Commons Math 库的项目。

在项目中,我使用了很多 RealMatrix 对象。我有一个方法如下

public static RealMatrix DistCalc(RealMatrix YCoord, RealMatrix ZCoord){
        RealMatrix Distance = new Array2DRowRealMatrix(YCoord.getRowDimension(),ZCoord.getRowDimension());
        for(int ii = 0; ii < YCoord.getRowDimension(); ii++){
            for(int jj = 0; jj < ZCoord.getRowDimension(); jj++){
                Distance.setEntry(ii,jj,Math.sqrt((YCoord.getEntry(ii, 0) - YCoord.getEntry(jj, 0))*(YCoord.getEntry(ii, 0) - YCoord.getEntry(jj, 0)) + (ZCoord.getEntry(jj, 0) - ZCoord.getEntry(ii, 0))*(ZCoord.getEntry(jj, 0) - ZCoord.getEntry(ii, 0))));
            }
        }        
        return Distance;
    }

另一个生成某个Complex矩阵,

// Define the random phase for the u- component
    public static Complex[][] RandPhi(int N, int nFFT){
        Complex[][] nn_u = new Complex[N][nFFT];        
        for(int ii = 0; ii < N; ii++){
            for(int jj = 0; jj < nFFT; jj++){
                nn_u[ii][jj] = new Complex(Math.cos(new Random().nextDouble()*2*Math.PI),Math.sin(new Random().nextDouble()*2*Math.PI));
            }
        }
        return nn_u;
    }

现在,我想将RealMatrix 距离Complex矩阵nn_u逐列相乘:最后我应该得出一个@ 987654328@矩阵。

你介意解释一下吗?

【问题讨论】:

    标签: java eclipse apache-commons


    【解决方案1】:

    我建议您基于RealMatrix 接口创建自己的ComplexMatrix 接口,然后基于Array2DRowRealMatrix 类创建自己的Array2DRowComplexMatrix 类。要创建类,只需download the source code,更改类名,将double data[][] 更改为Complex data[][],然后将所有引用更新为data

    要么创建一个接受RealMatrixComplexMatrix 构造函数,要么包含一个带有RealMatrix 参数的multiply 方法。

    Commons 应该具有您需要的所有方法,您可能只需要稍微调整它们的参数/返回类型。

    【讨论】:

    • 如果我切换回Java原语double[][]类型而不是RealMatrix会更容易吗?我目前正在使用RealMatrix,因为代码结果更清晰,我不必重新发明轮子。顺便说一句,我会试试你的建议。
    • 这是你的电话,你真正需要的是一个接受Complex[][]double[][]的乘法方法,这可以是ComplexMatrix类的实例方法,也可以是带有(Complex[][], double[][]) 参数的静态方法。无论哪种方式,下载 Math 源代码并将您的 multiply 方法基于 Array2DRowComplexMatrix 类中的方法 - 该方法是您不想自己重新实现的部分。
    猜你喜欢
    • 2015-01-24
    • 2016-12-11
    • 1970-01-01
    • 2021-06-06
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    相关资源
    最近更新 更多