class Vector2d extends GeomObj { public double x, y; Vector2d(double a, double b){ super(POINT_TYPE,0); x = a; y = b; } Vector2d(Vector2d v){ super(POINT_TYPE,0); x = v.x; y = v.y; } Vector2d(){ super(POINT_TYPE,0); x = y = 0.0; } // explicit constructors public Vector2d set(double a, double b){ x = a; y = b; return this; } // predicates public boolean isEqual(Vector2d v){ return ((x == v.x) && (y == v.y)); } public boolean isZero(){ return ((x == 0.0) && (y == 0.0)); } // return (this * v) public double scalarMul(Vector2d v) { return ( (x * v.x) + (y * v.y)); } // return this - v public Vector2d sub(Vector2d v) { x -= v.x; y -= v.y; return this; } // return M*this (i.e., the Transformation of this by M) public Vector2d Xform(Matrix2d M){ double xx = x; x = M.m[0]*x + M.m[1]*y; y = M.m[2]*xx + M.m[3]*y; return this; } // return the v such that M*v = c public Vector2d solve(Matrix2d M, Vector2d c){ return c.Xform(M.inv()); } // dump functions int dump(String pre, String post){ // super.dump(); System.out.print(pre); System.out.print( "(" + Double.toString(x) + ", " + Double.toString(y) + ")" ); System.out.print(post); return 0; } int dump(String s){ return dump(s, "\n"); } int dump(){ return dump(" ", "\n"); } public static void main(String[] args) { // Matrix: Matrix2d A = new Matrix2d(2.0, 5.0, 1.0, 3.0); A.dump("Matrix A = "); System.out.println("determinant = " + Double.toString(A.det()) ); A.inv().dump("inverse = \n "); A.inv().inv().dump("inverse of inverse = \n "); // Vector: Vector2d v=new Vector2d(); v.set(3.0, 2.0); v.dump("v = "); Vector2d u=new Vector2d(1.0, 4.0); u.dump("u = "); System.out.println("v.scalarMul(u) = " + Double.toString(v.scalarMul(u)) +"\n"); System.out.println("u.scalarMul(v) = " + Double.toString(u.scalarMul(v)) +"\n"); v.Xform(A).dump("v.Xform(A)"); u.solve(A,v).dump( "u.solve(A, v) = " ); Matrix2d B = new Matrix2d(); B.dump(" Zero matrix? "); B.set(); // create identity matrix B.dump(" Identity matrix? B = "); A.dump(" A = "); (B.mul(A)).dump( "B.mul(A) = "); System.out.println("sign of A.det() = " + Matrix2d.sign(A.det()) ); } } class Matrix2d { public double[] m; Matrix2d(double a, double b, double c, double d){ m = new double[4]; m[0] = a; m[1] = b; m[2] = c; m[3]= d; } // construct from 2 vectors Matrix2d(Vector2d p, Vector2d q){ m = new double[4]; m[0] = p.x; m[1] = q.x; m[2] = p.y; m[3] = q.y; } // create the zero matrix Matrix2d(){ m = new double[4]; m[0] = 0.0; m[1] = 0.0; m[2] = 0.0; m[3]= 0.0; } // create the identity matrix public Matrix2d set() { m[0] = m[3] = 1.0; m[1] = m[2] = 0.0; return this; } // set matrix from another matrix public Matrix2d set(Matrix2d A) { m[0] = A.m[0]; m[1] = A.m[1]; m[2] = A.m[2]; m[3]= A.m[3]; return this; } // set matrix from 4 numbers public Matrix2d set(double a, double b, double c, double d) { m[0] = a; m[1] = b; m[2] = c; m[3]= d; return this; } // set matrix from 2 vectors public Matrix2d set(Vector2d p, Vector2d q){ m[0] = p.x; m[1] = q.x; m[2] = p.y; m[3] = q.y; return this; } // determinant public double det() { return (m[0]*m[3] - m[1]*m[2]); } // static determinant function public static double det(double a, double b, double c, double d) { return ((a*d) - (b*c)); } public static double det(Vector2d p, Vector2d q) { return ((p.x * q.y) - (p.y * q.x)); } // static sign function public static int sign(double a){ if (a>0.0) return 1; if (a<0.0) return -1; return 0; } // static determinant function public static int signDet(double aa, double bb, double cc, double dd) { return Matrix2d.sign((aa*dd) - (bb*cc)); } // DOES NOT WORK: it did not succeed in passing the parameters // static determinant function public static int orientation2d(Vector2d p, Vector2d q, Vector2d r) { return Matrix2d.signDet((q.x-p.x), (r.x-p.x), (q.y-p.y), (r.y-p.y)); } // Matrix transpose public Matrix2d transpose(){ double tmp = m[1]; m[1] = m[2]; m[2] = tmp; return this; } // MATRIX INVERSE: returns the zero matrix if singular public Matrix2d inv() { Matrix2d M = new Matrix2d(m[3], -m[1], -m[2], m[0]); double d = det(); if (d == 0.0) // if unsolvable for (int i=0; i<4; i++) M.m[i] = 0.0; else for (int i=0; i<4; i++) M.m[i] /= d; return M; } // MATRIX MUL (= this * A) public Matrix2d mul(Matrix2d A){ double tmp[] = new double[2]; tmp[0] = m[0]*A.m[0] + m[1]*A.m[2]; m[1] = m[0]*A.m[1] + m[1]*A.m[3]; m[0] = tmp[0]; tmp[1] = m[2]*A.m[0] + m[3]*A.m[2]; m[3] = m[2]*A.m[1] + m[3]*A.m[3]; m[2] = tmp[1]; return this; } // Dump functions public int dump(String s, String t){ System.out.print(s); System.out.print( "[" + Double.toString(m[0]) + ", " + Double.toString(m[1]) + ", " + Double.toString(m[2]) + ", " + Double.toString(m[3]) + "]" ); System.out.print(t); return 0; } public int dump(String s){ return dump(s, "\n"); } public int dump(){ return dump(" ", "\n"); } }