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;
2using System.Collections.Generic;
3using System.Reflection;
9
11{
15 public sealed class DoubleVector : VectorSpaceElement
16 {
17 private double[] values;
18 private ICollection<IElement> elements;
19 private readonly int dimension;
20
25 public DoubleVector(params double[] Values)
26 {
27 this.values = Values;
28 this.elements = null;
29 this.dimension = Values.Length;
30 }
31
36 public DoubleVector(ICollection<IElement> Elements)
37 {
38 this.values = null;
39 this.elements = Elements;
40 this.dimension = Elements.Count;
41 }
42
46 public double[] Values
47 {
48 get
49 {
50 if (this.values is null)
51 {
52 double[] v = new double[this.dimension];
53 int i = 0;
54
55 foreach (IElement Element in this.elements)
56 {
57 if (Element.AssociatedObjectValue is double d)
58 v[i++] = d;
59 }
60
61 this.values = v;
62 }
63
64 return this.values;
65 }
66 }
67
71 public ICollection<IElement> Elements
72 {
73 get
74 {
75 if (this.elements is null)
76 {
77 int i;
78 IElement[] v = new IElement[this.dimension];
79
80 for (i = 0; i < this.dimension; i++)
81 v[i] = new DoubleNumber(this.values[i]);
82
83 this.elements = v;
84 }
85
86 return this.elements;
87 }
88 }
89
93 public override int Dimension => this.dimension;
94
96 public override string ToString()
97 {
98 return Expression.ToString(this.Values);
99 }
100
105 {
106 get
107 {
108 if (this.associatedVectorSpace is null)
109 this.associatedVectorSpace = new DoubleVectors(this.dimension);
110
111 return this.associatedVectorSpace;
112 }
113 }
114
115 private DoubleVectors associatedVectorSpace = null;
116
120 public override object AssociatedObjectValue => this.Values;
121
128 {
129 if (!(Scalar.AssociatedObjectValue is double d))
130 return null;
131
132 int i;
133 double[] Values = this.Values;
134 double[] v = new double[this.dimension];
135
136 for (i = 0; i < this.dimension; i++)
137 v[i] = d * Values[i];
138
139 return new DoubleVector(v);
140 }
141
148 {
150 return null;
151
152 int i;
153 if (DoubleVector.dimension != this.dimension)
154 return null;
155
156 double[] Values = this.Values;
157 double[] Values2 = DoubleVector.Values;
158 double[] v = new double[this.dimension];
159 for (i = 0; i < this.dimension; i++)
160 v[i] = Values[i] + Values2[i];
161
162 return new DoubleVector(v);
163 }
164
169 public override IGroupElement Negate()
170 {
171 int i;
172 double[] v = new double[this.dimension];
173 double[] Values = this.Values;
174 for (i = 0; i < this.dimension; i++)
175 v[i] = -Values[i];
176
177 return new DoubleVector(v);
178 }
179
185 public override bool Equals(object obj)
186 {
187 if (!(obj is DoubleVector DoubleVector))
188 return false;
189
190 int i;
191 if (DoubleVector.dimension != this.dimension)
192 return false;
193
194 double[] Values = this.Values;
195 double[] Values2 = DoubleVector.Values;
196 for (i = 0; i < this.dimension; i++)
197 {
198 if (Values[i] != Values2[i])
199 return false;
200 }
201
202 return true;
203 }
204
209 public override int GetHashCode()
210 {
211 double[] Values = this.Values;
212 int Result = 0;
213 int i;
214
215 for (i = 0; i < this.dimension; i++)
216 Result ^= Values[i].GetHashCode();
217
218 return Result;
219 }
220
224 public override bool IsScalar
225 {
226 get { return false; }
227 }
228
232 public override ICollection<IElement> ChildElements => this.Elements;
233
240 public override IElement Encapsulate(ICollection<IElement> Elements, ScriptNode Node)
241 {
242 return VectorDefinition.Encapsulate(Elements, true, Node);
243 }
244
249 {
250 get
251 {
252 if (this.zero is null)
253 this.zero = new DoubleVector(new double[this.dimension]);
254
255 return this.zero;
256 }
257 }
258
259 private DoubleVector zero = null;
260
266 public override IElement GetElement(int Index)
267 {
268 if (Index < 0 || Index >= this.dimension)
269 throw new ScriptException("Index out of bounds.");
270
271 double[] V = this.Values;
272
273 return new DoubleNumber(V[Index]);
274 }
275
281 public override void SetElement(int Index, IElement Value)
282 {
283 if (Index < 0 || Index >= this.dimension)
284 throw new ScriptException("Index out of bounds.");
285
286 if (!(Value.AssociatedObjectValue is double d))
287 throw new ScriptException("Elements in a double vector are required to be double values.");
288
289 double[] Values = this.Values;
290 this.elements = null;
291
292 Values[Index] = d;
293 }
294
301 public override bool TryConvertTo(Type DesiredType, out object Value)
302 {
303 if (DesiredType == typeof(double[]))
304 {
305 Value = this.Values;
306 return true;
307 }
308 else if (DesiredType.GetTypeInfo().IsAssignableFrom(typeof(DoubleVector).GetTypeInfo()))
309 {
310 Value = this;
311 return true;
312 }
313 else if (DesiredType.IsArray)
314 {
315 Type ElementType = DesiredType.GetElementType();
316 int i = 0;
317
318 if (ElementType == typeof(byte))
319 {
320 byte[] ba = new byte[this.dimension];
321 foreach (double d in this.Values)
322 {
323 if (d >= byte.MinValue && d <= byte.MaxValue)
324 ba[i++] = (byte)d;
325 else
326 {
327 Value = null;
328 return false;
329 }
330 }
331
332 Value = ba;
333 return true;
334 }
335 else if (ElementType == typeof(decimal))
336 {
337 decimal[] da = new decimal[this.dimension];
338 foreach (double d in this.Values)
339 da[i++] = (decimal)d;
340
341 Value = da;
342 return true;
343 }
344 else if (ElementType == typeof(short))
345 {
346 short[] sa = new short[this.dimension];
347 foreach (double d in this.Values)
348 {
349 if (d >= short.MinValue && d <= short.MaxValue)
350 sa[i++] = (short)d;
351 else
352 {
353 Value = null;
354 return false;
355 }
356 }
357
358 Value = sa;
359 return true;
360 }
361 else if (ElementType == typeof(int))
362 {
363 int[] ia = new int[this.dimension];
364 foreach (double d in this.Values)
365 {
366 if (d >= int.MinValue && d <= int.MaxValue)
367 ia[i++] = (byte)d;
368 else
369 {
370 Value = null;
371 return false;
372 }
373 }
374
375 Value = ia;
376 return true;
377 }
378 else if (ElementType == typeof(long))
379 {
380 long[] la = new long[this.dimension];
381 foreach (double d in this.Values)
382 {
383 if (d >= long.MinValue && d <= long.MaxValue)
384 la[i++] = (long)d;
385 else
386 {
387 Value = null;
388 return false;
389 }
390 }
391
392 Value = la;
393 return true;
394 }
395 else if (ElementType == typeof(sbyte))
396 {
397 sbyte[] sba = new sbyte[this.dimension];
398 foreach (double d in this.Values)
399 {
400 if (d >= sbyte.MinValue && d <= sbyte.MaxValue)
401 sba[i++] = (sbyte)d;
402 else
403 {
404 Value = null;
405 return false;
406 }
407 }
408
409 Value = sba;
410 return true;
411 }
412 else if (ElementType == typeof(float))
413 {
414 float[] fa = new float[this.dimension];
415 foreach (double d in this.Values)
416 fa[i++] = (float)d;
417
418 Value = fa;
419 return true;
420 }
421 else if (ElementType == typeof(ushort))
422 {
423 ushort[] usa = new ushort[this.dimension];
424 foreach (double d in this.Values)
425 {
426 if (d >= ushort.MinValue && d <= ushort.MaxValue)
427 usa[i++] = (ushort)d;
428 else
429 {
430 Value = null;
431 return false;
432 }
433 }
434
435 Value = usa;
436 return true;
437 }
438 else if (ElementType == typeof(uint))
439 {
440 uint[] uia = new uint[this.dimension];
441 foreach (double d in this.Values)
442 {
443 if (d >= uint.MinValue && d <= uint.MaxValue)
444 uia[i++] = (uint)d;
445 else
446 {
447 Value = null;
448 return false;
449 }
450 }
451
452 Value = uia;
453 return true;
454 }
455 else if (ElementType == typeof(ulong))
456 {
457 ulong[] ula = new ulong[this.dimension];
458 foreach (double d in this.Values)
459 {
460 if (d >= ulong.MinValue && d <= ulong.MaxValue)
461 ula[i++] = (ulong)d;
462 else
463 {
464 Value = null;
465 return false;
466 }
467 }
468
469 Value = ula;
470 return true;
471 }
472 else
473 {
474 Value = this;
475 return true;
476 }
477 }
478 else
479 return Expression.TryConvert(this.Values, DesiredType, out Value);
480 }
481
482 }
483}
Base class for all types of elements.
Definition: Element.cs:13
abstract object AssociatedObjectValue
Associated object value.
Definition: Element.cs:46
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 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
static string ToString(double Value)
Converts a value to a string, that can be parsed as part of an expression.
Definition: Expression.cs:4496
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:47
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:25
override IAbelianGroupElement Add(IAbelianGroupElement Element)
Tries to add an element to the current element.
DoubleVector(ICollection< IElement > Elements)
Double-valued vector.
Definition: DoubleVector.cs:36
override bool IsScalar
If the element represents a scalar value.
override int Dimension
Dimension of vector.
Definition: DoubleVector.cs:93
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:72
override ICollection< IElement > ChildElements
An enumeration of child elements. If the element is a scalar, this property will return null.
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: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.
Basic interface for all types of modules.
Definition: IVectorSpace.cs:10