Neuron®
The Neuron® is the basis for the creation of open and secure federated networks for smart societies.
Loading...
Searching...
No Matches
VectorDefinition.cs
1using System;
4using System.Numerics;
5using System.Threading.Tasks;
13
15{
20 {
30 {
31 }
32
39 {
40 ChunkedList<IElement> VectorElements = new ChunkedList<IElement>();
41
42 foreach (ScriptNode Node in this.Elements)
43 {
44 if (Node is null)
45 VectorElements.Add(ObjectValue.Null);
46 else
47 VectorElements.Add(Node.Evaluate(Variables));
48 }
49
50 return Encapsulate(VectorElements, true, this);
51 }
52
58 public override async Task<IElement> EvaluateAsync(Variables Variables)
59 {
60 if (!this.isAsync)
61 return this.Evaluate(Variables);
62
63 ChunkedList<IElement> VectorElements = new ChunkedList<IElement>();
64
65 foreach (ScriptNode Node in this.Elements)
66 {
67 if (Node is null)
68 VectorElements.Add(ObjectValue.Null);
69 else
70 VectorElements.Add(await Node.EvaluateAsync(Variables));
71 }
72
73 return Encapsulate(VectorElements, true, this);
74 }
75
83 public static IElement Encapsulate(Array Elements, bool CanEncapsulateAsMatrix, ScriptNode Node)
84 {
85 if (Elements is double[] dv)
86 return new DoubleVector(dv);
87 else if (Elements is bool[] bv)
88 return new BooleanVector(bv);
89 else if (Elements is Complex[] zv)
90 return new ComplexVector(zv);
91 else if (Elements is DateTime[] dtv)
92 return new DateTimeVector(dtv);
93 else if (Elements is byte[] Bin)
94 return new ObjectValue(Bin);
95 else
96 {
98
99 foreach (object Obj in Elements)
100 Elements2.Add(Expression.Encapsulate(Obj));
101
102 return Encapsulate(Elements2, CanEncapsulateAsMatrix, Node);
103 }
104 }
105
113 public static IElement Encapsulate(IEnumerable Elements, bool CanEncapsulateAsMatrix, ScriptNode Node)
114 {
116
117 foreach (object Obj in Elements)
118 Elements2.Add(Expression.Encapsulate(Obj));
119
120 return Encapsulate(Elements2, CanEncapsulateAsMatrix, Node);
121 }
122
130 public static IVector Encapsulate(IElement[] Elements, bool CanEncapsulateAsMatrix, ScriptNode Node)
131 {
132 return Encapsulate((ICollection<IElement>)Elements, CanEncapsulateAsMatrix, Node);
133 }
134
142 public static IVector Encapsulate(IEnumerable<object> Elements, bool CanEncapsulateAsMatrix, ScriptNode Node)
143 {
145
146 foreach (object Obj in Elements)
147 Elements2.Add(Expression.Encapsulate(Obj));
148
149 return Encapsulate(Elements2, CanEncapsulateAsMatrix, Node);
150 }
151
159 public static IVector Encapsulate(ICollection<IElement> Elements, bool CanEncapsulateAsMatrix, ScriptNode Node)
160 {
161 IElement SuperSetExample = null;
162 IElement Element2;
163 ISet CommonSuperSet = null;
164 IVectorSpaceElement Vector;
165 ISet Set;
166 int? Columns = null;
167 bool Upgraded = false;
168 bool SameDimensions = true;
169
170 foreach (IElement Element in Elements)
171 {
172 if (CanEncapsulateAsMatrix && SameDimensions)
173 {
174 Vector = Element as IVectorSpaceElement;
175 if (Vector is null)
176 SameDimensions = false;
177 else
178 {
179 if (!Columns.HasValue)
180 Columns = Vector.Dimension;
181 else if (Columns.Value != Vector.Dimension)
182 SameDimensions = false;
183 }
184 }
185
186 if (CommonSuperSet is null)
187 {
188 SuperSetExample = Element;
189
190 if (Element is null)
191 CommonSuperSet = new ObjectValues();
192 else
193 CommonSuperSet = Element.AssociatedSet;
194 }
195 else
196 {
197 if (Element is null)
198 Set = new ObjectValues();
199 else
201
202 if (!Set.Equals(CommonSuperSet))
203 {
204 Element2 = Element;
205 if (!Expression.UpgradeField(ref Element2, ref Set, ref SuperSetExample, ref CommonSuperSet))
206 {
207 CommonSuperSet = null;
208 break;
209 }
210 else
211 Upgraded = true;
212 }
213 }
214 }
215
216 if (CanEncapsulateAsMatrix && SameDimensions && Columns.HasValue)
217 {
218 IMatrix M = Matrices.MatrixDefinition.Encapsulate(Elements, Node);
219 if (M is IVector V)
220 return V;
221 else
222 throw new ScriptRuntimeException("Unable to convert matrix to vector.", Node);
223 }
224
225 if (!(CommonSuperSet is null))
226 {
227 if (Upgraded)
228 {
229 ChunkedList<IElement> SuperElements = new ChunkedList<IElement>();
230
231 foreach (IElement Element in Elements)
232 {
233 if (Element is null)
234 Set = new ObjectValues();
235 else
237
238 if (Set.Equals(CommonSuperSet))
239 SuperElements.Add(Element);
240 else
241 {
242 Element2 = Element;
243 if (Expression.UpgradeField(ref Element2, ref Set, ref SuperSetExample, ref CommonSuperSet))
244 SuperElements.Add(Element2);
245 else
246 {
247 SuperElements = null;
248 CommonSuperSet = null;
249 break;
250 }
251 }
252 }
253
254 if (!(SuperElements is null))
255 Elements = SuperElements;
256 }
257
258 if (!(CommonSuperSet is null))
259 {
260 if (CommonSuperSet is DoubleNumbers)
261 return new DoubleVector(Elements);
262 else if (CommonSuperSet is ComplexNumbers)
263 return new ComplexVector(Elements);
264 else if (CommonSuperSet is BooleanValues)
265 return new BooleanVector(Elements);
266 else if (CommonSuperSet is DateTimeValues)
267 return new DateTimeVector(Elements);
268 }
269 }
270
271 return new ObjectVector(Elements);
272 }
273
280 public override PatternMatchResult PatternMatch(IElement CheckAgainst, Dictionary<string, IElement> AlreadyFound)
281 {
283
284 if (!(CheckAgainst is IVector Vector) || Vector.Dimension != Elements.Length)
285 return PatternMatchResult.NoMatch;
286
287 PatternMatchResult Result;
288 int i = 0;
289
290 foreach (IElement E in Vector.VectorElements)
291 {
292 Result = Elements[i++].PatternMatch(E, AlreadyFound);
293 if (Result != PatternMatchResult.Match)
294 return Result;
295 }
296
297 return PatternMatchResult.Match;
298 }
299
300 }
301}
A chunked list is a linked list of chunks of objects of type T .
Definition: ChunkedList.cs:54
Base class for all types of elements.
Definition: Element.cs:14
abstract ISet AssociatedSet
Associated Set.
Definition: Element.cs:38
Base class for all types of sets.
Definition: Set.cs:14
abstract override bool Equals(object obj)
Compares the element to another.
Class managing a script expression.
Definition: Expression.cs:41
static IElement Encapsulate(object Value)
Encapsulates an object.
Definition: Expression.cs:5241
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:5327
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
virtual PatternMatchResult PatternMatch(IElement CheckAgainst, Dictionary< string, IElement > AlreadyFound)
Performs a pattern match operation.
Definition: ScriptNode.cs:169
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
The field Z_2 of boolean numbers ([0]_2, 0 or false, and [1]_2, 1 or true).
Pseudo-field of Complex numbers, as an approximation of the field of real numbers.
The set of Date & Time values.
Pseudo-field of double numbers, as an approximation of the field of real numbers.
static readonly ObjectValue Null
Null value.
Definition: ObjectValue.cs:88
Represents a list of elements.
Definition: ElementList.cs:15
bool isAsync
If any of the elements are asynchronous
Definition: ElementList.cs:22
ScriptNode[] Elements
Elements.
Definition: ElementList.cs:59
override IElement Evaluate(Variables Variables)
Evaluates the node, using the variables provided in the Variables collection.
static IVector Encapsulate(IElement[] Elements, bool CanEncapsulateAsMatrix, ScriptNode Node)
Encapsulates the elements of a vector.
static IVector Encapsulate(IEnumerable< object > Elements, bool CanEncapsulateAsMatrix, ScriptNode Node)
Encapsulates the elements of a vector.
static IElement Encapsulate(Array Elements, bool CanEncapsulateAsMatrix, ScriptNode Node)
Encapsulates the elements of a vector.
VectorDefinition(ScriptNode[] Elements, int Start, int Length, Expression Expression)
Creates a vector.
override async Task< IElement > EvaluateAsync(Variables Variables)
Evaluates the node, using the variables provided in the Variables collection.
static IVector Encapsulate(ICollection< IElement > Elements, bool CanEncapsulateAsMatrix, ScriptNode Node)
Encapsulates the elements of a vector.
override PatternMatchResult PatternMatch(IElement CheckAgainst, Dictionary< string, IElement > AlreadyFound)
Performs a pattern match operation.
static IElement Encapsulate(IEnumerable Elements, bool CanEncapsulateAsMatrix, ScriptNode Node)
Encapsulates the elements of a vector.
Collection of variables.
Definition: Variables.cs:25
Basic interface for all types of elements.
Definition: IElement.cs:21
IElement Encapsulate(ChunkedList< IElement > Elements, ScriptNode Node)
Encapsulates a set of elements into a similar structure as that provided by the current element.
Basic interface for matrices.
Definition: IMatrix.cs:7
Basic interface for vectors.
Definition: IVector.cs:9
int Dimension
Dimension of vector.
Definition: IVector.cs:14
Basic interface for all types of module elements.
Basic interface for all types of sets.
Definition: ISet.cs:10
PatternMatchResult
Status result of a pattern matching operation.
Definition: ScriptNode.cs:17