【问题标题】:Matrix Solve method java矩阵求解方法java
【发布时间】:2016-11-18 17:56:32
【问题描述】:

我正在尝试使用解决方法,但它给了我一个错误,谁能帮助我如何编写解决方法?我尝试运行此代码,解决方法之前的代码工作正常,但之后它给了我一个错误。非常感谢

import java.lang.Math;
public class MatrixDriver {

  public static void main(String[] args) {
    double[][] d = { { 1, 2, 3 }, { 4, 5, 6 }, { 9, 1, 3} };

     System.out.println("Matrix D - Testing Constructor that take double integer array as a parameter");
    Matrix D = new Matrix(d);
    System.out.println(D);
    System.out.println();

    System.out.println("Matrix A - Testing Named Constructor -- random\nTakes two parameters (row, col) and returns a \"new\" Matrix");
    Matrix A = Matrix.random(5, 5);
    System.out.println(A);
    System.out.println();

    System.out.println("Matrix A - Testing the swap rows method");
    A.swapRows(1,2);
    System.out.println(A);
    System.out.println();

    System.out.println("Matrix B - Testing the transpose method on Matrix A");
    Matrix B = A.transpose();
    System.out.println(B);
    System.out.println();

    System.out.println("Matrix C - Testing named constructor - identity - which takes a single parameter (i) and returns a 'new' (i x i) Identity Matrx");
    Matrix C = Matrix.identity(5);
    System.out.println(C);
    System.out.println();

    System.out.println("Matrix E - Testing matrix addition (A + B)");
    Matrix E = A.plus(B);
    System.out.println(E);
    System.out.println();

    System.out.println("Matrix F - Testing matrix subtraction (A - B)");
    Matrix F = A.minus(B);
    System.out.println(F);
    System.out.println();

    System.out.println("Matrix G - Testing matrix scalar multiplication (c * A), c = 1.2");
    double c = 1.2;
    Matrix G = A.times(c);
    System.out.println(G);
    System.out.println();

    System.out.println("Matrix AB - Testing matrix multiplication (A X B)");
    Matrix AB = A.times(B);
    System.out.println(AB);
    System.out.println();

  System.out.println("Matrix BA - Testing matrix multiplication (B X A)");
  Matrix BA = B.times(A);
  System.out.println(BA);
  System.out.println();

    System.out.println("Matrices AB and BA - Testing Matrix Equality (AB equals BA)"); 
    try 
    {
      System.out.println("  " + AB.equals(BA));
      System.out.println();
    } 
    catch(RuntimeException e) 
    {
      System.out.println(e.getMessage());
      System.out.println();

    }
  System.out.println("Matrix b - Creating a random (5 X 1)");
  Matrix b = Matrix.random(5,1);
  System.out.println(b);
  System.out.println();

  System.out.println("Matrix X - Testing the solve method (X = A^-1 * b)");
  Matrix L= Matrix.solve(X);
  double X = Math.pow(A,-1) * b;
  //b.solve(b);
  System.out.println(b);
  System.out.println();

  System.out.println("Matrix d - Testing the sove again (A x X) = b ... is it?");
  Matrix b = Matrix.solve();
  System.out.println(b);
  System.out.println();
   }
}

【问题讨论】:

  • 错误是什么,解决方法是什么?你从哪里得到 Matrix 不管 = new Matrix()?您是否构建了自己的 Matrix 类?还是您在使用 JAMA?
  • 用于Java矩阵类的JAMA包。能否请您为Java矩阵添加库。
  • 不能从静态上下文引用非静态方法solve(Matrix)
  • 所需的输出将是矩阵 X - 测试 s o l v e 方法 (X = A^−1 ∗ b) 2 。 9 2, ,2 . 1 2 ,2 . 4 5 ,5 . 3 3 ,−10.25 我用逗号分隔它们
  • @GerMex 所需的输出将是 Matrix X - 测试 s o l v e 方法 (X = A^−1 ∗ b) 2 。 9 2, ,2 . 1 2 ,2 . 4 5 ,5 . 3 3 ,−10.25 我用逗号分隔它们

标签: java


【解决方案1】:

您没有使用您正在使用的 JAMA:http://introcs.cs.princeton.edu/java/95linear/Matrix.java.html 不是吗?下次会有帮助的。

要回答您的问题,虽然“x”是什么,但 Matrix.solve 方法不知道“X”是什么。

Matrix L= Matrix.solve(X);
double X = Math.pow(A,-1) * b;

应该是:

double X = Math.pow(A,-1) * b;
Matrix L= Matrix.solve(X);

但即便如此,您仍然会遇到错误,因为 Math.pow 不适用于矩阵,如果您想 ^2 整个矩阵,则需要使用另一种方法。在这里:https://stackoverflow.com/a/22901024/4329778

【讨论】:

  • 欣赏它,但它仍然给我一个错误“不兼容的类型:矩阵无法转换为双精度”,您在答案中描述了该错误。但我只需要一个驱动程序类的方法作为我的任务的一部分
【解决方案2】:

我可以建议post。我认为 JAMA 不再更新,org.apache.commons.math3.linear 似乎是当前最先进的矩阵库。在 Android Studio 中,您可以在 Gradle 中添加:

实现'org.apache.commons:commons-math3:3.6.1'

【讨论】:

    猜你喜欢
    • 2019-09-17
    • 2017-11-24
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2014-04-05
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    相关资源
    最近更新 更多