Neuron®
The Neuron® is the basis for the creation of open and secure federated networks for smart societies.
Loading...
Searching...
No Matches
RowVector.cs
1using System;
2using System.Collections.Generic;
3using System.Threading.Tasks;
8
10{
15 {
25 public RowVector(ScriptNode Left, ScriptNode Y, bool NullCheck, int Start, int Length, Expression Expression)
26 : base(Left, Y, NullCheck, Start, Length, Expression)
27 {
28 }
29
36 {
37 IElement Left = this.left.Evaluate(Variables);
38 if (this.nullCheck && Left.AssociatedObjectValue is null)
39 return Left;
40
41 IElement Right = this.right.Evaluate(Variables);
42
43 return EvaluateIndex(Left, Right, this.nullCheck, this);
44 }
45
51 public override async Task<IElement> EvaluateAsync(Variables Variables)
52 {
53 if (!this.isAsync)
54 return this.Evaluate(Variables);
55
56 IElement Left = await this.left.EvaluateAsync(Variables);
57 if (this.nullCheck && Left.AssociatedObjectValue is null)
58 return Left;
59
60 IElement Right = await this.right.EvaluateAsync(Variables);
61
62 return EvaluateIndex(Left, Right, this.nullCheck, this);
63 }
72 public static IElement EvaluateIndex(IElement Matrix, IElement Index, bool NullCheck, ScriptNode Node)
73 {
74 if (Matrix is IMatrix M)
75 return EvaluateIndex(M, Index, Node);
76 else if (Matrix.IsScalar)
77 {
78 if (NullCheck && Matrix.AssociatedObjectValue is null)
79 return Matrix;
80
81 throw new ScriptRuntimeException("The row index operator operates on matrices.", Node);
82 }
83 else
84 {
85 LinkedList<IElement> Elements = new LinkedList<IElement>();
86
87 foreach (IElement E in Matrix.ChildElements)
88 Elements.AddLast(EvaluateIndex(E, Index, NullCheck, Node));
89
90 return Matrix.Encapsulate(Elements, Node);
91 }
92 }
93
101 public static IElement EvaluateIndex(IMatrix Matrix, IElement Index, ScriptNode Node)
102 {
103 if (Index.AssociatedObjectValue is double d)
104 {
105 if (d < 0 || d > int.MaxValue || d != Math.Truncate(d))
106 throw new ScriptRuntimeException("Row index must be a non-negative integer.", Node);
107
108 return Matrix.GetRow((int)d);
109 }
110
111 if (Index.IsScalar)
112 throw new ScriptRuntimeException("Row index must be a non-negative integer.", Node);
113 else
114 {
115 LinkedList<IElement> Elements = new LinkedList<IElement>();
116
117 foreach (IElement E in Index.ChildElements)
118 Elements.AddLast(EvaluateIndex(Matrix, E, Node));
119
120 return Index.Encapsulate(Elements, Node);
121 }
122 }
123
124 }
125}
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
static IElement EvaluateIndex(IMatrix Matrix, IElement Index, ScriptNode Node)
Evaluates the row index operator.
Definition: RowVector.cs:101
static IElement EvaluateIndex(IElement Matrix, IElement Index, bool NullCheck, ScriptNode Node)
Evaluates the row index operator.
Definition: RowVector.cs:72
RowVector(ScriptNode Left, ScriptNode Y, bool NullCheck, int Start, int Length, Expression Expression)
Row Vector operator.
Definition: RowVector.cs:25
override async Task< IElement > EvaluateAsync(Variables Variables)
Evaluates the node, using the variables provided in the Variables collection.
Definition: RowVector.cs:51
override IElement Evaluate(Variables Variables)
Evaluates the node, using the variables provided in the Variables collection.
Definition: RowVector.cs:35
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
IVector GetRow(int Row)
Gets a row vector from the matrix.