33 #ifndef __VMML__VECTOR__HPP__
34 #define __VMML__VECTOR__HPP__
36 #include <vmmlib/math.hpp>
37 #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 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 = 0 >
245 vector< N, T >
get_sub_vector(
typename enable_if< M >= N+O >::type* = 0 )
249 template<
size_t N,
size_t O = 0 >
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;
279 size_t find_abs_min_index()
const;
280 size_t find_abs_max_index()
const;
285 const T& find_min()
const;
286 const T& find_max()
const;
288 void clamp(
const T& min = 0.0,
const T& max = 1.0 );
290 inline static size_t size();
292 bool is_unit_vector()
const;
295 void perturb( T perturbation = 0.0001 );
297 void sqrt_elementwise();
305 void reciprocal_safe();
307 template<
typename TT >
308 void cast_from(
const vector< M, TT >& other );
315 friend std::ostream& operator<< ( std::ostream& os,
const vector& vector_ )
317 const std::ios::fmtflags flags = os.flags();
318 const int prec = os.precision();
320 os.setf( std::ios::right, std::ios::adjustfield );
323 for(
size_t index = 0; index < M; ++index )
324 os << std::setw(10) << vector_.at( index ) <<
" ";
326 os.precision( prec );
335 static const vector FORWARD;
336 static const vector BACKWARD;
340 static const vector RIGHT;
346 static const vector UNIT_X;
347 static const vector UNIT_Y;
348 static const vector UNIT_Z;
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 >
367 template<
size_t M,
typename T >
369 template<
size_t M,
typename T >
372 template<
size_t M,
typename T >
374 template<
size_t M,
typename T >
377 template<
size_t M,
typename T >
379 template<
size_t M,
typename T >
387 template<
size_t M,
typename T >
390 return a.equals( b );
394 template<
size_t M,
typename T >
395 static vector< M, T > operator* ( T factor,
const vector< M, T >& vector_ )
397 return vector_ * factor;
400 template<
size_t M,
typename T >
401 inline T dot(
const vector< M, T >& first,
const vector< M, T >& second )
403 return first.dot( second );
406 template<
size_t M,
typename T >
407 inline vector< M, T > cross( vector< M, T > a,
const vector< M, T >& b )
412 template<
size_t M,
typename T >
413 vector< M, T > compute_normal(
const vector< M, T >& a,
const vector< M, T >& b,
414 const vector< M, T >& c )
417 const vector< M, T > u = b - a;
418 const vector< M, T > v = c - a;
419 vector< M, T > w = cross( u, v );
424 template<
typename T >
428 return vec.rotate( theta, axis );
432 template<
size_t M,
typename T >
433 inline vector< M, T > normalize( vector< M, T > vector_ )
439 template<
typename T >
449 plane.w() = -plane.x() * a.x() - plane.y() * a.y() - plane.z() * a.z();
453 template<
size_t M,
typename T >
454 vector< M, T >::vector(
const T& _a )
456 for( iterator it = begin(), it_end = end(); it != it_end; ++it )
462 template<
size_t M,
typename T >
463 vector< M, T >::vector(
const T& _x,
const T& _y )
469 template<
size_t M,
typename T >
470 vector< M, T >::vector(
const T& _x,
const T& _y,
const T& _z )
477 template<
size_t M,
typename T >
478 vector< M, T >::vector(
const T& _x,
const T& _y,
const T& _z,
const T& _w )
486 template<
size_t M,
typename T >
487 vector< M, T >::vector(
const T* values )
489 memcpy( array, values, M *
sizeof( T ));
493 template<
size_t M,
typename T >
494 template<
typename OSGVEC3 >
495 vector< M, T >::vector(
const OSGVEC3& from,
496 typename enable_if< M == 3, OSGVEC3 >::type* )
498 array[ 0 ] = from.x();
499 array[ 1 ] = from.y();
500 array[ 2 ] = from.z();
505 template<
size_t M,
typename T >
507 vector< M, T >::vector(
const vector< M-1, T >& vector_, T last_ )
509 typename vector< M-1, T >::const_iterator
510 it = vector_.begin(), it_end = vector_.end();
512 iterator my_it = begin();
514 for( ; it != it_end; ++it, ++my_it )
523 template<
size_t M,
typename T >
525 vector< M, T >::vector(
const vector< N, T >& source_,
526 typename enable_if< N == M - 1 >::type* )
532 template<
size_t M,
typename T >
534 vector< M, T >::vector(
const vector< N, T >& source_,
535 typename enable_if< N == M + 1 >::type* )
540 template<
size_t M,
typename T >
541 template<
typename U >
542 vector< M, T >::vector(
const vector< M, U >& source_ )
547 template<
size_t M,
typename T >
void vector< M, T >::set( T _a )
549 for( iterator it = begin(), it_end = end(); it != it_end; ++it )
554 template<
size_t M,
typename T >
555 void vector< M, T >::set(
const vector< M-1, T >& v, T _a )
557 memcpy( array, v.array,
sizeof( T ) * (M-1) );
562 template<
size_t M,
typename T >
template<
size_t N >
563 void vector< M, T >::set(
const vector< N, T >& v )
566 if (N < M) minimum = N;
567 memcpy( array, v.array,
sizeof( T ) * minimum );
570 template<
size_t M,
typename T >
571 void vector< M, T >::set( T _x, T _y )
577 template<
size_t M,
typename T >
578 void vector< M, T >::set( T _x, T _y, T _z )
585 template<
size_t M,
typename T >
586 void vector< M, T >::set( T _x, T _y, T _z, T _w )
594 template<
size_t M,
typename T >
596 vector< M, T >::operator()(
size_t index )
601 template<
size_t M,
typename T >
603 vector< M, T >::operator()(
size_t index )
const
608 template<
size_t M,
typename T >
610 vector< M, T >::at(
size_t index )
613 throw std::runtime_error(
"at() - index out of bounds" );
614 return array[ index ];
617 template<
size_t M,
typename T >
619 vector< M, T >::at(
size_t index )
const
622 throw std::runtime_error(
"at() - index out of bounds" );
623 return array[ index ];
626 #ifndef VMMLIB_NO_CONVERSION_OPERATORS
628 template<
size_t M,
typename T >
629 vector< M, T >::operator T*()
634 template<
size_t M,
typename T >
635 vector< M, T >::operator
const T*()
const
641 template<
size_t M,
typename T >
643 vector< M, T >::operator[](
size_t index )
648 template<
size_t M,
typename T >
650 vector< M, T >::operator[](
size_t index )
const
658 template<
size_t M,
typename T >
660 vector< M, T >::operator[](
size_t index )
665 template<
size_t M,
typename T >
667 vector< M, T >::operator[](
size_t index )
const
673 template<
size_t M,
typename T >
675 vector< M, T >::operator*(
const vector< M, T >& other )
const
677 vector< M, T > result;
678 for(
size_t index = 0; index < M; ++index )
679 result.at( index ) = at( index ) * other.at( index );
683 template<
size_t M,
typename T >
685 vector< M, T >::operator/(
const vector< M, T >& other )
const
687 vector< M, T > result;
688 for(
size_t index = 0; index < M; ++index )
689 result.at( index ) = at( index ) / other.at( index );
693 template<
size_t M,
typename T >
695 vector< M, T >::operator+(
const vector< M, T >& other )
const
697 vector< M, T > result;
698 for(
size_t index = 0; index < M; ++index )
699 result.at( index ) = at( index ) + other.at( index );
703 template<
size_t M,
typename T >
705 vector< M, T >::operator-(
const vector< M, T >& other )
const
707 vector< M, T > result;
708 for(
size_t index = 0; index < M; ++index )
709 result.at( index ) = at( index ) - other.at( index );
713 template<
size_t M,
typename T >
715 vector< M, T >::operator*=(
const vector< M, T >& other )
717 for(
size_t index = 0; index < M; ++index )
718 at( index ) *= other.at( index );
721 template<
size_t M,
typename T >
723 vector< M, T >::operator/=(
const vector< M, T >& other )
725 for(
size_t index = 0; index < M; ++index )
726 at( index ) /= other.at( index );
729 template<
size_t M,
typename T >
731 vector< M, T >::operator+=(
const vector< M, T >& other )
733 for(
size_t index = 0; index < M; ++index )
734 at( index ) += other.at( index );
737 template<
size_t M,
typename T >
739 vector< M, T >::operator-=(
const vector< M, T >& other )
741 for(
size_t index = 0; index < M; ++index )
742 at( index ) -= other.at( index );
745 template<
size_t M,
typename T >
747 vector< M, T >::operator*(
const T other )
const
749 vector< M, T > result;
750 for(
size_t index = 0; index < M; ++index )
751 result.at( index ) = at( index ) * other;
755 template<
size_t M,
typename T >
757 vector< M, T >::operator/(
const T other )
const
759 vector< M, T > result;
760 for(
size_t index = 0; index < M; ++index )
761 result.at( index ) = at( index ) / other;
765 template<
size_t M,
typename T >
767 vector< M, T >::operator+(
const T other )
const
769 vector< M, T > result;
770 for(
size_t index = 0; index < M; ++index )
771 result.at( index ) = at( index ) + other;
775 template<
size_t M,
typename T >
777 vector< M, T >::operator-(
const T other )
const
779 vector< M, T > result;
780 for(
size_t index = 0; index < M; ++index )
781 result.at( index ) = at( index ) - other;
785 template<
size_t M,
typename T >
787 vector< M, T >::operator*=(
const T other )
789 for(
size_t index = 0; index < M; ++index )
790 at( index ) *= other;
793 template<
size_t M,
typename T >
795 vector< M, T >::operator/=(
const T other )
797 for(
size_t index = 0; index < M; ++index )
798 at( index ) /= other;
801 template<
size_t M,
typename T >
803 vector< M, T >::operator+=(
const T other )
805 for(
size_t index = 0; index < M; ++index )
806 at( index ) += other;
809 template<
size_t M,
typename T >
811 vector< M, T >::operator-=(
const T other )
813 for(
size_t index = 0; index < M; ++index )
814 at( index ) -= other;
817 template<
size_t M,
typename T >
819 vector< M, T >::operator-()
const
821 vector< M, T > v( *
this );
825 template<
size_t M,
typename T >
826 const vector< M, T >&
827 vector< M, T >::negate()
829 for(
size_t index = 0; index < M; ++index )
830 array[ index ] = -array[ index ];
834 template<
size_t M,
typename T >
841 template<
size_t M,
typename T >
848 template<
size_t M,
typename T >
855 template<
size_t M,
typename T >
862 template<
size_t M,
typename T >
864 vector< M, T >::x()
const
869 template<
size_t M,
typename T >
871 vector< M, T >::y()
const
876 template<
size_t M,
typename T >
878 vector< M, T >::z()
const
883 template<
size_t M,
typename T >
885 vector< M, T >::w()
const
890 template<
size_t M,
typename T >
897 template<
size_t M,
typename T >
904 template<
size_t M,
typename T >
911 template<
size_t M,
typename T >
918 template<
size_t M,
typename T >
920 vector< M, T >::r()
const
925 template<
size_t M,
typename T >
927 vector< M, T >::g()
const
932 template<
size_t M,
typename T >
934 vector< M, T >::b()
const
939 template<
size_t M,
typename T >
941 vector< M, T >::a()
const
946 template<
size_t M,
typename T >
template<
typename TT >
947 vector< M, T >& vector< M, T >::cross(
const vector< M, TT >& rhs,
948 typename enable_if< M == 3, TT >::type* )
950 const T x_ = y() * rhs.z() - z() * rhs.y();
951 const T y_ = z() * rhs.x() - x() * rhs.z();
952 const T z_ = x() * rhs.y() - y() * rhs.x();
959 template<
size_t M,
typename T >
960 inline T vector< M, T >::dot(
const vector< M, T >& other )
const
963 for(
size_t index = 0; index < M; ++index )
964 tmp += at( index ) * other.at( index );
969 template<
size_t M,
typename T >
inline T vector< M, T >::normalize()
971 const T len = length();
972 if ( len <= std::numeric_limits< T >::epsilon( ))
975 const T tmp = 1.0 / len;
980 template<
size_t M,
typename T >
981 inline T vector< M, T >::length()
const
983 return std::sqrt( squared_length() );
986 template<
size_t M,
typename T >
987 inline T vector< M, T >::squared_length()
const
989 T _squared_length = 0.0;
990 for( const_iterator it = begin(), it_end = end(); it != it_end; ++it )
991 _squared_length += (*it) * (*it);
993 return _squared_length;
996 template<
size_t M,
typename T >
998 vector< M, T >::distance(
const vector< M, T >& other )
const
1000 return std::sqrt( squared_distance( other ) );
1003 template<
size_t M,
typename T >
1004 inline T vector< M, T >::squared_distance(
const vector< M, T >& other )
const
1006 vector< M, T > tmp( *
this );
1008 return tmp.squared_length();
1014 for(
size_t i = 1; i < M; ++i )
1019 template<
size_t M,
typename T >
template<
typename TT >
1021 typename enable_if< M==3, TT >::type* )
1023 const T costheta = std::cos( theta );
1024 const T sintheta = std::sin( theta );
1028 (costheta + ( 1 - costheta ) * axis.x() * axis.x() ) * x() +
1029 (( 1 - costheta ) * axis.x() * axis.y() - axis.z() * sintheta ) * y() +
1030 (( 1 - costheta ) * axis.x() * axis.z() + axis.y() * sintheta ) * z(),
1032 (( 1 - costheta ) * axis.x() * axis.y() + axis.z() * sintheta ) * x() +
1033 ( costheta + ( 1 - costheta ) * axis.y() * axis.y() ) * y() +
1034 (( 1 - costheta ) * axis.y() * axis.z() - axis.x() * sintheta ) * z(),
1036 (( 1 - costheta ) * axis.x() * axis.z() - axis.y() * sintheta ) * x() +
1037 (( 1 - costheta ) * axis.y() * axis.z() + axis.x() * sintheta ) * y() +
1038 ( costheta + ( 1 - costheta ) * axis.z() * axis.z() ) * z( ));
1042 template<
size_t M,
typename T >
template<
typename TT >
inline vector< 3, T >
1043 vector< M, T >::project_point_onto_sphere(
const vector< 3, TT >& point,
1044 typename enable_if< M == 4, TT >::type* )
const
1049 projected_point -= center_;
1050 projected_point.normalize();
1051 projected_point *= w();
1052 return center_ + projected_point;
1056 template<
size_t M,
typename T >
template<
typename TT >
inline T
1057 vector< M, T >::distance_to_sphere(
const vector< 3, TT >& point,
1058 typename enable_if< M == 4, TT >::type* )
1062 return ( point - center_ ).length() - w();
1065 template<
size_t M,
typename T >
template<
size_t N,
size_t O >
1072 template<
size_t M,
typename T >
template<
size_t N,
size_t O >
1076 ::memcpy( array + O, sub.array, N *
sizeof( T ));
1080 template<
size_t M,
typename T >
template<
typename TT >
1082 typename enable_if< M == 4, TT >::type* )
const
1085 return normal.dot( point ) + w();
1089 template<
size_t M,
typename T >
template<
typename TT >
vector< 3, T >
1090 vector< M, T >::project_point_onto_plane(
const vector< 3, TT >& point,
1091 typename enable_if< M == 4, TT >::type* )
const
1094 return point - ( normal * distance_to_plane( point ) );
1097 template<
size_t M,
typename T >
1098 bool vector< M, T >::operator==(
const vector< M, T >& other )
const
1100 return memcmp( array, other.array,
sizeof( array )) == 0;
1103 template<
size_t M,
typename T >
1104 bool vector< M, T >::operator!=(
const vector< M, T >& other )
const
1106 return ! this->operator==( other );
1109 template<
size_t M,
typename T >
1110 bool vector< M, T >::equals(
const vector< M, T >& other, T tolerance )
const
1112 for(
size_t index = 0; index < M; ++index )
1113 if( fabs( at( index ) - other( index ) ) >= tolerance )
1119 template<
size_t M,
typename T >
1121 vector< M, T >::operator<(
const vector< M, T >& other )
const
1123 for(
size_t index = 0; index < M; ++index )
1125 if (at( index ) < other.at( index ))
return true;
1126 if (other.at( index ) < at( index ))
return false;
1133 template<
size_t M,
typename T >
template<
size_t N >
1134 typename enable_if< N == M - 1 >::type*
1135 vector< M, T >::operator=(
const vector< N, T >& source_ )
1137 std::copy( source_.begin(), source_.end(), begin() );
1138 at( M - 1 ) =
static_cast< T
>( 1.0 );
1144 template<
size_t M,
typename T >
template<
size_t N >
1145 typename enable_if< N == M + 1 >::type*
1146 vector< M, T >::operator=(
const vector< N, T >& source_ )
1148 const T w_reci =
static_cast< T
>( 1.0 ) / source_( M );
1149 iterator it = begin(), it_end = end();
1150 for(
size_t index = 0; it != it_end; ++it, ++index )
1151 *it = source_( index ) * w_reci;
1155 template<
size_t M,
typename T >
1156 vector< M, T >& vector< M, T >::operator=(
const T* c_array )
1158 iter_set( c_array, c_array + M );
1162 template<
size_t M,
typename T >
1163 T vector< M, T >::operator=( T filler_value )
1165 for(
size_t index = 0; index < M; ++index )
1166 at( index ) = filler_value;
1167 return filler_value;
1170 template<
size_t M,
typename T >
1171 vector< M, T >& vector< M, T >::operator=(
const vector< M, T >& other )
1173 if(
this != &other )
1174 memcpy( array, other.array, M *
sizeof( T ) );
1179 template<
size_t M,
typename T >
template<
typename U >
1180 void vector< M, T >::operator=(
const vector< M, U >& source_ )
1182 typedef typename vector< M, U >::const_iterator u_c_iter;
1183 u_c_iter it = source_.begin(), it_end = source_.end();
1184 for( iterator my_it = begin(); it != it_end; ++it, ++my_it )
1185 *my_it = static_cast< T >( *it );
1188 template<
size_t M,
typename T >
1189 template<
typename input_iterator_t >
1191 vector< M, T >::iter_set( input_iterator_t begin_, input_iterator_t end_ )
1193 input_iterator_t in_it = begin_;
1194 iterator it = begin(), it_end = end();
1195 for( ; it != it_end && in_it != end_; ++it, ++in_it )
1196 (*it) =
static_cast< T
>( *in_it );
1199 template<
size_t M,
typename T >
1200 void vector< M, T >::clamp(
const T& min,
const T& max )
1202 for(
size_t i = 0; i < M; ++i )
1204 if( array[i] < min )
1206 if( array[i] > max )
1211 template<
size_t M,
typename T >
1213 vector< M, T >::size()
1218 template<
size_t M,
typename T >
1220 vector< M, T >::find_min_index()
const
1222 return std::min_element( begin(), end() ) - begin();
1225 template<
size_t M,
typename T >
1227 vector< M, T >::find_max_index()
const
1229 return std::max_element( begin(), end() ) - begin();
1232 template<
size_t M,
typename T >
1234 vector< M, T >::find_abs_min_index()
const
1239 template<
size_t M,
typename T >
1241 vector< M, T >::find_abs_max_index()
const
1246 template<
size_t M,
typename T >
1248 vector< M, T >::find_min()
1250 return *std::min_element( begin(), end() );
1253 template<
size_t M,
typename T >
1255 vector< M, T >::find_min()
const
1257 return *std::min_element( begin(), end() );
1260 template<
size_t M,
typename T >
1262 vector< M, T >::find_max()
1264 return *std::max_element( begin(), end() );
1267 template<
size_t M,
typename T >
1269 vector< M, T >::find_max()
const
1271 return *std::max_element( begin(), end() );
1274 template<
size_t M,
typename T >
1275 inline typename vector< M, T >::iterator
1276 vector< M, T >::begin()
1281 template<
size_t M,
typename T >
1282 inline typename vector< M, T >::iterator
1283 vector< M, T >::end()
1288 template<
size_t M,
typename T >
1289 inline typename vector< M, T >::const_iterator
1290 vector< M, T >::begin()
const
1295 template<
size_t M,
typename T >
1296 inline typename vector< M, T >::const_iterator
1297 vector< M, T >::end()
const
1302 template<
size_t M,
typename T >
1303 inline typename vector< M, T >::reverse_iterator
1304 vector< M, T >::rbegin()
1306 return array + M - 1;
1309 template<
size_t M,
typename T >
1310 inline typename vector< M, T >::reverse_iterator
1311 vector< M, T >::rend()
1316 template<
size_t M,
typename T >
1317 inline typename vector< M, T >::const_reverse_iterator
1318 vector< M, T >::rbegin()
const
1320 return array + M - 1;
1323 template<
size_t M,
typename T >
1324 inline typename vector< M, T >::const_reverse_iterator
1325 vector< M, T >::rend()
const
1330 template<
size_t M,
typename T >
1332 vector< M, T >::is_unit_vector()
const
1334 const_iterator it = begin(), it_end = end();
1336 for( ; it != it_end; ++it )
1344 else if ( *it != 0.0 )
1352 template<
size_t M,
typename T >
1354 vector< M, T >::perturb( T perturbation )
1356 for( iterator it = begin(), it_end = end(); it != it_end; ++it )
1358 (*it) += ( rand() & 1u ) ? perturbation : -perturbation;
1363 template<
size_t M,
typename T >
1365 vector< M, T >::sqrt_elementwise()
1367 for( iterator it = begin(), it_end = end(); it != it_end; ++it )
1369 (*it) = std::sqrt(*it);
1373 template<
size_t M,
typename T >
void vector< M, T >::reciprocal()
1375 for( iterator it = begin(), it_end = end(); it != it_end; ++it )
1376 (*it) =
static_cast< T
>( 1 ) / (*it);
1379 template<
size_t M,
typename T >
void vector< M, T >::reciprocal_safe()
1381 for( iterator it = begin(), it_end = end(); it != it_end; ++it )
1386 v = std::numeric_limits< T >::max();
1388 v =
static_cast< T
>( 1 ) / v;
1392 template<
size_t M,
typename T >
1393 template<
typename TT >
1395 vector< M, T >::cast_from(
const vector< M, TT >& other )
1398 typedef typename vector_tt_type::const_iterator tt_const_iterator;
1400 iterator it = begin(), it_end = end();
1401 tt_const_iterator other_it = other.begin();
1402 for( ; it != it_end; ++it, ++other_it )
1404 *it =
static_cast< T
>( *other_it );
1408 template<
size_t M,
typename T >
1410 vector< M, T >::nnz()
const
1414 const_iterator it = begin(),
1416 for( ; it != it_end; ++it)
1426 template<
size_t M,
typename T >
1428 vector< M, T >::norm( )
const
1430 double norm_v = 0.0;
1432 const_iterator it = begin(), it_end = end();
1433 for( ; it != it_end; ++it )
1435 norm_v += *it * *it;
1438 return std::sqrt(norm_v);
1441 template<
size_t M,
typename T >
1443 vector< M, T >::set_random(
int seed )
1448 for(
size_t i = 0; i < M; ++i )
1450 const double fillValue = double( rand( )) / double( RAND_MAX );
1451 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