/* * 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 */ #ifdef __UTIL_MATHS_POLYNOMIAL_IPP #error Double includion of util/maths/polynomial.hpp #endif #define __UTIL_MATHS_POLYNOMIAL_IPP //----------------------------------------------------------------------------- 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; }*/