Polynomial template

Discussion in 'C++' started by Rubén Campos, Sep 1, 2004.

  1. 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 :)
    Rubén Campos, Sep 1, 2004
    #1
    1. Advertising

  2. 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::eek:ut_of_range("Polynomial order too large"); }


    Martin

    --
    Quidquid latine dictum sit, altum viditur.
    Martin Eisenberg, Sep 1, 2004
    #2
    1. Advertising

Want to reply to this thread or ask your own question?

It takes just 2 minutes to sign up (and it's free!). Just click the sign up button to choose a username and then you can ask your own questions on the forum.
Similar Threads
  1. Manfred Balik

    polynomial division remainder

    Manfred Balik, May 12, 2004, in forum: VHDL
    Replies:
    5
    Views:
    3,308
    Mike Treseler
    May 18, 2004
  2. S.Muralidharan

    polynomial

    S.Muralidharan, Nov 4, 2004, in forum: VHDL
    Replies:
    1
    Views:
    628
    Tuukka Toivonen
    Nov 4, 2004
  3. Lothar Leidner

    quartic polynomial solver

    Lothar Leidner, Dec 22, 2003, in forum: Java
    Replies:
    2
    Views:
    3,658
    Roedy Green
    Dec 22, 2003
  4. strotee76
    Replies:
    9
    Views:
    10,956
    Brian M. Dean
    May 8, 2005
  5. Chen L.
    Replies:
    2
    Views:
    875
    Dik T. Winter
    Jul 6, 2004
Loading...

Share This Page