ufunc.py
Go to the documentation of this file.
00001 #!/usr/bin/env python
00002 
00003 # Copyright Jim Bosch & Ankit Daftery 2010-2012.
00004 # Distributed under the Boost Software License, Version 1.0.
00005 #    (See accompanying file LICENSE_1_0.txt or copy at
00006 #          http://www.boost.org/LICENSE_1_0.txt)
00007 
00008 import ufunc_mod
00009 import unittest
00010 import numpy
00011 from numpy.testing.utils import assert_array_almost_equal
00012 
00013 class TestUnary(unittest.TestCase):
00014 
00015     def testScalar(self):
00016         f = ufunc_mod.UnaryCallable()
00017         assert_array_almost_equal(f(1.0), 2.0)
00018         assert_array_almost_equal(f(3.0), 6.0)
00019 
00020     def testArray(self):
00021         f = ufunc_mod.UnaryCallable()
00022         a = numpy.arange(5, dtype=float)
00023         b = f(a)
00024         assert_array_almost_equal(b, a*2.0) 
00025         c = numpy.zeros(5, dtype=float)
00026         d = f(a,output=c)
00027         self.assert_(c is d)
00028         assert_array_almost_equal(d, a*2.0) 
00029 
00030     def testList(self):
00031         f = ufunc_mod.UnaryCallable()
00032         a = range(5)
00033         b = f(a)
00034         assert_array_almost_equal(b/2.0, a) 
00035 
00036 class TestBinary(unittest.TestCase):
00037 
00038     def testScalar(self):
00039         f = ufunc_mod.BinaryCallable()
00040         assert_array_almost_equal(f(1.0, 3.0), 11.0) 
00041         assert_array_almost_equal(f(3.0, 2.0), 12.0) 
00042 
00043     def testArray(self):
00044         f = ufunc_mod.BinaryCallable()
00045         a = numpy.random.randn(5)
00046         b = numpy.random.randn(5)
00047         assert_array_almost_equal(f(a,b), (a*2+b*3)) 
00048         c = numpy.zeros(5, dtype=float)
00049         d = f(a,b,output=c)
00050         self.assert_(c is d)
00051         assert_array_almost_equal(d, a*2 + b*3) 
00052         assert_array_almost_equal(f(a, 2.0), a*2 + 6.0) 
00053         assert_array_almost_equal(f(1.0, b), 2.0 + b*3) 
00054         
00055 
00056 if __name__=="__main__":
00057     unittest.main()


boost_numpy
Author(s): Jim Bosch, Ankit Daftery
autogenerated on Fri Aug 28 2015 10:10:40