33 #ifndef __VMML__VECTOR__HPP__
34 #define __VMML__VECTOR__HPP__
36 #include <vmmlib/enable_if.hpp>
51 template<
size_t M,
typename T >
class vector
56 typedef const T* const_iterator;
57 typedef std::reverse_iterator< iterator > reverse_iterator;
58 typedef std::reverse_iterator< const_iterator > const_reverse_iterator;
60 static const size_t DIMENSION = M;
64 explicit vector(
const T& a );
65 vector(
const T& x,
const T& y );
66 vector(
const T& x,
const T& y,
const T& z );
67 vector(
const T& x,
const T& y,
const T& z,
const T& w );
71 vector(
const vector< M-1, T >& vector_, T last_ );
74 explicit vector(
const T* values );
77 template<
typename OSGVEC3 >
78 explicit vector(
const OSGVEC3& from,
79 typename enable_if< M == 3, OSGVEC3 >::type* = 0 );
85 vector(
const vector< N, T >& source_,
86 typename enable_if< N == M - 1 >::type* = 0 );
90 vector(
const vector< N, T >& source_,
91 typename enable_if< N == M + 1 >::type* = 0 );
93 template<
typename U > vector(
const vector< M, U >& source_ );
96 inline iterator begin();
97 inline iterator end();
98 inline const_iterator begin()
const;
99 inline const_iterator end()
const;
100 inline reverse_iterator rbegin();
101 inline reverse_iterator rend();
102 inline const_reverse_iterator rbegin()
const;
103 inline const_reverse_iterator rend()
const;
105 # ifndef VMMLIB_NO_CONVERSION_OPERATORS
107 inline operator T*();
108 inline operator const T*()
const;
110 inline T& operator[](
size_t index );
111 inline const T& operator[](
size_t index )
const;
115 inline T& operator()(
size_t index );
116 inline const T& operator()(
size_t index )
const;
118 inline T& at(
size_t index );
119 inline const T& at(
size_t index )
const;
126 inline const T& x()
const;
127 inline const T& y()
const;
128 inline const T& z()
const;
129 inline const T& w()
const;
136 inline const T& r()
const;
137 inline const T& g()
const;
138 inline const T& b()
const;
139 inline const T& a()
const;
141 bool operator==(
const vector& other )
const;
142 bool operator!=(
const vector& other )
const;
143 bool equals(
const vector& other,
144 T tolerance = std::numeric_limits< T >::epsilon( ))
const;
145 bool operator<(
const vector& other )
const;
148 vector& operator=(
const T* c_array );
149 T operator=( T filler );
151 vector& operator=(
const vector& other );
154 template<
typename U >
void operator=(
const vector< M, U >& other );
159 typename enable_if< N == M - 1 >::type*
160 operator=(
const vector< N, T >& source_ );
165 typename enable_if< N == M + 1 >::type*
166 operator=(
const vector< N, T >& source_ );
168 vector operator*(
const vector& other )
const;
169 vector operator/(
const vector& other )
const;
170 vector operator+(
const vector& other )
const;
171 vector operator-(
const vector& other )
const;
173 void operator*=(
const vector& other );
174 void operator/=(
const vector& other );
175 void operator+=(
const vector& other );
176 void operator-=(
const vector& other );
178 vector operator*(
const T other )
const;
179 vector operator/(
const T other )
const;
180 vector operator+(
const T other )
const;
181 vector operator-(
const T other )
const;
183 void operator*=(
const T other );
184 void operator/=(
const T other );
185 void operator+=(
const T other );
186 void operator-=(
const T other );
188 vector operator-()
const;
190 const vector& negate();
194 void set(
const vector< M-1, T >& v, T a );
197 void set(
const vector< N, T >& v );
200 void set( T x, T y );
201 void set( T x, T y, T z );
202 void set( T x, T y, T z, T w );
204 template<
typename input_iterator_t >
205 void iter_set( input_iterator_t begin_, input_iterator_t end_ );
210 template<
typename TT >
211 vector< M, T >& cross(
const vector< M, TT >& b,
212 typename enable_if< M == 3, TT >::type* = 0 );
217 inline T dot(
const vector& other )
const;
222 inline T normalize();
228 void set_random(
int seed = -1 );
230 inline T length()
const;
231 inline T squared_length()
const;
233 inline T distance(
const vector& other )
const;
234 inline T squared_distance(
const vector& other )
const;
239 template<
typename TT >
241 typename enable_if< M == 3, TT >::type* = 0 );
244 template<
size_t N,
size_t O >
245 vector< N, T >
get_sub_vector(
typename enable_if< M >= N+O >::type* = 0 )
249 template<
size_t N,
size_t O >
251 typename enable_if< M >= N+O >::type* = 0 );
254 template<
typename TT >
256 const vector< 3, TT >& point,
257 typename enable_if< M == 4, TT >::type* = 0 )
const;
260 template<
typename TT >
261 inline T distance_to_sphere(
const vector< 3, TT >& point,
262 typename enable_if< M == 4, TT >::type* = 0 )
const;
265 template<
typename TT >
266 inline T distance_to_plane(
const vector< 3, TT >& point,
267 typename enable_if< M == 4, TT >::type* = 0 )
const;
269 template<
typename TT >
271 const vector< 3, TT >& point,
272 typename enable_if< M == 4, TT >::type* = 0 )
const;
275 size_t find_min_index()
const;
276 size_t find_max_index()
const;
281 const T& find_min()
const;
282 const T& find_max()
const;
284 void clamp(
const T& min = 0.0,
const T& max = 1.0 );
286 inline static size_t size();
288 bool is_unit_vector()
const;
291 void perturb( T perturbation = 0.0001 );
293 void sqrt_elementwise();
301 void reciprocal_safe();
303 template<
typename TT >
304 void cast_from(
const vector< M, TT >& other );
311 friend std::ostream& operator<< ( std::ostream& os,
const vector& vector_ )
313 const std::ios::fmtflags flags = os.flags();
314 const int prec = os.precision();
316 os.setf( std::ios::right, std::ios::adjustfield );
319 for(
size_t index = 0; index < M; ++index )
320 os << std::setw(10) << vector_.at( index ) <<
" ";
322 os.precision( prec );
331 static const vector FORWARD;
332 static const vector BACKWARD;
336 static const vector RIGHT;
342 static const vector UNIT_X;
343 static const vector UNIT_Y;
344 static const vector UNIT_Z;
353 template<
size_t M,
typename T >
355 template<
size_t M,
typename T >
357 template<
size_t M,
typename T >
359 template<
size_t M,
typename T >
361 template<
size_t M,
typename T >
363 template<
size_t M,
typename T >
365 template<
size_t M,
typename T >
368 template<
size_t M,
typename T >
370 template<
size_t M,
typename T >
373 template<
size_t M,
typename T >
375 template<
size_t M,
typename T >
383 template<
size_t M,
typename T >
386 return a.equals( b );
390 template<
size_t M,
typename T >
391 static vector< M, T > operator* ( T factor,
const vector< M, T >& vector_ )
393 return vector_ * factor;
396 template<
size_t M,
typename T >
397 inline T dot(
const vector< M, T >& first,
const vector< M, T >& second )
399 return first.dot( second );
402 template<
size_t M,
typename T >
403 inline vector< M, T > cross( vector< M, T > a,
const vector< M, T >& b )
408 template<
size_t M,
typename T >
409 vector< M, T > compute_normal(
const vector< M, T >& a,
const vector< M, T >& b,
410 const vector< M, T >& c )
413 const vector< M, T > u = b - a;
414 const vector< M, T > v = c - a;
415 vector< M, T > w = cross( u, v );
420 template<
typename T >
424 return vec.rotate( theta, axis );
428 template<
size_t M,
typename T >
429 inline vector< M, T > normalize( vector< M, T > vector_ )
435 template<
typename T >
445 plane.w() = -plane.x() * a.x() - plane.y() * a.y() - plane.z() * a.z();
449 template<
size_t M,
typename T >
450 vector< M, T >::vector(
const T& _a )
452 for( iterator it = begin(), it_end = end(); it != it_end; ++it )
458 template<
size_t M,
typename T >
459 vector< M, T >::vector(
const T& _x,
const T& _y )
465 template<
size_t M,
typename T >
466 vector< M, T >::vector(
const T& _x,
const T& _y,
const T& _z )
473 template<
size_t M,
typename T >
474 vector< M, T >::vector(
const T& _x,
const T& _y,
const T& _z,
const T& _w )
482 template<
size_t M,
typename T >
483 vector< M, T >::vector(
const T* values )
485 memcpy( array, values, M *
sizeof( T ));
489 template<
size_t M,
typename T >
490 template<
typename OSGVEC3 >
491 vector< M, T >::vector(
const OSGVEC3& from,
492 typename enable_if< M == 3, OSGVEC3 >::type* )
494 array[ 0 ] = from.x();
495 array[ 1 ] = from.y();
496 array[ 2 ] = from.z();
501 template<
size_t M,
typename T >
503 vector< M, T >::vector(
const vector< M-1, T >& vector_, T last_ )
505 typename vector< M-1, T >::const_iterator
506 it = vector_.begin(), it_end = vector_.end();
508 iterator my_it = begin();
510 for( ; it != it_end; ++it, ++my_it )
519 template<
size_t M,
typename T >
521 vector< M, T >::vector(
const vector< N, T >& source_,
522 typename enable_if< N == M - 1 >::type* )
528 template<
size_t M,
typename T >
530 vector< M, T >::vector(
const vector< N, T >& source_,
531 typename enable_if< N == M + 1 >::type* )
536 template<
size_t M,
typename T >
537 template<
typename U >
538 vector< M, T >::vector(
const vector< M, U >& source_ )
543 template<
size_t M,
typename T >
void vector< M, T >::set( T _a )
545 for( iterator it = begin(), it_end = end(); it != it_end; ++it )
550 template<
size_t M,
typename T >
551 void vector< M, T >::set(
const vector< M-1, T >& v, T _a )
553 memcpy( array, v.array,
sizeof( T ) * (M-1) );
558 template<
size_t M,
typename T >
template<
size_t N >
559 void vector< M, T >::set(
const vector< N, T >& v )
562 if (N < M) minimum = N;
563 memcpy( array, v.array,
sizeof( T ) * minimum );
566 template<
size_t M,
typename T >
567 void vector< M, T >::set( T _x, T _y )
573 template<
size_t M,
typename T >
574 void vector< M, T >::set( T _x, T _y, T _z )
581 template<
size_t M,
typename T >
582 void vector< M, T >::set( T _x, T _y, T _z, T _w )
590 template<
size_t M,
typename T >
592 vector< M, T >::operator()(
size_t index )
597 template<
size_t M,
typename T >
599 vector< M, T >::operator()(
size_t index )
const
604 template<
size_t M,
typename T >
606 vector< M, T >::at(
size_t index )
609 throw std::runtime_error(
"at() - index out of bounds" );
610 return array[ index ];
613 template<
size_t M,
typename T >
615 vector< M, T >::at(
size_t index )
const
618 throw std::runtime_error(
"at() - index out of bounds" );
619 return array[ index ];
622 #ifndef VMMLIB_NO_CONVERSION_OPERATORS
624 template<
size_t M,
typename T >
625 vector< M, T >::operator T*()
630 template<
size_t M,
typename T >
631 vector< M, T >::operator
const T*()
const
637 template<
size_t M,
typename T >
639 vector< M, T >::operator[](
size_t index )
644 template<
size_t M,
typename T >
646 vector< M, T >::operator[](
size_t index )
const
654 template<
size_t M,
typename T >
656 vector< M, T >::operator[](
size_t index )
661 template<
size_t M,
typename T >
663 vector< M, T >::operator[](
size_t index )
const
669 template<
size_t M,
typename T >
671 vector< M, T >::operator*(
const vector< M, T >& other )
const
673 vector< M, T > result;
674 for(
size_t index = 0; index < M; ++index )
675 result.at( index ) = at( index ) * other.at( index );
679 template<
size_t M,
typename T >
681 vector< M, T >::operator/(
const vector< M, T >& other )
const
683 vector< M, T > result;
684 for(
size_t index = 0; index < M; ++index )
685 result.at( index ) = at( index ) / other.at( index );
689 template<
size_t M,
typename T >
691 vector< M, T >::operator+(
const vector< M, T >& other )
const
693 vector< M, T > result;
694 for(
size_t index = 0; index < M; ++index )
695 result.at( index ) = at( index ) + other.at( index );
699 template<
size_t M,
typename T >
701 vector< M, T >::operator-(
const vector< M, T >& other )
const
703 vector< M, T > result;
704 for(
size_t index = 0; index < M; ++index )
705 result.at( index ) = at( index ) - other.at( index );
709 template<
size_t M,
typename T >
711 vector< M, T >::operator*=(
const vector< M, T >& other )
713 for(
size_t index = 0; index < M; ++index )
714 at( index ) *= other.at( index );
717 template<
size_t M,
typename T >
719 vector< M, T >::operator/=(
const vector< M, T >& other )
721 for(
size_t index = 0; index < M; ++index )
722 at( index ) /= other.at( index );
725 template<
size_t M,
typename T >
727 vector< M, T >::operator+=(
const vector< M, T >& other )
729 for(
size_t index = 0; index < M; ++index )
730 at( index ) += other.at( index );
733 template<
size_t M,
typename T >
735 vector< M, T >::operator-=(
const vector< M, T >& other )
737 for(
size_t index = 0; index < M; ++index )
738 at( index ) -= other.at( index );
741 template<
size_t M,
typename T >
743 vector< M, T >::operator*(
const T other )
const
745 vector< M, T > result;
746 for(
size_t index = 0; index < M; ++index )
747 result.at( index ) = at( index ) * other;
751 template<
size_t M,
typename T >
753 vector< M, T >::operator/(
const T other )
const
755 vector< M, T > result;
756 for(
size_t index = 0; index < M; ++index )
757 result.at( index ) = at( index ) / other;
761 template<
size_t M,
typename T >
763 vector< M, T >::operator+(
const T other )
const
765 vector< M, T > result;
766 for(
size_t index = 0; index < M; ++index )
767 result.at( index ) = at( index ) + other;
771 template<
size_t M,
typename T >
773 vector< M, T >::operator-(
const T other )
const
775 vector< M, T > result;
776 for(
size_t index = 0; index < M; ++index )
777 result.at( index ) = at( index ) - other;
781 template<
size_t M,
typename T >
783 vector< M, T >::operator*=(
const T other )
785 for(
size_t index = 0; index < M; ++index )
786 at( index ) *= other;
789 template<
size_t M,
typename T >
791 vector< M, T >::operator/=(
const T other )
793 for(
size_t index = 0; index < M; ++index )
794 at( index ) /= other;
797 template<
size_t M,
typename T >
799 vector< M, T >::operator+=(
const T other )
801 for(
size_t index = 0; index < M; ++index )
802 at( index ) += other;
805 template<
size_t M,
typename T >
807 vector< M, T >::operator-=(
const T other )
809 for(
size_t index = 0; index < M; ++index )
810 at( index ) -= other;
813 template<
size_t M,
typename T >
815 vector< M, T >::operator-()
const
817 vector< M, T > v( *
this );
821 template<
size_t M,
typename T >
822 const vector< M, T >&
823 vector< M, T >::negate()
825 for(
size_t index = 0; index < M; ++index )
826 array[ index ] = -array[ index ];
830 template<
size_t M,
typename T >
837 template<
size_t M,
typename T >
844 template<
size_t M,
typename T >
851 template<
size_t M,
typename T >
858 template<
size_t M,
typename T >
860 vector< M, T >::x()
const
865 template<
size_t M,
typename T >
867 vector< M, T >::y()
const
872 template<
size_t M,
typename T >
874 vector< M, T >::z()
const
879 template<
size_t M,
typename T >
881 vector< M, T >::w()
const
886 template<
size_t M,
typename T >
893 template<
size_t M,
typename T >
900 template<
size_t M,
typename T >
907 template<
size_t M,
typename T >
914 template<
size_t M,
typename T >
916 vector< M, T >::r()
const
921 template<
size_t M,
typename T >
923 vector< M, T >::g()
const
928 template<
size_t M,
typename T >
930 vector< M, T >::b()
const
935 template<
size_t M,
typename T >
937 vector< M, T >::a()
const
942 template<
size_t M,
typename T >
template<
typename TT >
943 vector< M, T >& vector< M, T >::cross(
const vector< M, TT >& rhs,
944 typename enable_if< M == 3, TT >::type* )
946 const T x_ = y() * rhs.z() - z() * rhs.y();
947 const T y_ = z() * rhs.x() - x() * rhs.z();
948 const T z_ = x() * rhs.y() - y() * rhs.x();
955 template<
size_t M,
typename T >
956 inline T vector< M, T >::dot(
const vector< M, T >& other )
const
959 for(
size_t index = 0; index < M; ++index )
960 tmp += at( index ) * other.at( index );
965 template<
size_t M,
typename T >
inline T vector< M, T >::normalize()
967 const T len = length();
968 if ( len <= std::numeric_limits< T >::epsilon( ))
971 const T tmp = 1.0 / len;
976 template<
size_t M,
typename T >
977 inline T vector< M, T >::length()
const
979 return std::sqrt( squared_length() );
982 template<
size_t M,
typename T >
983 inline T vector< M, T >::squared_length()
const
985 T _squared_length = 0.0;
986 for( const_iterator it = begin(), it_end = end(); it != it_end; ++it )
987 _squared_length += (*it) * (*it);
989 return _squared_length;
992 template<
size_t M,
typename T >
994 vector< M, T >::distance(
const vector< M, T >& other )
const
996 return std::sqrt( squared_distance( other ) );
999 template<
size_t M,
typename T >
1000 inline T vector< M, T >::squared_distance(
const vector< M, T >& other )
const
1002 vector< M, T > tmp( *
this );
1004 return tmp.squared_length();
1010 for(
size_t i = 1; i < M; ++i )
1015 template<
size_t M,
typename T >
template<
typename TT >
1017 typename enable_if< M==3, TT >::type* )
1019 const T costheta = std::cos( theta );
1020 const T sintheta = std::sin( theta );
1024 (costheta + ( 1 - costheta ) * axis.x() * axis.x() ) * x() +
1025 (( 1 - costheta ) * axis.x() * axis.y() - axis.z() * sintheta ) * y() +
1026 (( 1 - costheta ) * axis.x() * axis.z() + axis.y() * sintheta ) * z(),
1028 (( 1 - costheta ) * axis.x() * axis.y() + axis.z() * sintheta ) * x() +
1029 ( costheta + ( 1 - costheta ) * axis.y() * axis.y() ) * y() +
1030 (( 1 - costheta ) * axis.y() * axis.z() - axis.x() * sintheta ) * z(),
1032 (( 1 - costheta ) * axis.x() * axis.z() - axis.y() * sintheta ) * x() +
1033 (( 1 - costheta ) * axis.y() * axis.z() + axis.x() * sintheta ) * y() +
1034 ( costheta + ( 1 - costheta ) * axis.z() * axis.z() ) * z( ));
1038 template<
size_t M,
typename T >
template<
typename TT >
inline vector< 3, T >
1039 vector< M, T >::project_point_onto_sphere(
const vector< 3, TT >& point,
1040 typename enable_if< M == 4, TT >::type* )
const
1045 projected_point -= center_;
1046 projected_point.normalize();
1047 projected_point *= w();
1048 return center_ + projected_point;
1052 template<
size_t M,
typename T >
template<
typename TT >
inline T
1053 vector< M, T >::distance_to_sphere(
const vector< 3, TT >& point,
1054 typename enable_if< M == 4, TT >::type* )
1058 return ( point - center_ ).length() - w();
1061 template<
size_t M,
typename T >
template<
size_t N,
size_t O >
1068 template<
size_t M,
typename T >
template<
size_t N,
size_t O >
1072 ::memcpy( array + O, sub.array, N *
sizeof( T ));
1076 template<
size_t M,
typename T >
template<
typename TT >
1078 typename enable_if< M == 4, TT >::type* )
const
1081 return normal.dot( point ) + w();
1085 template<
size_t M,
typename T >
template<
typename TT >
vector< 3, T >
1086 vector< M, T >::project_point_onto_plane(
const vector< 3, TT >& point,
1087 typename enable_if< M == 4, TT >::type* )
const
1090 return point - ( normal * distance_to_plane( point ) );
1093 template<
size_t M,
typename T >
1094 bool vector< M, T >::operator==(
const vector< M, T >& other )
const
1096 return memcmp( array, other.array,
sizeof( array )) == 0;
1099 template<
size_t M,
typename T >
1100 bool vector< M, T >::operator!=(
const vector< M, T >& other )
const
1102 return ! this->operator==( other );
1105 template<
size_t M,
typename T >
1106 bool vector< M, T >::equals(
const vector< M, T >& other, T tolerance )
const
1108 for(
size_t index = 0; index < M; ++index )
1109 if( fabs( at( index ) - other( index ) ) >= tolerance )
1115 template<
size_t M,
typename T >
1117 vector< M, T >::operator<(
const vector< M, T >& other )
const
1119 for(
size_t index = 0; index < M; ++index )
1121 if (at( index ) < other.at( index ))
return true;
1122 if (other.at( index ) < at( index ))
return false;
1129 template<
size_t M,
typename T >
template<
size_t N >
1130 typename enable_if< N == M - 1 >::type*
1131 vector< M, T >::operator=(
const vector< N, T >& source_ )
1133 std::copy( source_.begin(), source_.end(), begin() );
1134 at( M - 1 ) =
static_cast< T
>( 1.0 );
1140 template<
size_t M,
typename T >
template<
size_t N >
1141 typename enable_if< N == M + 1 >::type*
1142 vector< M, T >::operator=(
const vector< N, T >& source_ )
1144 const T w_reci =
static_cast< T
>( 1.0 ) / source_( M );
1145 iterator it = begin(), it_end = end();
1146 for(
size_t index = 0; it != it_end; ++it, ++index )
1147 *it = source_( index ) * w_reci;
1151 template<
size_t M,
typename T >
1152 vector< M, T >& vector< M, T >::operator=(
const T* c_array )
1154 iter_set( c_array, c_array + M );
1158 template<
size_t M,
typename T >
1159 T vector< M, T >::operator=( T filler_value )
1161 for(
size_t index = 0; index < M; ++index )
1162 at( index ) = filler_value;
1163 return filler_value;
1166 template<
size_t M,
typename T >
1167 vector< M, T >& vector< M, T >::operator=(
const vector< M, T >& other )
1169 if(
this != &other )
1170 memcpy( array, other.array, M *
sizeof( T ) );
1175 template<
size_t M,
typename T >
template<
typename U >
1176 void vector< M, T >::operator=(
const vector< M, U >& source_ )
1178 typedef typename vector< M, U >::const_iterator u_c_iter;
1179 u_c_iter it = source_.begin(), it_end = source_.end();
1180 for( iterator my_it = begin(); it != it_end; ++it, ++my_it )
1181 *my_it = static_cast< T >( *it );
1184 template<
size_t M,
typename T >
1185 template<
typename input_iterator_t >
1187 vector< M, T >::iter_set( input_iterator_t begin_, input_iterator_t end_ )
1189 input_iterator_t in_it = begin_;
1190 iterator it = begin(), it_end = end();
1191 for( ; it != it_end && in_it != end_; ++it, ++in_it )
1192 (*it) =
static_cast< T
>( *in_it );
1195 template<
size_t M,
typename T >
1196 void vector< M, T >::clamp(
const T& min,
const T& max )
1198 for(
size_t i = 0; i < M; ++i )
1200 if( array[i] < min )
1202 if( array[i] > max )
1207 template<
size_t M,
typename T >
1209 vector< M, T >::size()
1214 template<
size_t M,
typename T >
1216 vector< M, T >::find_min_index()
const
1218 return std::min_element( begin(), end() ) - begin();
1221 template<
size_t M,
typename T >
1223 vector< M, T >::find_max_index()
const
1225 return std::max_element( begin(), end() ) - begin();
1228 template<
size_t M,
typename T >
1230 vector< M, T >::find_min()
1232 return *std::min_element( begin(), end() );
1235 template<
size_t M,
typename T >
1237 vector< M, T >::find_min()
const
1239 return *std::min_element( begin(), end() );
1242 template<
size_t M,
typename T >
1244 vector< M, T >::find_max()
1246 return *std::max_element( begin(), end() );
1249 template<
size_t M,
typename T >
1251 vector< M, T >::find_max()
const
1253 return *std::max_element( begin(), end() );
1256 template<
size_t M,
typename T >
1257 inline typename vector< M, T >::iterator
1258 vector< M, T >::begin()
1263 template<
size_t M,
typename T >
1264 inline typename vector< M, T >::iterator
1265 vector< M, T >::end()
1270 template<
size_t M,
typename T >
1271 inline typename vector< M, T >::const_iterator
1272 vector< M, T >::begin()
const
1277 template<
size_t M,
typename T >
1278 inline typename vector< M, T >::const_iterator
1279 vector< M, T >::end()
const
1284 template<
size_t M,
typename T >
1285 inline typename vector< M, T >::reverse_iterator
1286 vector< M, T >::rbegin()
1288 return array + M - 1;
1291 template<
size_t M,
typename T >
1292 inline typename vector< M, T >::reverse_iterator
1293 vector< M, T >::rend()
1298 template<
size_t M,
typename T >
1299 inline typename vector< M, T >::const_reverse_iterator
1300 vector< M, T >::rbegin()
const
1302 return array + M - 1;
1305 template<
size_t M,
typename T >
1306 inline typename vector< M, T >::const_reverse_iterator
1307 vector< M, T >::rend()
const
1312 template<
size_t M,
typename T >
1314 vector< M, T >::is_unit_vector()
const
1316 const_iterator it = begin(), it_end = end();
1318 for( ; it != it_end; ++it )
1326 else if ( *it != 0.0 )
1334 template<
size_t M,
typename T >
1336 vector< M, T >::perturb( T perturbation )
1338 for( iterator it = begin(), it_end = end(); it != it_end; ++it )
1340 (*it) += ( rand() & 1u ) ? perturbation : -perturbation;
1345 template<
size_t M,
typename T >
1347 vector< M, T >::sqrt_elementwise()
1349 for( iterator it = begin(), it_end = end(); it != it_end; ++it )
1351 (*it) = std::sqrt(*it);
1355 template<
size_t M,
typename T >
void vector< M, T >::reciprocal()
1357 for( iterator it = begin(), it_end = end(); it != it_end; ++it )
1358 (*it) =
static_cast< T
>( 1 ) / (*it);
1361 template<
size_t M,
typename T >
void vector< M, T >::reciprocal_safe()
1363 for( iterator it = begin(), it_end = end(); it != it_end; ++it )
1368 v = std::numeric_limits< T >::max();
1370 v =
static_cast< T
>( 1 ) / v;
1374 template<
size_t M,
typename T >
1375 template<
typename TT >
1377 vector< M, T >::cast_from(
const vector< M, TT >& other )
1380 typedef typename vector_tt_type::const_iterator tt_const_iterator;
1382 iterator it = begin(), it_end = end();
1383 tt_const_iterator other_it = other.begin();
1384 for( ; it != it_end; ++it, ++other_it )
1386 *it =
static_cast< T
>( *other_it );
1390 template<
size_t M,
typename T >
1392 vector< M, T >::nnz()
const
1396 const_iterator it = begin(),
1398 for( ; it != it_end; ++it)
1408 template<
size_t M,
typename T >
1410 vector< M, T >::norm( )
const
1412 double norm_v = 0.0;
1414 const_iterator it = begin(), it_end = end();
1415 for( ; it != it_end; ++it )
1417 norm_v += *it * *it;
1420 return std::sqrt(norm_v);
1423 template<
size_t M,
typename T >
1425 vector< M, T >::set_random(
int seed )
1430 for(
size_t i = 0; i < M; ++i )
1432 const double fillValue = double( rand( )) / double( RAND_MAX );
1433 at( i ) = -1.0 + 2.0 * fillValue;
heavily inspired by boost::enable_if http://www.boost.org, file: boost/utility/enable_if.hpp, Copyright 2003 Jaakko Järvi, Jeremiah Willcock, Andrew Lumsdaine
void set_sub_vector(const vector< N, T > &sub, typename enable_if< M >=N+O >::type *=0)
Set the sub vector of the given length at the given offset.
vector< N, T > get_sub_vector(typename enable_if< M >=N+O >::type *=0) const