#line 1 "/user/cvsspst/ees1cg/RAVL/RAVL-0.7/Math/LinearAlgebra/General/TVector.hh" // This file is part of RAVL, Recognition And Vision Library // Copyright (C) 2001, University of Surrey // This code may be redistributed under the terms of the GNU Lesser // General Public License (LGPL). See the lgpl.licence file for details or // see http://www.gnu.org/copyleft/lesser.html // file-header-ends-here #ifndef RAVL_TVECTOR_HEADER #define RAVL_TVECTOR_HEADER 1 /////////////////////////////////////////////////// //! userlevel=Normal //! docentry="Ravl.Math.Linear Algebra" //! rcsid="$Id: TVector.hh,v 1.14 2002/08/02 12:25:04 jonstarck Exp $" //! file="Ravl/Math/LinearAlgebra/General/TVector.hh" //! author="Charles Galambos" //! date="10/09/1998" //! lib=RavlMath #include "Ravl/SArray1d.hh" #include "Ravl/Slice1d.hh" #include "Ravl/TFVector.hh" namespace RavlN { template class TMatrixC; //! userlevel=Advanced //: Templated vectors. template class TVectorC : public SArray1dC { public: inline TVectorC() {} //: Default constructor. inline TVectorC(const SArray1dC &oth) : SArray1dC(oth) {} //: Constructor for array of DataT's. inline TVectorC(const Slice1dC &oth,bool alwaysCopy = true) : SArray1dC(oth,alwaysCopy) {} //: Construct from a slice. explicit inline TVectorC(SizeT n); //: Constructor. #if !RAVL_COMPILER_VISUALCPP template inline TVectorC(const TFVectorC &dat) : SArray1dC(N) { const DataT *at = &(dat[0]); for(BufferAccessIterC it(*this);it;it++,at++) *it = *at; } //: Create from a fixed size vector. template operator TFVectorC () { RavlAssertMsg(N == Size(),"Size mismatch converting to fixed size array. "); TFVectorC ret; DataT *at = &(ret[0]); for(BufferAccessIterC it(*this);it;it++,at++) *at = *it; return ret; } //: Convert to a fixed size vector. #endif DataT Sum() const; //: Returns the sum all elements of the vector. DataT Product() const; //: Returns the product of all elements of the vector. DataT SumOfSqr() const; //: Returns the sum of the squares of all the elements of the vector. DataT SumOfAbs() const; //: Returns the sum of the absolute values of all the elements of the vector. DataT MaxValue() const; //: Largest value in the array. DataT MaxMagintude() const; //: Value of the largest magnitude in the vector. DataT MinValue() const; //: Smalleset value in the array. const TVectorC & Reciprocal(); //: All elements of the vector are changed to their reciprocal values. //: It is assumed that all elements of the vector differs from zero. DataT Modulus() const { return Sqrt(SumOfSqr()); } //: Returns the modulus of the vector. // The Sqrt(SumOfSqr()). DataT TMul(const TVectorC & b) const; //: multiplication 'DataT' = (*this).T() * b DataT Dot(const TVectorC & v) const; //: scalar product of vectors const TVectorC &SetSmallToBeZero(const DataT &min); //: Set values smaller than 'min' to zero in vector. TMatrixC OuterProduct(const TVectorC &a) const; //: Calculate the outer product of this vector and a. // To use the function you must also include 'Ravl/Matrix.hh'. TMatrixC OuterProduct(const TVectorC &a,RealT b) const; //: Calculate the outer product of this vector and a multiplied by b. // To use the function you must also include 'Ravl/Matrix.hh'. TMatrixC OuterProduct() const; //: Calculate the outer product of this vector with itself. // To use the function you must also include 'Ravl/Matrix.hh'. TVectorC Unit() const { return (*this) / Modulus(); } //: Return a unit vector const TVectorC &MakeUnit() { (*this) = Unit(); return *this; } //: Make this a unit vector. IndexC MaxIndex() const; //: Find the index with the most positive valued index. IndexC MaxAbsIndex() const; //: Find the index with the absolute maximum valued index. }; //////////////////////////////////////// template ostream &operator<<(ostream & s, const TVectorC & arr) { // This uses spaces instread of '\n' which makes vectors more readable. // otherwise its no different from the SArray1dC version. s << arr.Size() << ' '; for(BufferAccessIterC it(arr);it;it++) s << ((const DataC &) *it) << ' '; return s; } template inline TVectorC::TVectorC(SizeT n) : SArray1dC (n) {} template DataT TVectorC::Sum() const { BufferAccessIterC it(*this); if(!it.IsElm()) return 0; DataT sum = it.Data(); it.Next(); for(;it.IsElm();it.Next()) sum += it.Data(); return sum; } template DataT TVectorC::Product() const { BufferAccessIterC it(*this); if(!it.IsElm()) return 1; // Or throw an exception ? DataT prod = it.Data(); it.Next(); for(;it.IsElm();it.Next()) prod *= it.Data(); return prod; } template const TVectorC & TVectorC::Reciprocal() { for(BufferAccessIterC it(*this);it.IsElm();it.Next()) it.Data() = 1/it.Data(); return *this; } template DataT TVectorC::SumOfSqr() const { DataT ret; SetToZero(ret); for(BufferAccessIterC it(*this);it;it++) ret += Sqr(*it); return ret; } template DataT TVectorC::SumOfAbs() const { DataT ret; SetToZero(ret); for(BufferAccessIterC it(*this);it;it++) ret += Abs(*it); return ret; } template DataT TVectorC::TMul(const TVectorC & b) const { DataT sum = 0; for(BufferAccessIter2C it(*this,b);it.IsElm();it.Next()) sum += it.Data1() * it.Data2(); return sum; } template DataT TVectorC::Dot(const TVectorC & v) const { DataT sum = 0; for(BufferAccessIter2C it(*this,v);it.IsElm();it.Next()) sum += it.Data1() * it.Data2(); return sum; } template const TVectorC &TVectorC::SetSmallToBeZero(const DataT &min) { for(BufferAccessIterC it(*this);it;it++) if(Abs(*it) < min) SetToZero(*it); return *this; } template DataT TVectorC::MaxValue() const { BufferAccessIterC it(*this); if(!it.IsElm()) { DataT ret; SetToZero(ret); return ret; } DataT max = *it; for(;it;it++) if(*it > max) max = *it; return max; } template DataT TVectorC::MaxMagintude() const { BufferAccessIterC it(*this); if(!it.IsElm()) { DataT ret; SetToZero(ret); return ret; } DataT max = Abs(*it); for(;it;it++) { DataT mag = Abs(*it); if(mag > max) max = mag; } return max; } template DataT TVectorC::MinValue() const { BufferAccessIterC it(*this); if(!it.IsElm()) { DataT ret; SetToZero(ret); return ret; } DataT min = *it; for(;it;it++) if(*it < min) min = *it; return min; } template IndexC TVectorC::MaxIndex() const { IndexC ind = 0; BufferAccessIterC it(*this); DataT maxVal = *it; for(it++;it;it++) { if(*it > maxVal) { maxVal = *it; ind = (IntT) (&(*it) - &(*this)[0]); } } return ind; } template IndexC TVectorC::MaxAbsIndex() const { IndexC ind = 0; BufferAccessIterC it(*this); DataT maxVal = *it; for(it++;it;it++) { DataT aM = Abs(*it); if(aM > maxVal) { maxVal = aM; ind = (IntT) (&(*it) - &(*this)[0]); } } return ind; } } #endif