Search code examples
javaeclipseapache-commons

multiply RealMatrix with Complex matrix


I am pretty new to Java and I am facing an issue I believe it can be mastered pretty easily.

I am generating a project with is linked to the Apache - Commons Math library.

Within the project, I'm making use quite a lot of RealMatrix objects. I have a method working as follows

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;
    }

and another one generating a certain Complex matrix,

// 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;
    }

Now, I'd like multiplying column-wise the RealMatrix Distance with the Complex matrix nn_u: in the end I should come up with a Complex[N][nFFT] matrix.

Would you mind to shed some light?


Solution

  • I recommend that you create your own ComplexMatrix interface based on the RealMatrix interface, and that you then create your own Array2DRowComplexMatrix class based on the Array2DRowRealMatrix class. To create the class, simply download the source code, change the class name, change double data[][] to Complex data[][], and then update all references to data.

    Either create a ComplexMatrix constructor that accepts a RealMatrix, or else include a multiply method with a RealMatrix parameter.

    Commons should have all of the methods you need, you might just need to tweak their parameter/return types a bit.