Neuron®
The Neuron® is the basis for the creation of open and secure federated networks for smart societies.
Loading...
Searching...
No Matches
DefaultDifferentiation.cs
1using System.Threading.Tasks;
6
8{
13 {
14 private readonly int nrDifferentiations;
15
27 {
28 this.nrDifferentiations = NrDifferentiations;
29 }
30
34 public int NrDifferentiations => this.nrDifferentiations;
35
42 {
43 ScriptNode Node = this.op;
44 IElement Result = null;
45 int i;
46
47 for (i = 0; i < this.nrDifferentiations; i++)
48 {
49 if (Node is null)
51
52 Result = this.DifferentiateOnce(Node, Variables);
53 Node = Result as ScriptNode;
54 }
55
56 return Result;
57 }
58
64 public override Task<IElement> EvaluateAsync(Variables Variables)
65 {
66 return Task.FromResult<IElement>(this.Evaluate(Variables));
67 }
68
76 {
77 return ObjectValue.Null; // Node not evaluated directly.
78 }
79
80 private IElement DifferentiateOnce(ScriptNode Node, Variables Variables)
81 {
83 {
85 {
86 if (v.ValueObject is IDifferentiable Differentiable)
87 return this.DifferentiateOnce(Differentiable, Variables);
88 else if (v.ValueObject is ILambdaExpression)
89 throw new ScriptRuntimeException(VariableReference.VariableName + " not differentiable.", this);
90 else
92 }
93 else
94 {
95 IElement f = Expression.GetFunctionLambdaDefinition(VariableReference.VariableName, this.Start, this.Length, this.Expression);
96 if (!(f is null))
97 {
98 if (f is IDifferentiable Differentiable)
99 return this.DifferentiateOnce(Differentiable, Variables);
100 else
101 throw new ScriptRuntimeException(VariableReference.VariableName + " not differentiable.", this);
102 }
103 else if (this.nullCheck)
104 return ObjectValue.Null;
105 else
106 throw new ScriptRuntimeException(VariableReference.VariableName + " not defined.", this);
107 }
108 }
109 else if (Node is IDifferentiable Differentiable)
110 return this.DifferentiateOnce(Differentiable, Variables);
111 else
112 throw new ScriptRuntimeException("Not differentiable.", this);
113 }
114
115 private IElement DifferentiateOnce(IDifferentiable Differentiable, Variables Variables)
116 {
117 string VariableName = Differentiable.DefaultVariableName;
118 if (string.IsNullOrEmpty(VariableName))
119 throw new ScriptRuntimeException("No default variable available.", this);
120
121 ScriptNode Result = Differentiable.Differentiate(VariableName, Variables);
122 if (Result is IElement Element)
123 return Element;
124
125 return new LambdaDefinition(new string[] { VariableName }, new ArgumentType[] { ArgumentType.Normal },
126 Result, this.Start, this.Length, this.Expression);
127 }
128
129 }
130}
Base class for all types of elements.
Definition: Element.cs:14
Class managing a script expression.
Definition: Expression.cs:41
Base class for all unary operators performing operand null checks.
bool NullCheck
If null check is to be used.
readonly bool nullCheck
If null should be returned if operand is null.
Base class for all nodes in a parsed script tree.
Definition: ScriptNode.cs:69
int Length
Length of expression covered by node.
Definition: ScriptNode.cs:101
Expression Expression
Expression of which the node is a part.
Definition: ScriptNode.cs:177
int Start
Start position in script expression.
Definition: ScriptNode.cs:92
Represents a variable reference.
static readonly DoubleNumber ZeroElement
0
Definition: DoubleNumber.cs:16
static readonly ObjectValue Null
Null value.
Definition: ObjectValue.cs:88
override Task< IElement > EvaluateAsync(Variables Variables)
Evaluates the node, using the variables provided in the Variables collection.
override IElement Evaluate(Variables Variables)
Evaluates the node, using the variables provided in the Variables collection.
DefaultDifferentiation(ScriptNode Operand, int NrDifferentiations, bool NullCheck, int Start, int Length, Expression Expression)
Default Differentiation operator.
override IElement Evaluate(IElement Operand, Variables Variables)
Evaluates the operator.
Contains information about a variable.
Definition: Variable.cs:10
Collection of variables.
Definition: Variables.cs:25
virtual bool TryGetVariable(string Name, out Variable Variable)
Tries to get a variable object, given its name.
Definition: Variables.cs:56
Basic interface for all types of elements.
Definition: IElement.cs:21
Base interface for lambda expressions.
ScriptNode Differentiate(string VariableName, Variables Variables)
Differentiates a script node, if possible.
string DefaultVariableName
Default variable name, if any, null otherwise.
Base interface for lambda expressions.
ArgumentType
Type of parameter used in a function definition or a lambda definition.
Definition: IFunction.cs:9