static IEnumerable <Assignment> GetEquations(ValueTerm pattern, ValueTerm term) { if (pattern is BasicValueTerm) { if (!(term is BasicValueTerm)) { return(null); } BasicValueTerm patternBasicValueTerm = (BasicValueTerm)pattern; BasicValueTerm termBasicValueTerm = (BasicValueTerm)term; if (patternBasicValueTerm != termBasicValueTerm) { return(null); } return(Enumerables.Create <Assignment>()); } if (pattern is ValueDefinition) { if (!(term is ValueDefinition)) { return(null); } ValueDefinition patternBasicValueTerm = (ValueDefinition)pattern; ValueDefinition termBasicValueTerm = (ValueDefinition)term; if (patternBasicValueTerm != termBasicValueTerm) { return(null); } return(Enumerables.Create <Assignment>()); } if (pattern is Variable) { Variable patternVariable = (Variable)pattern; return(Enumerables.Create(new Assignment(patternVariable, term))); } if (pattern is Application) { if (!(term is Application)) { return(null); } Application patternApplication = (Application)pattern; Application termApplication = (Application)term; if (patternApplication.Function != termApplication.Function) { return(null); } return(GetEquations(patternApplication.Parameter, termApplication.Parameter)); } if (pattern is Vector) { if (!(term is Vector)) { return(null); } Vector patternVector = (Vector)pattern; Vector termVector = (Vector)term; if (patternVector.Terms.Count() != termVector.Terms.Count()) { return(null); } List <Assignment> result = new List <Assignment>(); foreach (IEnumerable <Assignment> equations in Enumerable.Zip(patternVector.Terms, termVector.Terms, GetEquations)) { if (equations == null) { return(null); } result.AddRange(equations); } return(result); } if (pattern is Selection) { if (!(term is Selection)) { return(null); } Selection patternSelection = (Selection)pattern; Selection termSelection = (Selection)term; if (patternSelection.Index != termSelection.Index) { return(null); } return(GetEquations(patternSelection.Term, termSelection.Term)); } throw new InvalidOperationException(); }
public static IEnumerable <ValueTerm> GetDerivatives(this ValueTerm term, Variable variable) { IEnumerable <ValueTerm> result = DeriveAny(ValueTermDerivationRules, term, variable); if (result != null) { return(result); } if (term is BasicValueTerm) { BasicValueTerm basicValueTerm = (BasicValueTerm)term; return (( from variableIndex in Enumerable.Range(0, variable.Dimension) select Term.Constant(Enumerable.Repeat(0.0, basicValueTerm.Dimension)) ) .ToArray()); } if (term is Variable) { Variable variableTerm = (Variable)term; return (( from variableIndex in Enumerable.Range(0, variable.Dimension) select Term.Vector ( from componentIndex in Enumerable.Range(0, variableTerm.Dimension) select Term.Constant(variable == variableTerm && variableIndex == componentIndex ? 1 : 0) ) ) .ToArray()); } if (term is Application) { Application applicationTerm = (Application)term; IEnumerable <ValueTerm> functionDerivatives = ( from derivative in GetDerivatives(applicationTerm.Function) select derivative.Apply(applicationTerm.Parameter) ) .ToArray(); IEnumerable <ValueTerm> flippedFunctionDerivatives = ( from index in Enumerable.Range(0, applicationTerm.Function.CodomainDimension) select Term.Vector ( from derivative in functionDerivatives select derivative.Select(index) ) ) .ToArray(); IEnumerable <ValueTerm> parameterDerivatives = GetDerivatives(applicationTerm.Parameter, variable); return (( from parameterDerivative in parameterDerivatives select Term.Vector ( from functionDerivative in flippedFunctionDerivatives select Term.DotProduct(functionDerivative, parameterDerivative) ) ) .ToArray()); } if (term is Vector) { Vector vectorTerm = (Vector)term; return (( from subTerm in vectorTerm.Terms select GetDerivatives(subTerm, variable) ) .Flip() .Select(Term.Vector) .ToArray()); } if (term is Selection) { Selection selectionTerm = (Selection)term; return (( from derivative in GetDerivatives(selectionTerm.Term, variable) select derivative.Select(selectionTerm.Index) ) .ToArray()); } throw new InvalidOperationException(); }