Neuron®
The Neuron® is the basis for the creation of open and secure federated networks for smart societies.
Loading...
Searching...
No Matches
DoubleVector.cs
1using System;
3using System.Reflection;
10
12{
16 public sealed class DoubleVector : VectorSpaceElement
17 {
18 private double[] values;
19 private ICollection<IElement> elements;
20 private readonly int dimension;
21
26 public DoubleVector(params double[] Values)
27 {
28 this.values = Values;
29 this.elements = null;
30 this.dimension = Values.Length;
31 }
32
37 public DoubleVector(ICollection<IElement> Elements)
38 {
39 this.values = null;
40 this.elements = Elements;
41 this.dimension = Elements.Count;
42 }
43
47 public double[] Values
48 {
49 get
50 {
51 if (this.values is null)
52 {
53 double[] v = new double[this.dimension];
54 int i = 0;
55
56 if (this.elements is ChunkedList<IElement> Values)
57 {
58 ChunkNode<IElement> Loop = Values.FirstChunk;
59 int j, c;
60
61 while (!(Loop is null))
62 {
63 for (j = Loop.Start, c = Loop.Pos; j < c; j++)
64 {
65 if (!(Loop[j].AssociatedObjectValue is double d))
66 d = 0;
67
68 v[i++] = d;
69 }
70
71 Loop = Loop.Next;
72 }
73 }
74 else
75 {
76 foreach (IElement Element in this.elements)
77 {
78 if (!(Element.AssociatedObjectValue is double d))
79 d = 0;
80
81 v[i++] = d;
82 }
83 }
84
85 this.values = v;
86 }
87
88 return this.values;
89 }
90 }
91
95 public ICollection<IElement> Elements
96 {
97 get
98 {
99 if (this.elements is null)
100 {
101 int i;
102 IElement[] v = new IElement[this.dimension];
103
104 for (i = 0; i < this.dimension; i++)
105 v[i] = new DoubleNumber(this.values[i]);
106
107 this.elements = v;
108 }
109
110 return this.elements;
111 }
112 }
113
117 public override int Dimension => this.dimension;
118
120 public override string ToString()
121 {
122 return Expression.ToString(this.Values);
123 }
124
129 {
130 get
131 {
132 if (this.associatedVectorSpace is null)
133 this.associatedVectorSpace = new DoubleVectors(this.dimension);
134
135 return this.associatedVectorSpace;
136 }
137 }
138
139 private DoubleVectors associatedVectorSpace = null;
140
144 public override object AssociatedObjectValue => this.Values;
145
152 {
153 if (!(Scalar.AssociatedObjectValue is double d))
154 return null;
155
156 int i;
157 double[] Values = this.Values;
158 double[] v = new double[this.dimension];
159
160 for (i = 0; i < this.dimension; i++)
161 v[i] = d * Values[i];
162
163 return new DoubleVector(v);
164 }
165
172 {
174 return null;
175
176 int i;
177 if (DoubleVector.dimension != this.dimension)
178 return null;
179
180 double[] Values = this.Values;
181 double[] Values2 = DoubleVector.Values;
182 double[] v = new double[this.dimension];
183 for (i = 0; i < this.dimension; i++)
184 v[i] = Values[i] + Values2[i];
185
186 return new DoubleVector(v);
187 }
188
193 public override IGroupElement Negate()
194 {
195 int i;
196 double[] v = new double[this.dimension];
197 double[] Values = this.Values;
198 for (i = 0; i < this.dimension; i++)
199 v[i] = -Values[i];
200
201 return new DoubleVector(v);
202 }
203
209 public override bool Equals(object obj)
210 {
211 if (!(obj is DoubleVector DoubleVector))
212 return false;
213
214 int i;
215 if (DoubleVector.dimension != this.dimension)
216 return false;
217
218 double[] Values = this.Values;
219 double[] Values2 = DoubleVector.Values;
220 for (i = 0; i < this.dimension; i++)
221 {
222 if (Values[i] != Values2[i])
223 return false;
224 }
225
226 return true;
227 }
228
233 public override int GetHashCode()
234 {
235 double[] Values = this.Values;
236 int Result = 0;
237 int i;
238
239 for (i = 0; i < this.dimension; i++)
240 Result ^= Values[i].GetHashCode();
241
242 return Result;
243 }
244
248 public override bool IsScalar
249 {
250 get { return false; }
251 }
252
256 public override ICollection<IElement> ChildElements => this.Elements;
257
265 {
266 return VectorDefinition.Encapsulate(Elements, true, Node);
267 }
268
275 public override IElement Encapsulate(ICollection<IElement> Elements, ScriptNode Node)
276 {
277 return VectorDefinition.Encapsulate(Elements, true, Node);
278 }
279
284 {
285 get
286 {
287 if (this.zero is null)
288 this.zero = new DoubleVector(new double[this.dimension]);
289
290 return this.zero;
291 }
292 }
293
294 private DoubleVector zero = null;
295
301 public override IElement GetElement(int Index)
302 {
303 if (Index < 0 || Index >= this.dimension)
304 throw new ScriptException("Index out of bounds.");
305
306 double[] V = this.Values;
307
308 return new DoubleNumber(V[Index]);
309 }
310
316 public override void SetElement(int Index, IElement Value)
317 {
318 if (Index < 0 || Index >= this.dimension)
319 throw new ScriptException("Index out of bounds.");
320
321 if (!(Value.AssociatedObjectValue is double d))
322 throw new ScriptException("Elements in a double vector are required to be double values.");
323
324 double[] Values = this.Values;
325 this.elements = null;
326
327 Values[Index] = d;
328 }
329
336 public override bool TryConvertTo(Type DesiredType, out object Value)
337 {
338 if (DesiredType == typeof(double[]))
339 {
340 Value = this.Values;
341 return true;
342 }
343 else if (DesiredType.IsAssignableFrom(typeof(DoubleVector).GetTypeInfo()))
344 {
345 Value = this;
346 return true;
347 }
348 else if (DesiredType.IsArray)
349 {
350 Type ElementType = DesiredType.GetElementType();
351 int i = 0;
352
353 if (ElementType == typeof(byte))
354 {
355 byte[] ba = new byte[this.dimension];
356 foreach (double d in this.Values)
357 {
358 if (d >= byte.MinValue && d <= byte.MaxValue)
359 ba[i++] = (byte)d;
360 else
361 {
362 Value = null;
363 return false;
364 }
365 }
366
367 Value = ba;
368 return true;
369 }
370 else if (ElementType == typeof(decimal))
371 {
372 decimal[] da = new decimal[this.dimension];
373 foreach (double d in this.Values)
374 da[i++] = (decimal)d;
375
376 Value = da;
377 return true;
378 }
379 else if (ElementType == typeof(short))
380 {
381 short[] sa = new short[this.dimension];
382 foreach (double d in this.Values)
383 {
384 if (d >= short.MinValue && d <= short.MaxValue)
385 sa[i++] = (short)d;
386 else
387 {
388 Value = null;
389 return false;
390 }
391 }
392
393 Value = sa;
394 return true;
395 }
396 else if (ElementType == typeof(int))
397 {
398 int[] ia = new int[this.dimension];
399 foreach (double d in this.Values)
400 {
401 if (d >= int.MinValue && d <= int.MaxValue)
402 ia[i++] = (byte)d;
403 else
404 {
405 Value = null;
406 return false;
407 }
408 }
409
410 Value = ia;
411 return true;
412 }
413 else if (ElementType == typeof(long))
414 {
415 long[] la = new long[this.dimension];
416 foreach (double d in this.Values)
417 {
418 if (d >= long.MinValue && d <= long.MaxValue)
419 la[i++] = (long)d;
420 else
421 {
422 Value = null;
423 return false;
424 }
425 }
426
427 Value = la;
428 return true;
429 }
430 else if (ElementType == typeof(sbyte))
431 {
432 sbyte[] sba = new sbyte[this.dimension];
433 foreach (double d in this.Values)
434 {
435 if (d >= sbyte.MinValue && d <= sbyte.MaxValue)
436 sba[i++] = (sbyte)d;
437 else
438 {
439 Value = null;
440 return false;
441 }
442 }
443
444 Value = sba;
445 return true;
446 }
447 else if (ElementType == typeof(float))
448 {
449 float[] fa = new float[this.dimension];
450 foreach (double d in this.Values)
451 fa[i++] = (float)d;
452
453 Value = fa;
454 return true;
455 }
456 else if (ElementType == typeof(ushort))
457 {
458 ushort[] usa = new ushort[this.dimension];
459 foreach (double d in this.Values)
460 {
461 if (d >= ushort.MinValue && d <= ushort.MaxValue)
462 usa[i++] = (ushort)d;
463 else
464 {
465 Value = null;
466 return false;
467 }
468 }
469
470 Value = usa;
471 return true;
472 }
473 else if (ElementType == typeof(uint))
474 {
475 uint[] uia = new uint[this.dimension];
476 foreach (double d in this.Values)
477 {
478 if (d >= uint.MinValue && d <= uint.MaxValue)
479 uia[i++] = (uint)d;
480 else
481 {
482 Value = null;
483 return false;
484 }
485 }
486
487 Value = uia;
488 return true;
489 }
490 else if (ElementType == typeof(ulong))
491 {
492 ulong[] ula = new ulong[this.dimension];
493 foreach (double d in this.Values)
494 {
495 if (d >= ulong.MinValue && d <= ulong.MaxValue)
496 ula[i++] = (ulong)d;
497 else
498 {
499 Value = null;
500 return false;
501 }
502 }
503
504 Value = ula;
505 return true;
506 }
507 else
508 {
509 Value = this;
510 return true;
511 }
512 }
513 else
514 return Expression.TryConvert(this.Values, DesiredType, true, out Value);
515 }
516
517 }
518}
Node referencing a chunk in a ChunkedList<T>
Definition: ChunkNode.cs:11
ChunkNode< T > Next
Next chunk
Definition: ChunkNode.cs:26
int Pos
Index after the last element in chunk.
Definition: ChunkNode.cs:51
int Start
Index of first element in chunk.
Definition: ChunkNode.cs:46
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 object AssociatedObjectValue
Associated object value.
Definition: Element.cs:43
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 string ToString(double Value)
Converts a value to a string, that can be parsed as part of an expression.
Definition: Expression.cs:4760
Base class for all nodes in a parsed script tree.
Definition: ScriptNode.cs:69
override object AssociatedObjectValue
Associated object value.
override bool TryConvertTo(Type DesiredType, out object Value)
Converts the value to a .NET type.
double[] Values
Vector element values.
Definition: DoubleVector.cs:48
override IGroupElement Negate()
Negates the element.
override IAbelianGroupElement Zero
Returns the zero element of the group.
override IVectorSpace AssociatedVectorSpace
Associated Right-VectorSpace.
override bool Equals(object obj)
Compares the element to another.
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 IVectorSpaceElement MultiplyScalar(IFieldElement Scalar)
Tries to multiply a scalar to the current element.
override void SetElement(int Index, IElement Value)
Sets an element in the vector.
DoubleVector(params double[] Values)
Double-valued vector.
Definition: DoubleVector.cs:26
override IAbelianGroupElement Add(IAbelianGroupElement Element)
Tries to add an element to the current element.
DoubleVector(ICollection< IElement > Elements)
Double-valued vector.
Definition: DoubleVector.cs:37
override bool IsScalar
If the element represents a scalar value.
override int Dimension
Dimension of vector.
override int GetHashCode()
Calculates a hash code of the element.
override IElement GetElement(int Index)
Gets an element of the vector.
ICollection< IElement > Elements
Vector elements.
Definition: DoubleVector.cs:96
override ICollection< IElement > ChildElements
An enumeration of child elements. If the element is a scalar, this property will return null.
override IElement Encapsulate(ChunkedList< IElement > Elements, ScriptNode Node)
Encapsulates a set of elements into a similar structure as that provided by the current element.
Pseudo-vector space of Double-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.
Basic interface for all types of modules.
Definition: IVectorSpace.cs:10