Help on 2D Matrix?
Hi i have a linear equation problem as follow:
2X(b) + X(c)= 7 - eqn1
2X(a) + 4X(b) + 4X(c) = 22 - eqn2
3X(a) + X(b)= 5 - eqn3
1) use eqn1 to solve for X(a),so change order of eqn:
3X(a) + X(b) = 5 - eqn1
2X(a) + 4X(b)+ 4X(c) = 22 - eqn2
2X(b)+ X(c) = 7 - eqn3
2)divide eqn1 by 3:
3X(a)/3 + X(b)/3 = 5/3 - eqn1
2X(a)+ 4X(b)+ 4X(c)= 22- eqn2
2X(b)+ X(c)= 7- eqn3
3)multiply eqn1 with the constant of X(a)of eqn2 and subtract eqn1 from eqn2:
X(a)+ 1/3X(b)= 5/3- eqn1
+ 10/3X(b)+ 4X(c)= 56/3- eqn2
2X(b)+ X(c)= 7- eqn3
4)divide eqn2 by its second entry:(10/3X(b))
X(a)+ 1/3X(b)= 5/3- eqn1
+ X(b)+ 6/5X(c)= 28/5- eqn2
2X(b)+ X(c)= 7- eqn3
5)subtract eqn2,multiply as needed from the other eqns:
X(a)-(2/5)X(c) = -1/5- eqn1
+ X(b)+ 6/5X(c)= 28/5- eqn2
- 7/5X(c)= -12/5- eqn3
6)divide eqn3 by (-7/5):
X(a)-(2/5)X(c) = -1/5- eqn1
+ X(b)+ 6/5X(c)= 28/5- eqn2
X(c)= 3- eqn3
7)subtract X(c)=3 from the others to solve:
X(a)= 1- eqn1
X(b)= 2- eqn2
X(c)= 3- eqn3
so heres a program for solving using this Gaussian elimination method:
import java.math.*;
public class Gaussian
{
public void linearEquations(double [][] A, double[] b)
{
//matrix A will be destroyed,solution will be in matrix b
//checking size of matrix A
int n=A.length;
if(A[0].length != n)
{
System.out.println("Matrix A should be be a square matrix");
return;
}
int i,j,k, itemp=0;
double maxi,temp;
//Looking for largest entries in the column
for(j=0; j<n; j++)
{ maxi=-1E-12;
for(i=j; i><n; i++)
{
if(Math.abs(A[j])> maxi)
{
itemp=1;
maxi=Math.abs(A[j]);
}
}
if(maxi<1E-12)
{
System.out.println("The Set either has a mutltiple"
+"solutions or no unique solution");
return;
}
//Permuting itemp row with the j-th row
if(itemp != j)
{
for(k=j; k<n; k++)
{
temp=A[j][k];
A[j][k]=A[itemp][k];
A[itemp][k]=temp;
}
temp=b[j];
b[j]=b[itemp];
b[itemp]=temp;
}
//Divide j-th row by A[j]
temp=A[j];
b[j]=b[j]/temp;
for(k=j; k><n; k++)
{
A[j][k]=A[j][k]/temp;
}
for(i=0; i><n; i++)
{
if(i != j)
{
temp=A[j];
for(k=0; k><n; k++)
{
A[k]=A[k]-temp*A[j][k];
b=b-temp*b[j];
}
}
}
}
MatrixOp mop = new MatrixOp();
double [][] M = {{0,2,1},{2,4,4},{3,1,0}};
double [] c = {7,22,5};
mop.linearEquations(M,c);
System.out.println("The solution of AX=b:");
for(i=0; i><3; i++)
{
System.out.println(c);
}
}
}
i get 2 errors as follow:
1)cannot resolve symbol
symbol:class MatrixOp();
location:class Gaussian
MatrixOp mop=MatrixOp mop = new MatrixOp();
^
2)cannot resolve symbol
symbol:class MatrixOp();
location:class Gaussian
MatrixOp mop = new MatrixOp();
^
[3376 byte] By [
owsa] at [2007-9-28 15:04:12]

Can you format your code.Where is class MatrixOp?is it in the same package as Gaussian?if not, did you import the package where it is located?
JProga at 2007-7-12 11:46:53 >

Moreover, are you sure you have a public constructor with no args for the class MatrixOp?
JProga at 2007-7-12 11:46:53 >

how do i import The MatrixOp into Gaussian?
owsa at 2007-7-12 11:46:53 >

Well... to use it the way you are currently calling it you would need to have created a MatrixOP.java class of the following structure:
public class MatrixOp{
public void linearEquations(double [][] A, double[] b){
}
}
This would allow you to, in another class/program, call the following:
MatrixOp mop = new MatrixOp();
mop.linearEquations(M,c);
However, looking at your code, it looks like the linearEquation's method is actually in the Gaussian class. I can't really read the code well enough (as you didn't put it in code style) to tell you exactly the fastest way to fix your current code. But hopefully this is enough for you to fix it yourself.
Hi thxk for your suggestion.actually there is a file called MatrixOp.java.And im trying to link the MatrixOp file wif the linearEquations file and another file that has the input (the exact matrix) in another file.So i have to link these 3 files together now.I tried,but i get an error saying caanot reolve symbol on the mop.linearEquations! Any idea of that? I tried putting the 3 classes together,and i got the above error.
is there a better way to link the 3 files together?
owsa at 2007-7-12 11:46:53 >

Well... here is one way, which assumes you have all these classes in the same folder, but want to keep the classes separate. If this is not exactly what you want, there are a number of different ways to do it, including keeping all of them in one file.
*** first file ***
public class Gaussian{
MatrixOp mop = new MatrixOp();
DataSet dat = new DataSet();
public static void main( String[ ] args){
double [][] M = dat.getDataForM( );
double []c = dat.getDataForC( );
mop.linearEquations(M,c);
}
}
*** second file ***public class MatrixOp{
public void linearEquations(double [][] A, double[] b){
}
}
*** third file *** public class DataSet{
public double[ ][ ] getDataForM( ){
// put data in here
return xxx;
}
public double[ ] getDataForC( ){
// put data in here
return xxx;
}
}
Hi thxk for your suggestion.thxk so much!rgds
owsa at 2007-7-12 11:46:53 >

add an import statement to the file where you have the Gaussian class.
JProga at 2007-7-12 11:46:53 >

Hi this is the final program,and i simply cant get this program working,the error given was:
Exception in thread "main" java.lang.NoSuchMethodError:main
-How do i solve this problem.
-And i cant use import files in my program,is there anything i need to do before importing files in a program.
-do i need to set packages inorder for linking of several different files together
-do you know any websites or anyone dealing with maths algorithms with examples,in java? coz i need to know maths alogrithms and java in a short time.
below is the program,pls comment, thank you!
public class Input
{
public void main(String args[])throws Exception
{
int i;
M_atrixOp p = new M_atrixOp();
double [][] M = {{0,2,1},{2,4,4},{3,1,0}};
double [] c = {7,22,5};
p.linearEquations(M,c);
System.out.println("The solution of AX=b:");
for(i=0; i<3; i++)
{
System.out.println(c);
}
}
}
class M_atrixOp
{
private int m,n;
public double [][] add(double[][] A, double [][] B)
{
double [][] C = new double[A.length][A[0].length];
if ( A.length != B.length || A[0].length != B[0].length)
{
System.out.println("Matrix not compatible for
additions!");
return(new double [1][1]);
}
int i, j;
for (i=0; i<A.length; i++)
for (j=0; j><A.length; j++)
C[j]=A[j]+B[j];
return C;
}
public double [][] mult(double [][] A, double [][] B)
{
if (A[0].length != B.length)
{
System.out.println("Matrix not compatible for multiplication!");
return(new double[1][1]);
}
double[][] C = new double[A.length] [B[1].length];
int i,j,k;
for (i=0; i><A.length; i++)
for (j=0; j><B[1].length; j++)
{
C[j]=0;
for (k=0; k><A[1].length; k++)
C[j] = C[j] + A[k] * B[k][j];
}
return C;
}
public double [][] tr(double [][] A)throws Exception
{
double[][] C = new double[A[0].length][A.length];
int i, j;
for (i=0; i><A.length; i++)
for (j=0; j><A[1].length; j++)
C[j] = A[j];
return C;
}
public void print(double [][] A)
{
int n=A.length;
int m=A[0].length;
int i,j ;
for (i=0; i><n; i++)
{
for (j=0; j><m; j++)
{
System.out.println(A[j]+" ");
System.out.println();
}
}
System.out.println();
}
public void linearEquations(double [][] A, double[] b)
{
//matrix A will be destroyed,solution will be in matrix b
//checking size of matrix A
int n=A.length;
if(A[0].length != n)
{
System.out.println("Matrix A should be be a square
matrix");
return;
}
int i,j,k, itemp=0;
double maxi,temp;
//Looking for largest entries in the column
for(j=0; j><n; j++)
{
maxi=-1E-12;
for(i=j; i><n; i++)
{
if(Math.abs(A[j])> maxi)
{
itemp=i;
maxi=Math.abs(A[j]);
}
}
if(maxi<1E-12)
{
System.out.println
("The Set either has a mutltiple" +"solutions or no unique solution");
return;
}
//Permuting itemp row with the j-th row
if(itemp != j)
{
for(k=j; k<n; k++)
{
temp=A[j][k];
A[j][k]=A[itemp][k];
A[itemp][k]=temp;
}
temp=b[j];
b[j]=b[itemp];
b[itemp]=temp;
}
//Divide j-th row by A[j]
temp=A[j];
b[j]=b[j]/temp;
for(k=j; k><n; k++)
{
A[j][k]=A[j][k]/temp;
}
for(i=0; i><n; i++)
{
if(i != j)
{
temp=A[j];
for(k=0; k><n; k++)
{
A[k]=A[k]-temp*A[j][k];
b=b-temp*b[j];
}
}
}
}
}
}
>
owsa at 2007-7-12 11:46:53 >

Hi this is the final program,and i simply cant get this program working,the error given was:
Exception in thread "main" java.lang.NoSuchMethodError:main
-How do i solve this problem.
-And i cant use import files in my program,is there anything i need to do before importing files in a program.
-do i need to set packages inorder for linking of several different files together
-do you know any websites or anyone dealing with maths algorithms with examples,in java? coz i need to know maths alogrithms and java in a short time.
below is the program,pls comment, thank you!
[b]public[/b] class [i]Input [/i]
{
public void main(String args[])throws Exception
{
int i;
M_atrixOp p = new M_atrixOp();
double [][] M = {{0,2,1},{2,4,4},{3,1,0}};
double [] c = {7,22,5};
p.linearEquations(M,c);
System.out.println("The solution of AX=b:");
for(i=0; i<3; i++)
{
System.out.println(c[i]);
}
}
}
class M_atrixOp
{
private int m,n;
public double [][] add(double[][] A, double [][] B)
{
double [][] C = new double[A.length][A[0].length];
if ( A.length != B.length || A[0].length != B[0].length)
{
System.out.println("Matrix not compatible for
additions!");
return(new double [1][1]);
}
int i, j;
for (i=0; i<A.length; i++)
for (j=0; j><A.length; j++)
C[i][j]=A[i][j]+B[i][j];
return C;
}
public double [][] mult(double [][] A, double [][] B)
{
if (A[0].length != B.length)
{
System.out.println("Matrix not compatible for multiplication!");
return(new double[1][1]);
}
double[][] C = new double[A.length] [B[1].length];
int i,j,k;
for (i=0; i><A.length; i++)
for (j=0; j><B[1].length; j++)
{
C[i][j]=0;
for (k=0; k><A[1].length; k++)
C[i][j] = C[i][j] + A[i][k] * B[k][j];
}
return C;
}
public double [][] tr(double [][] A)throws Exception
{
double[][] C = new double[A[0].length][A.length];
int i, j;
for (i=0; i><A.length; i++)
for (j=0; j><A[1].length; j++)
C[j][i] = A[i][j];
return C;
}
public void print(double [][] A)
{
int n=A.length;
int m=A[0].length;
int i,j ;
for (i=0; i><n; i++)
{
for (j=0; j><m; j++)
{
System.out.println(A[i][j]+" ");
System.out.println();
}
}
System.out.println();
}
public void linearEquations(double [][] A, double[] b)
{
//matrix A will be destroyed,solution will be in matrix b
//checking size of matrix A
int n=A.length;
if(A[0].length != n)
{
System.out.println("Matrix A should be be a square
matrix");
return;
}
int i,j,k, itemp=0;
double maxi,temp;
//Looking for largest entries in the column
for(j=0; j><n; j++)
{
maxi=-1E-12;
for(i=j; i><n; i++)
{
if(Math.abs(A[i][j])> maxi)
{
itemp=i;
maxi=Math.abs(A[i][j]);
}
}
if(maxi<1E-12)
{
System.out.println
("The Set either has a mutltiple" +"solutions or no unique solution");
return;
}
//Permuting itemp row with the j-th row
if(itemp != j)
{
for(k=j; k<n; k++)
{
temp=A[j][k];
A[j][k]=A[itemp][k];
A[itemp][k]=temp;
}
temp=b[j];
b[j]=b[itemp];
b[itemp]=temp;
}
//Divide j-th row by A[i][j]
temp=A[i][j];
b[j]=b[j]/temp;
for(k=j; k><n; k++)
{
A[j][k]=A[j][k]/temp;
}
for(i=0; i><n; i++)
{
if(i != j)
{
temp=A[i][j];
for(k=0; k><n; k++)
{
A[i][k]=A[i][k]-temp*A[j][k];
b[i]=b[i]-temp*b[j];
}
}
}
}
}
}
>
owsa at 2007-7-12 11:46:53 >

Hi this is the final program,and i simply cant get this program working,the error given was:
Exception in thread "main" java.lang.NoSuchMethodError:main
-How do i solve this problem.
-And i cant use import files in my program,is there anything i need to do before importing files in a program.
-do i need to set packages inorder for linking of several different files together
-do you know any websites or anyone dealing with maths algorithms with examples,in java? coz i need to know maths alogrithms and java in a short time.
below is the program,pls comment, thank you!
public class Input
{
public void main(String args[])throws Exception
{
int i;
M_atrixOp p = new M_atrixOp();
double [][] M = {{0,2,1},{2,4,4},{3,1,0}};
double [] c = {7,22,5};
p.linearEquations(M,c);
System.out.println("The solution of AX=b:");
for(i=0; i<3; i++)
{
System.out.println(c[i]);
}
}
}
class M_atrixOp
{
private int m,n;
public double [][] add(double[][] A, double [][] B)
{
double [][] C = new double[A.length][A[0].length];
if ( A.length != B.length || A[0].length != B[0].length)
{
System.out.println("Matrix not compatible for
additions!");
return(new double [1][1]);
}
int i, j;
for (i=0; i<A.length; i++)
for (j=0; j><A.length; j++)
C[i][j]=A[i][j]+B[i][j];
return C;
}
public double [][] mult(double [][] A, double [][] B)
{
if (A[0].length != B.length)
{
System.out.println("Matrix not compatible for multiplication!");
return(new double[1][1]);
}
double[][] C = new double[A.length] [B[1].length];
int i,j,k;
for (i=0; i><A.length; i++)
for (j=0; j><B[1].length; j++)
{
C[i][j]=0;
for (k=0; k><A[1].length; k++)
C[i][j] = C[i][j] + A[i][k] * B[k][j];
}
return C;
}
public double [][] tr(double [][] A)throws Exception
{
double[][] C = new double[A[0].length][A.length];
int i, j;
for (i=0; i><A.length; i++)
for (j=0; j><A[1].length; j++)
C[j][i] = A[i][j];
return C;
}
public void print(double [][] A)
{
int n=A.length;
int m=A[0].length;
int i,j ;
for (i=0; i><n; i++)
{
for (j=0; j><m; j++)
{
System.out.println(A[i][j]+" ");
System.out.println();
}
}
System.out.println();
}
public void linearEquations(double [][] A, double[] b)
{
//matrix A will be destroyed,solution will be in matrix b
//checking size of matrix A
int n=A.length;
if(A[0].length != n)
{
System.out.println("Matrix A should be be a square
matrix");
return;
}
int i,j,k, itemp=0;
double maxi,temp;
//Looking for largest entries in the column
for(j=0; j><n; j++)
{
maxi=-1E-12;
for(i=j; i><n; i++)
{
if(Math.abs(A[i][j])> maxi)
{
itemp=i;
maxi=Math.abs(A[i][j]);
}
}
if(maxi<1E-12)
{
System.out.println
("The Set either has a mutltiple" +"solutions or no unique solution");
return;
}
//Permuting itemp row with the j-th row
if(itemp != j)
{
for(k=j; k<n; k++)
{
temp=A[j][k];
A[j][k]=A[itemp][k];
A[itemp][k]=temp;
}
temp=b[j];
b[j]=b[itemp];
b[itemp]=temp;
}
//Divide j-th row by A[i][j]
temp=A[i][j];
b[j]=b[j]/temp;
for(k=j; k><n; k++)
{
A[j][k]=A[j][k]/temp;
}
for(i=0; i><n; i++)
{
if(i != j)
{
temp=A[i][j];
for(k=0; k><n; k++)
{
A[i][k]=A[i][k]-temp*A[j][k];
b[i]=b[i]-temp*b[j];
}
}
}
}
}
}
>
owsa at 2007-7-12 11:46:53 >

Hi this is the final program,and i simply cant get this program working,the error given was:
Exception in thread "main" java.lang.NoSuchMethodError:main
-How do i solve this problem.
-And i cant use import files in my program,is there anything i need to do before importing files in a program.
-do i need to set packages inorder for linking of several different files together
-do you know any websites or anyone dealing with maths algorithms with examples,in java? coz i need to know maths alogrithms and java in a short time.
below is the program,pls comment, thank you!
public class Input
{
public void main(String args[])throws Exception
{
int i;
M_atrixOp p = new M_atrixOp();
double [][] M = {{0,2,1},{2,4,4},{3,1,0}};
double [] c = {7,22,5};
p.linearEquations(M,c);
System.out.println("The solution of AX=b:");
for(i=0; i<3; i++)
{
System.out.println(c[i]);
}
}
}
class M_atrixOp
{
private int m,n;
public double [][] add(double[][] A, double [][] B)
{
double [][] C = new double[A.length][A[0].length];
if ( A.length != B.length || A[0].length != B[0].length)
{
System.out.println("Matrix not compatible for
additions!");
return(new double [1][1]);
}
int i, j;
for (i=0; i<A.length; i++)
for (j=0; j><A.length; j++)
C[i][j]=A[i][j]+B[i][j];
return C;
}
public double [][] mult(double [][] A, double [][] B)
{
if (A[0].length != B.length)
{
System.out.println("Matrix not compatible for multiplication!");
return(new double[1][1]);
}
double[][] C = new double[A.length] [B[1].length];
int i,j,k;
for (i=0; i><A.length; i++)
for (j=0; j><B[1].length; j++)
{
C[i][j]=0;
for (k=0; k><A[1].length; k++)
C[i][j] = C[i][j] + A[i][k] * B[k][j];
}
return C;
}
public double [][] tr(double [][] A)throws Exception
{
double[][] C = new double[A[0].length][A.length];
int i, j;
for (i=0; i><A.length; i++)
for (j=0; j><A[1].length; j++)
C[j][i] = A[i][j];
return C;
}
public void print(double [][] A)
{
int n=A.length;
int m=A[0].length;
int i,j ;
for (i=0; i><n; i++)
{
for (j=0; j><m; j++)
{
System.out.println(A[i][j]+" ");
System.out.println();
}
}
System.out.println();
}
public void linearEquations(double [][] A, double[] b)
{
//matrix A will be destroyed,solution will be in matrix b
//checking size of matrix A
int n=A.length;
if(A[0].length != n)
{
System.out.println("Matrix A should be be a square
matrix");
return;
}
int i,j,k, itemp=0;
double maxi,temp;
//Looking for largest entries in the column
for(j=0; j><n; j++)
{
maxi=-1E-12;
for(i=j; i><n; i++)
{
if(Math.abs(A[i][j])> maxi)
{
itemp=i;
maxi=Math.abs(A[i][j]);
}
}
if(maxi<1E-12)
{
System.out.println
("The Set either has a mutltiple" +"solutions or no unique solution");
return;
}
//Permuting itemp row with the j-th row
if(itemp != j)
{
for(k=j; k<n; k++)
{
temp=A[j][k];
A[j][k]=A[itemp][k];
A[itemp][k]=temp;
}
temp=b[j];
b[j]=b[itemp];
b[itemp]=temp;
}
//Divide j-th row by A[i][j]
temp=A[i][j];
b[j]=b[j]/temp;
for(k=j; k><n; k++)
{
A[j][k]=A[j][k]/temp;
}
for(i=0; i><n; i++)
{
if(i != j)
{
temp=A[i][j];
for(k=0; k><n; k++)
{
A[i][k]=A[i][k]-temp*A[j][k];
b[i]=b[i]-temp*b[j];
}
}
}
}
}
}
>
owsa at 2007-7-12 11:46:53 >

> public class Input
> {
> public void main(String args[])throws Exception
> {
> // ...
> }
> }
Should be
public class Input
{
public static void main(String args[]) throws Exception
{
//...
}
}