//
import java.awt.*;
import java.applet.*;
import java.awt.event.*;
public class PartPiv extends Applet{
MyFrame window;
TextField ta;
Panel P_control, P_bottom;
Checkbox hilbert;
public static void main(String args[]) {
PartPiv bob = new PartPiv();
bob.show();
bob.init();
}
public void init () {
P_control = new Panel();
P_bottom = new Panel();
Color C_gray = new Color(150,150,150);
Color C_gray2 = new Color(120,120,120);
this.setLayout(new BorderLayout());
P_bottom.setLayout(new GridLayout(3,1));
P_bottom.setBackground(C_gray);
P_control.setBackground(C_gray2);
ta = new TextField(5);
P_control.add(new Label("Number of equations"));
P_control.add(ta);
P_control.add(new Button("Enter Equations"));
CheckboxGroup cbg = new CheckboxGroup();
hilbert = new Checkbox("Hilbert Matrix",cbg,false);
P_bottom.add(new Label("Matrix Created From"));
P_bottom.add(new Checkbox("User input",cbg,true));
P_bottom.add(hilbert);
add("North",P_control);
add("Center",P_bottom);
}
public boolean action(Event evt, Object arg) {
if (evt.target instanceof Button){
int cellNum=1;
cellNum = Integer.parseInt(ta.getText());
if (cellNum > 15)
cellNum = 15;
window = new MyFrame("Equation Input",cellNum,hilbert.getState());
}
return true;
}
}// End class
/////////////////////////////////////////////
class MyFrame extends Frame{
Panel main_P, bot_P;
double a[][];
TextField ta[][];
int n, cellNum;
public MyFrame(String s,int cellNum, boolean fillBert) {
super(s);
setBackground(new Color(150,150,150));
main_P = new Panel();
this.cellNum = cellNum;
main_P.setLayout(new GridLayout(cellNum,(cellNum+1)*2));
this.setLayout(new BorderLayout());
add("South",new Button("Done"));
add("Center",main_P);
takeInput(cellNum);
fillHillbert(cellNum, fillBert);
if (cellNum > 5)
resize(cellNum*60,((cellNum+1)*2)*15);
else
resize(cellNum*80,((cellNum+1)*2)*20);
if (cellNum == 1)
resize(150,150);
show();
}
public void takeInput(int num) {
n = num;
a = new double [n+1][n+2];
ta = new TextField [n+1][n+2];
// loop inputs the matrix data
for (int i =1;i<=n;i++) {
for (int j=1; j <= n+1;j++) {
main_P.add(new Label(""+(i)+","+(j)+":"));
ta[i][j] = new TextField();
main_P.add(ta[i][j]);
}
}
} // End takeInput()
public void fillHillbert(int n, boolean state) {
if (state) {
for (int i = 1; i <=n;i++) {
for (int j =1 ; j <= n;j++) {
ta[i][j].setText("1/"+(i+j-1));
}
ta[i][n+1].setText("0");
}
ta[1][n+1].setText("1");
ta[1][1].setText("1");
}
}
public boolean action(Event evt, Object arg) {
if (evt.target instanceof Button){
hide();
// 1/2x + y = 2
// 2x + 4y = 8x
for (int i =1;i<=n;i++) {
for (int j=1; j <= n+1;j++) {
a[i][j] = checkFraction(ta[i][j].getText());
System.out.println("ij "+a[i][j]);
}
}
doPiv();
double b [] = new double [cellNum+1];
for (int i = 1; i <= cellNum;i++) {
b[i]=a[i][cellNum+1];
}
new AnswerFrame("Solutions",b,cellNum);
}
return true;
}
public double checkFraction(String s) {
String tempNoSpace="";
for (int i = 0; i < s.length();i++) {
if (s.charAt(i) != ' ')
tempNoSpace += s.charAt(i);
}
s = tempNoSpace;
for (int i = 0; i < s.length();i++) {
if (s.charAt(i) == '/') {
String tempS="";
for (int j=0;j m) {
m = Math.abs(a[j][i]);
c = j;
}
} // end for j
//next loop interchanges rows i and c
for (int j=i;j<=n+1;j++) {
double d = a[i][j];
a[i][j] = a[c][j];
a[c][j] = d;
count++;
} // end for j
//next loop divids row i by a(i,i)
for (int j=n+1;j >= i; j--) {
a[i][j] = a[i][j] / a[i][i];
} // end for j
//next loop subracts appropriate multiples of row i from rows below
for (int j=i+1;j<=n;j++) {
for (int k=n+1; k >= i; k--) {
a[j][k] = a[j][k] - a[j][i] * a[i][k];
} //end for k
} // End for j
} //end for i
//next two statements fix the last row
a[n][n+1] = a[n][n+1]/a[n][n];
a[n][n]=1;
//next loop transforms the triangular matrix to the identity matrix
//plus a column (the last column) which is the solution vector
for (int i=n;i>=2; i--) {
for (int k=i-1;k>=1;k--){
for (int j=n+1;j>=i;j--) {
System.out.println("value8 "+a[k][j]+" N: "+n);
a[k][j] = a[k][j] - a[k][i]*a[i][j];
System.out.println("value9 "+a[k][j]);
}
}
}
//next loop prints the solution
for (int i=1;i<=n;i++) {
System.out.println("x"+i+" is equal to "+a[i][n+1]);
}
}
///////
} // End class
////////////////////////
class AnswerFrame extends Frame{
Panel main_P, bot_P;
double a[][];
TextField ta[][];
int n;
public AnswerFrame(String s,double []a, int cellNum) {
super(s);
main_P = new Panel();
main_P.setLayout(new GridLayout(cellNum,1));
this.setLayout(new BorderLayout());
add("South",new Button("Done"));
add("Center",main_P);
if (cellNum > 5)
resize(150,((cellNum+1)*2)*15);
else
resize(150,((cellNum+1)*2)*20);
if (cellNum == 1)
resize(150,150);
char c_Var='x';
c_Var -= 1;
if (cellNum > 3) {
c_Var -= cellNum;
c_Var += 3;
}
for (int i=1; i <= cellNum;i++) {
main_P.add(new Label(""+(char)(c_Var+i)+" = "+a[i]));
}
show();
}
public boolean action(Event evt, Object arg) {
if (evt.target instanceof Button){
hide();
}
return true;
}
///////
} // End class AnswerFrame