vector.h
上传用户:gzelex
上传日期:2007-01-07
资源大小:707k
文件大小:4k
开发平台:

MultiPlatform

  1. /*******************************************************************************
  2. +
  3. +  LEDA-R  3.2.3
  4. +
  5. +  vector.h
  6. +
  7. +  Copyright (c) 1995  by  Max-Planck-Institut fuer Informatik
  8. +  Im Stadtwald, 66123 Saarbruecken, Germany     
  9. +  All rights reserved.
  10. *******************************************************************************/
  11. #ifndef LEDA_VECTOR_H
  12. #define LEDA_VECTOR_H
  13. //------------------------------------------------------------------------------
  14. //  vectors
  15. //------------------------------------------------------------------------------
  16. #include <LEDA/basic.h>
  17. /*{Manpage {vector} {} {Real-Valued Vectors}}*/
  18. class vector
  19. {
  20. /*{Mdefinition
  21. An instance of the data type $vector$ is a vector of real variables.}*/
  22.   friend class matrix;
  23.   double* v;
  24.   int d;
  25.   void check_dimensions(const vector&) const;
  26.  
  27. public:
  28. /*{Mcreation v }*/
  29. vector(); 
  30. /*{Mcreate creates an instance $v$ of type $vector$; $v$ is initialized to 
  31.             the zero-dimensional vector.}*/
  32. vector(int d); 
  33. /*{Mcreate creates an instance $v$ of type $vector$; $v$ is initialized to 
  34.             the zero vector of dimension $d$.}*/ 
  35. vector(double a, double b);
  36. /*{Mcreate creates an instance $v$ of type $vector$; $v$ is initialized to 
  37.             the two-dimensional vector $(a,b)$.}*/
  38. vector(double a, double b, double c);
  39. /*{Mcreate creates an instance $v$ of type $vector$; $v$ is initialized to 
  40.             the three-dimensional vector $(a,b,c)$.}*/
  41.   vector(const vector&);
  42.  ~vector(); 
  43.   vector& operator=(const vector&);
  44. /*{Moperations 2 4.5}*/
  45. int    dim()    const { return d; }
  46. /*{Mop       returns the dimension of $v$.}*/ 
  47. double length() const;
  48. /*{Mop      returns the Euclidean length of $v$.}*/
  49.   
  50. vector norm()   const { return *this/length(); }
  51.   
  52. double angle(const vector& w) const; 
  53. /*{Mop     returns the angle between $v$ and $w$.}*/
  54.   
  55. double& operator[](int i);
  56. /*{Marrop     returns $i$-th component of $v$.\
  57.        precond $0le i le v$.dim()$-$1.}*/
  58.   
  59. double  operator[](int) const;
  60. vector& operator+=(const vector&);
  61. vector& operator-=(const vector&);
  62.   
  63. vector  operator+(const vector& v1) const;
  64. /*{Mbinop     Addition.\
  65.        precond $v$.dim() = $v1$.dim().}*/
  66. vector  operator-(const vector& v1) const;
  67. /*{Mbinop     Subtraction.\
  68.        precond $v$.dim() = $v1$.dim().}*/
  69. double  operator*(const vector& v1) const;
  70. /*{Mbinop     Scalar multiplication.\
  71.                precond $v$.dim() = $v1$.dim().}*/
  72. vector  operator*(double r)        const;
  73. /*{Mbinop     Componentwise multiplication with double $r$.}*/
  74. vector  operator-() const;
  75. vector  operator/(double)        const;
  76.   
  77. bool     operator==(const vector& w) const;
  78. /*{Mbinop     Test for equality.}*/
  79. bool     operator!=(const vector& w)  const { return !(*this == w); }
  80. /*{Mbinop     Test for inequality.}*/
  81.   
  82. /*
  83. friend vector operator*(double f, const vector& v);
  84. friend vector operator/(const vector& v, double f);
  85. */
  86.   
  87. friend ostream& operator<<(ostream& O, const vector& v);
  88. /*{Mbinopfunc  writes $v$ componentwise to the output stream $O$.}*/
  89. friend istream& operator>>(istream& I, vector& v);
  90. /*{Mbinopfunc  reads $v$ componentwise from the input stream $I$.}*/
  91. static int cmp(const vector&, const vector&);
  92.  
  93. LEDA_MEMORY(vector)
  94. };
  95. inline int compare(const vector& x, const vector& y)
  96. { return vector::cmp(x,y); }
  97. inline void Print(const vector& v, ostream& out) 
  98. { out << v.dim() << " " << v; }
  99. inline void Read(vector& v, istream& in)         
  100. { int d;
  101.   in >> d;  
  102.   vector x(d);
  103.   in >> x;
  104.   v = x;
  105. }
  106. LEDA_TYPE_PARAMETER(vector)
  107. /*{Mimplementation
  108. Vectors are implemented by arrays of real numbers. All operations on a vector 
  109. $v$ take time $O(v.dim())$, except for dim and $[ ]$ which take constant
  110. time. The space requirement is $O(v.dim())$.
  111. }*/
  112. #endif