ufunc.py 1.7 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657
  1. #!/usr/bin/env python
  2. # Copyright Jim Bosch & Ankit Daftery 2010-2012.
  3. # Distributed under the Boost Software License, Version 1.0.
  4. # (See accompanying file LICENSE_1_0.txt or copy at
  5. # http://www.boost.org/LICENSE_1_0.txt)
  6. import ufunc_ext
  7. import unittest
  8. import numpy
  9. from numpy.testing.utils import assert_array_almost_equal
  10. class TestUnary(unittest.TestCase):
  11. def testScalar(self):
  12. f = ufunc_ext.UnaryCallable()
  13. assert_array_almost_equal(f(1.0), 2.0)
  14. assert_array_almost_equal(f(3.0), 6.0)
  15. def testArray(self):
  16. f = ufunc_ext.UnaryCallable()
  17. a = numpy.arange(5, dtype=float)
  18. b = f(a)
  19. assert_array_almost_equal(b, a*2.0)
  20. c = numpy.zeros(5, dtype=float)
  21. d = f(a,output=c)
  22. self.assert_(c is d)
  23. assert_array_almost_equal(d, a*2.0)
  24. def testList(self):
  25. f = ufunc_ext.UnaryCallable()
  26. a = range(5)
  27. b = f(a)
  28. assert_array_almost_equal(b/2.0, a)
  29. class TestBinary(unittest.TestCase):
  30. def testScalar(self):
  31. f = ufunc_ext.BinaryCallable()
  32. assert_array_almost_equal(f(1.0, 3.0), 11.0)
  33. assert_array_almost_equal(f(3.0, 2.0), 12.0)
  34. def testArray(self):
  35. f = ufunc_ext.BinaryCallable()
  36. a = numpy.random.randn(5)
  37. b = numpy.random.randn(5)
  38. assert_array_almost_equal(f(a,b), (a*2+b*3))
  39. c = numpy.zeros(5, dtype=float)
  40. d = f(a,b,output=c)
  41. self.assert_(c is d)
  42. assert_array_almost_equal(d, a*2 + b*3)
  43. assert_array_almost_equal(f(a, 2.0), a*2 + 6.0)
  44. assert_array_almost_equal(f(1.0, b), 2.0 + b*3)
  45. if __name__=="__main__":
  46. unittest.main()