Calling D and Mir functions for Python and NumPy
Shigeki Karita
shigekikarita at gmail.com
Tue May 1 23:19:13 UTC 2018
To call D functions from python, especially to support mir
ndslice and numpy ndarray, I'm making a new library
"mir-pybuffer". The pybuffer is python's official C struct to
support writing C extensions of numpy-like ndarray. The
mir-pybuffer generates functions automatically to convert
mir.ndslice.Slice from/into PyBuffer functions by UDA and mixin
as follows:
(D side)
import mir.ndslice : Slice, Contiguous;
import pybuffer : pybuffer, MixinPyBufferWrappers;
@pybuffer
void func1(Slice!(Contiguous, [2LU], double*) mat,
Slice!(Contiguous, [1LU], double*) vec, double a) {
mat[0][] += vec;
vec[] *= a;
}
mixin MixinPyBufferWrappers;
(Python side)
import ctypes
import numpy
import pybuffer
x = numpy.array([[0, 1, 2], [3, 4, 5]]).astype(numpy.float64)
y = numpy.array([0, 1, 2]).astype(numpy.float64)
lib = pybuffer.CDLL("./libyour-D-lib.so")
lib.func1(x, y, ctypes.c_double(2.0))
- example
https://github.com/ShigekiKarita/mir-pybuffer/tree/master/test
- DUB https://pypi.org/project/pybuffer/
- PyPI https://code.dlang.org/packages/mir-pybuffer
Enjoy extending your numpy scripts with D and Mir. Bug report and
pull request are welcome!
More information about the Digitalmars-d-announce
mailing list