如何使用Ctypes将此numpy数组传递给C?

编程入门 行业动态 更新时间:2024-10-27 03:41:51
本文介绍了如何使用Ctypes将此numpy数组传递给C?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧! 问题描述

目前,我正在学习C类型.我的目标是产生一个numpypython中的数组A从500到4 * pi分500步.该数组传递给计算这些值的切线的C代码.C代码也将这些值传递回python中的numpy数组B.

Currently I'm learning about C types. My goal is to generate an numpy array A in python from 0 to 4*pi in 500 steps. That array is passed to C code which calculates the tangent of those values. The C code also passes those values back to an numpy array B in python.

昨天我只是尝试将一个值从python转换为C并(经过一些帮助)成功.今天,我尝试传递整个数组,而不是值.

Yesterday I tried simply to convert one value from python to C and (after some help) succeeded. Today I try to pass a whole array, not a value.

我认为向C库添加另一个函数是一个好主意,处理数组.新函数应该在循环中传递每个值将A转换为函数tan1()并将该值存储在数组B中.

I think it's an good idea to add another function to the C library to process the array. The new function should in a loop pass each value of A to the function tan1() and store that value in array B.

我有两个问题:

  • 编写处理numpy数组A的函数
  • 在python和C代码之间传递numpy数组.

我阅读了以下信息:

  • nenadmarkus/p/numpy-to-native/
  • 如何将NumPy数组与ctypes一起使用?

有帮助,但我仍然不知道如何解决我的问题.

Helpful, but I still don't know how to solve my problem.

C代码(仅显示相关的部分):

C code (Only the piece that seems relevant):

double tan1(f) double f; { return sin1(f)/cos1(f); } void loop(double A, int n); { double *B; B = (double*) malloc(n * sizeof(double)); for(i=0; i<= n, i++) { B[i] = tan1(A[i]) } }

Python代码:

import numpy as np import ctypes A = np.array(np.linspace(0,4*np.pi,500), dtype=np.float64) testlib = ctypes.CDLL('./testlib.so') testlib.loop.argtypes = ctypes.c_double, testlib.loop.restype = ctypes.c_double #print(testlib.tan1(3))

我知道在这种情况下ctypes.c_double是错误的,但这就是我在1值版本中所拥有的,并且还不知道要替代什么.

I'm aware that ctypes.c_double is wrong in this context, but that is what I had in the 1 value version and don't know yet for what to substitute.

请问关于如何实现这一目标的一些反馈意见?

Could I please get some feedback on how to achieve this goal?

推荐答案

您需要返回动态分配的内存,例如将您的C代码更改为:

You need to return the dynamically allocated memory, e.g. change your C code to something like:

#include <math.h> #include <stdlib.h> #include <stdio.h> double tan1(double f) { return sin(f)/cos(f); } double *loop(double *arr, int n) { double *b = malloc(n * sizeof(double)); for(int i = 0; i < n; i++) { b[i] = tan(arr[i]); } return b; } void freeArray(double *b) { free(b); }

在Python方面,您必须声明参数并返回类型.正如其他人在评论中提到的那样,您还应该释放动态分配的内存.请注意,在C端,数组总是衰减为指针.因此,您需要一个附加参数来告诉您数组中元素的数量.

On the Python side you have to declare parameter and return types. As mentioned by others in comments, you should also free dynamically allocated memory. Note that on the C side, arrays always decay into pointers. Therefore, you need an additional parameter which tells you the number of elements in the array.

此外,如果您返回一个指向double的指针到Python页面,则必须指定数组的大小.使用 np.frombuffer ,您无需复制数据即可处理数据.

Also if you return a pointer to double to the Python page, you must specify the size of the array. With np.frombuffer you can work with the data without making a copy of it.

import numpy as np from ctypes import * testlib = ctypes.CDLL('./testlib.so') n = 500 dtype = np.float64 input_array = np.array(np.linspace(0, 4 * np.pi, n), dtype=dtype) input_ptr = input_array.ctypes.data_as(POINTER(c_double)) testlib.loop.argtypes = (POINTER(c_double), c_int) testlib.loop.restype = POINTER(c_double * n) testlib.freeArray.argtypes = POINTER(c_double * n), result_ptr = testlib.loop(input_ptr, n) result_array = np.frombuffer(result_ptr.contents) # ...do some processing for value in result_array: print(value) # free buffer testlib.freeArray(result_ptr)

更多推荐

如何使用Ctypes将此numpy数组传递给C?

本文发布于:2023-07-29 19:33:20,感谢您对本站的认可!
本文链接:https://www.elefans.com/category/jswz/34/1243449.html
版权声明:本站内容均来自互联网,仅供演示用,请勿用于商业和其他非法用途。如果侵犯了您的权益请与我们联系,我们将在24小时内删除。
本文标签:数组   将此   如何使用   Ctypes   numpy

发布评论

评论列表 (有 0 条评论)
草根站长

>www.elefans.com

编程频道|电子爱好者 - 技术资讯及电子产品介绍!