Skip to content

Navigation Menu

Sign in
Appearance settings

Search code, repositories, users, issues, pull requests...

Provide feedback

We read every piece of feedback, and take your input very seriously.

Saved searches

Use saved searches to filter your results more quickly

Sign up
Appearance settings

Commit cfe2f9d

Browse files
author
Rue Yokaze
committed
add demo
1 parent 8e905ef commit cfe2f9d

File tree

3 files changed

+40
-1
lines changed

3 files changed

+40
-1
lines changed

‎demo.py

Lines changed: 9 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,9 @@
1+
import demo_module
2+
import multi_array as ma
3+
import numpy as np
4+
5+
x = ma.make(100, ma.float32)
6+
x.set(np.random.rand(100))
7+
8+
# calculate average using a custom C++ module
9+
print demo_module.average(x)

‎demo_module.cpp

Lines changed: 29 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,29 @@
1+
//
2+
// demo_module.cpp
3+
// python-multi-array
4+
//
5+
// Copyright (C) 2017 Rue Yokaze
6+
// Distributed under the MIT License.
7+
//
8+
#include <boost/multi_array.hpp>
9+
#include <boost/python.hpp>
10+
11+
using boost::multi_array;
12+
using std::shared_ptr;
13+
14+
float average(const shared_ptr<multi_array<float, 1>>& vec)
15+
{
16+
float total = 0.f;
17+
size_t size = vec->shape()[0];
18+
for (size_t i = 0; i < size; ++i)
19+
{
20+
total += (*vec)[i];
21+
}
22+
return total / size;
23+
}
24+
25+
BOOST_PYTHON_MODULE(demo_module)
26+
{
27+
using boost::python::def;
28+
def("average", average);
29+
}

‎setup.py

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -17,6 +17,7 @@
1717

1818
modules = []
1919
modules.append(['multi_array', 'python_multi_array.cpp'])
20+
modules.append(['demo_module', 'demo_module.cpp'])
2021

2122
extensions = []
2223
for module in modules:
@@ -32,4 +33,4 @@
3233
)
3334
extensions.append(ex_module)
3435

35-
setup(name = "multi_array", version = "0.9", ext_modules = extensions)
36+
setup(name = "multi_array", version = "1.0", ext_modules = extensions)

0 commit comments

Comments
(0)

AltStyle によって変換されたページ (->オリジナル) /