Skip to content

Commit fecaefb

Browse files
committed
Added slicing
Updated README
1 parent 0826ee0 commit fecaefb

4 files changed

Lines changed: 64 additions & 11 deletions

File tree

README.md

Lines changed: 6 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -17,14 +17,20 @@ NumSharp has implemented the arange, array, max, min, reshape, normalize, unique
1717
The NumPy class is a high-level abstraction of NDArray that allows NumSharp to be used in the same way as Python's NumPy, minimizing API differences caused by programming language features, allowing .NET developers to maximize Utilize a wide range of NumPy code resources to seamlessly translate python code into .NET code.
1818

1919
* NumPy
20+
* absolute
21+
* amax
2022
* amin
2123
* arange
2224
* array
2325
* hstack
2426
* linspace
2527
* random
28+
* normal
2629
* randint
30+
* randn
31+
* stardard_normal
2732
* reshape
33+
* sin
2834
* vstack
2935
* zeros
3036

src/NumSharp/NumPy.cs

Lines changed: 9 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -15,14 +15,14 @@ public NDArray<double> absolute(NDArray<double> np)
1515
return np.Absolute();
1616
}
1717

18-
public NDArray<double> amin(NDArray<double> np, int? axis = null)
18+
public NDArray<double> amax(NDArray<double> np, int? axis = null)
1919
{
20-
return np.AMin(axis);
20+
return np.AMax(axis);
2121
}
2222

23-
public NDArray<double> amax(NDArray<double> np, int? axis = null)
23+
public NDArray<double> amin(NDArray<double> np, int? axis = null)
2424
{
25-
return np.AMax(axis);
25+
return np.AMin(axis);
2626
}
2727

2828
public NDArray<T> arange(int stop)
@@ -121,6 +121,11 @@ public NDArray<int> reshape(NDArray<int> np, params int[] shape)
121121
return np;
122122
}
123123

124+
public NDArray<double> sin(NDArray<double> nd)
125+
{
126+
return nd.sin();
127+
}
128+
124129
public NDArray<double> vstack(params NDArray<double>[] nps)
125130
{
126131
var n = new NDArray<double>();

src/NumSharp/Selection/NDArray.Slicing.cs

Lines changed: 32 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -6,6 +6,37 @@ namespace NumSharp
66
{
77
public partial class NDArray<T>
88
{
9-
9+
public NDArray<T>[] this[Slice select]
10+
{
11+
get
12+
{
13+
var list = new List<NDArray<T>>();
14+
15+
int[] shape = new int[Shape.Length];
16+
for (int i = 0; i < Shape.Length; i++)
17+
{
18+
if (i == 0)
19+
{
20+
shape[i] = select.Step;
21+
}
22+
else
23+
{
24+
shape[i] = Shape[i];
25+
}
26+
}
27+
28+
for (int s = select.Start; s< select.Stop; s+= select.Step)
29+
{
30+
var n = new NDArray<T>();
31+
Span<T> data = Data;
32+
n.Data = data.Slice(s, select.Step).ToArray();
33+
n.Shape = new Shape(shape);
34+
35+
list.Add(n);
36+
}
37+
38+
return list.ToArray();
39+
}
40+
}
1041
}
1142
}

src/NumSharp/Slice.cs

Lines changed: 17 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -10,18 +10,29 @@ namespace NumSharp
1010
/// </summary>
1111
public class Slice
1212
{
13-
private int start;
14-
private int stop;
15-
private int step;
13+
public int Start { get; set; }
14+
public int Stop { get; set; }
15+
public int Step { get; set; }
1616

17+
public int Length => Stop - Start;
18+
19+
/// <summary>
20+
/// start, stop, step
21+
/// </summary>
22+
/// <param name="p"></param>
1723
public Slice(params int[] p)
1824
{
1925
switch (p.Length)
2026
{
27+
case 2:
28+
Start = p[0];
29+
Stop = p[1];
30+
Step = 1;
31+
break;
2132
case 3:
22-
start = p[0];
23-
stop = p[1];
24-
step = p[2];
33+
Start = p[0];
34+
Stop = p[1];
35+
Step = p[2];
2536
break;
2637
}
2738
}

0 commit comments

Comments
 (0)