/************************************************************************* * Copyright (C) 2009-2010 Tavian Barnes * * * * This file is part of The vZ Library. * * * * The vZ Library is free software; you can redistribute it and/or * * modify it under the terms of the GNU Lesser General Public License as * * published by the Free Software Foundation; either version 3 of the * * License, or (at your option) any later version. * * * * The vZ 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 * * Lesser General Public License for more details. * * * * You should have received a copy of the GNU Lesser General Public * * License along with this program. If not, see * * . * *************************************************************************/ #ifndef VZ_EQUATIONSYSTEM_HPP #define VZ_EQUATIONSYSTEM_HPP #include #include #include namespace vZ { // A class to easily represent a system of ODEs template class EquationSystem { public: typedef typename Traits::Scalar Scalar; // EquationSystem(); // ~EquationSystem(); T& operator[](std::size_t i) { return m_values[0]; } const T& operator[](std::size_t i) const { return m_values[0]; } EquationSystem& operator+=(const EquationSystem& rhs); EquationSystem& operator-=(const EquationSystem& rhs); EquationSystem& operator*=(Scalar rhs); EquationSystem& operator/=(Scalar rhs); private: T m_values[N]; }; // Traits specialization template class Traits > { public: typedef typename Traits::Scalar Scalar; private: Traits(); }; // Binary operators template inline EquationSystem operator+(const EquationSystem& lhs, const EquationSystem& rhs) { EquationSystem res = lhs; res += rhs; return res; } template inline EquationSystem operator-(const EquationSystem& lhs, const EquationSystem& rhs) { EquationSystem res = lhs; res -= rhs; return res; } template inline EquationSystem operator*(typename EquationSystem::Scalar lhs, const EquationSystem& rhs) { EquationSystem res = rhs; res *= lhs; return res; } template inline EquationSystem operator*(const EquationSystem& lhs, typename EquationSystem::Scalar rhs) { EquationSystem res = lhs; res *= rhs; return res; } template inline EquationSystem operator/(const EquationSystem& lhs, typename EquationSystem::Scalar rhs) { EquationSystem res = lhs; res /= rhs; return res; } // Implementation template EquationSystem& EquationSystem::operator+=(const EquationSystem& rhs) { for (std::size_t i = 0; i < N; ++i) { m_values[i] += rhs.m_values[i]; } return *this; } template EquationSystem& EquationSystem::operator-=(const EquationSystem& rhs) { for (std::size_t i = 0; i < N; ++i) { m_values[i] -= rhs.m_values[i]; } return *this; } template EquationSystem& EquationSystem::operator*=(typename EquationSystem::Scalar rhs) { for (std::size_t i = 0; i < N; ++i) { m_values[i] *= rhs; } return *this; } template EquationSystem& EquationSystem::operator/=(typename EquationSystem::Scalar rhs) { for (std::size_t i = 0; i < N; ++i) { m_values[i] /= rhs; } return *this; } template typename EquationSystem::Scalar abs(const EquationSystem& es) { typename EquationSystem::Scalar ret(0); for (std::size_t i = 0; i < N; ++i) { using std::abs; ret = std::max(ret, abs(es[i])); } return ret; } } #endif // VZ_EQUATIONSYSTEM_HPP