Newsgroups: comp.ai.neural-nets
Path: cantaloupe.srv.cs.cmu.edu!rochester!udel!delmarva.com!news.internetMCI.com!newsfeed.internetmci.com!btnet!demon!sunsite.doc.ic.ac.uk!lut.ac.uk!usenet
From: Dave Barnett <eldb3@lut.ac.uk>
Subject: Re: Transfer Functions in C++????
Sender: usenet@lut.ac.uk (Usenet-News)
Message-ID: <DHz7u4.HMM@lut.ac.uk>
Date: Mon, 13 Nov 1995 10:01:16 GMT
To: pierre
X-Nntp-Posting-Host: pceloptics.lut.ac.uk
Content-Transfer-Encoding: 7bit
Content-Type: text/plain; charset=us-ascii
References: <47vrvh$jm2@wn1.sci.kun.nl>
Mime-Version: 1.0
X-Mailer: Mozilla 1.2N (Windows; I; 16bit)
Organization: LUT
Lines: 99

"Pierre v.d. Laar" <pierre> wrote:
>Hello World,
>
>I need to make an array of functions.
>Each function has also a derivative.
>I started as follows
>
>class function
>{
>    public:
>        virtual     double      operator () (const double& x) const
 *snip*
>        virtual     double      derivative (const double& x) const
 *snip*
>};
>
>class  TANH: public function
>{
 *snip* detail deleted
>};
 *snip*   Further example deleted
>
>But using this code
>I can not make a : function array[2];
>And use it like:  array[0] = TANH();
>                  array[1] = LINEAR();
>
>Has anybody an idea how to solve this ?
>
>Is there maybe a more simpler way to implement this kind of functions?

Try using pointers to functions - 
  the class becomes

class function
{
public:
    double (*f)(double x);
    double (*df)(double x);

    double operator(const double &x) const
    {
        return (*f)(x);
    }

    double derivative(const double &x) const
    {
        return (*df)(x);
    }
    function(double (*F)(double), double (*dF)(double))
    {
        f=F;
        df=dF;
    }
    function();
}

Then specific functions become instances of the class rather than
derivaed classes.
One can then use

double dtanh(double x)
{
    double f=tanh(x);
    return (1-f*f);
}
double linear(double x)
{
   return x;
}
double unit(double x)
{
   return 1.;
}

function TANH(tanh, dtanh);
function LINEAR(linear,unit);

function a[2];
(a[0]).f=tanh;
(a[0]).df=dtanh;
(a[1]).f=linear;
(a[1]).df=unit;

Hope this is of some use,
it hasn't been tested in any way and is just an idea.
If it doesn't work straight off a little development may be
needed.


-- 
Dave the Troll @ RL                      |
                                         |   "With peers like these,
     D.Barnett1@lut.ac.uk                |    who need inferiors?"
     Alfred @ Ravensmark                 |     - Bugs Bunny
     Cathdiad @ Mountains Cauldron       |
     Karl @ ZoneMUSH                     |


