diff --git a/Makefile.am b/Makefile.am index 5d264f4d..4e778af8 100644 --- a/Makefile.am +++ b/Makefile.am @@ -58,6 +58,8 @@ UTIL_FILES = \ maths.hpp \ maths/matrix.cpp \ maths/matrix.hpp \ + maths/polynomial.ipp\ + maths/polynomial.hpp\ maths/vector.cpp \ maths/vector.hpp \ matrix.cpp \ diff --git a/maths/polynomial.hpp b/maths/polynomial.hpp new file mode 100644 index 00000000..b85bc094 --- /dev/null +++ b/maths/polynomial.hpp @@ -0,0 +1,37 @@ +/* + * This file is part of libgim. + * + * libgim 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 3 of the License, or (at your option) any later + * version. + * + * libgim 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 libgim. If not, see . + * + * Copyright 2010 Danny Robson + */ + +#ifndef __UTIL_MATHS_POLYNOMIAL_HPP +#define __UTIL_MATHS_POLYNOMIAL_HPP + +#include +#include + +namespace maths { + template + struct polynomial { + std::array coefficients; + + T eval (T x) const; + }; +} + +#include "polynomial.ipp" + +#endif diff --git a/maths/polynomial.ipp b/maths/polynomial.ipp new file mode 100644 index 00000000..6bbe2137 --- /dev/null +++ b/maths/polynomial.ipp @@ -0,0 +1,123 @@ +/* + * This file is part of libgim. + * + * libgim 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 3 of the License, or (at your option) any later + * version. + * + * libgim 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 libgim. If not, see . + * + * Copyright 2010 Danny Robson + */ + +template +T +maths::polynomial::eval (T x) const { + T val = 0; + T exp = 1; + + for (const T &i: coefficients) { + val += i * exp; + exp *= x; + } + + return val; +} + + +//----------------------------------------------------------------------------- +#include "../json.hpp" +#include "../types.hpp" + +namespace json { + template + struct io> { + typedef maths::polynomial type; + + static type + deserialise (const json::node &node) { + const json::array &source = node.as_array (); + type dest; + + if (source.size () != N) + throw std::runtime_error ("Polynomial size mismatch in json serialisation"); + + for (size_t i = 0; i < N; ++i) + dest.coefficients[i] = source[i].as_number (); + return dest; + } + + static std::unique_ptr + serialise (type src) { + auto dst = make_unique (); + + for (const auto &i: src.coefficients) + dst->insert (i); + + return std::unique_ptr (dst.release ()); + } + }; +} + + +namespace json { + //template + //std::unique_ptr + //io>::serialise (const maths::polynomial&); + + //template + //maths::polynomial + //io>::deserialise(const json::node &node); + /*{ + typedef maths::polynomial type; + + json::array &source = node.as_array (); + type dest; + + if (source.size () != N) + throw std::runtime_error ("Polynomial size mismatch in json serialisation"); + + for (size_t i = 0; i < N; ++i) + dest.coefficients[i] = source[i].as_number (); + return dest; + }*/ +} + + +/*namespace json { + template + struct io > { + static std::unique_ptr serialise (const maths::polynomial&); + static maths::polynomial deserialise (const json::node&); + }; + + template + std::unique_ptr + node::serialise> (const maths::polynomial &poly) { + std::unique_ptr val (new array); + + for (const double &i: poly.coefficients) + val->insert (node::serialise (i)); + + std::unique_ptr ret (val.release ()); + return ret; + } +}*/ + + +/*from_json> (const json::node &node) { + maths::polynomial val; + + const json::array &source = node.as_array (); + for (size_t i = 0; i < N; ++i) + val.coefficients[i] = source[i].as_number (); + + return val; +}*/