// // MathVector.cpp // // This file contains function definitions for the Vector class. // This code is part of the Bioinformatics Template Library (BTL). // // Copyright (C) 1997 Birkbeck College, Malet Street, London WC1E 7HX, U.K. // (classlib@mail.cryst.bbk.ac.uk) // // This library is free software; you can // redistribute it and/or modify it under the terms of // the GNU Library General Public License as published by the Free // Software Foundation; either version 2 of the License, or (at your // option) any later version. This library is distributed in the hope // that it will be useful, but WITHOUT ANY WARRANTY; without even the // implied warranty of MERCHANTABILITY or FITNESS FOR A PARTICULAR // PURPOSE. See the GNU Library General Public License for more details. // You should have received a copy of the GNU Library General Public // License along with this library; if not, write to the Free Software // Foundation, 59 Temple Place - Suite 330, Boston, MA 02111-1307, USA. //////////////////////////////////////////////////////////////////////////// #include "MathVector.h" #include "NumericLimits.h" #include "NumericUtilities.h" #include //............................................................................. // Construct from an array of reals ( given value_type* ) Vector::Vector(const value_type* const array, const size_type& p) { for (size_type i=0; i= 0). Vector::value_type& Vector::operator[](const size_type& i) { #if defined(DEBUG_VERSION) // Bounds checks // if (i>=size()) FATAL_ERROR("Index i is out of range"); #endif return vec[i]; } //............................................................................. // Returns the element, i positions from the beginning of the Vector, // in constant time. (i >= 0). Vector::value_type Vector::operator[](const size_type& i) const { #if defined(DEBUG_VERSION) // Bounds checks // if (i>=size()) FATAL_ERROR("Index i is out of range"); #endif return vec[i]; } //............................................................................. // Returns the i-th element in the Vector (i >= 1). Vector::value_type& Vector::operator()(const size_type& i) { #if defined(DEBUG_VERSION) // Bounds checks // if (i>size() || i<1) FATAL_ERROR("Index i is out of range"); #endif return vec[i-1]; } //************************** VECTOR MODULUS *********************************** Vector::value_type Vector::Modulus() const { value_type sumsqrs = 0.0; for (const_iterator i=vec.begin(); i!=vec.end(); i++) sumsqrs += (*i) * (*i); return sqrt(sumsqrs); } //************************** LARGEST ELEMENT DIFFERENCE *********************** // Returns the largest difference between equivalent elements in this and // Vector v. Vector::value_type Vector::MaxDifference(const Vector& v) const { if (v.size() != size()) { WARNING("The input Vector must be the same size as the this Vector."); return 99999.9; } double maxdiff = 0.0, diff; for (size_type i=0; i maxdiff) maxdiff = diff; } return maxdiff; } //************************** VECTOR DISTANCE ********************************** Vector::value_type Vector::Distance(const Vector& v) const { if (v.size() != size()) { WARNING("The input Vector must be the same size as the this Vector."); return 99999.9; } value_type dist=0.0, diff; for (size_type i=0; i::digits10(); } unsigned int error, maxError=0; for (size_type i=0; i::AreNotEqual(vec[i], v.vec[i]); if (error > maxError) maxError = error; } return maxError; }