File: numpy.cpp

package info (click to toggle)
boost 1.33.1-10
  • links: PTS
  • area: main
  • in suites: etch, etch-m68k
  • size: 100,948 kB
  • ctags: 145,103
  • sloc: cpp: 573,492; xml: 49,055; python: 15,626; ansic: 13,588; sh: 2,099; yacc: 858; makefile: 660; perl: 427; lex: 111; csh: 6
file content (104 lines) | stat: -rw-r--r-- 2,686 bytes parent folder | download | duplicates (2)
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
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
// Copyright David Abrahams 2002.
// 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)

#include <boost/python/numeric.hpp>
#include <boost/python/tuple.hpp>
#include <boost/python/module.hpp>
#include <boost/python/def.hpp>

using namespace boost::python;

// See if we can invoke array() from C++
object new_array()
{
    return numeric::array(
        make_tuple(
            make_tuple(1,2,3)
          , make_tuple(4,5,6)
          , make_tuple(7,8,9)
            )
        );
}

// test argument conversion
void take_array(numeric::array x)
{
}

// A separate function to invoke the info() member. Must happen
// outside any doctests since this prints directly to stdout and the
// result text includes the address of the 'self' array.
void info(numeric::array const& z)
{
    z.info();
}

// Tests which work on both Numeric and numarray array objects. Of
// course all of the operators "just work" since numeric::array
// inherits that behavior from object.
void exercise(numeric::array& y, object check)
{
    y[make_tuple(2,1)] = 3;
    check(y);
    check(y.astype('D'));
    check(y.copy());
    check(y.typecode());
}

// numarray-specific tests.  check is a callable object which we can
// use to record intermediate results, which are later compared with
// the results of corresponding python operations.
void exercise_numarray(numeric::array& y, object check)
{
    check(y.astype());
    
    check(y.argmax());
    check(y.argmax(0));
    
    check(y.argmin());
    check(y.argmin(0));
    
    check(y.argsort());
    check(y.argsort(1));

    y.byteswap();
    check(y);
    
    check(y.diagonal());
    check(y.diagonal(1));
    check(y.diagonal(0, 1));
    check(y.diagonal(0, 1, 0));

    check(y.is_c_array());
    check(y.isbyteswapped());

    check(y.trace());
    check(y.trace(1));
    check(y.trace(0, 1));
    check(y.trace(0, 1, 0));

    check(y.new_('D'));
    y.sort();
    check(y);
    check(y.type());

    check(y.factory(make_tuple(1.2, 3.4)));
    check(y.factory(make_tuple(1.2, 3.4), "Double"));
    check(y.factory(make_tuple(1.2, 3.4), "Double", make_tuple(1,2,1)));
    check(y.factory(make_tuple(1.2, 3.4), "Double", make_tuple(2,1,1), false));
    check(y.factory(make_tuple(1.2, 3.4), "Double", make_tuple(2), true, true));
}

BOOST_PYTHON_MODULE(numpy_ext)
{
    def("new_array", new_array);
    def("take_array", take_array);
    def("exercise", exercise);
    def("exercise_numarray", exercise_numarray);
    def("set_module_and_type", &numeric::array::set_module_and_type);
    def("info", info);
}

#include "module_tail.cpp"