summaryrefslogtreecommitdiffstats
path: root/src/boost/libs/python/test/numpy/ufunc.py
blob: e820121ee1b56b0f6197fbf4a2dec6b9d8bfe6bd (plain)
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
#!/usr/bin/env python

# Copyright Jim Bosch & Ankit Daftery 2010-2012.
# Distributed under the Boost Software License, Version 1.0.
#    (See accompanying file LICENSE_1_0.txt or copy at
#          http://www.boost.org/LICENSE_1_0.txt)

import ufunc_ext
import unittest
import numpy
from numpy.testing.utils import assert_array_almost_equal

class TestUnary(unittest.TestCase):

    def testScalar(self):
        f = ufunc_ext.UnaryCallable()
        assert_array_almost_equal(f(1.0), 2.0)
        assert_array_almost_equal(f(3.0), 6.0)

    def testArray(self):
        f = ufunc_ext.UnaryCallable()
        a = numpy.arange(5, dtype=float)
        b = f(a)
        assert_array_almost_equal(b, a*2.0) 
        c = numpy.zeros(5, dtype=float)
        d = f(a,output=c)
        self.assert_(c is d)
        assert_array_almost_equal(d, a*2.0) 

    def testList(self):
        f = ufunc_ext.UnaryCallable()
        a = range(5)
        b = f(a)
        assert_array_almost_equal(b/2.0, a) 

class TestBinary(unittest.TestCase):

    def testScalar(self):
        f = ufunc_ext.BinaryCallable()
        assert_array_almost_equal(f(1.0, 3.0), 11.0) 
        assert_array_almost_equal(f(3.0, 2.0), 12.0) 

    def testArray(self):
        f = ufunc_ext.BinaryCallable()
        a = numpy.random.randn(5)
        b = numpy.random.randn(5)
        assert_array_almost_equal(f(a,b), (a*2+b*3)) 
        c = numpy.zeros(5, dtype=float)
        d = f(a,b,output=c)
        self.assert_(c is d)
        assert_array_almost_equal(d, a*2 + b*3) 
        assert_array_almost_equal(f(a, 2.0), a*2 + 6.0) 
        assert_array_almost_equal(f(1.0, b), 2.0 + b*3) 
        

if __name__=="__main__":
    unittest.main()