Back to index

texmacs  1.0.7.15
vector.hpp
Go to the documentation of this file.
00001 
00002 /******************************************************************************
00003 * MODULE     : vector.hpp
00004 * DESCRIPTION: vectors with reference counting and pointer copying
00005 * COPYRIGHT  : (C) 2006  Joris van der Hoeven
00006 *******************************************************************************
00007 * This software falls under the GNU general public license version 3 or later.
00008 * It comes WITHOUT ANY WARRANTY WHATSOEVER. For details, see the file LICENSE
00009 * in the root directory or <http://www.gnu.org/licenses/gpl-3.0.html>.
00010 ******************************************************************************/
00011 
00012 #ifndef VECTOR_H
00013 #define VECTOR_H
00014 #include "properties.hpp"
00015 #include "operators.hpp"
00016 #define TMPL template<typename T>
00017 #define BINARY_TMPL template<typename T, typename U>
00018 #define R typename unary_properties<T>::norm_type
00019 #define M typename binary_properties<T,U>::product_type
00020 
00021 TMPL class vector;
00022 TMPL int N (vector<T> a);
00023 TMPL T* A (vector<T> a);
00024 
00025 /******************************************************************************
00026 * The vector class
00027 ******************************************************************************/
00028 
00029 TMPL
00030 class vector_rep: concrete_struct {
00031   int n;
00032   T* a;
00033 public:
00034   inline vector_rep (T* a2, int n2): n(n2), a(a2) {}
00035   inline ~vector_rep () { if (a != NULL) tm_delete_array (a); }
00036   friend class vector<T>;
00037   friend int N LESSGTR (vector<T> a);
00038   friend T* A LESSGTR (vector<T> a);
00039 };
00040 
00041 TMPL
00042 class vector {
00043 CONCRETE_TEMPLATE(vector,T);
00044   inline vector (T *a, int n):
00045     rep (tm_new<vector_rep<T> > (a, n)) {}
00046   inline vector (T c, int n) {
00047     T* a= (n == 0? (T*) NULL: tm_new_array<T> (n));
00048     for (int i=0; i<n; i++) a[i]= c;
00049     rep= tm_new<vector_rep<T> > (a, n); }
00050   inline vector () {
00051     rep= tm_new<vector_rep<T> > ((T*) NULL, 0); }
00052   inline vector (T c1) {
00053     T* a= tm_new_array<T> (1); a[0]= c1;
00054     rep= tm_new<vector_rep<T> > (a, 1); }
00055   inline vector (T c1, T c2) {
00056     T* a= tm_new_array<T> (2); a[0]= c1; a[1]= c2;
00057     rep= tm_new<vector_rep<T> > (a, 2); }
00058   inline vector (T c1, T c2, T c3) {
00059     T* a= tm_new_array<T> (3); a[0]= c1; a[1]= c2; a[2]= c3;
00060     rep= tm_new<vector_rep<T> > (a, 3); }
00061   inline T& operator [] (int i) { return rep->a[i]; }
00062 };
00063 CONCRETE_TEMPLATE_CODE(vector,typename,T);
00064 
00065 TMPL
00066 class unary_properties<vector<T> > {
00067 public:
00068   typedef T scalar_type;
00069   typedef typename unary_properties<T>::norm_type norm_type;
00070   typedef int index_type;
00071   static inline tree index_name (index_type i) {
00072     return tree (RSUB, "x", as_string (i+1)); }
00073   static inline scalar_type access (vector<T> v, index_type var) {
00074     return v[var]; }
00075 };
00076 
00077 BINARY_TMPL
00078 class binary_properties<vector<T>,vector<U> > {
00079 public:
00080   typedef vector<M > product_type;
00081 };
00082 
00083 BINARY_TMPL
00084 class binary_properties<T,vector<U> > {
00085 public:
00086   typedef vector<M > product_type;
00087 };
00088 
00089 BINARY_TMPL
00090 class binary_properties<vector<T>,U > {
00091 public:
00092   typedef vector<M > product_type;
00093 };
00094 
00095 /******************************************************************************
00096 * Basic vector routines
00097 ******************************************************************************/
00098 
00099 TMPL inline int N (vector<T> v) { return v->n; }
00100 TMPL inline T* A (vector<T> v) { return v->a; }
00101 
00102 TMPL tree
00103 as_tree (vector<T> v) {
00104   int i, n= N(v);
00105   T* a= A(v);
00106   tree t (TUPLE, n);
00107   for (i=0; i<n; i++)
00108     t[i]= as_tree (a[i]);
00109   return t;
00110 }
00111 
00112 TMPL inline tm_ostream&
00113 operator << (tm_ostream& out, vector<T> v) {
00114   return out << as_math_string (as_tree (v));
00115 }
00116 
00117 /******************************************************************************
00118 * Abstract operations on vectors
00119 ******************************************************************************/
00120 
00121 template<typename T, typename Op> vector<T>
00122 unary (vector<T> v) {
00123   int i, n= N(v);
00124   T* a= A(v);
00125   T* r= tm_new_array<T> (n);
00126   for (i=0; i<n; i++)
00127     r[i]= Op::eval (a[i]);
00128   return vector<T> (r, n);
00129 }
00130 
00131 template<typename T, typename Op> vector<T>
00132 binary (vector<T> v, vector<T> w) {
00133   int i, n= N(v);
00134   ASSERT (N(w) == n, "vector lengths don't match");
00135   T* a= A(v);
00136   T* b= A(w);
00137   T* r= tm_new_array<T> (n);
00138   for (i=0; i<n; i++)
00139     r[i]= Op::eval (a[i], b[i]);
00140   return vector<T> (r, n);
00141 }
00142 
00143 /******************************************************************************
00144 * Standard operations on vectors
00145 ******************************************************************************/
00146 
00147 TMPL inline vector<T>
00148 operator - (vector<T> v) {
00149   return unary<T,neg_op> (v); }
00150 
00151 TMPL inline vector<T>
00152 operator + (vector<T> v, vector<T> w) {
00153   return binary<T,add_op> (v, w); }
00154 
00155 TMPL inline vector<T>
00156 operator - (vector<T> v, vector<T> w) {
00157   return binary<T,sub_op> (v, w); }
00158 
00159 TMPL inline vector<T>
00160 operator * (vector<T> v, vector<T> w) {
00161   return binary<T,mul_op> (v, w); }
00162 
00163 TMPL inline vector<T>
00164 operator / (vector<T> v, vector<T> w) {
00165   return binary<T,div_op> (v, w); }
00166 
00167 TMPL inline vector<T>
00168 sqrt (vector<T> v) {
00169   return unary<T,sqrt_op> (v); }
00170 
00171 TMPL inline vector<T>
00172 exp (vector<T> v) {
00173   return unary<T,exp_op> (v); }
00174 
00175 TMPL inline vector<T>
00176 log (vector<T> v) {
00177   return unary<T,log_op> (v); }
00178 
00179 TMPL inline vector<T>
00180 pow (vector<T> v, vector<T> w) {
00181   return binary<T,pow_op> (v, w); }
00182 
00183 TMPL inline vector<T>
00184 cos (vector<T> v) {
00185   return unary<T,cos_op> (v); }
00186 
00187 TMPL inline vector<T>
00188 sin (vector<T> v) {
00189   return unary<T,sin_op> (v); }
00190 
00191 TMPL inline vector<T>
00192 tan (vector<T> v) {
00193   return unary<T,tan_op> (v); }
00194 
00195 /******************************************************************************
00196 * Casted operations on vectors
00197 ******************************************************************************/
00198 
00199 TMPL inline vector<T>
00200 operator + (T c, vector<T> v) {
00201   return vector<T> (c, N(v)) + v; }
00202 
00203 TMPL inline vector<T>
00204 operator + (vector<T> v, T c) {
00205   return v + vector<T> (c, N(v)); }
00206 
00207 TMPL inline vector<T>
00208 operator - (T c, vector<T> v) {
00209   return vector<T> (c, N(v)) - v; }
00210 
00211 TMPL inline vector<T>
00212 operator - (vector<T> v, T c) {
00213   return v - vector<T> (c, N(v)); }
00214 
00215 TMPL inline vector<T>
00216 operator * (T c, vector<T> v) {
00217   return vector<T> (c, N(v)) * v; }
00218 
00219 TMPL inline vector<T>
00220 operator * (vector<T> v, T c) {
00221   return v * vector<T> (c, N(v)); }
00222 
00223 TMPL inline vector<T>
00224 operator / (T c, vector<T> v) {
00225   return vector<T> (c, N(v)) / v; }
00226 
00227 TMPL inline vector<T>
00228 operator / (vector<T> v, T c) {
00229   return v / vector<T> (c, N(v)); }
00230 
00231 /******************************************************************************
00232 * Other operations on vectors
00233 ******************************************************************************/
00234 
00235 TMPL R
00236 square_norm (vector<T> v) {
00237   R r= 0;
00238   int i, n= N(v);
00239   T* a= A(v);
00240   for (i=0; i<n; i++)
00241     r += square_norm (a[i]);
00242   return r;
00243 }
00244 
00245 TMPL inline R
00246 norm (vector<T> v) {
00247   return sqrt (square_norm (v));
00248 }
00249 
00250 TMPL inline vector<T>
00251 derive (vector<T> v) {
00252   return unary<T,derive_op> (v); }
00253 
00254 #undef TMPL
00255 #undef BINARY_TMPL
00256 #undef R
00257 #undef M
00258 #endif // defined VECTOR_H