Machine Learning Library
CDenseVector.h
Go to the documentation of this file.
1 /*
2  COPYRIGHT (C) 2003 APPLIED NEUROINFORMATIC GROUP - UNIVERSITY OF BIELEFELD.
3 
4  ALL RIGHTS RESERVED.
5 
6  REDISTRIBUTION AND USE IN SOURCE AND BINARY FORM, WITH OR WITHOUT
7  MODIFICATION, REQUIRE THE PERMISSION OF THE COPYRIGHT HOLDERS.
8 
9  COMMERCIAL USE WITHOUT THE EXPLICIT PERMISSION OF THE COPYRIGHT HOLDERS
10  IS FORBIDDEN
11 
12  THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS ``AS IS''
13  AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
14  IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
15  ARE DISCLAIMED. IN NO EVENT SHALL THE AUTHORS OR CONTRIBUTORS BE LIABLE FOR
16  ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL
17  DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR
18  SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER
19  CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY,
20  OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE
21  OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
22 */
23 
24 
25 
26 #ifndef CDENSEVECTOR_H
27 #define CDENSEVECTOR_H
28 
29 #include "CVector.h"
30 #include "CDenseVector.h"
31 #include "CMatrix.h"
32 #include "Macros.h"
33 
34 
35 template<typename Type> struct keyed_iterator;
36 
37 template<class Type> class CDenseVector;
38 template<class Type> class CMatrix;
39 
43 template<class Type> CDenseVector<Type> elementMul(const CDenseVector<Type>& rtVec1, const CDenseVector<Type>& rtVec2);
44 
48 template<class Type> CMatrix<Type> outerProduct(const CDenseVector<Type>& rtVec1, const CDenseVector<Type>& rtVec2);
49 
53 template<class Type>
54 class CDenseVector : public CVector<Type>
55 {
56 protected:
57  int iDim;
58  Type* ptData;
59 
60  void copy(const CVector<Type>&vec);
61 
62 public:
67  explicit CDenseVector(int iDim = 0);
68 
74  CDenseVector(int iDim, const Type* ptData);
75 
82  CDenseVector(int iDim, Type tValue );
83 
89  CDenseVector(const vector<Type>& rtVec);
90 
96  CDenseVector(const CDenseVector<Type>& tVec);
97 
98  CDenseVector(const CVector<Type>& vec);
99  virtual string className() const { return string("CDenseVector");};
103  virtual ~CDenseVector();
104 
105  CVector<Type>* clone() const;
106 
110  void zeros(void);
111 
115  void ones(void);
116 
121  CVector<double>* toDouble() const;
122 
123 
128  CVector<float>* toFloat() const;
129 
130  vector<Type> toSTL() const;
131 
133 
134 
135 
144 
150  int dimension() const { return iDim; };
151 
157  CMatrix<Type> asMatrix() const;
158 
165  Type getElement(int iIndex) const;
166 
167  const Type& operator[](int i) const {
168  ML_LS_CHK(i,iDim);
169  return ptData[i];
170  }
171 
178  Type& operator[](int i) {
179  ML_LS_CHK(i,iDim);
180  return ptData[i];
181  };
182 
189  void setElement(int iIndex, Type tValue);
190 
199  void resize(int iNewDim);
200 
206  void operator+=(Type tScalar);
207 
212  bool isNaN() const;
213 
214 
217  return *this;
218  }
221  return *this;
222  }
223 
225  virtual CVector<Type>& operator*=(const Type&);
227  virtual CVector<Type>& operator/=(const Type&);
228 
234  void operator-=(Type tScalar);
235 
242 
249 
256  Type manhattenDistance( const CDenseVector<Type>& rtVec) const;
257 
264  Type maximumDistance( const CDenseVector<Type>& rtVec) const;
265 
269  Type sumElements() const;
270 
274  void cat(const CDenseVector<Type>& tVec);
275 
281  Type minElement() const;
282 
288  Type maxElement() const;
289 
295  int minElementIndex() const;
296 
302  int maxElementIndex() const;
303 
304 
305 
306 
313 
319  CDenseVector<Type> power(int iPower = 2) const;
320 
321 
328 
335 
340  CDenseVector<Type> subVector(int iStart, int iElements) const;
341 
347  Type mean() const;
348 
349 
350 
356  Type stdDev() const;
365  CDenseVector<Type> shiftLeft(int iNumElements=1);
366 
375  CDenseVector<Type> shiftRight(int iNumElements=1);
376 
386  CDenseVector<Type> scale(const CDenseVector<Type>& tCurrentMin, const CDenseVector<Type>& tCurrentMax,
387  const CDenseVector<Type>& tNewMin, const CDenseVector<Type>& tNewMax) const ;
388 
389 
390 
394  int importFromMatFile(const char* acPath, const char* acVarname);
399  bool exportToMatFile(const char* acPath, const char* acVarname, bool bAppend);
400 
404  // void zero(){ for(int i=0;i<iDim;i++) ptData[i]=static_cast<Type>(0);};
405 
409  void dump(ostream& ostr=cout) const;
410 
414  Type* _data() const { return ptData;} ;
415  void _setData(Type *ptNewData);
416 
417  bool serialize(fstream& stream, IO_MODE mode = READ);
418  bool serialize2(CArchiv& tA);
419  bool isSparse() const { return 0; }
420  int getNumNonZero() const;
421  Type euclideanLength(bool bQuad=false) const;
422 
423  Type* begin() { return ptData; }
424  Type* end() { return ptData + iDim; }
425  Type const* begin() const { return ptData; }
426  Type const* end() const { return ptData + iDim; }
427 
428 
429 
430 
431 }; // end of class
432 
433 template<class Type>
434 ostream& operator<<(ostream& stream, const CDenseVector<Type>& vec)
435 {
436  for(const Type* i = vec.begin(); i != vec.end(); ++i) {
437  stream << *i << ' ';
438  }
439  return stream;
440 }
441 
442 
443 
444 // CVector operators that will result in a dense vector
445 
446 // vector-vector ops
447 template<typename Type>
449 {
450  CDenseVector<Type> rtResult(rtVec1.dimension());
451  CVectorOperations<Type>::getInstance().getElementAddition(rtVec1, rtVec2)(rtResult, rtVec1, rtVec2);
452  return rtResult;
453 }
454 
455 template<class Type> CDenseVector<Type> operator-(const CVector<Type>& rtVec1, const CVector<Type>& rtVec2)
456 {
457  CDenseVector<Type> rtResult(rtVec1.dimension());
458  CVectorOperations<Type>::getInstance().getElementSubtraction(rtVec1, rtVec2)(rtResult, rtVec1, rtVec2);
459  return rtResult;
460 }
461 
462 template<class Type> CDenseVector<Type> operator+(const CVector<Type>& rtVec, Type tScalar)
463 {
464  CDenseVector<Type> rtResult(rtVec);
465  rtResult+=tScalar;
466  return rtResult;
467 }
468 template<class Type> CDenseVector<Type> operator-(const CVector<Type>& rtVec, Type tScalar)
469 {
470  CDenseVector<Type> rtResult(rtVec);
471  rtResult-=tScalar;
472  return rtResult;
473 }
474 
475 // operators w/ by-value returns
476 template<typename Type>
478 {
479  return (CDenseVector<Type>(v1) /= v2);
480 }
481 
482 template<typename Type>
484 {
485  return (CDenseVector<Type>(v) *= s);
486 }
487 template<typename Type>
489 {
490  return (CDenseVector<Type>(v) /= s);
491 }
492 
493 
494 
495 
496 
497 #endif // CDENSEVECTOR_H
#define ML_LS_CHK(var, val)
Definition: Macros.h:111
CVector< double > * toDouble() const
Type * end()
Definition: CDenseVector.h:424
void cat(const CDenseVector< Type > &tVec)
CDenseVector< Type > & operator=(const CDenseVector< Type > &tSrc)
CVector< float > * toFloat() const
Type euclideanLength(bool bQuad=false) const
returns euclidean length of vector
CDenseVector< Type > shiftRight(int iNumElements=1)
IO_MODE
Definition: CObject.h:38
void zeros(void)
void operator+=(Type tScalar)
Type * begin()
Definition: CDenseVector.h:423
Template object implementing a matrix of single and double precision elements.
Definition: CDenseVector.h:38
virtual ~CDenseVector()
int dimension() const
Definition: CDenseVector.h:150
static CVectorOperations< T > & getInstance()
get Singleton instance
int iDim
Definition: CDenseVector.h:57
void setElement(int iIndex, Type tValue)
CDenseVector< Type > elementDiv(const CVector< Type > &v1, const CVector< Type > &v2)
Definition: CDenseVector.h:477
Definition: CDenseVector.h:35
bool isSparse() const
Definition: CDenseVector.h:419
CDenseVector< Type > operator-(const CVector< Type > &rtVec1, const CVector< Type > &rtVec2)
Definition: CDenseVector.h:455
Type const * end() const
Definition: CDenseVector.h:426
CVector< Type > & operator/=(const CVector< Type > &v2)
Definition: CDenseVector.h:219
void resize(int iNewDim)
CDenseVector< Type > maxElements(const CDenseVector< Type > &rtVec) const
void copy(const CVector< Type > &vec)
int getNumNonZero() const
returns the number of non-zero entries
CDenseVector< Type > elementMul(const CDenseVector< Type > &rtVec1, const CDenseVector< Type > &rtVec2)
CVector< Type > & operator*=(const CVector< Type > &)
multiply vectors elementwise
void operator-=(Type tScalar)
CVector< Type > & operator/=(const CVector< Type > &)
divide vectors elementwise
void _setData(Type *ptNewData)
vector< Type > toSTL() const
CDenseVector< Type > squareroot() const
CDenseVector< Type > operator*(const CVector< Type > &v, const Type &s)
Definition: CDenseVector.h:483
CDenseVector< Type > shiftLeft(int iNumElements=1)
bool serialize2(CArchiv &tA)
int minElementIndex() const
Definition: CObject.h:38
Type sumElements() const
virtual string className() const
Returns the class name.
Definition: CDenseVector.h:99
CMatrix< Type > outerProduct(const CDenseVector< Type > &rtVec1, const CDenseVector< Type > &rtVec2)
Type minElement() const
CDenseVector< Type > scale(const CDenseVector< Type > &tCurrentMin, const CDenseVector< Type > &tCurrentMax, const CDenseVector< Type > &tNewMin, const CDenseVector< Type > &tNewMax) const
const Type & operator[](int i) const
return constant reference to n'th vector element
Definition: CDenseVector.h:167
Type * _data() const
Definition: CDenseVector.h:414
Type mean() const
CVector< Type > * clone() const
destructor
int importFromMatFile(const char *acPath, const char *acVarname)
Type maximumDistance(const CDenseVector< Type > &rtVec) const
Base class of object serialization.
Definition: CArchiv.h:19
Type * ptData
Definition: CDenseVector.h:58
Template object for vectors of single and double precision and integer.
Definition: CDenseVector.h:37
bool serialize(fstream &stream, IO_MODE mode=READ)
Read/write from binary stream.
templatized vector for numerical applications
Definition: CMatrix.h:39
CDenseVector< Type > subVector(int iStart, int iElements) const
void ones(void)
bool exportToMatFile(const char *acPath, const char *acVarname, bool bAppend)
CDenseVector< Type > operator+(const CVector< Type > &rtVec1, const CVector< Type > &rtVec2)
Definition: CDenseVector.h:448
Type const * begin() const
Definition: CDenseVector.h:425
CVector< Type > & operator*=(const CVector< Type > &v2)
Definition: CDenseVector.h:215
Type getElement(int iIndex) const
CDenseVector< Type > minElements(const CDenseVector< Type > &rtVec) const
int maxElementIndex() const
Type maxElement() const
CMatrix< Type > asMatrix() const
Type stdDev() const
void dump(ostream &ostr=cout) const
CDenseVector(int iDim=0)
CDenseVector< Type > operator/(const CVector< Type > &v, const Type &s)
Definition: CDenseVector.h:488
Type & operator[](int i)
Definition: CDenseVector.h:178
CDenseVector< Type > power(int iPower=2) const
Type manhattenDistance(const CDenseVector< Type > &rtVec) const
bool isNaN() const
virtual int dimension(void) const =0
return vector dimension
CDenseVector< int > sort()