Add a simple polynomial class
This commit is contained in:
parent
7bad091cfe
commit
1be63bb760
@ -58,6 +58,8 @@ UTIL_FILES = \
|
|||||||
maths.hpp \
|
maths.hpp \
|
||||||
maths/matrix.cpp \
|
maths/matrix.cpp \
|
||||||
maths/matrix.hpp \
|
maths/matrix.hpp \
|
||||||
|
maths/polynomial.ipp\
|
||||||
|
maths/polynomial.hpp\
|
||||||
maths/vector.cpp \
|
maths/vector.cpp \
|
||||||
maths/vector.hpp \
|
maths/vector.hpp \
|
||||||
matrix.cpp \
|
matrix.cpp \
|
||||||
|
37
maths/polynomial.hpp
Normal file
37
maths/polynomial.hpp
Normal file
@ -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 <http://www.gnu.org/licenses/>.
|
||||||
|
*
|
||||||
|
* Copyright 2010 Danny Robson <danny@nerdcruft.net>
|
||||||
|
*/
|
||||||
|
|
||||||
|
#ifndef __UTIL_MATHS_POLYNOMIAL_HPP
|
||||||
|
#define __UTIL_MATHS_POLYNOMIAL_HPP
|
||||||
|
|
||||||
|
#include <array>
|
||||||
|
#include <cstddef>
|
||||||
|
|
||||||
|
namespace maths {
|
||||||
|
template <typename T, size_t N>
|
||||||
|
struct polynomial {
|
||||||
|
std::array<T,N> coefficients;
|
||||||
|
|
||||||
|
T eval (T x) const;
|
||||||
|
};
|
||||||
|
}
|
||||||
|
|
||||||
|
#include "polynomial.ipp"
|
||||||
|
|
||||||
|
#endif
|
123
maths/polynomial.ipp
Normal file
123
maths/polynomial.ipp
Normal file
@ -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 <http://www.gnu.org/licenses/>.
|
||||||
|
*
|
||||||
|
* Copyright 2010 Danny Robson <danny@nerdcruft.net>
|
||||||
|
*/
|
||||||
|
|
||||||
|
template <typename T, size_t N>
|
||||||
|
T
|
||||||
|
maths::polynomial<T,N>::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 <typename T, size_t N>
|
||||||
|
struct io<maths::polynomial<T,N>> {
|
||||||
|
typedef maths::polynomial<T,N> 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<json::node>
|
||||||
|
serialise (type src) {
|
||||||
|
auto dst = make_unique<json::array> ();
|
||||||
|
|
||||||
|
for (const auto &i: src.coefficients)
|
||||||
|
dst->insert (i);
|
||||||
|
|
||||||
|
return std::unique_ptr<json::node> (dst.release ());
|
||||||
|
}
|
||||||
|
};
|
||||||
|
}
|
||||||
|
|
||||||
|
|
||||||
|
namespace json {
|
||||||
|
//template <typename T, size_t N>
|
||||||
|
//std::unique_ptr<json::node>
|
||||||
|
//io<maths::polynomial<T,N>>::serialise (const maths::polynomial<T,N>&);
|
||||||
|
|
||||||
|
//template <typename T, size_t N>
|
||||||
|
//maths::polynomial<T,N>
|
||||||
|
//io<maths::polynomial<T,N>>::deserialise(const json::node &node);
|
||||||
|
/*{
|
||||||
|
typedef maths::polynomial<T,N> 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 <typename T, size_t N>
|
||||||
|
struct io <maths::polynomial<T,N>> {
|
||||||
|
static std::unique_ptr<json::node> serialise (const maths::polynomial<T,N>&);
|
||||||
|
static maths::polynomial<T,N> deserialise (const json::node&);
|
||||||
|
};
|
||||||
|
|
||||||
|
template <typename T, size_t N>
|
||||||
|
std::unique_ptr<node>
|
||||||
|
node::serialise<maths::polynomial<T,N>> (const maths::polynomial<T,N> &poly) {
|
||||||
|
std::unique_ptr<array> val (new array);
|
||||||
|
|
||||||
|
for (const double &i: poly.coefficients)
|
||||||
|
val->insert (node::serialise<double> (i));
|
||||||
|
|
||||||
|
std::unique_ptr<node> ret (val.release ());
|
||||||
|
return ret;
|
||||||
|
}
|
||||||
|
}*/
|
||||||
|
|
||||||
|
|
||||||
|
/*from_json<maths::polynomial<T,N>> (const json::node &node) {
|
||||||
|
maths::polynomial<T,N> 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;
|
||||||
|
}*/
|
Loading…
Reference in New Issue
Block a user