栏目分类:
子分类:
返回
名师互学网用户登录
快速导航关闭
当前搜索
当前分类
子分类
实用工具
热门搜索
名师互学网 > IT > 面试经验 > 面试问答

Java逆矩阵计算

面试问答 更新时间: 发布时间: IT归档 最新发布 模块sitemap 名妆网 法律咨询 聚返吧 英语巴士网 伯小乐 网商动力

Java逆矩阵计算

指数?不,我相信矩阵求逆是O(N ^ 3)。

我建议使用LU分解来求解矩阵方程。使用时无需求解行列式。

更好的是,寻找一个可以帮助您的软件包。 JAMA浮现在脑海。

12x12或19x19不是大型矩阵。这是常见的解决与几十或几百问题 十万 的自由度。

这是如何使用JAMA的有效示例。编译和运行时,必须在CLASSPATH中包含JAMA JAR:

package linearalgebra;import Jama.LUDecomposition;import Jama.Matrix;public class JamaDemo{    public static void main(String[] args)    {        double [][] values = {{1, 1, 2}, {2, 4, -3}, {3, 6, -5}};  // each array is a row in the matrix        double [] rhs = { 9, 1, 0 }; // rhs vector        double [] answer = { 1, 2, 3 }; // this is the answer that you should get.        Matrix a = new Matrix(values);        a.print(10, 2);        LUDecomposition luDecomposition = new LUDecomposition(a);        luDecomposition.getL().print(10, 2); // lower matrix        luDecomposition.getU().print(10, 2); // upper matrix        Matrix b = new Matrix(rhs, rhs.length);        Matrix x = luDecomposition.solve(b); // solve Ax = b for the unknown vector x        x.print(10, 2); // print the solution        Matrix residual = a.times(x).minus(b); // calculate the residual error        double rnorm = residual.normInf(); // get the max error (yes, it's very small)        System.out.println("residual: " + rnorm);    }}

这是根据quantum_dev的建议使用Apache Commons Math解决的相同问题:

package linearalgebra;import org.apache.commons.math.linear.Array2DRowRealMatrix;import org.apache.commons.math.linear.ArrayRealVector;import org.apache.commons.math.linear.DecompositionSolver;import org.apache.commons.math.linear.LUDecompositionImpl;import org.apache.commons.math.linear.RealMatrix;import org.apache.commons.math.linear.RealVector;public class LinearAlgebraDemo{    public static void main(String[] args)    {        double [][] values = {{1, 1, 2}, {2, 4, -3}, {3, 6, -5}};        double [] rhs = { 9, 1, 0 };        RealMatrix a = new Array2DRowRealMatrix(values);        System.out.println("a matrix: " + a);        DecompositionSolver solver = new LUDecompositionImpl(a).getSolver();        RealVector b = new ArrayRealVector(rhs);        RealVector x = solver.solve(b);        System.out.println("solution x: " + x);;        RealVector residual = a.operate(x).subtract(b);        double rnorm = residual.getLInfNorm();        System.out.println("residual: " + rnorm);    }}

使这些适应您的情况。



转载请注明:文章转载自 www.mshxw.com
本文地址:https://www.mshxw.com/it/487011.html
我们一直用心在做
关于我们 文章归档 网站地图 联系我们

版权所有 (c)2021-2022 MSHXW.COM

ICP备案号:晋ICP备2021003244-6号