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.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 {
46 int i, c = Matrix.Rows;
47
48 if (Matrix is DoubleMatrix)
49 {
50 for (i = 0; i < c; i++)
51 Elements.Add(this.Evaluate((DoubleVector)Matrix.GetRow(i), Variables));
52 }
53 else if (Matrix is ComplexMatrix)
54 {
55 for (i = 0; i < c; i++)
56 Elements.Add(this.Evaluate((ComplexVector)Matrix.GetRow(i), Variables));
57 }
58 else if (Matrix is BooleanMatrix)
59 {
60 for (i = 0; i < c; i++)
61 Elements.Add(this.Evaluate((BooleanVector)Matrix.GetRow(i), Variables));
62 }
63 else
64 {
65 for (i = 0; i < c; i++)
66 Elements.Add(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 {
76
77 foreach (IElement E in Set.ChildElements)
78 Elements.Add(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 {
135 int i, c = Matrix.Rows;
136
137 if (Matrix is DoubleMatrix)
138 {
139 for (i = 0; i < c; i++)
140 Elements.Add(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.Add(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.Add(await this.EvaluateAsync((BooleanVector)Matrix.GetRow(i), Variables));
151 }
152 else
153 {
154 for (i = 0; i < c; i++)
155 Elements.Add(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 {
165
166 foreach (IElement E in Set.ChildElements)
167 Elements.Add(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 #region IIterativeEvaluation
296
300 public virtual bool CanEvaluateIteratively => true;
301
307 {
308 return new FunctionOneVectorVariableIterator(this);
309 }
310
311 #endregion
312 }
313}
A chunked list is a linked list of chunks of objects of type T .
Definition: ChunkedList.cs:54
void Add(T Item)
Adds an item to the collection.
Definition: ChunkedList.cs:272
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:84
Class managing a script expression.
Definition: Expression.cs:41
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 bool CanEvaluateIteratively
If the node can be evaluated iteratively.
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 IIterativeEvaluator CreateEvaluator()
Creates an iterative evaluator for the node.
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.
Default iterator for one-vector-variable functions.
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:21
Basic interface for matrices.
Definition: IMatrix.cs:7
Basic interface for vectors.
Definition: IVector.cs:9
Basic interface for all types of sets.
Definition: ISet.cs:10
A function that can be iteratively evaluated, meaning it can be iteratively computed one element at a...
An interative evaluator of a function supporting the IIterativeEvaluation interface.