471,107 Members | 1,757 Online
Bytes | Software Development & Data Engineering Community
Post +

Home Posts Topics Members FAQ

Join Bytes to post your question to a community of 471,107 software developers and data experts.

Polynomial template

I've trying to implement polynomials of arbitrary order as a C++ template,
as shown here:

template <unsigned long int N>
class Polynomial
{
public:
Polynomial ();
~Polynomial ();

float getCoefficient (unsigned long int
const index);
void setCoefficient (unsigned long int const
index, float const value);

float evaluate (float const x);

Polynomial <N-1> derive ();
Polynomial <N+1> integrate ();

private:
float mCoefficients[N+1];
};

As could be expected, I've found problems with the extreme order
polynomials, that is, trying to derive a polynomial of 0 order (in fact, a
real number) or trying to integrate a polynomial of maximum order ((2^N) -
1).

So the question is, how can I deal with extreme order polynomials? It's
possible to provide different declarations (not only definitions) to
derive() and integrate()? Should I detect those illegal attempts and break
it with exception throwing?

Thank you :-)
Jul 22 '05 #1
1 3434
Rubén Campos wrote:
I've trying to implement polynomials of arbitrary order as a C++
template, as shown here:

template <unsigned long int N>
class Polynomial
{
public:
Polynomial ();
~Polynomial ();

float getCoefficient (unsigned long int const index);
void setCoefficient (unsigned long int const index, float const value);
float evaluate (float const x);

Polynomial <N-1> derive ();
Polynomial <N+1> integrate ();

private:
float mCoefficients[N+1];
};

As could be expected, I've found problems with the extreme order
polynomials, that is, trying to derive a polynomial of 0 order
(in fact, a real number) or trying to integrate a polynomial of
maximum order ((2^N) - 1).

So the question is, how can I deal with extreme order
polynomials? It's possible to provide different declarations
(not only definitions) to derive() and integrate()? Should I
detect those illegal attempts and break it with exception
throwing?


You could specialize Polynomial, but that would mean a lot of
duplication, so you might prefer to make derive and integrate
overloaded free functions.

#include <limits>
#include <stdexcept>

template <unsigned long int N>
Polynomial <N-1> derive(Polynomial <N> const & poly)
{ /* ... */ }

Polynomial <0> derive(Polynomial <0> const &)
{ return Polynomial <0> (); }
// I assume a fresh polynomial is identically zero.

template <unsigned long int N>
Polynomial <N+1> integrate(Polynomial <N> const & poly)
{ /* ... */ }

void integrate(Polynomial<std::numeric_limits<
unsigned long int>::max()> const &)
{ throw std::out_of_range("Polynomial order too large"); }
Martin

--
Quidquid latine dictum sit, altum viditur.
Jul 22 '05 #2

This discussion thread is closed

Replies have been disabled for this discussion.

Similar topics

2 posts views Thread by temper3243 | last post: by
2 posts views Thread by UnixUser | last post: by
14 posts views Thread by Tiza Naziri | last post: by
12 posts views Thread by daniel.wolff | last post: by

By using Bytes.com and it's services, you agree to our Privacy Policy and Terms of Use.

To disable or enable advertisements and analytics tracking please visit the manage ads & tracking page.