实现相同语法的完全不同的方法。使用成员数组极大地简化了库的开发:
使用示例:
int main()
{
constexpr auto p0 = 1.25*X;
std::cout << "p0: " << p0 << std::endl;
constexpr auto p1 = p0*p0;
std::cout << "p1: " << p1 << std::endl;
constexpr auto p2 = Y*Z; // can already multiply different monomials!!
std::cout << "p2: " << p2 << std::endl;
constexpr auto p3 = p1*p2;
std::cout << "p2: " << p2 << std::endl;
}
从辅助类型开始:
#include <type_traits>
#include <iostream>
// an array type similar to `std::array`
// but with `constexpr` operators
template < typename T, std::size_t t_dim >
struct c_array
{
T arr[t_dim];
template < typename... TT >
constexpr c_array(TT... pp)
: arr{pp...}
{}
constexpr T operator[](std::size_t i)
{ return arr[i]; }
constexpr std::size_t size()
{ return t_dim; }
};
单项式和多项式类型:
// the monomial type, stores a coefficient and an array of exponent
// the array index identifies a variable (X -> index 0, Y -> index 1, ..)
template < std::size_t t_numberOfVariables >
struct monomial
{
using ExponentT = int;
using ExponentArr = c_array < ExponentT, t_numberOfVariables >;
double coefficient;
ExponentArr exponents;
// used to simplify brace-init syntax
constexpr monomial(double c, ExponentArr e)
: coefficient{c}
, exponents(e)
{}
};
// the polynomial type, stores a sum of monomials as a list (c_array)
template < std::size_t t_numberOfVariables,
std::size_t t_numOfMonomials >
struct polynomial
{
using MonT = monomial < t_numberOfVariables >;
using MonArr = c_array < MonT, t_numOfMonomials >;
MonArr monomials;
constexpr polynomial(MonArr p)
: monomials(p)
{}
};
// output / print a polynomial
template < typename T_Char, typename T_CharTraits,
std::size_t t_nV, std::size_t t_nP >
std::basic_ostream<T_Char, T_CharTraits>&
operator <<( std::basic_ostream<T_Char, T_CharTraits>& o,
polynomial<t_nV, t_nP> const& p )
{
for(std::size_t iM = 0; iM < p.monomials.size(); ++iM)
{
auto const& m = p.monomials[iM];
std::cout << m.coefficient;
for(std::size_t iExp = 0; iExp < m.exponents.size(); ++iExp)
{
std::cout << " * X" << iExp << "^" << m.exponents[iExp];
}
if( iM+1 < p.monomials.size() )
{
std::cout << " + ";
}
}
return o;
}
几个帮手:
// helper; construct a sequence of non-type template arguments
template < std::size_t... tt_i >
struct seq
{};
template < std::size_t t_n, std::size_t... tt_i >
struct gen_seq
: gen_seq < t_n-1, t_n-1, tt_i...>
{};
template < std::size_t... tt_i >
struct gen_seq < 0, tt_i... >
: seq < tt_i... >
{};
// helper; compile-time max
template < typename T0, typename T1 >
constexpr auto c_max(T0 const& p0, T1 const& p1)
-> decltype( p0 > p1 ? p0 : p1 )
{
return p0 > p1 ? p0 : p1;
}
template < typename T, typename... TT >
constexpr auto c_max(T const& p, TT const&... pp)
-> decltype( p > c_max(pp...) ? p : c_max(pp...) )
{
return p > c_max(pp...) ? p : c_max(pp...);
}
创建命名空间范围对象:
// helper; construct a monomial as type `polynomial`
template < std::size_t t_numberOfVariables >
constexpr polynomial<t_numberOfVariables, 1>
create_polynomial(monomial<t_numberOfVariables> m)
{
return polynomial<t_numberOfVariables, 1>{ m };
}
template < std::size_t... tt_i >
constexpr monomial<sizeof...(tt_i) + 1>
create_monomial(double coefficient, int exponent, seq<tt_i...>)
{
return monomial<sizeof...(tt_i) + 1>{ coefficient,
{(int)(0*tt_i)..., exponent} };
}
template < std::size_t t_variableID >
constexpr polynomial<t_variableID, 1>
create_polynomial(double coefficient, int exponent)
{
return create_polynomial<t_variableID>(
create_monomial(coefficient, exponent, gen_seq<t_variableID-1>{}) );
}
// the namespace-scope objects
constexpr auto X = create_monomial<1>(1.0, 1);
constexpr auto Y = create_monomial<2>(1.0, 1);
constexpr auto Z = create_monomial<3>(1.0, 1);
constexpr auto X0 = create_monomial<4>(1.0, 1);
// ...
两个多项式上算术运算符的助手:
// helper; expands a monomial (-> more space in array)
// without changing its contents
template < std::size_t t_targetDim, std::size_t t_currDim,
std::size_t... tt_curr, std::size_t... tt_exp >
constexpr monomial < t_targetDim >
expand( monomial<t_currDim> m, seq<tt_curr...>, seq<tt_exp...> )
{
return {m.coefficient, {m.exponents[tt_curr]..., (int)(0*tt_exp)...}};
}
template < std::size_t t_targetDim, std::size_t t_currDim >
constexpr monomial < t_targetDim >
expand( monomial<t_currDim> m )
{
using exp = std::integral_constant < std::size_t,
(t_targetDim > t_currDim ? t_targetDim-t_currDim : 0) >;
return expand<t_targetDim>( m, gen_seq<t_currDim>{}, gen_seq<exp{}>{} );
}
乘法运算符的定义:
// helper for multiplication of polynomials with same array size
template < std::size_t t_dim, std::size_t... tt_i >
constexpr polynomial<t_dim>
multiply(polynomial<t_dim> p0, polynomial<t_dim> p1, seq<tt_i...>)
{
return { p0.m[tt_i]*p1.m[tt_i]... };
}
// polynomial*polynomial, with different array size
template < std::size_t t_dim0, std::size_t t_dim1 >
constexpr polynomial < c_max(t_dim0, t_dim1) >
operator*( polynomial<t_dim0> p0, polynomial<t_dim1> p1 )
{
using ret_dim = std::integral_constant < std::size_t,
c_max(t_dim0, t_dim1) >;
return multiply( expand<ret_dim{}>(p0), expand<ret_dim{}>(p1),
gen_seq<ret_dim{}>{} );
}
// helper for multiplication of monomials with same array size
template < std::size_t t_dim, std::size_t... tt_i >
constexpr monomial<t_dim>
multiply(monomial<t_dim> m0, monomial<t_dim> m1, seq<tt_i...>)
{
return { m0.coefficient*m1.coefficient,
{m0.exponents[tt_i]+m1.exponents[tt_i]...} };
}
// monomial*monomial, with (possibly) different array size
template < std::size_t t_dim0, std::size_t t_dim1 >
constexpr monomial < c_max(t_dim0, t_dim1) >
operator*( monomial<t_dim0> m0, monomial<t_dim1> m1 )
{
using ret_dim = std::integral_constant < std::size_t,
c_max(t_dim0, t_dim1) >;
return multiply( expand<ret_dim{}>(m0), expand<ret_dim{}>(m1),
gen_seq<ret_dim{}>{} );
}
// coefficient*monomial
template < typename T_Arithmetic, std::size_t t_dim >
constexpr monomial<t_dim>
operator*(T_Arithmetic c, monomial<t_dim> m)
{
return { c*m.coefficient, m.exponents };
}
// monomial*coefficient
template < typename T_Arithmetic, std::size_t t_dim >
constexpr monomial<t_dim>
operator*(monomial<t_dim> m, T_Arithmetic c)
{
return { m.coefficient*c, m.exponents };
}
// helper for multiplication of coefficient*polynomial
template < typename T_Arithmetic,
std::size_t t_nM, std::size_t t_nV,
std::size_t... tt_i >
constexpr polynomial<t_nM, t_nV>
multiply(T_Arithmetic c, polynomial<t_nM, t_nVs> p, seq<tt_i...>)
{
return {{c*p.monomials[tt_i]...}};
}
// helper for multiplication of polynomial*coefficient
template < typename T_Arithmetic,
std::size_t t_nM, std::size_t t_nV,
std::size_t... tt_i >
constexpr polynomial<t_nM, t_nV>
multiply(polynomial<t_nM, t_nV> p,
T_Arithmetic c, seq<tt_i...>)
{
return {{p.monomials[tt_i]*c...}};
}
// coefficient*polynomial
template < typename T_Arithmetic,
std::size_t t_nM, std::size_t t_nV >
constexpr polynomial<t_nM, t_nV>
operator*(T_Arithmetic c, polynomial<t_nM, t_nV> p)
{
return multiply(c, p, gen_seq<t_nM>{});
}
// polynomial*coefficient
template < typename T_Arithmetic,
std::size_t t_nM, std::size_t t_nV >
constexpr polynomial<t_nM, t_nV>
operator*(polynomial<t_nM, t_nV> p, T_Arithmetic c)
{
return multiply(p, c, gen_seq<t_nM>{});
}
// polynomial<N0, 1>*polynomial<N1, 1> (monomials)
template < std::size_t t_nV0,
std::size_t t_nV1 >
constexpr polynomial< c_max(t_nV0, t_nV1), 1 >
operator*(polynomial<t_nV0, 1> p0, polynomial<t_nV1, 1> p1)
{
return {{ p0.monomials[0]*p1.monomials[0] }};
}