// 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 } } }