I have got the following working
var p = new Variable("p");
var x = new Variable("x");
var y = new Variable("y");
var z = new Variable("z");
var f = Function.Pow(x, p);
var g = f[x  y * z, p  2.0] + f[x  Function.Sin(y), p  3.0];
// the following expression evaluates to (4*3)^2 + Sin(4)^3
var result = g.Value(y  4.0, z  3.0);
// the following expression evaluates to true
var test = (f == f[x  x]) && (f == f[z  3.0]);
by adding these new classes
public interface IVariableSubstitution
{
Variable Variable
{
get;
}
Function Substitute
{
get;
}
}
public class VariableSubstitution : IVariableSubstitution
{
public VariableSubstitution(Variable variable, Function substitute)
{
Variable = variable;
Substitute = substitute;
}
public Variable Variable
{
get;
protected set;
}
public Function Substitute
{
get;
protected set;
}
}
inserting the following code into the Function class
protected class SubstitutionContext
{
Dictionary<Function, Function> cache = new Dictionary<Function, Function>();
public SubstitutionContext(IVariableSubstitution[] substitutions)
{
foreach(var s in substitutions)
cache.Add(s.Variable, s.Substitute);
}
public bool Contains(Function f)
{
return cache.ContainsKey(f);
}
public Function this[Function f]
{
get
{
Function g;
if(cache.TryGetValue(f, out g))
return g;
else
{
cache[f] = g = f.Substitute(this);
return g;
}
}
}
}
protected virtual Function Substitute(SubstitutionContext context)
{
throw new NotSupportedException();
}
public Function this[params IVariableSubstitution[] substitutions]
{
get
{
SubstitutionContext context = new SubstitutionContext(substitutions);
if(context.Contains(this))
return context[this];
else
return this.Substitute(context);
}
}
and in each function class implementing a Substitute function, for example in AddFunction
protected override Function Substitute(Function.SubstitutionContext context)
{
var cf = context[f];
var cg = context[g];
if(cf != f  cg != g)
return cf + cg;
return this;
}
Let me know what you think about this approach!
