Neuron®
The Neuron® is the basis for the creation of open and secure federated networks for smart societies.
Loading...
Searching...
No Matches
FunctionOneVectorVariable.cs
1using System.Collections.Generic;
2using System.Threading.Tasks;
8
9namespace Waher.Script.Model
10{
15 {
25 {
26 }
27
31 public override string[] DefaultArgumentNames => new string[] { "v" };
32
40 {
41 if (!(Argument is IVector Vector))
42 {
43 if (Argument is IMatrix Matrix)
44 {
45 LinkedList<IElement> Elements = new LinkedList<IElement>();
46 int i, c = Matrix.Rows;
47
48 if (Matrix is DoubleMatrix)
49 {
50 for (i = 0; i < c; i++)
51 Elements.AddLast(this.Evaluate((DoubleVector)Matrix.GetRow(i), Variables));
52 }
53 else if (Matrix is ComplexMatrix)
54 {
55 for (i = 0; i < c; i++)
56 Elements.AddLast(this.Evaluate((ComplexVector)Matrix.GetRow(i), Variables));
57 }
58 else if (Matrix is BooleanMatrix)
59 {
60 for (i = 0; i < c; i++)
61 Elements.AddLast(this.Evaluate((BooleanVector)Matrix.GetRow(i), Variables));
62 }
63 else
64 {
65 for (i = 0; i < c; i++)
66 Elements.AddLast(this.Evaluate(Matrix.GetRow(i), Variables));
67 }
68
69 return Argument.Encapsulate(Elements, this);
70 }
71 else
72 {
73 if (Argument is ISet Set)
74 {
75 LinkedList<IElement> Elements = new LinkedList<IElement>();
76
77 foreach (IElement E in Set.ChildElements)
78 Elements.AddLast(this.Evaluate(E, Variables));
79
80 return Argument.Encapsulate(Elements, this);
81 }
82 else
83 return this.EvaluateNonVector(Argument, Variables);
84 }
85 }
86
87 if (Vector is DoubleVector DoubleVector)
88 return this.EvaluateVector(DoubleVector, Variables);
89
90 if (Vector is ComplexVector ComplexVector)
91 return this.EvaluateVector(ComplexVector, Variables);
92
93 if (Vector is BooleanVector BooleanVector)
94 return this.EvaluateVector(BooleanVector, Variables);
95
96 return this.EvaluateVector(Vector, Variables);
97 }
98
106 {
107 IVector Vector = VectorDefinition.Encapsulate(new IElement[] { Argument }, false, this);
108
109 if (Vector is DoubleVector DoubleVector)
110 return this.EvaluateVector(DoubleVector, Variables);
111
112 if (Vector is ComplexVector ComplexVector)
113 return this.EvaluateVector(ComplexVector, Variables);
114
115 if (Vector is BooleanVector BooleanVector)
116 return this.EvaluateVector(BooleanVector, Variables);
117
118 return this.EvaluateVector(Vector, Variables);
119 }
120
121
128 public override async Task<IElement> EvaluateAsync(IElement Argument, Variables Variables)
129 {
130 if (!(Argument is IVector Vector))
131 {
132 if (Argument is IMatrix Matrix)
133 {
134 LinkedList<IElement> Elements = new LinkedList<IElement>();
135 int i, c = Matrix.Rows;
136
137 if (Matrix is DoubleMatrix)
138 {
139 for (i = 0; i < c; i++)
140 Elements.AddLast(await this.EvaluateAsync((DoubleVector)Matrix.GetRow(i), Variables));
141 }
142 else if (Matrix is ComplexMatrix)
143 {
144 for (i = 0; i < c; i++)
145 Elements.AddLast(await this.EvaluateAsync((ComplexVector)Matrix.GetRow(i), Variables));
146 }
147 else if (Matrix is BooleanMatrix)
148 {
149 for (i = 0; i < c; i++)
150 Elements.AddLast(await this.EvaluateAsync((BooleanVector)Matrix.GetRow(i), Variables));
151 }
152 else
153 {
154 for (i = 0; i < c; i++)
155 Elements.AddLast(await this.EvaluateAsync(Matrix.GetRow(i), Variables));
156 }
157
158 return Argument.Encapsulate(Elements, this);
159 }
160 else
161 {
162 if (Argument is ISet Set)
163 {
164 LinkedList<IElement> Elements = new LinkedList<IElement>();
165
166 foreach (IElement E in Set.ChildElements)
167 Elements.AddLast(await this.EvaluateAsync(E, Variables));
168
169 return Argument.Encapsulate(Elements, this);
170 }
171 else
172 return await this.EvaluateNonVectorAsync(Argument, Variables);
173 }
174 }
175
176 if (Vector is DoubleVector DoubleVector)
177 return await this.EvaluateVectorAsync(DoubleVector, Variables);
178
179 if (Vector is ComplexVector ComplexVector)
180 return await this.EvaluateVectorAsync(ComplexVector, Variables);
181
182 if (Vector is BooleanVector BooleanVector)
183 return await this.EvaluateVectorAsync(BooleanVector, Variables);
184
185 return await this.EvaluateVectorAsync(Vector, Variables);
186 }
187
194 protected virtual async Task<IElement> EvaluateNonVectorAsync(IElement Argument, Variables Variables)
195 {
196 IVector Vector = VectorDefinition.Encapsulate(new IElement[] { Argument }, false, this);
197
198 if (Vector is DoubleVector DoubleVector)
199 return await this.EvaluateVectorAsync(DoubleVector, Variables);
200
201 if (Vector is ComplexVector ComplexVector)
202 return await this.EvaluateVectorAsync(ComplexVector, Variables);
203
204 if (Vector is BooleanVector BooleanVector)
205 return await this.EvaluateVectorAsync(BooleanVector, Variables);
206
207 return await this.EvaluateVectorAsync(Vector, Variables);
208 }
209
217
225 {
227 }
228
236 {
238 }
239
247 {
249 }
250
258 {
259 return Task.FromResult(this.EvaluateVector(Argument, Variables));
260 }
261
269 {
270 return Task.FromResult(this.EvaluateVector(Argument, Variables));
271 }
272
280 {
281 return Task.FromResult(this.EvaluateVector(Argument, Variables));
282 }
283
291 {
292 return Task.FromResult(this.EvaluateVector(Argument, Variables));
293 }
294 }
295}
Base class for all types of sets.
Definition: Set.cs:14
override ICollection< IElement > ChildElements
An enumeration of child elements. If the element is a scalar, this property will return null.
Definition: Set.cs:73
Class managing a script expression.
Definition: Expression.cs:39
Base class for funcions of one variable.
ScriptNode Argument
Function argument.
Base class for funcions of one vector variable.
virtual Task< IElement > EvaluateVectorAsync(DoubleVector Argument, Variables Variables)
Evaluates the function on a vector argument.
virtual async Task< IElement > EvaluateNonVectorAsync(IElement Argument, Variables Variables)
Evaluates the function on a non-vector. By default, the non-vector argument is converted to a vector ...
override string[] DefaultArgumentNames
Default Argument names
override async Task< IElement > EvaluateAsync(IElement Argument, Variables Variables)
Evaluates the function.
virtual Task< IElement > EvaluateVectorAsync(BooleanVector Argument, Variables Variables)
Evaluates the function on a vector argument.
virtual Task< IElement > EvaluateVectorAsync(IVector Argument, Variables Variables)
Evaluates the function on a vector argument.
virtual Task< IElement > EvaluateVectorAsync(ComplexVector Argument, Variables Variables)
Evaluates the function on a vector argument.
abstract IElement EvaluateVector(IVector Argument, Variables Variables)
Evaluates the function on a vector argument.
virtual IElement EvaluateVector(ComplexVector Argument, Variables Variables)
Evaluates the function on a vector argument.
override IElement Evaluate(IElement Argument, Variables Variables)
Evaluates the function.
virtual IElement EvaluateNonVector(IElement Argument, Variables Variables)
Evaluates the function on a non-vector. By default, the non-vector argument is converted to a vector ...
virtual IElement EvaluateVector(BooleanVector Argument, Variables Variables)
Evaluates the function on a vector argument.
FunctionOneVectorVariable(ScriptNode Argument, int Start, int Length, Expression Expression)
Base class for funcions of one vector variable.
virtual IElement EvaluateVector(DoubleVector Argument, Variables Variables)
Evaluates the function on a vector argument.
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
static IElement Encapsulate(Array Elements, bool CanEncapsulateAsMatrix, ScriptNode Node)
Encapsulates the elements of a vector.
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