//
import java.awt.*;
import java.applet.*;
import java.math.*;
public class Plot extends Applet {
final Color myColor = new Color(255,200,200);
static TextField2 tf_A11,
tf_A12,
tf_A21,
tf_A22,
tf_x1,
tf_x2,
tf_b1,
tf_b2,
tfcondition ;
static Choice c7;
GraphPanel gp;
public void init(){
setLayout(new BorderLayout());
createTextField2s();
Panel pete = new Panel();
//pete.setLayout(new GridLayout(1,2));
pete.setLayout(new BorderLayout());
gp = new GraphPanel(300,300);
gp.setBackground(myColor);
gp.setEquation(2.0,3.0,8.0,5.0,4.0,13.0);
pete.add("Center",gp);
Panel pp1 = new Panel();
pp1.setLayout(new GridLayout(7,1));
pp1.setBackground(myColor);
Panel pp = new Panel();
pp.setBackground(myColor);
tfcondition = new TextField2(8,this);
pp.add(new Label("Condition:"));
pp.add(tfcondition);
pp1.add(new Button("Zoom In"));
pp1.add(new Button("Zoom Out"));
pp1.add(new Button("Example #1"));
pp1.add(new Button("Choose Random"));
pp1.add(new Button("Calculate"));
Panel p_Choice = new Panel();
Choice c = new Choice();
for (int i=1;i<=10;i++)
c.addItem(""+i);
c.select(2);
p_Choice.add(new Label("Precision: "));
p_Choice.add(c);
pp1.add(p_Choice);
c7 = c;
pp1.add(pp);
pete.add("East",pp1);
add("South",embedPanels());
add("Center",pete);
}
public Panel embedPanels(){
Panel pete = new Panel();
pete.setLayout(new BorderLayout());
pete.setBackground(myColor);
pete.add("Center",createPanels());
pete.add("North",createButtons());
return pete;
}
public void createTextField2s(){
tf_A11 = new TextField2(8,this);
tf_A12 = new TextField2(8,this);
tf_A21 = new TextField2(8,this);
tf_A22 = new TextField2(8,this);
tf_x1 = new TextField2(8,this);
tf_x2 = new TextField2(8,this);
tf_b1 = new TextField2(8,this);
tf_b2 = new TextField2(8,this);
tf_A11.setText(""+2);
tf_A12.setText(""+3);
tf_A21.setText(""+5);
tf_A22.setText(""+4);
tf_b1.setText(""+8);
tf_b2.setText(""+13);
}
public Panel createButtons(){
Panel p_But = new Panel();
p_But.add(new Button("Example #1"));
p_But.add(new Button("Choose Random"));
p_But.add(new Button("Calculate"));
Panel p_Choice = new Panel();
Choice c = new Choice();
for (int i=1;i<=10;i++)
c.addItem(""+i);
c.select(2);
p_Choice.add(new Label("Precision: "));
p_Choice.add(c);
p_But.add(p_Choice);
return new Panel();
}
public Panel createPanels(){
Panel p1 = new Panel();
p1.setLayout(new GridLayout(4,1));
p1.add(new Label("a11"));
p1.add(tf_A11);
p1.add(new Label("a21"));
p1.add(tf_A21);
Panel p2 = new Panel();
p2.setLayout(new GridLayout(4,1));
p2.add(new Label("a12"));
p2.add(tf_A12);
p2.add(new Label("a22"));
p2.add(tf_A22);
Panel p3 = new Panel();
p3.setLayout(new GridLayout(4,1));
p3.add(new Label("x1"));
p3.add(tf_x1);
p3.add(new Label("x2"));
p3.add(tf_x2);
Panel p4 = new Panel();
p4.setLayout(new GridLayout(4,1));
p4.add(new Label("b1"));
p4.add(tf_b1);
p4.add(new Label("b2"));
p4.add(tf_b2);
Panel p_Equal = new Panel();
p_Equal.setLayout(new GridLayout(4,1));
p_Equal.add(new Label(" "));
p_Equal.add(new Label("="));
p_Equal.add(new Label(" "));
p_Equal.add(new Label("="));
Panel p_Control = new Panel();
p_Control.setBackground(myColor);
p_Control.add(p1);
p_Control.add(p2);
p_Control.add(p3);
p_Control.add(p_Equal);
p_Control.add(p4);
return p_Control;
}
public boolean action(Event evt, Object arg){
if (arg.equals("Zoom In")){
gp.factor=gp.factor*1.5;
gp.shiftx = (int)Math.round(gp.z1*gp.factor);
gp.shifty = (int)Math.round(gp.z2*gp.factor);
gp.repaint();
repaint();
}
if (arg.equals("Zoom Out")){
gp.factor=gp.factor*2.0/3.0;
gp.shiftx = (int)Math.round(gp.z1*gp.factor);
gp.shifty = (int)Math.round(gp.z2*gp.factor);
gp.repaint();
repaint();
}
if (evt.target instanceof Choice){
fixPrecision();
//GraphPanel.showMe = true; // was2
doPiv();
fixPrecision();
gp.setEquation(Double.valueOf(tf_A11.getText()).doubleValue(),
Double.valueOf(tf_A12.getText()).doubleValue(),
Double.valueOf(tf_b1.getText()).doubleValue(),
Double.valueOf(tf_A21.getText()).doubleValue(),
Double.valueOf(tf_A22.getText()).doubleValue(),
Double.valueOf(tf_b2.getText()).doubleValue());
fixPrecision();
}
if (arg.equals("Calculate") || arg.equals("Choose Random")){
if (arg.equals("Choose Random"))
generateRandom();
fixPrecision();
GraphPanel.showMe = true; //was2
doPiv();
fixPrecision();
gp.setEquation(Double.valueOf(tf_A11.getText()).doubleValue(),
Double.valueOf(tf_A12.getText()).doubleValue(),
Double.valueOf(tf_b1.getText()).doubleValue(),
Double.valueOf(tf_A21.getText()).doubleValue(),
Double.valueOf(tf_A22.getText()).doubleValue(),
Double.valueOf(tf_b2.getText()).doubleValue());
fixPrecision();
gp.shiftx=0;
gp.shifty=0;
}
if (arg.equals("Example #1")){
tf_A11.setText("2");
tf_A12.setText("3");
tf_A21.setText("5");
tf_A22.setText("4");
tf_b1.setText("8");
tf_b2.setText("13");
fixPrecision();
double a[][] = new double[4][4];
a[1][1]= Double.valueOf(tf_A11.getText()).doubleValue();
a[1][2]=Double.valueOf(tf_A12.getText()).doubleValue();
a[1][3]=Double.valueOf(tf_b1.getText()).doubleValue();
a[2][1]=Double.valueOf(tf_A21.getText()).doubleValue();
a[2][2]=Double.valueOf(tf_A22.getText()).doubleValue();
a[2][3]=Double.valueOf(tf_b2.getText()).doubleValue();
//GraphPanel.showMe = true; //was2
doPiv();
fixPrecision();
gp.setEquation(Double.valueOf(tf_A11.getText()).doubleValue(),
Double.valueOf(tf_A12.getText()).doubleValue(),
Double.valueOf(tf_b1.getText()).doubleValue(),
Double.valueOf(tf_A21.getText()).doubleValue(),
Double.valueOf(tf_A22.getText()).doubleValue(),
Double.valueOf(tf_b2.getText()).doubleValue());
gp.shiftx=0;
gp.shifty=0;
}
return true;
}
public void generateRandom(){
double multiple = c7.getSelectedIndex()+1;
multiple = Math.pow(10,multiple);
tf_A11.setText(""+Math.round(1+((Math.random()-.5)*multiple)));
tf_A12.setText(""+Math.round(1+((Math.random()-.5)*multiple)));
tf_A21.setText(""+Math.round(1+((Math.random()-.5)*multiple)));
tf_A22.setText(""+Math.round(1+((Math.random()-.5)*multiple)));
tf_b1.setText(""+Math.round(1+((Math.random()-.5)*multiple)));
tf_b2.setText(""+Math.round(1+((Math.random()-.5)*multiple)));
tf_x1.setText("");
tf_x2.setText("");
}
//////////////////////////// Partial Pivoting ////////////////////////////////
public void doPiv(){
double a[][] = new double[4][4];
a[1][1]= Double.valueOf(tf_A11.getText()).doubleValue();
a[1][2]=Double.valueOf(tf_A12.getText()).doubleValue();
a[1][3]=Double.valueOf(tf_b1.getText()).doubleValue();
a[2][1]=Double.valueOf(tf_A21.getText()).doubleValue();
a[2][2]=Double.valueOf(tf_A22.getText()).doubleValue();
a[2][3]=Double.valueOf(tf_b2.getText()).doubleValue();
// System.out.println("Condition:"+condition(a[1][1],a[1][2],a[2][1],a[2][2]));
tfcondition.setText(GraphPanel.correctScale(
new BigDecimal(""+condition(a[1][1],a[1][2],a[2][1],a[2][2])),
c7.getSelectedIndex()+1).toString());
doPiv_(2,a);
}
public void fixPrecision(){
tf_A11.setText(GraphPanel.correctScale(new BigDecimal(tf_A11.getText()),c7.getSelectedIndex()+1).toString());
tf_A12.setText(GraphPanel.correctScale(new BigDecimal(tf_A12.getText()),c7.getSelectedIndex()+1).toString());
tf_A21.setText(GraphPanel.correctScale(new BigDecimal(tf_A21.getText()),c7.getSelectedIndex()+1).toString());
tf_A22.setText(GraphPanel.correctScale(new BigDecimal(tf_A22.getText()),c7.getSelectedIndex()+1).toString());
tf_b1.setText(GraphPanel.correctScale(new BigDecimal(tf_b1.getText()),c7.getSelectedIndex()+1).toString());
tf_b2.setText(GraphPanel.correctScale(new BigDecimal(tf_b2.getText()),c7.getSelectedIndex()+1).toString());
try {
if (!tf_x1.getText().equals(""))
tf_x1.setText(GraphPanel.correctScale(new BigDecimal(tf_x1.getText()),c7.getSelectedIndex()+1).toString());
if (!tf_x2.getText().equals(""))
tf_x2.setText(GraphPanel.correctScale(new BigDecimal(tf_x2.getText()),c7.getSelectedIndex()+1).toString());
} catch (NumberFormatException e){}
}
public void doPiv_(int n, double a[][]) {
int count=0;
//next loop and the following 2 statements put the matrix system in
//diagonal form with 1's on the diagonal
for (int i=1;i<=n-1;i++) {
double m = Math.abs(a[i][i]);
int c = i;
// next loop finds the highest element down the column
for (int j=i+1;j<=n;j++)
if (Math.abs(a[j][i])> m) {
m = Math.abs(a[j][i]);
c = 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];
//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 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]);
tf_x1.setText(""+a[1][2+1]);
tf_x2.setText(""+a[2][2+1]);
gp.z1 = a[1][3];
gp.z2 = a[2][3];
}
//////////////////////////// End Partial Pivoting /////////////////////////////
public double condition (double a11,double a12, double a21, double a22){
double p = a11*a11+a21*a21;
double q = a11*a12+a21*a22;
double r = a12*a12+a22*a22;
double t = p+r;
double d = t*t-4.0*(p*r-q*q);
double s = Math.sqrt(d);
double x = t+s;
double y = t-s;
if (y <= 0){
System.out.println("Condition number is infinite & matrix is singular.");
return 0;
}
double z = x/y;
//System.out.println("z: "+z);
return Math.sqrt(z);
}
////////////////////////////////////////////////////////////////////////////////
} // End class
//
////////////////////////////////////////////////////////////////
//
class GraphPanel extends Panel {
int w,
h,shiftx=0,shifty=0;
double x,
y,
b,
factor,
z1,
z2;
static double factor2=1.0;
double x_, y_, b_;
static boolean showMe;
public GraphPanel(int w, int h){
this.w = w;
this.h = h;
shiftx=0;
shifty=0;
}
public void paint(Graphics g){
g.setColor(Color.white);
g.fillRect(0,0,w,h);
drawAxis(g);
g.setColor(Color.red);
drawEquationFuzz(g);
drawEquation(g);
// Clear Edges
g.setColor(new Color(255,200,200));
g.fillRect(0,h,w,h+200);
g.fillRect(w,h,w,h+200);
// Draw Box around graph
g.setColor(Color.black);
g.drawRect(0,0,w-1,h-1);
}
public void drawAxis(Graphics g){
g.setColor(Color.blue);
// Vertical Line
g.drawLine(scaleX((reScaleX(h/2))),0,scaleX((reScaleX(h/2))),h);
// Horizontal Line
g.drawLine(0,scaleY((reScaleY(w/2))),h,scaleY((reScaleY(w/2))));
// Draw graph ticks
//----------------------------------------------------------------------------
// Hash marks on horizontal line
g.drawLine(-shiftx+3,scaleY(reScaleY(h/2-2)),-shiftx+3,scaleY(reScaleY(h/2+2)));
g.drawLine(-shiftx+w-3,scaleY(reScaleY(h/2-2)),-shiftx+w-3,scaleY(reScaleY(h/2+2)));
g.drawLine(-shiftx+w-w/4,scaleY(reScaleY(h/2-2)),-shiftx+w-w/4,scaleY(reScaleY(h/2+2)));
g.drawLine(-shiftx+w/4,scaleY(reScaleY(h/2-2)),-shiftx+w/4,scaleY(reScaleY(h/2+2)));
// Hash marks on vertical line
g.drawLine(scaleX(reScaleX(w/2-2)),shifty+3,scaleX(reScaleX(w/2+2)),shifty+3);
g.drawLine(scaleX(reScaleX(w/2-2)),shifty+h-3,scaleX(reScaleX(w/2+2)),shifty+h-3);
g.drawLine(scaleX(reScaleX(w/2-2)),shifty+h/4,scaleX(reScaleX(w/2+2)),shifty+h/4);
g.drawLine(scaleX(reScaleX(w/2-2)),shifty+h-h/4,scaleX(reScaleX(w/2+2)),shifty+h-h/4);
// Draw Scaling
g.setColor(new Color(200,200,200));
g.drawString("0",scaleX(reScaleX(h/2))+2,scaleY(reScaleY(w/2))+11);
try {
g.drawString(""+correctScale(new BigDecimal(""+h/(factor*2.0)),
1+Plot.c7.getSelectedIndex()),scaleX(reScaleX(w/2))+2,shifty+11);
g.drawString(""+correctScale(new BigDecimal(""+w/(factor*-2.0)),
1+Plot.c7.getSelectedIndex()),scaleX(reScaleX(0))+3,scaleY(reScaleY(w/2))+11);
} catch(Exception e){System.out.println("Burp!!"+factor);}
}
public void drawEquation(Graphics g){
g.setColor(Color.red);
if (!showMe)
g.setColor(new Color(255,200,200));
g.drawLine(0,scaleY(solveForY(reScaleY(shiftx))),w,scaleY(solveForY(reScaleY(shiftx+w))));
g.drawLine(0,scaleY(solveForY_(reScaleY(shiftx))),w,scaleY(solveForY_(reScaleY(shiftx+w))));
}
// Draw Line2
public void drawEquationFuzz(Graphics g){
if (showMe) {
g.setColor(new Color(200,255,200));
double ne1,
ne2;
ne1 = solveForY(reScaleY(shiftx));
ne2 = solveForY(reScaleY(shiftx+h));
int ld = Plot.c7.getSelectedIndex()+1;
double diff = Math.pow(5,-ld+1);
Polygon p = new Polygon();
double dif = diff*Math.sqrt(getSlope()*getSlope()+1);
p.addPoint(0,scaleY(ne1+dif));
p.addPoint(0,scaleY(ne1-dif));
p.addPoint(w,scaleY(ne2-dif));
p.addPoint(w,scaleY(ne2+dif));
g.fillPolygon(p);
// Equation2
ne1 = solveForY_(reScaleY(shiftx));
ne2 = solveForY_(reScaleY(shiftx+w));
dif = diff*Math.sqrt(getSlope_()*getSlope_()+1);
p = new Polygon();
p.addPoint(0,scaleY(ne1+dif));
p.addPoint(0,scaleY(ne1-dif));
p.addPoint(w,scaleY(ne2-dif));
p.addPoint(w,scaleY(ne2+dif));
g.fillPolygon(p);
}
}
public void setEquation(double x, double y, double b, double x_, double y_, double b_){
this.x = x;
this.y = y;
this.b = b;
this.x_ = x_;
this.y_ = y_;
this.b_ = b_;
setScale();
repaint();
}
public double solveForX(double y1){
double t = y*y1;
t = b-t;
return t/x;
}
public double solveForY(double x1){
double t = x*x1;
t = b-t;
return t/y;
}
public double solveForX_(double y1){
double t = y_*y1;
t = b_-t;
return t/x_;
}
public double solveForY_(double x1){
double t = x_*x1;
t = b_-t;
return t/y_;
}
public double getSlope(){
return -x/y;
}
public double getSlope_(){
return -x_/y_;
}
public void setScale(){
double big = Math.abs(solveForX(0));
double uY = Math.abs(solveForY(0));
double uX_ = Math.abs(solveForX_(0));
double uY_ = Math.abs(solveForY_(0));
big = (uY > big) ? uY : big;
big = (uX_ > big) ? uX_ : big;
big = (uY_ > big) ? uY_ : big;
if (z1 != 0 && z2 !=0){
double z1a = Math.abs(z1);
double z2a = Math.abs(z2);
big = (z1a > big) ? z1a+z1a/8.0 : big;
big = (z2a > big) ? z2a+z1a/8.0 : big;
}
factor = w/(2.0*big);
}
public void setScaleCenter(){
shiftx = scaleX(z1)+w/2;
shifty = scaleY(z2)+h/2;
double big = Math.abs(solveForX(0));
double uY = Math.abs(solveForY(0));
double uX_ = Math.abs(solveForX_(0));
double uY_ = Math.abs(solveForY_(0));
big = (uY > big) ? uY : big;
big = (uX_ > big) ? uX_ : big;
big = (uY_ > big) ? uY_ : big;
if (z1 != 0 && z2 !=0){
z1 = Math.abs(z1);
z2 = Math.abs(z2);
big = (z1 > big) ? z1+z1/8.0 : big;
big = (z2 > big) ? z2+z1/8.0 : big;
}
factor = w/(2.0*big);
}
public int scaleX(double v){
double value = v*factor;
value += w/2.0;
value -=shiftx;
return (int)Math.round(value);
}
public int scaleY(double v){
double value = v*factor;
value = h/2.0 - value;
value +=shifty;
return (int)Math.round(value);
}
public double reScaleY(int v){
double value = (v-(h/2.0));
value = value/factor;
return value;
}
public double reScaleX(int v){
double value = v-w/2.0;
value = (value*1.0)/factor;
return value;
}
public boolean mouseDown (Event evt, int x, int y){
//System.out.println("X: "+x+" y: "+y);
//setScale();
repaint();
return true;
}
public boolean keyDown (Event evt, int key){
if (key == Event.UP)
shifty-=5;
if (key == Event.DOWN)
shifty+=5;
if (key == Event.LEFT)
shiftx+=5;
if (key == Event.RIGHT)
shiftx-=5;
if (key == Event.HOME){
shiftx-=5;
shifty-=5;
}
if (key == Event.PGUP){
shiftx-=5;
shifty-=5;
}
if (key == Event.END){
shiftx+=5;
shifty+=5;
}
if (key == Event.PGDN){
shiftx-=5;
shifty+=5;
}
//setScale();
repaint();
return true;
}
public static BigDecimal correctScale(BigDecimal b,int sc){
boolean stillZero = true;
int x = b.toString().indexOf('.');
if (x < 0)
x = b.toString().length();
b = b.movePointLeft(x);
int ggg = slide(b);
b = b.movePointRight(ggg);
b = b.setScale(sc,BigDecimal.ROUND_HALF_EVEN);
b = b.movePointLeft(ggg);
b = b.movePointRight(x);
return b;
}
public static int slide(BigDecimal b){
int slideRight = 0;
boolean stillZero = true;
for (int i=b.toString().indexOf('.')+1;i