/***************************************************************************
 *   Copyright (C) 2005 by Robot Group Leipzig                             *
 *    martius@informatik.uni-leipzig.de                                    *
 *    fhesse@informatik.uni-leipzig.de                                     *
 *    der@informatik.uni-leipzig.de                                        *
 *                                                                         *
 *   This program is free software; you can redistribute it and/or modify  *
 *   it under the terms of the GNU General Public License as published by  *
 *   the Free Software Foundation; either version 2 of the License, or     *
 *   (at your option) any later version.                                   *
 *                                                                         *
 *   This program is distributed in the hope that it will be useful,       *
 *   but WITHOUT ANY WARRANTY; without even the implied warranty of        *
 *   MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the         *
 *   GNU General Public License for more details.                          *
 *                                                                         *
 *   You should have received a copy of the GNU General Public License     *
 *   along with this program; if not, write to the                         *
 *   Free Software Foundation, Inc.,                                       *
 *   59 Temple Place - Suite 330, Boston, MA  02111-1307, USA.             *
 ***************************************************************************
 *                                                                         *
 *  DESCRIPTION                                                            *
 *                                                                         *
 *   $Log: feedforwardnn.h,v $
 *   Revision 1.11  2008/05/30 11:57:12  martius
 *   do processing of inputs in learning function if network is not
 *    activated with this input before (was a very probable mistake)
 *
 *   Revision 1.10  2007/12/11 14:43:53  martius
 *   tanhr and tanhc as regularised versions of tanh
 *
 *   Revision 1.9  2007/06/08 15:45:24  martius
 *   cosmetics
 *
 *   Revision 1.8  2007/04/03 11:22:37  martius
 *   inverse of actfuns
 *
 *   Revision 1.7  2007/02/23 09:40:45  der
 *   regularisation used from regularisation.h
 *
 *   Revision 1.6  2007/02/20 15:41:06  martius
 *   big model stuff, elman and co
 *
 *   Revision 1.5  2006/07/20 17:14:34  martius
 *   removed std namespace from matrix.h
 *   storable interface
 *   abstract model and invertablemodel as superclasses for networks
 *
 *   Revision 1.4  2006/07/18 14:49:48  martius
 *   invertable networks provide the linear response function (jacobian)
 *
 *                                                                 *
 ***************************************************************************/
#ifndef __FEEDFORWARDNN_H
#define __FEEDFORWARDNN_H

#include "invertablemodel.h"
#include "controller_misc.h"
#include "regularisation.h"

#include <cmath>

/// activation function type: input: membrane potential
typedef double (*ActivationFunction) (double);
/** inverse of Activation function with respect to some membrane potential 
    and a certain output error.
 */
typedef double (*InvActivationFunction) (double, double);

/// abstract class (interface) for feed forward rate based neural networks
class FeedForwardNN : public InvertableModel {
 public: 
  FeedForwardNN() {}
  FeedForwardNN(const std::string& name, const std::string& revision)
    : InvertableModel(name, revision){};
  virtual ~FeedForwardNN(){};

  /// damps the weights and the biases by multiplying (1-damping)
  virtual void damp(double damping) =0 ;

  /******** Activation functions and there derivative 
	    and inversion with certain output shift (regularised) */
  static double linear(double z)   { return z;}
  static double dlinear(double )   { return 1;}
  static double invlinear(double z, double xsi) { return xsi;}

  static double tanh(double z)     { return ::tanh(z); } 
  static double dtanh(double z)    { double k = ::tanh(z); return 1-k*k; } 
  static double invtanh(double z, double xsi) { return g_s_inv_expand2(z,xsi)*xsi;}

  // clipped tanh
  static double tanhc(double z)     { return ::tanh(z); } 
  static double dtanhc(double z) { double k = ::tanh(clip(z, -3.0, 3.0)); return 1-k*k; }

  // regularised tanh
  static double tanhr(double z)     { return ::tanh(z); } 
  static double dtanhr(double z) { double k = tanh(z); return 1.01-k*k; }
  //  static double dtanhr(double z) { return 1/(1+z*z); }


  static double sigmoid(double z)  { return 1/(1+exp(-z)); } 
  static double dsigmoid(double z) { double k = sigmoid(clip(z, -3.0, 3.0)); return k*(1-k); } 
  static double invsigmoid(double z, double xsi) { return 1/(0.01+dsigmoid(z))*xsi;}
};


#endif
