Reputation: 2897
I am using the following library for solving linear equation . The link of that library is as follows : efficient-java-matrix-library
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 ?
Upvotes: 0
Views: 1475
Reputation: 2897
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");
}
Upvotes: 0