Diploma Thesis Percolation Simulation
C++ Sourcecode Documentation

www.AndreasKrueger.de/thesis/code

Main Page   Namespace List   Class Hierarchy   Alphabetical List   Compound List   File List   Namespace Members   Compound Members   File Members  

my_vector.h

Go to the documentation of this file.
00001 // ndim_vector.h
00002 // class definition for a variable-dim vector 
00003 // up to MAXDIM dimensions (faster than variable-dim!) 
00004 // with subtraction, length, unit-, null- and randomvector
00005 // first implemented idea 1.2.2000  
00006 //
00007 // VERSION 2.3
00008 // last update: 27.9.2001
00009 // Andreas Krueger
00010 
00011 // #define COORDFLOAT float             // for cordinates
00012 // #define REAL double          // for radius, filligfactor, etc. 
00013 // #define NUMBER long          // for the number of clusters, etc.
00014 
00015 // TODO: 
00016 //
00017 // COORDFLOAT coord(int dir);
00018 // set the OUTSIDE access to coordinate from [0;...;MAXDIM-1] to [1;...;MAXDIM]
00019 // so that x=1, y=2, z=3, etc.
00020 //
00021 // it is already that way in the counting algos 
00022 // using bool lower {   return (u.x[dir-1] < border); }
00023 // but it is NOT in the visualization 
00024 // and all other places that use COORDFLOAT coord(int dir); !!!
00025 // e.g. coordinate2boxes
00026 // e.g. ...
00027 
00028 #ifndef LOADED_LITTLE_HELPERS
00029 #include "little_helpers.h"
00030 #endif
00031 
00032 class myVector {
00033   int dim;                                                      // protected variable for dimensionality
00034   COORDFLOAT x[MAXDIM];                                 // protected vector components 
00035 
00036 public:       
00037   myVector();                                                           // Default-Constructor (dim=0)
00038   myVector(int setdim);                                 // Constructor-function for definition, calls set_dim
00039   myVector(const myVector& u);                          // CopyConstructor
00040 
00041   void set_dim(int setdim);                             // sets dimensionality and sets coordinates=0;
00042   friend int getdim(const myVector &u); // gives dimension
00043 
00044   void randomvect (COORDFLOAT L);                       // (0...L)^dimension
00045   void nullvect();                                              // 0,0,0,...,0 (x0, x1, x2, , ... , dim-1)
00046   void unitvect();                                              // 0,0,0,...,0 (x0, x1, x2, , ... , dim-1)
00047   friend REAL length(const myVector &u);        // vector length
00048   friend REAL squarelength(const myVector &u);  // square of vector length (no SQRT!)
00049 
00050   myVector& operator=(const myVector& u);                               // sets same dimension and copies
00051   myVector& operator-=(const myVector&);                                                // subtraction for connection vectors
00052   friend myVector operator- (const myVector&,const myVector&);  // subtraction for connection vectors
00053   friend myVector operator- (const myVector&);                                          // (x1, x2)  -->  (-x1, -x2)
00054   
00055   friend ostream& operator<<(ostream&, myVector&);                      // output operator
00056   friend istream& operator>>(istream&, myVector&);                      // input operator
00057   friend bool operator<(const myVector&,const myVector&);               // compare operator (length order)
00058   bool operator!=(const myVector& u);                                                   // different vectors?
00059   friend bool lower(const myVector&, COORDFLOAT border,int dir);        // is vector in lower (than 'border') part of this dimensional direction?
00060 
00061   COORDFLOAT coord(int dir);                            // returns x[...];
00062 };
00063 
00064 myVector::myVector(){
00065         dim=0;
00066 }
00067 
00068 myVector::myVector(int setdim){
00069         if ((setdim<1)||(setdim>MAXDIM)) {
00070                 errorout("'myVector' dimension < 1 or > MAXDIM");
00071                 exit(1);
00072         }
00073         else{
00074                 set_dim(setdim);
00075         }
00076 }
00077 
00078 myVector::myVector(const myVector &u) {                         // copy constructor
00079         dim=u.dim;                                                                      // create the new vector with same dimensionality
00080         for (int i=0;i<dim;i++) {                                       // copy components
00081                 x[i] = u.x[i];
00082         }
00083 }
00084 
00085 inline void myVector::set_dim(int setdim){
00086         dim=setdim;
00087         nullvect();
00088 }
00089 
00090 int getdim(const myVector &u) {
00091         return u.dim;
00092 }
00093 
00094 
00095 inline REAL length(const myVector &u) {
00096    COORDFLOAT temp=0;
00097         for (int i=0;i<u.dim;i++) 
00098                 {temp+=u.x[i]*u.x[i];}
00099         return sqrt(temp);
00100 }
00101 
00102 REAL squarelength(const myVector &u) {
00103    COORDFLOAT temp=0;
00104         for (int i=0;i<u.dim;i++) 
00105                 {temp+=u.x[i]*u.x[i];}
00106         return temp;
00107 }
00108 
00109 ostream& operator<<(ostream& os, myVector& z) {
00110         cout <<'(';
00111         for (int i=0;i<z.dim-1;i++) {
00112                 cout<<z.x[i]<<',';
00113         }
00114         return os<<z.x[z.dim-1]<<')';
00115 }
00116 
00117 istream& operator>>(istream& is, myVector& z) {
00118         for (int i=0;i<z.dim;i++) {
00119                 cin>>z.x[i];
00120         }
00121         return is;
00122 }
00123 
00124 inline myVector& myVector::operator=(const myVector& u) {                       // give only reference for faster code
00125         if (dim!=u.dim) errorout("'=' different dimensionality");
00126         else{
00127                 for (int i=0;i<dim;i++) {
00128                         x[i] = u.x[i];
00129                 }
00130         }
00131         return *this;
00132 }
00133 
00134 inline myVector& myVector::operator-=(const myVector& u) {      // give only reference for faster code
00135         if (dim!=u.dim) errorout("'-=' different dimensionality");
00136         else {
00137                 for (int i=0;i<dim;i++) {
00138                         x[i] -= u.x[i];
00139                 }
00140         }
00141         return *this;
00142 }
00143 
00144 
00145 myVector operator- (const myVector& u, const myVector& v) {             
00146         if (u.dim!=v.dim) {
00147                 errorout("'-' different dimensionality");
00148                 return u;
00149         }
00150         else{
00151                 myVector temp = u;
00152                 return (temp -= v);                                                             // calls myVector::operator-=(myVector)
00153         }
00154 }
00155 
00156 myVector operator- (const myVector& u){
00157         myVector temp(u.dim);
00158         temp.nullvect();
00159         return (temp-=u);
00160 }
00161 
00162 inline void myVector::nullvect () {
00163         for (int i=0;i<dim;i++) {
00164                 x[i] = 0;
00165         }
00166 }
00167 void myVector::unitvect() {
00168         for (int i=0;i<dim;i++) {
00169                 x[i] = 1;
00170         }
00171 }
00172 
00173 
00174 
00175 REAL short_random (REAL L) {
00176         REAL temp=rand()*L/(double)RAND_MAX;
00177         return temp;
00178 }
00179 
00180 REAL long_random(REAL L){
00181         REAL temp=rand()+rand()/(REAL)RAND_MAX;
00182         temp*=L/(REAL)RAND_MAX;
00183         return temp;
00184 }
00185 
00186 
00187 void myVector::randomvect (COORDFLOAT L) {
00188         for (int i=0;i<dim;i++) {
00189                 x[i] = long_random(L);
00190         }
00191 }
00192 
00193 
00194 
00195 bool operator<(const myVector &u, const myVector &v) {                  // length order
00196   return (length(u) < length(v) );
00197 }
00198 
00199 bool myVector::operator!=(const myVector& u) {  // give only reference for faster code
00200         bool temp=0;
00201         if (u.dim!=dim) temp=1;
00202         else {
00203                 for (int i=0;i<u.dim;i++) {
00204                         if (x[i] != u.x[i]) temp=1;
00205                 }
00206         }
00207         return temp;
00208 }
00209 
00210 
00211 bool lower(const myVector& u, COORDFLOAT border, int dir) {
00212         // is myVector u in lower (than 'border') part of this dimensional direction?
00213         if ((dir<1)||(dir>u.dim)) {
00214                 errorout("space direction < 1 or > dim");
00215                 exit(1);
00216                 return false;
00217         }
00218         else return (u.x[dir-1] < border);
00219 }
00220 
00221 
00222 COORDFLOAT myVector::coord(int dir) {
00223         return x[dir];
00224 }
00225 
00226 //  end of class definition: myVector
00227 




Diploma Thesis Sourcecode Documentation
check out the text and the executable binaries

www.AndreasKrueger.de/thesis/code