This is an implementation of multi-variable polynomial in C++.
This library treats multi-variable polynomial as a map from indexes to coefficients.
For example, x^2 * y^2 - 2 * x * y ^2 + 3 is
{
{{2, 2}, 1},
{{1, 2}, -2},
{{0, 0}, 3}
}
First, you clone this repository.
git clone https://github.com/sukeya/mvPolynomial.git
Next, add the following codes in your CMakeLists.txt.
add_subdirectory(mvPolynomial)
Finally, add linked libraries.
target_link_libraries(your_exe PRIVATE mvPolynomial)
Then, you will be able to use this library.
The namespace is mvPolynomial.
The examples exist in "test" directory.
A class MVPolynomial implements multi-variable polynomials.
#include "mvPolynomial/mvPolynomial.hpp"
using MP2 = mvPolynomial::MVPolynomial<int, double, 2>;You can construct a polynomial from an initializer list of (index, coefficient) pairs.
auto p = MP2({
{{0, 0}, 1.0},
{{1, 0}, 2.0},
{{0, 1}, 3.0},
});The constant term is the zero index.
double c = p.get(Eigen::Array2i::Zero());
p.set(Eigen::Array2i({2, 0}), 5.0);If you want to read a term that may be absent, use try_get.
auto maybe_xy = p.try_get(Eigen::Array2i({1, 1}));
if (maybe_xy.has_value()) {
double coeff = *maybe_xy;
}set(index, 0.0) removes the term, and zero-coefficient terms are normalized away automatically.
The class provides basic polynomial arithmetic.
auto q = MP2({
{{0, 0}, 4.0},
{{1, 0}, 1.0},
});
auto sum = p + q;
auto sub = p - q;
auto mul = p * q;
auto div = p / 2.0;
auto pw = p.pow(3);p /= scalar and p / scalar are supported. Division by zero scalar throws std::invalid_argument.
You can evaluate a polynomial at a point.
double value = p(Eigen::Vector2d({2.0, 3.0}));You can also substitute a polynomial for one axis.
auto x = MP2({
{{0, 0}, 1.0},
{{1, 0}, 2.0},
});
auto composed = p(x, 1);Differentiation and integration are available both as member functions and as free functions.
auto dx_member = p.D(0);
auto iy_member = p.Integrate(1);
auto dx = mvPolynomial::D(p, 0);
auto iy = mvPolynomial::Integrate(p, 1);Negative indices are not supported and cause std::invalid_argument.