Skip to content

Commit 1e6db97

Browse files
committed
More tests for cumsum, and documentation
1 parent f5815cb commit 1e6db97

2 files changed

Lines changed: 88 additions & 16 deletions

File tree

src/NumSharp.Core/Math/NDArray.cumsum.cs

Lines changed: 27 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -6,22 +6,41 @@ namespace NumSharp
66
{
77
public partial class NDArray
88
{
9-
public NDArray cumsum()
9+
/// <summary>
10+
/// Return the cumulative sum of the elements along a given axis.
11+
/// </summary>
12+
/// <param name="axis">Axis along which the cumulative sum is computed. The default (-1) is to compute the cumsum over the flattened array.</param>
13+
/// <param name="dtype">Type of the returned array and of the accumulator in which the elements are summed. If dtype is not specified, it defaults to the dtype of a, unless a has an integer dtype with a precision less than that of the default platform integer. In that case, the default platform integer is used.</param>
14+
/// <returns>A new array holding the result is returned unless out is specified, in which case a reference to out is returned. The result has the same size as a, and the same shape as a if axis is not None or a is a 1-d array.</returns>
15+
/// <remarks>https://docs.scipy.org/doc/numpy/reference/generated/numpy.cumsum.html</remarks>
16+
public NDArray cumsum(int axis = -1, Type dtype = null)
1017
{
11-
return np.cumsum(this);
18+
return np.cumsum(this, axis, dtype);
1219
}
1320
}
1421

1522
public static partial class np
1623
{
17-
public static NDArray cumsum(NDArray a)
24+
/// <summary>
25+
/// Return the cumulative sum of the elements along a given axis.
26+
/// </summary>
27+
/// <param name="arr">Input array.</param>
28+
/// <param name="axis">Axis along which the cumulative sum is computed. The default (-1) is to compute the cumsum over the flattened array.</param>
29+
/// <param name="dtype">Type of the returned array and of the accumulator in which the elements are summed. If dtype is not specified, it defaults to the dtype of a, unless a has an integer dtype with a precision less than that of the default platform integer. In that case, the default platform integer is used.</param>
30+
/// <returns>A new array holding the result is returned unless out is specified, in which case a reference to out is returned. The result has the same size as a, and the same shape as a if axis is not None or a is a 1-d array.</returns>
31+
/// <remarks>https://docs.scipy.org/doc/numpy/reference/generated/numpy.cumsum.html</remarks>
32+
public static NDArray cumsum(NDArray arr, int axis = -1, Type dtype = null)
1833
{
19-
// TODO currently no support for multidimensional a
20-
NDArray cs = np.zeros(a.shape[0]);
21-
cs[0] = a[0];
22-
for (int i = 1; i < a.shape[0]; i++)
34+
if (axis > -1)
2335
{
24-
cs[i] = cs[i - 1] + a[i];
36+
// TODO currently no support for multidimensional a
37+
throw new NotImplementedException();
38+
}
39+
NDArray cs = np.zeros(arr.shape[0]);
40+
cs[0] = arr[0];
41+
for (int i = 1; i < arr.shape[0]; i++)
42+
{
43+
cs[i] = cs[i - 1] + arr[i];
2544
}
2645
return cs;
2746
}

test/NumSharp.UnitTest/Math/NDArray.cumsum.Test.cs

Lines changed: 61 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -1,38 +1,91 @@
11
using System;
22
using System.Collections.Generic;
3+
using System.Linq;
34
using System.Text;
45
using Microsoft.VisualStudio.TestTools.UnitTesting;
56

7+
using FluentAssertions;
8+
69
namespace NumSharp.UnitTest.Maths
710
{
811
[TestClass]
912
public class NDArrayCumsumTest : TestClass
1013
{
1114
[TestMethod]
12-
public void CumsumStaticTest()
15+
public void CumsumStaticFunctionTest()
1316
{
1417
NDArray arr = new double[] { 0, 1, 4, 2, 5, 6, 2 };
1518
NDArray expected = new double[] { 0, 1, 5, 7, 12, 18, 20 };
1619

1720
NDArray actual = np.cumsum(arr);
1821

19-
for (int i = 0; i < expected.shape[0]; i++)
20-
{
21-
Assert.AreEqual((double)expected[i], (double)actual[i]);
22-
}
22+
actual.Array.Should().BeEquivalentTo(expected.Array);
2323
}
2424

2525
[TestMethod]
26-
public void CumsumMemberTest()
26+
public void CumsumMemberFunctionTest()
2727
{
2828
NDArray arr = new double[] { 0, 1, 4, 2, 5, 6, 2 };
2929
NDArray expected = new double[] { 0, 1, 5, 7, 12, 18, 20 };
3030

3131
NDArray actual = arr.cumsum();
3232

33-
for (int i = 0; i < expected.shape[0]; i++)
33+
actual.Array.Should().BeEquivalentTo(expected.Array);
34+
}
35+
36+
[TestMethod]
37+
public void Cumsum2dTest()
38+
{
39+
NDArray arr = new int[,] { { 1, 2, 3 }, { 4, 5, 6 } };
40+
NDArray expected = new int[] { 1, 3, 6, 10, 15, 21 };
41+
42+
NDArray actual = np.cumsum(arr);
43+
44+
actual.Array.Should().BeEquivalentTo(expected.Array);
45+
}
46+
47+
[TestMethod]
48+
public void Cumsum2dDtypeTest()
49+
{
50+
NDArray arr = new int[,] { { 1, 2, 3 }, { 4, 5, 6 } };
51+
NDArray expected = new float[] { 1, 3, 6, 10, 15, 21 };
52+
53+
NDArray actual = np.cumsum(arr, dtype: typeof(float));
54+
55+
actual.Array.Should().BeEquivalentTo(expected.Array);
56+
}
57+
58+
[TestMethod]
59+
public void Cumsum2dAxisRowsTest()
60+
{
61+
NDArray arr = new int[,] { { 1, 2, 3 }, { 4, 5, 6 } };
62+
NDArray expected = new int[,] { { 1, 2, 3 }, { 5, 7, 9 } };
63+
64+
NDArray actual = np.cumsum(arr, axis: 0);
65+
66+
for (int i = 0; i < actual.shape[0]; i++)
67+
{
68+
for (int j = 0; j < actual.shape[1]; j++)
69+
{
70+
Assert.AreEqual((int)expected[i, j], (int)actual[i, j]);
71+
}
72+
}
73+
}
74+
75+
[TestMethod]
76+
public void Cumsum2dAxisColsTest()
77+
{
78+
NDArray arr = new int[,] { { 1, 2, 3 }, { 4, 5, 6 } };
79+
NDArray expected = new int[,] { { 1, 3, 6 }, { 4, 9, 15 } };
80+
81+
NDArray actual = np.cumsum(arr, axis: 1);
82+
83+
for (int i = 0; i < actual.shape[0]; i++)
3484
{
35-
Assert.AreEqual((double)expected[i], (double)actual[i]);
85+
for (int j = 0; j < actual.shape[1]; j++)
86+
{
87+
Assert.AreEqual((int)expected[i, j], (int)actual[i, j]);
88+
}
3689
}
3790
}
3891
}

0 commit comments

Comments
 (0)