-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathLinearType.cs
186 lines (151 loc) · 7.92 KB
/
LinearType.cs
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
using System;
using System.Collections.Generic;
using System.Numerics;
namespace Lattice.Math
{
public interface ILinearType<T>
{
T Zero { get; }
T One { get; }
T MinusOne { get; }
bool IsZero(T value);
int Sign(T value);
int Compare(T left, T right);
T Min(T left, T right);
T Max(T left, T right);
T AbsoluteValue(T value);
T Negate(T value);
T Add(T left, T right);
T Subtract(T left, T right);
T Multiply(T left, T right);
T Divide(T left, T right);
}
public static class LinearType<T>
{
private static ILinearType<T> _type = null;
public static ILinearType<T> Type { get => _type = _type ?? LinearTypes.GetLinearType<T>(); }
public static T Zero { get => Type.Zero; }
public static T One { get => Type.One; }
public static T MinusOne { get => Type.MinusOne; }
public static bool IsZero(T value) => Type.IsZero(value);
public static int Sign(T value) => Type.Sign(value);
public static int Compare(T left, T right) => Type.Compare(left, right);
public static T Min(T left, T right) => Type.Min(left, right);
public static T Max(T left, T right) => Type.Max(left, right);
public static T AbsoluteValue(T value) => Type.AbsoluteValue(value);
public static T Negate(T value) => Type.Negate(value);
public static T Add(T left, T right) => Type.Add(left, right);
public static T Subtract(T left, T right) => Type.Subtract(left, right);
public static T Multiply(T left, T right) => Type.Multiply(left, right);
public static T Divide(T left, T right) => Type.Divide(left, right);
}
public static class LinearTypes
{
private static readonly Dictionary<Type, dynamic> types = new Dictionary<Type, dynamic>();
static LinearTypes()
{
RegisterLinearType(new SingleLinearType());
RegisterLinearType(new DoubleLinearType());
RegisterLinearType(new DecimalLinearType());
RegisterLinearType(new BigRationalLinearType());
}
public static ILinearType<T> GetLinearType<T>()
{
dynamic type;
if (!types.TryGetValue(typeof(T), out type))
{
throw new ArgumentException($"No registered LinearType for type {typeof(T).FullName}");
}
return (ILinearType<T>) type;
}
public static void RegisterLinearType<T>(ILinearType<T> type)
{
types[typeof(T)] = type;
}
}
internal class SingleLinearType : ILinearType<Single>
{
public Single Zero { get => 0.0f; }
public Single One { get => 1.0f; }
public Single MinusOne { get => -1.0f; }
public bool IsZero(Single value) => value == 0.0f;
public int Sign(Single value) => System.Math.Sign(value);
public int Compare(Single left, Single right) => System.Math.Sign(left - right);
public Single Min(Single left, Single right) => System.Math.Min(left, right);
public Single Max(Single left, Single right) => System.Math.Max(left, right);
public Single AbsoluteValue(Single value) => System.Math.Abs(value);
public Single Negate(Single value) => -value;
public Single Add(Single left, Single right) => left + right;
public Single Subtract(Single left, Single right) => left - right;
public Single Multiply(Single left, Single right) => left * right;
public Single Divide(Single left, Single right) => left / right;
}
internal class DoubleLinearType : ILinearType<Double>
{
public Double Zero { get => 0.0; }
public Double One { get => 1.0; }
public Double MinusOne { get => -1.0; }
public bool IsZero(Double value) => value == 0.0;
public int Sign(Double value) => System.Math.Sign(value);
public int Compare(Double left, Double right) => System.Math.Sign(left - right);
public Double Min(Double left, Double right) => System.Math.Min(left, right);
public Double Max(Double left, Double right) => System.Math.Max(left, right);
public Double AbsoluteValue(Double value) => System.Math.Abs(value);
public Double Negate(Double value) => -value;
public Double Add(Double left, Double right) => left + right;
public Double Subtract(Double left, Double right) => left - right;
public Double Multiply(Double left, Double right) => left * right;
public Double Divide(Double left, Double right) => left / right;
}
internal class DecimalLinearType : ILinearType<Decimal>
{
public Decimal Zero { get => 0.0m; }
public Decimal One { get => 1.0m; }
public Decimal MinusOne { get => -1.0m; }
public bool IsZero(Decimal value) => value == 0.0m;
public int Sign(Decimal value) => System.Math.Sign(value);
public int Compare(Decimal left, Decimal right) => System.Math.Sign(left - right);
public Decimal Min(Decimal left, Decimal right) => System.Math.Min(left, right);
public Decimal Max(Decimal left, Decimal right) => System.Math.Max(left, right);
public Decimal AbsoluteValue(Decimal value) => System.Math.Abs(value);
public Decimal Negate(Decimal value) => -value;
public Decimal Add(Decimal left, Decimal right) => left + right;
public Decimal Subtract(Decimal left, Decimal right) => left - right;
public Decimal Multiply(Decimal left, Decimal right) => left * right;
public Decimal Divide(Decimal left, Decimal right) => left / right;
}
internal class BigRationalLinearType : ILinearType<BigRational>
{
public BigRational Zero { get => BigRational.Zero; }
public BigRational One { get => BigRational.One; }
public BigRational MinusOne { get => BigRational.MinusOne; }
public bool IsZero(BigRational value) => value.IsZero;
public int Sign(BigRational value) => value.Sign;
public int Compare(BigRational left, BigRational right) => BigRational.Compare(left, right);
public BigRational AbsoluteValue(BigRational value) => BigRational.AbsoluteValue(value);
public BigRational Min(BigRational left, BigRational right) => BigRational.Min(left, right);
public BigRational Max(BigRational left, BigRational right) => BigRational.Max(left, right);
public BigRational Negate(BigRational value) => -value;
public BigRational Add(BigRational left, BigRational right) => left + right;
public BigRational Subtract(BigRational left, BigRational right) => left - right;
public BigRational Multiply(BigRational left, BigRational right) => left * right;
public BigRational Divide(BigRational left, BigRational right) => left / right;
}
internal class BigIntegerRationalType : ILinearType<BigInteger>
{
public BigInteger Zero { get => BigInteger.Zero; }
public BigInteger One { get => BigInteger.One; }
public BigInteger MinusOne { get => BigInteger.MinusOne; }
public bool IsZero(BigInteger value) => value.IsZero;
public int Sign(BigInteger value) => value.Sign;
public int Compare(BigInteger left, BigInteger right) => BigInteger.Compare(left, right);
public BigInteger AbsoluteValue(BigInteger value) => BigInteger.Abs(value);
public BigInteger Min(BigInteger left, BigInteger right) => BigInteger.Min(left, right);
public BigInteger Max(BigInteger left, BigInteger right) => BigInteger.Max(left, right);
public BigInteger Negate(BigInteger value) => -value;
public BigInteger Add(BigInteger left, BigInteger right) => left + right;
public BigInteger Subtract(BigInteger left, BigInteger right) => left - right;
public BigInteger Multiply(BigInteger left, BigInteger right) => left * right;
public BigInteger Divide(BigInteger left, BigInteger right) => left / right;
}
}