fixed.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 fixed_vector_h
00014 #define fixed_vector_h
00015 
00016 #include <cml/core/fixed_1D.h>
00017 #include <cml/vector/vector_expr.h>
00018 #include <cml/vector/class_ops.h>
00019 #include <cml/vector/vector_unroller.h>
00020 #include <cml/vector/external.h>
00021 #include <cml/util.h>
00022 
00023 namespace cml {
00024 
00026 template<typename Element, int Size>
00027 class vector< Element, fixed<Size> >
00028 : public fixed_1D<Element,Size>
00029 {
00030   public:
00031 
00032     /* Shorthand for the generator: */
00033     typedef fixed<> storage_type;
00034     typedef fixed<Size> generator_type;
00035 
00036     /* Shorthand for the array type: */
00037     typedef fixed_1D<Element,Size> array_type;
00038 
00039     /* Shorthand for the type of this vector: */
00040     typedef vector<Element,generator_type> vector_type;
00041 
00042     /* The vector coordinate type: */
00043     typedef Element coordinate_type;
00044 
00045     /* For integration into the expression template code: */
00046     typedef vector_type expr_type;
00047 
00048     /* For integration into the expression template code: */
00049     typedef vector_type temporary_type;
00050     typedef vector< Element, fixed<Size-1> > subvector_type;
00051 
00052     /* Standard: */
00053     typedef typename array_type::value_type value_type;
00054     typedef typename array_type::reference reference;
00055     typedef typename array_type::const_reference const_reference;
00056 
00057     /* For integration into the expression templates code: */
00058     typedef vector_type& expr_reference;
00059     typedef const vector_type& expr_const_reference;
00060 
00061     /* For matching by storage type: */
00062     typedef typename array_type::memory_tag memory_tag;
00063 
00064     /* For matching by size type: */
00065     typedef typename array_type::size_tag size_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     enum { dimension = Size };
00078 
00079 
00080   public:
00081 
00083     value_type length_squared() const {
00084         return cml::dot(*this,*this);
00085     }
00086 
00088     value_type length() const {
00089         return std::sqrt(length_squared());
00090     }
00091 
00093     vector_type& normalize() {
00094         return (*this /= length());
00095     }
00096 
00098     vector_type& zero() {
00099         typedef cml::et::OpAssign<Element,Element> OpT;
00100         cml::et::UnrollAssignment<OpT>(*this,Element(0));
00101         return *this;
00102     }
00103 
00105     vector_type& cardinal(size_t i) {
00106         zero();
00107         (*this)[i] = Element(1);
00108         return *this;
00109     }
00110 
00112     template<typename E, class AT>
00113     void minimize(const vector<E,AT>& v) {
00114       /* XXX This should probably use ScalarPromote: */
00115       for (size_t i = 0; i < this->size(); ++i) {
00116         (*this)[i] = std::min((*this)[i],v[i]);
00117       }
00118     }
00119 
00121     template<typename E, class AT>
00122     void maximize(const vector<E,AT>& v) {
00123       /* XXX This should probably use ScalarPromote: */
00124       for (size_t i = 0; i < this->size(); ++i) {
00125         (*this)[i] = std::max((*this)[i],v[i]);
00126       }
00127     }
00128 
00130     void random(value_type min, value_type max) {
00131         for (size_t i = 0; i < this->size(); ++i) {
00132             (*this)[i] = cml::random_real(min,max);
00133         }
00134     }
00135 
00140     subvector_type subvector(size_t i) const {
00141         subvector_type s;
00142         for(size_t m = 0, n = 0; m < this->size(); ++ m)
00143             if(m != i) s[n++] = (*this)[m];
00144         return s;
00145     };
00146 
00147 
00148   public:
00149 
00150     vector() : array_type() {}
00151 
00152 
00153   public:
00154 
00155     /* Define common class operators: */
00156 
00157     CML_CONSTRUCT_VEC_2()
00158     CML_CONSTRUCT_VEC_3()
00159     CML_CONSTRUCT_VEC_4()
00160 
00161     CML_CONSTRUCT_FROM_SUBVEC()
00162 
00163     CML_VEC_COPY_FROM_FIXED_ARRAY(array_type::array_size,)
00164     CML_VEC_COPY_FROM_VECTYPE(: array_type())
00165     CML_VEC_COPY_FROM_VEC
00166     CML_VEC_COPY_FROM_VECXPR
00167 
00168     CML_ASSIGN_VEC_2
00169     CML_ASSIGN_VEC_3
00170     CML_ASSIGN_VEC_4
00171 
00172     CML_VEC_ASSIGN_FROM_VECTYPE
00173 
00174     CML_VEC_ASSIGN_FROM_VEC(=, cml::et::OpAssign)
00175     CML_VEC_ASSIGN_FROM_VEC(+=, cml::et::OpAddAssign)
00176     CML_VEC_ASSIGN_FROM_VEC(-=, cml::et::OpSubAssign)
00177 
00178     CML_VEC_ASSIGN_FROM_VECXPR(=, cml::et::OpAssign)
00179     CML_VEC_ASSIGN_FROM_VECXPR(+=, cml::et::OpAddAssign)
00180     CML_VEC_ASSIGN_FROM_VECXPR(-=, cml::et::OpSubAssign)
00181 
00182     CML_VEC_ASSIGN_FROM_SCALAR(*=, cml::et::OpMulAssign)
00183     CML_VEC_ASSIGN_FROM_SCALAR(/=, cml::et::OpDivAssign)
00184 };
00185 
00186 } // namespace cml
00187 
00188 #endif
00189 
00190 // -------------------------------------------------------------------------
00191 // vim:ft=cpp

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