/** * Implementation of fast vector type with support of linear algebra operations * Copyright (C) 2009 Lukáš Jirkovský * * This program is free software; you can redistribute it and/or * modify it under the terms of the GNU General Public License * as published by the Free Software Foundation; either version 2 * of the License, or (at your option) any later version. * * This program 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 General Public License for more details. * * You should have received a copy of the GNU General Public License * along with this program; if not, write to the Free Software *Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301, USA. */ #include #include namespace vigra { /** Fixed size vector with scalar multiplication and element-wise substraction and addition */ template class AlgTinyVector { public: AlgTinyVector(const AlgTinyVector & t) { for (unsigned int i = 0; i < SIZE; ++i) { content[i] = t[i]; } } AlgTinyVector(T t) { for (unsigned int i = 0; i < SIZE; ++i) { content[i] = t; } } AlgTinyVector() { AlgTinyVector(0); } const T operator[](int i) const { return content[i]; } T& operator[](int i) { return content[i]; } const T operator*(const AlgTinyVector & t) const { T retVal = 0; for (unsigned int i = 0; i < SIZE; ++i) { retVal += t[i] * content[i]; } return retVal; } const AlgTinyVector operator*(const int t) const { AlgTinyVector retVal; for (unsigned int i = 0; i < SIZE; ++i) { retVal[i] = t * content[i]; } return retVal; } const AlgTinyVector operator/(const int t) const { AlgTinyVector retVal; for (unsigned int i = 0; i < SIZE; ++i) { retVal[i] = content[i] / t; } return retVal; } const AlgTinyVector operator-(const AlgTinyVector & t) const { AlgTinyVector retVal; for (unsigned int i = 0; i < SIZE; ++i) { retVal[i] = t[i] - content[i]; } return retVal; } const AlgTinyVector operator+(const AlgTinyVector & t) const { AlgTinyVector retVal; for (unsigned int i = 0; i < SIZE; ++i) { retVal[i] = t[i] + content[i]; } return retVal; } AlgTinyVector & operator=(const AlgTinyVector & t) { if (this == &t) return *this; for (unsigned int i = 0; i < SIZE; ++i) { content[i] = t[i]; } return *this; } AlgTinyVector & operator=(const TinyVector & t) { for (unsigned int i = 0; i < SIZE; ++i) { content[i] = t[i]; } return *this; } private: T content[SIZE]; }; template struct NumericTraits > { typedef AlgTinyVector Type; typedef AlgTinyVector::Promote, SIZE> Promote; typedef AlgTinyVector::RealPromote, SIZE> RealPromote; typedef AlgTinyVector::ComplexPromote, SIZE> ComplexPromote; typedef T ValueType; typedef typename NumericTraits::isIntegral isIntegral; typedef VigraFalseType isScalar; typedef typename NumericTraits::isSigned isSigned; typedef VigraFalseType isOrdered; typedef VigraFalseType isComplex; static AlgTinyVector zero() { return AlgTinyVector(NumericTraits::zero()); } static AlgTinyVector one() { return AlgTinyVector(NumericTraits::one()); } static AlgTinyVector nonZero() { return AlgTinyVector(NumericTraits::nonZero()); } static Promote toPromote(const AlgTinyVector & v) { return Promote(v); } static RealPromote toRealPromote(const AlgTinyVector & v) { return RealPromote(v); } /*static AlgTinyVector fromPromote(AlgTinyVector::Promote> const & v) { AlgTinyVector res; return res; } static TinyVector fromRealPromote(AlgTinyVector::RealPromote> const & v) { TinyVector res(detail::dontInit()); typedef typename detail::LoopType::type ltype; ltype::fromRealPromote(res.begin(), v.begin()); return res; }*/ }; }