Imagine++
FVector.h
1// ===========================================================================
2// Imagine++ Libraries
3// Copyright (C) Imagine
4// For detailed information: http://imagine.enpc.fr/software
5// ===========================================================================
6
7namespace Imagine {
10
11
17 template <typename T,int dim> class FVector : public FArray<T,dim> {
18 typedef FArray<T,dim> Base;
19 public:
32 explicit FVector(const T& v) : Base(v) {}
41 template <typename T2> FVector(const T2 t[dim]) : Base(t) {}
50 template <typename T2> FVector(const FArray<T2,dim>& a) : Base(a) {}
58 FVector(T x, T y) : Base(x,y) {}
67 FVector(T x, T y, T z) : Base(x,y,z) {}
75 FVector& fill(const T& v) { Base::fill(v); return *this; }
82 static FVector Zero() {
83 FVector Z;
84 memset(Z._data,0,dim*sizeof(T));
85 return Z;
86 }
96 template <typename T2>
97 FVector& operator = (const FArray<T2,dim>& b) { Base::operator=(b); return *this; }
104 const T& x() const { assert(dim>=1); return (*this)[0]; }
111 T& x() { assert(dim>=1); return (*this)[0]; }
118 const T& y() const { assert(dim>=2); return (*this)[1]; }
125 T& y() { assert(dim>=2); return (*this)[1]; }
132 const T& z() const { assert(dim>=3); return (*this)[2]; }
139 T& z() { assert(dim>=3); return (*this)[2]; }
147 FVector operator + (const FVector& v) const {
148 FVector w;
149 for (int i=0;i<dim;i++)
150 w[i]=(*this)[i]+v[i];
151 return w;
152 }
160 FVector operator - (const FVector& v) const {
161 FVector w;
162 for (int i=0;i<dim;i++)
163 w[i]=(*this)[i]-v[i];
164 return w;
165 }
174 for (int i=0;i<dim;i++)
175 (*this)[i] += v[i];
176 return *this;
177 }
186 for (int i=0;i<dim;i++)
187 (*this)[i] -= v[i];
188 return *this;
189 }
197 FVector operator + (T s) const {
198 FVector w;
199 for (int i=0;i<dim;i++)
200 w[i]=(*this)[i]+s;
201 return w;
202 }
210 FVector operator - (T s) const {
211 FVector w;
212 for (int i=0;i<dim;i++)
213 w[i]=(*this)[i]-s;
214 return w;
215 }
224 for (int i=0;i<dim;i++)
225 (*this)[i] += s;
226 return *this;
227 }
236 for (int i=0;i<dim;i++)
237 (*this)[i] -= s;
238 return *this;
239 }
247 FVector w;
248 for (int i=0;i<dim;i++)
249 w[i]=-(*this)[i];
250 return w;
251 }
259 T operator * (const FVector& v) const {
260 T s=0;
261 for (int i=0;i<dim;i++)
262 s+=(*this)[i]*v[i];
263 return s;
264 }
272 T operator ^ (const FVector<T,2>& v) const {
273 assert(dim==2);
274 return (*this)[0]*v[1]-(*this)[1]*v[0];
275 }
284 assert(dim==3);
285 FVector<T,3> w;
286 w[0]=(*this)[1]*v[2]-(*this)[2]*v[1];
287 w[1]=(*this)[2]*v[0]-(*this)[0]*v[2];
288 w[2]=(*this)[0]*v[1]-(*this)[1]*v[0];
289 return w;
290 }
298 FVector operator*(T s) const {
299 FVector v;
300 for (int i=0;i<dim;i++)
301 v[i]=(*this)[i]*s;
302 return v;
303 }
311 FVector operator/(T s) const {
312 FVector v;
313 for (int i=0;i<dim;i++)
314 v[i]=(*this)[i]/s;
315 return v;
316 }
325 for (int i=0;i<dim;i++)
326 (*this)[i] *= s;
327 return *this;
328 }
337 for (int i=0;i<dim;i++)
338 (*this)[i] /= s;
339 return *this;
340 }
349 friend inline FVector operator*(T s,const FVector& v) {
350 return v*s;
351 }
360 friend inline FVector operator+(T s,const FVector& v) {
361 return v+s;
362 }
371 friend inline FVector operator-(T s,const FVector& v) {
372 return (-v)+s;
373 }
381 friend inline T norm2(const FVector& v) {
382 T n=0;
383 for (int i=0;i<dim;i++)
384 n+=v[i]*v[i];
385 return n;
386 }
394 friend inline T norm(const FVector& v) {
395 assert( !std::numeric_limits<T>::is_integer );
396 return ::sqrt(norm2(v));
397 }
405 T n=norm(*this);
406 if (n!=0) return operator /= (n);
407 return *this;
408 }
416 friend inline FVector normalized(const FVector& v) {
417 T n=norm(v);
418 assert(n!=0);
419 return v/n;
420 }
428 friend inline T maxNorm(const FVector& v) {
429 T mx = std::abs(v[0]);
430 for (int i=0;i<dim;i++){
431 T a = std::abs(v[i]);
432 if (a>mx) mx = a;
433 }
434 return mx;
435 }
443 friend inline int intNorm1(const FVector & v)
444 {
445 assert( std::numeric_limits<T>::is_integer );
446 int n = 0;
447 for(int i=0; i < dim; ++i)
448 n+= abs(v[i]);
449 return n;
450 }
458 friend inline int intNorm2(const FVector& v) {
459 assert( std::numeric_limits<T>::is_integer );
460 int n=0;
461 for (int i=0;i<dim;i++)
462 n+=int(v[i])*int(v[i]);
463 return n;
464 }
472 friend inline double doubleNorm1(const FVector& v) {
473 double d=0;
474 for (int i=0;i<dim;++i)
475 d+=fabs(double(v[i]));
476 return d;
477 }
485 friend inline double doubleNorm2(const FVector& v) {
486 double n=0;
487 for (int i=0;i<dim;i++)
488 n+=double(v[i])*double(v[i]);
489 return n;
490 }
498 friend inline double doubleNorm(const FVector& v) {
499 return ::sqrt(doubleNorm2(v));
500 }
508 friend inline T sum(const FVector& v) {
509 T n=0;
510 for (int i=0;i<dim;i++)
511 n+=v[i];
512 return n;
513 }
521 friend inline FVector mult(const FVector &v, const FVector& w) {
522 FVector r;
523 for (int i=0;i<dim;i++)
524 r[i]=v[i]*w[i];
525 return r;
526 }
534 friend inline FVector div(const FVector &v, const FVector& w) {
535 FVector r;
536 for (int i=0;i<dim;i++)
537 r[i]=v[i]/w[i];
538 return r;
539 }
547 friend inline FVector sqrt(const FVector& a) {
548 assert( !std::numeric_limits<T>::is_integer );
549 FVector v;
550 for (int i=0;i<dim;i++)
551 v[i]=::sqrt(a._data[i]);
552 return v;
553 }
561 friend inline FVector log(const FVector& a) {
562 assert( !std::numeric_limits<T>::is_integer );
563 FVector v;
564 for (int i=0;i<dim;i++)
565 v[i]=::log(a._data[i]);
566 return v;
567 }
575 friend inline FVector exp(const FVector& a) {
576 assert( !std::numeric_limits<T>::is_integer );
577 FVector v;
578 for (int i=0;i<dim;i++)
579 v[i]=::exp(a._data[i]);
580 return v;
581 }
589 friend inline FVector pmin(const FVector& a, const FVector& b) {
590 FVector m;
591 for (int i=0;i<dim;++i)
592 m[i]=std::min(a[i],b[i]);
593 return m;
594 }
602 friend inline FVector pmax(const FVector& a, const FVector& b) {
603 FVector m;
604 for (int i=0;i<dim;++i)
605 m[i]=std::max(a[i],b[i]);
606 return m;
607 }
615 friend inline int intL1Dist(const FVector &a, const FVector& b) {
616 assert( std::numeric_limits<T>::is_integer );
617 int n=0;
618 for (int i = 0; i<dim; ++i)
619 n += abs(int(a[i])-int(b[i]));
620 return n;
621 }
629 friend inline double L1Dist(const FVector &a, const FVector& b) {
630 double d=0;
631 for (int i = 0; i < dim; ++i)
632 d += fabs(double(a[i])-double(b[i]));
633 return d;
634 }
642 friend inline double squaredDist(const FVector &a, const FVector& b) {
643 double d=0;
644 for (int i=0;i<dim;i++) {
645 double e=double(a[i])-double(b[i]);
646 d+=e*e;
647 }
648 return d;
649 }
657 friend inline double dist(const FVector&a, const FVector& b) {
658 return std::sqrt( squaredDist(a,b) );
659 }
660 };
661
662
664}
Array of fixed size.
Definition: FArray.h:17
FArray & fill(const T &v)
Filling.
Definition: FArray.h:109
FArray & operator=(const FArray< T2, S > &b)
Assignment.
Definition: FArray.h:146
T _data[S]
internal storage.
Definition: FArray.h:21
Vector of fixed size.
Definition: FVector.h:17
friend int intNorm1(const FVector &v)
Integer L1-norm.
Definition: FVector.h:443
friend FVector pmax(const FVector &a, const FVector &b)
Pointwise max.
Definition: FVector.h:602
friend T norm(const FVector &v)
Euclidean norm.
Definition: FVector.h:394
T & y()
Write alias.
Definition: FVector.h:125
friend T sum(const FVector &v)
Sum of coordinates.
Definition: FVector.h:508
FVector & normalize()
Euclidean in-place normalization.
Definition: FVector.h:404
FVector(const FArray< T2, dim > &a)
Copy constructor.
Definition: FVector.h:50
T operator*(const FVector &v) const
Scalar product.
Definition: FVector.h:259
FVector operator-() const
Opposite.
Definition: FVector.h:246
FVector & operator-=(const FVector &v)
In place Substraction.
Definition: FVector.h:185
friend double doubleNorm2(const FVector &v)
Double squared Euclidean norm.
Definition: FVector.h:485
friend FVector log(const FVector &a)
Pointwise log logs of each coordinate.
Definition: FVector.h:561
friend int intL1Dist(const FVector &a, const FVector &b)
Integer L1-distance.
Definition: FVector.h:615
T & z()
Write alias.
Definition: FVector.h:139
FVector & operator=(const FArray< T2, dim > &b)
Assignment.
Definition: FVector.h:97
const T & z() const
Read alias.
Definition: FVector.h:132
friend FVector div(const FVector &v, const FVector &w)
Pointwise division.
Definition: FVector.h:534
FVector & fill(const T &v)
Filling.
Definition: FVector.h:75
friend FVector operator*(T s, const FVector &v)
Scalar multiplication.
Definition: FVector.h:349
friend double L1Dist(const FVector &a, const FVector &b)
Double L1-distance.
Definition: FVector.h:629
friend FVector operator-(T s, const FVector &v)
Scalar substraction.
Definition: FVector.h:371
friend FVector exp(const FVector &a)
Pointwise exp exps of each coordinate.
Definition: FVector.h:575
friend T maxNorm(const FVector &v)
Maximum norm.
Definition: FVector.h:428
friend double doubleNorm(const FVector &v)
Double Euclidean norm.
Definition: FVector.h:498
friend FVector pmin(const FVector &a, const FVector &b)
Pointwise min.
Definition: FVector.h:589
friend double squaredDist(const FVector &a, const FVector &b)
Squared distance.
Definition: FVector.h:642
FVector(const T &v)
Constructor with constant value.
Definition: FVector.h:32
T & x()
Write alias.
Definition: FVector.h:111
FVector operator/(T s) const
Scalar division.
Definition: FVector.h:311
FVector(T x, T y, T z)
3D alias.
Definition: FVector.h:67
FVector & operator*=(T s)
Scalar in place multiplication.
Definition: FVector.h:324
const T & y() const
Read alias.
Definition: FVector.h:118
friend T norm2(const FVector &v)
Squared Euclidean norm.
Definition: FVector.h:381
FVector(T x, T y)
2D alias.
Definition: FVector.h:58
FVector operator+(const FVector &v) const
Addition.
Definition: FVector.h:147
friend FVector mult(const FVector &v, const FVector &w)
Pointwise multiplication.
Definition: FVector.h:521
FVector & operator/=(T s)
Scalar in place division.
Definition: FVector.h:336
friend FVector normalized(const FVector &v)
Euclidean normalization.
Definition: FVector.h:416
static FVector Zero()
Zero vector Vector with constant 0 value.
Definition: FVector.h:82
FVector()
Empty constructor.
Definition: FVector.h:25
friend double doubleNorm1(const FVector &v)
Double L1-norm.
Definition: FVector.h:472
friend FVector sqrt(const FVector &a)
Pointwise square root Square roots of each coordinate.
Definition: FVector.h:547
FVector(const T2 t[dim])
Constructor from C array.
Definition: FVector.h:41
friend FVector operator+(T s, const FVector &v)
Scalar addition.
Definition: FVector.h:360
friend double dist(const FVector &a, const FVector &b)
Distance.
Definition: FVector.h:657
FVector operator*(T s) const
Scalar multiplication.
Definition: FVector.h:298
FVector & operator+=(const FVector &v)
In place Addition.
Definition: FVector.h:173
friend int intNorm2(const FVector &v)
Integer squared Euclidean norm.
Definition: FVector.h:458
const T & x() const
Read alias.
Definition: FVector.h:104
T operator^(const FVector< T, 2 > &v) const
2D cross product.
Definition: FVector.h:272
Imagine++ namespace.