forked from SciSharp/NumSharp
-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathnp.repeat.cs
More file actions
114 lines (112 loc) · 4.7 KB
/
np.repeat.cs
File metadata and controls
114 lines (112 loc) · 4.7 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
using System;
using System.Threading.Tasks;
using NumSharp.Backends;
using NumSharp.Utilities;
namespace NumSharp
{
public static partial class np
{
/// <summary>
/// Repeat elements of an array.
/// </summary>
/// <param name="a">Input array.</param>
/// <param name="repeats">The number of repetitions for each element. repeats is broadcasted to fit the shape of the given axis.</param>
/// <param name="axis"></param>
/// <returns></returns>
/// <remarks>https://docs.scipy.org/doc/numpy/reference/generated/numpy.repeat.html</remarks>
public static NDArray repeat(NDArray a, int repeats) //TODO! , int axis = -1
{
int size = a.size * repeats;
a = a.ravel();
switch (a.GetTypeCode)
{
#if _REGEN1
%foreach supported_dtypes,supported_dtypes_lowercase%
case NPTypeCode.#1:
{
var ret = new NDArray(NPTypeCode.#1, Shape.Vector(size));
var data = a.MakeGeneric<#2>();
for (int i = 0; i < a.size; i++)
for (int j = 0; j < repeats; j++)
ret.itemset(new int[1] {i * repeats + j}, data.GetAtIndex(i));
return ret;
}
%
default:
throw new NotSupportedException();
#else
case NPTypeCode.Boolean:
{
var ret = new NDArray(NPTypeCode.Boolean, Shape.Vector(size));
var data = a.MakeGeneric<bool>();
for (int i = 0; i < a.size; i++)
for (int j = 0; j < repeats; j++)
ret.itemset(new int[1] {i * repeats + j}, data.GetAtIndex(i));
return ret;
}
case NPTypeCode.Byte:
{
var ret = new NDArray(NPTypeCode.Byte, Shape.Vector(size));
var data = a.MakeGeneric<byte>();
for (int i = 0; i < a.size; i++)
for (int j = 0; j < repeats; j++)
ret.itemset(new int[1] {i * repeats + j}, data.GetAtIndex(i));
return ret;
}
case NPTypeCode.Int32:
{
var ret = new NDArray(NPTypeCode.Int32, Shape.Vector(size));
var data = a.MakeGeneric<int>();
for (int i = 0; i < a.size; i++)
for (int j = 0; j < repeats; j++)
ret.itemset(new int[1] {i * repeats + j}, data.GetAtIndex(i));
return ret;
}
case NPTypeCode.Int64:
{
var ret = new NDArray(NPTypeCode.Int64, Shape.Vector(size));
var data = a.MakeGeneric<long>();
for (int i = 0; i < a.size; i++)
for (int j = 0; j < repeats; j++)
ret.itemset(new int[1] {i * repeats + j}, data.GetAtIndex(i));
return ret;
}
case NPTypeCode.Single:
{
var ret = new NDArray(NPTypeCode.Single, Shape.Vector(size));
var data = a.MakeGeneric<float>();
for (int i = 0; i < a.size; i++)
for (int j = 0; j < repeats; j++)
ret.itemset(new int[1] {i * repeats + j}, data.GetAtIndex(i));
return ret;
}
case NPTypeCode.Double:
{
var ret = new NDArray(NPTypeCode.Double, Shape.Vector(size));
var data = a.MakeGeneric<double>();
for (int i = 0; i < a.size; i++)
for (int j = 0; j < repeats; j++)
ret.itemset(new int[1] {i * repeats + j}, data.GetAtIndex(i));
return ret;
}
default:
throw new NotSupportedException();
#endif
}
}
/// <summary>
/// Repeat a scalar.
/// </summary>
/// <param name="a">Input scalar.</param>
/// <param name="repeats">The number of repetitions for each element. repeats is broadcasted to fit the shape of the given axis.</param>
/// <param name="axis"></param>
/// <returns></returns>
/// <remarks>https://docs.scipy.org/doc/numpy/reference/generated/numpy.repeat.html</remarks>
public static NDArray repeat<T>(T a, int repeats) where T : unmanaged //TODO! , int axis = -1
{
var ret = new NDArray(InfoOf<T>.NPTypeCode, Shape.Vector(repeats));
Parallel.For(0, repeats, j => ret.itemset<T>(new int[1] {j}, a));
return ret;
}
}
}