Neuron®
The Neuron® is the basis for the creation of open and secure federated networks for smart societies.
Loading...
Searching...
No Matches
RationalNumbers.cs
1using System;
2using System.Numerics;
5
7{
11 public sealed class RationalNumbers : Field, IOrderedSet
12 {
13 internal static readonly RationalNumber zero = new RationalNumber(BigInteger.Zero);
14 internal static readonly RationalNumber one = new RationalNumber(BigInteger.One);
15 private static readonly int hashCode = typeof(RationalNumbers).FullName.GetHashCode();
16
21 {
22 }
23
28 {
29 get { return one; }
30 }
31
35 public override IAbelianGroupElement Zero
36 {
37 get { return zero; }
38 }
39
45 public override bool Contains(IElement Element)
46 {
48 return true;
49
50 object Obj = Element.AssociatedObjectValue;
51 return Obj is BigInteger || Obj is double;
52 }
53
55 public override bool Equals(object obj)
56 {
57 return obj is RationalNumbers;
58 }
59
61 public override int GetHashCode()
62 {
63 return hashCode;
64 }
65
69 public static readonly RationalNumbers Instance = new RationalNumbers();
70
72 public override string ToString()
73 {
74 return "ℚ";
75 }
76
83 public static int CompareNumbers(IElement x, IElement y)
84 {
85 BigInteger n1, d1;
86 BigInteger n2, d2;
87
88 if (x is RationalNumber q1)
89 {
90 n1 = q1.Numerator;
91 d1 = q1.Denominator;
92 }
93 else if (x.AssociatedObjectValue is BigInteger i1)
94 {
95 n1 = i1;
96 d1 = BigInteger.One;
97 }
98 else
99 {
100 n1 = BigInteger.Zero;
101 d1 = BigInteger.One;
102 }
103
104 if (y is RationalNumber q2)
105 {
106 n2 = q2.Numerator;
107 d2 = q2.Denominator;
108 }
109 else if (y.AssociatedObjectValue is BigInteger i2)
110 {
111 n2 = i2;
112 d2 = BigInteger.One;
113 }
114 else
115 {
116 n2 = BigInteger.Zero;
117 d2 = BigInteger.One;
118 }
119
120 BigInteger l = n1 * d2;
121 BigInteger r = n2 * d1;
122
123 return l.CompareTo(r);
124 }
125
132 public int Compare(IElement x, IElement y)
133 {
134 return CompareNumbers(x, y);
135 }
136 }
137}
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 fields.
Definition: Field.cs:10
Field of rational numbers.
override IAbelianGroupElement Zero
Returns the zero element of the group.
int Compare(IElement x, IElement y)
Compares two rational numbers.
static readonly RationalNumbers Instance
Instance of the set of integers.
static int CompareNumbers(IElement x, IElement y)
Compares two rational numbers.
override bool Equals(object obj)
Compares the element to another. If elements are equal.
RationalNumbers()
Field of rational numbers.
override bool Contains(IElement Element)
Checks if the set contains an element.
override int GetHashCode()
Calculates a hash code of the element. Hash code.
override ICommutativeRingWithIdentityElement One
Returns the identity element of the commutative ring with identity.
Basic interface for all types of abelian group elements.
Basic interface for all types of commutative ring with identity elements.
Basic interface for all types of elements.
Definition: IElement.cs:20
object AssociatedObjectValue
Associated object value.
Definition: IElement.cs:33
Basic interface for ordered sets.
Definition: IOrderedSet.cs:11