Basic Image AlgorithmS Library  2.8.0
 All Classes Namespaces Functions Variables Typedefs Enumerations Enumerator Friends Groups Pages
Vector.hh
1 /*
2 This file is part of the BIAS library (Basic ImageAlgorithmS).
3 
4 Copyright (C) 2003-2009 (see file CONTACT for details)
5 Multimediale Systeme der Informationsverarbeitung
6 Institut fuer Informatik
7 Christian-Albrechts-Universitaet Kiel
8 
9 
10 BIAS is free software; you can redistribute it and/or modify
11 it under the terms of the GNU Lesser General Public License as published by
12 the Free Software Foundation; either version 2.1 of the License, or
13 (at your option) any later version.
14 
15 BIAS is distributed in the hope that it will be useful,
16 but WITHOUT ANY WARRANTY; without even the implied warranty of
17 MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
18 GNU Lesser General Public License for more details.
19 
20 You should have received a copy of the GNU Lesser General Public License
21 along with BIAS; if not, write to the Free Software
22 Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA 02111-1307 USA
23 */
24 
25 
26 #ifndef _Vector_hh_
27 #define _Vector_hh_
28 
29 #include "bias_config.h"
30 #include <Base/Common/W32Compat.hh>
31 #include "math.h"
32 
33 #include <string>
34 #include <vector>
35 #ifdef BIAS_HAVE_CPP11
36 #include <initializer_list>
37 #endif // BIAS_HAVE_CPP11
38 
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>
43 
44 namespace BIAS {
45 
46  // forward declaration to avoid mutual inclusion
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;
51 
52  /**@class Vector
53  @ingroup g_math
54  @brief class for column vectors with arbitrary size
55 
56  class Vector is the vector class that should be used by common
57  BIAS algorithms and data structures.
58  It is derived from TNT::Vector to inherit the basic operations and
59  algorithm.
60  Special implementations should be done here and NOT in TNT:Vector
61  because the true 'base' Matrix class "TNT::Vector"
62  should be interchangeable and aside from this maintained by NIST.
63 
64  changed from BIAS to BIAS 11/05/2002 (jw)
65 
66  The Vector is in row-major order (=n rows, 1 column)
67  @author Jan Woetzel
68  **/
69  template<class T=double>
70  class BIASMathBase_EXPORT Vector : public TNT::Vector<T>
71  {
72  public:
73  typedef T value_type;
74 
75  virtual ~Vector();
76 
77  Vector() :TNT::Vector<T>() {};
78 
79  explicit Vector(const int rows) : TNT::Vector<T>(rows) {};
80 
81  explicit Vector(const int rows, const std::string & s);
82 
83 #ifdef BIAS_HAVE_CPP11
84  /// Initializer list constructor
85  explicit Vector(std::initializer_list<T> l) : Vector(std::vector<T>(l)) {}
86 #endif // BIAS_HAVE_CPP11
87 
88  /** assignment with a constant value for all elements (=set)
89  @author Jan Woetzel
90  */
91  Vector(int rows, const T & value) :TNT::Vector<T>(rows, value) {};
92 
93  Vector(int rows, const T * v) :TNT::Vector<T>(rows, v) {};
94 
95  Vector(const Vector<T>& v) :TNT::Vector<T>(v) {};
96 
97  /** @author JMF */
98  Vector(const TNT::Vector<T> &v) :TNT::Vector<T>(v) {};
99 
100  /** @author fw */
101  Vector(const Vector2<T>& v);
102 
103  /** @author fw */
104  Vector(const Vector3<T>& v);
105 
106  /** @author fw */
107  Vector(const Vector4<T>& v);
108 
109  explicit Vector(const std::vector<T>& v);
110 
111  /** assignment operators
112  calling corresponding operator from base class "TNT::Vector" if
113  appropriate
114  @author Jan Woetzel (02/25/2002) **/
115  Vector<T>& operator=(const TNT::Vector<T>& vec);
116 
117  /** assignment operator
118  calling corresponding operator from base class "TNT::Vector" if
119  appropriate @author grest */
121  { TNT::Vector<T>::operator=(vec); return *this; }
122 
123  inline void operator+=(const Vector<T>& vec);
124 
125  inline void operator-=(const Vector<T>& vec);
126 
127  inline void operator*=(T scalar);
128 
129  /** @author woelk 12/2004 */
130  inline void operator/=(T scalar);
131 
132  inline bool operator==(const Vector<T> &vec) const;
133 
134  inline bool operator!=(const Vector<T> &vec) const
135  { return !operator==(vec); }
136 
137  /** @brief returns the Euclidean Length of the Vector
138  */
139  inline double Length() const
140  { return NormL2(); };
141 
142  /** length of the vector */
143  inline unsigned int Size() const
144  { return this->size(); };
145 
146  /** conformance interface JW */
147  inline int GetNumElements() const {
148  return this->size();
149  };
150 
151  /** fills complete Vector with scalar value */
152  void Fill(const T & scalar);
153  inline void Set(const T & scalar) {Fill(scalar);}
154 
155  /** equivalent to matrix call */
156  inline void SetZero()
157  {
158 #ifdef BUILD_ALLOW_NON_INTEGRAL_MATRIX_ELEMENTS
159  Fill((T)(0));
160 #else
161  memset(GetData(), 0, GetNumElements()*sizeof(T)); // JW
162 #endif
163  };
164 
165  /// stl conform interface JW
166  inline void clear() {
167  this->destroy();
168  };
169 
170  /** @return true if all elements are equal zero JW */
171  bool IsZero() const;
172 
173  /** scalar product (inner product) of two vectors returning a scalr
174  @author Ingo Thomsen
175  @status tested(03/11/2002) **/
176  inline T ScalarProduct(const Vector<T>& argvec) const;
177 
178  /** scalar product (inner product) of two vectors returning a scalar,
179  storing the result in result
180  @author Ingo Thomsen
181  @status tested(03/11/2002) **/
182  inline void ScalarProduct(const Vector<T>& argvec, T& result) const;
183 
184  /** outer product, constructs a matrix.
185  Often written as v * v^T for col vectors
186  @author Daniel Grest, Oct 2002
187  @status tested */
188  Matrix<T> OuterProduct(const Vector<T> &v) const;
189 
190  /** kronecker product
191  @author woelk 08/2004 */
192  void KroneckerProduct(const Vector<T> &arg, Vector<T>& dst) const;
193 
194  /** multiply components with scalar storing result in res
195  @author Ingo Thomsen
196  @status tested(03/11/2002) **/
197  inline void Multiply(const T& scalar, Vector<T>& res) const;
198 
199  /** in place multiplication with scalar
200  @author Ingo Thomsen
201  @status untested **/
202  inline void MultiplyIP(const T& scalar)
203  { Multiply(scalar, *this); };
204 
205  /** in place subtracting
206  @author Ingo Thomsen
207  @status untested **/
208  inline void SubIP(const Vector<T>& argmat);
209 
210  /** Substraction of vector, storing results in destination vector
211  @author woelk 07/2006 **/
212  inline void Sub(const Vector<T>& arg, Vector<T>& dest) const;
213 
214  /** get the pointer to the data array of the vector
215  (for faster direct memory access)
216  @author Jan Woetzel
217  @return a pointer to the data array (beginning with GetData[0])
218  -added const (jw. 03/12/2002) **/
219  inline T *GetData() const
220  { return this->v_; };
221 
222  /** Get the pointer to last element of the data array
223  @author Ingo Thomsen
224  @date 04/11/2002
225  @return pointer to last element **/
226  inline T *GetDataLast() const
227  { return GetData() + this->size() - 1; };
228 
229  /** Return the L1 norm: |a| + |b| + |c| + ...
230  @author Ingo Thomsen
231  @date 04/11/2002
232  **/
233  inline T NormL1() const;
234 
235  /** Return the L2 norm: sqrt(a^1 + a^2 + ...)
236  @author Ingo Thomsen
237  @date 04/11/2002
238  **/
239  inline double NormL2() const;
240 
241  /** Return the euclidean distance of 2 vectors.
242  @author Birger Streckel
243  @date 08/2002
244  **/
245  inline double Dist(const Vector<T> &vec) const;
246 
247  /** Return the L infinity distance of 2 vectors.
248  @author djung
249  @date 2009/07 untested
250  **/
251  inline double DistLinf(const Vector<T> &vec) const;
252 
253  /** multiply this with arg elementwise and store the result in dest
254  @author woelk 12/2004 */
255  inline void ElementwiseProduct(const Vector<T> &arg,Vector<T> &dest) const;
256 
257  /** multiply this with arg elementwise and return the result
258  @author woelk 12/2004 */
259  inline Vector<T> ElementwiseProduct(const Vector<T> &arg) const;
260 
261  /** returns the subvector with elements between offset and offset+length-1
262  @author Dennis Herzog
263  @date 2005-07-29 */
264  Vector<T> SubVec(int length, int offset = 0) const;
265 
266  /** method to load directly from a given filename.
267  internally using stream operator
268  @author Jan Woetzel 09/2005
269  @return false in case of error, true in case of success */
270  bool Load(const std::string & filename);
271 
272  /** method to save directly to a given filename.
273  internally using stream operator
274  @author Jan Woetzel 09/2009
275  @return false in case of error, true in case of success */
276  bool Save(const std::string & filename) const;
277 
278  /** @author woelk 11/2007 (c) www.vision-n.de */
279  void GetSTLVec(std::vector<T>& vec) const;
280  std::vector<T> GetSTLVec() const;
281 
282  /** @author Ove Schimmer, www.sequid.com
283  @date 2008-07-17 */
284  double GetMean(void) const;
285 
286  }; // class ===============================================================
287 
288  /** @relates Vector
289  Output with streams
290  @author woelk 10/2004 major corrections by Jan Woetzel/Sun Min 11/2004 */
291  template <class T>
292  std::ostream & operator<<(std::ostream& os, const BIAS::Vector<T>& vec);
293 
294 
295  // ==== implementation: ==========================================
296 
297  template <class T>
298  inline void Vector<T>::operator+=(const Vector<T> &argvec)
299  {
300  BIASASSERT(this->size() == argvec.size());
301  T* data= GetData(), *argData = argvec.GetData();
302 
303  for (int i=0; i <this->size(); i++) {
304  *data = *data + *argData;
305  data++; argData++;
306  }
307  }
308 
309  template <class T>
310  inline void Vector<T>::operator-=(const Vector<T> &argvec)
311  {
312  BIASASSERT(this->size() == argvec.size());
313  T* data= GetData(), *argData = argvec.GetData();
314 
315  for (int i=0; i <this->size(); i++) {
316  *data = *data - *argData;
317  data++; argData++;
318  }
319  }
320 
321  template <class T>
322  inline void Vector<T>::operator*=(T scalar)
323  {
324  T* data= GetData();
325 
326  for (int i=0; i <this->size(); i++) {
327  *data = *data * scalar;
328  data++;
329  }
330  }
331 
332  template <class T>
333  inline void Vector<T>::operator/=(T scalar)
334  {
335  T* data= GetData();
336  const int size=Size();
337  for (int i=0; i<size; i++) {
338  *data = *data / scalar;
339  data++;
340  }
341  }
342 
343  template <class T>
344  inline bool Vector<T>::operator==(const Vector<T> &vec) const
345  {
346  if (this->size() != vec.size()) {
347  return false;
348  }
349 
350  const size_t s = sizeof(T) * this->size();
351  return (memcmp((void *)GetData(), (void*)vec.GetData(), s) == 0);
352  }
353 
354  template <class T>
355  inline T Vector<T>::ScalarProduct(const Vector < T > &argvec) const
356  {
357  T result;
358  ScalarProduct(argvec, result);
359  return result;
360  }
361 
362  template <class T> inline void
363  Vector<T>::ScalarProduct(const Vector<T> &argvec, T & result) const
364  {
365  BIASASSERT(this->size() == argvec.size());
366  // initialized with the first product, so no initialization
367  // with 0 is necessary
368  result = this->v_[0] * argvec[0];
369  for (int i = 1; i < this->size(); i++) {
370  result += this->v_[i] * argvec[i];
371  }
372  }
373 
374 
375 
376  template <class T>
377  inline void Vector<T>::Multiply(const T& scalar, Vector<T>& res) const
378  {
379  for (int i = 0; i < this->size(); i++) {
380  res[i] = this->v_[i] * scalar;
381  }
382  }
383 
384  template <class T>
385  inline void Vector<T>::SubIP(const Vector<T>& argmat)
386  {
387  BIASASSERT(this->size() == argmat.size());
388 
389  for (int i = 0; i < this->size(); i++) {
390  this->v_[i] = this->v_[i] - argmat[i];
391  }
392  }
393 
394  template <class T>
395  void Vector<T>::Sub(const Vector<T> &arg, Vector<T> &dest) const
396  {
397  const int size = (int)Size();
398  dest.newsize(size);
399  for (int i = 0; i < size; i++) {
400  dest[i] = this->v_[i] - arg[i];
401  }
402  }
403 
404  template <class T>
405  inline T Vector<T>::NormL1() const
406  {
407  T result = 0;
408  for(register T* dataP = GetDataLast(); dataP >= GetData(); dataP--)
409  {
410  result += *dataP > 0 ? *dataP : *dataP * -1;
411  }
412  return result;
413  }
414 
415  template <class T>
416  inline double Vector<T>::NormL2() const
417  {
418  double result = 0;
419  for(register T* dataP = GetDataLast(); dataP >= GetData(); dataP--) {
420  result += (double)(*dataP * *dataP);
421  }
422  result = sqrt((double) result);
423  return result;
424  }
425 #ifdef BUILD_ALLOW_NON_INTEGRAL_MATRIX_ELEMENTS
426  template <>
427  inline double Vector<BIAS::Polynom>::NormL2() const {
428  BIASERR("Not implemented for BIAS::Polynom");
429  BIASABORT;
430  return 0;
431  }
432 #endif
433 
434  template <class T>
435  inline double Vector<T>::Dist(const Vector<T> &vec) const
436  {
437  BIASASSERT(this->size() == vec.size());
438  double result = 0;
439  register T* thisP;
440  register T* vecP;
441  for(thisP=GetDataLast(), vecP=vec.GetDataLast();
442  thisP >= GetData();
443  thisP--, vecP--) {
444  result += ((*thisP)-(*vecP))*((*thisP)-(*vecP));
445  }
446  result = sqrt(result);
447  return result;
448  }
449 #ifdef BUILD_ALLOW_NON_INTEGRAL_MATRIX_ELEMENTS
450  template <>
451  inline double Vector<BIAS::Polynom>::Dist(const Vector<BIAS::Polynom> &vec) const {
452  BIASERR("Not implemented for BIAS::Polynom");
453  BIASABORT;
454  return 0;
455  }
456 #endif
457 
458  template <class T>
459  inline double Vector<T>::DistLinf(const Vector<T> &vec) const
460  {
461  BIASASSERT(this->size() == vec.size());
462  double result = 0., tmp;
463  register T* thisP;
464  register T* vecP;
465  for(thisP=GetDataLast(), vecP=vec.GetDataLast();
466  thisP >= GetData();
467  thisP--, vecP--)
468  {
469  tmp = static_cast<double>(*thisP - *vecP);
470  if(tmp < 0.)
471  {// abs(tmp)
472  tmp = -tmp;
473  }
474  if(result < tmp)
475  {// max(tmp, result)
476  result = tmp;
477  }
478  }
479  return result;
480  }
481 #ifdef BUILD_ALLOW_NON_INTEGRAL_MATRIX_ELEMENTS
482  template <>
483  inline double Vector<BIAS::Polynom>::DistLinf(const Vector<BIAS::Polynom> &vec) const {
484  BIASERR("Not implemented for BIAS::Polynom");
485  BIASABORT;
486  return 0;
487  }
488 #endif
489 
490  template <class T>
491  inline void Vector<T>::ElementwiseProduct(const Vector<T> &arg,
492  Vector<T> &dest) const
493  {
494  const int size=Size();
495  BIASASSERT((int)arg.Size()==size);
496  dest.newsize(size);
497  for (int i=0; i<size; i++)
498  dest[i] = (*this)[i] * arg[i];
499  }
500 
501  template <class T>
503  {
504  Vector<T> dest;
505  ElementwiseProduct(arg, dest);
506  return dest;
507  }
508 
509 
510  inline
511  std::ostream & operator<<(std::ostream& os, const BIAS::Vector<char>& vec)
512  {
513  std::ios_base::fmtflags oldFlags = os.flags(); // push
514 
515  if (!IsConsoleStream(os) || (vec.size()>5))
516  {
517  // not cout and not cerr, use default operator from TNT (printing trailing dimension)
518  os << (TNT::Vector<char>)(vec);
519 
520  } else { // cout or cerr
521  os << "[ ";
522  for (int row = 0; row < vec.size(); row++){
523  os<<std::setw(3);
524  os<<vec.GetData()[row]<< " ";
525  }
526  os << "] ";
527  }
528 
529  os.flags(oldFlags); // pop
530  return os;
531  }
532 
533  template <class T>
534  inline
535  std::ostream & operator<<(std::ostream& os, const BIAS::Vector<T>& vec)
536  {
537  std::ios_base::fmtflags oldFlags = os.flags(); // push
538 
539  if (!IsConsoleStream(os) || (vec.size()>5))
540  {
541  // not cout and not cerr, use default operator from TNT (printing trailing dimension)
542  os<<std::setprecision( BIAS::DecimalsToStore<T>() );
543  os<<(TNT::Vector<T>)(vec);
544  } else { // cout or cerr
545  os << "[ ";
546  for (int row = 0; row < vec.size(); row++){
547  os<<std::setprecision( CONSOLE_PRECISION_DIGITS ); // less precise display in console
548  os<<vec.GetData()[row]<< " ";
549  }
550  os << "] ";
551  }
552 
553  os.flags(oldFlags); // pop
554  return os;
555  }
556 
557 
558 
559 } // namespace BIAS
560 #endif // _Vector_hh_
561 
T ScalarProduct(const Vector< T > &argvec) const
scalar product (inner product) of two vectors returning a scalr
Definition: Vector.hh:355
Vector< T > & operator=(const Vector< T > &A)
Definition: vec.h:233
T * GetDataLast() const
Get the pointer to last element of the data array.
Definition: Vector.hh:226
bool operator==(const Vector< T > &vec) const
Definition: Vector.hh:344
class for column vectors with arbitrary size
void operator*=(T scalar)
Definition: Vector.hh:322
double NormL2() const
Return the L2 norm: sqrt(a^1 + a^2 + ...)
Definition: Vector.hh:416
void operator-=(const Vector< T > &vec)
Definition: Vector.hh:310
class BIASMathBase_EXPORT Vector2
Definition: Matrix2x2.hh:45
Vector(const TNT::Vector< T > &v)
Definition: Vector.hh:98
bool IsConsoleStream(const std::ostream &os)
Helper returning true on console ostreams like cerr, cout Useful to distinguish between console (cout...
Definition: Utils.hh:54
void ElementwiseProduct(const Vector< T > &arg, Vector< T > &dest) const
multiply this with arg elementwise and store the result in dest
Definition: Vector.hh:491
BIASMathBase_EXPORT Vector2< T > & operator/=(Vector2< T > &vec, const Vector2< T > &argvec)
elementwise division
Definition: Vector2.hh:846
Vector(int rows, const T &value)
assignment with a constant value for all elements (=set)
Definition: Vector.hh:91
unsigned int Size() const
length of the vector
Definition: Vector.hh:143
class BIASMathBase_EXPORT Vector3
Definition: Operators.hh:44
Vector2< T > & operator-=(Vector2< T > &vec, const Vector2< T > &argvec)
sub operator for two Vectors
Definition: Vector2.hh:839
void SetZero()
equivalent to matrix call
Definition: Vector.hh:156
Vector< T > & operator=(const Vector< T > &vec)
assignment operator calling corresponding operator from base class &quot;TNT::Vector&quot; if appropriate ...
Definition: Vector.hh:120
T NormL1() const
Return the L1 norm: |a| + |b| + |c| + ...
Definition: Vector.hh:405
class BIASMathBase_EXPORT Matrix
Definition: Operators.hh:37
class Vector2 contains a Vector of dim.
Definition: Vector2.hh:79
class Vector4 contains a Vector of dim.
Definition: Vector4.hh:65
Vector< T > & newsize(Subscript N)
Definition: vec.h:220
T value_type
Definition: Vector.hh:73
BIASMathBase_EXPORT Vector2< T > & operator+=(Vector2< T > &vec, const Vector2< T > &argvec)
add operator for two Vectors
Definition: Vector2.hh:830
Vector2< T > & operator*=(Vector2< T > &vec, const T &scalar)
Multiplication operator with scalar argument.
Definition: Vector2.hh:873
int GetNumElements() const
conformance interface JW
Definition: Vector.hh:147
void operator/=(T scalar)
Definition: Vector.hh:333
double Length() const
returns the Euclidean Length of the Vector
Definition: Vector.hh:139
bool operator!=(const Vector< T > &vec) const
Definition: Vector.hh:134
Vector(int rows, const T *v)
Definition: Vector.hh:93
double DistLinf(const Vector< T > &vec) const
Return the L infinity distance of 2 vectors.
Definition: Vector.hh:459
void Set(const T &scalar)
Definition: Vector.hh:153
class Vector3 contains a Vector of fixed dim.
Definition: Matrix.hh:53
Vector(const int rows)
Definition: Vector.hh:79
double Dist(const Vector< T > &vec) const
Return the euclidean distance of 2 vectors.
Definition: Vector.hh:435
T * GetData() const
get the pointer to the data array of the vector (for faster direct memory access) ...
Definition: Vector.hh:219
matrix class with arbitrary size, indexing is row major.
void clear()
stl conform interface JW
Definition: Vector.hh:166
void Multiply(const T &scalar, Vector< T > &res) const
multiply components with scalar storing result in res
Definition: Vector.hh:377
void operator+=(const Vector< T > &vec)
Definition: Vector.hh:298
class BIASMathBase_EXPORT Vector4
Definition: Operators.hh:46
class BIASMathBase_EXPORT Vector
void SubIP(const Vector< T > &argmat)
in place subtracting
Definition: Vector.hh:385
Subscript size() const
Definition: vec.h:262
void MultiplyIP(const T &scalar)
in place multiplication with scalar
Definition: Vector.hh:202
Vector(const Vector< T > &v)
Definition: Vector.hh:95
void Sub(const Vector< T > &arg, Vector< T > &dest) const
Substraction of vector, storing results in destination vector.
Definition: Vector.hh:395