dynamic.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  *-----------------------------------------------------------------------*/
00013 #ifndef dynamic_vector_h
00014 #define dynamic_vector_h
00015 
00016 #include <cml/core/dynamic_1D.h>
00017 #include <cml/vector/vector_expr.h>
00018 #include <cml/vector/class_ops.h>
00019 #include <cml/vector/vector_unroller.h>
00020 
00021 namespace cml {
00022 
00024 template<typename Element, typename Alloc>
00025 class vector< Element, dynamic<Alloc> >
00026 : public dynamic_1D<Element,Alloc>
00027 {
00028   public:
00029 
00030     /* Shorthand for the generator: */
00031     typedef dynamic<> storage_type;
00032     typedef dynamic<Alloc> generator_type;
00033 
00034     /* Shorthand for the array type: */
00035     typedef dynamic_1D<Element,Alloc> array_type;
00036 
00037     /* Shorthand for the type of this vector: */
00038     typedef vector<Element,generator_type> vector_type;
00039 
00040     /* The vector coordinate type: */
00041     typedef Element coordinate_type;
00042 
00043     /* For integration into the expression template code: */
00044     typedef vector_type expr_type;
00045 
00046     /* For integration into the expression template code: */
00047     typedef vector_type temporary_type;
00048 
00049     /* Standard: */
00050     typedef typename array_type::value_type value_type;
00051     typedef typename array_type::reference reference;
00052     typedef typename array_type::const_reference const_reference;
00053 
00054     /* For integration into the expression templates code: */
00055     typedef vector_type& expr_reference;
00056     typedef const vector_type& expr_const_reference;
00057 
00058     /* For matching by storage type: */
00059     typedef typename array_type::memory_tag memory_tag;
00060 
00061     /* For matching by size type: */
00062     typedef typename array_type::size_tag size_tag;
00063 
00064     /* For matching by resizability: */
00065     typedef typename array_type::resizing_tag resizing_tag;
00066 
00067     /* For matching by result-type: */
00068     typedef cml::et::vector_result_tag result_tag;
00069 
00070     /* For matching by assignability: */
00071     typedef cml::et::assignable_tag assignable_tag;
00072 
00073 
00074   public:
00075 
00077     value_type length_squared() const {
00078         return cml::dot(*this,*this);
00079     }
00080 
00082     value_type length() const {
00083         return std::sqrt(length_squared());
00084     }
00085 
00087     vector_type& normalize() {
00088         return (*this /= length());
00089     }
00090 
00092     vector_type& zero() {
00093         typedef cml::et::OpAssign<Element,Element> OpT;
00094         cml::et::UnrollAssignment<OpT>(*this,Element(0));
00095         return *this;
00096     }
00097 
00099     vector_type& cardinal(size_t i) {
00100         zero();
00101         (*this)[i] = Element(1);
00102         return *this;
00103     }
00104 
00106     template<typename E, class AT>
00107     void minimize(const vector<E,AT>& v) {
00108       /* XXX This should probably use ScalarPromote: */
00109       for (size_t i = 0; i < this->size(); ++i) {
00110         (*this)[i] = std::min((*this)[i],v[i]);
00111       }
00112     }
00113 
00115     template<typename E, class AT>
00116     void maximize(const vector<E,AT>& v) {
00117       /* XXX This should probably use ScalarPromote: */
00118       for (size_t i = 0; i < this->size(); ++i) {
00119         (*this)[i] = std::max((*this)[i],v[i]);
00120       }
00121     }
00122 
00124     void random(value_type min, value_type max) {
00125         for (size_t i = 0; i < this->size(); ++i) {
00126             (*this)[i] = cml::random_real(min,max);
00127         }
00128     }
00129 
00130 
00131   public:
00132 
00134     vector() : array_type() {}
00135 
00137     vector(size_t N) : array_type(N) {}
00138 
00139 
00140   public:
00141 
00142     /* Define common class operators: */
00143 
00144     CML_CONSTRUCT_VEC_2(: array_type())
00145     CML_CONSTRUCT_VEC_3(: array_type())
00146     CML_CONSTRUCT_VEC_4(: array_type())
00147 
00148     CML_VEC_COPY_FROM_ARRAY(: array_type())
00149     CML_VEC_COPY_FROM_VECTYPE(: array_type())
00150     CML_VEC_COPY_FROM_VEC
00151     CML_VEC_COPY_FROM_VECXPR
00152 
00153     CML_ASSIGN_VEC_2
00154     CML_ASSIGN_VEC_3
00155     CML_ASSIGN_VEC_4
00156 
00157     CML_VEC_ASSIGN_FROM_VECTYPE
00158 
00159     CML_VEC_ASSIGN_FROM_VEC(=, cml::et::OpAssign)
00160     CML_VEC_ASSIGN_FROM_VEC(+=, cml::et::OpAddAssign)
00161     CML_VEC_ASSIGN_FROM_VEC(-=, cml::et::OpSubAssign)
00162 
00163     CML_VEC_ASSIGN_FROM_VECXPR(=, cml::et::OpAssign)
00164     CML_VEC_ASSIGN_FROM_VECXPR(+=, cml::et::OpAddAssign)
00165     CML_VEC_ASSIGN_FROM_VECXPR(-=, cml::et::OpSubAssign)
00166 
00167     CML_VEC_ASSIGN_FROM_SCALAR(*=, cml::et::OpMulAssign)
00168     CML_VEC_ASSIGN_FROM_SCALAR(/=, cml::et::OpDivAssign)
00169 };
00170 
00171 } // namespace cml
00172 
00173 #endif
00174 
00175 // -------------------------------------------------------------------------
00176 // vim:ft=cpp

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