external.h

Go to the documentation of this file.
00001 /* -*- C++ -*- ------------------------------------------------------------
00002  
00003 Copyright (c) 2007 Jesse Anders and Demian Nave http://cmldev.net/
00004 
00005 The Configurable Math Library (CML) is distributed under the terms of the
00006 Boost Software License, v1.0 (see cml/LICENSE for details).
00007 
00008  *-----------------------------------------------------------------------*/
00017 #ifndef external_vector_h
00018 #define external_vector_h
00019 
00020 #include <cml/core/external_1D.h>
00021 #include <cml/vector/vector_expr.h>
00022 #include <cml/vector/class_ops.h>
00023 #include <cml/vector/vector_unroller.h>
00024 #include <cml/vector/dynamic.h>
00025 
00026 namespace cml {
00027 
00029 template<typename Element, int Size>
00030 class vector< Element, external<Size> >
00031 : public external_1D<Element,Size>
00032 {
00033   public:
00034 
00035     /* Shorthand for the generator: */
00036     typedef external<> storage_type;
00037     typedef external<Size> generator_type;
00038 
00039     /* Shorthand for the array type: */
00040     typedef external_1D<Element,Size> array_type;
00041 
00042     /* Shorthand for the type of this vector: */
00043     typedef vector<Element,generator_type> vector_type;
00044 
00045     /* The vector coordinate type: */
00046     typedef Element coordinate_type;
00047 
00048     /* For integration into the expression template code: */
00049     typedef vector_type expr_type;
00050 
00051     /* For integration into the expression template code: */
00052     typedef vector< Element,fixed<Size> > temporary_type;
00053     typedef typename temporary_type::subvector_type subvector_type;
00054     /* Note: this ensures that an external vector is copied into the proper
00055      * temporary; external<> temporaries are not allowed.
00056      */
00057 
00058     /* Standard: */
00059     typedef typename array_type::value_type value_type;
00060     typedef typename array_type::reference reference;
00061     typedef typename array_type::const_reference const_reference;
00062 
00063     /* For integration into the expression templates code: */
00064     typedef vector_type& expr_reference;
00065     typedef const vector_type& expr_const_reference;
00066 
00067     /* For matching by storage type: */
00068     typedef typename array_type::memory_tag memory_tag;
00069 
00070     /* For matching by size type: */
00071     typedef typename array_type::size_tag size_tag;
00072 
00073     /* For matching by result-type: */
00074     typedef cml::et::vector_result_tag result_tag;
00075 
00076     /* For matching by assignability: */
00077     typedef cml::et::assignable_tag assignable_tag;
00078 
00079 
00080   public:
00081 
00083     enum { dimension = Size };
00084 
00085 
00086   public:
00087 
00089     value_type length_squared() const {
00090         return cml::dot(*this,*this);
00091     }
00092 
00094     value_type length() const {
00095         return std::sqrt(length_squared());
00096     }
00097 
00099     vector_type& normalize() {
00100         return (*this /= length());
00101     }
00102 
00104     vector_type& zero() {
00105         typedef cml::et::OpAssign<Element,Element> OpT;
00106         cml::et::UnrollAssignment<OpT>(*this,Element(0));
00107         return *this;
00108     }
00109 
00111     vector_type& cardinal(size_t i) {
00112         zero();
00113         (*this)[i] = Element(1);
00114         return *this;
00115     }
00116 
00118     template<typename E, class AT>
00119     void minimize(const vector<E,AT>& v) {
00120       /* XXX This should probably use ScalarPromote: */
00121       for (size_t i = 0; i < this->size(); ++i) {
00122         (*this)[i] = std::min((*this)[i],v[i]);
00123       }
00124     }
00125 
00127     template<typename E, class AT>
00128     void maximize(const vector<E,AT>& v) {
00129       /* XXX This should probably use ScalarPromote: */
00130       for (size_t i = 0; i < this->size(); ++i) {
00131         (*this)[i] = std::max((*this)[i],v[i]);
00132       }
00133     }
00134 
00136     void random(value_type min, value_type max) {
00137         for (size_t i = 0; i < this->size(); ++i) {
00138             (*this)[i] = cml::random_real(min,max);
00139         }
00140     }
00141 
00142 
00143   public:
00144 
00146     vector(Element* const array) : array_type(array) {}
00147 
00148 
00149   public:
00150 
00151     CML_ASSIGN_VEC_2
00152     CML_ASSIGN_VEC_3
00153     CML_ASSIGN_VEC_4
00154 
00155     CML_VEC_ASSIGN_FROM_VECTYPE
00156 
00157     /* Only assignment operators can be used to copy from other types: */
00158     CML_VEC_ASSIGN_FROM_VEC(=, cml::et::OpAssign)
00159     CML_VEC_ASSIGN_FROM_VEC(+=, cml::et::OpAddAssign)
00160     CML_VEC_ASSIGN_FROM_VEC(-=, cml::et::OpSubAssign)
00161 
00162     CML_VEC_ASSIGN_FROM_VECXPR(=, cml::et::OpAssign)
00163     CML_VEC_ASSIGN_FROM_VECXPR(+=, cml::et::OpAddAssign)
00164     CML_VEC_ASSIGN_FROM_VECXPR(-=, cml::et::OpSubAssign)
00165 
00166     CML_VEC_ASSIGN_FROM_SCALAR(*=, cml::et::OpMulAssign)
00167     CML_VEC_ASSIGN_FROM_SCALAR(/=, cml::et::OpDivAssign)
00168 };
00169 
00171 template<typename Element>
00172 class vector< Element, external<> >
00173 : public external_1D<Element>
00174 {
00175   public:
00176 
00177     /* Shorthand for the generator: */
00178     typedef external<> storage_type;
00179     typedef external<> generator_type;
00180 
00181     /* Shorthand for the array type: */
00182     typedef external_1D<Element> array_type;
00183 
00184     /* Shorthand for the type of this vector: */
00185     typedef vector<Element,generator_type> vector_type;
00186 
00187     /* For integration into the expression template code: */
00188     typedef vector_type expr_type;
00189 
00190     /* For integration into the expression template code: */
00191     typedef vector< Element, dynamic<> > temporary_type;
00192     /* Note: this ensures that an external vector is copied into the proper
00193      * temporary; external<> temporaries are not allowed.
00194      */
00195 
00196     /* Standard: */
00197     typedef typename array_type::value_type value_type;
00198     typedef typename array_type::reference reference;
00199     typedef typename array_type::const_reference const_reference;
00200 
00201     /* For integration into the expression templates code: */
00202     typedef vector_type& expr_reference;
00203     typedef const vector_type& expr_const_reference;
00204 
00205     /* For matching by storage type: */
00206     typedef typename array_type::memory_tag memory_tag;
00207 
00208     /* For matching by size type: */
00209     typedef typename array_type::size_tag size_tag;
00210 
00211     /* For matching by resizability: */
00212     typedef typename array_type::resizing_tag resizing_tag;
00213 
00214     /* For matching by result-type: */
00215     typedef cml::et::vector_result_tag result_tag;
00216 
00217     /* For matching by assignability: */
00218     typedef cml::et::assignable_tag assignable_tag;
00219 
00220 
00221   public:
00222 
00224     value_type length_squared() const {
00225         return dot(*this,*this);
00226     }
00227 
00229     value_type length() const {
00230         return std::sqrt(length_squared());
00231     }
00232 
00234     vector_type& normalize() {
00235         return (*this /= length());
00236     }
00237 
00239     vector_type& zero() {
00240         typedef cml::et::OpAssign<Element,Element> OpT;
00241         cml::et::UnrollAssignment<OpT>(*this,Element(0));
00242         return *this;
00243     }
00244 
00246     vector_type& cardinal(size_t i) {
00247         zero();
00248         (*this)[i] = Element(1);
00249         return *this;
00250     }
00251 
00253     template<typename E, class AT>
00254     void minimize(const vector<E,AT>& v) {
00255       /* XXX This should probably use ScalarPromote: */
00256       for (size_t i = 0; i < this->size(); ++i) {
00257         (*this)[i] = std::min((*this)[i],v[i]);
00258       }
00259     }
00260 
00262     template<typename E, class AT>
00263     void maximize(const vector<E,AT>& v) {
00264       /* XXX This should probably use ScalarPromote: */
00265       for (size_t i = 0; i < this->size(); ++i) {
00266         (*this)[i] = std::max((*this)[i],v[i]);
00267       }
00268     }
00269 
00271     void random(value_type min, value_type max) {
00272         for (size_t i = 0; i < this->size(); ++i) {
00273             (*this)[i] = random_real(min,max);
00274         }
00275     }
00276 
00277 
00278   public:
00279 
00281     vector(Element* const array, size_t size)
00282         : array_type(array, size) {}
00283 
00284 
00285   public:
00286 
00287     /* Define class operators for external vectors. Note: external vectors
00288      * cannot be copy-constructed, but they can be assigned to:
00289      */
00290     CML_ASSIGN_VEC_2
00291     CML_ASSIGN_VEC_3
00292     CML_ASSIGN_VEC_4
00293 
00294     CML_VEC_ASSIGN_FROM_VECTYPE
00295 
00296     /* Only assignment operators can be used to copy from other types: */
00297     CML_VEC_ASSIGN_FROM_VEC(=, cml::et::OpAssign)
00298     CML_VEC_ASSIGN_FROM_VEC(+=, cml::et::OpAddAssign)
00299     CML_VEC_ASSIGN_FROM_VEC(-=, cml::et::OpSubAssign)
00300 
00301     CML_VEC_ASSIGN_FROM_VECXPR(=, cml::et::OpAssign)
00302     CML_VEC_ASSIGN_FROM_VECXPR(+=, cml::et::OpAddAssign)
00303     CML_VEC_ASSIGN_FROM_VECXPR(-=, cml::et::OpSubAssign)
00304 
00305     CML_VEC_ASSIGN_FROM_SCALAR(*=, cml::et::OpMulAssign)
00306     CML_VEC_ASSIGN_FROM_SCALAR(/=, cml::et::OpDivAssign)
00307 };
00308 
00309 } // namespace cml
00310 
00311 #endif
00312 
00313 // -------------------------------------------------------------------------
00314 // vim:ft=cpp

Generated on Sat Jul 18 19:35:21 2009 for CML 1.0 by  doxygen 1.5.9