/* Hyperbolic Tessellations Copyright (C) 2007 Dmitry Brant http://dmitrybrant.com Based largely on code from http://www.plunk.org/~hatch/HyperbolicApplet/ */ #ifndef _matrix_h #define _matrix_h #include "myvector.h" //The almighty recursive Matrix class template class Matrix : public Vector { public: //dimension of matrix int dimension; //cannot be < 1 //pointer to lower dimensions Vector* items; //each of these will have dimension-1 Matrix() : Vector(), dimension(1), items(NULL) { } ~Matrix(){ if(items){ delete [] items; items = NULL; } } Matrix(const Matrix& a){ dimension = a.dimension; length = a.length; if(dimension == 2){ items = new Vector[length]; }else{ items = new Matrix[length]; } for(int i=length-1; i>=0; i--) items[i] = a.items[i]; } Vector& operator=(const Matrix& a){ if(items){ delete [] items; items = NULL; } dimension = a.dimension; length = a.length; if(dimension == 2){ items = new Vector[length]; }else{ items = new Matrix[length]; } for(int i=length-1; i>=0; i--) items[i] = a.items[i]; return *this; } Matrix(int a, int b){ //2-d dimension = 2; length = a; items = new Vector[length]; for(int i=length-1; i>=0; i--) items[i].Resize(b); } Matrix(int a, int b, int c){ //3-d dimension = 3; length = a; items = new Matrix(b,c)[length]; } Matrix(int a, int b, int c, int d){ //4-d dimension = 4; length = a; items = new Matrix(b,c,d)[length]; } Matrix(int a, int b, int c, int d, int e){ //5-d dimension = 5; length = a; items = new Matrix(b,c,d,e)[length]; } Vector& operator[](int index){ return items[index]; } Vector& operator*=(const T& t){ for(int i=length-1; i>=0; i--) items[i] *= t; return *this; } Vector& operator/=(const T& t){ for(int i=length-1; i>=0; i--) items[i] /= t; return *this; } Vector& operator+=(const Matrix& a){ for(int i=length-1; i>=0; i--) items[i] += a.items[i]; return *this; } Vector& operator-=(const Matrix& a){ for(int i=length-1; i>=0; i--) items[i] -= a.items[i]; return *this; } String toString(){ String s; for(int i=length-1; i>=0; i--) s += items[i].toString() + "\r\n"; return s; } }; #define DoubleMatrix Matrix #define IntMatrix Matrix template Matrix operator+(Matrix& a, Matrix& b){ Matrix c(a); c += b; return c; } template Matrix operator-(Matrix& a, Matrix& b){ Matrix c(a); c -= b; return c; } template Matrix operator*(Matrix& a, Matrix& b){ Matrix c(a); c += b; return c; } #endif