使用ejml进行LU分解

时间:2012-05-24 16:45:42

标签: java matrix decomposition ejml

我正在尝试使用ejml库来对矩阵进行LU分解。对于SVD分解的计算一切顺利,对于LU分解,我不知道使用ejml库。我想帮助你

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();

1 个答案:

答案 0 :(得分:1)

SimpleMatrix接口不提供计算LU分解的直接方法。相反,您需要从分解算法计算它。

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