Construct Matrix in Matrix Toolkit Java

1.2k Views Asked by At

I have quite a naive question, concerning the the Matrix Toolkit Java (MTJ): how to I build a Matrix B starting from a double[][] A?

Cause, within the library, Matrix is only an interface and not a class.

EDIT

So, I thought that having JAMA and 'MTJ' would have solved the problem, since in JAMA it's possible to directly define Matrix objects, but it hasn't worked.

My code is this:

import java.util.Arrays; import Jama.; import no.uib.cipr.matrix.;

public class MainCalc extends TurbulentModel {

    public static void main(String[] args){
        //      TurbulentModel A = new TurbulentModel();
        //      A.numberOfPointsAlongX = 4096;
        //      A.numberOfPointsAlongY = 3;
        //      A.numberOfPointsAlongZ = 3;
        //      A.averageHubWindSpeed = 8;
        //      A.durationOfWindFile = 600;
        //      A.hubHeight = 90;
        //      A.turbulentSeedNumber = 1;
        //      A.volumeWidthAlongY = 150;
        //      A.volumeHeightAlongZ = 150; 
        //      float[] pointsYCoord = A.calcPointsYCoord();
        //      float[] pointsZCoord = A.calcPointsZCoord();
        double[][] rr = {{2, -1, 0},{-1, 2, -1},{0, -1, 2}};
        Matrix test = new Matrix(rr);
        LowerTriangPackMatrix test1 = new LowerTriangPackMatrix(test);

       System.exit(0);

    }
}

But it is resolved into an evident conflict between JAMAsMatrixconcept and MTJ'sMatrix` definition.

How shall I solve the issue?

1

There are 1 best solutions below

0
On

You don't need JAMA to create a matrix in MTJ. In fact, as you've already found, JAMA is going to get in the way of MTJ.

The simplest approach to creating a matrix object in MTJ is to use the DenseMatrix class which implements the Matrix interface. One of its constructors accepts a double[][] and creates a matrix whose entries are those given in the input array. For example,

// create array of backing values for an n-by-n matrix
double[][] matValues = new double[n][n];
... // initialize values somehow

// create a matrix from the matValues array with deep-copy semantics
// the matrix A is independent of any changes to the matValues array and vis-versa
Matrix A = new DenseMatrix(matValues);

// create a matrix from the matValues array **without** deep-copy semantics
// the matrix B will reflect any changes made to the matValues array and vis-versa
Matrix B = new DenseMatrix(matValues, false);

There are other constructors available, but these two forms seem the most pertinent to your needs. You should consult the javadoc (be aware that this isn't for the latest version 1.01, but seems close) for more options.

I assume that you need dense storage for you matrix. If you have a sparse matrix, then there are other classes in MTJ which you should use in place of DenseMatrix such as the CompColMatrix or SymmTridiagMatrix classes. Which of these sparse matrix classes you would use depends on the type of sparseness inherent to the matrix being represented.

When in doubt, however, the dense storage approach will work for all possible matricies. The benefit to using sparse matricies is speed and storage space, but only for matricies which are appropriately sparse.