45#ifndef LASS_GUARDIAN_OF_INCLUSION_NUM_VECTOR_INL
46#define LASS_GUARDIAN_OF_INCLUSION_NUM_VECTOR_INL
52#define LASS_NUM_VECTOR_ENFORCE_EQUAL_DIMENSION(a, b)\
53 *LASS_UTIL_IMPL_MAKE_ENFORCER(\
54 ::lass::util::impl::EqualPredicate,\
55 ::lass::util::impl::DefaultRaiser,\
58 "Vectors '" LASS_STRINGIFY(a) "' and '" LASS_STRINGIFY(b) "' have different dimensions in '" LASS_HERE "'.")
72template <
typename T,
typename S>
73Vector<T, S>::Vector():
76 LASS_ASSERT(
size() == 0);
92template <
typename T,
typename S>
93Vector<T, S>::Vector(TSize iDimension, TParam iInitialValue):
94 storage_(iDimension, iInitialValue)
108template <
typename T,
typename S>
109Vector<T, S>::Vector(
const TStorage& iStorage):
125template <
typename T,
typename S>
126template <
typename VectorType>
127Vector<T, S>::Vector(
const VectorType& iVector)
129 init(iVector, meta::Wrap<
typename meta::IsIntegral<VectorType>::Type>());
144template <
typename T,
typename S>
145template <
typename T2,
typename S2>
146Vector<T, S>::Vector(
const Vector<T2, S2>& iOther)
148 TSize n = iOther.storage_.size();
150 for (TSize i = 0; i < n; ++i)
152 storage_[i] = iOther.storage_[i];
168template <
typename T,
typename S>
169template <
typename T2,
typename S2>
172 TSize n = iOther.storage_.size();
174 for (TSize i = 0; i < n; ++i)
176 storage_[i] = iOther.storage_[i];
189template <
typename T,
typename S>
inline
190typename Vector<T, S>::TSize
193 return storage_.size();
205template <
typename T,
typename S>
inline
206const typename Vector<T, S>::TValue
209 LASS_ASSERT(iIndex <
size());
210 return storage_[iIndex];
224template <
typename T,
typename S>
inline
225typename Vector<T, S>::TReference
228 LASS_ASSERT(iIndex <
size());
229 return storage_[iIndex];
240template <
typename T,
typename S>
inline
241const typename Vector<T, S>::TValue
244 return storage_[mod(iIndex, storage_.size())];
258template <
typename T,
typename S>
inline
259typename Vector<T, S>::TReference
262 return storage_[num::mod(iIndex, storage_.size())];
275template <
typename T,
typename S>
inline
293template <
typename T,
typename S>
297 typedef impl::VNeg<T, S> TExpression;
298 return Vector<T, TExpression>(TExpression(storage_));
315template <
typename T,
typename S>
316template <
typename T2,
typename S2>
319 LASS_NUM_VECTOR_ENFORCE_EQUAL_DIMENSION(*
this, iB);
320 const TSize n = storage_.size();
321 for (TSize i = 0; i < n; ++i)
323 storage_[i] += iB[i];
342template <
typename T,
typename S>
343template <
typename T2,
typename S2>
346 LASS_NUM_VECTOR_ENFORCE_EQUAL_DIMENSION(*
this, iB);
347 const TSize n = storage_.size();
348 for (TSize i = 0; i < n; ++i)
350 storage_[i] -= iB[i];
369template <
typename T,
typename S>
370template <
typename T2,
typename S2>
373 LASS_NUM_VECTOR_ENFORCE_EQUAL_DIMENSION(*
this, iB);
374 const TSize n = storage_.size();
375 for (TSize i = 0; i < n; ++i)
377 storage_[i] *= iB[i];
396template <
typename T,
typename S>
397template <
typename T2,
typename S2>
400 LASS_NUM_VECTOR_ENFORCE_EQUAL_DIMENSION(*
this, iB);
401 const TSize n = storage_.size();
402 for (TSize i = 0; i < n; ++i)
404 storage_[i] /= iB[i];
418template <
typename T,
typename S>
419template <
typename T2>
422 const TSize n = storage_.
size();
423 for (TSize i = 0; i < n; ++i)
439template <
typename T,
typename S>
440template <
typename T2>
443 const TSize n = storage_.
size();
444 for (TSize i = 0; i < n; ++i)
460template <
typename T,
typename S>
461template <
typename T2>
464 const TSize n = storage_.
size();
465 for (TSize i = 0; i < n; ++i)
481template <
typename T,
typename S>
482template <
typename T2>
485 const TSize n = storage_.
size();
486 for (TSize i = 0; i < n; ++i)
502template <
typename T,
typename S>
inline
505 return storage_.size() == 0;
515template <
typename T,
typename S>
518 const TSize n = storage_.size();
519 for (TSize i = 0; i < n; ++i)
523 if (storage_[i] != TNumTraits::zero)
538template <
typename T,
typename S>
539const typename Vector<T, S>::TValue
542 const TSize n = storage_.size();
543 TValue result = TNumTraits::zero;
544 for (TSize i = 0; i < n; ++i)
546 result += storage_[i];
558template <
typename T,
typename S>
559const typename Vector<T, S>::TValue
562 const TSize n = storage_.size();
565 return TNumTraits::zero;
567 TValue result = storage_[0];
568 for (TSize i = 1; i < n; ++i)
570 result = std::min(result, storage_[i]);
582template <
typename T,
typename S>
583const typename Vector<T, S>::TValue
586 const TSize n = storage_.size();
589 return TNumTraits::zero;
591 TValue result = storage_[0];
592 for (TSize i = 1; i < n; ++i)
594 result = std::max(result, storage_[i]);
607template <
typename T,
typename S>
608const typename Vector<T, S>::TValue
611 const TSize n = storage_.size();
612 TValue result = TNumTraits::zero;
613 for (TSize i = 0; i < n; ++i)
628template <
typename T,
typename S>
629const typename Vector<T, S>::TValue
648template <
typename T,
typename S>
652 const TValue scale = TNumTraits::one /
norm();
653 typedef impl::VMul<T, S, impl::VScalar<T> > TExpression;
654 return Vector<T, TExpression>(TExpression(storage_, impl::VScalar<T>(storage_.size(), scale)));
664template <
typename T,
typename S>
668 typedef impl::VRec<T, S> TExpression;
669 return Vector<T, TExpression>(TExpression(storage_));
681template <
typename T,
typename S>
682template <
typename S2>
686 LASS_NUM_VECTOR_ENFORCE_EQUAL_DIMENSION(*
this, iB);
688 typedef impl::VMul<T, S, impl::VScalar<T> > TExpression;
689 return Vector<T, TExpression>(TExpression(storage_, impl::VScalar<T>(storage_.size(), scale)));
701template <
typename T,
typename S>
702template <
typename S2>
718template <
typename T,
typename S>
726template <
typename T,
typename S>
inline
727const typename Vector<T, S>::TStorage&
728Vector<T, S>::storage()
const
735template <
typename T,
typename S>
inline
736typename Vector<T, S>::TStorage&
737Vector<T, S>::storage()
754template <
typename T,
typename S>
757 storage_.swap(iOther.storage_);
764template <
typename T,
typename S>
765template <
typename IntegralType>
766void Vector<T, S>::init(IntegralType iDimension, meta::Wrap<meta::True>)
768 TStorage temp(iDimension, T());
774template <
typename T,
typename S>
775template <
typename VectorType>
776void Vector<T, S>::init(
const VectorType& iVector, meta::Wrap<meta::False>)
778 TSize n = iVector.size();
779 TStorage temp(n, T());
780 for (TSize i = 0; i < n; ++i)
782 temp[i] = iVector[i];
798template <
typename T,
typename S1,
typename S2>
801 typedef typename Vector<T, S1>::TSize TSize;
802 const TSize n = iA.size();
808 for (TSize i = 0; i < n; ++i)
825template <
typename T,
typename S1,
typename S2>
841template <
typename T,
typename S1,
typename S2>
842const T
dot(
const Vector<T, S1>& iA,
const Vector<T, S2>& iB)
844 LASS_NUM_VECTOR_ENFORCE_EQUAL_DIMENSION(iA, iB);
845 typedef typename Vector<T, S1>::TSize TSize;
846 const TSize n = iA.
size();
848 T result = NumTraits<T>::zero;
849 for (TSize i = 0; i < n; ++i)
851 result += iA[i] * iB[i];
866template <
typename T,
typename S1,
typename S2>
868operator+(
const Vector<T, S1>& iA,
const Vector<T, S2>& iB)
870 LASS_NUM_VECTOR_ENFORCE_EQUAL_DIMENSION(iA, iB);
871 typedef impl::VAdd<T, S1, S2> TExpression;
872 return Vector<T, TExpression>(TExpression(iA.storage(), iB.storage()));
885template <
typename T,
typename S1,
typename S2>
887operator-(
const Vector<T, S1>& iA,
const Vector<T, S2>& iB)
889 LASS_NUM_VECTOR_ENFORCE_EQUAL_DIMENSION(iA, iB);
890 typedef impl::VSub<T, S1, S2> TExpression;
891 return Vector<T, TExpression>(TExpression(iA.storage(), iB.storage()));
904template <
typename T,
typename S1,
typename S2>
906operator*(
const Vector<T, S1>& iA,
const Vector<T, S2>& iB)
908 LASS_NUM_VECTOR_ENFORCE_EQUAL_DIMENSION(iA, iB);
909 typedef impl::VMul<T, S1, S2> TExpression;
910 return Vector<T, TExpression>(TExpression(iA.storage(), iB.storage()));
923template <
typename T,
typename S1,
typename S2>
925operator/(
const Vector<T, S1>& iA,
const Vector<T, S2>& iB)
927 LASS_NUM_VECTOR_ENFORCE_EQUAL_DIMENSION(iA, iB);
928 typedef impl::VDiv<T, S1, S2> TExpression;
929 return Vector<T, TExpression>(TExpression(iA.storage(), iB.storage()));
940template <
typename T,
typename S>
944 typedef impl::VAdd<T, impl::VScalar<T>, S> TExpression;
945 return Vector<T, TExpression>(TExpression(impl::VScalar<T>(iB.
size(), iA), iB.storage()));
956template <
typename T,
typename S>
960 typedef impl::VSub<T, impl::VScalar<T>, S> TExpression;
961 return Vector<T, TExpression>(TExpression(impl::VScalar<T>(iB.
size(), iA), iB.storage()));
972template <
typename T,
typename S>
976 typedef impl::VMul<T, impl::VScalar<T>, S> TExpression;
977 return Vector<T, TExpression>(TExpression(impl::VScalar<T>(iB.
size(), iA), iB.storage()));
988template <
typename T,
typename S>
992 typedef impl::VDiv<T, impl::VScalar<T>, S> TExpression;
993 return Vector<T, TExpression>(TExpression(impl::VScalar<T>(iB.
size(), iA), iB.storage()));
1004template <
typename T,
typename S>
1008 typedef impl::VAdd<T, S, impl::VScalar<T> > TExpression;
1009 return Vector<T, TExpression>(TExpression(iA.storage(), impl::VScalar<T>(iA.
size(), iB)));
1020template <
typename T,
typename S>
1024 typedef impl::VSub<T, S, impl::VScalar<T> > TExpression;
1025 return Vector<T, TExpression>(TExpression(iA.storage(), impl::VScalar<T>(iA.
size(), iB)));
1036template <
typename T,
typename S>
1040 typedef impl::VMul<T, S, impl::VScalar<T> > TExpression;
1041 return Vector<T, TExpression>(TExpression(iA.storage(), impl::VScalar<T>(iA.
size(), iB)));
1052template <
typename T,
typename S>
1056 typedef impl::VDiv<T, S, impl::VScalar<T> > TExpression;
1057 return Vector<T, TExpression>(TExpression(iA.storage(), impl::VScalar<T>(iA.
size(), iB)));
1067template <
typename T,
typename S,
typename Char,
typename Traits>
1068std::basic_ostream<Char, Traits>&
1069operator<<(std::basic_ostream<Char, Traits>& oOStream,
const Vector<T, S>& iB)
1071 typedef typename Vector<T, S>::TSize TSize;
1072 const TSize n = iB.
size();
1074 LASS_ENFORCE_STREAM(oOStream) <<
"(";
1077 LASS_ENFORCE_STREAM(oOStream) << iB[0];
1079 for (TSize i = 1; i < n; ++i)
1081 LASS_ENFORCE_STREAM(oOStream) <<
", " << iB[i];
1083 LASS_ENFORCE_STREAM(oOStream) <<
")";
1091template <
typename T,
typename Char,
typename Traits>
1092std::basic_istream<Char, Traits>&
1093operator>>(std::basic_istream<Char, Traits>& ioIStream,
Vector<T>& oB)
1095 typedef typename Vector<T>::TValue
TValue;
1096 typedef typename Vector<T>::TSize TSize;
1097 typedef typename Vector<T>::TStorage TStorage;
1104 LASS_ENFORCE_STREAM(ioIStream) >> c;
1107 ioIStream.clear(std::ios::failbit);
1117 LASS_ENFORCE_STREAM(ioIStream) >> value >> c;
1118 buffer.push_back(value);
1120 if (c !=
',' && c !=
')')
1122 ioIStream.clear(std::ios::failbit);
1128 oB.storage().swap(buffer);
a dynamic sized n-dimensional vector with vector expression templates
bool isZero() const
Return true if all the components are (exactly!) zero.
const TValue sum() const
Return sum of all components of vector.
const Vector< T, impl::VMul< T, S, impl::VScalar< T > > > operator*(const Vector< T, S > &iA, const T &iB)
multiply all components of iA with iB.
const TValue operator[](TSize iIndex) const
return the iIndex'th component value.
const TValue squaredNorm() const
Return squared norm of vector.
const Vector< T, impl::VAdd< T, S1, S2 > > operator+(const Vector< T, S1 > &iA, const Vector< T, S2 > &iB)
componentwise addition
const Vector< T, impl::VMul< T, S1, S2 > > operator*(const Vector< T, S1 > &iA, const Vector< T, S2 > &iB)
componentwise multiplication
const Vector< T, impl::VMul< T, S, impl::VScalar< T > > > project(const Vector< T, S2 > &iB) const
Project vector on this one.
const Vector< T, impl::VMul< T, S, impl::VScalar< T > > > normal() const
return a unit vector with same direction/sense as this vector.
const Vector< T, impl::VNeg< T, S > > operator-() const
return a vector with all components negated (-v)[i] == -(v[i]).
const Vector< T, impl::VDiv< T, impl::VScalar< T >, S > > operator/(const T &iA, const Vector< T, S > &iB)
multiply iA with all reciprocal components of iB
void swap(Vector< T, S > &iOther)
swap storage of two vectors
const TValue min() const
Return minimum of all components of vector.
Vector< T, S > & operator*=(const Vector< T2, S2 > &iB)
multiply storage/expression vector with this (this should be a storage vector).
const TValue norm() const
Return norm of vector.
Vector< T, S > & operator=(const Vector< T2, S2 > &iOther)
assign storage/expression vector to this (this should be a storage vector).
Vector< T, S > & operator/=(const Vector< T2, S2 > &iB)
divide this by storage/expression vector (this should be a storage vector).
const Vector< T, impl::VSub< T, S, impl::VScalar< T > > > operator-(const Vector< T, S > &iA, const T &iB)
subtract iB from all components of iA
const Vector< T, impl::VRec< T, S > > reciprocal() const
return a vector with each component being the reciprocal value of this vector.
bool isEmpty() const
return true if vector contains no dataa at all
const Vector< T, impl::VSub< T, impl::VScalar< T >, S > > operator-(const T &iA, const Vector< T, S > &iB)
add iA to all negated components of iB
const Vector< T, impl::VSub< T, S1, S2 > > operator-(const Vector< T, S1 > &iA, const Vector< T, S2 > &iB)
componentwise subtraction
const Vector< T, impl::VDiv< T, S, impl::VScalar< T > > > operator/(const Vector< T, S > &iA, const T &iB)
multiply all components of iA with iB.
const Vector< T, impl::VAdd< T, S, impl::VScalar< T > > > operator+(const Vector< T, S > &iA, const T &iB)
add iB to all components of iA
const Vector< T, impl::VSub< T, S2, impl::VMul< T, S, impl::VScalar< T > > > > reject(const Vector< T, S2 > &iB) const
Project vector on this one.
const Vector< T, impl::VMul< T, impl::VScalar< T >, S > > operator*(const T &iA, const Vector< T, S > &iB)
multiply iA with all components of iB
TSize size() const
return dimension of vector.
const Vector< T, impl::VAdd< T, impl::VScalar< T >, S > > operator+(const T &iA, const Vector< T, S > &iB)
add iA to all components of iB
const Vector< T, S > & operator+() const
A weird way to get back the same object.
Vector< T, S > & operator+=(const Vector< T2, S2 > &iB)
add storage/expression vector to this (this should be a storage vector).
const TValue at(TSize iIndex) const
return the iIndex'th component value and wrap index if necessary.
Vector< T, S > & operator-=(const Vector< T2, S2 > &iB)
subtract storage/expression vector from this (this should be a storage vector).
const TValue max() const
Return maximum of all components of vector.
const T dot(const Vector< T, S1 > &iA, const Vector< T, S2 > &iB)
dot product.
void normalize()
Normalize vector.
const Vector< T, impl::VDiv< T, S1, S2 > > operator/(const Vector< T, S1 > &iA, const Vector< T, S2 > &iB)
componentwise division
T sqr(const T &x)
return x * x
T norm(const T &x)
return norm of x as if x is real part of complex number: sqr(x)
numeric types and traits.
Library for Assembled Shared Sources.