
Boost : 
Subject: Re: [boost] Math tools polynomial enhancements
From: John Maddock (jz.maddock_at_[hidden])
Date: 20151030 13:53:56
> I'm thinking it would be a variadic tuple.
>
> here's what I had in mind:
>
> // define a polynomial with coefficients of type T (int, float, etc)
>
> template<typename T>
> using polynomial = std::tuple<T ...>
>
> // now define some operators on these polynomials
>
> template<typename T>j
> // polynomial addition  easy to implement
> operator+(polynomial<T>lhs, polynomial<T>rhs);
> // other ops ... easy to implement
> // polynomial division  aaaa  more compilicated to implement
> operator/(polynomial<T>lhs, polynomial<T>rhs);
>
These are all good ideas, however since polynomial above *is* a tuple,
you have just added arithmetic operators for all tuples, which is not so
good :(
I suspect the explosion of template instances for any nontrivial uses
would also explode compile times?
Of course C++17 may (or not) fix both of the above.
A simpler alternative might be:
template <class T, size_t N>
class polynomial;
Which implements a polynomial of maximum order N. Multiplication would
have to be mod x^N as I can't think of an error handling mechanism that
works for both runtime and constexpr contexts?
Best, John.
> // very cool functions
> template<typename T>
> constexpr polynomial<T> derivative(polynomial<T> p);
>
> template<typename T>
> constexpr polynomial<T> integrate(polynomial<T> p);
>
> // and of course we could evaluate any polynomial at
> // specific point at either compile and/or run time
> constexpr T evaluate(const polynomial<T> & p);
>
> // a taylor series has the last two terms reserved for a rational
> // representation of an error term
> template<typename T>
> using taylor_series = polynomial<T>;
>
> // evaluate
> T value(taylor_series<T>, T x);
>
> // get error value
> T error(taylor_series<T>, T x);
>
> // given a sequence of derivatives of F at point x
> // calculate a taylor series
> template<T, F>
> constexpr taylor_series::taylor_series(x = 0);
>
> // now I can replace my clunky quadrature of function
> // F with
>
> // return value and error
> template<class T, class F>
> const expr std::pair<T, T> fast_integral(
> const T & start,
> const T & finish
> ){
> using f = integrate(taylor_series<F>(3.233));
> T value = evaluate(f, finish)  evaluate(f, start);
> T error_value = abs(error(f, finish)) + abs(error(f, start));
> return std::pair<value, error_value>
> };
>
> This would be pretty useful as it stands. Of course it
> brings to mind a few more ideas
>
> a) T above is presumed to be a scalar variable  but it
> doesn't have to be. What I really need is to permit T
> to be a tuble itself so I could handle complex and
> and n dimensional space situations.
>
> b) The creating of F is problematic and tedious. For this we need
> TMP expression template driven automatic differentiation.
>
> I'm thinking you could put this together in a couple of days
>
> Robert Ramey
>
>
>
> _______________________________________________
> Unsubscribe & other changes:
> http://lists.boost.org/mailman/listinfo.cgi/boost
>
Boost list run by bdawes at acm.org, gregod at cs.rpi.edu, cpdaniel at pacbell.net, john at johnmaddock.co.uk