Repository navigation
Expand file tree
/
Copy pathNDArray.Indexing.cs
More file actions
113 lines (104 loc) · 4.13 KB
/
Copy pathNDArray.Indexing.cs
File metadata and controls
113 lines (104 loc) · 4.13 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
using System;
using System.Collections.Generic;
using System.Diagnostics;
using System.Diagnostics.CodeAnalysis;
using System.Globalization;
using System.Linq;
using System.Runtime.CompilerServices;
using NumSharp.Backends;
using NumSharp.Generic;
using NumSharp.Utilities;
namespace NumSharp
{
[SuppressMessage("ReSharper", "CoVariantArrayConversion")]
public partial class NDArray
{
/// <summary>
/// Throws if this NDArray is not writeable (e.g., broadcast arrays).
/// NumPy raises: ValueError: assignment destination is read-only
/// </summary>
[MethodImpl(Inline)]
private void ThrowIfNotWriteable()
{
NumSharpException.ThrowIfNotWriteable(Shape);
}
/// <summary>
/// Used to perform selection based on given indices.
/// </summary>
/// <param name="dims">The pointer to the dimensions</param>
/// <param name="ndims">The count of longs in <paramref name="dims"/></param>
public unsafe NDArray this[long* dims, int ndims]
{
get => new NDArray(Storage.GetData(dims, ndims));
set { ThrowIfNotWriteable(); Storage.GetData(dims, ndims).SetData(value, new int[0]); }
}
/// <summary>
/// Used to perform selection based on a selection indices.
/// </summary>
/// <remarks>https://numpy.org/doc/stable/user/basics.indexing.html</remarks>
/// <exception cref="IndexOutOfRangeException">When one of the indices exceeds limits.</exception>
/// <exception cref="ArgumentException">indices must be of Int type (byte, u/short, u/int, u/long).</exception>
public NDArray this[params NDArray<int>[] selection]
{
get => FetchIndices(this, selection.Select(array => (NDArray)array).ToArray(), null, true);
set
{
ThrowIfNotWriteable();
SetIndices(this, selection, value);
}
}
/// <summary>
/// Slice the array with Python slice notation like this: ":, 2:7:1, ..., np.newaxis"
/// </summary>
/// <param name="slice">A string containing slice notations for every dimension, delimited by comma</param>
/// <returns>A sliced view</returns>
public NDArray this[string slice]
{
get
{
return new NDArray(Storage.GetView(Slice.ParseSlices(slice)));
}
set { ThrowIfNotWriteable(); Storage.GetView(Slice.ParseSlices(slice)).SetData(value, new int[0]); }
}
/// <summary>
/// Slice the array with Python slice notation like this: ":, 2:7:1, ..., np.newaxis"
/// </summary>
/// <param name="slice">A string containing slice notations for every dimension, delimited by comma</param>
/// <returns>A sliced view</returns>
public NDArray this[params Slice[] slice]
{
get
{
return new NDArray(Storage.GetView(slice));
}
set { ThrowIfNotWriteable(); Storage.GetView(slice).SetData(value, new int[0]); }
}
///// <summary>
///// todo: doc
///// </summary>
///// <param name="slice">A string containing slice notations for every dimension, delimited by comma</param>
///// <returns>A sliced view</returns>
//public NDArray this[params IIndex[] slice] //TODO IIndex is NDArray and
//{
// get => new NDArray(Storage.GetView(slice));
// set => Storage.GetView(slice).SetData(value);
//}
/// <summary>
/// Perform slicing, index extraction, masking and indexing all at the same time with mixed index objects
/// </summary>
/// <param name="indicesObjects"></param>
/// <returns></returns>
public NDArray this[params object[] indicesObjects]
{
get
{
return this.FetchIndices(indicesObjects);
}
set
{
ThrowIfNotWriteable();
SetIndices(indicesObjects, value);
}
}
}
}