Skip to content

Instantly share code, notes, and snippets.

@habnabit
Last active August 23, 2020 08:42
Show Gist options
  • Save habnabit/dfffd795a9d5e37d1cff4925a49733d1 to your computer and use it in GitHub Desktop.
Save habnabit/dfffd795a9d5e37d1cff4925a49733d1 to your computer and use it in GitHub Desktop.
cffi with numpy example
[2.99616175+5.54885256j 2.50767967+4.65893257j 4.13392399+1.69048949j
0.93860821+1.46470066j 5.6235234 +2.36071585j 2.28013476+4.78671259j
3.34736328+4.25295228j 5.7851614 +5.63424175j 1.90595192+2.16040048j
1.62844748+0.06384722j 0.89949666+4.65337538j 1.95680565+2.67681418j
5.78686059+5.3124304j 1.9592114 +0.16909678j 2.01095818+3.03112179j
4.20331404+3.13157214j 0.05917195+2.77188645j 5.47970406+1.40063894j
2.05722142+2.48133127j 4.59507957+5.93884264j 0.17166239+4.79953363j
5.44861648+1.22816477j 3.39911951+2.21297174j 0.01094132+0.19348674j
0.82714832+1.73206116j]
[0.+0.j 0.+0.j 0.+0.j 0.+0.j 0.+0.j 0.+0.j 0.+0.j 0.+0.j 0.+0.j 0.+0.j
0.+0.j 0.+0.j 0.+0.j 0.+0.j 0.+0.j 0.+0.j 0.+0.j 0.+0.j 0.+0.j 0.+0.j
0.+0.j 0.+0.j 0.+0.j 0.+0.j 0.+0.j]
[-1.27117005e+02-1.86176212e+01j -4.25147579e+01-3.12480509e+01j
-1.53266429e+00+2.19277272e+00j 1.34650840e+00-1.65183359e+00j
4.22475741e+00+3.21874284e+00j -3.90521940e+01-4.54889535e+01j
-3.44216410e+01+7.18174722e+00j 1.22928158e+02+6.68391763e+01j
-1.44556821e+00-4.04154175e+00j -5.77367066e-02-6.37844654e-02j
3.26390856e+01-4.10804605e+01j -2.74981776e+00-6.70260361e+00j
8.91858454e+01+4.82953437e+01j -3.84149474e-01-1.57247733e-01j
-4.42463038e+00-9.35089196e+00j -5.59344896e+00+9.98338633e+00j
8.01160963e+00-4.70917936e-01j 1.49400986e+00+1.37166217e+00j
-2.81434904e+00-5.24816217e+00j -2.22083313e+01+1.88442314e+02j
5.98383833e+01-1.03726954e+01j 1.24488406e+00+1.15675908e+00j
-4.47355460e+00+1.15036621e+00j 1.01871605e+00-2.13019111e-03j
1.97312214e+00-2.01494772e+00j]
[-1.27117005e+02-1.86176212e+01j -4.25147579e+01-3.12480509e+01j
-1.53266429e+00+2.19277272e+00j 1.34650840e+00-1.65183359e+00j
4.22475741e+00+3.21874284e+00j -3.90521940e+01-4.54889535e+01j
-3.44216410e+01+7.18174722e+00j 1.22928158e+02+6.68391763e+01j
-1.44556821e+00-4.04154175e+00j -5.77367066e-02-6.37844654e-02j
3.26390856e+01-4.10804605e+01j -2.74981776e+00-6.70260361e+00j
8.91858454e+01+4.82953437e+01j -3.84149474e-01-1.57247733e-01j
-4.42463038e+00-9.35089196e+00j -5.59344896e+00+9.98338633e+00j
8.01160963e+00-4.70917936e-01j 1.49400986e+00+1.37166217e+00j
-2.81434904e+00-5.24816217e+00j -2.22083313e+01+1.88442314e+02j
5.98383833e+01-1.03726954e+01j 1.24488406e+00+1.15675908e+00j
-4.47355460e+00+1.15036621e+00j 1.01871605e+00-2.13019111e-03j
1.97312214e+00-2.01494772e+00j]
from cffi import FFI
ffibuilder = FFI()
ffibuilder.set_source("_example", """
#include <complex.h>
void
mangle(size_t n, double complex* a_in, double complex* a_out)
{
for (size_t i = 0; i < n; ++i) {
a_out[i] = ccos(a_in[i]);
}
}
""", libraries=[])
ffibuilder.cdef("""
// pycparser supports the C99 _Complex but not the 'complex' shorthand. this
// could also just be void* since it's all using numpy anyway.
void
mangle(size_t n, double _Complex* a_in, double _Complex* a_out);
""")
ffibuilder.compile(verbose=True)
import math, numpy.random
from _example import ffi, lib
a = (
numpy.random.uniform(0, math.tau, 25)
+ 1j * numpy.random.uniform(0, math.tau, 25)
).astype(numpy.cdouble)
b = numpy.zeros_like(a)
print(a)
print(b)
def for_cffi(arr):
return ffi.cast('double _Complex*', ffi.from_buffer(arr))
lib.mangle(len(a), for_cffi(a), for_cffi(b))
print(b)
print(numpy.cos(a))
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment