Skip to content

Commit b4765e5

Browse files
zhuoshui-AIOceania2018
authored andcommitted
just modify the function name
1 parent 4edf91d commit b4765e5

File tree

3 files changed

+110
-2
lines changed

3 files changed

+110
-2
lines changed

src/NumSharp.Core/Utilities/ArrayConvert.cs

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -4097,7 +4097,7 @@ public static Complex[] ToComplex(String[] sourceArray)
40974097
output[i] = Complex.Zero; // NullString save as zero.
40984098
return;
40994099
}
4100-
var match = py.Complex(sourceArray[i]);
4100+
var match = py.complex(sourceArray[i]);
41014101
});
41024102
return output;
41034103
}

src/NumSharp.Core/Utilities/py.cs

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -22,7 +22,7 @@ public static int[] range(int n)
2222
private static readonly Regex _pythonComplexRegex = new Regex(
2323
@"^(?<real>-?\d+(\.\d+)?)?((?<imagSign>\+|-)?(?<imag>\d+(\.\d+)?)?)?j$|^(?<onlyReal>-?\d+(\.\d+)?)$",
2424
RegexOptions.IgnoreCase | RegexOptions.Compiled | RegexOptions.ExplicitCapture);
25-
public static Complex Complex(string input)
25+
public static Complex complex(string input)
2626
{
2727
var match = _pythonComplexRegex.Match(input);
2828
if (!match.Success)
Lines changed: 108 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,108 @@
1+
using System;
2+
using Microsoft.VisualStudio.TestTools.UnitTesting;
3+
using NumSharp;
4+
5+
namespace NumSharp.UnitTest.Logic
6+
{
7+
[TestClass]
8+
public class NpAnyTest
9+
{
10+
[TestMethod]
11+
public void Any1DArrayTest()
12+
{
13+
// 测试1维数组
14+
var arr = np.array(new int[] { 0, 1, 2 });
15+
var result = np.any(arr, axis: 0, keepdims: false);
16+
Assert.AreEqual(true, result.GetItem(0));
17+
}
18+
19+
[TestMethod]
20+
public void Any2DArrayTest()
21+
{
22+
// 测试2维数组
23+
var arr = np.array(new int[,] { { 0, 0 }, { 1, 0 } });
24+
var result = np.any(arr, axis: 0, keepdims: false);
25+
var expected = np.array(new bool[] { true, false });
26+
Assert.IsTrue(Enumerable.SequenceEqual(result.Data<bool>(), expected.Data<bool>()));
27+
}
28+
29+
[TestMethod]
30+
public void Any2DArrayWithAxis1Test()
31+
{
32+
// 测试2维数组,axis=1
33+
var arr = np.array(new int[,] { { 0, 0 }, { 1, 0 } });
34+
var result = np.any(arr, axis: 1, keepdims: false);
35+
var expected = np.array(new bool[] { false, true });
36+
Assert.IsTrue(Enumerable.SequenceEqual(result.Data<bool>(), expected.Data<bool>()));
37+
}
38+
39+
[TestMethod]
40+
public void AnyWithKeepdimsTest()
41+
{
42+
// 测试keepdims参数
43+
var arr = np.array(new int[,] { { 0, 0 }, { 1, 0 } });
44+
var result = np.any(arr, axis: 0, keepdims: true);
45+
// 结果形状应该是 (1, 2) 而不是 (2,)
46+
Assert.AreEqual(2, result.ndim);
47+
Assert.AreEqual(1, result.shape[0]);
48+
Assert.AreEqual(2, result.shape[1]);
49+
}
50+
51+
[TestMethod]
52+
public void AnyWithNegativeAxisTest()
53+
{
54+
// 测试负轴
55+
var arr = np.array(new int[,] { { 0, 0 }, { 1, 0 } });
56+
var result1 = np.any(arr, axis: 1, keepdims: false); // axis=1
57+
var result2 = np.any(arr, axis: -1, keepdims: false); // axis=-1 (应该是等价的)
58+
Assert.IsTrue(Enumerable.SequenceEqual(result1.Data<bool>(), result2.Data<bool>()));
59+
}
60+
61+
[TestMethod]
62+
public void AnyAllZerosTest()
63+
{
64+
// 测试全零数组
65+
var arr = np.array(new int[,] { { 0, 0 }, { 0, 0 } });
66+
var result = np.any(arr, axis: 0, keepdims: false);
67+
var expected = np.array(new bool[] { false, false });
68+
Assert.IsTrue(Enumerable.SequenceEqual(result.Data<bool>(), expected.Data<bool>()));
69+
}
70+
71+
[TestMethod]
72+
public void AnyAllNonZerosTest()
73+
{
74+
// 测试全非零数组
75+
var arr = np.array(new int[,] { { 1, 2 }, { 3, 4 } });
76+
var result = np.any(arr, axis: 0, keepdims: false);
77+
var expected = np.array(new bool[] { true, true });
78+
Assert.IsTrue(Enumerable.SequenceEqual(result.Data<bool>(), expected.Data<bool>()));
79+
}
80+
81+
[TestMethod]
82+
[ExpectedException(typeof(ArgumentOutOfRangeException))]
83+
public void AnyInvalidAxisTest()
84+
{
85+
// 测试无效轴
86+
var arr = np.array(new int[,] { { 0, 1 }, { 2, 3 } });
87+
np.any(arr, axis: 5, keepdims: false); // 轴5不存在
88+
}
89+
90+
[TestMethod]
91+
[ExpectedException(typeof(ArgumentException))]
92+
public void AnyZeroDimensionalArrayTest()
93+
{
94+
// 测试零维数组
95+
var arr = np.array(5); // 零维数组
96+
np.any(arr, axis: 0, keepdims: false);
97+
}
98+
99+
[TestMethod]
100+
[ExpectedException(typeof(ArgumentException))]
101+
public void AnyNullArrayTest()
102+
{
103+
// 测试空数组
104+
NDArray arr = null;
105+
np.any(arr, axis: 0, keepdims: false);
106+
}
107+
}
108+
}

0 commit comments

Comments
 (0)