forked from SciSharp/NumSharp
-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathNDArray.unique.cs
More file actions
91 lines (87 loc) · 3.96 KB
/
NDArray.unique.cs
File metadata and controls
91 lines (87 loc) · 3.96 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
using System;
using System.Linq;
using System.Threading.Tasks;
using NumSharp.Backends;
using NumSharp.Backends.Unmanaged;
using NumSharp.Utilities;
namespace NumSharp
{
public partial class NDArray
{
/// <summary>
/// Find the unique elements of an array.<br></br>
///
/// Returns the sorted unique elements of an array.There are three optional outputs in addition to the unique elements:<br></br>
/// * the indices of the input array that give the unique values<br></br>
/// * the indices of the unique array that reconstruct the input array<br></br>
/// * the number of times each unique value comes up in the input array<br></br>
/// </summary>
/// <returns>The sorted unique values.</returns>
/// <remarks>https://docs.scipy.org/doc/numpy/reference/generated/numpy.unique.html</remarks>
public NDArray unique()
{
switch (typecode)
{
#if _REGEN
%foreach supported_dtypes,supported_dtypes_lowercase%
case NPTypeCode.#1: return unique<#2>();
%
default: throw new NotSupportedException();
#else
case NPTypeCode.Boolean: return unique<bool>();
case NPTypeCode.Byte: return unique<byte>();
case NPTypeCode.Int16: return unique<short>();
case NPTypeCode.UInt16: return unique<ushort>();
case NPTypeCode.Int32: return unique<int>();
case NPTypeCode.UInt32: return unique<uint>();
case NPTypeCode.Int64: return unique<long>();
case NPTypeCode.UInt64: return unique<ulong>();
case NPTypeCode.Char: return unique<char>();
case NPTypeCode.Double: return unique<double>();
case NPTypeCode.Single: return unique<float>();
case NPTypeCode.Decimal: return unique<decimal>();
default: throw new NotSupportedException();
#endif
}
}
/// <summary>
/// Find the unique elements of an array.<br></br>
///
/// Returns the sorted unique elements of an array.There are three optional outputs in addition to the unique elements:<br></br>
/// * the indices of the input array that give the unique values<br></br>
/// * the indices of the unique array that reconstruct the input array<br></br>
/// * the number of times each unique value comes up in the input array<br></br>
/// </summary>
/// <returns></returns>
/// <remarks>https://docs.scipy.org/doc/numpy/reference/generated/numpy.unique.html</remarks>
protected NDArray unique<T>() where T : unmanaged
{
unsafe
{
var hashset = new Hashset<T>();
if (Shape.IsContiguous)
{
var src = (T*)this.Address;
int len = this.size;
for (int i = 0; i < len; i++) //we do not use Parellel.For to retain order like numpy does.
hashset.Add(src[i]);
var dst = new NDArray(InfoOf<T>.NPTypeCode, Shape.Vector(hashset.Count));
Hashset<T>.CopyTo(hashset, (ArraySlice<T>)dst.Array);
return dst;
}
else
{
int len = this.size;
var flat = this.flat;
var src = (T*)flat.Address;
Func<int, int> getOffset = flat.Shape.GetOffset_1D;
for (int i = 0; i < len; i++) //we do not use Parellel.For to retain order like numpy does.
hashset.Add(src[getOffset(i)]);
var dst = new NDArray(InfoOf<T>.NPTypeCode, Shape.Vector(hashset.Count));
Hashset<T>.CopyTo(hashset, (ArraySlice<T>)dst.Array);
return dst;
}
}
}
}
}