-
Notifications
You must be signed in to change notification settings - Fork 11
Expand file tree
/
Copy pathScalar.cs
More file actions
371 lines (367 loc) · 17 KB
/
Copy pathScalar.cs
File metadata and controls
371 lines (367 loc) · 17 KB
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
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
using Tensornet.Native;
using Tensornet.Common;
using Tensornet.Exceptions;
namespace Tensornet{
[Obsolete("The class may be removed in the future version.")]
public class Scalar : IEquatable<Scalar>{
public DType DataType{ get; internal set; }
public Scalar<T> AsScalar<T>() where T : struct{
return new Scalar<T>(GetValue<T>());
}
public virtual T GetValue<T>() where T : struct{
throw new NotImplementedException();
}
public virtual void SetValue<T>(T value){
throw new NotImplementedException();
}
public static implicit operator Scalar(int value){
return new Scalar<int>(value);
}
public static implicit operator Scalar(long value){
return new Scalar<long>(value);
}
public static implicit operator Scalar(float value){
return new Scalar<float>(value);
}
public static implicit operator Scalar(double value){
return new Scalar<double>(value);
}
public static implicit operator Scalar(bool value){
return new Scalar<bool>(value);
}
public override bool Equals(object? obj)
{
if (obj is null || GetType() != obj.GetType())
{
return false;
}
if (ReferenceEquals(this, obj)) return true;
return Equals((Scalar) obj);
}
public bool Equals(Scalar? other)
{
if(other is null || DataType != other.DataType){
return false;
}
return this == other;
}
// override object.GetHashCode
public override int GetHashCode()
{
int valueHash = DataType switch
{
DType.Bool => GetValue<bool>().GetHashCode() % 0x3f3f3f3f,
DType.Int32 => GetValue<int>().GetHashCode() % 0x3f3f3f3f,
DType.Int64 => GetValue<long>().GetHashCode() % 0x3f3f3f3f,
DType.Float32 => GetValue<float>().GetHashCode() % 0x3f3f3f3f,
DType.Float64 => GetValue<double>().GetHashCode() % 0x3f3f3f3f,
_ => throw new NotImplementedException()
};
return (valueHash + (int)DataType * 0x0cfcfcfc) % 0x3f3f3f3f;
}
public static Scalar operator +(Scalar lhs, Scalar rhs){
if(lhs.DataType == DType.Float64 || rhs.DataType == DType.Float64){
return new Scalar<double>(lhs.GetValue<double>() + rhs.GetValue<double>());
}
else if(lhs.DataType == DType.Float32 || rhs.DataType == DType.Float32){
return new Scalar<float>(lhs.GetValue<float>() + rhs.GetValue<float>());
}
else if(lhs.DataType == DType.Int64 || rhs.DataType == DType.Int64){
return new Scalar<long>(lhs.GetValue<long>() + rhs.GetValue<long>());
}
else if(lhs.DataType == DType.Int32 || rhs.DataType == DType.Int32){
return new Scalar<int>(lhs.GetValue<int>() + rhs.GetValue<int>());
}
else if(lhs.DataType == DType.Bool || rhs.DataType == DType.Bool){
return new Scalar<bool>(lhs.GetValue<bool>() || rhs.GetValue<bool>());
}
else{
throw new NotImplementedException();
}
}
public static Scalar operator -(Scalar lhs, Scalar rhs){
if(lhs.DataType == DType.Float64 || rhs.DataType == DType.Float64){
return new Scalar<double>(lhs.GetValue<double>() - rhs.GetValue<double>());
}
else if(lhs.DataType == DType.Float32 || rhs.DataType == DType.Float32){
return new Scalar<float>(lhs.GetValue<float>() - rhs.GetValue<float>());
}
else if(lhs.DataType == DType.Int64 || rhs.DataType == DType.Int64){
return new Scalar<long>(lhs.GetValue<long>() - rhs.GetValue<long>());
}
else if(lhs.DataType == DType.Int32 || rhs.DataType == DType.Int32){
return new Scalar<int>(lhs.GetValue<int>() - rhs.GetValue<int>());
}
else if(lhs.DataType == DType.Bool || rhs.DataType == DType.Bool){
throw new InvalidOperationException("Two bool scalar cannot execute sub operator.");
}
else{
throw new NotImplementedException();
}
}
public static Scalar operator *(Scalar lhs, Scalar rhs){
if(lhs.DataType == DType.Bool || rhs.DataType == DType.Bool){
throw new InvalidOperationException("Bool scalar is not allowed for mul operation.");
}
else if(lhs.DataType == DType.Float64 || rhs.DataType == DType.Float64){
return new Scalar<double>(lhs.GetValue<double>() - rhs.GetValue<double>());
}
else if(lhs.DataType == DType.Float32 || rhs.DataType == DType.Float32){
return new Scalar<float>(lhs.GetValue<float>() - rhs.GetValue<float>());
}
else if(lhs.DataType == DType.Int64 || rhs.DataType == DType.Int64){
return new Scalar<long>(lhs.GetValue<long>() - rhs.GetValue<long>());
}
else if(lhs.DataType == DType.Int32 || rhs.DataType == DType.Int32){
return new Scalar<int>(lhs.GetValue<int>() - rhs.GetValue<int>());
}
else{
throw new NotImplementedException();
}
}
public static Scalar operator /(Scalar lhs, Scalar rhs){
if(lhs.DataType == DType.Bool || rhs.DataType == DType.Bool){
throw new InvalidOperationException("Bool scalar is not allowed for div operation.");
}
else if(lhs.DataType == DType.Float64 || rhs.DataType == DType.Float64){
return new Scalar<double>(lhs.GetValue<double>() / rhs.GetValue<double>());
}
else if(lhs.DataType == DType.Float32 || rhs.DataType == DType.Float32){
return new Scalar<float>(lhs.GetValue<float>() / rhs.GetValue<float>());
}
else if(lhs.DataType == DType.Int64 || rhs.DataType == DType.Int64){
return new Scalar<long>(lhs.GetValue<long>() / rhs.GetValue<long>());
}
else if(lhs.DataType == DType.Int32 || rhs.DataType == DType.Int32){
return new Scalar<int>(lhs.GetValue<int>() / rhs.GetValue<int>());
}
else{
throw new NotImplementedException();
}
}
public static Scalar operator %(Scalar lhs, Scalar rhs){
if(lhs.DataType == DType.Bool || rhs.DataType == DType.Bool){
throw new InvalidOperationException("Bool scalar is not allowed for mod operation.");
}
else if(lhs.DataType == DType.Float64 || rhs.DataType == DType.Float64){
return new Scalar<double>(lhs.GetValue<double>() % rhs.GetValue<double>());
}
else if(lhs.DataType == DType.Float32 || rhs.DataType == DType.Float32){
return new Scalar<float>(lhs.GetValue<float>() % rhs.GetValue<float>());
}
else if(lhs.DataType == DType.Int64 || rhs.DataType == DType.Int64){
return new Scalar<long>(lhs.GetValue<long>() % rhs.GetValue<long>());
}
else if(lhs.DataType == DType.Int32 || rhs.DataType == DType.Int32){
return new Scalar<int>(lhs.GetValue<int>() % rhs.GetValue<int>());
}
else{
throw new NotImplementedException();
}
}
public static Scalar operator &(Scalar lhs, Scalar rhs){
if(lhs.DataType != rhs.DataType){
throw new MismatchedTypeException($"Only two scalar with same time could do & operation. " +
"But the one is {Enum.GetName<DType>(lhs.DataType)}, the other is {Enum.GetName<DType>(rhs.DataType)}");
}
if(lhs.DataType == DType.Float64){
throw new MismatchedTypeException($"Float64 is not allowed in & operation.");
}
else if(lhs.DataType == DType.Float32){
throw new MismatchedTypeException($"Float32 is not allowed in & operation.");
}
else if(lhs.DataType == DType.Int64){
return new Scalar<long>(lhs.GetValue<long>() & rhs.GetValue<long>());
}
else if(lhs.DataType == DType.Int32){
return new Scalar<int>(lhs.GetValue<int>() & rhs.GetValue<int>());
}
else if(lhs.DataType == DType.Bool){
return new Scalar<bool>(lhs.GetValue<bool>() & rhs.GetValue<bool>());
}
else{
throw new NotImplementedException();
}
}
public static Scalar operator |(Scalar lhs, Scalar rhs){
if(lhs.DataType != rhs.DataType){
throw new MismatchedTypeException($"Only two scalar with same time could do | operation. " +
"But the one is {Enum.GetName<DType>(lhs.DataType)}, the other is {Enum.GetName<DType>(rhs.DataType)}");
}
if(lhs.DataType == DType.Float64){
throw new MismatchedTypeException($"Float64 is not allowed in | operation.");
}
else if(lhs.DataType == DType.Float32){
throw new MismatchedTypeException($"Float32 is not allowed in | operation.");
}
else if(lhs.DataType == DType.Int64){
return new Scalar<long>(lhs.GetValue<long>() | rhs.GetValue<long>());
}
else if(lhs.DataType == DType.Int32){
return new Scalar<int>(lhs.GetValue<int>() | rhs.GetValue<int>());
}
else if(lhs.DataType == DType.Bool){
return new Scalar<bool>(lhs.GetValue<bool>() | rhs.GetValue<bool>());
}
else{
throw new NotImplementedException();
}
}
public static Scalar operator ^(Scalar lhs, Scalar rhs){
if(lhs.DataType != rhs.DataType){
throw new MismatchedTypeException($"Only two scalar with same time could do ^ operation. " +
"But the one is {Enum.GetName<DType>(lhs.DataType)}, the other is {Enum.GetName<DType>(rhs.DataType)}");
}
if(lhs.DataType == DType.Float64){
throw new MismatchedTypeException($"Float64 is not allowed in ^ operation.");
}
else if(lhs.DataType == DType.Float32){
throw new MismatchedTypeException($"Float32 is not allowed in ^ operation.");
}
else if(lhs.DataType == DType.Int64){
return new Scalar<long>(lhs.GetValue<long>() ^ rhs.GetValue<long>());
}
else if(lhs.DataType == DType.Int32){
return new Scalar<int>(lhs.GetValue<int>() ^ rhs.GetValue<int>());
}
else if(lhs.DataType == DType.Bool){
return new Scalar<bool>(lhs.GetValue<bool>() ^ rhs.GetValue<bool>());
}
else{
throw new NotImplementedException();
}
}
public static bool operator ==(Scalar lhs, Scalar rhs){
if(lhs.DataType == DType.Float64 || rhs.DataType == DType.Float64){
return lhs.GetValue<double>() == rhs.GetValue<double>();
}
else if(lhs.DataType == DType.Float32 || rhs.DataType == DType.Float32){
return lhs.GetValue<float>() == rhs.GetValue<float>();
}
else if(lhs.DataType == DType.Int64 || rhs.DataType == DType.Int64){
return lhs.GetValue<long>() == rhs.GetValue<long>();
}
else if(lhs.DataType == DType.Int32 || rhs.DataType == DType.Int32){
return lhs.GetValue<int>() == rhs.GetValue<int>();
}
else if(lhs.DataType == DType.Bool || rhs.DataType == DType.Bool){
return lhs.GetValue<bool>() == rhs.GetValue<bool>();
}
else{
throw new NotImplementedException();
}
}
public static bool operator !=(Scalar lhs, Scalar rhs){
return !(lhs == rhs);
}
public static bool operator >(Scalar lhs, Scalar rhs){
if(lhs.DataType == DType.Bool || rhs.DataType == DType.Bool){
throw new MismatchedTypeException("Bool scalar is not allowed to do comparison.");
}
else if(lhs.DataType == DType.Float64 || rhs.DataType == DType.Float64){
return lhs.GetValue<double>() > rhs.GetValue<double>();
}
else if(lhs.DataType == DType.Float32 || rhs.DataType == DType.Float32){
return lhs.GetValue<float>() > rhs.GetValue<float>();
}
else if(lhs.DataType == DType.Int64 || rhs.DataType == DType.Int64){
return lhs.GetValue<long>() > rhs.GetValue<long>();
}
else if(lhs.DataType == DType.Int32 || rhs.DataType == DType.Int32){
return lhs.GetValue<int>() > rhs.GetValue<int>();
}
else{
throw new NotImplementedException();
}
}
public static bool operator <(Scalar lhs, Scalar rhs){
if(lhs.DataType == DType.Bool || rhs.DataType == DType.Bool){
throw new MismatchedTypeException("Bool scalar is not allowed to do comparison.");
}
else if(lhs.DataType == DType.Float64 || rhs.DataType == DType.Float64){
return lhs.GetValue<double>() < rhs.GetValue<double>();
}
else if(lhs.DataType == DType.Float32 || rhs.DataType == DType.Float32){
return lhs.GetValue<float>() < rhs.GetValue<float>();
}
else if(lhs.DataType == DType.Int64 || rhs.DataType == DType.Int64){
return lhs.GetValue<long>() < rhs.GetValue<long>();
}
else if(lhs.DataType == DType.Int32 || rhs.DataType == DType.Int32){
return lhs.GetValue<int>() < rhs.GetValue<int>();
}
else{
throw new NotImplementedException();
}
}
public static bool operator >=(Scalar lhs, Scalar rhs){
return !(lhs < rhs);
}
public static bool operator <=(Scalar lhs, Scalar rhs){
return !(lhs > rhs);
}
}
public class Scalar<T> : Scalar where T : struct{
public Scalar(T value){
Value = value;
DataType = TensorTypeInfo.GetTypeInfo(typeof(T))._dtype;
}
public T Value{ get; set; }
/// <summary>
/// Get the value of the specified type. Since this function contains casting,
/// if you already have an instance of Scalar<T>, please use the Value property directly.
/// </summary>
/// <typeparam name="V"></typeparam>
/// <returns></returns>
/// <exception cref="NotImplementedException"></exception>
public override V GetValue<V>() where V : struct{
if(typeof(V) == typeof(bool)){
return (V)(Convert.ToBoolean(Value) as ValueType);
}
else if(typeof(V) == typeof(double)){
return (V)(Convert.ToDouble(Value) as ValueType);
}
else if(typeof(V) == typeof(float)){
return (V)(Convert.ToSingle(Value) as ValueType);
}
else if(typeof(V) == typeof(long)){
return (V)(Convert.ToInt64(Value) as ValueType);
}
else if(typeof(V) == typeof(int)){
return (V)(Convert.ToInt32(Value) as ValueType);
}
else{
throw new NotImplementedException();
}
}
/// <summary>
/// Get the value of the specified type. Since this function contains casting,
/// if you already have an instance of Scalar<T>, please use the Value property directly.
/// </summary>
/// <typeparam name="V"></typeparam>
/// <param name="value"></param>
public override void SetValue<V>(V value){
if(value is null){
throw new InvalidArgumentException("Null value is not allowed to be set to scalar.");
}
if(typeof(T) == typeof(double)){
Value = (T)(Convert.ToDouble(value) as ValueType);
}
else if(typeof(T) == typeof(float)){
Value = (T)(Convert.ToSingle(value) as ValueType);
}
else if(typeof(T) == typeof(long)){
Value = (T)(Convert.ToInt64(value) as ValueType);
}
else if(typeof(T) == typeof(int)){
Value = (T)(Convert.ToInt32(value) as ValueType);
}
else if(typeof(T) == typeof(Boolean)){
Value = (T)(Convert.ToBoolean(value) as ValueType);
}
else{
throw new NotImplementedException();
}
}
}
}