Skip to content

Commit 6cf9bf4

Browse files
author
XuhuaHuang
committed
Fix Numpy API example and add one for Pybind11
1 parent 3f53363 commit 6cf9bf4

File tree

4 files changed

+88
-45
lines changed

4 files changed

+88
-45
lines changed

NumPyAPI/CMakeLists.txt

Lines changed: 6 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -1,13 +1,13 @@
1-
cmake_minimum_required(VERSION 3.18)
1+
cmake_minimum_required(VERSION 3.20)
22

3-
project("numpy_cxx")
3+
project("numpy_cxx" LANGUAGES CXX)
44

5-
set(CMAKE_CXX_STANDARD 17)
5+
set(CMAKE_CXX_STANDARD 20)
66
set(CMAKE_CXX_STANDARD_REQUIRED ON)
77

8-
add_executable(numpycxx main.cpp)
8+
find_package(Python3 COMPONENTS Interpreter NumPy Development REQUIRED)
99

10-
find_package (Python3 COMPONENTS Interpreter NumPy Development REQUIRED)
10+
add_executable(numpycxx main.cpp)
1111

1212
# Stop Visual Studio Solution from looking for python311_d.dll
1313
set(CMAKE_BUILD_TYPE Release)
@@ -23,5 +23,6 @@ target_include_directories(numpycxx
2323
target_link_libraries(numpycxx
2424
PUBLIC
2525
${PYTHON_LIBRARIES}
26+
Python3::Python
2627
Python3::NumPy
2728
)

NumPyAPI/main.cpp

Lines changed: 41 additions & 40 deletions
Original file line numberDiff line numberDiff line change
@@ -1,57 +1,58 @@
1+
// clang-format off
12
/*****************************************************************//**
23
* \file main.cpp
34
* \brief
45
*
56
* \author Xuhua Huang
67
* \date January 07, 2023
78
*********************************************************************/
9+
// clang-format on
810

911
#define NPY_NO_DEPRECATED_API NPY_1_7_API_VERSION
1012

1113
#include <Python.h>
1214
#include <numpy/arrayobject.h>
1315

1416
#include <cstdlib>
15-
#include <iostream>
1617

1718
auto main(int argc, char* argv[]) -> int {
18-
// Initialize the Python interpreter
19-
Py_Initialize();
20-
21-
// Import the NumPy module
22-
PyObject* numpy = PyImport_ImportModule("numpy");
23-
24-
// Convert a Python list to a NumPy array
25-
PyObject* list = PyList_New(3);
26-
PyList_SetItem(list, 0, PyLong_FromLong(1));
27-
PyList_SetItem(list, 1, PyLong_FromLong(2));
28-
PyList_SetItem(list, 2, PyLong_FromLong(3));
29-
PyObject* array = PyObject_CallMethod(numpy, "array", "O", list);
30-
31-
// Get the data type of the array
32-
PyObject* dtype = PyObject_GetAttrString(array, "dtype");
33-
const char* str = PyUnicode_AsUTF8(dtype);
34-
printf("Data type: %s\n", str);
35-
36-
// Get the shape of the array
37-
PyObject* shape = PyObject_GetAttrString(array, "shape");
38-
Py_ssize_t len = PyTuple_Size(shape);
39-
printf("Shape: (%ld", PyLong_AsLong(PyTuple_GetItem(shape, 0)));
40-
for (int i = 1; i < len; i++) {
41-
printf(", %ld", PyLong_AsLong(PyTuple_GetItem(shape, i)));
42-
}
43-
printf(")\n");
44-
45-
// Access the elements of the array
46-
// for (int i = 0; i < PyArray_SIZE(array); i++) {
47-
// printf("%ld ", PyLong_AsLong(PyArray_GetItem(array, i)));
48-
// }
49-
// printf("\n");
50-
51-
// Clean up
52-
Py_DECREF(array);
53-
Py_DECREF(numpy);
54-
Py_Finalize();
55-
56-
return EXIT_SUCCESS;
19+
// Initialize the Python interpreter
20+
Py_Initialize();
21+
22+
// Import the NumPy module
23+
PyObject* numpy = PyImport_ImportModule("numpy");
24+
25+
// Convert a Python list to a NumPy array
26+
PyObject* list = PyList_New(3);
27+
PyList_SetItem(list, 0, PyLong_FromLong(1));
28+
PyList_SetItem(list, 1, PyLong_FromLong(2));
29+
PyList_SetItem(list, 2, PyLong_FromLong(3));
30+
PyObject* array = PyObject_CallMethod(numpy, "array", "O", list);
31+
32+
// Get the data type of the array
33+
PyObject* dtype = PyObject_GetAttrString(array, "dtype");
34+
const char* str = PyUnicode_AsUTF8(dtype);
35+
printf("Data type: %s\n", str);
36+
37+
// Get the shape of the array
38+
PyObject* shape = PyObject_GetAttrString(array, "shape");
39+
Py_ssize_t len = PyTuple_Size(shape);
40+
printf("Shape: (%ld", PyLong_AsLong(PyTuple_GetItem(shape, 0)));
41+
for (int i = 1; i < len; i++) {
42+
printf(", %ld", PyLong_AsLong(PyTuple_GetItem(shape, i)));
43+
}
44+
printf(")\n");
45+
46+
// Access the elements of the array
47+
// for (int i = 0; i < PyArray_SIZE(array); i++) {
48+
// printf("%ld ", PyLong_AsLong(PyArray_GetItem(array, i)));
49+
// }
50+
// printf("\n");
51+
52+
// Clean up
53+
Py_DECREF(array);
54+
Py_DECREF(numpy);
55+
Py_Finalize();
56+
57+
return EXIT_SUCCESS;
5758
}

PyBind11/CMakeLists.txt

Lines changed: 10 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,10 @@
1+
cmake_minimum_required(VERSION 3.20)
2+
project(linear LANGUAGES CXX)
3+
4+
set(CMAKE_CXX_STANDARD 17)
5+
set(CMAKE_CXX_STANDARD_REQUIRED ON)
6+
7+
find_package(Python COMPONENTS Interpreter Development.Module REQUIRED)
8+
find_package(pybind11 CONFIG REQUIRED)
9+
10+
pybind11_add_module(linear "linear.cpp")

PyBind11/linear.cpp

Lines changed: 31 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,31 @@
1+
/**
2+
* @file main.cpp
3+
* @author Xuhua Huang
4+
* @brief
5+
* @version 0.1
6+
* @date 2026-01-16
7+
*
8+
* @copyright Copyright (c) 2026
9+
*
10+
*/
11+
12+
#include <pybind11/numpy.h>
13+
#include <pybind11/pybind11.h>
14+
15+
namespace py = pybind11;
16+
17+
// y = a*x + b for a 1D array
18+
py::array_t<double> axpb(py::array_t<double, py::array::c_style | py::array::forcecast> x, double a, double b) {
19+
auto xin = x.unchecked<1>();
20+
py::array_t<double> y(xin.shape(0));
21+
auto yout = y.mutable_unchecked<1>();
22+
23+
for (py::ssize_t i = 0; i < xin.shape(0); ++i) {
24+
yout(i) = a * xin(i) + b;
25+
}
26+
return y;
27+
}
28+
29+
PYBIND11_MODULE(linear, m) {
30+
m.def("axpb", &axpb, "Compute y = a*x + b for a 1D NumPy array");
31+
}

0 commit comments

Comments
 (0)