Skip to content

Commit ab2ac4b

Browse files
authored
Merge pull request #307 from Plankton555/feature/np_cumsum
Feature/np cumsum
2 parents 5533c8c + 1e6db97 commit ab2ac4b

2 files changed

Lines changed: 140 additions & 0 deletions

File tree

Lines changed: 48 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,48 @@
1+
using System;
2+
using System.Collections.Generic;
3+
using System.Text;
4+
5+
namespace NumSharp
6+
{
7+
public partial class NDArray
8+
{
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)
17+
{
18+
return np.cumsum(this, axis, dtype);
19+
}
20+
}
21+
22+
public static partial class np
23+
{
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)
33+
{
34+
if (axis > -1)
35+
{
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];
44+
}
45+
return cs;
46+
}
47+
}
48+
}
Lines changed: 92 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,92 @@
1+
using System;
2+
using System.Collections.Generic;
3+
using System.Linq;
4+
using System.Text;
5+
using Microsoft.VisualStudio.TestTools.UnitTesting;
6+
7+
using FluentAssertions;
8+
9+
namespace NumSharp.UnitTest.Maths
10+
{
11+
[TestClass]
12+
public class NDArrayCumsumTest : TestClass
13+
{
14+
[TestMethod]
15+
public void CumsumStaticFunctionTest()
16+
{
17+
NDArray arr = new double[] { 0, 1, 4, 2, 5, 6, 2 };
18+
NDArray expected = new double[] { 0, 1, 5, 7, 12, 18, 20 };
19+
20+
NDArray actual = np.cumsum(arr);
21+
22+
actual.Array.Should().BeEquivalentTo(expected.Array);
23+
}
24+
25+
[TestMethod]
26+
public void CumsumMemberFunctionTest()
27+
{
28+
NDArray arr = new double[] { 0, 1, 4, 2, 5, 6, 2 };
29+
NDArray expected = new double[] { 0, 1, 5, 7, 12, 18, 20 };
30+
31+
NDArray actual = arr.cumsum();
32+
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++)
84+
{
85+
for (int j = 0; j < actual.shape[1]; j++)
86+
{
87+
Assert.AreEqual((int)expected[i, j], (int)actual[i, j]);
88+
}
89+
}
90+
}
91+
}
92+
}

0 commit comments

Comments
 (0)