通过高效的Java矩阵库求解线性方程 [英] Solving linear eqn by efficient-java-matrix-library

查看:538
本文介绍了通过高效的Java矩阵库求解线性方程的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我正在使用以下库来求解线性方程.该库的链接如下: efficiency-java-matrix-library

I am using the following library for solving linear equation . The link of that library is as follows : efficient-java-matrix-library

我已经下载了库文件,并导入了eclipse中.然后,我编写了以下代码来求解一组线性方程.

I have downloaded the library file and imported in the eclipse . Then I have written the following code to solve a set of linear equations .

 SimpleMatrix A1 = new SimpleMatrix(m,n);
      SimpleMatrix b1 = new SimpleMatrix(m,1);

      for(int i=0;i<m;i++)
      {
          for(int j=0;j<n;j++)
          {
              A1.setRow(i, i, A2[i][j]);
             // System.out.println();
          }
           b1.setColumn(i, 0, B2[i]); 
      }
      try {
          SimpleMatrix x1 = A1.solve(b1);
          for(int i=0;i<x1.getNumElements();i++)
          {
              double value = x1.get(i, 0);
              System.out.println(" value of x"+i+" is "+value);
          }
      } 
      catch ( SingularMatrixException e ) 
      {
        //  throw new IllegalArgument("Singular matrix");
      }

但是对于此代码,我有以下异常.

But for this code I am having the following exception .

Exception in thread "main" java.lang.IllegalArgumentException: Specified element is out of bounds: (0 , 1)
    at org.ejml.data.DenseMatrix64F.set(Unknown Source)
    at org.ejml.simple.SimpleBase.setColumn(Unknown Source)
    at com.temp.temp.Main_function.main(Main_function.java:37)

我不明白为什么会出现此错误.您能帮我解决这个错误吗?

I cant understand why I am getting this error . Can you please help me to solve this error ?

推荐答案

SimpleMatrix A1 = new SimpleMatrix(m,n); SimpleMatrix b1 =新的SimpleMatrix(m,1);

SimpleMatrix A1 = new SimpleMatrix(m,n); SimpleMatrix b1 = new SimpleMatrix(m,1);

  for(int i=0;i<m;i++)
  {
      for(int j=0;j<n;j++)
      {
          A1.setRow(i, j, A2[i][j]);
          double value1 = A1.get(i,j);
          System.out.print(" "+value1);
          // System.out.println();
      }
      b1.setRow(i,0, B2[i]); 
      double value2 = b1.get(i,0);
      System.out.print(" = "+value2);
      System.out.println();
  }


  try {
      SimpleMatrix x1 = A1.solve(b1);
      for(int i=0;i<x1.getNumElements();i++)
      {
          double value = x1.get(i, 0);
          System.out.println(" value of x"+i+" is "+value);
      } 
  } 
  catch ( SingularMatrixException e ) 
  {
    //  throw new IllegalArgument("Singular matrix");
  }

这篇关于通过高效的Java矩阵库求解线性方程的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆