forked from SciSharp/Numpy.NET
-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathNumPy_set.tests.cs
More file actions
277 lines (243 loc) · 9.29 KB
/
NumPy_set.tests.cs
File metadata and controls
277 lines (243 loc) · 9.29 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
// Copyright (c) 2019 by the SciSharp Team
// Code generated by CodeMinion: https://github.com/SciSharp/CodeMinion
using System;
using System.Collections;
using System.Collections.Generic;
using System.IO;
using System.Linq;
using System.Runtime.InteropServices;
using System.Text;
using Numpy.Models;
using Microsoft.VisualStudio.TestTools.UnitTesting;
using Assert = NUnit.Framework.Assert;
namespace Numpy.UnitTest
{
[TestClass]
public class NumPy_setTest : BaseTestCase
{
[TestMethod]
public void in1dTest()
{
// >>> test = np.array([0, 1, 2, 5, 0])
// >>> states = [0, 2]
// >>> mask = np.in1d(test, states)
// >>> mask
// array([ True, False, True, False, True])
// >>> test[mask]
// array([0, 2, 0])
// >>> mask = np.in1d(test, states, invert=True)
// >>> mask
// array([False, True, False, True, False])
// >>> test[mask]
// array([1, 5])
//
#if TODO
var given= test = np.array({0, 1, 2, 5, 0});
given= states = [0, 2];
given= mask = np.in1d(test, states);
given= mask;
var expected=
"array([ True, False, True, False, True])";
Assert.AreEqual(expected, given.repr);
given= test[mask];
expected=
"array([0, 2, 0])";
Assert.AreEqual(expected, given.repr);
given= mask = np.in1d(test, states, invert=True);
given= mask;
expected=
"array([False, True, False, True, False])";
Assert.AreEqual(expected, given.repr);
given= test[mask];
expected=
"array([1, 5])";
Assert.AreEqual(expected, given.repr);
#endif
}
[TestMethod]
public void intersect1dTest()
{
// >>> np.intersect1d([1, 3, 4, 3], [3, 1, 2, 1])
// array([1, 3])
//
#if TODO
var given= np.intersect1d({1, 3, 4, 3}, {3, 1, 2, 1});
var expected=
"array([1, 3])";
Assert.AreEqual(expected, given.repr);
#endif
// To intersect more than two arrays, use functools.reduce:
// >>> from functools import reduce
// >>> reduce(np.intersect1d, ([1, 3, 4, 3], [3, 1, 2, 1], [6, 3, 4, 2]))
// array([3])
//
#if TODO
given= from functools import reduce;
given= reduce(np.intersect1d, ({1, 3, 4, 3}, {3, 1, 2, 1}, {6, 3, 4, 2}));
expected=
"array([3])";
Assert.AreEqual(expected, given.repr);
#endif
// To return the indices of the values common to the input arrays
// along with the intersected values:
// >>> x = np.array([1, 1, 2, 3, 4])
// >>> y = np.array([2, 1, 4, 6])
// >>> xy, x_ind, y_ind = np.intersect1d(x, y, return_indices=True)
// >>> x_ind, y_ind
// (array([0, 2, 4]), array([1, 0, 2]))
// >>> xy, x[x_ind], y[y_ind]
// (array([1, 2, 4]), array([1, 2, 4]), array([1, 2, 4]))
}
[TestMethod]
public void isinTest()
{
// >>> element = 2*np.arange(4).reshape((2, 2))
// >>> element
// array([[0, 2],
// [4, 6]])
// >>> test_elements = [1, 2, 4, 8]
// >>> mask = np.isin(element, test_elements)
// >>> mask
// array([[ False, True],
// [ True, False]])
// >>> element[mask]
// array([2, 4])
//
#if TODO
var given= element = 2*np.arange(4).reshape((2, 2));
given= element;
var expected=
"array([[0, 2],\n" +
" [4, 6]])";
Assert.AreEqual(expected, given.repr);
given= test_elements = [1, 2, 4, 8];
given= mask = np.isin(element, test_elements);
given= mask;
expected=
"array([[ False, True],\n" +
" [ True, False]])";
Assert.AreEqual(expected, given.repr);
given= element[mask];
expected=
"array([2, 4])";
Assert.AreEqual(expected, given.repr);
#endif
// The indices of the matched values can be obtained with nonzero:
// >>> np.nonzero(mask)
// (array([0, 1]), array([1, 0]))
//
#if TODO
given= np.nonzero(mask);
expected=
"(array([0, 1]), array([1, 0]))";
Assert.AreEqual(expected, given.repr);
#endif
// The test can also be inverted:
// >>> mask = np.isin(element, test_elements, invert=True)
// >>> mask
// array([[ True, False],
// [ False, True]])
// >>> element[mask]
// array([0, 6])
//
#if TODO
given= mask = np.isin(element, test_elements, invert=True);
given= mask;
expected=
"array([[ True, False],\n" +
" [ False, True]])";
Assert.AreEqual(expected, given.repr);
given= element[mask];
expected=
"array([0, 6])";
Assert.AreEqual(expected, given.repr);
#endif
// Because of how array handles sets, the following does not
// work as expected:
// >>> test_set = {1, 2, 4, 8}
// >>> np.isin(element, test_set)
// array([[ False, False],
// [ False, False]])
//
#if TODO
given= test_set = {1, 2, 4, 8};
given= np.isin(element, test_set);
expected=
"array([[ False, False],\n" +
" [ False, False]])";
Assert.AreEqual(expected, given.repr);
#endif
// Casting the set to a list gives the expected result:
// >>> np.isin(element, list(test_set))
// array([[ False, True],
// [ True, False]])
//
#if TODO
given= np.isin(element, list(test_set));
expected=
"array([[ False, True],\n" +
" [ True, False]])";
Assert.AreEqual(expected, given.repr);
#endif
}
[TestMethod]
public void setdiff1dTest()
{
// >>> a = np.array([1, 2, 3, 2, 4, 1])
// >>> b = np.array([3, 4, 5, 6])
// >>> np.setdiff1d(a, b)
// array([1, 2])
//
#if TODO
var given= a = np.array({1, 2, 3, 2, 4, 1});
given= b = np.array({3, 4, 5, 6});
given= np.setdiff1d(a, b);
var expected=
"array([1, 2])";
Assert.AreEqual(expected, given.repr);
#endif
}
[TestMethod]
public void setxor1dTest()
{
// >>> a = np.array([1, 2, 3, 2, 4])
// >>> b = np.array([2, 3, 5, 7, 5])
// >>> np.setxor1d(a,b)
// array([1, 4, 5, 7])
//
#if TODO
var given= a = np.array({1, 2, 3, 2, 4});
given= b = np.array({2, 3, 5, 7, 5});
given= np.setxor1d(a,b);
var expected=
"array([1, 4, 5, 7])";
Assert.AreEqual(expected, given.repr);
#endif
}
[TestMethod]
public void union1dTest()
{
// >>> np.union1d([-1, 0, 1], [-2, 0, 2])
// array([-2, -1, 0, 1, 2])
//
#if TODO
var given= np.union1d({-1, 0, 1}, {-2, 0, 2});
var expected=
"array([-2, -1, 0, 1, 2])";
Assert.AreEqual(expected, given.repr);
#endif
// To find the union of more than two arrays, use functools.reduce:
// >>> from functools import reduce
// >>> reduce(np.union1d, ([1, 3, 4, 3], [3, 1, 2, 1], [6, 3, 4, 2]))
// array([1, 2, 3, 4, 6])
//
#if TODO
given= from functools import reduce;
given= reduce(np.union1d, ({1, 3, 4, 3}, {3, 1, 2, 1}, {6, 3, 4, 2}));
expected=
"array([1, 2, 3, 4, 6])";
Assert.AreEqual(expected, given.repr);
#endif
}
}
}