1 #ifndef MATH_VECTOR_TEMPLATE_H 2 #define MATH_VECTOR_TEMPLATE_H 26 typedef size_t difference_type;
27 typedef std::random_access_iterator_tag iterator_category;
30 inline VectorIterator(
const MyT& i) :ptr(i.ptr),stride(i.stride) {}
31 inline VectorIterator(T* _ptr,
int _stride) :ptr(_ptr),stride(_stride) {}
32 inline T& operator*() {
return *ptr; }
33 inline T* operator->() {
return ptr; }
34 inline MyT& operator ++() { ptr+=stride;
return *
this; }
35 inline MyT& operator --() { ptr-=stride;
return *
this; }
36 inline MyT& operator ++(
int) { ptr+=stride;
return *
this; }
37 inline MyT& operator --(
int) { ptr-=stride;
return *
this; }
40 inline MyT operator +(
int i)
const {
return MyT(ptr+i*stride,stride); }
41 inline MyT operator -(
int i)
const {
return MyT(ptr-i*stride,stride); }
42 inline bool operator !=(
const MyT& i) {
return ptr!=i.ptr; }
43 inline bool operator ==(
const MyT& i) {
return ptr==i.ptr; }
44 inline bool operator < (
const MyT& i) {
return ptr<i.ptr; }
45 inline bool operator > (
const MyT& i) {
return ptr>i.ptr; }
46 inline size_t operator - (
const MyT& i)
const {
return (ptr-i.ptr)/stride; }
139 inline T* getPointer()
const {
return vals; }
140 inline int getCapacity()
const {
return capacity; }
141 inline T* getStart()
const {
return vals+base; }
142 inline ItT begin()
const {
return ItT(vals+base,stride); }
143 inline ItT end()
const {
return ItT(vals+base+n*stride,stride); }
144 inline int size()
const {
return n; }
145 inline bool empty()
const {
return n==0; }
147 void resize(
int size);
148 void resize(
int size, T initval);
149 void resizePersist(
int size);
150 void resizePersist(
int size, T initval);
153 MyT& operator = (
const MyT& v);
154 MyT& operator = (MyT&& v);
155 bool operator == (
const MyT&)
const;
156 inline bool operator != (
const MyT& a)
const {
return !operator==(a); }
157 inline operator T* ();
158 inline operator const T* ()
const;
159 operator std::vector<T> ()
const;
160 inline MyT& operator = (
const std::vector<T>& v) {
copy(v);
return *
this; }
161 inline const T& operator() (
int i)
const {
return operator[](i); }
162 inline T& operator() (
int i) {
return operator[](i); }
163 inline const T& operator[] (
int i)
const;
164 inline T& operator[] (
int i);
165 inline void operator += (
const MyT& a) { inc(a); }
166 inline void operator -= (
const MyT& a) { dec(a); }
167 inline void operator *= (T c) { inplaceMul(c); }
168 inline void operator /= (T c) { inplaceDiv(c); }
170 void copy(
const MyT&);
172 void copy(
const T* vals);
173 template <
class T2>
void copy(
const std::vector<T2>& vals);
174 void copySubVector(
int i,
const MyT&);
177 void add(
const MyT&,
const MyT&);
178 void sub(
const MyT&,
const MyT&);
179 void mul(
const MyT&, T);
180 void div(
const MyT&, T);
181 void axpby(T a,
const MyT& x,T b,
const MyT& y);
184 void inc(
const MyT&);
185 void dec(
const MyT&);
186 void madd(
const MyT&, T);
188 void setRef(
const MyT&,
int base=0,
int stride=1,
int size=-1);
189 void setRef(T* vals,
int length,
int base=0,
int stride=1,
int size=-1);
192 void setNegative(
const MyT&);
193 void setNormalized(
const MyT&);
194 void setConjugate(
const MyT&);
196 void inplaceNegative();
199 void inplaceNormalize();
200 void inplaceConjugate();
202 void getCopy(MyT&)
const;
203 void getCopy(T* vals)
const;
204 void getSubVectorCopy(
int i,MyT&)
const;
205 void getRef(MyT&,
int base=0,
int stride=1,
int size=-1)
const;
206 inline void getNegative(MyT& v)
const { v.setNegative(*
this); }
207 inline void getNormalized(MyT& v)
const { v.setNormalized(*
this); }
208 inline void getConjugate(MyT& v)
const { v.setConjugate(*
this); }
210 inline bool isReference()
const {
return !allocated; }
211 inline bool isUninitialized()
const {
return vals==NULL; }
212 inline bool isValidIndex(
int i)
const {
return i>=0 && i<n; }
213 inline bool isCompact()
const {
return (stride==1); }
214 bool isValid()
const;
215 bool isZero(T eps=0)
const;
216 bool isEqual(
const MyT&,T eps=0)
const;
218 T dot(
const MyT&)
const;
221 T normSquared()
const;
222 T distance(
const MyT&)
const;
223 T distanceSquared(
const MyT&)
const;
224 T minElement(
int* index=NULL)
const;
225 T maxElement(
int* index=NULL)
const;
226 T minAbsElement(
int* index=NULL)
const;
227 T maxAbsElement(
int* index=NULL)
const;
230 bool Write(
File&)
const;
232 void componentMul(
const MyT& a,
const MyT& b);
233 void componentDiv(
const MyT& a,
const MyT& b);
234 void componentMadd(
const MyT& a,
const MyT& b);
235 void inplaceComponentMul(
const MyT& c);
236 void inplaceComponentDiv(
const MyT& c);
239 template <
class UnaryOp>
240 void componentOp(
const MyT& a,UnaryOp& f);
242 template <
class BinaryOp>
243 void componentOp(
const MyT& a,
const MyT& b,BinaryOp& f);
245 template <
class BinaryOp>
246 void componentOp(
const MyT& a,T c,BinaryOp& f);
249 template <
class UnaryOp>
250 void inplaceComponentOp(UnaryOp& f);
252 template <
class BinaryOp>
253 void inplaceComponentOp(
const MyT& c,BinaryOp& f);
255 template <
class BinaryOp>
256 void inplaceComponentOp(T c,BinaryOp& f);
275 std::ostream& operator << (std::ostream&, const VectorTemplate<T>&);
282 template <
class UnaryOp>
285 if(empty()) resize(a.n);
286 else assert(size() == a.size());
289 for(
int i=0;i<n;i++,v+=stride,va+=a.stride)
294 template <
class BinaryOp>
297 assert(a.size()==b.size());
298 if(empty()) resize(a.n);
299 else assert(size() == a.size());
303 for(
int i=0;i<n;i++,v+=stride,va+=a.stride,vb+=b.stride)
308 template <
class BinaryOp>
311 if(empty()) resize(a.n);
312 else assert(size() == a.size());
315 for(
int i=0;i<n;i++,v+=stride,va+=a.stride)
320 template <
class UnaryOp>
325 for(
int i=0;i<n;i++,v+=stride)
330 template <
class BinaryOp>
334 assert(size() == c.size());
337 for(
int i=0;i<n;i++,v+=stride,vc+=c.stride)
342 template <
class BinaryOp>
347 for(
int i=0;i<n;i++,v+=stride)
355 return vals[i*stride+base];
361 return vals[i*stride+base];
381 return a.isEqual(b,eps);
387 return a.isZero(eps);
406 for(
int i=0;i<x.n;i++)
415 for(
int i=0;i<x.n;i++)
416 if(
IsNaN(x(i)))
return true;
424 for(
int i=0;i<x.n;i++)
Common math typedefs, constants, functions.
int IsInf(double x)
Returns +1 if x is +inf, -1 if x is -inf, and 0 otherwise.
Definition: infnan.cpp:40
Cross-platform infinity and not-a-number routines.
Definition: rayprimitives.h:132
void inplaceComponentOp(UnaryOp &f)
for each element xi, sets xi = f(xi)
Definition: VectorTemplate.h:321
int HasInf(const MatrixTemplate< T > &A)
returns nonzero if any element of A is infinite
Definition: MatrixTemplate.h:262
void componentOp(const MyT &a, UnaryOp &f)
for each element xi, sets xi = f(ai)
Definition: VectorTemplate.h:283
double dot(double a, double b)
Dot product of a scalar as a 1-d vector.
Definition: math.h:220
bool FuzzyZero(double a, double eps=dEpsilon)
Returns true if a is zero within +/- eps.
Definition: math.h:224
Complex number class (x + iy).
Definition: complex.h:17
bool HasNaN(const MatrixTemplate< T > &A)
returns true if any element of A is NaN
Definition: MatrixTemplate.h:252
An iterator through VectorTemplate elements.
Definition: VectorTemplate.h:19
void copy(const T &a, T *out, int n)
Definition: arrayutils.h:34
int IsNaN(double x)
Returns nonzero if x is not-a-number (NaN)
Definition: infnan.cpp:9
Contains all definitions in the Math package.
Definition: WorkspaceBound.h:12
A vector over the field T.
Definition: function.h:9
bool FuzzyEquals(double a, double b, double eps=dEpsilon)
Returns true if a and b are within +/- eps.
Definition: math.h:222
A cross-platform class for reading/writing binary data.
Definition: File.h:47
int IsFinite(double x)
Returns nonzero unless x is infinite or a NaN.
Definition: infnan.cpp:23