Neuron®
The Neuron® is the basis for the creation of open and secure federated networks for smart societies.
Loading...
Searching...
No Matches
FindElements.cs
1using System.Collections.Generic;
9
11{
16 {
26 : base(new ScriptNode[] { SearchFor, In }, new ArgumentType[] { ArgumentType.Scalar, ArgumentType.Normal },
28 {
29 }
30
34 public override string FunctionName => nameof(FindElements);
35
39 public override string[] DefaultArgumentNames => new string[] { "SearchFor", "In" };
40
48 {
49 IElement SearchFor = Arguments[0];
50 IElement In = Arguments[1];
51
52 if (In is IMatrix M)
53 {
54 List<IElement> Coordinates = new List<IElement>();
55 int Rows = M.Rows;
56 int Columns = M.Columns;
57 int x, y;
58
59 for (y = 0; y < Rows; y++)
60 {
61 for (x = 0; x < Columns; x++)
62 {
63 if (SearchFor.Equals(M.GetElement(x, y)))
64 {
65 Coordinates.Add(new DoubleNumber(x));
66 Coordinates.Add(new DoubleNumber(y));
67 }
68 }
69 }
70
71 return new DoubleMatrix(Coordinates.Count / 2, 2, Coordinates);
72 }
73 else if (In is IVector V)
74 {
75 List<double> Indices = new List<double>();
76 int i = 0;
77
78 foreach (IElement Element in V.VectorElements)
79 {
80 if (SearchFor.Equals(Element))
81 Indices.Add(i);
82
83 i++;
84 }
85
86 return new DoubleVector(Indices.ToArray());
87 }
88 else if (In is ISet S)
89 {
90 List<double> Indices = new List<double>();
91 int i = 0;
92
93 foreach (IElement Element in S.ChildElements)
94 {
95 if (SearchFor.Equals(Element))
96 Indices.Add(i);
97
98 i++;
99 }
100
101 return new DoubleVector(Indices.ToArray());
102 }
103 else
104 throw new ScriptRuntimeException("Expected second argument to be matrix, vector or set.", this);
105 }
106 }
107}
Base class for all types of elements.
Definition: Element.cs:13
virtual ICollection< IElement > ChildElements
An enumeration of child elements. If the element is a scalar, this property will return null.
Definition: Element.cs:62
Class managing a script expression.
Definition: Expression.cs:39
Finds elements in matrices or vectors.
Definition: FindElements.cs:16
override string[] DefaultArgumentNames
Default Argument names
Definition: FindElements.cs:39
FindElements(ScriptNode SearchFor, ScriptNode In, int Start, int Length, Expression Expression)
Finds elements in matrices or vectors.
Definition: FindElements.cs:25
override string FunctionName
Name of the function
Definition: FindElements.cs:34
override IElement Evaluate(IElement[] Arguments, Variables Variables)
Evaluates the function.
Definition: FindElements.cs:47
Base class for multivariate funcions.
ScriptNode[] Arguments
Function arguments.
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
Expression Expression
Expression of which the node is a part.
Definition: ScriptNode.cs:177
int Start
Start position in script expression.
Definition: ScriptNode.cs:92
Collection of variables.
Definition: Variables.cs:25
Basic interface for all types of elements.
Definition: IElement.cs:20
Basic interface for matrices.
Definition: IMatrix.cs:9
Basic interface for vectors.
Definition: IVector.cs:9
Basic interface for all types of sets.
Definition: ISet.cs:10
ArgumentType
Type of parameter used in a function definition or a lambda definition.
Definition: IFunction.cs:9