// Author: Douglas Wilhelm Harder // Copyright (c) 2009 by Douglas Wilhelm Harder. All rights reserved. /*************************************************** * Vector-Vector Addition * - Add the entries of the vector u onto the * entries of this vector. * * Run time: O( N ) * Memory: O( 1 ) ***************************************************/ template Vector &Vector::operator+= ( Vector const &u ) { for ( int i = 0; i < N; ++i ) { array[i] += u.array[i]; } return *this; } /*************************************************** * Vector-Vector Difference * - Subtract the entries of the vector u from the * entries of this vector. * * Run time: O( N ) * Memory: O( 1 ) ***************************************************/ template Vector &Vector::operator-= ( Vector const &u ) { for ( int i = 0; i < N; ++i ) { array[i] -= u.array[i]; } return *this; } /*************************************************** * Elementwise Vector-Vector Multiplication * - Multiply the entries of the vector u * elementwise by the entries of this vector. * * Run time: O( N ) * Memory: O( 1 ) ***************************************************/ template Vector &Vector::operator*= ( Vector const &u ) { for ( int i = 0; i < N; ++i ) { array[i] *= u.array[i]; } return *this; } /*************************************************** * Elementwise Vector-Vector Division * - Divide the entries of this vector * elementwise by the entries of the vector u. * * Run time: O( N ) * Memory: O( 1 ) ***************************************************/ template Vector &Vector::operator/= ( Vector const &u ) { for ( int i = 0; i < N; ++i ) { array[i] /= u.array[i]; } return *this; } /*************************************************** * *********************************************** * * * * * * * FRIENDS * * * * * * * *********************************************** * ***************************************************/ /*************************************************** * Add the entries of the vectors u and v. * * Run time: O( N ) * Memory: O( N ) ***************************************************/ template Vector operator+ ( Vector const &u, Vector const &v ) { Vector w( u ); w += v; return w; } /*************************************************** * Subtract the entries of the vector v from the * entries of the vector u. * * Run time: O( N ) * Memory: O( N ) ***************************************************/ template Vector operator- ( Vector const &u, Vector const &v ) { Vector w( u ); w -= v; return w; } /*************************************************** * Multiply the entries of vectors u and v * elementwise. * * Run time: O( N ) * Memory: O( N ) ***************************************************/ template Vector operator* ( Vector const &u, Vector const &v ) { Vector w( u ); w *= v; return w; } /*************************************************** * Divide the entries of the vector u by the * entries of the vector v. * * Run time: O( N ) * Memory: O( N ) ***************************************************/ template Vector operator/ ( Vector const &u, Vector const &v ) { Vector w( u ); w /= v; return w; } /*************************************************** * Take the inner product of the row vector u * and the column vector v. * * Run time: O( N ) * Memory: O( 1 ) ***************************************************/ template double operator* ( Vector const &u, Vector const &v ) { double inner_product = 0.0; for ( int i = 0; i < N; ++i ) { inner_product += u.array[i] * v.array[i]; } return inner_product; }