Neuron®
The Neuron® is the basis for the creation of open and secure federated networks for smart societies.
Loading...
Searching...
No Matches
MatrixDefinition.cs
1using System.Collections.Generic;
2using System.Threading.Tasks;
9
11{
16 {
25 : base(Rows, Start, Length, Expression)
26 {
27 }
28
35 {
36 LinkedList<IElement> Rows = new LinkedList<IElement>();
37
38 foreach (ScriptNode Node in this.Elements)
39 Rows.AddLast(Node.Evaluate(Variables));
40
41 return Encapsulate(Rows, this);
42 }
43
49 public override async Task<IElement> EvaluateAsync(Variables Variables)
50 {
51 if (!this.isAsync)
52 return this.Evaluate(Variables);
53
54 LinkedList<IElement> Rows = new LinkedList<IElement>();
55
56 foreach (ScriptNode Node in this.Elements)
57 Rows.AddLast(await Node.EvaluateAsync(Variables));
58
59 return Encapsulate(Rows, this);
60 }
67 public static IMatrix Encapsulate(ICollection<IElement> Rows, ScriptNode Node)
68 {
69 LinkedList<IElement> Elements = new LinkedList<IElement>();
71 int? Columns = null;
72 int i;
73
74 foreach (IElement Row in Rows)
75 {
76 Vector = Row as IVectorSpaceElement;
77
78 if (Vector is null)
79 {
80 Columns = -1;
81 break;
82 }
83 else
84 {
85 i = Vector.Dimension;
86 if (Columns.HasValue)
87 {
88 if (Columns.Value != i)
89 {
90 Columns = -1;
91 break;
92 }
93 }
94 else
95 Columns = i;
96
97 foreach (IElement Element in Vector.VectorElements)
98 Elements.AddLast(Element);
99 }
100 }
101
102 if (!Columns.HasValue || Columns.Value < 0)
103 {
104 IVector V = Vectors.VectorDefinition.Encapsulate(Rows, false, Node);
105 if (V is IMatrix M)
106 return M;
107 else
108 throw new ScriptRuntimeException("Unable to convert vector of vectors to matrix.", Node);
109 }
110 else
111 return Encapsulate(Elements, Rows.Count, Columns.Value, Node);
112 }
113
122 public static IMatrix Encapsulate(ICollection<IElement> Elements, int Rows, int Columns, ScriptNode Node)
123 {
124 IElement SuperSetExample = null;
125 IElement Element2;
126 ISet CommonSuperSet = null;
127 ISet Set;
128 LinkedList<IElement> Upgraded = null;
129 int ItemIndex = 0;
130
131 if (Elements.Count == Rows && Columns > 1)
132 {
133 List<IElement> Temp = new List<IElement>();
134
135 foreach (IElement E in Elements)
136 {
137 if (E is IVector V)
138 Temp.AddRange(V.VectorElements);
139 else
140 throw new ScriptRuntimeException("Invalid number of elements.", Node);
141 }
142
143 Elements = Temp;
144 }
145
146 foreach (IElement Element in Elements)
147 {
148 if (CommonSuperSet is null)
149 {
150 SuperSetExample = Element;
151
152 if (Element is null)
153 CommonSuperSet = new ObjectValues();
154 else
155 CommonSuperSet = Element.AssociatedSet;
156 }
157 else
158 {
159 if (Element is null)
160 Set = new ObjectValues();
161 else
163
164 if (Set.Equals(CommonSuperSet))
165 Upgraded?.AddLast(Element);
166 else
167 {
168 Element2 = Element;
169 if (!Expression.UpgradeField(ref Element2, ref Set, ref SuperSetExample, ref CommonSuperSet))
170 {
171 CommonSuperSet = null;
172 break;
173 }
174 else
175 {
176 if (Upgraded is null)
177 {
178 Upgraded = new LinkedList<IElement>();
179
180 IElement Element3;
181 int i = 0;
182
183 foreach (IElement E in Elements)
184 {
185 Element3 = E;
186 if (!Expression.UpgradeField(ref Element3, ref Set, ref SuperSetExample, ref CommonSuperSet))
187 {
188 CommonSuperSet = null;
189 break;
190 }
191
192 Upgraded.AddLast(Element3);
193 if (++i >= ItemIndex)
194 break;
195 }
196 }
197
198 Upgraded.AddLast(Element2);
199 }
200 }
201 }
202
203 ItemIndex++;
204 }
205
206 if (!(CommonSuperSet is null))
207 {
208 if (!(Upgraded is null))
209 Elements = Upgraded;
210
211 if (!(CommonSuperSet is null))
212 {
213 if (CommonSuperSet is DoubleNumbers)
214 return new DoubleMatrix(Rows, Columns, Elements);
215 else if (CommonSuperSet is ComplexNumbers)
216 return new ComplexMatrix(Rows, Columns, Elements);
217 else if (CommonSuperSet is BooleanValues)
218 return new BooleanMatrix(Rows, Columns, Elements);
219 }
220 }
221
222 return new ObjectMatrix(Rows, Columns, Elements);
223 }
224
231 public override PatternMatchResult PatternMatch(IElement CheckAgainst, Dictionary<string, IElement> AlreadyFound)
232 {
234 int c = Elements.Length;
235
236 if (!(CheckAgainst is IMatrix Matrix) || Matrix.Rows != c)
237 return PatternMatchResult.NoMatch;
238
239 PatternMatchResult Result;
240 int i;
241
242 if (Matrix is IVector RowVectors)
243 {
244 i = 0;
245
246 foreach (IElement E in RowVectors.VectorElements)
247 {
248 Result = Elements[i++].PatternMatch(E, AlreadyFound);
249 if (Result != PatternMatchResult.Match)
250 return Result;
251 }
252 }
253 else
254 {
255 for (i = 0; i < c; i++)
256 {
257 Result = Elements[i].PatternMatch(Matrix.GetRow(i), AlreadyFound);
258 if (Result != PatternMatchResult.Match)
259 return Result;
260 }
261 }
262
263 return PatternMatchResult.Match;
264 }
265
266 }
267}
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 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.
Pseudo-field of double numbers, as an approximation of the field of real numbers.
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 PatternMatchResult PatternMatch(IElement CheckAgainst, Dictionary< string, IElement > AlreadyFound)
Performs a pattern match operation.
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.
MatrixDefinition(ScriptNode[] Rows, int Start, int Length, Expression Expression)
Creates a matrix.
static IMatrix Encapsulate(ICollection< IElement > Elements, int Rows, int Columns, ScriptNode Node)
Encapsulates the elements of a matrix.
static IMatrix Encapsulate(ICollection< IElement > Rows, ScriptNode Node)
Encapsulates the elements of a matrix.
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