Question

I'm trying to use ejml library to make LU decomposition of a matrix. All goes well for the calculation of the SVD decomposition, for LU decomposition I don't know to use ejml library. I would like your help

import java.util.Scanner;
import org.ejml.alg.dense.decomposition.CholeskyDecomposition;
import org.ejml.alg.dense.decomposition.DecompositionFactory;
import org.ejml.alg.dense.decomposition.DecompositionInterface; 
import org.ejml.alg.dense.decomposition.LUDecomposition;
import org.ejml.alg.dense.decomposition.lu.*;
import org.ejml.data.DenseMatrix64F;
import org.ejml.simple.SimpleMatrix;
import org.ejml.simple.SimpleSVD;


public class autovalori {
public static void main(String[] args) {


double[][] matrix;
double[] u;
int r, c, t = 0;


Scanner scanner = new Scanner(System.in);

System.out.println("Insert row matrix: ");
r = scanner.nextInt();//row matrix
System.out.println("Insert columns matrix: ");
c =  scanner.nextInt(); //columns matrix
matrix = new double[r][c];



    for (int i = 0; i < r; i++){
         for (int j = 0; j < c; j++){

             System.out.printf("value: " + i + " - " + j);
             System.out.printf("\n");
             matrix[i][j] = scanner.nextDouble();

         }
     }

    System.out.printf("\n");


    SimpleMatrix A = new SimpleMatrix(matrix);

    SimpleSVD SVD = A.svd();
    SimpleMatrix U = SVD.getU();
    SimpleMatrix S = SVD.getV();
    SimpleMatrix V = SVD.getW();

    U.print();
    S.print();
    V.print();
Was it helpful?

Solution

The SimpleMatrix interface does not provide a direct way to compute LU decomposition. Instead you will need to compute it from a decomposition algorithm.

    SimpleMatrix A = new SimpleMatrix(10,5);

    LUDecomposition<DenseMatrix64F> lu = DecompositionFactory.lu(A.numCols());

    if( !lu.decompose(A.getMatrix()) ) {
        throw new RuntimeException("LU Decomposition failed!");
    }

    SimpleMatrix L = SimpleMatrix.wrap(lu.getLower(null));
    SimpleMatrix U = SimpleMatrix.wrap(lu.getUpper(null));
Licensed under: CC-BY-SA with attribution
Not affiliated with StackOverflow
scroll top