Neuron®
The Neuron® is the basis for the creation of open and secure federated networks for smart societies.
Loading...
Searching...
No Matches
ObjectVector.cs
1using System;
3using System.Reflection;
4using System.Text;
11
13{
17 public sealed class ObjectVector : VectorSpaceElement
18 {
19 private IElement[] values = null;
20 private ICollection<IElement> elements;
21 private readonly int dimension;
22
27 public ObjectVector(ICollection<IElement> Elements)
28 {
29 this.elements = Elements;
30 this.dimension = Elements.Count;
31 }
32
37 public ObjectVector(ICollection<object> Elements)
38 {
40
41 foreach (object Obj in Elements)
42 Elements2.Add(Expression.Encapsulate(Obj));
43
44 this.elements = Elements2;
45 this.dimension = Elements2.Count;
46 }
47
52 public ObjectVector(Array Elements)
53 {
55
56 foreach (object Obj in Elements)
57 Elements2.Add(Expression.Encapsulate(Obj));
58
59 this.elements = Elements2;
60 this.dimension = Elements2.Count;
61 }
62
68 : this((ICollection<IElement>)Elements)
69 {
70 }
71
76 public ObjectVector(params object[] Elements)
77 : this((ICollection<object>)Elements)
78 {
79 }
80
85 {
86 get
87 {
88 if (this.values is null)
89 {
90 IElement[] v = new IElement[this.dimension];
91 this.elements.CopyTo(v, 0);
92 this.values = v;
93 }
94
95 return this.values;
96 }
97 }
98
102 public ICollection<IElement> Elements => this.elements;
103
107 public override int Dimension => this.dimension;
108
110 public override string ToString()
111 {
112 StringBuilder sb = null;
113
114 foreach (IElement Element in this.elements)
115 {
116 if (sb is null)
117 sb = new StringBuilder("[");
118 else
119 sb.Append(", ");
120
121 if (!(Element is null))
122 sb.Append(Element.ToString());
123 }
124
125 if (sb is null)
126 return "[]";
127 else
128 {
129 sb.Append(']');
130 return sb.ToString();
131 }
132 }
133
138 {
139 get
140 {
141 if (this.associatedVectorSpace is null)
142 this.associatedVectorSpace = new ObjectVectors(this);
143
144 return this.associatedVectorSpace;
145 }
146 }
147
148 private ObjectVectors associatedVectorSpace = null;
149
153 public override object AssociatedObjectValue
154 {
155 get
156 {
157 if (!(this.associatedObjectValue is null))
158 return this.associatedObjectValue;
159
160 object[] V = new object[this.dimension];
161 int i = 0;
162
163 foreach (IElement E in this.elements)
164 {
165 if (E is null)
166 V[i++] = null;
167 else
168 V[i++] = E.AssociatedObjectValue;
169 }
170
171 this.associatedObjectValue = V;
172 return this.associatedObjectValue;
173 }
174 }
175
176 private object[] associatedObjectValue = null;
177
184 {
186
187 foreach (IElement Element in this.elements)
188 Elements.Add(Operators.Arithmetics.Multiply.EvaluateMultiplication(Scalar, Element, null));
189
190 return new ObjectVector(Elements);
191 }
192
199 {
201
202 if (Element.IsScalar)
203 return null;
204
205 ICollection<IElement> ChildElements = Element.ChildElements;
206 IEnumerator<IElement> e1 = this.elements.GetEnumerator();
207 IEnumerator<IElement> e2 = ChildElements.GetEnumerator();
208
209 try
210 {
211 while (e1.MoveNext() && e2.MoveNext())
212 Elements.Add(Operators.Arithmetics.Add.EvaluateAddition(e1.Current, e2.Current, null));
213 }
214 finally
215 {
216 e1.Dispose();
217 e2.Dispose();
218 }
219
220 return new ObjectVector(Elements);
221 }
222
227 public override IGroupElement Negate()
228 {
230
231 foreach (IElement Element in this.elements)
232 Elements.Add(Operators.Arithmetics.Negate.EvaluateNegation(Element));
233
234 return new ObjectVector(Elements);
235 }
236
242 public override bool Equals(object obj)
243 {
245 if (ObjectVector is null)
246 return false;
247
248 if (ObjectVector.dimension != this.dimension)
249 return false;
250
251 IEnumerator<IElement> e1 = this.elements.GetEnumerator();
252 IEnumerator<IElement> e2 = ObjectVector.elements.GetEnumerator();
253
254 try
255 {
256 while (e1.MoveNext() && e2.MoveNext())
257 {
258 if (!e1.Current.Equals(e2.Current))
259 return false;
260 }
261 }
262 finally
263 {
264 e1.Dispose();
265 e2.Dispose();
266 }
267
268 return true;
269 }
270
275 public override int GetHashCode()
276 {
277 int Result = 0;
278
279 foreach (IElement Element in this.elements)
280 Result ^= Element.GetHashCode();
281
282 return Result;
283 }
284
288 public override bool IsScalar
289 {
290 get { return false; }
291 }
292
296 public override ICollection<IElement> ChildElements => this.Elements;
297
305 {
306 return VectorDefinition.Encapsulate(Elements, true, Node);
307 }
308
315 public override IElement Encapsulate(ICollection<IElement> Elements, ScriptNode Node)
316 {
317 return VectorDefinition.Encapsulate(Elements, true, Node);
318 }
319
324 {
325 get
326 {
327 return this.AssociatedVectorSpace.Zero;
328 }
329 }
330
336 public override IElement GetElement(int Index)
337 {
338 if (Index < 0 || Index >= this.dimension)
339 throw new ScriptException("Index out of bounds.");
340
341 IElement[] V = this.Values;
342 return V[Index];
343 }
344
350 public override void SetElement(int Index, IElement Value)
351 {
352 if (Index < 0 || Index >= this.dimension)
353 throw new ScriptException("Index out of bounds.");
354
355 IElement[] Values = this.Values;
356 this.elements = null;
357
358 Values[Index] = Value;
359 }
360
367 public override bool TryConvertTo(Type DesiredType, out object Value)
368 {
369 if (DesiredType == typeof(object[]))
370 {
371 Value = this.Values;
372 return true;
373 }
374 else if (DesiredType.IsAssignableFrom(typeof(ObjectVector).GetTypeInfo()))
375 {
376 Value = this;
377 return true;
378 }
379 else if (DesiredType.IsArray)
380 {
381 Type ElementType = DesiredType.GetElementType();
382 Array Result = Array.CreateInstance(ElementType, this.dimension);
383 IElement[] Values = this.Values;
384 int i;
385
386 for (i = 0; i < this.dimension; i++)
387 {
388 if (!Values[i].TryConvertTo(ElementType, out Value))
389 {
390 Value = null;
391 return false;
392 }
393
394 Result.SetValue(Value, i);
395 }
396
397 Value = Result;
398 return true;
399 }
400 else
401 return Expression.TryConvert(this.Values, DesiredType, true, out Value);
402 }
403
404 }
405}
A chunked list is a linked list of chunks of objects of type T .
Definition: ChunkedList.cs:54
Base class for all types of elements.
Definition: Element.cs:14
abstract override int GetHashCode()
Calculates a hash code of the element.
virtual bool IsScalar
If the element represents a scalar value.
Definition: Element.cs:48
virtual ICollection< IElement > ChildElements
An enumeration of child elements. If the element is a scalar, this property will return null.
Definition: Element.cs:53
Base class for all types of vector space elements (vectors).
Base class for script exceptions.
Class managing a script expression.
Definition: Expression.cs:41
static bool TryConvert(object Value, Type DesiredType, bool AcceptInformationLoss, out object Result)
Tries to convert an object Value to an object of type DesiredType .
Definition: Expression.cs:5530
static IElement Encapsulate(object Value)
Encapsulates an object.
Definition: Expression.cs:5241
Base class for all nodes in a parsed script tree.
Definition: ScriptNode.cs:69
override IAbelianGroupElement Zero
Returns the zero element of the group.
override IElement Encapsulate(ICollection< IElement > Elements, ScriptNode Node)
Encapsulates a set of elements into a similar structure as that provided by the current element.
override void SetElement(int Index, IElement Value)
Sets an element in the vector.
override int Dimension
Dimension of vector.
override IElement GetElement(int Index)
Gets an element of the vector.
override IVectorSpaceElement MultiplyScalar(IFieldElement Scalar)
Tries to multiply a scalar to the current element.
override bool IsScalar
If the element represents a scalar value.
override bool TryConvertTo(Type DesiredType, out object Value)
Converts the value to a .NET type.
override IAbelianGroupElement Add(IAbelianGroupElement Element)
Tries to add an element to the current element.
ObjectVector(params object[] Elements)
Object-valued vector.
Definition: ObjectVector.cs:76
ObjectVector(ICollection< IElement > Elements)
Object-valued vector.
Definition: ObjectVector.cs:27
override ICollection< IElement > ChildElements
An enumeration of child elements. If the element is a scalar, this property will return null.
ObjectVector(Array Elements)
Object-valued vector.
Definition: ObjectVector.cs:52
override int GetHashCode()
Calculates a hash code of the element.
override IVectorSpace AssociatedVectorSpace
Associated Right-VectorSpace.
ObjectVector(ICollection< object > Elements)
Object-valued vector.
Definition: ObjectVector.cs:37
ICollection< IElement > Elements
Vector elements.
IElement[] Values
Vector element values.
Definition: ObjectVector.cs:85
ObjectVector(params IElement[] Elements)
Object-valued vector.
Definition: ObjectVector.cs:67
override IElement Encapsulate(ChunkedList< IElement > Elements, ScriptNode Node)
Encapsulates a set of elements into a similar structure as that provided by the current element.
override bool Equals(object obj)
Compares the element to another.
override IGroupElement Negate()
Negates the element.
override object AssociatedObjectValue
Associated object value.
Pseudo-vector space of Object-valued vectors.
static IElement Encapsulate(Array Elements, bool CanEncapsulateAsMatrix, ScriptNode Node)
Encapsulates the elements of a vector.
Basic interface for all types of abelian group elements.
Basic interface for all types of elements.
Definition: IElement.cs:21
object AssociatedObjectValue
Associated object value.
Definition: IElement.cs:34
Basic interface for all types of field elements.
Basic interface for all types of group elements.
Definition: IGroupElement.cs:9
Basic interface for all types of module elements.
IAbelianGroupElement Zero
Returns the zero element of the group.
Basic interface for all types of modules.
Definition: IVectorSpace.cs:10