// Authors: Unknown. Please, if you are the author of this file, or if you // know who are the authors of this file, let us know, so we can give the // adequate credits and/or get the adequate authorizations. #ifdef MATRIX_H // Do nothing if not included from matrix.h namespace libNumerics { /// Constructor template vector::vector(int m) : matrix(m, 1) {} /// 1-vector constructor. template vector::vector(T x) : matrix(1,1) { this->p[0] = x; } /// 2-vector constructor. template vector::vector(T x, T y) : matrix(2,1) { this->p[0] = x; this->p[1] = y; } /// 3-vector constructor. template vector::vector(T x, T y, T z) : matrix(3,1) { this->p[0] = x; this->p[1] = y; this->p[2] = z; } /// Copy constructor template vector::vector(const vector& v) : matrix(v) {} /// Assignment operator template vector& vector::operator=(const vector& v) { matrix::operator=(v); return *this; } /// Multiply a vector by scalar. /// \param a a scalar. template vector vector::operator*(T a) const { vector v(this->m_rows); for(int i = this->m_rows-1; i >= 0; i--) v.p[i] = a*this->p[i]; return v; } /// Divide a vector by scalar. /// \param a a scalar. template inline vector vector::operator/(T a) const { return operator*( (T)1/a ); } /// Addition of vectors. template vector vector::operator+(const vector& v) const { assert(this->m_rows == v.m_rows); vector sum(this->m_rows); for(int i = this->m_rows-1; i >= 0; i--) sum.p[i] = this->p[i] + v.p[i]; return sum; } /// Subtraction of vectors. template vector vector::operator-(const vector& v) const { assert(this->m_rows == v.m_rows); vector sub(this->m_rows); for(int i = this->m_rows-1; i >= 0; i--) sub.p[i] = this->p[i] - v.p[i]; return sub; } /// Opposite of vector. template vector vector::operator-() const { vector v(this->m_rows); for(int i = this->m_rows-1; i >= 0; i--) v.p[i] = -this->p[i]; return v; } /// Vector times matrix. template matrix vector::operator*(const matrix& m) const { return matrix::operator*(m); } /// Diagonal matrix defined by its diagonal vector. template matrix vector::diag() const { matrix d(this->m_rows, this->m_rows); d = (T)0; for(int i = this->m_rows-1; i >= 0; i--) d(i,i) = this->p[i]; return d; } /// Square L^2 norm of vector. template T vector::qnorm() const { T q = (T)0; for(int i = this->m_rows-1; i >= 0; i--) q += this->p[i]*this->p[i]; return q; } /// Subvector from \a i0 to \a i1. template vector vector::copy(int i0, int i1) const { assert(0 <= i0 && i0 <= i1 && i1 <= this->m_rows); vector v(i1-i0+1); for(int i=i0; i <= i1; i++) v.p[i-i0] = this->p[i]; return v; } /// Paste vector \a v from row i0. template void vector::paste(int i0, const vector& v) { matrix::paste(i0, 0, v); } } // namespace libNumerics /// Scalar product. template T dot(const libNumerics::vector& u, const libNumerics::vector& v) { assert(u.nrow() == v.nrow()); T d = (T)0; for(int i = u.nrow()-1; i >= 0; i--) d += u(i)*v(i); return d; } /// Cross product. template libNumerics::vector cross(const libNumerics::vector& u, const libNumerics::vector& v) { assert(u.nrow() == 3 && v.nrow() == 3); libNumerics::vector w(3); w(0) = u(1)*v(2) - u(2)*v(1); w(1) = u(2)*v(0) - u(0)*v(2); w(2) = u(0)*v(1) - u(1)*v(0); return w; } #endif // MATRIX_H