Imagine++
Vector.h
1 // ===========================================================================
2 // Imagine++ Libraries
3 // Copyright (C) Imagine
4 // For detailed information: http://imagine.enpc.fr/software
5 // ===========================================================================
6 
7 namespace Imagine {
10 
11  template <typename T> class Matrix;
12  template <typename T> class SymMatrix;
13 
22 
23  template <typename T>
24  class Vector : public Array<T> {
25  typedef Array<T> Base;
26  public:
28  typedef typename Base::iterator iterator;
36  Vector() : Base() {}
43  explicit Vector(size_t n) : Base(n) {}
50  Vector(const Base& v) : Base(v) {}
62  Vector(T* ptr, size_t n,bool handleDelete=false):Base(ptr,n,handleDelete) {}
65  ~Vector() {}
73  Vector& operator=(const Vector& v) { Base::operator=(v); return *this; }
80  Vector clone() const
81  {
82  Vector v(this->size());
83  equalize((int)this->size(),this->data(),1,v.data(),1);
84  return v;
85  }
93  Vector& fill(T x) { Base::fill(x); return *this; }
101  static Vector Zero(size_t n) {
102  Vector Z(n);
103  memset(Z.data(),0,n*sizeof(T));
104  return Z;
105  }
114  Vector getSubVect(size_t offset, size_t size) const {
115  return Vector(Base::getSubArray(offset,size));
116  }
126  Vector getSubVectRef(size_t offset, size_t size) {
127  assert(offset+size<=this->size());
128  return Vector(this->data()+offset,size);
129  }
137  Vector operator+(const Vector& v) const {
138  assert(this->size()==v.size());
139  Vector p=this->clone();
140  return (p+=v);
141  }
149  Vector& operator+=(const Vector& v) {
150  assert(this->size()==v.size());
151  combine((int)this->size(), 1, v.data(), this->data());
152  return *this;
153  }
161  Vector operator-(const Vector& v) const {
162  assert(this->size()==v.size());
163  Vector p=this->clone();
164  return (p-=v);
165  }
173  Vector& operator-=(const Vector& v) {
174  assert(this->size()==v.size());
175  combine((int)this->size(), -1, v.data(), this->data());
176  return *this;
177  }
185  Vector operator+(T x) const {
186  Vector p=this->clone();
187  return (p+=x);
188  }
197  for (iterator it = this->begin() ; it != this->end() ; ++it)
198  *it+=x;
199  return *this;
200  }
208  Vector operator-(T x) const {
209  Vector p=this->clone();
210  return (p-=x);
211  }
220  for (iterator it = this->begin() ; it != this->end() ; ++it)
221  *it-=x;
222  return *this;
223  }
230  Vector operator - () const {
231  return (*this)*T(-1);
232  }
240  Vector operator*(T x) const {
241  Vector p=this->clone();
242  return (p*=x);
243  }
252  multiply((int)this->size(), x, this->data());
253  return *this;
254  }
262  Vector operator/(T x) const {
263  return (*this)*(1/x);
264  }
273  (*this)*=(1/x);
274  return *this;
275  }
284  friend inline Vector operator * (T x, const Vector& v) {return v*x;}
293  friend inline Vector operator + (T x, const Vector& v) {return v+x;}
302  friend inline Vector operator - (T x, const Vector& v) {return (-v)+x;}
310  T operator*(const Vector& v) const {
311  assert(this->size()==v.size());
312  return scalarProduct((int)this->size(), this->data(), v.data());
313  }
321  friend T norm(const Vector& v) {
322  return squaredNorm((int) v.size(), v.data());
323  }
331  friend T norm2(const Vector& v) {
332  return norm(v)*norm(v);
333  }
341  T n=norm(*this);
342  if (n!=0) return operator /= (n);
343  return *this;
344  }
352  friend inline Vector normalized(const Vector& v) {
353  T n=norm(v);
354  assert(n!=0);
355  return v/n;
356  }
364  friend inline T maxNorm(const Vector& v) {
365  if (v.empty()) return 0;
366  const_iterator it = v.begin();
367  T mx=std::abs(*it);
368  for ( ++it ; it != v.end() ; ++it) {
369  T a = std::abs(*it);
370  if (a>mx)
371  mx=a;
372  }
373  return mx;
374  }
382  friend inline T sum(const Vector& v) {
383  return std::accumulate(v.begin(),v.end(),T(0));
384  }
392  friend inline T mean(const Vector& v) {
393  return sum(v)/v.size();
394  }
403  friend Vector convolution(const Vector& K,const Vector& v) {
404  if (v.size()<K.size()) return convolution(v,K);
405  Vector p(K.size()+v.size()-1);
406  p.fill(T(0));
407  for (size_t i=0; i<v.size(); i++) {
408  combine((int)K.size(), v[i], K.data(), p.data()+i);
409  }
410  return p;
411  }
421  friend Vector truncConvolution(const Vector& K,const Vector& v, size_t n=0) {
422  assert(n<K.size());
423  Vector p(v.size());
424  p.fill(T(0));
425  for (size_t i=0; i<v.size(); i++) {
426  int start = std::max((int)(0),(int)(n)-(int)(i));
427  int end = std::min((int)(K.size()),(int)(v.size()+n)-(int)(i));
428  combine((end - start), v[i], K.data()+start, p.data() + start + (i - n));
429  }
430  return p;
431  }
439  friend inline T correlation(const Vector& V1,const Vector& V2) {
440  Vector v1=V1-mean(V1);
441  Vector v2=V2-mean(V2);
442  return v1*v2;
443  }
451  friend inline T normalizedCorrelation(const Vector& V1,const Vector& V2) {// Non optimized version
452  T c1=correlation(V1,V1);
453  if (c1==0)
454  return 0;
455  T c2=correlation(V2,V2);
456  if (c2==0)
457  return 0;
458  return correlation(V1,V2)/::sqrt(c1*c2);
459  }
467  friend inline double dist(const Vector& V1,const Vector& V2) {
468  double d=0;
469  for (size_t i=0;i<V1.size();i++) {
470  double e=double(V1[i])-double(V2[i]);
471  d+=e*e;
472  }
473  return ::sqrt(d);
474  }
475  };
476 
477 
479 }
Vector operator/(T x) const
Scalar division.
Definition: Vector.h:262
Vector & fill(T x)
Filling.
Definition: Vector.h:93
Vector(const Base &v)
Copy constructor.
Definition: Vector.h:50
Vector clone() const
Cloning.
Definition: Vector.h:80
Vector of variable size.
Definition: Vector.h:24
Vector()
Empty constructor.
Definition: Vector.h:36
friend T norm2(const Vector &v)
Squared Euclidean norm.
Definition: Vector.h:331
Vector & operator=(const Vector &v)
Assignment.
Definition: Vector.h:73
Vector operator+(T x) const
Scalar Addition.
Definition: Vector.h:185
Vector operator+(const Vector &v) const
Addition.
Definition: Vector.h:137
friend T normalizedCorrelation(const Vector &V1, const Vector &V2)
Normalized Correlation.
Definition: Vector.h:451
Vector getSubVectRef(size_t offset, size_t size)
Sub vector by reference.
Definition: Vector.h:126
Vector operator-() const
Opposite.
Definition: Vector.h:230
Array of variable size.
Definition: Array.h:20
Array & operator=(const Array &A)
Assignment.
Definition: Array.h:154
friend T sum(const Vector &v)
Sum of values.
Definition: Vector.h:382
size_t size() const
Size.
Definition: Array.h:194
friend Vector truncConvolution(const Vector &K, const Vector &v, size_t n=0)
Truncated Convolution.
Definition: Vector.h:421
Vector(T *ptr, size_t n, bool handleDelete=false)
Constructor (pre-allocated).
Definition: Vector.h:62
Vector operator-(T x) const
Scalar Substraction.
Definition: Vector.h:208
friend Vector normalized(const Vector &v)
Euclidean normalization.
Definition: Vector.h:352
Base::const_iterator const_iterator
Const iterator type.
Definition: Vector.h:30
Vector operator*(T x) const
Scalar multiplication.
Definition: Vector.h:240
~Vector()
Destructor.
Definition: Vector.h:65
T * data()
Data pointer (read/write).
Definition: Array.h:217
Vector & normalize()
Euclidean in-place normalization.
Definition: Vector.h:340
Vector getSubVect(size_t offset, size_t size) const
Sub vector.
Definition: Vector.h:114
bool empty() const
Is empty.
Definition: Array.h:187
friend Vector convolution(const Vector &K, const Vector &v)
Convolution.
Definition: Vector.h:403
friend T norm(const Vector &v)
Euclidean norm.
Definition: Vector.h:321
Vector(size_t n)
Constructor (known size).
Definition: Vector.h:43
Base::iterator iterator
Iterator type.
Definition: Vector.h:28
friend T mean(const Vector &v)
Mean of values.
Definition: Vector.h:392
friend double dist(const Vector &V1, const Vector &V2)
Distance.
Definition: Vector.h:467
T operator*(const Vector &v) const
Scalar product.
Definition: Vector.h:310
Vector & operator+=(const Vector &v)
In place Addition.
Definition: Vector.h:149
friend T correlation(const Vector &V1, const Vector &V2)
Correlation.
Definition: Vector.h:439
Vector & operator+=(T x)
Scalar in place Addition.
Definition: Vector.h:196
Vector & operator/=(T x)
Scalar in place division.
Definition: Vector.h:272
Vector & operator-=(T x)
Scalar in place substractrion.
Definition: Vector.h:219
Array & fill(const T &x)
Filling.
Definition: Array.h:276
Array getSubArray(size_t offset, size_t size) const
Sub array.
Definition: Array.h:262
Vector operator-(const Vector &v) const
Substraction.
Definition: Vector.h:161
Vector & operator-=(const Vector &v)
In place Substraction.
Definition: Vector.h:173
iterator begin()
Begin iterator.
Definition: Array.h:231
Imagine++ namespace.
Definition: Array.h:7
RED GREEN BLUE color.
Definition: Color.h:26
const T * const_iterator
Const iterator type.
Definition: Array.h:73
static Vector Zero(size_t n)
Zero vector.
Definition: Vector.h:101
iterator end()
End iterator.
Definition: Array.h:245
Vector & operator*=(T x)
Scalar in place multiplication.
Definition: Vector.h:251
friend T maxNorm(const Vector &v)
Maximum norm.
Definition: Vector.h:364