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;
2using System.Threading.Tasks;
7
9{
14 {
15 private readonly int nrDifferentiations;
16
28 {
29 this.nrDifferentiations = NrDifferentiations;
30 }
31
35 public int NrDifferentiations => this.nrDifferentiations;
36
43 {
44 ScriptNode Node = this.op;
45 IElement Result = null;
46 int i;
47
48 for (i = 0; i < this.nrDifferentiations; i++)
49 {
50 if (Node is null)
52
53 Result = this.DifferentiateOnce(Node, Variables);
54 Node = Result as ScriptNode;
55 }
56
57 return Result;
58 }
59
65 public override Task<IElement> EvaluateAsync(Variables Variables)
66 {
67 return Task.FromResult<IElement>(this.Evaluate(Variables));
68 }
69
77 {
78 return ObjectValue.Null; // Node not evaluated directly.
79 }
80
81 private IElement DifferentiateOnce(ScriptNode Node, Variables Variables)
82 {
84 {
86 {
87 if (v.ValueObject is IDifferentiable Differentiable)
88 return this.DifferentiateOnce(Differentiable, Variables);
89 else if (v.ValueObject is ILambdaExpression)
90 throw new ScriptRuntimeException(VariableReference.VariableName + " not differentiable.", this);
91 else
93 }
94 else
95 {
96 LambdaDefinition f = Expression.GetFunctionLambdaDefinition(VariableReference.VariableName, this.Start, this.Length, this.Expression);
97 if (!(f is null))
98 {
99 if (f is IDifferentiable Differentiable)
100 return this.DifferentiateOnce(Differentiable, Variables);
101 else
102 throw new ScriptRuntimeException(VariableReference.VariableName + " not differentiable.", this);
103 }
104 else if (this.nullCheck)
105 return ObjectValue.Null;
106 else
107 throw new ScriptRuntimeException(VariableReference.VariableName + " not defined.", this);
108 }
109 }
110 else if (Node is IDifferentiable Differentiable)
111 return this.DifferentiateOnce(Differentiable, Variables);
112 else
113 throw new ScriptRuntimeException("Not differentiable.", this);
114 }
115
116 private IElement DifferentiateOnce(IDifferentiable Differentiable, Variables Variables)
117 {
118 string VariableName = Differentiable.DefaultVariableName;
119 if (string.IsNullOrEmpty(VariableName))
120 throw new ScriptRuntimeException("No default variable available.", this);
121
122 ScriptNode Result = Differentiable.Differentiate(VariableName, Variables);
123 if (Result is IElement Element)
124 return Element;
125
126 return new LambdaDefinition(new string[] { VariableName }, new ArgumentType[] { ArgumentType.Normal },
127 Result, this.Start, this.Length, this.Expression);
128 }
129
130 }
131}
Base class for all types of elements.
Definition: Element.cs:13
Class managing a script expression.
Definition: Expression.cs:39
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:86
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:52
Basic interface for all types of elements.
Definition: IElement.cs:20
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