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;
2using System.Collections.Generic;
3using System.Reflection;
4using System.Text;
10
12{
16 public sealed class ObjectVector : VectorSpaceElement
17 {
18 private IElement[] values = null;
19 private ICollection<IElement> elements;
20 private readonly int dimension;
21
26 public ObjectVector(ICollection<IElement> Elements)
27 {
28 this.elements = Elements;
29 this.dimension = Elements.Count;
30 }
31
36 public ObjectVector(ICollection<object> Elements)
37 {
38 LinkedList<IElement> Elements2 = new LinkedList<IElement>();
39
40 foreach (object Obj in Elements)
41 Elements2.AddLast(Expression.Encapsulate(Obj));
42
43 this.elements = Elements2;
44 this.dimension = Elements2.Count;
45 }
46
51 public ObjectVector(Array Elements)
52 {
53 LinkedList<IElement> Elements2 = new LinkedList<IElement>();
54
55 foreach (object Obj in Elements)
56 Elements2.AddLast(Expression.Encapsulate(Obj));
57
58 this.elements = Elements2;
59 this.dimension = Elements2.Count;
60 }
61
67 : this((ICollection<IElement>)Elements)
68 {
69 }
70
75 public ObjectVector(params object[] Elements)
76 : this((ICollection<object>)Elements)
77 {
78 }
79
84 {
85 get
86 {
87 if (this.values is null)
88 {
89 IElement[] v = new IElement[this.dimension];
90 this.elements.CopyTo(v, 0);
91 this.values = v;
92 }
93
94 return this.values;
95 }
96 }
97
101 public ICollection<IElement> Elements
102 {
103 get
104 {
105 return this.elements;
106 }
107 }
108
112 public override int Dimension => this.dimension;
113
115 public override string ToString()
116 {
117 StringBuilder sb = null;
118
119 foreach (IElement Element in this.elements)
120 {
121 if (sb is null)
122 sb = new StringBuilder("[");
123 else
124 sb.Append(", ");
125
126 if (!(Element is null))
127 sb.Append(Element.ToString());
128 }
129
130 if (sb is null)
131 return "[]";
132 else
133 {
134 sb.Append(']');
135 return sb.ToString();
136 }
137 }
138
143 {
144 get
145 {
146 if (this.associatedVectorSpace is null)
147 this.associatedVectorSpace = new ObjectVectors(this);
148
149 return this.associatedVectorSpace;
150 }
151 }
152
153 private ObjectVectors associatedVectorSpace = null;
154
158 public override object AssociatedObjectValue
159 {
160 get
161 {
162 if (!(this.associatedObjectValue is null))
163 return this.associatedObjectValue;
164
165 object[] V = new object[this.dimension];
166 int i = 0;
167
168 foreach (IElement E in this.elements)
169 {
170 if (E is null)
171 V[i++] = null;
172 else
173 V[i++] = E.AssociatedObjectValue;
174 }
175
176 this.associatedObjectValue = V;
177 return this.associatedObjectValue;
178 }
179 }
180
181 private object[] associatedObjectValue = null;
182
189 {
190 LinkedList<IElement> Elements = new LinkedList<IElement>();
191
192 foreach (IElement Element in this.elements)
193 Elements.AddLast(Operators.Arithmetics.Multiply.EvaluateMultiplication(Scalar, Element, null));
194
195 return new ObjectVector(Elements);
196 }
197
204 {
205 LinkedList<IElement> Elements = new LinkedList<IElement>();
206
207 if (Element.IsScalar)
208 return null;
209
210 ICollection<IElement> ChildElements = Element.ChildElements;
211 IEnumerator<IElement> e1 = this.elements.GetEnumerator();
212 IEnumerator<IElement> e2 = ChildElements.GetEnumerator();
213
214 while (e1.MoveNext() && e2.MoveNext())
215 Elements.AddLast(Operators.Arithmetics.Add.EvaluateAddition(e1.Current, e2.Current, null));
216
217 return new ObjectVector(Elements);
218 }
219
224 public override IGroupElement Negate()
225 {
226 LinkedList<IElement> Elements = new LinkedList<IElement>();
227
228 foreach (IElement Element in this.elements)
229 Elements.AddLast(Operators.Arithmetics.Negate.EvaluateNegation(Element));
230
231 return new ObjectVector(Elements);
232 }
233
239 public override bool Equals(object obj)
240 {
242 if (ObjectVector is null)
243 return false;
244
245 if (ObjectVector.dimension != this.dimension)
246 return false;
247
248 IEnumerator<IElement> e1 = this.elements.GetEnumerator();
249 IEnumerator<IElement> e2 = ObjectVector.elements.GetEnumerator();
250
251 while (e1.MoveNext() && e2.MoveNext())
252 {
253 if (!e1.Current.Equals(e2.Current))
254 return false;
255 }
256
257 return true;
258 }
259
264 public override int GetHashCode()
265 {
266 int Result = 0;
267
268 foreach (IElement Element in this.elements)
269 Result ^= Element.GetHashCode();
270
271 return Result;
272 }
273
277 public override bool IsScalar
278 {
279 get { return false; }
280 }
281
285 public override ICollection<IElement> ChildElements => this.Elements;
286
293 public override IElement Encapsulate(ICollection<IElement> Elements, ScriptNode Node)
294 {
295 return VectorDefinition.Encapsulate(Elements, true, Node);
296 }
297
302 {
303 get
304 {
305 return this.AssociatedVectorSpace.Zero;
306 }
307 }
308
314 public override IElement GetElement(int Index)
315 {
316 if (Index < 0 || Index >= this.dimension)
317 throw new ScriptException("Index out of bounds.");
318
319 IElement[] V = this.Values;
320 return V[Index];
321 }
322
328 public override void SetElement(int Index, IElement Value)
329 {
330 if (Index < 0 || Index >= this.dimension)
331 throw new ScriptException("Index out of bounds.");
332
333 IElement[] Values = this.Values;
334 this.elements = null;
335
336 Values[Index] = Value;
337 }
338
345 public override bool TryConvertTo(Type DesiredType, out object Value)
346 {
347 if (DesiredType == typeof(object[]))
348 {
349 Value = this.Values;
350 return true;
351 }
352 else if (DesiredType.GetTypeInfo().IsAssignableFrom(typeof(ObjectVector).GetTypeInfo()))
353 {
354 Value = this;
355 return true;
356 }
357 else if (DesiredType.IsArray)
358 {
359 Type ElementType = DesiredType.GetElementType();
360 Array Result = Array.CreateInstance(ElementType, this.dimension);
361 IElement[] Values = this.Values;
362 int i;
363
364 for (i = 0; i < this.dimension; i++)
365 {
366 if (!Values[i].TryConvertTo(ElementType, out Value))
367 {
368 Value = null;
369 return false;
370 }
371
372 Result.SetValue(Value, i);
373 }
374
375 Value = Result;
376 return true;
377 }
378 else
379 return Expression.TryConvert(this.Values, DesiredType, out Value);
380 }
381
382 }
383}
Base class for all types of elements.
Definition: Element.cs:13
abstract override int GetHashCode()
Calculates a hash code of the element.
virtual bool IsScalar
If the element represents a scalar value.
Definition: Element.cs:54
virtual ICollection< IElement > ChildElements
An enumeration of child elements. If the element is a scalar, this property will return null.
Definition: Element.cs:62
Base class for all types of vector space elements (vectors).
Base class for script exceptions.
Class managing a script expression.
Definition: Expression.cs:39
static IElement Encapsulate(object Value)
Encapsulates an object.
Definition: Expression.cs:4955
static bool TryConvert(object Value, Type DesiredType, out object Result)
Tries to convert an object Value to an object of type DesiredType .
Definition: Expression.cs:5268
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:75
ObjectVector(ICollection< IElement > Elements)
Object-valued vector.
Definition: ObjectVector.cs:26
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:51
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:36
ICollection< IElement > Elements
Vector elements.
IElement[] Values
Vector element values.
Definition: ObjectVector.cs:84
ObjectVector(params IElement[] Elements)
Object-valued vector.
Definition: ObjectVector.cs:66
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:20
object AssociatedObjectValue
Associated object value.
Definition: IElement.cs:33
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