/* File: Simplex.java */ /* Copyright (C) 1997 K. Ikeda */ import java.util.*; import java.awt.*; import java.applet.*; class RN { int n; int d; RN() {n=0; d=1;} RN(int n) {this.n=n; this.d=1;} RN(int n, int d) {this.n=n; this.d=d; reduce();} RN(String s) { int k = s.indexOf('/'); if (k>0) { d = Integer.valueOf(s.substring(k+1)).intValue(); s = s.substring(0,k); } else d = 1; n = Integer.valueOf(s).intValue(); reduce(); } int euclid(int a, int b) { int q,r; if (a < 0) a = -a; if (b < 0) b = -b; if (b == 0) if (a==0) return -1; else return a; for (;;) { q = a / b; r = a % b; if (r==0) break; a = b; b = r; } return b; } boolean reduce() { int c; if ((c = euclid(n,d))<0) return false; if (d<0) c *= -1; n /= c; d /= c; return true; } void set(int n) {this.n=n; this.d=1;} void set(int n, int d) {this.n=n; this.d=d;} void set(RN a) {n=a.n; d=a.d;} void mul(RN a) { a.reduce(); RN aa = new RN(n,a.d); RN bb = new RN(a.n,d); aa.reduce(); bb.reduce(); n = aa.n * bb.n; d = aa.d * bb.d; } void div(RN a) { a.reduce(); RN aa = new RN(n, a.n); RN bb = new RN(a.d, d); aa.reduce(); bb.reduce(); n = aa.n * bb.n; d = aa.d * bb.d; } void inv() {int x; x = n; n = d; d = x; reduce();} boolean plus(RN a) { int c,x,y; c = euclid(d, a.d); if (c < 0) return false; if ((x = a.d/c*n + d/c*a.n)==0) { x = 0; y = 1; } else y = d/c*a.d; n = x; d = y; this.reduce(); return true; } boolean minus(RN a) { int c,x,y; c = euclid(d, a.d); if (c < 0) return false; if ((x = a.d/c*n - d/c*a.n)==0) { x = 0; y = 1; } else y = d/c*a.d; n = x; d = y; this.reduce(); return true; } boolean gt(RN a) {RN c=new RN(n,d); c.minus(a); return c.n>0;} boolean ge(RN a) {RN c=new RN(n,d); c.minus(a); return c.n>=0;} boolean eq(RN a) {RN c=new RN(n,d); c.minus(a); return c.n==0;} boolean le(RN a) {RN c=new RN(n,d); c.minus(a); return c.n<=0;} boolean lt(RN a) {RN c=new RN(n,d); c.minus(a); return c.n<0;} } public class Simplex extends Applet { int m,n,r,s; int step,cycle; RN[][] a = new RN[10][20]; int[] base = new int[10]; String message = ""; void Print(Graphics g, FontMetrics fm, int x, int y, String s) { int w = fm.stringWidth(s); int h = fm.getHeight(); g.drawString(s,x-w/2,y-h/2+fm.getAscent()); } void Print(Graphics g, FontMetrics fm, int x, int y, int n) { Print(g,fm,x,y,""+n); } void Print(Graphics g, FontMetrics fm, int x, int y, RN rn) { int wn, wd, h = fm.getHeight(); int sign; if (rn.n<0) sign = -1; else sign = 1; if (rn.d == 1) { Print(g,fm,x,y,sign*rn.n); wd = fm.stringWidth(""+(sign*rn.n)); } else { Print(g,fm,x,y-h/2-2,sign*rn.n); Print(g,fm,x,y+h/2+2,rn.d); wn = fm.stringWidth(""+sign*rn.n); wd = fm.stringWidth(""+rn.d); if (wn>wd) wd = wn; g.drawLine(x-wd/2,y-1,x+wd/2,y-1); } if (sign<0) { wn = wd/2+fm.stringWidth("-")+2; wd = fm.getHeight(); g.drawString("-",x-wn,y-wd/2+fm.getAscent()); } } void input_data() { m = Integer.parseInt(getParameter("m")); n = Integer.parseInt(getParameter("n")); String sdat = getParameter("data"); StringTokenizer st = new StringTokenizer(sdat,","); for (int i = 0; i<=m; i++) { for (int j=0; j=0) { s = -1; return true; } else return false; } boolean step2() { /* search pivot r of (r, s) */ RN t = new RN(); RN c = new RN(); for (int i=0; i0)|| (step==3&&(i==r||j==s))) c = Color.blue; else c = Color.black; g.setColor(c); } public void paint(Graphics g) { int w,h; int x,y; FontMetrics fm = g.getFontMetrics(); w = fm.stringWidth("012345")+10; h = fm.getHeight()*2+4; x = 25 + w/2; y = 25 + h/2; g.setColor(getBackground()); g.fillRect(x-w/2,y-h/2,(n+2)*w+1,h+1); g.setColor(Color.black); Print(g,fm,x,y,"basis"); for (int j=0; j