#ifndef VECTOR_ARITHMETICS #define VECTOR_ARITHMETICS #include #include using namespace std; #pragma mark - #pragma mark Vectors: Extending operations on STL vectors #pragma mark template inline vector operator*(vector x, const vector &y){ for(unsigned long i=0; i inline vector operator+(vector x, const vector &y){ for(unsigned long i=0; i inline vector operator-(vector x, const vector &y){ for(unsigned long i=0; i inline vector operator/(vector x, const vector &y){ for(unsigned long i=0; i inline vector operator*(vector x, double scalar){ for(unsigned long i=0; i inline vector operator/(vector x, double scalar){ for(unsigned long i=0; i sqrt(vector x){ for(std::vector::iterator i=x.begin(); i!=x.end(); ++i){ *i = std::sqrt(*i); } return x; } inline vector exp(vector x){ for(std::vector::iterator i=x.begin(); i!=x.end(); ++i){ *i = std::exp(*i); } return x; } inline vector log(vector x){ for(std::vector::iterator i=x.begin(); i!=x.end(); ++i){ *i = std::log(*i); } return x; } inline vector pow(vector x, double power){ for(std::vector::iterator i=x.begin(); i!=x.end(); ++i){ *i = std::pow(*i,power); } return x; } inline vector abs(vector x){ for(std::vector::iterator i=x.begin(); i!=x.end(); ++i){ *i = std::abs(*i); } return x; } //pairwise maxima of vector entries (vectors must have same size) inline vector max(vector x, const vector &y){ std::vector::iterator i; std::vector::const_iterator j; for(i=x.begin(), j=y.begin(); i!=x.end(); ++i, ++j){ *i = max(*i, *j); } return x; } inline vector max(vector x, double b){ std::vector::iterator i; for(i=x.begin(); i!=x.end(); ++i){ *i = max(*i, b); } return x; } // tensor product between a scalar and a vector of size DIM2 // resulting vector respresents a 1*DIM2 dimensional matrix stored in row-major order // That is, matrix[r*DIM2+c] = p1[r]*p2[c] inline vector TensorProduct(double p1, const vector &p2){ unsigned int c, DIM2=p2.size(); vector p(1*DIM2); for(c=0; c TensorProduct(const vector &p1, const vector &p2){ unsigned int r, c, DIM1 = p1.size(), DIM2=p2.size(); vector p(DIM1*DIM2); for(r=0; r TensorProduct(const vector &p1, double p2){ vector p(p1.size()); for(unsigned int i=0; i