Neuron®
The Neuron® is the basis for the creation of open and secure federated networks for smart societies.
Loading...
Searching...
No Matches
Subtract.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 EvaluateSubtraction(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 EvaluateSubtraction(Left, Right, this);
56 }
57
65 public static IElement EvaluateSubtraction(IElement Left, IElement Right, ScriptNode Node)
66 {
67 IElement Result;
68 IGroupElement Temp;
69
70 if (Left is IGroupElement LE && Right is IGroupElement RE)
71 {
72 Temp = RE.Negate();
73 if (!(Temp is null))
74 {
75 Result = LE.AddRight(Temp);
76 if (!(Result is null))
77 return Result;
78
79 Result = Temp.AddLeft(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 IGroupElement;
97 RE = Right as IGroupElement;
98 if (!(LE is null) && !(RE is null))
99 {
100 Temp = RE.Negate();
101 if (!(Temp is null))
102 {
103 Result = LE.AddRight(Temp);
104 if (!(Result is null))
105 return Result;
106
107 Result = Temp.AddLeft(LE);
108 if (!(Result is null))
109 return Result;
110 }
111 }
112 }
113 }
114
115 Result = EvaluateNamedOperator("op_Subtraction", Left, Right, Node);
116 if (!(Result is null))
117 return Result;
118
119 throw new ScriptRuntimeException("Operands cannot be subtracted.", Node);
120 }
121 else
122 {
123 LinkedList<IElement> Elements = new LinkedList<IElement>();
124
125 foreach (IElement RightChild in Right.ChildElements)
126 Elements.AddLast(EvaluateSubtraction(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(EvaluateSubtraction(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(EvaluateSubtraction(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(EvaluateSubtraction(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 return new Subtract(
198 Left.Differentiate(VariableName, Variables),
199 Right.Differentiate(VariableName, Variables),
200 this.Start, this.Length, this.Expression);
201 }
202 else
203 throw new ScriptRuntimeException("Terms not differentiable.", this);
204 }
205
206 }
207}
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
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
Subtract(ScriptNode Left, ScriptNode Right, int Start, int Length, Expression Expression)
Subtraction operator.
Definition: Subtract.cs:24
static IElement EvaluateSubtraction(IElement Left, IElement Right, ScriptNode Node)
Subtracts the right operand from the left one.
Definition: Subtract.cs:65
override IElement Evaluate(Variables Variables)
Evaluates the node, using the variables provided in the Variables collection.
Definition: Subtract.cs:34
ScriptNode Differentiate(string VariableName, Variables Variables)
Differentiates a script node, if possible.
Definition: Subtract.cs:192
override async Task< IElement > EvaluateAsync(Variables Variables)
Evaluates the node, using the variables provided in the Variables collection.
Definition: Subtract.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 group elements.
Definition: IGroupElement.cs:9
IGroupElement Negate()
Negates the element.
ISemiGroupElement AddLeft(ISemiGroupElement Element)
Tries to add 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.