Neuron®
The Neuron® is the basis for the creation of open and secure federated networks for smart societies.
Loading...
Searching...
No Matches
ComplexNumber.cs
1using System;
2using System.Numerics;
3using System.Reflection;
6
8{
12 public sealed class ComplexNumber : FieldElement
13 {
14 private static readonly ComplexNumbers associatedField = new ComplexNumbers();
15
16 private Complex value;
17
22 public ComplexNumber(Complex Value)
23 {
24 this.value = Value;
25 }
26
32 public ComplexNumber(double Re, double Im)
33 {
34 this.value = new Complex(Re, Im);
35 }
36
40 public Complex Value
41 {
42 get => this.value;
43 set => this.value = value;
44 }
45
47 public override string ToString()
48 {
49 return Expression.ToString(this.value);
50 }
51
55 public override IField AssociatedField
56 {
57 get { return associatedField; }
58 }
59
63 public override object AssociatedObjectValue => this.value;
64
71 {
72 object Obj = Element.AssociatedObjectValue;
73
74 if (Obj is Complex z)
75 return new ComplexNumber(this.value * z);
76 else if (Obj is double d)
77 return new ComplexNumber(this.value * d);
78 else
79 return null;
80 }
81
86 public override IRingElement Invert()
87 {
88 return new ComplexNumber(1.0 / this.value);
89 }
90
97 {
98 object Obj = Element.AssociatedObjectValue;
99
100 if (Obj is Complex z)
101 return new ComplexNumber(this.value + z);
102 else if (Obj is double d)
103 return new ComplexNumber(this.value + d);
104 else
105 return null;
106 }
107
112 public override IGroupElement Negate()
113 {
114 return new ComplexNumber(-this.value);
115 }
116
118 public override bool Equals(object obj)
119 {
120 if (!(obj is IElement E))
121 return false;
122
123 object Obj = E.AssociatedObjectValue;
124
125 if (Obj is Complex z)
126 return this.value == z;
127 else if (Obj is double d)
128 return this.value == d;
129 else
130 return false;
131 }
132
134 public override int GetHashCode()
135 {
136 return this.value.GetHashCode();
137 }
138
143 {
144 get { return ComplexNumbers.zero; }
145 }
146
151 {
152 get { return ComplexNumbers.one; }
153 }
154
161 public override bool TryConvertTo(Type DesiredType, out object Value)
162 {
163 if (DesiredType.GetTypeInfo().IsAssignableFrom(typeof(Complex).GetTypeInfo()))
164 {
165 Value = this.value;
166 return true;
167 }
168 else if (this.value.Imaginary == 0)
169 {
170 double d = this.value.Real;
171
172 if (DesiredType == typeof(byte))
173 {
174 if (d >= byte.MinValue && d <= byte.MaxValue)
175 {
176 Value = (byte)d;
177 return true;
178 }
179 }
180 else if (DesiredType == typeof(decimal))
181 {
182 Value = (decimal)d;
183 return true;
184 }
185 else if (DesiredType == typeof(double))
186 {
187 Value = (double)d;
188 return true;
189 }
190 else if (DesiredType == typeof(short))
191 {
192 if (d >= short.MinValue && d <= short.MaxValue)
193 {
194 Value = (short)d;
195 return true;
196 }
197 }
198 else if (DesiredType == typeof(int))
199 {
200 if (d >= int.MinValue && d <= int.MaxValue)
201 {
202 Value = (int)d;
203 return true;
204 }
205 }
206 else if (DesiredType == typeof(long))
207 {
208 if (d >= long.MinValue && d <= long.MaxValue)
209 {
210 Value = (long)d;
211 return true;
212 }
213 }
214 else if (DesiredType == typeof(sbyte))
215 {
216 if (d >= sbyte.MinValue && d <= sbyte.MaxValue)
217 {
218 Value = (sbyte)d;
219 return true;
220 }
221 }
222 else if (DesiredType == typeof(float))
223 {
224 Value = (float)d;
225 return true;
226 }
227 else if (DesiredType == typeof(ushort))
228 {
229 if (d >= ushort.MinValue && d <= ushort.MaxValue)
230 {
231 Value = (ushort)d;
232 return true;
233 }
234 }
235 else if (DesiredType == typeof(uint))
236 {
237 if (d >= uint.MinValue && d <= uint.MaxValue)
238 {
239 Value = (uint)d;
240 return true;
241 }
242 }
243 else if (DesiredType == typeof(ulong))
244 {
245 if (d >= ulong.MinValue && d <= ulong.MaxValue)
246 {
247 Value = (ulong)d;
248 return true;
249 }
250 }
251 else if (DesiredType.GetTypeInfo().IsAssignableFrom(typeof(Complex).GetTypeInfo()))
252 {
253 Value = this.value;
254 return true;
255 }
256 else if (DesiredType == typeof(ComplexNumber))
257 {
258 Value = this;
259 return true;
260 }
261 }
262
263 return Expression.TryConvert(this.value, DesiredType, out Value);
264 }
265 }
266}
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 field elements.
Definition: FieldElement.cs:10
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
override bool Equals(object obj)
Compares the element to another. If elements are equal.
override ICommutativeRingWithIdentityElement One
Returns the identity element of the commutative ring with identity.
override IAbelianGroupElement Add(IAbelianGroupElement Element)
Tries to add an element to the current element.
override IField AssociatedField
Associated Field.
override IGroupElement Negate()
Negates the element.
ComplexNumber(double Re, double Im)
Complex-valued number.
ComplexNumber(Complex Value)
Complex-valued number.
override object AssociatedObjectValue
Associated object value.
override IRingElement Invert()
Inverts the element, if possible.
override IAbelianGroupElement Zero
Returns the zero element of the group.
override int GetHashCode()
Calculates a hash code of the element. Hash code.
override bool TryConvertTo(Type DesiredType, out object Value)
Converts the value to a .NET type.
override ICommutativeRingElement Multiply(ICommutativeRingElement Element)
Tries to multiply an element to the current element.
Pseudo-field of Complex numbers, as an approximation of the field of real numbers.
Basic interface for all types of abelian group elements.
Basic interface for all types of commutative ring elements.
Basic interface for all types of commutative ring with identity elements.
Basic interface for all types of elements.
Definition: IElement.cs:20
Basic interface for all types of group elements.
Definition: IGroupElement.cs:9
Basic interface for all types of ring elements.
Definition: IRingElement.cs:10
Basic interface for all types of fields.
Definition: IField.cs:9