1 /* -*- C++ -*- ------------------------------------------------------------
3 Copyright (c) 2007 Jesse Anders and Demian Nave http://cmldev.net/
5 The Configurable Math Library (CML) is distributed under the terms of the
6 Boost Software License, v1.0 (see cml/LICENSE for details).
8 *-----------------------------------------------------------------------*/
10 * @brief Specialization for fixed-size, fixed-memory vectors.
13 #ifndef fixed_vector_h
14 #define fixed_vector_h
16 #include <cml/core/fixed_1D.h>
17 #include <cml/vector/vector_expr.h>
18 #include <cml/vector/class_ops.h>
19 #include <cml/vector/vector_unroller.h>
20 #include <cml/vector/external.h>
25 /** Fixed-size, fixed-memory vector. */
26 template<typename Element
, int Size
>
27 class vector
< Element
, fixed
<Size
> >
28 : public fixed_1D
<Element
,Size
>
32 /* Shorthand for the generator: */
33 typedef fixed
<> storage_type
;
34 typedef fixed
<Size
> generator_type
;
36 /* Shorthand for the array type: */
37 typedef fixed_1D
<Element
,Size
> array_type
;
39 /* Shorthand for the type of this vector: */
40 typedef vector
<Element
,generator_type
> vector_type
;
42 /* For integration into the expression template code: */
43 typedef vector_type expr_type
;
45 /* For integration into the expression template code: */
46 typedef vector_type temporary_type
;
47 typedef vector
< Element
, fixed
<Size
-1> > subvector_type
;
50 typedef typename
array_type::value_type value_type
;
51 typedef typename
array_type::reference reference
;
52 typedef typename
array_type::const_reference const_reference
;
54 /* For integration into the expression templates code: */
55 typedef vector_type
& expr_reference
;
56 typedef const vector_type
& expr_const_reference
;
58 /* For matching by storage type: */
59 typedef typename
array_type::memory_tag memory_tag
;
61 /* For matching by size type: */
62 typedef typename
array_type::size_tag size_tag
;
64 /* For matching by result-type: */
65 typedef cml::et::vector_result_tag result_tag
;
67 /* For matching by assignability: */
68 typedef cml::et::assignable_tag assignable_tag
;
73 /** Return square of the length. */
74 value_type
length_squared() const {
75 return cml::dot(*this,*this);
78 /** Return the length. */
79 value_type
length() const {
80 return std::sqrt(length_squared());
83 /** Normalize the vector. */
84 vector_type
& normalize() {
85 return (*this /= length());
88 /** Set this vector to [0]. */
90 typedef cml::et::OpAssign
<Element
,Element
> OpT
;
91 cml::et::UnrollAssignment
<OpT
>(*this,Element(0));
95 /** Set this vector to a cardinal vector. */
96 vector_type
& cardinal(size_t i
) {
98 (*this)[i
] = Element(1);
102 /** Pairwise minimum of this vector with another. */
103 template<typename E
, class AT
>
104 void minimize(const vector
<E
,AT
>& v
) {
105 /* XXX This should probably use ScalarPromote: */
106 for (size_t i
= 0; i
< this->size(); ++i
) {
107 (*this)[i
] = std::min((*this)[i
],v
[i
]);
111 /** Pairwise maximum of this vector with another. */
112 template<typename E
, class AT
>
113 void maximize(const vector
<E
,AT
>& v
) {
114 /* XXX This should probably use ScalarPromote: */
115 for (size_t i
= 0; i
< this->size(); ++i
) {
116 (*this)[i
] = std::max((*this)[i
],v
[i
]);
120 /** Fill vector with random elements. */
121 void random(value_type min
, value_type max
) {
122 for (size_t i
= 0; i
< this->size(); ++i
) {
123 (*this)[i
] = cml::random_real(min
,max
);
130 vector() : array_type() {}
135 /* Define common class operators: */
137 CML_CONSTRUCT_VEC_2(/**/)
138 CML_CONSTRUCT_VEC_3(/**/)
139 CML_CONSTRUCT_VEC_4(/**/)
141 CML_CONSTRUCT_FROM_SUBVEC(/**/)
143 CML_VEC_COPY_FROM_FIXED_ARRAY(array_type::array_size
,/**/)
144 CML_VEC_COPY_FROM_VECTYPE(: array_type())
145 CML_VEC_COPY_FROM_VEC
146 CML_VEC_COPY_FROM_VECXPR
152 CML_VEC_ASSIGN_FROM_VECTYPE
154 CML_VEC_ASSIGN_FROM_VEC(=, cml::et::OpAssign
)
155 CML_VEC_ASSIGN_FROM_VEC(+=, cml::et::OpAddAssign
)
156 CML_VEC_ASSIGN_FROM_VEC(-=, cml::et::OpSubAssign
)
158 CML_VEC_ASSIGN_FROM_VECXPR(=, cml::et::OpAssign
)
159 CML_VEC_ASSIGN_FROM_VECXPR(+=, cml::et::OpAddAssign
)
160 CML_VEC_ASSIGN_FROM_VECXPR(-=, cml::et::OpSubAssign
)
162 CML_VEC_ASSIGN_FROM_SCALAR(*=, cml::et::OpMulAssign
)
163 CML_VEC_ASSIGN_FROM_SCALAR(/=, cml::et::OpDivAssign
)
170 // -------------------------------------------------------------------------