Neuron®
The Neuron® is the basis for the creation of open and secure federated networks for smart societies.
Loading...
Searching...
No Matches
Integer.cs
1using System;
2using System.Numerics;
3using System.Reflection;
6
8{
13 {
14 private static readonly Integers associatedEuclidianDomain = new Integers();
15
16 private BigInteger value;
17
22 public Integer(BigInteger Value)
23 {
24 this.value = Value;
25 }
26
31 public Integer(int Value)
32 {
33 this.value = Value;
34 }
35
40 public Integer(uint Value)
41 {
42 this.value = Value;
43 }
44
49 public Integer(long Value)
50 {
51 this.value = Value;
52 }
53
58 public Integer(ulong Value)
59 {
60 this.value = Value;
61 }
62
66 public BigInteger Value
67 {
68 get => this.value;
69 set => this.value = value;
70 }
71
73 public override string ToString()
74 {
75 return Expression.ToString(this.value);
76 }
77
82 {
83 get { return associatedEuclidianDomain; }
84 }
85
89 public override object AssociatedObjectValue => this.value;
90
97 {
98 object Obj = Element.AssociatedObjectValue;
99
100 if (Obj is BigInteger i)
101 return new Integer(this.value * i);
102
103 if (Obj is double d)
104 return new DoubleNumber((double)this.value * d);
105
106 if (Obj is Complex z)
107 return new ComplexNumber((double)this.value * z);
108
109 return null;
110 }
111
116 public override IRingElement Invert()
117 {
118 if (this.value.IsOne || this.value == BigInteger.MinusOne)
119 return new Integer(this.value);
120 else
121 return new RationalNumber(BigInteger.One, this.value);
122 }
123
130 {
131 object Obj = Element.AssociatedObjectValue;
132
133 if (Obj is BigInteger i)
134 return new Integer(this.value + i);
135
136 if (Obj is double d)
137 return new DoubleNumber((double)this.value + d);
138
139 if (Obj is Complex z)
140 return new ComplexNumber((double)this.value + z);
141
142 return null;
143 }
144
149 public override IGroupElement Negate()
150 {
151 return new Integer(-this.value);
152 }
153
155 public override bool Equals(object obj)
156 {
157 if (!(obj is IElement E))
158 return false;
159
160 object Obj = E.AssociatedObjectValue;
161
162 if (Obj is BigInteger i)
163 return this.value == i;
164
165 if (Obj is double d)
166 return (double)this.value == d;
167
168 if (Obj is Complex z)
169 return (double)this.value == z;
170
171 return false;
172 }
173
175 public override int GetHashCode()
176 {
177 return this.value.GetHashCode();
178 }
179
184 {
185 get { return Integers.zero; }
186 }
187
192 {
193 get { return Integers.one; }
194 }
195
202 public override bool TryConvertTo(Type DesiredType, out object Value)
203 {
204 if (DesiredType.GetTypeInfo().IsAssignableFrom(typeof(BigInteger).GetTypeInfo()))
205 {
206 Value = this.value;
207 return true;
208 }
209 else if (DesiredType == typeof(byte))
210 {
211 if (this.value >= byte.MinValue && this.value <= byte.MaxValue)
212 {
213 Value = (byte)this.value;
214 return true;
215 }
216 }
217 else if (DesiredType == typeof(decimal))
218 {
219 Value = (decimal)this.value;
220 return true;
221 }
222 else if (DesiredType == typeof(double))
223 {
224 Value = (double)this.value;
225 return true;
226 }
227 else if (DesiredType == typeof(short))
228 {
229 if (this.value >= short.MinValue && this.value <= short.MaxValue)
230 {
231 Value = (short)this.value;
232 return true;
233 }
234 }
235 else if (DesiredType == typeof(int))
236 {
237 if (this.value >= int.MinValue && this.value <= int.MaxValue)
238 {
239 Value = (int)this.value;
240 return true;
241 }
242 }
243 else if (DesiredType == typeof(long))
244 {
245 if (this.value >= long.MinValue && this.value <= long.MaxValue)
246 {
247 Value = (long)this.value;
248 return true;
249 }
250 }
251 else if (DesiredType == typeof(sbyte))
252 {
253 if (this.value >= sbyte.MinValue && this.value <= sbyte.MaxValue)
254 {
255 Value = (sbyte)this.value;
256 return true;
257 }
258 }
259 else if (DesiredType == typeof(float))
260 {
261 Value = (float)this.value;
262 return true;
263 }
264 else if (DesiredType == typeof(ushort))
265 {
266 if (this.value >= ushort.MinValue && this.value <= ushort.MaxValue)
267 {
268 Value = (ushort)this.value;
269 return true;
270 }
271 }
272 else if (DesiredType == typeof(uint))
273 {
274 if (this.value >= uint.MinValue && this.value <= uint.MaxValue)
275 {
276 Value = (uint)this.value;
277 return true;
278 }
279 }
280 else if (DesiredType == typeof(ulong))
281 {
282 if (this.value >= ulong.MinValue && this.value <= ulong.MaxValue)
283 {
284 Value = (ulong)this.value;
285 return true;
286 }
287 }
288 else if (DesiredType == typeof(Integer))
289 {
290 Value = this;
291 return true;
292 }
293
294 return Expression.TryConvert(this.value, DesiredType, out Value);
295 }
296
303 public int Compare(IElement x, IElement y)
304 {
305 return RationalNumbers.CompareNumbers(x, y);
306 }
307 }
308}
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 Euclidian domain elements.
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
Integer-valued number.
Definition: Integer.cs:13
Integer(uint Value)
BigInteger-valued number.
Definition: Integer.cs:40
override IEuclidianDomain AssociatedEuclidianDomain
Associated Euclidian Domain.
Definition: Integer.cs:82
override IAbelianGroupElement Add(IAbelianGroupElement Element)
Tries to add an element to the current element.
Definition: Integer.cs:129
override int GetHashCode()
Calculates a hash code of the element. Hash code.
Definition: Integer.cs:175
override IRingElement Invert()
Inverts the element, if possible.
Definition: Integer.cs:116
Integer(int Value)
BigInteger-valued number.
Definition: Integer.cs:31
override ICommutativeRingElement Multiply(ICommutativeRingElement Element)
Tries to multiply an element to the current element.
Definition: Integer.cs:96
BigInteger Value
BigInteger value.
Definition: Integer.cs:67
override bool Equals(object obj)
Compares the element to another. If elements are equal.
Definition: Integer.cs:155
override ICommutativeRingWithIdentityElement One
Returns the identity element of the commutative ring with identity.
Definition: Integer.cs:192
override object AssociatedObjectValue
Associated object value.
Definition: Integer.cs:89
override IAbelianGroupElement Zero
Returns the zero element of the group.
Definition: Integer.cs:184
Integer(ulong Value)
BigInteger-valued number.
Definition: Integer.cs:58
int Compare(IElement x, IElement y)
Compares two rational numbers.
Definition: Integer.cs:303
Integer(BigInteger Value)
Integer-valued number.
Definition: Integer.cs:22
override string ToString()
Definition: Integer.cs:73
Integer(long Value)
BigInteger-valued number.
Definition: Integer.cs:49
override bool TryConvertTo(Type DesiredType, out object Value)
Converts the value to a .NET type.
Definition: Integer.cs:202
override IGroupElement Negate()
Negates the element.
Definition: Integer.cs:149
Euclidian domain of integers.
Definition: Integers.cs:12
Field of rational numbers.
static int CompareNumbers(IElement x, IElement y)
Compares two rational 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 euclidian domains.
Basic interface for ordered sets.
Definition: IOrderedSet.cs:11