/**************************************************************************

	Markov.h												JJS 9/30/96
	
		part of CONICAL, the Computational Neuroscience Class Library
	
	This class implements a simple Markov model, i.e. a kinetic model
	based on state diagrams in which the rates of state transitions
	are time-independent.  The "value" of each state is a real number
	between 0 and 1 which represents the fraction of items (e.g.,
	channels) in that state.  This class is not suitable for simulation
	of a single item or small number of items.

	Requires:
		Stepper		-- base class

**************************************************************************/

#ifndef MARKOV_H
#define MARKOV_H

#include "Stepper.h"

// declare the Markov class

class Markov : virtual public Stepper
{
	static real unity;
	
  public:

	Markov( const int pQtyStates );			// constructor
	virtual ~Markov();
	
	virtual void Step( const real dt );		// update itsStateValues[]
	
	// setters & inspectors:
	
	real GetValue( const int pState ) const
		{ return ( pState>=0 && pState<itsQtyStates ?
			itsStateValues[pState] : 0 ); }
			
	void SetValue( const int pState, const real pValue )
		{ if ( pState>=0 && pState<itsQtyStates )
			itsStateValues[pState] = pValue; }
			
	real GetRate( const int pFrom, const int pTo ) const
		{ return ( pFrom>=0 && pFrom<itsQtyStates && pTo>=0 && pTo<itsQtyStates ?
			itsRates[pFrom*itsQtyStates+pTo] : 0 ); }
			
	void SetRate( const int pFrom, const int pTo, const real pValue  )
		{ if ( pFrom>=0 && pFrom<itsQtyStates && pTo>=0 && pTo<itsQtyStates )
			itsRates[pFrom*itsQtyStates+pTo] = pValue; }
	
	real *GetCoeff( const int pFrom, const int pTo ) const
		{ return (itsRateCoeff[pFrom*itsQtyStates+pTo] == &unity ?
			0 : itsRateCoeff[pFrom*itsQtyStates+pTo]); }
	
	void SetCoeff( const int pFrom, const int pTo, real *pPtr )
		{ if ( pFrom>=0 && pFrom<itsQtyStates && pTo>=0 && pTo<itsQtyStates )
			itsRateCoeff[pFrom*itsQtyStates+pTo] = (pPtr ? pPtr:&unity); }
	
	int GetQtyStates( void ) const
		{ return itsQtyStates; }
		
  protected:

	int itsQtyStates;					// how many states
	real *itsStateValues;				// value of each state
	real *itsRates;						// rate terms
	real **itsRateCoeff;				// rate coefficients
};

#endif
