forked from SciSharp/NumSharp
-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathnp.asscalar.cs
More file actions
94 lines (87 loc) · 3.88 KB
/
np.asscalar.cs
File metadata and controls
94 lines (87 loc) · 3.88 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
using System;
using NumSharp.Backends.Unmanaged;
using NumSharp.Utilities;
namespace NumSharp
{
public static partial class np
{
/// <summary>
/// Convert an array of size 1 to its scalar equivalent.
/// </summary>
/// <param name="nd">Input NDArray of size 1.</param>
/// <returns></returns>
/// <remarks>https://docs.scipy.org/doc/numpy-1.16.0/reference/generated/numpy.asscalar.html</remarks>
public static T asscalar<T>(NDArray nd) where T : unmanaged
{
if (nd.size != 1)
throw new IncorrectSizeException("Unable to convert NDArray to scalar because size is not 1.");
var value = nd.Storage.GetAtIndex(0);
if (nd.dtype != typeof(T))
return (T)Converts.ChangeType(value, InfoOf<T>.NPTypeCode);
return (T)value;
}
/// <summary>
/// Convert an array of size 1 to its scalar equivalent.
/// </summary>
/// <param name="arr">Input array of size 1.</param>
/// <returns></returns>
/// <remarks>https://docs.scipy.org/doc/numpy-1.16.0/reference/generated/numpy.asscalar.html</remarks>
public static T asscalar<T>(Array arr)
{
if (arr.Length != 1)
throw new IncorrectSizeException("Unable to convert NDArray to scalar because size is not 1.");
var value = arr.GetValue(0);
if (value.GetType() != typeof(T))
return (T)Converts.ChangeType(value, InfoOf<T>.NPTypeCode);
return (T)value;
}
/// <summary>
/// Convert an array of size 1 to its scalar equivalent.
/// </summary>
/// <param name="nd">Input NDArray of size 1.</param>
/// <returns></returns>
/// <remarks>https://docs.scipy.org/doc/numpy-1.16.0/reference/generated/numpy.asscalar.html</remarks>
public static ValueType asscalar(NDArray nd)
{
if (nd.size != 1)
throw new IncorrectSizeException("Unable to convert NDArray to scalar because size is not 1.");
return nd.Storage.GetAtIndex(0);
}
/// <summary>
/// Convert an array of size 1 to its scalar equivalent.
/// </summary>
/// <param name="arr">Input array of size 1.</param>
/// <returns></returns>
/// <remarks>https://docs.scipy.org/doc/numpy-1.16.0/reference/generated/numpy.asscalar.html</remarks>
public static ValueType asscalar(Array arr)
{
if (arr.Length != 1)
throw new IncorrectSizeException("Unable to convert NDArray to scalar because size is not 1.");
return (ValueType) arr.GetValue(0);
}
/// <summary>
/// Convert an array of size 1 to its scalar equivalent.
/// </summary>
/// <param name="arr">Input array of size 1.</param>
/// <returns></returns>
/// <remarks>https://docs.scipy.org/doc/numpy-1.16.0/reference/generated/numpy.asscalar.html</remarks>
public static T asscalar<T>(ArraySlice<T> arr) where T : unmanaged
{
if (arr.Count != 1)
throw new IncorrectSizeException("Unable to convert NDArray to scalar because size is not 1.");
return arr[0];
}
/// <summary>
/// Convert an array of size 1 to its scalar equivalent.
/// </summary>
/// <param name="arr">Input array of size 1.</param>
/// <returns></returns>
/// <remarks>https://docs.scipy.org/doc/numpy-1.16.0/reference/generated/numpy.asscalar.html</remarks>
public static T asscalar<T>(IArraySlice arr) where T : unmanaged
{
if (arr.Count != 1)
throw new IncorrectSizeException("Unable to convert NDArray to scalar because size is not 1.");
return Converts.ChangeType<T>(arr[0]);
}
}
}