Neuron®
The Neuron® is the basis for the creation of open and secure federated networks for smart societies.
Loading...
Searching...
No Matches
MatrixIndex.cs
1using System;
2using System.Collections.Generic;
3using System.Threading.Tasks;
8
10{
15 {
26 public MatrixIndex(ScriptNode Left, ScriptNode X, ScriptNode Y, bool NullCheck, int Start, int Length, Expression Expression)
27 : base(Left, X, Y, NullCheck, Start, Length, Expression)
28 {
29 }
30
37 {
38 IElement Left = this.left.Evaluate(Variables);
39 if (this.nullCheck && Left.AssociatedObjectValue is null)
40 return Left;
41
42 IElement Middle = this.middle.Evaluate(Variables);
43 IElement Right = this.right.Evaluate(Variables);
44
45 return EvaluateIndex(Left, Middle, Right, this.nullCheck, this);
46 }
47
53 public override async Task<IElement> EvaluateAsync(Variables Variables)
54 {
55 if (!this.isAsync)
56 return this.Evaluate(Variables);
57
58 IElement Left = await this.left.EvaluateAsync(Variables);
59 if (this.nullCheck && Left.AssociatedObjectValue is null)
60 return Left;
61
62 IElement Middle = await this.middle.EvaluateAsync(Variables);
63 IElement Right = await this.right.EvaluateAsync(Variables);
64
65 return EvaluateIndex(Left, Middle, Right, this.nullCheck, this);
66 }
76 public static IElement EvaluateIndex(IElement Matrix, IElement IndexX, IElement IndexY, bool NullCheck, ScriptNode Node)
77 {
78 if (Matrix is IMatrix M)
79 return EvaluateIndex(M, IndexX, IndexY, Node);
80 else if (Matrix.IsScalar)
81 {
82 if (NullCheck && Matrix.AssociatedObjectValue is null)
83 return Matrix;
84
85 throw new ScriptRuntimeException("The index operator operates on matrices.", Node);
86 }
87 else
88 {
89 LinkedList<IElement> Elements = new LinkedList<IElement>();
90
91 foreach (IElement E in Matrix.ChildElements)
92 Elements.AddLast(EvaluateIndex(E, IndexX, IndexY, NullCheck, Node));
93
94 return Matrix.Encapsulate(Elements, Node);
95 }
96 }
97
106 public static IElement EvaluateIndex(IMatrix Matrix, IElement IndexX, IElement IndexY, ScriptNode Node)
107 {
108 if (IndexX.AssociatedObjectValue is double x &&
109 IndexY.AssociatedObjectValue is double y)
110 {
111 if (x < 0 || x > int.MaxValue || x != Math.Truncate(x) || y < 0 || y > int.MaxValue || y != Math.Truncate(y))
112 throw new ScriptRuntimeException("Indices must be non-negative integers.", Node);
113
114 return Matrix.GetElement((int)x, (int)y);
115 }
116
117 if (IndexX.IsScalar)
118 {
119 if (IndexY.IsScalar)
120 throw new ScriptRuntimeException("Index must be a non-negative integer.", Node);
121 else
122 {
123 LinkedList<IElement> Elements = new LinkedList<IElement>();
124
125 foreach (IElement E in IndexY.ChildElements)
126 Elements.AddLast(EvaluateIndex(Matrix, IndexX, E, Node));
127
128 return IndexY.Encapsulate(Elements, Node);
129 }
130 }
131 else
132 {
133 if (IndexY.IsScalar)
134 {
135 LinkedList<IElement> Elements = new LinkedList<IElement>();
136
137 foreach (IElement E in IndexX.ChildElements)
138 Elements.AddLast(EvaluateIndex(Matrix, E, IndexY, Node));
139
140 return IndexX.Encapsulate(Elements, Node);
141 }
142 else
143 {
144 ICollection<IElement> IndexXChildren = IndexX.ChildElements;
145 ICollection<IElement> IndexYChildren = IndexY.ChildElements;
146
147 if (IndexXChildren.Count == IndexYChildren.Count)
148 {
149 LinkedList<IElement> Elements = new LinkedList<IElement>();
150 IEnumerator<IElement> eX = IndexXChildren.GetEnumerator();
151 IEnumerator<IElement> eY = IndexYChildren.GetEnumerator();
152
153 try
154 {
155 while (eX.MoveNext() && eY.MoveNext())
156 Elements.AddLast(EvaluateIndex(Matrix, eX.Current, eY.Current, Node));
157 }
158 finally
159 {
160 eX.Dispose();
161 eY.Dispose();
162 }
163
164 return IndexX.Encapsulate(Elements, Node);
165 }
166 else
167 {
168 LinkedList<IElement> XResult = new LinkedList<IElement>();
169
170 foreach (IElement XChild in IndexXChildren)
171 {
172 LinkedList<IElement> YResult = new LinkedList<IElement>();
173
174 foreach (IElement YChild in IndexYChildren)
175 YResult.AddLast(EvaluateIndex(Matrix, XChild, YChild, Node));
176
177 XResult.AddLast(IndexY.Encapsulate(YResult, Node));
178 }
179
180 return IndexX.Encapsulate(XResult, Node);
181 }
182 }
183 }
184 }
185
186 }
187}
Class managing a script expression.
Definition: Expression.cs:39
ScriptNode right
Right operand.
ScriptNode left
Left operand.
bool isAsync
If subtree is asynchroneous.
Base class for all unary operators performing operand null checks.
readonly bool nullCheck
If null should be returned if operand is null.
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
ScriptNode middle
Middle operand.
static IElement EvaluateIndex(IMatrix Matrix, IElement IndexX, IElement IndexY, ScriptNode Node)
Evaluates the vector index operator.
Definition: MatrixIndex.cs:106
static IElement EvaluateIndex(IElement Matrix, IElement IndexX, IElement IndexY, bool NullCheck, ScriptNode Node)
Evaluates the matrix index operator.
Definition: MatrixIndex.cs:76
override IElement Evaluate(Variables Variables)
Evaluates the node, using the variables provided in the Variables collection.
Definition: MatrixIndex.cs:36
MatrixIndex(ScriptNode Left, ScriptNode X, ScriptNode Y, bool NullCheck, int Start, int Length, Expression Expression)
Matrix Index operator.
Definition: MatrixIndex.cs:26
override async Task< IElement > EvaluateAsync(Variables Variables)
Evaluates the node, using the variables provided in the Variables collection.
Definition: MatrixIndex.cs:53
Collection of variables.
Definition: Variables.cs:25
Basic interface for all types of elements.
Definition: IElement.cs:20
object AssociatedObjectValue
Associated object value.
Definition: IElement.cs:33
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 matrices.
Definition: IMatrix.cs:9
IElement GetElement(int Column, int Row)
Gets an element of the matrix.