forked from SciSharp/NumSharp
-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathNDArray.roll.cs
More file actions
97 lines (77 loc) · 3.33 KB
/
NDArray.roll.cs
File metadata and controls
97 lines (77 loc) · 3.33 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
using System;
namespace NumSharp
{
public partial class NDArray
{
/// <summary>
/// Roll array elements along a given axis.
///
/// Elements that roll beyond the last position are re-introduced at the first.
/// </summary>
public NDArray roll(int shift, int axis)
{
if (axis > this.ndim)
throw new IncorrectShapeException();
shift = (axis == 0) ? (-1) * shift : shift;
shift = ((shift % this.shape[axis]) < 0) ? shift + this.shape[axis] : shift;
//TODO! rest of data types and remove usage of .Data, it'll fail if this NDArray's shape is a slice.
switch (dtype.Name)
{
case "Int32":
{
var data = this.Data<int>();
var newData = new int[this.size];
for (int idx = 0; idx < this.size; idx++)
{
int[] indexes = this.Storage.Shape.GetCoordinates(idx);
indexes[axis] = (indexes[axis] + shift) % this.shape[axis];
newData[this.Storage.Shape.GetOffset(indexes)] = data[idx];
}
return new NDArray(newData, this.shape);
}
case "Single":
{
var data = this.Data<float>();
var newData = new float[this.size];
for (int idx = 0; idx < this.size; idx++)
{
int[] indexes = this.Storage.Shape.GetCoordinates(idx);
indexes[axis] = (indexes[axis] + shift) % this.shape[axis];
newData[this.Storage.Shape.GetOffset(indexes)] = data[idx];
}
return new NDArray(newData, this.shape);
}
case "Double":
{
var data = this.Data<double>();
var newData = new double[this.size];
for (int idx = 0; idx < this.size; idx++)
{
int[] indexes = this.Storage.Shape.GetCoordinates(idx);
indexes[axis] = (indexes[axis] + shift) % this.shape[axis];
newData[this.Storage.Shape.GetOffset(indexes)] = data[idx];
}
return new NDArray(newData, this.shape);
}
default:
throw new NotImplementedException($"NDArray.roll {dtype.Name}");
}
}
public NDArray roll(int shift)
{
return null;
//shift = (-1) * shift;
//Array cpy = Arrays.Create(this.dtype, this.size);
//shift = ((shift % this.size) < 0) ? shift + this.size : shift;
////shift = shift % this.size;
//var strg = this.Storage.GetData();
//Array.Copy(strg, shift, cpy, 0, this.size - shift);
//for (int idx = 0; idx < shift; idx++)
// cpy.SetValue(strg.GetValue(idx), this.size - shift + idx);
//var returnValue = new NDArray(this.dtype);
//returnValue.Storage.Allocate(new Shape(this.shape));
//returnValue.Storage.ReplaceData(cpy);
//return returnValue;
}
}
}