29 #include "bias_config.h"
30 #include <Base/Common/W32Compat.hh>
35 #ifdef BIAS_HAVE_CPP11
36 #include <initializer_list>
37 #endif // BIAS_HAVE_CPP11
39 #include <Base/Math/tnt/tnt.h>
40 #include <Base/Math/tnt/vec.h>
41 #include <Base/Math/Operators.hh>
42 #include <Base/Math/Utils.hh>
47 template<
class T>
class BIASMathBase_EXPORT
Vector2;
48 template<
class T>
class BIASMathBase_EXPORT
Vector3;
49 template<
class T>
class BIASMathBase_EXPORT
Vector4;
50 template<
class T>
class BIASMathBase_EXPORT
Matrix;
69 template<
class T=
double>
81 explicit Vector(
const int rows,
const std::string & s);
83 #ifdef BIAS_HAVE_CPP11
85 explicit Vector(std::initializer_list<T> l) :
Vector(std::vector<T>(l)) {}
86 #endif // BIAS_HAVE_CPP11
109 explicit Vector(
const std::vector<T>& v);
132 inline bool operator==(
const Vector<T> &vec)
const;
135 {
return !operator==(vec); }
140 {
return NormL2(); };
143 inline unsigned int Size()
const
144 {
return this->size(); };
152 void Fill(
const T & scalar);
153 inline void Set(
const T & scalar) {Fill(scalar);}
158 #ifdef BUILD_ALLOW_NON_INTEGRAL_MATRIX_ELEMENTS
161 memset(GetData(), 0, GetNumElements()*
sizeof(T));
176 inline T ScalarProduct(
const Vector<T>& argvec)
const;
182 inline void ScalarProduct(
const Vector<T>& argvec, T& result)
const;
197 inline void Multiply(
const T& scalar,
Vector<T>& res)
const;
203 { Multiply(scalar, *
this); };
208 inline void SubIP(
const Vector<T>& argmat);
220 {
return this->v_; };
227 {
return GetData() + this->size() - 1; };
233 inline T NormL1()
const;
239 inline double NormL2()
const;
245 inline double Dist(
const Vector<T> &vec)
const;
251 inline double DistLinf(
const Vector<T> &vec)
const;
264 Vector<T> SubVec(
int length,
int offset = 0)
const;
270 bool Load(
const std::string & filename);
276 bool Save(
const std::string & filename)
const;
279 void GetSTLVec(std::vector<T>& vec)
const;
280 std::vector<T> GetSTLVec()
const;
284 double GetMean(
void)
const;
292 std::ostream & operator<<(std::ostream& os, const BIAS::Vector<T>& vec);
300 BIASASSERT(this->size() == argvec.
size());
301 T* data= GetData(), *argData = argvec.
GetData();
303 for (
int i=0; i <this->size(); i++) {
304 *data = *data + *argData;
312 BIASASSERT(this->size() == argvec.
size());
313 T* data= GetData(), *argData = argvec.
GetData();
315 for (
int i=0; i <this->size(); i++) {
316 *data = *data - *argData;
326 for (
int i=0; i <this->size(); i++) {
327 *data = *data * scalar;
336 const int size=Size();
337 for (
int i=0; i<size; i++) {
338 *data = *data / scalar;
346 if (this->size() != vec.
size()) {
350 const size_t s =
sizeof(T) * this->size();
351 return (memcmp((
void *)GetData(), (
void*)vec.
GetData(), s) == 0);
358 ScalarProduct(argvec, result);
362 template <
class T>
inline void
365 BIASASSERT(this->size() == argvec.
size());
368 result = this->v_[0] * argvec[0];
369 for (
int i = 1; i < this->size(); i++) {
370 result += this->v_[i] * argvec[i];
379 for (
int i = 0; i < this->size(); i++) {
380 res[i] = this->v_[i] * scalar;
387 BIASASSERT(this->size() == argmat.
size());
389 for (
int i = 0; i < this->size(); i++) {
390 this->v_[i] = this->v_[i] - argmat[i];
397 const int size = (int)Size();
399 for (
int i = 0; i < size; i++) {
400 dest[i] = this->v_[i] - arg[i];
408 for(
register T* dataP = GetDataLast(); dataP >= GetData(); dataP--)
410 result += *dataP > 0 ? *dataP : *dataP * -1;
419 for(
register T* dataP = GetDataLast(); dataP >= GetData(); dataP--) {
420 result += (double)(*dataP * *dataP);
422 result = sqrt((
double) result);
425 #ifdef BUILD_ALLOW_NON_INTEGRAL_MATRIX_ELEMENTS
428 BIASERR(
"Not implemented for BIAS::Polynom");
437 BIASASSERT(this->size() == vec.
size());
444 result += ((*thisP)-(*vecP))*((*thisP)-(*vecP));
446 result = sqrt(result);
449 #ifdef BUILD_ALLOW_NON_INTEGRAL_MATRIX_ELEMENTS
452 BIASERR(
"Not implemented for BIAS::Polynom");
461 BIASASSERT(this->size() == vec.
size());
462 double result = 0., tmp;
469 tmp =
static_cast<double>(*thisP - *vecP);
481 #ifdef BUILD_ALLOW_NON_INTEGRAL_MATRIX_ELEMENTS
484 BIASERR(
"Not implemented for BIAS::Polynom");
494 const int size=Size();
495 BIASASSERT((
int)arg.
Size()==size);
497 for (
int i=0; i<size; i++)
498 dest[i] = (*
this)[i] * arg[i];
505 ElementwiseProduct(arg, dest);
511 std::ostream & operator<<(std::ostream& os, const BIAS::Vector<char>& vec)
513 std::ios_base::fmtflags oldFlags = os.flags();
518 os << (TNT::Vector<char>)(vec);
522 for (
int row = 0; row < vec.size(); row++){
524 os<<vec.GetData()[row]<<
" ";
535 std::ostream & operator<<(std::ostream& os, const BIAS::Vector<T>& vec)
537 std::ios_base::fmtflags oldFlags = os.flags();
542 os<<std::setprecision( BIAS::DecimalsToStore<T>() );
543 os<<(TNT::Vector<T>)(vec);
546 for (
int row = 0; row < vec.size(); row++){
547 os<<std::setprecision( CONSOLE_PRECISION_DIGITS );
548 os<<vec.GetData()[row]<<
" ";
560 #endif // _Vector_hh_
T ScalarProduct(const Vector< T > &argvec) const
scalar product (inner product) of two vectors returning a scalr
Vector< T > & operator=(const Vector< T > &A)
T * GetDataLast() const
Get the pointer to last element of the data array.
bool operator==(const Vector< T > &vec) const
class for column vectors with arbitrary size
void operator*=(T scalar)
double NormL2() const
Return the L2 norm: sqrt(a^1 + a^2 + ...)
void operator-=(const Vector< T > &vec)
class BIASMathBase_EXPORT Vector2
Vector(const TNT::Vector< T > &v)
bool IsConsoleStream(const std::ostream &os)
Helper returning true on console ostreams like cerr, cout Useful to distinguish between console (cout...
void ElementwiseProduct(const Vector< T > &arg, Vector< T > &dest) const
multiply this with arg elementwise and store the result in dest
BIASMathBase_EXPORT Vector2< T > & operator/=(Vector2< T > &vec, const Vector2< T > &argvec)
elementwise division
Vector(int rows, const T &value)
assignment with a constant value for all elements (=set)
unsigned int Size() const
length of the vector
class BIASMathBase_EXPORT Vector3
Vector2< T > & operator-=(Vector2< T > &vec, const Vector2< T > &argvec)
sub operator for two Vectors
void SetZero()
equivalent to matrix call
Vector< T > & operator=(const Vector< T > &vec)
assignment operator calling corresponding operator from base class "TNT::Vector" if appropriate ...
T NormL1() const
Return the L1 norm: |a| + |b| + |c| + ...
class BIASMathBase_EXPORT Matrix
class Vector2 contains a Vector of dim.
class Vector4 contains a Vector of dim.
Vector< T > & newsize(Subscript N)
BIASMathBase_EXPORT Vector2< T > & operator+=(Vector2< T > &vec, const Vector2< T > &argvec)
add operator for two Vectors
Vector2< T > & operator*=(Vector2< T > &vec, const T &scalar)
Multiplication operator with scalar argument.
int GetNumElements() const
conformance interface JW
void operator/=(T scalar)
double Length() const
returns the Euclidean Length of the Vector
bool operator!=(const Vector< T > &vec) const
Vector(int rows, const T *v)
double DistLinf(const Vector< T > &vec) const
Return the L infinity distance of 2 vectors.
void Set(const T &scalar)
class Vector3 contains a Vector of fixed dim.
double Dist(const Vector< T > &vec) const
Return the euclidean distance of 2 vectors.
T * GetData() const
get the pointer to the data array of the vector (for faster direct memory access) ...
matrix class with arbitrary size, indexing is row major.
void clear()
stl conform interface JW
void Multiply(const T &scalar, Vector< T > &res) const
multiply components with scalar storing result in res
void operator+=(const Vector< T > &vec)
class BIASMathBase_EXPORT Vector4
class BIASMathBase_EXPORT Vector
void SubIP(const Vector< T > &argmat)
in place subtracting
void MultiplyIP(const T &scalar)
in place multiplication with scalar
Vector(const Vector< T > &v)
void Sub(const Vector< T > &arg, Vector< T > &dest) const
Substraction of vector, storing results in destination vector.