Neuron®
The Neuron® is the basis for the creation of open and secure federated networks for smart societies.
Loading...
Searching...
No Matches
VectorForDefinition.cs
1using System.Collections.Generic;
2using System.Threading.Tasks;
9
11{
16 {
17 private readonly string variableName;
18
27 : base(Elements.LeftOperand, Elements.MiddleOperand, Elements.Middle2Operand, Elements.RightOperand, Start, Length, Expression)
28 {
29 this.variableName = Elements.VariableName;
30 }
31
35 public string VariableName => this.variableName;
36
43 {
44 if (!(this.left.Evaluate(Variables) is ICommutativeRingWithIdentityElement From))
45 throw new ScriptRuntimeException("Invalid range.", this);
46
47 if (!(this.middle.Evaluate(Variables) is ICommutativeRingWithIdentityElement To))
48 throw new ScriptRuntimeException("Invalid range.", this);
49
50 if (!(From.AssociatedSet is IOrderedSet S))
51 throw new ScriptRuntimeException("Cannot compare range.", this);
52
53 IElement Step;
54 int Direction = S.Compare(From, To);
55 bool Done;
56
57 if (!(this.middle2 is null))
58 {
59 Step = this.middle2.Evaluate(Variables);
60
61 if (Direction < 0)
62 {
63 if (S.Compare(Step, From.Zero) <= 0)
64 throw new ScriptRuntimeException("Invalid step size for corresponding range.", this);
65 }
66 else if (Direction > 0)
67 {
68 if (S.Compare(Step, From.Zero) >= 0)
69 throw new ScriptRuntimeException("Invalid step size for corresponding range.", this);
70 }
71 }
72 else
73 {
74 if (Direction <= 0)
75 Step = From.One;
76 else
77 Step = From.One.Negate();
78 }
79
80 LinkedList<IElement> Elements = new LinkedList<IElement>();
81
82 do
83 {
84 Variables[this.variableName] = From;
85 Elements.AddLast(this.right.Evaluate(Variables));
86
87 if (Direction == 0)
88 Done = true;
89 else
90 {
91 From = Arithmetics.Add.EvaluateAddition(From, Step, this) as ICommutativeRingWithIdentityElement;
92 if (From is null)
93 throw new ScriptRuntimeException("Invalid step size.", this);
94
95 if (Direction > 0)
96 Done = S.Compare(From, To) < 0;
97 else
98 Done = S.Compare(From, To) > 0;
99 }
100 }
101 while (!Done);
102
103 return this.Encapsulate(Elements);
104 }
105
111 public override async Task<IElement> EvaluateAsync(Variables Variables)
112 {
113 if (!this.isAsync)
114 return this.Evaluate(Variables);
115
116 if (!(await this.left.EvaluateAsync(Variables) is ICommutativeRingWithIdentityElement From))
117 throw new ScriptRuntimeException("Invalid range.", this);
118
119 if (!(await this.middle.EvaluateAsync(Variables) is ICommutativeRingWithIdentityElement To))
120 throw new ScriptRuntimeException("Invalid range.", this);
121
122 if (!(From.AssociatedSet is IOrderedSet S))
123 throw new ScriptRuntimeException("Cannot compare range.", this);
124
125 IElement Step;
126 int Direction = S.Compare(From, To);
127 bool Done;
128
129 if (!(this.middle2 is null))
130 {
131 Step = await this.middle2.EvaluateAsync(Variables);
132
133 if (Direction < 0)
134 {
135 if (S.Compare(Step, From.Zero) <= 0)
136 throw new ScriptRuntimeException("Invalid step size for corresponding range.", this);
137 }
138 else if (Direction > 0)
139 {
140 if (S.Compare(Step, From.Zero) >= 0)
141 throw new ScriptRuntimeException("Invalid step size for corresponding range.", this);
142 }
143 }
144 else
145 {
146 if (Direction <= 0)
147 Step = From.One;
148 else
149 Step = From.One.Negate();
150 }
151
152 LinkedList<IElement> Elements = new LinkedList<IElement>();
153
154 do
155 {
156 Variables[this.variableName] = From;
157 Elements.AddLast(await this.right.EvaluateAsync(Variables));
158
159 if (Direction == 0)
160 Done = true;
161 else
162 {
163 From = Arithmetics.Add.EvaluateAddition(From, Step, this) as ICommutativeRingWithIdentityElement;
164 if (From is null)
165 throw new ScriptRuntimeException("Invalid step size.", this);
166
167 if (Direction > 0)
168 Done = S.Compare(From, To) < 0;
169 else
170 Done = S.Compare(From, To) > 0;
171 }
172 }
173 while (!Done);
174
175 return this.Encapsulate(Elements);
176 }
177
183 protected virtual IElement Encapsulate(LinkedList<IElement> Elements)
184 {
185 return VectorDefinition.Encapsulate(Elements, true, this);
186 }
187
188 }
189}
Class managing a script expression.
Definition: Expression.cs:39
ScriptNode RightOperand
Right operand.
ScriptNode LeftOperand
Left operand.
ScriptNode right
Right operand.
ScriptNode left
Left operand.
bool isAsync
If subtree is asynchroneous.
Base class for all quaternary operators.
ScriptNode Middle2Operand
Second middle operand.
ScriptNode middle2
Second Middle operand.
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
ScriptNode middle
Middle operand.
ScriptNode MiddleOperand
Middle operand.
string VariableName
Variable Name.
Definition: For.cs:37
static IElement Encapsulate(Array Elements, bool CanEncapsulateAsMatrix, ScriptNode Node)
Encapsulates the elements of a vector.
Creates a vector using a FOR statement.
VectorForDefinition(For Elements, int Start, int Length, Expression Expression)
Creates a vector using a FOR statement.
override async 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.
virtual IElement Encapsulate(LinkedList< IElement > Elements)
Encapsulates the calculated elements.
Collection of variables.
Definition: Variables.cs:25
virtual Variable Add(string Name, object Value)
Adds a variable to the collection.
Definition: Variables.cs:122
Basic interface for all types of commutative ring with identity elements.
Basic interface for all types of elements.
Definition: IElement.cs:20
Basic interface for ordered sets.
Definition: IOrderedSet.cs:11