In this post I will create the Parameter class. This is a class used in future numerical methods and represents a parameter on a model such as the temperature, pressure, volume, etc.
One of the properties of a parameter is its value. I will create a double field to store the parameter value and a public property to access it. The field will be called m_dValue and the property Value.
In most problems involving a model, some of the model parameters are fixed and some of the parameters are solved for. For example, using an ideal gas model, the temperature and molar volume may be fixed and the pressure solved for. To differentiate the category a parameter is currently in, I will create a Boolean field call m_bIsSolvedFor and expose it as a property called IsSolvedFor.
For some numerical methods involving parameters, I will need to numerically compute the partial derivative of a function with respect to that parameter. To do this, I will need to compute multiple function values at equally spaced parameter values around the current parameter value. I will determine the distance to use between points in the Parameter class. To do this, I will add a double field called m_dDerivativeStep and a public property called DerivativeStep. How this field is used is dependent upon the settings of another field which is an enum of type DerivativeStepType called m_enDerivativeStepType. The DerivativeStepType enum has values of Relative and Absolute. When m_enDerivativeStepType is set to Absolute, the distance between points for calculating partial derivatives is equal to the value set in m_dDerivativeStep. When m_enDerivativeStepType is set to Relative, the distance between points for calculating partial derivatives is equal to the current value of the parameter times m_dDerivativeStep. By default, m_enDerivativeStepType is set to Relative and m_dDerivativeStep is set to 0.01.
I also added an implicit converter to the class so that a Parameter can automatically be converted to a double value. The conversion returns the current Value property of the Parameter.
Multiple constructors are also created so that the various Parameter properties can be set in the constructor.
The full code for this class is given below.
using System;
using System.Collections.ObjectModel;
namespace NumericalMethods
{
public class Parameter
{
private bool _isSolvedFor = true;
private double _value;
private double _derivativeStep = 1e-3;
private DerivativeStepType _derivativeStepType = DerivativeStepType.Relative;
public Parameter()
: base()
{
}
public Parameter(double value)
: this()
{
_value = value;
}
public Parameter(double value, double derivativeStep)
: this(value)
{
_derivativeStep = derivativeStep;
}
public Parameter(double value, double derivativeStep, DerivativeStepType stepSizeType)
: this(value, derivativeStep)
{
_derivativeStepType = stepSizeType;
}
public Parameter(double value, double derivativeStep, DerivativeStepType stepSizeType, bool isSolvedFor)
: this(value, derivativeStep, stepSizeType)
{
_isSolvedFor = isSolvedFor;
}
public Parameter(bool isSolvedFor)
: this()
{
_isSolvedFor = isSolvedFor;
}
public Parameter(Parameter clone)
{
_isSolvedFor = clone.IsSolvedFor;
_value = clone.Value;
_derivativeStep = clone.DerivativeStep;
_derivativeStepType = clone.DerivativeStepType;
}
public bool IsSolvedFor
{
get { return _isSolvedFor; }
set { _isSolvedFor = value; }
}
public double Value
{
get { return _value; }
set { _value = value; }
}
public double DerivativeStep
{
get { return _derivativeStep; }
set { _derivativeStep = value; }
}
public double DerivativeStepSize
{
get
{
double derivativeStepSize;
if (_derivativeStepType == DerivativeStepType.Absolute)
{
derivativeStepSize = _derivativeStep;
}
else
{
if (_value != 0.0)
{
derivativeStepSize = _derivativeStep * Math.Abs(_value);
}
else
{
derivativeStepSize = _derivativeStep;
}
}
return derivativeStepSize;
}
}
public DerivativeStepType DerivativeStepType
{
get { return _derivativeStepType; }
set { _derivativeStepType = value; }
}
public static implicit operator double(Parameter p)
{
return p.Value;
}
public override string ToString()
{
return "Parameter: Value:" + Value.ToString() + " IsSolvedFor:" + _isSolvedFor.ToString();
}
}
public enum DerivativeStepType
{
Relative,
Absolute
}
public class ParameterCollection : Collection<Parameter>
{
public ParameterCollection()
: base()
{
}
}
}