forked from SciSharp/NumSharp
-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathNDStorage.cs
More file actions
511 lines (473 loc) · 18.5 KB
/
NDStorage.cs
File metadata and controls
511 lines (473 loc) · 18.5 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
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
using NumSharp;
using NumSharp.Core.Interfaces;
using NumSharp.Core;
using System;
using System.Linq;
using System.Runtime.InteropServices;
namespace NumSharp.Core
{
/// <summary>
/// Storage
///
/// Responsible for :
///
/// - store data type, elements, Shape
/// - offers methods for accessing elements depending on shape
/// - offers methods for casting elements
/// - offers methods for change tensor order
/// - GetData always return reference object to the true storage
/// - GetData<T> and SetData<T> change dtype and cast storage
/// - CloneData always create a clone of storage and return this as reference object
/// - CloneData<T> clone storage and cast this clone
///
/// </summary>
public class NDStorage : IStorage
{
protected Array _values;
protected Type _DType;
protected Shape _Shape;
protected int _TensorLayout;
/*protected void _ChangeRowToColumnLayout()
{
if ( _Shape.NDim == 1 )
{
}
else if (_Shape.NDim == 2)
{
var puffer = Array.CreateInstance(_values.GetType().GetElementType(),_values.Length);
var pufferShape = new Shape(_Shape.Dimensions);
pufferShape.ChangeTensorLayout(2);
for(int idx = 0; idx < _values.Length;idx++)
puffer.SetValue(_values.GetValue(idx),pufferShape.GetIndexInShape(Shape.GetDimIndexOutShape(idx)));
_values = puffer;
}
else
{
var puffer = Array.CreateInstance(_values.GetType().GetElementType(),_values.Length);
var pufferShape = new Shape(_Shape.Dimensions);
pufferShape.ChangeTensorLayout(2);
for(int idx = 0; idx < _values.Length;idx++)
puffer.SetValue(_values.GetValue(idx),pufferShape.GetIndexInShape(Shape.GetDimIndexOutShape(idx)));
_values = puffer;
}
Shape.ChangeTensorLayout(2);
_TensorLayout = 2;
}
protected void _ChangeColumnToRowLayout()
{
if ( _Shape.NDim == 1 )
{
}
else if (_Shape.NDim == 2)
{
var puffer = Array.CreateInstance(_values.GetType().GetElementType(),_values.Length);
var pufferShape = new Shape(_Shape.Dimensions);
pufferShape.ChangeTensorLayout(1);
for(int idx = 0; idx < _values.Length;idx++)
puffer.SetValue(_values.GetValue(idx),pufferShape.GetIndexInShape(Shape.GetDimIndexOutShape(idx)));
_values = puffer;
}
else
{
var puffer = Array.CreateInstance(_values.GetType().GetElementType(),_values.Length);
var pufferShape = new Shape(_Shape.Dimensions);
pufferShape.ChangeTensorLayout(1);
for(int idx = 0; idx < _values.Length;idx++)
puffer.SetValue(_values.GetValue(idx),pufferShape.GetIndexInShape(Shape.GetDimIndexOutShape(idx)));
_values = puffer;
}
_TensorLayout = 1;
Shape.ChangeTensorLayout(1);
}*/
protected Array _ChangeTypeOfArray(Array arrayVar, Type dtype)
{
Array newValues = null;
switch (Type.GetTypeCode(dtype))
{
case TypeCode.Double :
{
newValues = new double[arrayVar.Length];
for(int idx = 0;idx < arrayVar.Length;idx++)
newValues.SetValue(Convert.ToDouble(arrayVar.GetValue(idx)),idx);
break;
}
case TypeCode.Single :
{
newValues = new float[arrayVar.Length];
for(int idx = 0;idx < arrayVar.Length;idx++)
newValues.SetValue(Convert.ToSingle(arrayVar.GetValue(idx)),idx);
break;
}
case TypeCode.Decimal :
{
newValues = new Decimal[arrayVar.Length];
for(int idx = 0;idx < arrayVar.Length;idx++)
newValues.SetValue(Convert.ToDecimal(arrayVar.GetValue(idx)),idx);
break;
}
case TypeCode.Int32 :
{
newValues = new int[arrayVar.Length];
for(int idx = 0;idx < arrayVar.Length;idx++)
newValues.SetValue(Convert.ToInt32(arrayVar.GetValue(idx)),idx);
break;
}
case TypeCode.Int64 :
{
newValues = new Int64[arrayVar.Length];
for(int idx = 0;idx < arrayVar.Length;idx++)
newValues.SetValue(Convert.ToInt64(arrayVar.GetValue(idx)),idx);
break;
}
case TypeCode.Object :
{
if( dtype == typeof(System.Numerics.Complex) )
{
newValues = new System.Numerics.Complex[arrayVar.Length];
for(int idx = 0;idx < arrayVar.Length;idx++)
newValues.SetValue(new System.Numerics.Complex((double)arrayVar.GetValue(idx),0),idx);
break;
}
/*else if ( dtype == typeof(System.Numerics.Quaternion) )
{
newValues = new System.Numerics.Quaternion[arrayVar.Length];
for(int idx = 0;idx < arrayVar.Length;idx++)
newValues.SetValue(new System.Numerics.Quaternion(new System.Numerics.Vector3(0,0,0) , (float)arrayVar.GetValue(idx)),idx);
break;
}*/
else
{
newValues = new object[arrayVar.Length];
for(int idx = 0;idx < arrayVar.Length;idx++)
newValues.SetValue(arrayVar.GetValue(idx),idx);
break;
}
}
default :
{
break;
}
}
return newValues;
}
/// <summary>
/// Data Type of stored elements
/// </summary>
/// <value>numpys equal dtype</value>
public Type DType {get {return _DType;}}
public int DTypeSize
{
get
{
if(_DType == typeof(string))
{
return 0;
}
else
{
return Marshal.SizeOf(_DType);
}
}
}
/// <summary>
/// storage shape for outside representation
/// </summary>
/// <value>numpys equal shape</value>
public Shape Shape {get {return _Shape;}}
/// <summary>
/// column wise or row wise order
/// </summary>
/// <value>0 row wise, 1 column wise</value>
public int TensorLayout {get {return _TensorLayout;}}
public NDStorage()
{
_DType = np.float64;
_values = new double[1];
_Shape = new Shape(1);
//_TensorLayout = 1;
}
public NDStorage(Type dtype)
{
_DType = dtype;
_values = Array.CreateInstance(dtype,1);
_Shape = new Shape(1);
//_TensorLayout = 1;
}
public NDStorage(double[] values)
{
_DType = typeof(double);
_Shape = new Shape(values.Length);
_values = values;
//_TensorLayout = 1;
}
public NDStorage(object[] values)
{
_DType = values.GetType().GetElementType();
_Shape = new Shape(values.Length);
_values = values;
//_TensorLayout = 1;
}
/// <summary>
/// Allocate memory by dtype, shape, tensororder (default column wise)
/// </summary>
/// <param name="dtype">storage data type</param>
/// <param name="shape">storage data shape</param>
/// <param name="tensorOrder">row or column wise</param>
public void Allocate(Type dtype, Shape shape, int tensorOrder = 1)
{
_DType = dtype;
_Shape = shape;
_Shape.ChangeTensorLayout(tensorOrder);
int elementNumber = 1;
for(int idx = 0; idx < shape.Dimensions.Length;idx++)
elementNumber *= shape.Dimensions[idx];
_values = Array.CreateInstance(dtype,elementNumber);
//_TensorLayout = tensorOrder;
}
/// <summary>
/// Allocate memory by Array and tensororder and deduce shape and dtype (default column wise)
/// </summary>
/// <param name="values">elements to store</param>
/// <param name="tensorOrder">row or column wise</param>
public void Allocate(Array values, int tensorOrder = 1)
{
//_TensorLayout = tensorOrder;
int[] dim = new int[values.Rank];
for (int idx = 0; idx < dim.Length;idx++)
dim[idx] = values.GetLength(idx);
_Shape = new Shape(dim);
Type elementType = values.GetType();
while (elementType.IsArray)
elementType = elementType.GetElementType();
_DType = elementType;
}
/// <summary>
/// Get Back Storage with Columnwise tensor Layout
/// By this method the layout is changed if layout is not columnwise
/// </summary>
/// <returns>reference to storage (transformed or not)</returns>
public IStorage GetColumWiseStorage()
{
//if ( _TensorLayout != 2 )
//this._ChangeRowToColumnLayout();
return this;
}
/// <summary>
/// Get Back Storage with row wise tensor Layout
/// By this method the layout is changed if layout is not row wise
/// </summary>
/// <returns>reference to storage (transformed or not)</returns>
public IStorage GetRowWiseStorage()
{
//if ( _TensorLayout != 1 )
//this._ChangeColumnToRowLayout();
return this;
}
/// <summary>
/// Get reference to internal data storage
/// </summary>
/// <returns>reference to internal storage as System.Array</returns>
public Array GetData()
{
return _values;
}
/// <summary>
/// Clone internal storage and get reference to it
/// </summary>
/// <returns>reference to cloned storage as System.Array</returns>
public Array CloneData()
{
return (Array) _values.Clone();
}
/// <summary>
/// Get reference to internal data storage and cast elements to new dtype
/// </summary>
/// <param name="dtype">new storage data type</param>
/// <returns>reference to internal (casted) storage as System.Array </returns>
public Array GetData(Type dtype)
{
var methods = this.GetType().GetMethods().Where(x => x.Name.Equals("GetData") && x.IsGenericMethod && x.ReturnType.Name.Equals("T[]"));
var genMethods = methods.First().MakeGenericMethod(dtype);
return (Array) genMethods.Invoke(this,null);
}
/// <summary>
/// Clone internal storage and cast elements to new dtype
/// </summary>
/// <param name="dtype">cloned storage data type</param>
/// <returns>reference to cloned storage as System.Array</returns>
public Array CloneData(Type dtype)
{
var puffer = (Array) this.GetData().Clone();
if (puffer.GetType().GetElementType() != dtype)
puffer = _ChangeTypeOfArray(puffer,dtype);
return puffer;
}
/// <summary>
/// Get reference to internal data storage and cast elements to new dtype
/// </summary>
/// <typeparam name="T">new storage data type</typeparam>
/// <returns>reference to internal (casted) storage as T[]</returns>
public T[] GetData<T>()
{
if (typeof(T) != this._DType)
this.ChangeDataType(typeof(T));
return _values as T[];
}
/// <summary>
/// Get all elements from cloned storage as T[] and cast dtype
/// </summary>
/// <typeparam name="T">cloned storgae dtype</typeparam>
/// <returns>reference to cloned storage as T[]</returns>
public T[] CloneData<T>()
{
var puffer = (Array) this.GetData().Clone();
if (puffer.GetType().GetElementType() != typeof(T))
puffer = _ChangeTypeOfArray(puffer,typeof(T));
return puffer as T[];
}
/// <summary>
/// Get single value from internal storage and do not cast dtype
/// </summary>
/// <param name="indexes">indexes</param>
/// <returns>element from internal storage</returns>
public object GetData(params int[] indexes)
{
object element = null;
if (indexes.Length == Shape.NDim)
element = _values.GetValue(Shape.GetIndexInShape(indexes));
else if (Shape.Dimensions.Last() == 1)
element = _values.GetValue(Shape.GetIndexInShape(indexes));
else if (indexes.Length == Shape.NDim - 1)
{
var offset = new int[Shape.NDim];
for (int i = 0; i < Shape.NDim - 1; i++)
offset[i] = indexes[i];
NDArray nd = new NDArray(DType, Shape.Dimensions[Shape.NDim - 1]);
for (int i = 0; i < Shape.Dimensions[Shape.NDim - 1]; i++)
{
offset[offset.Length - 1] = i;
nd[i] = _values.GetValue(Shape.GetIndexInShape(offset));
}
return nd;
}
// 3 Dim
else if (indexes.Length == Shape.NDim - 2)
{
var offset = new int[Shape.NDim];
NDArray nd = new NDArray(DType, new int[]{ Shape.Dimensions[Shape.NDim - 2] , Shape.Dimensions[Shape.NDim - 1] });
for (int i = 0; i < Shape.Dimensions[Shape.NDim - 2]; i++)
{
for (int j = 0; j < Shape.Dimensions[Shape.NDim - 1]; j++)
{
offset[0] = 0;
offset[1] = i;
offset[2] = j;
nd[i,j] = _values.GetValue(Shape.GetIndexInShape(offset));
}
}
return nd;
}
else
throw new Exception("indexes must be equal to number of dimension.");
return element;
}
/// <summary>
/// Get single value from internal storage as type T and cast dtype to T
/// </summary>
/// <param name="indexes">indexes</param>
/// <typeparam name="T">new storage data type</typeparam>
/// <returns>element from internal storage</returns>
public T GetData<T>(params int[] indexes)
{
T[] values = this.GetData() as T[];
return values[Shape.GetIndexInShape(indexes)];
}
/// <summary>
/// Set an array to internal storage and keep dtype
/// </summary>
/// <param name="values"></param>
public void SetData(Array values)
{
_values = values;
this.ChangeDataType(this._DType);
}
/// <summary>
/// Set 1 single value to internal storage and keep dtype
/// </summary>
/// <param name="value"></param>
/// <param name="indexes"></param>
public void SetData(object value, params int[] indexes)
{
_values.SetValue(value,_Shape.GetIndexInShape(indexes));
}
/// <summary>
/// Set a 1D Array of type T to internal storage and cast dtype
/// </summary>
/// <param name="values"></param>
/// <typeparam name="T"></typeparam>
public void SetData<T>(Array values)
{
_values = values;
this.ChangeDataType(typeof(T));
}
/// <summary>
/// Set an Array to internal storage, cast it to new dtype and change dtype
/// </summary>
/// <param name="values"></param>
/// <param name="dtype"></param>
public void SetData(Array values, Type dtype)
{
_values = values;
this.ChangeDataType(dtype);
}
/// <summary>
/// Change dtype of elements
/// </summary>
/// <param name="dtype">new storage data type</param>
/// <returns>sucess or not</returns>
public void ChangeDataType(Type dtype)
{
if( _values.GetType().GetElementType() != dtype)
_values = this._ChangeTypeOfArray(_values,dtype);
_DType = dtype;
}
public void SetNewShape(params int[] dimensions)
{
_Shape = new Shape(dimensions);
}
public void Reshape(params int[] dimensions)
{
//if (_TensorLayout == 2)
{
_Shape = new Shape(dimensions);
}
/*else
{
ChangeTensorLayout(2);
_Shape = new Shape(dimensions);
_Shape.ChangeTensorLayout(2);
ChangeTensorLayout(1);
}*/
}
public object Clone()
{
var puffer = new NDStorage();
puffer.Allocate(_DType, new Shape(_Shape.Dimensions), _TensorLayout);
puffer.SetData((Array)_values.Clone());
return puffer;
}
/// <summary>
/// Cange layout to 0 row wise or 1 colum wise
/// </summary>
/// <param name="order">0 or 1</param>
/// <returns>success or not</returns>
public void ChangeTensorLayout(int layout)
{
/*if (layout != _TensorLayout)
if (_TensorLayout == 1)
_ChangeRowToColumnLayout();
else
_ChangeColumnToRowLayout();*/
}
}
}