Neuron®
The Neuron® is the basis for the creation of open and secure federated networks for smart societies.
Loading...
Searching...
No Matches
ComplexVector.cs
1using System;
3using System.Numerics;
4using System.Reflection;
11
13{
17 public sealed class ComplexVector : VectorSpaceElement
18 {
19 private Complex[] values;
20 private ICollection<IElement> elements;
21 private readonly int dimension;
22
27 public ComplexVector(params Complex[] Values)
28 {
29 this.values = Values;
30 this.elements = null;
31 this.dimension = Values.Length;
32 }
33
38 public ComplexVector(ICollection<IElement> Elements)
39 {
40 this.values = null;
41 this.elements = Elements;
42 this.dimension = Elements.Count;
43 }
44
48 public Complex[] Values
49 {
50 get
51 {
52 if (this.values is null)
53 {
54 Complex[] v = new Complex[this.dimension];
55 int i = 0;
56
57 if (this.elements is ChunkedList<IElement> Values)
58 {
59 ChunkNode<IElement> Loop = Values.FirstChunk;
60 int j, c;
61
62 while (!(Loop is null))
63 {
64 for (j = Loop.Start, c = Loop.Pos; j < c; j++)
65 {
66 if (!(Loop[j].AssociatedObjectValue is Complex z))
67 z = 0;
68
69 v[i++] = z;
70 }
71
72 Loop = Loop.Next;
73 }
74 }
75 else
76 {
77 foreach (IElement Element in this.elements)
78 {
79 if (!(Element.AssociatedObjectValue is Complex z))
80 z = 0;
81
82 v[i++] = z;
83 }
84 }
85
86 this.values = v;
87 }
88
89 return this.values;
90 }
91 }
92
96 public ICollection<IElement> Elements
97 {
98 get
99 {
100 if (this.elements is null)
101 {
102 int i;
103 IElement[] v = new IElement[this.dimension];
104
105 for (i = 0; i < this.dimension; i++)
106 v[i] = new ComplexNumber(this.values[i]);
107
108 this.elements = v;
109 }
110
111 return this.elements;
112 }
113 }
114
118 public override int Dimension => this.dimension;
119
121 public override string ToString()
122 {
123 return Expression.ToString(this.Values);
124 }
125
130 {
131 get
132 {
133 if (this.associatedVectorSpace is null)
134 this.associatedVectorSpace = new ComplexVectors(this.dimension);
135
136 return this.associatedVectorSpace;
137 }
138 }
139
140 private ComplexVectors associatedVectorSpace = null;
141
145 public override object AssociatedObjectValue => this.Values;
146
153 {
155 Complex d;
156 if (ComplexNumber is null)
157 {
159 if (DoubleNumber is null)
160 return null;
161 else
162 d = new Complex(DoubleNumber.Value, 0);
163 }
164 else
166
167 int i;
168 Complex[] Values = this.Values;
169 Complex[] v = new Complex[this.dimension];
170
171 for (i = 0; i < this.dimension; i++)
172 v[i] = d * Values[i];
173
174 return new ComplexVector(v);
175 }
176
183 {
185 if (ComplexVector is null)
186 return null;
187
188 int i;
189 if (ComplexVector.dimension != this.dimension)
190 return null;
191
192 Complex[] Values = this.Values;
193 Complex[] Values2 = ComplexVector.Values;
194 Complex[] v = new Complex[this.dimension];
195 for (i = 0; i < this.dimension; i++)
196 v[i] = Values[i] + Values2[i];
197
198 return new ComplexVector(v);
199 }
200
205 public override IGroupElement Negate()
206 {
207 int i;
208 Complex[] v = new Complex[this.dimension];
209 Complex[] Values = this.Values;
210 for (i = 0; i < this.dimension; i++)
211 v[i] = -Values[i];
212
213 return new ComplexVector(v);
214 }
215
221 public override bool Equals(object obj)
222 {
224 if (ComplexVector is null)
225 return false;
226
227 int i;
228 if (ComplexVector.dimension != this.dimension)
229 return false;
230
231 Complex[] Values = this.Values;
232 Complex[] Values2 = ComplexVector.Values;
233 for (i = 0; i < this.dimension; i++)
234 {
235 if (Values[i] != Values2[i])
236 return false;
237 }
238
239 return true;
240 }
241
246 public override int GetHashCode()
247 {
248 Complex[] Values = this.Values;
249 int Result = 0;
250 int i;
251
252 for (i = 0; i < this.dimension; i++)
253 Result ^= Values[i].GetHashCode();
254
255 return Result;
256 }
257
261 public override bool IsScalar
262 {
263 get { return false; }
264 }
265
269 public override ICollection<IElement> ChildElements => this.Elements;
270
278 {
279 return VectorDefinition.Encapsulate(Elements, true, Node);
280 }
281
288 public override IElement Encapsulate(ICollection<IElement> Elements, ScriptNode Node)
289 {
290 return VectorDefinition.Encapsulate(Elements, true, Node);
291 }
292
297 {
298 get
299 {
300 if (this.zero is null)
301 this.zero = new ComplexVector(new Complex[this.dimension]);
302
303 return this.zero;
304 }
305 }
306
307 private ComplexVector zero = null;
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 Complex[] V = this.Values;
320
321 return new ComplexNumber(V[Index]);
322 }
323
329 public override void SetElement(int Index, IElement Value)
330 {
331 if (Index < 0 || Index >= this.dimension)
332 throw new ScriptException("Index out of bounds.");
333
334 if (!(Value.AssociatedObjectValue is Complex V))
335 throw new ScriptException("Elements in a complex vector are required to be complex values.");
336
337 Complex[] Values = this.Values;
338 this.elements = null;
339
340 Values[Index] = V;
341 }
342
349 public override bool TryConvertTo(Type DesiredType, out object Value)
350 {
351 if (DesiredType == typeof(Complex[]))
352 {
353 Value = this.Values;
354 return true;
355 }
356 else if (DesiredType.IsAssignableFrom(typeof(ComplexVector).GetTypeInfo()))
357 {
358 Value = this;
359 return true;
360 }
361 else if (DesiredType.IsArray)
362 {
363 Type ElementType = DesiredType.GetElementType();
364 int i = 0;
365
366 double d;
367
368 if (ElementType == typeof(byte))
369 {
370 byte[] ba = new byte[this.dimension];
371
372 foreach (Complex c in this.Values)
373 {
374 if (c.Imaginary == 0 && (d = c.Real) >= byte.MinValue && d <= byte.MaxValue)
375 ba[i++] = (byte)d;
376 else
377 {
378 Value = null;
379 return false;
380 }
381 }
382
383 Value = ba;
384 return true;
385 }
386 else if (ElementType == typeof(decimal))
387 {
388 decimal[] da = new decimal[this.dimension];
389 foreach (Complex c in this.Values)
390 {
391 if (c.Imaginary == 0)
392 da[i++] = (decimal)c.Real;
393 else
394 {
395 Value = null;
396 return false;
397 }
398 }
399
400 Value = da;
401 return true;
402 }
403 else if (ElementType == typeof(double))
404 {
405 double[] da2 = new double[this.dimension];
406 foreach (Complex c in this.Values)
407 {
408 if (c.Imaginary == 0)
409 da2[i++] = (double)c.Real;
410 else
411 {
412 Value = null;
413 return false;
414 }
415 }
416
417 Value = da2;
418 return true;
419 }
420 else if (ElementType == typeof(short))
421 {
422 short[] sa = new short[this.dimension];
423 foreach (Complex c in this.Values)
424 {
425 if (c.Imaginary == 0 && (d = c.Real) >= short.MinValue && d <= short.MaxValue)
426 sa[i++] = (short)d;
427 else
428 {
429 Value = null;
430 return false;
431 }
432 }
433
434 Value = sa;
435 return true;
436 }
437 else if (ElementType == typeof(int))
438 {
439 int[] ia = new int[this.dimension];
440 foreach (Complex c in this.Values)
441 {
442 if (c.Imaginary == 0 && (d = c.Real) >= int.MinValue && d <= int.MaxValue)
443 ia[i++] = (byte)d;
444 else
445 {
446 Value = null;
447 return false;
448 }
449 }
450
451 Value = ia;
452 return true;
453 }
454 else if (ElementType == typeof(long))
455 {
456 long[] la = new long[this.dimension];
457 foreach (Complex c in this.Values)
458 {
459 if (c.Imaginary == 0 && (d = c.Real) >= long.MinValue && d <= long.MaxValue)
460 la[i++] = (long)d;
461 else
462 {
463 Value = null;
464 return false;
465 }
466 }
467
468 Value = la;
469 return true;
470 }
471 else if (ElementType == typeof(sbyte))
472 {
473 sbyte[] sba = new sbyte[this.dimension];
474 foreach (Complex c in this.Values)
475 {
476 if (c.Imaginary == 0 && (d = c.Real) >= sbyte.MinValue && d <= sbyte.MaxValue)
477 sba[i++] = (sbyte)d;
478 else
479 {
480 Value = null;
481 return false;
482 }
483 }
484
485 Value = sba;
486 return true;
487 }
488 else if (ElementType == typeof(float))
489 {
490 float[] fa = new float[this.dimension];
491 foreach (Complex c in this.Values)
492 {
493 if (c.Imaginary == 0)
494 fa[i++] = (float)c.Real;
495 else
496 {
497 Value = null;
498 return false;
499 }
500 }
501
502 Value = fa;
503 return true;
504 }
505 else if (ElementType == typeof(ushort))
506 {
507 ushort[] usa = new ushort[this.dimension];
508 foreach (Complex c in this.Values)
509 {
510 if (c.Imaginary == 0 && (d = c.Real) >= ushort.MinValue && d <= ushort.MaxValue)
511 usa[i++] = (ushort)d;
512 else
513 {
514 Value = null;
515 return false;
516 }
517 }
518
519 Value = usa;
520 return true;
521 }
522 else if (ElementType == typeof(uint))
523 {
524 uint[] uia = new uint[this.dimension];
525 foreach (Complex c in this.Values)
526 {
527 if (c.Imaginary == 0 && (d = c.Real) >= uint.MinValue && d <= uint.MaxValue)
528 uia[i++] = (uint)d;
529 else
530 {
531 Value = null;
532 return false;
533 }
534 }
535
536 Value = uia;
537 return true;
538 }
539 else if (ElementType == typeof(ulong))
540 {
541 ulong[] ula = new ulong[this.dimension];
542 foreach (Complex c in this.Values)
543 {
544 if (c.Imaginary == 0 && (d = c.Real) >= ulong.MinValue && d <= ulong.MaxValue)
545 ula[i++] = (ulong)d;
546 else
547 {
548 Value = null;
549 return false;
550 }
551 }
552
553 Value = ula;
554 return true;
555 }
556 else
557 {
558 Value = this;
559 return true;
560 }
561 }
562 else
563 return Expression.TryConvert(this.Values, DesiredType, true, out Value);
564 }
565
566 }
567}
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
ComplexVector(ICollection< IElement > Elements)
Complex-valued vector.
override IGroupElement Negate()
Negates the element.
override bool TryConvertTo(Type DesiredType, out object Value)
Converts the value to a .NET type.
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 bool IsScalar
If the element represents a scalar value.
override int Dimension
Dimension of vector.
ComplexVector(params Complex[] Values)
Complex-valued vector.
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 int GetHashCode()
Calculates a hash code of the element.
override IAbelianGroupElement Add(IAbelianGroupElement Element)
Tries to add an element to the current element.
override IAbelianGroupElement Zero
Returns the zero element of the group.
override IVectorSpace AssociatedVectorSpace
Associated Right-VectorSpace.
override object AssociatedObjectValue
Associated object value.
Complex[] Values
Vector element values.
ICollection< IElement > Elements
Vector elements.
override bool Equals(object obj)
Compares the element to another.
override void SetElement(int Index, IElement Value)
Sets an element in the 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 ICollection< IElement > ChildElements
An enumeration of child elements. If the element is a scalar, this property will return null.
Pseudo-vector space of Complex-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