diff options
Diffstat (limited to 'src/vZ')
-rw-r--r-- | src/vZ/EquationSystem.hpp | 154 | ||||
-rw-r--r-- | src/vZ/Integrator.hpp | 4 |
2 files changed, 156 insertions, 2 deletions
diff --git a/src/vZ/EquationSystem.hpp b/src/vZ/EquationSystem.hpp new file mode 100644 index 0000000..0963baf --- /dev/null +++ b/src/vZ/EquationSystem.hpp @@ -0,0 +1,154 @@ +/************************************************************************* + * Copyright (C) 2009-2010 Tavian Barnes <tavianator@gmail.com> * + * * + * 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 * + * <http://www.gnu.org/licenses/>. * + *************************************************************************/ + +#ifndef VZ_EQUATIONSYSTEM_HPP +#define VZ_EQUATIONSYSTEM_HPP + +#include <cstddef> + +namespace vZ +{ + // A class to easily represent a system of ODEs + template <std::size_t N, typename T = double> + class EquationSystem + { + public: + typedef typename Traits<T>::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 <std::size_t N, typename T> + class Traits<EquationSystem<N, T> > + { + public: + typedef typename Traits<T>::Scalar Scalar; + + private: + Traits(); + }; + + // Binary operators + + template <std::size_t N, typename T> + inline EquationSystem<N, T> + operator+(const EquationSystem<N, T>& lhs, const EquationSystem<N, T>& rhs) + { + EquationSystem<N, T> res = lhs; + res += rhs; + return res; + } + + template <std::size_t N, typename T> + inline EquationSystem<N, T> + operator-(const EquationSystem<N, T>& lhs, const EquationSystem<N, T>& rhs) + { + EquationSystem<N, T> res = lhs; + res -= rhs; + return res; + } + + template <std::size_t N, typename T> + inline EquationSystem<N, T> + operator*(typename EquationSystem<N, T>::Scalar lhs, + const EquationSystem<N, T>& rhs) + { + EquationSystem<N, T> res = rhs; + res *= lhs; + return res; + } + + template <std::size_t N, typename T> + inline EquationSystem<N, T> + operator*(const EquationSystem<N, T>& lhs, + typename EquationSystem<N, T>::Scalar rhs) + { + EquationSystem<N, T> res = lhs; + res *= rhs; + return res; + } + + template <std::size_t N, typename T> + inline EquationSystem<N, T> + operator/(const EquationSystem<N, T>& lhs, + typename EquationSystem<N, T>::Scalar rhs) + { + EquationSystem<N, T> res = lhs; + res /= rhs; + return res; + } + + // Implementation + + template <std::size_t N, typename T> + EquationSystem<N, T>& + EquationSystem<N, T>::operator+=(const EquationSystem<N, T>& rhs) + { + for (std::size_t i = 0; i < N; ++i) { + m_values[i] += rhs.m_values[i]; + } + return *this; + } + + template <std::size_t N, typename T> + EquationSystem<N, T>& + EquationSystem<N, T>::operator-=(const EquationSystem<N, T>& rhs) + { + for (std::size_t i = 0; i < N; ++i) { + m_values[i] -= rhs.m_values[i]; + } + return *this; + } + + template <std::size_t N, typename T> + EquationSystem<N, T>& + EquationSystem<N, T>::operator*=(typename EquationSystem<N, T>::Scalar rhs) + { + for (std::size_t i = 0; i < N; ++i) { + m_values[i] *= rhs; + } + return *this; + } + + template <std::size_t N, typename T> + EquationSystem<N, T>& + EquationSystem<N, T>::operator/=(typename EquationSystem<N, T>::Scalar rhs) + { + for (std::size_t i = 0; i < N; ++i) { + m_values[i] /= rhs; + } + return *this; + } +} + +#endif // VZ_EQUATIONSYSTEM_HPP diff --git a/src/vZ/Integrator.hpp b/src/vZ/Integrator.hpp index c6d6fbe..cb9649a 100644 --- a/src/vZ/Integrator.hpp +++ b/src/vZ/Integrator.hpp @@ -39,9 +39,9 @@ namespace vZ typedef typename Traits<Y>::Scalar Scalar; typedef std::tr1::function<Y (Scalar, Y)> Function; - // By default, y and t start at zero, h starts UNDEFINED + // By default, y, t, and h start UNDEFINED GenericIntegrator(Function f) - : m_f(f), m_y(0), m_x(0), m_iterations(0) { } + : m_f(f),m_iterations(0) { } virtual ~GenericIntegrator() { } GenericIntegrator& y(Y y) { m_y = y; return *this; } |