Neuron®
The Neuron® is the basis for the creation of open and secure federated networks for smart societies.
Loading...
Searching...
No Matches
Divide.cs
1using System;
2using System.Collections.Generic;
3using System.Threading.Tasks;
8
10{
15 {
25 : base(Left, Right, Start, Length, Expression)
26 {
27 }
28
35 {
36 IElement Left = this.left.Evaluate(Variables);
37 IElement Right = this.right.Evaluate(Variables);
38
39 return EvaluateDivision(Left, Right, this);
40 }
41
47 public override async Task<IElement> EvaluateAsync(Variables Variables)
48 {
49 if (!this.isAsync)
50 return this.Evaluate(Variables);
51
52 IElement Left = await this.left.EvaluateAsync(Variables);
53 IElement Right = await this.right.EvaluateAsync(Variables);
54
55 return EvaluateDivision(Left, Right, this);
56 }
57
65 public static IElement EvaluateDivision(IElement Left, IElement Right, ScriptNode Node)
66 {
67 IElement Result;
68 IRingElement Temp;
69
70 if (Left is IRingElement LE && Right is IRingElement RE)
71 {
72 Temp = RE.Invert();
73 if (!(Temp is null))
74 {
75 Result = LE.MultiplyRight(Temp);
76 if (!(Result is null))
77 return Result;
78
79 Result = Temp.MultiplyLeft(LE);
80 if (!(Result is null))
81 return Result;
82 }
83 }
84
85 if (Left.IsScalar)
86 {
87 if (Right.IsScalar)
88 {
89 ISet LeftSet = Left.AssociatedSet;
90 ISet RightSet = Right.AssociatedSet;
91
92 if (!LeftSet.Equals(RightSet))
93 {
94 if (Expression.UpgradeField(ref Left, ref LeftSet, ref Right, ref RightSet))
95 {
96 LE = Left as IRingElement;
97 RE = Right as IRingElement;
98 if (!(LE is null) && !(RE is null))
99 {
100 Temp = RE.Invert();
101 if (!(Temp is null))
102 {
103 Result = LE.MultiplyRight(Temp);
104 if (!(Result is null))
105 return Result;
106
107 Result = Temp.MultiplyLeft(LE);
108 if (!(Result is null))
109 return Result;
110 }
111 }
112 }
113 }
114
115 Result = EvaluateNamedOperator("op_Division", Left, Right, Node);
116 if (!(Result is null))
117 return Result;
118
119 throw new ScriptRuntimeException("Operands cannot be divided.", Node);
120 }
121 else
122 {
123 LinkedList<IElement> Elements = new LinkedList<IElement>();
124
125 foreach (IElement RightChild in Right.ChildElements)
126 Elements.AddLast(EvaluateDivision(Left, RightChild, Node));
127
128 return Right.Encapsulate(Elements, Node);
129 }
130 }
131 else
132 {
133 if (Right.IsScalar)
134 {
135 LinkedList<IElement> Elements = new LinkedList<IElement>();
136
137 foreach (IElement LeftChild in Left.ChildElements)
138 Elements.AddLast(EvaluateDivision(LeftChild, Right, Node));
139
140 return Left.Encapsulate(Elements, Node);
141 }
142 else
143 {
144 ICollection<IElement> LeftChildren = Left.ChildElements;
145 ICollection<IElement> RightChildren = Right.ChildElements;
146
147 if (LeftChildren.Count == RightChildren.Count)
148 {
149 LinkedList<IElement> Elements = new LinkedList<IElement>();
150 IEnumerator<IElement> eLeft = LeftChildren.GetEnumerator();
151 IEnumerator<IElement> eRight = RightChildren.GetEnumerator();
152
153 try
154 {
155 while (eLeft.MoveNext() && eRight.MoveNext())
156 Elements.AddLast(EvaluateDivision(eLeft.Current, eRight.Current, Node));
157 }
158 finally
159 {
160 eLeft.Dispose();
161 eRight.Dispose();
162 }
163
164 return Left.Encapsulate(Elements, Node);
165 }
166 else
167 {
168 LinkedList<IElement> LeftResult = new LinkedList<IElement>();
169
170 foreach (IElement LeftChild in LeftChildren)
171 {
172 LinkedList<IElement> RightResult = new LinkedList<IElement>();
173
174 foreach (IElement RightChild in RightChildren)
175 RightResult.AddLast(EvaluateDivision(LeftChild, RightChild, Node));
176
177 LeftResult.AddLast(Right.Encapsulate(RightResult, Node));
178 }
179
180 return Left.Encapsulate(LeftResult, Node);
181 }
182 }
183 }
184 }
185
192 public ScriptNode Differentiate(string VariableName, Variables Variables)
193 {
194 if (this.left is IDifferentiable Left &&
195 this.right is IDifferentiable Right)
196 {
197 int Start = this.Start;
198 int Len = this.Length;
200
201 return new Divide(
202 new Subtract(
203 new Multiply(
204 Left.Differentiate(VariableName, Variables),
205 this.right,
206 Start, Len, Expression),
207 new Multiply(
208 this.left,
209 Right.Differentiate(VariableName, Variables),
210 Start, Len, Expression),
211 Start, Len, Expression),
212 new Square(
213 this.right, Start, Len, Expression),
214 Start, Len, Expression);
215 }
216 else
217 throw new ScriptRuntimeException("Factors not differentiable.", this);
218 }
219
220 }
221}
Class managing a script expression.
Definition: Expression.cs:39
static bool UpgradeField(ref IElement E1, ref ISet Set1, ref IElement E2, ref ISet Set2)
Upgrades elements if necessary, to a common field extension, trying to make them compatible.
Definition: Expression.cs:5070
Base class for all binary operators.
ScriptNode right
Right operand.
ScriptNode left
Left operand.
static IElement EvaluateNamedOperator(string Name, IElement Left, IElement Right, ScriptNode Node)
Evaluates a named operator available in code-behind.
bool isAsync
If subtree is asynchroneous.
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
abstract IElement Evaluate(Variables Variables)
Evaluates the node, using the variables provided in the Variables collection. This method should be ...
virtual Task< IElement > EvaluateAsync(Variables Variables)
Evaluates the node, using the variables provided in the Variables collection. This method should be ...
Definition: ScriptNode.cs:158
static IElement EvaluateDivision(IElement Left, IElement Right, ScriptNode Node)
Divides the right operand from the left one.
Definition: Divide.cs:65
ScriptNode Differentiate(string VariableName, Variables Variables)
Differentiates a script node, if possible.
Definition: Divide.cs:192
override IElement Evaluate(Variables Variables)
Evaluates the node, using the variables provided in the Variables collection.
Definition: Divide.cs:34
Divide(ScriptNode Left, ScriptNode Right, int Start, int Length, Expression Expression)
Division operator.
Definition: Divide.cs:24
override async Task< IElement > EvaluateAsync(Variables Variables)
Evaluates the node, using the variables provided in the Variables collection.
Definition: Divide.cs:47
Collection of variables.
Definition: Variables.cs:25
Basic interface for all types of elements.
Definition: IElement.cs:20
ICollection< IElement > ChildElements
An enumeration of child elements. If the element is a scalar, this property will return null.
Definition: IElement.cs:49
IElement Encapsulate(ICollection< IElement > Elements, ScriptNode Node)
Encapsulates a set of elements into a similar structure as that provided by the current element.
bool IsScalar
If the element represents a scalar value.
Definition: IElement.cs:41
Basic interface for all types of ring elements.
Definition: IRingElement.cs:10
IRingElement Invert()
Inverts the element, if possible.
IRingElement MultiplyLeft(IRingElement Element)
Tries to multiply an element to the current element, from the left.
Basic interface for all types of sets.
Definition: ISet.cs:10
Base interface for lambda expressions.