有 Java 编程相关的问题?

你可以在下面搜索框中键入要查询的问题!

矩阵如何从Apache Commons数学Java库中找到伪逆

根据这一准则。它可以使用ApacheCommons数学库查找伪逆

RealMatrix Swinv = new LUDecomposition(Sw).getSolver().getInverse(); // MATLAB: Swinv = pinv(Sw)

但是即使Sw是正方形,我也可以得到异常org.apache.commons.math3.linear.SingularMatrixException:

根据文献,这个getInverse()是伪逆的

Get the pseudo-inverse of the decomposed matrix.

这等于分解矩阵的逆(如果存在这样的逆)

如果不存在这样的逆,则结果具有类似于逆的属性

In particular, in this case, if the decomposed matrix is A, then the system of equations ( A x = b ) may have no solutions, or many. If it has no solutions, then the pseudo-inverse ( A^+ ) gives the "closest" solution ( z = A^+ b ), meaning ( \left \| A z - b \right \|_2 ) is minimized. If there are many solutions, then ( z = A^+ b ) is the smallest solution, meaning ( \left \| z \right \|_2 ) is minimized.

那我在这里该怎么办?我想找到伪逆。但是我仍然得到了一个错误,因为Sw是单数


共 (1) 个答案

  1. # 1 楼答案

    所以我不确定你是从哪里得到这个想法的LUDecomposition解算器会产生伪逆。事实上,如果你看一下代码,你会发现这只适用于if a solution exists "in exact linear sense"

    相反,您应该首先创建一个SingularValueDecomposition

    SingularValueDecomposition svd = new SingularValueDecomposition(A);
    

    然后得到这个奇异值分解产生的分解解算器

    DecompositionSolver solver = svd.getSolver();
    

    然后你可以:

    1. 使用
    RealMatrix pinv = solver.getInverse();
    
    1. 或者用它来解方程组,比如Ax=b
    RealVector x = decompositionSolver.solve(b);
    
    1. 或者用它来解矩阵方程组,比如Ax=B
    RealMatrix X = decompositionSolver.solve(B);