1
1
using System ;
2
2
using System . Collections . Generic ;
3
+
3
4
using NUnit . Framework ;
5
+
4
6
using Python . Runtime ;
5
7
using Python . Runtime . Codecs ;
6
8
@@ -24,17 +26,6 @@ public void Dispose()
24
26
[ Test ]
25
27
public void TestReadme ( )
26
28
{
27
- dynamic np ;
28
- try
29
- {
30
- np = Py . Import ( "numpy" ) ;
31
- }
32
- catch ( PythonException )
33
- {
34
- Assert . Inconclusive ( "Numpy or dependency not installed" ) ;
35
- return ;
36
- }
37
-
38
29
Assert . AreEqual ( "1.0" , np . cos ( np . pi * 2 ) . ToString ( ) ) ;
39
30
40
31
dynamic sin = np . sin ;
@@ -55,40 +46,52 @@ public void TestReadme()
55
46
[ Test ]
56
47
public void MultidimensionalNumPyArray ( )
57
48
{
58
- PyObject np ;
59
- try {
60
- np = Py . Import ( "numpy" ) ;
61
- } catch ( PythonException ) {
62
- Assert . Inconclusive ( "Numpy or dependency not installed" ) ;
63
- return ;
64
- }
65
-
66
49
var array = new [ , ] { { 1 , 2 } , { 3 , 4 } } ;
67
50
var ndarray = np . InvokeMethod ( "asarray" , array . ToPython ( ) ) ;
68
- Assert . AreEqual ( ( 2 , 2 ) , ndarray . GetAttr ( "shape" ) . As < ( int , int ) > ( ) ) ;
51
+ Assert . AreEqual ( ( 2 , 2 ) , ndarray . GetAttr ( "shape" ) . As < ( int , int ) > ( ) ) ;
69
52
Assert . AreEqual ( 1 , ndarray [ ( 0 , 0 ) . ToPython ( ) ] . InvokeMethod ( "__int__" ) . As < int > ( ) ) ;
70
53
Assert . AreEqual ( array [ 1 , 0 ] , ndarray [ ( 1 , 0 ) . ToPython ( ) ] . InvokeMethod ( "__int__" ) . As < int > ( ) ) ;
71
54
}
72
55
73
56
[ Test ]
74
57
public void Int64Array ( )
75
58
{
76
- PyObject np ;
77
- try
78
- {
79
- np = Py . Import ( "numpy" ) ;
80
- }
81
- catch ( PythonException )
82
- {
83
- Assert . Inconclusive ( "Numpy or dependency not installed" ) ;
84
- return ;
85
- }
86
-
87
59
var array = new long [ , ] { { 1 , 2 } , { 3 , 4 } } ;
88
60
var ndarray = np . InvokeMethod ( "asarray" , array . ToPython ( ) ) ;
89
61
Assert . AreEqual ( ( 2 , 2 ) , ndarray . GetAttr ( "shape" ) . As < ( int , int ) > ( ) ) ;
90
62
Assert . AreEqual ( 1 , ndarray [ ( 0 , 0 ) . ToPython ( ) ] . InvokeMethod ( "__int__" ) . As < long > ( ) ) ;
91
63
Assert . AreEqual ( array [ 1 , 0 ] , ndarray [ ( 1 , 0 ) . ToPython ( ) ] . InvokeMethod ( "__int__" ) . As < long > ( ) ) ;
92
64
}
65
+
66
+ [ Test ]
67
+ public void VarArg ( )
68
+ {
69
+ dynamic zX = np . array ( new [ , ] { { 1 , 2 , 3 } , { 4 , 5 , 6 } , { 8 , 9 , 0 } } ) ;
70
+ dynamic grad = np . gradient ( zX , 4.0 , 5.0 ) ;
71
+ dynamic grad2 = np . InvokeMethod ( "gradient" , new PyObject [ ] { zX , new PyFloat ( 4.0 ) , new PyFloat ( 5.0 ) } ) ;
72
+
73
+ Assert . AreEqual ( 4.125 , grad [ 0 ] . sum ( ) . __float__ ( ) . As < double > ( ) , 0.001 ) ;
74
+ Assert . AreEqual ( - 1.2 , grad [ 1 ] . sum ( ) . __float__ ( ) . As < double > ( ) , 0.001 ) ;
75
+ Assert . AreEqual ( 4.125 , grad2 [ 0 ] . sum ( ) . __float__ ( ) . As < double > ( ) , 0.001 ) ;
76
+ Assert . AreEqual ( - 1.2 , grad2 [ 1 ] . sum ( ) . __float__ ( ) . As < double > ( ) , 0.001 ) ;
77
+ }
78
+
79
+ #pragma warning disable IDE1006
80
+ dynamic np
81
+ {
82
+ get
83
+ {
84
+ try
85
+ {
86
+ return Py . Import ( "numpy" ) ;
87
+ }
88
+ catch ( PythonException )
89
+ {
90
+ Assert . Inconclusive ( "Numpy or dependency not installed" ) ;
91
+ return null ;
92
+ }
93
+ }
94
+ }
95
+
93
96
}
94
97
}
0 commit comments