Skip to content

Commit 25eb901

Browse files
committed
Added full implementation for np.clip
1 parent 7334c3f commit 25eb901

File tree

8 files changed

+1367
-10
lines changed

8 files changed

+1367
-10
lines changed

src/NumSharp.Core/Backends/Default/Math/Default.Clip.cs

Lines changed: 623 additions & 0 deletions
Large diffs are not rendered by default.

src/NumSharp.Core/Backends/Default/Math/Default.ClipNDArray.cs

Lines changed: 588 additions & 0 deletions
Large diffs are not rendered by default.

src/NumSharp.Core/Backends/TensorEngine.cs

Lines changed: 5 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -80,6 +80,11 @@ public abstract class TensorEngine
8080
public abstract NDArray Sinh(in NDArray nd, Type dtype);
8181
public abstract NDArray Sinh(in NDArray nd, NPTypeCode? typeCode = null);
8282

83+
public abstract NDArray Clip(in NDArray lhs, in ValueType min, in ValueType max, Type dtype);
84+
public abstract NDArray Clip(in NDArray lhs, in ValueType min, in ValueType max, NPTypeCode? typeCode = null);
85+
public abstract NDArray ClipNDArray(in NDArray lhs, in NDArray min, in NDArray max, Type dtype);
86+
public abstract NDArray ClipNDArray(in NDArray lhs, in NDArray min, in NDArray max, NPTypeCode? typeCode = null);
87+
8388
#endregion
8489

8590
#region Logic

src/NumSharp.Core/Creation/np.broadcast_to.cs

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -19,8 +19,8 @@ public static Shape broadcast_to(Shape from, Shape against)
1919
/// <summary>
2020
/// Broadcast an array to a new shape.
2121
/// </summary>
22-
/// <param name="lhs">An array to broadcast.</param>
23-
/// <param name="rhs">An array to broadcast.</param>
22+
/// <param name="from">The NDArray to broadcast.</param>
23+
/// <param name="against">The shape to broadcast against.</param>
2424
/// <returns>These arrays are views on the original arrays. They are typically not contiguous. Furthermore, more than one element of a broadcasted array may refer to a single memory location. If you need to write to the arrays, make copies first.</returns>
2525
/// <remarks>https://docs.scipy.org/doc/numpy/reference/generated/numpy.broadcast_to.html</remarks>
2626
public static NDArray broadcast_to(NDArray from, Shape against)

src/NumSharp.Core/Math/np.clip.cs

Lines changed: 112 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,112 @@
1+
using System;
2+
using NumSharp.Backends;
3+
4+
namespace NumSharp
5+
{
6+
public static partial class np
7+
{
8+
/// <summary>
9+
/// Clip (limit) the values in an array.<br></br>
10+
/// Given an interval, values outside the interval are clipped to the interval edges. For example, if an interval of [0, 1] is specified, values smaller than 0 become 0, and values larger than 1 become 1.
11+
/// </summary>
12+
/// <param name="a">Array containing elements to clip.</param>
13+
/// <param name="a_max">Maximum value. If None, clipping is not performed on upper interval edge. Not more than one of a_min and a_max may be None.</param>
14+
/// <param name="a_min">Minimum value. If None, clipping is not performed on lower interval edge. Not more than one of a_min and a_max may be None.</param>
15+
/// <param name="outType">The dtype the returned ndarray should be of, only non integer values are supported.</param>
16+
/// <returns>An array with the elements of a, but where values &lt; a_min are replaced with a_min, and those &gt; a_max with a_max.</returns>
17+
/// <remarks>https://docs.scipy.org/doc/numpy-1.15.0/reference/generated/numpy.clip.html</remarks>
18+
public static NDArray clip(in NDArray a, ValueType a_min, ValueType a_max, NPTypeCode? outType = null)
19+
=> a.TensorEngine.Clip(a, a_min, a_max, outType);
20+
21+
/// <summary>
22+
/// Clip (limit) the values in an array.<br></br>
23+
/// Given an interval, values outside the interval are clipped to the interval edges. For example, if an interval of [0, 1] is specified, values smaller than 0 become 0, and values larger than 1 become 1.
24+
/// </summary>
25+
/// <param name="a">Array containing elements to clip.</param>
26+
/// <param name="a_max">Maximum value. If None, clipping is not performed on upper interval edge. Not more than one of a_min and a_max may be None.</param>
27+
/// <param name="a_min">Minimum value. If None, clipping is not performed on lower interval edge. Not more than one of a_min and a_max may be None.</param>
28+
/// <param name="outType">The dtype the returned ndarray should be of, only non integer values are supported.</param>
29+
/// <returns>An array with the elements of a, but where values &lt; a_min are replaced with a_min, and those &gt; a_max with a_max.</returns>
30+
/// <remarks>https://docs.scipy.org/doc/numpy-1.15.0/reference/generated/numpy.clip.html</remarks>
31+
public static NDArray clip(in NDArray a, ValueType a_min, ValueType a_max, Type outType)
32+
=> a.TensorEngine.Clip(a, a_min, a_max, outType);
33+
34+
/// <summary>
35+
/// Clip (limit) the values in an array.<br></br>
36+
/// Given an interval, values outside the interval are clipped to the interval edges. For example, if an interval of [0, 1] is specified, values smaller than 0 become 0, and values larger than 1 become 1.
37+
/// </summary>
38+
/// <param name="a">Array containing elements to clip.</param>
39+
/// <param name="a_max">Maximum value. If None, clipping is not performed on upper interval edge. Not more than one of a_min and a_max may be None.</param>
40+
/// <param name="a_min">Minimum value. If None, clipping is not performed on lower interval edge. Not more than one of a_min and a_max may be None.</param>
41+
/// <param name="outType">The dtype the returned ndarray should be of, only non integer values are supported.</param>
42+
/// <returns>An array with the elements of a, but where values &lt; a_min are replaced with a_min, and those &gt; a_max with a_max.</returns>
43+
/// <remarks>https://docs.scipy.org/doc/numpy-1.15.0/reference/generated/numpy.clip.html</remarks>
44+
public static NDArray clip(in NDArray a, NDArray a_min, NDArray a_max, NPTypeCode? outType = null)
45+
=> a.TensorEngine.ClipNDArray(a, a_min, a_max, outType);
46+
47+
/// <summary>
48+
/// Clip (limit) the values in an array.<br></br>
49+
/// Given an interval, values outside the interval are clipped to the interval edges. For example, if an interval of [0, 1] is specified, values smaller than 0 become 0, and values larger than 1 become 1.
50+
/// </summary>
51+
/// <param name="a">Array containing elements to clip.</param>
52+
/// <param name="a_max">Maximum value. If None, clipping is not performed on upper interval edge. Not more than one of a_min and a_max may be None.</param>
53+
/// <param name="a_min">Minimum value. If None, clipping is not performed on lower interval edge. Not more than one of a_min and a_max may be None.</param>
54+
/// <param name="outType">The dtype the returned ndarray should be of, only non integer values are supported.</param>
55+
/// <returns>An array with the elements of a, but where values &lt; a_min are replaced with a_min, and those &gt; a_max with a_max.</returns>
56+
/// <remarks>https://docs.scipy.org/doc/numpy-1.15.0/reference/generated/numpy.clip.html</remarks>
57+
public static NDArray clip(in NDArray a, NDArray a_min, NDArray a_max, Type outType)
58+
=> a.TensorEngine.ClipNDArray(a, a_min, a_max, outType);
59+
60+
/// <summary>
61+
/// Clip (limit) the values in an array.<br></br>
62+
/// Given an interval, values outside the interval are clipped to the interval edges. For example, if an interval of [0, 1] is specified, values smaller than 0 become 0, and values larger than 1 become 1.
63+
/// </summary>
64+
/// <param name="a">Array containing elements to clip.</param>
65+
/// <param name="a_max">Maximum value. If None, clipping is not performed on upper interval edge. Not more than one of a_min and a_max may be None.</param>
66+
/// <param name="a_min">Minimum value. If None, clipping is not performed on lower interval edge. Not more than one of a_min and a_max may be None.</param>
67+
/// <param name="outType">The dtype the returned ndarray should be of, only non integer values are supported.</param>
68+
/// <returns>An array with the elements of a, but where values &lt; a_min are replaced with a_min, and those &gt; a_max with a_max.</returns>
69+
/// <remarks>https://docs.scipy.org/doc/numpy-1.15.0/reference/generated/numpy.clip.html</remarks>
70+
public static NDArray clip<TMax>(in NDArray a, NDArray a_min, TMax a_max, NPTypeCode? outType = null) where TMax : unmanaged
71+
=> a.TensorEngine.ClipNDArray(a, a_min, NDArray.Scalar(a_max), outType);
72+
73+
/// <summary>
74+
/// Clip (limit) the values in an array.<br></br>
75+
/// Given an interval, values outside the interval are clipped to the interval edges. For example, if an interval of [0, 1] is specified, values smaller than 0 become 0, and values larger than 1 become 1.
76+
/// </summary>
77+
/// <param name="a">Array containing elements to clip.</param>
78+
/// <param name="a_max">Maximum value. If None, clipping is not performed on upper interval edge. Not more than one of a_min and a_max may be None.</param>
79+
/// <param name="a_min">Minimum value. If None, clipping is not performed on lower interval edge. Not more than one of a_min and a_max may be None.</param>
80+
/// <param name="outType">The dtype the returned ndarray should be of, only non integer values are supported.</param>
81+
/// <returns>An array with the elements of a, but where values &lt; a_min are replaced with a_min, and those &gt; a_max with a_max.</returns>
82+
/// <remarks>https://docs.scipy.org/doc/numpy-1.15.0/reference/generated/numpy.clip.html</remarks>
83+
public static NDArray clip<TMax>(in NDArray a, NDArray a_min, TMax a_max, Type outType) where TMax : unmanaged
84+
=> a.TensorEngine.ClipNDArray(a, a_min, NDArray.Scalar(a_max), outType);
85+
86+
/// <summary>
87+
/// Clip (limit) the values in an array.<br></br>
88+
/// Given an interval, values outside the interval are clipped to the interval edges. For example, if an interval of [0, 1] is specified, values smaller than 0 become 0, and values larger than 1 become 1.
89+
/// </summary>
90+
/// <param name="a">Array containing elements to clip.</param>
91+
/// <param name="a_max">Maximum value. If None, clipping is not performed on upper interval edge. Not more than one of a_min and a_max may be None.</param>
92+
/// <param name="a_min">Minimum value. If None, clipping is not performed on lower interval edge. Not more than one of a_min and a_max may be None.</param>
93+
/// <param name="outType">The dtype the returned ndarray should be of, only non integer values are supported.</param>
94+
/// <returns>An array with the elements of a, but where values &lt; a_min are replaced with a_min, and those &gt; a_max with a_max.</returns>
95+
/// <remarks>https://docs.scipy.org/doc/numpy-1.15.0/reference/generated/numpy.clip.html</remarks>
96+
public static NDArray clip<TMin>(in NDArray a, TMin a_min, NDArray a_max, NPTypeCode? outType = null) where TMin : unmanaged
97+
=> a.TensorEngine.ClipNDArray(a, NDArray.Scalar(a_min), a_max, outType);
98+
99+
/// <summary>
100+
/// Clip (limit) the values in an array.<br></br>
101+
/// Given an interval, values outside the interval are clipped to the interval edges. For example, if an interval of [0, 1] is specified, values smaller than 0 become 0, and values larger than 1 become 1.
102+
/// </summary>
103+
/// <param name="a">Array containing elements to clip.</param>
104+
/// <param name="a_max">Maximum value. If None, clipping is not performed on upper interval edge. Not more than one of a_min and a_max may be None.</param>
105+
/// <param name="a_min">Minimum value. If None, clipping is not performed on lower interval edge. Not more than one of a_min and a_max may be None.</param>
106+
/// <param name="outType">The dtype the returned ndarray should be of, only non integer values are supported.</param>
107+
/// <returns>An array with the elements of a, but where values &lt; a_min are replaced with a_min, and those &gt; a_max with a_max.</returns>
108+
/// <remarks>https://docs.scipy.org/doc/numpy-1.15.0/reference/generated/numpy.clip.html</remarks>
109+
public static NDArray clip<TMin>(in NDArray a, TMin a_min, NDArray a_max, Type outType) where TMin : unmanaged
110+
=> a.TensorEngine.ClipNDArray(a, NDArray.Scalar(a_min), a_max, outType);
111+
}
112+
}

test/NumSharp.Benchmark/Unmanaged/NestingCalls.cs

Lines changed: 0 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -167,8 +167,6 @@ public void Switchcase()
167167
break;
168168
case NPTypeCode.String:
169169
break;
170-
case NPTypeCode.NDArray:
171-
break;
172170
case NPTypeCode.Complex:
173171
b = a + 1 - 1;
174172
break;
Lines changed: 37 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,37 @@
1+
using System;
2+
using System.Collections.Generic;
3+
using System.Text;
4+
using FluentAssertions;
5+
using Microsoft.VisualStudio.TestTools.UnitTesting;
6+
using NumSharp.UnitTest.Utilities;
7+
8+
namespace NumSharp.UnitTest
9+
{
10+
[TestClass]
11+
public class np_clip_test
12+
{
13+
[TestMethod]
14+
public void Case1()
15+
{
16+
var a = np.arange(12).reshape(3, 4);
17+
var max = np.repeat(8, 12).reshape(3, 4);
18+
np.clip(a, 3, max).Should().BeOfValues(3, 3, 3, 3, 4, 5, 6, 7, 8, 8, 8, 8).And.BeShaped(3, 4);
19+
}
20+
21+
[TestMethod]
22+
public void Case2()
23+
{
24+
var a = np.arange(12).reshape(3, 4);
25+
var max = np.repeat(8, 12).reshape(3, 4);
26+
np.clip(a, max, null).Should().BeOfValues(8, 8, 8, 8, 8, 8, 8, 8, 8, 9, 10, 11).And.BeShaped(3, 4);
27+
}
28+
29+
[TestMethod]
30+
public void Case3()
31+
{
32+
var a = np.arange(12).reshape(3, 4);
33+
var max = np.repeat(8, 12).reshape(3, 4);
34+
np.clip(a, null, max).Should().BeOfValues(0, 1, 2, 3, 4, 5, 6, 7, 8, 8, 8, 8).And.BeShaped(3, 4);
35+
}
36+
}
37+
}

test/NumSharp.UnitTest/Utilities/ArraysTests.cs

Lines changed: 0 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -30,12 +30,6 @@ public void Create_3()
3030
Arrays.Create(NPTypeCode.Int32, 1000).Should().BeOfType<int[]>().And.HaveCount(1000);
3131
}
3232

33-
[TestMethod]
34-
public void Create_4()
35-
{
36-
Arrays.Create(NPTypeCode.NDArray, 1000).Should().BeOfType<NDArray[]>().And.HaveCount(1000);
37-
}
38-
3933
[TestMethod]
4034
public void Insert_0()
4135
{

0 commit comments

Comments
 (0)