/* solution to ES101 Laboratory Asignment for Wednesday of the week of 6/9/04 */ import java.awt.*; import javabook.MainWindow; import javabook.InputBox; class Test { public static void main (String[] arg) { MainWindow mainWindow; InputBox inputBox; mainWindow = new MainWindow(" "); Matrix4x4 m; mainWindow.setVisible(true); inputBox = new InputBox(mainWindow); m = new Matrix4x4( inputBox.getInteger("m11"),inputBox.getInteger("m21"), inputBox.getInteger("m31"),inputBox.getInteger("m41"),inputBox.getInteger("m12"), inputBox.getInteger("m22"),inputBox.getInteger("m32"),inputBox.getInteger("m42"), inputBox.getInteger("m13"),inputBox.getInteger("m23"),inputBox.getInteger("m33"), inputBox.getInteger("m43"),inputBox.getInteger("m14"),inputBox.getInteger("m24"), inputBox.getInteger("m34"),inputBox.getInteger("m44")); System.out.println("the determinant of the matrix is " + m.determinant()); m = m.multiply(m.transpose()); System.out.print(m.getM12u().getColVectorA().getA() + " " + m.getM12u().getColVectorB().getA()+" "); System.out.print(m.getM34u().getColVectorA().getA() + " " + m.getM34u().getColVectorB().getA()); System.out.println(); System.out.print(m.getM12u().getColVectorA().getB() + " " + m.getM12u().getColVectorB().getB()+" "); System.out.print(m.getM34u().getColVectorA().getB() + " " + m.getM34u().getColVectorB().getB()); System.out.println(); System.out.print(m.getM12l().getColVectorA().getA() + " " + m.getM12l().getColVectorB().getA()+" "); System.out.print(m.getM34l().getColVectorA().getA() + " " + m.getM34l().getColVectorB().getA()); System.out.println(); System.out.print(m.getM12l().getColVectorA().getB() + " " + m.getM12l().getColVectorB().getB()+" "); System.out.print(m.getM34l().getColVectorA().getB() + " " + m.getM34l().getColVectorB().getB()); System.out.println(); } } class TwoVector { //Vector elements private int a, b; /* constructors */ TwoVector() { a = 0; b = 0; } TwoVector(int x, int y) { a = x; b = y; } //////////////////////////////////// /* set methods fot the elements */ public void setA(int x) { a = x; } public void setB(int y) { b = y; } //////////////////////////////////// /* get methods for the elements */ public int getA() { return a; } public int getB() { return b; } //////////////////////////////////// public TwoVector addTwoVector( TwoVector v) { TwoVector sum = new TwoVector(); sum.a = v.a + a; sum.b = v.b + b; return sum; } public TwoVector dotProductTwoVector( TwoVector v) { TwoVector dotProduct = new TwoVector(); dotProduct.a = v.a*a; dotProduct.b = v.b*b; return dotProduct; } } class Matrix2x2 { private TwoVector a,b; /* constructors */ Matrix2x2() { a = new TwoVector(); b = new TwoVector(); } Matrix2x2(TwoVector x,TwoVector y) { a = x; b = y; } Matrix2x2(int aa,int ab,int ba, int bb) { a = new TwoVector(aa,ab); b = new TwoVector(ba,bb); } //////////////////////////////////// /* set methods fot the elements */ public void setAA(int aa) { a.setA(aa); } public void setAB(int ab) { a.setB(ab); } public void setBA(int ba) { b.setA(ba); } public void setBB(int bb) { b.setB(bb); } //////////////////////////////////// /* get methods for the elements */ public int getAA() { return a.getA(); } public int getAB() { return a.getB(); } public int getBA() { return b.getA(); } public int getBB() { return b.getB(); } //////////////////////////////////// /* set methods for vectors */ public void setColVectorA(TwoVector x) { a = x; } public void serColVectorB(TwoVector y) { b = y; } //////////////////////////////////// /* get methods for vectors */ public TwoVector getColVectorA() { return a; } public TwoVector getColVectorB() { return b; } //////////////////////////////////// public Matrix2x2 transpose() { Matrix2x2 t = new Matrix2x2(a.getA(),b.getA(),a.getB(),b.getB()); return t; } public Matrix2x2 multiply(Matrix2x2 m) { Matrix2x2 t = new Matrix2x2( a.getA()*m.getColVectorA().getA() + b.getA()*m.getColVectorA().getB(), a.getB()*m.getColVectorA().getA() + b.getB()*m.getColVectorA().getB(), a.getA()*m.getColVectorB().getA() + b.getA()*m.getColVectorB().getB(), a.getB()*m.getColVectorB().getA() + b.getB()*m.getColVectorB().getB()); return t; } public Matrix2x2 add(Matrix2x2 m) { Matrix2x2 t = new Matrix2x2( a.getA()+m.getColVectorA().getA(), a.getB()+m.getColVectorA().getB(), b.getA()+m.getColVectorB().getA(), b.getB()+m.getColVectorB().getB()); return t; } public int determinant() { return (a.getA()*b.getB() - a.getB()*b.getA()); } } class Matrix4x4 { private Matrix2x2 m12u,m12l,m34u,m34l; /* constructors */ Matrix4x4() { m12u = new Matrix2x2(); m12l = new Matrix2x2(); m34u = new Matrix2x2(); m34l = new Matrix2x2(); } Matrix4x4(Matrix2x2 im12u,Matrix2x2 im12l,Matrix2x2 im34u,Matrix2x2 im34l) { m12u = im12u; m12l = im12l; m34u = im34u; m34l = im34l; } Matrix4x4(int m12uaa,int m12uab,int m12laa,int m12lab,int m12uba,int m12ubb,int m12lba,int m12lbb, int m34uaa,int m34uab,int m34laa,int m34lab,int m34uba,int m34ubb,int m34lba,int m34lbb) { m12u = new Matrix2x2(m12uaa,m12uab,m12uba,m12ubb); m12l = new Matrix2x2(m12laa,m12lab,m12lba,m12lbb); m34u = new Matrix2x2(m34uaa,m34uab,m34uba,m34ubb); m34l = new Matrix2x2(m34laa,m34lab,m34lba,m34lbb); } //////////////////////////////////// public Matrix2x2 getM12u() { return m12u; } public Matrix2x2 getM12l() { return m12l; } public Matrix2x2 getM34u() { return m34u; } public Matrix2x2 getM34l() { return m34l; } public Matrix4x4 add(Matrix4x4 m) { Matrix4x4 t = new Matrix4x4(m12u.add(m.getM12u()),m12l.add(m.getM12l()),m34u.add(m.getM34u()),m34l.add(m.getM34l())); return t; } public Matrix4x4 multiply(Matrix4x4 m) { Matrix4x4 t = new Matrix4x4(m12u.multiply(m.m12u).add(m34u.multiply(m.m12l)), m12l.multiply(m.m12u).add(m34l.multiply(m.m12l)), m12u.multiply(m.m34u).add(m34u.multiply(m.m34l)), m12l.multiply(m.m34u).add(m34l.multiply(m.m34l))); return t; } public Matrix4x4 transpose() { Matrix4x4 t = new Matrix4x4(m12u.transpose(),m34u.transpose(),m12l.transpose(),m34l.transpose()); return t; } public int determinant() { Matrix2x2 m13u = new Matrix2x2(m12u.getColVectorA(),m34u.getColVectorA()); Matrix2x2 m13l = new Matrix2x2(m12l.getColVectorA(),m34l.getColVectorA()); Matrix2x2 m24u = new Matrix2x2(m12u.getColVectorB(),m34u.getColVectorB()); Matrix2x2 m24l = new Matrix2x2(m12l.getColVectorB(),m34l.getColVectorB()); Matrix2x2 m14u = new Matrix2x2(m12u.getColVectorA(),m34u.getColVectorB()); Matrix2x2 m14l = new Matrix2x2(m12l.getColVectorA(),m34l.getColVectorB()); Matrix2x2 m23u = new Matrix2x2(m12u.getColVectorB(),m34u.getColVectorA()); Matrix2x2 m23l = new Matrix2x2(m12l.getColVectorB(),m34l.getColVectorA()); return (m12u.determinant()*m34l.determinant()+m12l.determinant()*m34u.determinant()- m13u.determinant()*m24l.determinant()-m13l.determinant()*m24u.determinant()+ m14u.determinant()*m23l.determinant()+m14l.determinant()*m23u.determinant()); } }