36#if defined(MPolynomial_RECURSES)
37#error Recursive header files inclusion detected in MPolynomial.h
40#define MPolynomial_RECURSES
42#if !defined MPolynomial_h
50#include "DGtal/base/Common.h"
56 template <
int n,
typename TRing,
57 typename TAlloc = std::allocator<TRing> >
59 template <
int N,
int n,
typename TRing,
60 typename TAlloc = std::allocator<TRing> >
61 class MPolynomialDerivativeComputer;
62 template <
int n,
typename TRing,
63 typename TAlloc = std::allocator<TRing>,
65 class MPolynomialEvaluator;
67 template <
int n,
typename TRing,
typename TOwner,
68 typename TAlloc,
typename TX >
69 class MPolynomialEvaluatorImpl;
74 template <
typename TRing,
typename TAlloc >
88 template <
typename TRing,
typename TOwner,
89 typename TAlloc,
typename TX >
98 template <
int nn,
class TT,
class AA,
class SS>
101 template <
int nn,
class TT,
class HLHL,
class AA,
class SS>
136 for (
int i = 0; i < (int) p.myValue.size(); ++i )
138 res += (
X)(
Ring) p.myValue[i] * xx;
149 inline operator X()
const
152 myOwner.evaluate( res, EvalFun( *
this ) );
180 template <
int n,
typename TRing,
typename TOwner,
181 typename TAlloc,
typename TX >
199 typename Alloc::template rebind<X>::other >
MPolyNM1;
201 template<
int nn,
class TT,
class AA,
class SS>
204 template<
int nn,
class TT,
class HLHL,
class AA,
class SS>
221 template <
typename XX,
typename Fun >
244 for (
int i = 0; i < (int) p.myValue.size(); ++i )
246 res +=
myEvalFun(p.myValue[i]) * (XX) xx;
259 template <
typename XX,
typename Fun >
261 void evaluate( XX & res,
const Fun & evalfun )
const
295 for (
int i = 0; i < (int) p.myValue.size(); ++i )
297 res += ( (
MPolyNM1) p.myValue[i] ) * xx;
323 template <
typename XX >
332 Alloc, XX >( *
this, x );
344 template <
typename TRing,
typename TAlloc,
typename TX >
399 template <
int n,
typename TRing,
typename TAlloc,
typename TX >
404 template<
int nn,
class TT,
class HLHL,
class AA,
class SS>
413 typename Alloc::template rebind<X>::other >
MPolyNM1;
429 template <
typename XX,
typename Fun >
431 void evaluate( XX & res,
const Fun & evalfun )
const
461 template <
typename XX>
494 template <
typename TRing,
typename TAlloc >
515 : myAllocator(allocator),
myValue(v)
541 inline operator const Ring & ()
const
719 template <
typename T,
typename TAlloc = std::allocator<T>,
720 bool usePo
inters = false>
725 typedef typename std::vector<T, Alloc>::size_type
Size;
735 :
myVec( aSize, T(), allocator )
739 :
myVec(aSize, entry, allocator)
749 myVec.resize(aSize, entry);
774 myVec.swap( v.myVec );
779 return myVec.get_allocator();
784 return myVec.get_allocator();
792 template <
typename T,
typename TAlloc>
797 typedef typename std::vector<typename Alloc::pointer, typename Alloc::template rebind<typename Alloc::pointer>::other>::size_type
Size;
801 std::vector<typename Alloc::pointer, typename Alloc::template rebind<typename Alloc::pointer>::other>
myVec;
805 for (
Size i = begin; i < end; ++i)
807 myVec[i] = myAllocator.allocate(
sizeof(T));
808 myAllocator.construct(
myVec[i], entry);
814 for (
Size i = begin; i < end; ++i)
816 myAllocator.destroy(
myVec[i]);
817 myAllocator.deallocate(
myVec[i],
sizeof(T));
822 void copy_from(
const std::vector<typename Alloc::pointer, A> & source)
826 myVec[i] = myAllocator.allocate(
sizeof(T));
827 myAllocator.construct(
myVec[i], *source[i]);
833 : myAllocator(allocator),
myVec(allocator)
837 : myAllocator(allocator),
myVec(aSize, 0, allocator)
839 create(0, aSize, T());
843 : myAllocator(allocator),
myVec(aSize, 0, allocator)
845 create(0, aSize, entry);
864 myVec.resize(v.size());
879 free(aSize, oldsize);
882 create(oldsize, aSize, entry);
897 return *
myVec.back();
902 return *
myVec.back();
912 return myVec.get_allocator();
917 return myVec.get_allocator();
953 template <
int n,
typename TRing,
class TAlloc >
958 template<
int NN,
int nn,
typename TT,
typename AA>
961 friend void euclidDiv<TRing, TAlloc>
967 template<
int nn,
typename TT,
typename AA,
typename SS>
970 template<
int nn,
typename TT,
typename HLHL,
typename AA,
typename SS>
984 typename Alloc::template rebind<MPolyNM1 >::other, (n>1) >
1059 template <
typename Ring2,
typename Alloc2 >
1073 template <
typename Ring2,
typename Alloc2 >
1171 template <
typename Ring2>
1214 return *
this = *
this * p;
1252 for (
Size i = 0; i < p.myValue.size(); ++i )
1276 if (r.myValue.size() < q.myValue.size())
1277 r.myValue.resize(q.myValue.size());
1278 for (
Size i = 0; i < q.myValue.size(); ++i )
1292 if (r.myValue.size() < q.myValue.size())
1293 r.myValue.resize(q.myValue.size());
1294 for (
Size i = 0; i < q.myValue.size(); ++i )
1309 for (
Size i = 0; i < q.myValue.size(); ++i )
1324 for (
Size i = 0; i < q.myValue.size(); ++i )
1339 if (r.myValue.size() < 1)
1340 r.myValue.resize(1);
1355 if (r.myValue.size() < 1)
1356 r.myValue.resize(1);
1370 if (r.myValue.size() < 1)
1371 r.myValue.resize(1);
1385 if (r.myValue.size() < 1)
1386 r.myValue.resize(1);
1463 if (!
isZero() && !p.isZero())
1464 for (
Size i = 0; i < r.myValue.size(); ++i )
1466 if (i < j + p.myValue.size())
1467 r[i] +=
myValue[j] * p[i - j];
1497 return !(*
this == q);
1521 return !(*
this == v);
1546 if (nonzero > 1) s <<
"(";
1551 if (first) first =
false;
1558 if (i > 1) s <<
"^" << i;
1588 template <
int N,
typename TRing,
class TAlloc>
1602 template <
int n,
typename Ring,
typename Alloc>
1614 get(
unsigned int k,
unsigned int e )
1628 template <
typename Ring,
typename Alloc>
1635 get(
unsigned int ,
unsigned int )
1651 template <
int n,
typename Ring,
typename Alloc>
1653 MPolynomial<n, Ring, Alloc>
1654 Xe_k(
unsigned int k,
unsigned int e )
1667 template <
int n,
typename Ring>
1669 MPolynomial<n, Ring, std::allocator<Ring> >
1670 Xe_k(
unsigned int k,
unsigned int e )
1683 template <
typename Ring,
typename Alloc>
1685 MPolynomial<1, Ring, Alloc>
1701 template <
typename Ring,
typename Alloc>
1703 MPolynomial<2, Ring, Alloc>
1720 template <
typename Ring,
typename Alloc>
1721 inline MPolynomial<3, Ring, Alloc>
1739 template <
typename Ring,
typename Alloc>
1741 MPolynomial<4, Ring, Alloc>
1742 mmonomial(
unsigned int e,
unsigned int f,
unsigned int g,
unsigned int h)
1755 template <
typename Ring>
1757 MPolynomial<1, Ring, std::allocator<Ring> >
1772 template <
typename Ring>
1774 MPolynomial<2, Ring, std::allocator<Ring> >
1790 template <
typename Ring>
1792 MPolynomial<3, Ring, std::allocator<Ring> >
1809 template <
typename Ring>
1811 MPolynomial<4, Ring, std::allocator<Ring> >
1813 (
unsigned int e,
unsigned int f,
unsigned int g,
unsigned int h)
1841 template <
int n,
typename Ring,
class Alloc>
1859 for (
int i = 1; i <= src.degree(); ++i )
1860 dest[i - 1] = src[i] * (Ring)i;
1886 template <
int N,
int n,
typename Ring,
typename Alloc>
1904 for (
int i = 0; i <= src.degree(); ++i )
1914 template<
typename Ring,
class Alloc>
1918 class ERROR_N_must_be_strictly_less_than_n_in_derivative_template;
1924 ERROR_N_must_be_strictly_less_than_n_in_derivative_template();
1931 template<
int N,
typename Ring,
class Alloc>
1935 class ERROR_N_must_be_strictly_less_than_n_in_derivative_template;
1941 ERROR_N_must_be_strictly_less_than_n_in_derivative_template();
1963 template <
int N,
int n,
typename Ring,
typename Alloc>
1965 MPolynomial<n, Ring, Alloc>
1986 template<
int N,
int n,
typename Ring>
1988 MPolynomial<n, Ring, std::allocator<Ring> >
1990 (
const MPolynomial<n,Ring,std::allocator<Ring> > & p)
1994 ::computeDerivative( p, res );
2001 template<
typename Ring,
typename Alloc>
2008 if (f.degree() < g.degree())
2018 for (
int i = q.degree(); i >= 0; --i)
2020 q[i] = r[i + g.degree()] / g.leading();
2021 for (
int j = g.degree(); j >= 0; --j)
2022 r[i + j] -= q[i] * g[j];
2031 template <
typename Ring>
2034 const MPolynomial<1, Ring, std::allocator<Ring> > & g,
2038 euclidDiv<Ring, std::allocator<Ring> >(f, g, q, r);
2045 template<
typename Ring,
typename Alloc>
2046 MPolynomial<1, Ring, Alloc>
2052 if (g.isZero())
return f;
2053 else return g / g.leading();
2056 d1(f / f.leading()),
2057 d2(g / g.leading()),
2058 q(f.getAllocator()),
2059 r(f.getAllocator());
2060 while (!d2.isZero())
2074 template<
typename Ring>
2075 MPolynomial<1, Ring, std::allocator<Ring> >
2077 const MPolynomial<1, Ring, std::allocator<Ring> > & g )
2079 return gcd<Ring, std::allocator<Ring> >(f, g);
2087#include "DGtal/math/MPolynomial.ih"
2094#undef MPolynomial_RECURSES
IVector(Size aSize, const Alloc &allocator=Alloc())
Alloc getAllocator() const
IVector(Size aSize, const T &entry, const Alloc &allocator=Alloc())
void copy_from(const std::vector< typename Alloc::pointer, A > &source)
IVector(const Alloc &allocator=Alloc())
void create(Size begin, Size end, typename Alloc::const_reference entry)
void resize(Size aSize, const T &entry=T())
std::vector< typenameAlloc::pointer, typenameAlloc::templaterebind< typenameAlloc::pointer >::other >::size_type Size
Alloc get_allocator() const
std::vector< typename Alloc::pointer, typename Alloc::template rebind< typename Alloc::pointer >::other > myVec
IVector(const IVector &v)
void free(Size begin, Size end)
std::vector< T, Alloc >::size_type Size
IVector(const Alloc &allocator=Alloc())
IVector(Size aSize, const Alloc &allocator=Alloc())
Alloc get_allocator() const
std::vector< T, Alloc > myVec
const T & operator[](Size i) const
IVector(Size aSize, const T &entry, const Alloc &allocator=Alloc())
Alloc getAllocator() const
void resize(Size aSize, const T &entry=T())
MPolynomial< 0, Ring, Alloc > MPoly0
static void computeDerivative(const MPoly0 &, MPoly0 &)
static void computeDerivative(const MPolyN &src, MPolyN &dest)
MPolynomial< n, Ring, Alloc > MPolyN
Type for polynomial with n variable in the ring Ring.
static void computeDerivative(const MPoly0 &, MPoly0 &)
MPolynomial< 0, Ring, Alloc > MPoly0
MPolynomial< n, Ring, Alloc > MPolyN
Type for polynomial with n variable in the ring Ring.
static void computeDerivative(const MPolyN &src, MPolyN &dest)
const MPolynomialEvaluatorImpl< n, Ring, Owner, Alloc, X > & myOwner
MPolyNM1 operator()(const MPolyN &p) const
EvalFun2(const MPolynomialEvaluatorImpl< n, Ring, Owner, Alloc, X > &owner)
const MPolynomialEvaluatorImpl< n, Ring, Owner, Alloc, X > & myOwner
XX operator()(const MPolynomial< n, Ring, Alloc > &p) const
EvalFun(const MPolynomialEvaluatorImpl< n, Ring, Owner, Alloc, X > &owner, const Fun &evalfun)
const MPolynomialEvaluatorImpl< 1, Ring, Owner, Alloc, X > & myOwner
EvalFun(const MPolynomialEvaluatorImpl< 1, Ring, Owner, Alloc, X > &owner)
const Owner & myOwner
The "owner".
MPolynomialEvaluatorImpl(const Owner &owner, const X &evalpoint)
const X & myEvalPoint
The evaluation point on this level.
void evaluate(XX &res, const Fun &evalfun) const
MPolynomial< n - 1, X, typename Alloc::template rebind< X >::other > MPolyNM1
MPolynomialEvaluatorImpl< n - 1, Ring, MPolynomialEvaluatorImpl< n, Ring, Owner, Alloc, X >, Alloc, XX > operator()(const XX &x) const
MPolynomial< n, Ring, Alloc > MPolyN
Type for the multivariate polynomial.
MPolynomialEvaluatorImpl(const Owner &owner, const X &evalpoint)
MPolynomial< 1, Ring, Alloc > MPoly1
const X & myEvalPoint
The evaluation point.
const MPoly1 & myPoly
The polynomial in question.
MPolynomialEvaluator(const MPoly1 &poly, const X &evalpoint)
MPolynomial< n, Ring, Alloc > MPolyN
const X & myEvalPoint
the evaluation point
MPolynomial< n - 1, X, typename Alloc::template rebind< X >::other > MPolyNM1
MPolynomialEvaluator(const MPolyN &poly, const X &evalpoint)
MPolynomialEvaluatorImpl< n - 1, Ring, MPolynomialEvaluator< n, Ring, Alloc, X >, Alloc, XX > operator()(const XX &x) const
void evaluate(XX &res, const Fun &evalfun) const
const MPolyN & myPoly
The polynomial in question.
MPolynomial(const Alloc &allocator)
MPolynomial(const Ring &v=0, const Alloc &allocator=Alloc())
void selfDisplay(std::ostream &s, int) const
Alloc getAllocator() const
void swap(MPolynomial &p)
Aim: Represents a multivariate polynomial, i.e. an element of , where K is some ring or field.
MPolynomial(const Ring &v, const Alloc &allocator=Alloc())
bool operator==(const MPolynomial &q) const
static MPolyNM1 myZeroPolynomial
The zero polynomial of n-1 variables for a n-multivariate polynomial.
MPolynomialEvaluator< n, Ring, Alloc, Ring > operator()(const Ring &x) const
void selfDisplay(std::ostream &s, int N=n) const
MPolynomial(const Alloc &allocator=Alloc())
friend MPolynomial operator*(const Ring &v, const MPolynomial &p)
MPolynomial & operator-=(const MPolynomial &q)
MPolynomial operator/(const Ring &v) const
MPolynomial & operator/=(const Ring &v)
MPolynomial< n - 1, Ring, Alloc > MPolyNM1
MPolynomial operator-() const
MPolynomial & operator+=(const MPolynomial &q)
MPolynomial(const MPolyNM1 &pdm1, const Alloc &)
MPolyNM1 & operator[](Size i)
MPolynomial & operator*=(const MPolynomial &p)
Alloc getAllocator() const
bool operator!=(const MPolynomial &q) const
void swap(MPolynomial &p)
const MPolyNM1 & leading() const
MPolynomial(bool, Size s, const Alloc &)
MPolynomial(const MPolynomial< n, Ring2, Alloc2 > &p, const Alloc &allocator=Alloc())
MPolynomial & operator=(const MPolynomial< n, Ring2, Alloc2 > &p)
MPolynomial operator+(const MPolynomial &q) const
MPolynomial< 0, Ring, Alloc > get(unsigned int, unsigned int)
MPolynomial< n, Ring, Alloc > get(unsigned int k, unsigned int e)
DGtal is the top-level namespace which contains all DGtal functions and types.
void euclidDiv(const MPolynomial< 1, TRing, TAlloc > &f, const MPolynomial< 1, TRing, TAlloc > &g, MPolynomial< 1, TRing, TAlloc > &q, MPolynomial< 1, TRing, TAlloc > &r)
MPolynomial< 1, Ring, Alloc > mmonomial(unsigned int e)
MPolynomial< n, Ring, Alloc > Xe_k(unsigned int k, unsigned int e)
std::ostream & operator<<(std::ostream &out, const ClosedIntegerHalfPlane< TSpace > &object)
MPolynomial< 1, Ring, Alloc > gcd(const MPolynomial< 1, Ring, Alloc > &f, const MPolynomial< 1, Ring, Alloc > &g)
MPolynomial< n, Ring, Alloc > derivative(const MPolynomial< n, Ring, Alloc > &p)