home *** CD-ROM | disk | FTP | other *** search
/ Power-Programmierung / CD2.mdf / c / library / dos / diverses / leda / src / basic / _vector.c < prev    next >
Encoding:
C/C++ Source or Header  |  1991-11-15  |  3.4 KB  |  174 lines

  1. /*******************************************************************************
  2. +
  3. +  LEDA  2.1.1                                                 11-15-1991
  4. +
  5. +
  6. +  _vector.c
  7. +
  8. +
  9. +  Copyright (c) 1991  by  Max-Planck-Institut fuer Informatik
  10. +  Im Stadtwald, 6600 Saarbruecken, FRG     
  11. +  All rights reserved.
  12. *******************************************************************************/
  13.  
  14.  
  15.  
  16. /*  Vektoren und Matrizen */
  17.  
  18. #include <LEDA/vector.h> 
  19. #include <math.h>
  20.  
  21. vector_rep::vector_rep(int d) 
  22.  if (d<0) error_handler(1,"vector: negative dimension."); 
  23.  
  24.  dim = d; 
  25.  
  26.  if (d>0)
  27.  { v   = new double[dim];
  28.    while (d--) v[d] = 0.0;
  29.   }
  30.  else v = nil;
  31.  
  32.  }
  33.  
  34. vector_rep::vector_rep(vector_rep* p) 
  35. {
  36.   int n = p->dim;
  37.   dim = n; 
  38.  
  39.   if (n>0)
  40.   { v   = new double[n];
  41.     while (n--) v[n] = p->v[n];
  42.    }
  43.   else v = nil;
  44.  }
  45.  
  46.  
  47. void vector::check_dimensions(const vector& vec) const
  48. { if (dim()!=vec.dim() )
  49.    error_handler(1,"vector arguments have different dimensions.");
  50.  }
  51.  
  52.  
  53.  
  54. vector::vector(double x, double y) 
  55. { ptr = new vector_rep(2); 
  56.   ptr->v[0] = x;
  57.   ptr->v[1] = y;
  58.  }
  59.  
  60. vector::vector(double x, double y, double z) 
  61. { ptr = new vector_rep(3); 
  62.   ptr->v[0] = x;
  63.   ptr->v[1] = y;
  64.   ptr->v[2] = z;
  65.  }
  66.  
  67. double  vector::operator[](int i) const
  68. { if (i<0 || i>=dim())  error_handler(1,"vector: index out of range ");
  69.   return ptr->v[i]; 
  70. }
  71.  
  72. double& vector::operator[](int i)
  73. { if (i<0 || i>=dim())  error_handler(1,"vector: index out of range ");
  74.   return ptr->v[i]; 
  75. }
  76.  
  77. vector vector::operator+(const vector& vec) const
  78. { check_dimensions(vec);
  79.   register int n = dim();
  80.   vector result(n);
  81.   while (n--) result.ptr->v[n] = ptr->v[n]+vec.ptr->v[n];
  82.   return result;
  83. }
  84.  
  85. vector vector::operator-(const vector& vec) const
  86. { check_dimensions(vec);
  87.   register int n = dim();
  88.   vector result(n);
  89.   while (n--) result.ptr->v[n] = ptr->v[n]-vec.ptr->v[n];
  90.   return result;
  91. }
  92.  
  93.  
  94. vector vector::operator*(double d) const
  95. { int n = dim();
  96.   vector result(n);
  97.   while (n--) result.ptr->v[n] = ptr->v[n] * d;
  98.   return result;
  99. }
  100.  
  101. vector vector::operator/(double d) const
  102. { int n = dim();
  103.   vector result(n);
  104.   while (n--) result.ptr->v[n] = ptr->v[n] / d;
  105.   return result;
  106. }
  107.  
  108. //friend
  109. vector operator*(double f, const vector& v) { return v*f;     } 
  110.  
  111. double vector::operator*(const vector& vec) const
  112. { check_dimensions(vec);
  113.   double result=0;
  114.   register int n = dim();
  115.   while (n--) result = result+ptr->v[n]*vec.ptr->v[n];
  116.   return result;
  117. }
  118.  
  119. vector::vector(const vector& vec)
  120. { register int n = vec.dim();
  121.   ptr = new vector_rep(n);
  122.   while (n--) ptr->v[n] = vec.ptr->v[n];
  123. }
  124.   
  125. vector& vector::operator=(const vector& vec)
  126. { register int d = vec.dim();
  127.   if (d!=dim())
  128.   { clear();
  129.     ptr = new vector_rep(d);
  130.    }
  131.   while (d--) ptr->v[d] = vec.ptr->v[d];
  132.   return *this;
  133. }
  134.  
  135.  
  136. int vector::operator==(const vector& vec)  const
  137. { if (vec.dim() != dim()) return false;
  138.   int i = 0;
  139.   int d = dim();
  140.   while ((i<d) && (ptr->v[i]==vec.ptr->v[i])) i++;
  141.   return (i==d) ? true : false;
  142.  }
  143.  
  144.  
  145. ostream& operator<<(ostream& s, const vector& v)
  146. { int i;
  147.   char buf[16];
  148.   for (i=0;i<v.dim();i++) 
  149.   { sprintf(buf,"%7.2f ",v[i]);
  150.     s << buf;
  151.    }
  152.   return s;
  153. }
  154.  
  155. istream& operator>>(istream& s, vector& v)
  156. { int i=0;
  157.   while (i<v.dim() && s >> v.ptr->v[i++]);
  158.   return s;
  159. }
  160.  
  161. double vector::length() const { return sqrt(*this * *this); }
  162.  
  163. double vector::angle(const vector& y)  const
  164. { double l = length();
  165.   double yl = y.length();
  166.  
  167.   if ( l==0 || yl==0)
  168.    error_handler(1,"angle: zero argument\n");
  169.  
  170.   return  acos((*this)*y/(l*yl));  
  171. }
  172.