Velocity Reviews - Computer Hardware Reviews

Velocity Reviews > Newsgroups > Programming > C++ > Polynomial template

Reply
Thread Tools

Polynomial template

 
 
Rubén Campos
Guest
Posts: n/a
 
      09-01-2004
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


 
Reply With Quote
 
 
 
 
Martin Eisenberg
Guest
Posts: n/a
 
      09-01-2004
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:ut_of_range("Polynomial order too large"); }


Martin

--
Quidquid latine dictum sit, altum viditur.
 
Reply With Quote
 
 
 
Reply

Thread Tools

Posting Rules
You may not post new threads
You may not post replies
You may not post attachments
You may not edit your posts

BB code is On
Smilies are On
[IMG] code is On
HTML code is Off
Trackbacks are On
Pingbacks are On
Refbacks are Off


Similar Threads
Thread Thread Starter Forum Replies Last Post
Dynamic arrays - polynomial - C++ - completing the code... strotee76 C++ 9 05-08-2005 03:32 AM
polynomial S.Muralidharan VHDL 1 11-04-2004 12:44 PM
is there a inverse polynomial for the CRC generator polynomial Chen L. C Programming 2 07-06-2004 01:23 AM
polynomial division remainder Manfred Balik VHDL 5 05-18-2004 02:37 AM
quartic polynomial solver Lothar Leidner Java 2 12-22-2003 11:02 PM



Advertisments