|
| 1 | +// clang-format off |
1 | 2 | /*****************************************************************//** |
2 | 3 | * \file main.cpp |
3 | 4 | * \brief |
4 | 5 | * |
5 | 6 | * \author Xuhua Huang |
6 | 7 | * \date January 07, 2023 |
7 | 8 | *********************************************************************/ |
| 9 | +// clang-format on |
8 | 10 |
|
9 | 11 | #define NPY_NO_DEPRECATED_API NPY_1_7_API_VERSION |
10 | 12 |
|
11 | 13 | #include <Python.h> |
12 | 14 | #include <numpy/arrayobject.h> |
13 | 15 |
|
14 | 16 | #include <cstdlib> |
15 | | -#include <iostream> |
16 | 17 |
|
17 | 18 | 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; |
57 | 58 | } |
0 commit comments