// Author: Ce Liu (c) Dec, 2009; celiu@mit.edu // Modified By: Deepak Pathak (c) 2016; pathak@berkeley.edu #pragma once #include "stdio.h" #include "project.hpp" #include using namespace std; template class Vector { protected: int nDim; T* pData; public: Vector(void); Vector(int ndim,const T *data=NULL); Vector(const Vector& vect); ~Vector(void); void releaseData(); void allocate(int ndim); void allocate(const Vector& vect){allocate(vect.nDim);}; void copyData(const Vector& vect); void dimcheck(const Vector& vect) const; void reset(); double norm2() const; T sum() const; void printVector(); // access the members const T* data() const{return (const T*)pData;}; T* data() {return pData;}; int dim() const {return nDim;}; inline bool matchDimension(int _ndim) const {if(nDim==_ndim) return true;else return false;}; inline bool matchDimension(const Vector& vect) const {return matchDimension(vect.nDim);}; // operators inline T operator[](int index) const {return pData[index];}; inline T& operator[](int index){return *(pData+index);}; Vector& operator=(const Vector& vect); //const Vector& operator/(double val) const //{ // Vector result(nDim); // for(int i =0;i& operator+=(const Vector& vect); Vector& operator*=(const Vector& vect); Vector& operator-=(const Vector& vect); Vector& operator/=(const Vector& vect); Vector& operator+=(double val); Vector& operator*=(double val); Vector& operator-=(double val); Vector& operator/=(double val); //friend const Vector operator+(const Vector& vect1,const Vector& vect2); //friend const Vector operator*(const Vector& vect1,const Vector& vect2); //friend const Vector operator-(const Vector& vect1,const Vector& vect2); //friend const Vector operator/(const Vector& vect1,const Vector& vect2); //friend const Vector operator+(const Vector& vect1,double val); //friend const Vector operator*(const Vector& vect1,double val); //friend const Vector operator-(const Vector& vect1,double val); //friend Vector operator/(const Vector& vect,double val); friend double innerproduct(const Vector& vect1,const Vector& vect2) { double result = 0; for(int i = 0;i >& vect); //friend const Vector concatenate(const vector>& vect){Vector result; result.concatenate(vect); return result;}; bool write(ofstream& myfile) { myfile.write((char *)&nDim,sizeof(int)); myfile.write((char *)pData,sizeof(T)*nDim); return true; } bool read(ifstream& myfile) { myfile.read((char *)&nDim,sizeof(int)); allocate(nDim); myfile.read((char *)pData,sizeof(T)*nDim); return true; } T mean(int N=-1) const { if(N==-1) N = nDim; T result = 0; for(int i = 0;i //double innerproduct(const Vector& vect1,const Vector& vect2) //{ // double result = 0; // for(int i = 0;i Vector::Vector(void) { nDim=0; pData=NULL; } template Vector::Vector(int ndim, const T *data) { nDim=ndim; pData=new T[nDim]; if(data!=NULL) memcpy(pData,data,sizeof(T)*nDim); else memset(pData,0,sizeof(T)*nDim); } template Vector::Vector(const Vector& vect) { nDim=0; pData=NULL; copyData(vect); } template Vector::~Vector(void) { releaseData(); } template void Vector::releaseData() { if(pData!=NULL) delete[] pData; pData=NULL; nDim=0; } template void Vector::allocate(int ndim) { releaseData(); nDim=ndim; if(nDim>0) { pData=new T[nDim]; reset(); } } template void Vector::copyData(const Vector &vect) { if(nDim!=vect.nDim) { releaseData(); nDim=vect.nDim; pData=new T[nDim]; } memcpy(pData,vect.pData,sizeof(T)*nDim); } template void Vector::dimcheck(const Vector &vect) const { if(nDim!=vect.nDim) cout<<"The dimensions of the vectors don't match!"< void Vector::reset() { if(pData!=NULL) memset(pData,0,sizeof(T)*nDim); } template T Vector::sum() const { T total = 0; for(int i=0;i double Vector::norm2() const { double temp=0; for(int i=0;i void Vector::printVector() { for(int i=0;i Vector& Vector::operator =(const Vector &vect) { copyData(vect); return *this; } template Vector& Vector::operator +=(const Vector &vect) { dimcheck(vect); for(int i=0;i Vector& Vector::operator *=(const Vector &vect) { dimcheck(vect); for(int i=0;i Vector& Vector::operator -=(const Vector &vect) { dimcheck(vect); for(int i=0;i Vector& Vector::operator /=(const Vector &vect) { dimcheck(vect); for(int i=0;i Vector& Vector::operator +=(double val) { for(int i=0;i Vector& Vector::operator *=(double val) { for(int i=0;i Vector& Vector::operator -=(double val) { for(int i=0;i Vector& Vector::operator /=(double val) { for(int i=0;i const Vector operator+(const Vector& vect1,const Vector& vect2) { vect1.dimcheck(vect2); Vector result(vect1); result+=vect2; return result; } template const Vector operator-(const Vector& vect1,const Vector& vect2) { vect1.dimcheck(vect2); Vector result(vect1); result-=vect2; return result; } template const Vector operator*(const Vector& vect1,const Vector& vect2) { vect1.dimcheck(vect2); Vector result(vect1); result*=vect2; return result; } template const Vector operator/(const Vector& vect1,const Vector& vect2) { vect1.dimcheck(vect2); Vector result(vect1); result/=vect2; return result; } template Vector operator+(const Vector& vect,double val) { Vector result(vect); result+=val; return result; } template Vector operator-(const Vector& vect,double val) { Vector result(vect); result-=val; return result; } template Vector operator*(const Vector& vect,double val) { Vector result(vect); result*=val; return result; } template Vector operator/(const Vector& vect,double val) { Vector result(vect); result/=val; return result; } template double innerproduct(const Vector& vect1,const Vector& vect2) { vect1.dimcheck(vect2); double result=0; for(int i=0;i void Vector::concatenate(const vector< Vector >& vect) { releaseData(); nDim = 0; for(int i = 0;i0) { allocate(nDim); if(file.read((char *)pData,sizeof(double)*nDim)!=sizeof(double)*nDim) return false; } return true; } #endif #ifdef _MATLAB template void Vector::readVector(const mxArray* prhs) { if(pData!=NULL) delete[] pData; int nElements = mxGetNumberOfDimensions(prhs); if(nElements>2) mexErrMsgTxt("A vector is expected to be loaded!"); const int* dims = mxGetDimensions(prhs); nDim = dims[0]*dims[1]; pData = new T[nDim]; double* ptr = (double*)mxGetData(prhs); for(int i =0;i void Vector::writeVector(mxArray*& plhs) const { int dims[2]; dims[0]=nDim;dims[1]=1; plhs=mxCreateNumericArray(2, dims,mxDOUBLE_CLASS, mxREAL); double *ptr = (double*)mxGetData(plhs); for(int i =0;i