2
0
mirror of https://github.com/boostorg/python.git synced 2026-01-26 18:52:26 +00:00

Added index array and boolean tests

This commit is contained in:
Ankit Daftery
2011-06-30 02:50:40 +00:00
parent 88dd5330d0
commit 2979e4b062
2 changed files with 48 additions and 20 deletions

View File

@@ -4,24 +4,45 @@ import indexing_mod
class TestIndexing(unittest.TestCase):
def testSingle(self):
x = numpy.arange(0,10)
for i in range(0,10):
numpy.testing.assert_equal(indexing_mod.single(x,i), i)
for i in range(-10,0):
numpy.testing.assert_equal(indexing_mod.single(x,i),10+i)
def testSingle(self):
x = numpy.arange(0,10)
for i in range(0,10):
numpy.testing.assert_equal(indexing_mod.single(x,i), i)
for i in range(-10,0):
numpy.testing.assert_equal(indexing_mod.single(x,i),10+i)
def testSlice(self):
x = numpy.arange(0,10)
sl = slice(3,8)
b = [3,4,5,6,7]
numpy.testing.assert_equal(indexing_mod.slice(x,sl), b)
def testSlice(self):
x = numpy.arange(0,10)
sl = slice(3,8)
b = [3,4,5,6,7]
numpy.testing.assert_equal(indexing_mod.slice(x,sl), b)
def testStepSlice(self):
x = numpy.arange(0,10)
sl = slice(3,8,2)
b = [3,5,7]
numpy.testing.assert_equal(indexing_mod.slice(x,sl), b)
def testStepSlice(self):
x = numpy.arange(0,10)
sl = slice(3,8,2)
b = [3,5,7]
numpy.testing.assert_equal(indexing_mod.slice(x,sl), b)
def testIndex(self):
x = numpy.arange(0,10)
chk = numpy.array([3,4,5,6])
numpy.testing.assert_equal(indexing_mod.indexarray(x,chk),chk)
chk = numpy.array([[0,1],[2,3]])
numpy.testing.assert_equal(indexing_mod.indexarray(x,chk),chk)
# x = numpy.arange(9).reshape(3,3)
# y = numpy.array([0,1])
# z = numpy.array([0,2])
# chk = numpy.array([0,5])
# numpy.testing.assert_equal(indexing_mod.indexarray(x,y,z),chk) # This throws an assertion error, indicates shape mismatch
x = numpy.arange(0,10)
b = x>4
chk = numpy.array([5,6,7,8,9])
numpy.testing.assert_equal(indexing_mod.indexarray(x,b),chk)
x = numpy.arange(9).reshape(3,3)
b = numpy.array([0,2])
sl = slice(0,2)
chk = numpy.array([[0,1,2],[6,7,8]])
numpy.testing.assert_equal(indexing_mod.indexslice(x,b,sl),chk)
if __name__=="__main__":
unittest.main()
unittest.main()

View File

@@ -5,10 +5,17 @@ namespace bp = boost::python;
bp::object single(bp::numpy::ndarray ndarr, int i) { return ndarr[i];}
bp::object slice(bp::numpy::ndarray ndarr, bp::slice sl) { return ndarr[sl];}
bp::object indexarray(bp::numpy::ndarray ndarr, bp::numpy::ndarray d1) { return ndarr[d1];}
bp::object indexarray_2d(bp::numpy::ndarray ndarr, bp::numpy::ndarray d1,bp::numpy::ndarray d2) { return ndarr[d1][d2];}
bp::object indexslice(bp::numpy::ndarray ndarr, bp::numpy::ndarray d1,bp::slice sl) { return ndarr[d1][sl];}
BOOST_PYTHON_MODULE(indexing_mod)
{
bp::numpy::initialize();
bp::def("single", single);
bp::def("slice", slice);
bp::numpy::initialize();
bp::def("single", &single);
bp::def("slice", &slice);
bp::def("indexarray", &indexarray);
bp::def("indexarray", &indexarray_2d);
bp::def("indexslice", &indexslice);
}