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;
2using System.Collections.Generic;
3using System.Numerics;
4using System.Threading.Tasks;
11
13{
18 {
28 {
29 }
30
37 {
38 LinkedList<IElement> VectorElements = new LinkedList<IElement>();
39
40 foreach (ScriptNode Node in this.Elements)
41 {
42 if (Node is null)
43 VectorElements.AddLast(ObjectValue.Null);
44 else
45 VectorElements.AddLast(Node.Evaluate(Variables));
46 }
47
48 return Encapsulate(VectorElements, true, this);
49 }
50
56 public override async Task<IElement> EvaluateAsync(Variables Variables)
57 {
58 if (!this.isAsync)
59 return this.Evaluate(Variables);
60
61 LinkedList<IElement> VectorElements = new LinkedList<IElement>();
62
63 foreach (ScriptNode Node in this.Elements)
64 {
65 if (Node is null)
66 VectorElements.AddLast(ObjectValue.Null);
67 else
68 VectorElements.AddLast(await Node.EvaluateAsync(Variables));
69 }
70
71 return Encapsulate(VectorElements, true, this);
72 }
73
81 public static IElement Encapsulate(Array Elements, bool CanEncapsulateAsMatrix, ScriptNode Node)
82 {
83 if (Elements is double[] dv)
84 return new DoubleVector(dv);
85 else if (Elements is bool[] bv)
86 return new BooleanVector(bv);
87 else if (Elements is Complex[] zv)
88 return new ComplexVector(zv);
89 else if (Elements is DateTime[] dtv)
90 return new DateTimeVector(dtv);
91 else if (Elements is byte[] Bin)
92 return new ObjectValue(Bin);
93 else
94 {
95 LinkedList<IElement> Elements2 = new LinkedList<IElement>();
96
97 foreach (object Obj in Elements)
98 Elements2.AddLast(Expression.Encapsulate(Obj));
99
100 return Encapsulate(Elements2, CanEncapsulateAsMatrix, Node);
101 }
102 }
103
111 public static IVector Encapsulate(IElement[] Elements, bool CanEncapsulateAsMatrix, ScriptNode Node)
112 {
113 return Encapsulate((ICollection<IElement>)Elements, CanEncapsulateAsMatrix, Node);
114 }
115
123 public static IVector Encapsulate(IEnumerable<object> Elements, bool CanEncapsulateAsMatrix, ScriptNode Node)
124 {
125 LinkedList<IElement> Elements2 = new LinkedList<IElement>();
126
127 foreach (object Obj in Elements)
128 Elements2.AddLast(Expression.Encapsulate(Obj));
129
130 return Encapsulate(Elements2, CanEncapsulateAsMatrix, Node);
131 }
132
140 public static IVector Encapsulate(ICollection<IElement> Elements, bool CanEncapsulateAsMatrix, ScriptNode Node)
141 {
142 IElement SuperSetExample = null;
143 IElement Element2;
144 ISet CommonSuperSet = null;
145 IVectorSpaceElement Vector;
146 ISet Set;
147 int? Columns = null;
148 bool Upgraded = false;
149 bool SameDimensions = true;
150
151 foreach (IElement Element in Elements)
152 {
153 if (CanEncapsulateAsMatrix && SameDimensions)
154 {
155 Vector = Element as IVectorSpaceElement;
156 if (Vector is null)
157 SameDimensions = false;
158 else
159 {
160 if (!Columns.HasValue)
161 Columns = Vector.Dimension;
162 else if (Columns.Value != Vector.Dimension)
163 SameDimensions = false;
164 }
165 }
166
167 if (CommonSuperSet is null)
168 {
169 SuperSetExample = Element;
170
171 if (Element is null)
172 CommonSuperSet = new ObjectValues();
173 else
174 CommonSuperSet = Element.AssociatedSet;
175 }
176 else
177 {
178 if (Element is null)
179 Set = new ObjectValues();
180 else
182
183 if (!Set.Equals(CommonSuperSet))
184 {
185 Element2 = Element;
186 if (!Expression.UpgradeField(ref Element2, ref Set, ref SuperSetExample, ref CommonSuperSet))
187 {
188 CommonSuperSet = null;
189 break;
190 }
191 else
192 Upgraded = true;
193 }
194 }
195 }
196
197 if (CanEncapsulateAsMatrix && SameDimensions && Columns.HasValue)
198 {
199 IMatrix M = Matrices.MatrixDefinition.Encapsulate(Elements, Node);
200 if (M is IVector V)
201 return V;
202 else
203 throw new ScriptRuntimeException("Unable to convert matrix to vector.", Node);
204 }
205
206 if (!(CommonSuperSet is null))
207 {
208 if (Upgraded)
209 {
210 LinkedList<IElement> SuperElements = new LinkedList<IElement>();
211
212 foreach (IElement Element in Elements)
213 {
214 if (Element is null)
215 Set = new ObjectValues();
216 else
218
219 if (Set.Equals(CommonSuperSet))
220 SuperElements.AddLast(Element);
221 else
222 {
223 Element2 = Element;
224 if (Expression.UpgradeField(ref Element2, ref Set, ref SuperSetExample, ref CommonSuperSet))
225 SuperElements.AddLast(Element2);
226 else
227 {
228 SuperElements = null;
229 CommonSuperSet = null;
230 break;
231 }
232 }
233 }
234
235 if (!(SuperElements is null))
236 Elements = SuperElements;
237 }
238
239 if (!(CommonSuperSet is null))
240 {
241 if (CommonSuperSet is DoubleNumbers)
242 return new DoubleVector(Elements);
243 else if (CommonSuperSet is ComplexNumbers)
244 return new ComplexVector(Elements);
245 else if (CommonSuperSet is BooleanValues)
246 return new BooleanVector(Elements);
247 else if (CommonSuperSet is DateTimeValues)
248 return new DateTimeVector(Elements);
249 }
250 }
251
252 return new ObjectVector(Elements);
253 }
254
261 public override PatternMatchResult PatternMatch(IElement CheckAgainst, Dictionary<string, IElement> AlreadyFound)
262 {
264
265 if (!(CheckAgainst is IVector Vector) || Vector.Dimension != Elements.Length)
266 return PatternMatchResult.NoMatch;
267
268 PatternMatchResult Result;
269 int i = 0;
270
271 foreach (IElement E in Vector.VectorElements)
272 {
273 Result = Elements[i++].PatternMatch(E, AlreadyFound);
274 if (Result != PatternMatchResult.Match)
275 return Result;
276 }
277
278 return PatternMatchResult.Match;
279 }
280
281 }
282}
Base class for all types of elements.
Definition: Element.cs:13
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:39
static IElement Encapsulate(object Value)
Encapsulates an object.
Definition: Expression.cs:4955
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 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:86
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.
Collection of variables.
Definition: Variables.cs:25
Basic interface for all types of elements.
Definition: IElement.cs:20
IElement Encapsulate(ICollection< 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:9
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