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.
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.
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.
I have two issues:
- writing the function that processes the numpy array A
- Passing the numpy array between python and C code.
I read the following info:
Helpful, but I still don't know how to solve my problem.
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 code:
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))
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?