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;
2using System.Collections.Generic;
3using System.Numerics;
4using System.Reflection;
10
12{
16 public sealed class ComplexVector : VectorSpaceElement
17 {
18 private Complex[] values;
19 private ICollection<IElement> elements;
20 private readonly int dimension;
21
26 public ComplexVector(params Complex[] Values)
27 {
28 this.values = Values;
29 this.elements = null;
30 this.dimension = Values.Length;
31 }
32
37 public ComplexVector(ICollection<IElement> Elements)
38 {
39 this.values = null;
40 this.elements = Elements;
41 this.dimension = Elements.Count;
42 }
43
47 public Complex[] Values
48 {
49 get
50 {
51 if (this.values is null)
52 {
53 Complex[] v = new Complex[this.dimension];
54 int i = 0;
55
56 foreach (IElement Element in this.elements)
57 {
58 if (!(Element.AssociatedObjectValue is Complex z))
59 z = 0;
60
61 v[i++] = z;
62 }
63
64 this.values = v;
65 }
66
67 return this.values;
68 }
69 }
70
74 public ICollection<IElement> Elements
75 {
76 get
77 {
78 if (this.elements is null)
79 {
80 int i;
81 IElement[] v = new IElement[this.dimension];
82
83 for (i = 0; i < this.dimension; i++)
84 v[i] = new ComplexNumber(this.values[i]);
85
86 this.elements = v;
87 }
88
89 return this.elements;
90 }
91 }
92
96 public override int Dimension => this.dimension;
97
99 public override string ToString()
100 {
101 return Expression.ToString(this.Values);
102 }
103
108 {
109 get
110 {
111 if (this.associatedVectorSpace is null)
112 this.associatedVectorSpace = new ComplexVectors(this.dimension);
113
114 return this.associatedVectorSpace;
115 }
116 }
117
118 private ComplexVectors associatedVectorSpace = null;
119
123 public override object AssociatedObjectValue => this.Values;
124
131 {
133 Complex d;
134 if (ComplexNumber is null)
135 {
137 if (DoubleNumber is null)
138 return null;
139 else
140 d = new Complex(DoubleNumber.Value, 0);
141 }
142 else
144
145 int i;
146 Complex[] Values = this.Values;
147 Complex[] v = new Complex[this.dimension];
148
149 for (i = 0; i < this.dimension; i++)
150 v[i] = d * Values[i];
151
152 return new ComplexVector(v);
153 }
154
161 {
163 if (ComplexVector is null)
164 return null;
165
166 int i;
167 if (ComplexVector.dimension != this.dimension)
168 return null;
169
170 Complex[] Values = this.Values;
171 Complex[] Values2 = ComplexVector.Values;
172 Complex[] v = new Complex[this.dimension];
173 for (i = 0; i < this.dimension; i++)
174 v[i] = Values[i] + Values2[i];
175
176 return new ComplexVector(v);
177 }
178
183 public override IGroupElement Negate()
184 {
185 int i;
186 Complex[] v = new Complex[this.dimension];
187 Complex[] Values = this.Values;
188 for (i = 0; i < this.dimension; i++)
189 v[i] = -Values[i];
190
191 return new ComplexVector(v);
192 }
193
199 public override bool Equals(object obj)
200 {
202 if (ComplexVector is null)
203 return false;
204
205 int i;
206 if (ComplexVector.dimension != this.dimension)
207 return false;
208
209 Complex[] Values = this.Values;
210 Complex[] Values2 = ComplexVector.Values;
211 for (i = 0; i < this.dimension; i++)
212 {
213 if (Values[i] != Values2[i])
214 return false;
215 }
216
217 return true;
218 }
219
224 public override int GetHashCode()
225 {
226 Complex[] Values = this.Values;
227 int Result = 0;
228 int i;
229
230 for (i = 0; i < this.dimension; i++)
231 Result ^= Values[i].GetHashCode();
232
233 return Result;
234 }
235
239 public override bool IsScalar
240 {
241 get { return false; }
242 }
243
247 public override ICollection<IElement> ChildElements => this.Elements;
248
255 public override IElement Encapsulate(ICollection<IElement> Elements, ScriptNode Node)
256 {
257 return VectorDefinition.Encapsulate(Elements, true, Node);
258 }
259
264 {
265 get
266 {
267 if (this.zero is null)
268 this.zero = new ComplexVector(new Complex[this.dimension]);
269
270 return this.zero;
271 }
272 }
273
274 private ComplexVector zero = null;
275
281 public override IElement GetElement(int Index)
282 {
283 if (Index < 0 || Index >= this.dimension)
284 throw new ScriptException("Index out of bounds.");
285
286 Complex[] V = this.Values;
287
288 return new ComplexNumber(V[Index]);
289 }
290
296 public override void SetElement(int Index, IElement Value)
297 {
298 if (Index < 0 || Index >= this.dimension)
299 throw new ScriptException("Index out of bounds.");
300
301 if (!(Value.AssociatedObjectValue is Complex V))
302 throw new ScriptException("Elements in a complex vector are required to be complex values.");
303
304 Complex[] Values = this.Values;
305 this.elements = null;
306
307 Values[Index] = V;
308 }
309
316 public override bool TryConvertTo(Type DesiredType, out object Value)
317 {
318 if (DesiredType == typeof(Complex[]))
319 {
320 Value = this.Values;
321 return true;
322 }
323 else if (DesiredType.GetTypeInfo().IsAssignableFrom(typeof(ComplexVector).GetTypeInfo()))
324 {
325 Value = this;
326 return true;
327 }
328 else if (DesiredType.IsArray)
329 {
330 Type ElementType = DesiredType.GetElementType();
331 int i = 0;
332
333 double d;
334
335 if (ElementType == typeof(byte))
336 {
337 byte[] ba = new byte[this.dimension];
338
339 foreach (Complex c in this.Values)
340 {
341 if (c.Imaginary == 0 && (d = c.Real) >= byte.MinValue && d <= byte.MaxValue)
342 ba[i++] = (byte)d;
343 else
344 {
345 Value = null;
346 return false;
347 }
348 }
349
350 Value = ba;
351 return true;
352 }
353 else if (ElementType == typeof(decimal))
354 {
355 decimal[] da = new decimal[this.dimension];
356 foreach (Complex c in this.Values)
357 {
358 if (c.Imaginary == 0)
359 da[i++] = (decimal)c.Real;
360 else
361 {
362 Value = null;
363 return false;
364 }
365 }
366
367 Value = da;
368 return true;
369 }
370 else if (ElementType == typeof(double))
371 {
372 double[] da2 = new double[this.dimension];
373 foreach (Complex c in this.Values)
374 {
375 if (c.Imaginary == 0)
376 da2[i++] = (double)c.Real;
377 else
378 {
379 Value = null;
380 return false;
381 }
382 }
383
384 Value = da2;
385 return true;
386 }
387 else if (ElementType == typeof(short))
388 {
389 short[] sa = new short[this.dimension];
390 foreach (Complex c in this.Values)
391 {
392 if (c.Imaginary == 0 && (d = c.Real) >= short.MinValue && d <= short.MaxValue)
393 sa[i++] = (short)d;
394 else
395 {
396 Value = null;
397 return false;
398 }
399 }
400
401 Value = sa;
402 return true;
403 }
404 else if (ElementType == typeof(int))
405 {
406 int[] ia = new int[this.dimension];
407 foreach (Complex c in this.Values)
408 {
409 if (c.Imaginary == 0 && (d = c.Real) >= int.MinValue && d <= int.MaxValue)
410 ia[i++] = (byte)d;
411 else
412 {
413 Value = null;
414 return false;
415 }
416 }
417
418 Value = ia;
419 return true;
420 }
421 else if (ElementType == typeof(long))
422 {
423 long[] la = new long[this.dimension];
424 foreach (Complex c in this.Values)
425 {
426 if (c.Imaginary == 0 && (d = c.Real) >= long.MinValue && d <= long.MaxValue)
427 la[i++] = (long)d;
428 else
429 {
430 Value = null;
431 return false;
432 }
433 }
434
435 Value = la;
436 return true;
437 }
438 else if (ElementType == typeof(sbyte))
439 {
440 sbyte[] sba = new sbyte[this.dimension];
441 foreach (Complex c in this.Values)
442 {
443 if (c.Imaginary == 0 && (d = c.Real) >= sbyte.MinValue && d <= sbyte.MaxValue)
444 sba[i++] = (sbyte)d;
445 else
446 {
447 Value = null;
448 return false;
449 }
450 }
451
452 Value = sba;
453 return true;
454 }
455 else if (ElementType == typeof(float))
456 {
457 float[] fa = new float[this.dimension];
458 foreach (Complex c in this.Values)
459 {
460 if (c.Imaginary == 0)
461 fa[i++] = (float)c.Real;
462 else
463 {
464 Value = null;
465 return false;
466 }
467 }
468
469 Value = fa;
470 return true;
471 }
472 else if (ElementType == typeof(ushort))
473 {
474 ushort[] usa = new ushort[this.dimension];
475 foreach (Complex c in this.Values)
476 {
477 if (c.Imaginary == 0 && (d = c.Real) >= ushort.MinValue && d <= ushort.MaxValue)
478 usa[i++] = (ushort)d;
479 else
480 {
481 Value = null;
482 return false;
483 }
484 }
485
486 Value = usa;
487 return true;
488 }
489 else if (ElementType == typeof(uint))
490 {
491 uint[] uia = new uint[this.dimension];
492 foreach (Complex c in this.Values)
493 {
494 if (c.Imaginary == 0 && (d = c.Real) >= uint.MinValue && d <= uint.MaxValue)
495 uia[i++] = (uint)d;
496 else
497 {
498 Value = null;
499 return false;
500 }
501 }
502
503 Value = uia;
504 return true;
505 }
506 else if (ElementType == typeof(ulong))
507 {
508 ulong[] ula = new ulong[this.dimension];
509 foreach (Complex c in this.Values)
510 {
511 if (c.Imaginary == 0 && (d = c.Real) >= ulong.MinValue && d <= ulong.MaxValue)
512 ula[i++] = (ulong)d;
513 else
514 {
515 Value = null;
516 return false;
517 }
518 }
519
520 Value = ula;
521 return true;
522 }
523 else
524 {
525 Value = this;
526 return true;
527 }
528 }
529 else
530 return Expression.TryConvert(this.Values, DesiredType, out Value);
531 }
532
533 }
534}
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
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 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: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