Call Fortran Dll from Python with ctypes (III)
Saturday, 20. May 2006, 08:20:11
Fortran的复数在C中以structure表示
在ctypes中构造一个结构很简单,只要继承Structure这个类就可以了,但是这个子类必须具有_fields_属性
对应的Fortran子程序。注意这里使用alias来任意指定输出函数的名字为'cmpx',如果没有alias,则输出的函数名为原函数的名字大写'TESTCOMPLEX',还是使用alias比较灵活一些。
- complex(4) --> struct complex4 { float real, imag; };
- complex(8) --> struct complex8 { double real, imag; };
在ctypes中构造一个结构很简单,只要继承Structure这个类就可以了,但是这个子类必须具有_fields_属性
# complex(4) structure
class Complex4(Structure):
_fields_ = [('real', c_float),
('imag', c_float)]
x = Complex4(c_float(2.0), c_float(3.1415926))
y = Complex4()
d.cmpx(byref(x), byref(y))
print 'x:', x.real, x.imag, 'y:', y.real, y.imag
对应的Fortran子程序。注意这里使用alias来任意指定输出函数的名字为'cmpx',如果没有alias,则输出的函数名为原函数的名字大写'TESTCOMPLEX',还是使用alias比较灵活一些。
subroutine testcomplex(x, y) ! Expose subroutine cmpx to users of this DLL ! !DEC$ ATTRIBUTES DLLEXPORT, ALIAS:'cmpx' :: testcomplex ! Variables complex:: x, y y = x + x end subroutine cmpx








How to use Quote function: