ICode9

精准搜索请尝试: 精确搜索
首页 > 编程语言> 文章详细

如何将复数从python numpy传递给c(目前尝试使用SWIG)

2019-11-27 20:55:18  阅读:399  来源: 互联网

标签:swig c-3 python numpy


我想用python调用带有复数输入的c编写函数.我尝试使用SWIG生成包装器-但它似乎掉下来了.我认为我需要找出适合在numpy.i中使用的“宏”-但不确定是什么-任何人对此都有任何经验-或其他可以解决此问题的方法?

numpy.i在底部显示了它-尽管已被注释掉.我尝试使用这些宏-但是它们失败了,SWIG抱怨我尝试了以下宏扩展的语法错误:

%numpy_typemaps(complex float, NPY_CFLOAT , int)
%numpy_typemaps(complex double, NPY_CDOUBLE, int)
%numpy_typemaps(complex long double, NPY_CLONGDOUBLE, int)

这些是我的文件:

ComplexNumbers.c

# include <math.h>
# include <complex.h>


double complex returnX(double complex X) 
/*
    fresnel reflection coefficient rs
*/
{
    return X;
}

ComplexNumbers.i:

%{
#define SWIG_FILE_WITH_INIT
%}
%include "numpy.i"
%init %{
import_array();
%}


%module ComplexNumbers

%inline %{
extern double complex returnX(double complex X);
%}

Python:

#!/usr/bin/env python

"""
setup.py file for ComplexNumbers
"""

from distutils.core import setup
from distutils.extension import Extension

import numpy


ComplexNumbers_module = Extension('_ComplexNumbers',
                           sources=['ComplexNumbers_wrap.c', 
                                    'ComplexNumbers.c'],
                           include_dirs=[numpy.get_include()]
                           )

setup (name = 'ComplexNumbers',
       version = '1.0',
       author      = "JP Hadden jp.hadden@bristol.ac.uk",
       description = """Spectral Interfereometry functions""",
       ext_modules = [ComplexNumbers_module],
       py_modules = ["ComplexNumbers"],
       )

编译器输出错误

C:\MinGW32-xy\bin\gcc.exe -mno-cygwin -mdll -O -Wall -IC:\Python27\lib\site-pack
ages\numpy\core\include -IC:\Python27\include -IC:\Python27\PC -c ComplexNumbers
_wrap.c -o build\temp.win32-2.7\Release\complexnumbers_wrap.o
ComplexNumbers_wrap.c:2975:23: error: expected '=', ',', ';', 'asm' or '__attrib
ute__' before 'returnX'
ComplexNumbers_wrap.c: In function '_wrap_returnX':
ComplexNumbers_wrap.c:2982:18: error: expected '=', ',', ';', 'asm' or '__attrib
ute__' before 'arg1'
ComplexNumbers_wrap.c:2982:18: error: 'arg1' undeclared (first use in this funct
ion)
ComplexNumbers_wrap.c:2982:18: note: each undeclared identifier is reported only
 once for each function it appears in
ComplexNumbers_wrap.c:2986:18: error: expected '=', ',', ';', 'asm' or '__attrib
ute__' before 'result'
ComplexNumbers_wrap.c:2986:18: error: 'result' undeclared (first use in this fun
ction)
ComplexNumbers_wrap.c:2997:24: error: expected ')' before 'complex'
ComplexNumbers_wrap.c:2997:24: error: pointer value used where a floating point
value was expected
ComplexNumbers_wrap.c:2997:14: error: invalid type argument of unary '*' (have '
double')
ComplexNumbers_wrap.c:3000:20: error: expected ')' before 'complex'
ComplexNumbers_wrap.c:3000:20: warning: implicit declaration of function 'return
X'
ComplexNumbers_wrap.c:3001:15: error: expected ')' before 'complex'
ComplexNumbers_wrap.c:3001:15: error: expected ')' before 'complex'
ComplexNumbers_wrap.c:3001:15: error: expected ')' before 'complex'
ComplexNumbers_wrap.c:3001:15: error: pointer value used where a floating point
value was expected
ComplexNumbers_wrap.c:3001:15: error: expected ')' before 'complex'
ComplexNumbers_wrap.c:3001:15: error: incompatible type for argument 1 of 'memcp
y'
c:\mingw32-xy\bin\../lib/gcc/mingw32/4.5.2/../../../../include/string.h:38:40: n
ote: expected 'void *' but argument is of type 'double'
error: command 'gcc' failed with exit status 1

解决方法:

我不确定它如何与Numpy交互,但是SWIG当然包括对C99复杂类型的支持.我可以使用以下示例对此进行验证:

%module test
%{
#include <complex.h>
%}

%include <complex.i>

%inline %{
  double complex test(double complex X) {
    return X;
  }
%}

这使用了SWIG complex.i接口.

(请注意,这里%inline用于声明,定义和包装所有内容-方便进行测试,但是对于“真实”代码,我倾向于使用%include).

我在Linux上使用以下命令对此进行了编译:

swig -Wall -python test.i 
gcc -std=c99 -Wall -Wextra test_wrap.c -I/usr/include/python2.7 -shared -o _test.so

内置一个警告就很好.完成后,我可以执行以下操作:

LD_LIBRARY_PATH=.  python                       
Python 2.7.3 (default, Aug  1 2012, 05:16:07) 
[GCC 4.6.3] on linux2
Type "help", "copyright", "credits" or "license" for more information.
>>> import test
>>> test.test(complex(0,1))
1j
>>> test.test(complex(0,2))
2j
>>> test.test(complex(1,2))
(1+2j)
>>> 

似乎正在接受并返回本机Python复杂类型.

我还能够编译以下接口:

%{
#define SWIG_FILE_WITH_INIT
#include <complex.h>
%}
%include <complex.i>
%include <numpy.i>
%init %{
import_array();
%}

%numpy_typemaps(complex float, NPY_CFLOAT , int)
%numpy_typemaps(complex double, NPY_CDOUBLE, int)
%numpy_typemaps(complex long double, NPY_CLONGDOUBLE, int)

%module test

%inline %{
  double complex test(double complex X) {
    return X;
  }
%}

通过添加%include< complex.i&gt ;,和-std = c99到编译器标志.我认为您可以使用these options之类的东西为distutils设置它.

标签:swig,c-3,python,numpy
来源: https://codeday.me/bug/20191127/2077047.html

本站声明: 1. iCode9 技术分享网(下文简称本站)提供的所有内容,仅供技术学习、探讨和分享;
2. 关于本站的所有留言、评论、转载及引用,纯属内容发起人的个人观点,与本站观点和立场无关;
3. 关于本站的所有言论和文字,纯属内容发起人的个人观点,与本站观点和立场无关;
4. 本站文章均是网友提供,不完全保证技术分享内容的完整性、准确性、时效性、风险性和版权归属;如您发现该文章侵犯了您的权益,可联系我们第一时间进行删除;
5. 本站为非盈利性的个人网站,所有内容不会用来进行牟利,也不会利用任何形式的广告来间接获益,纯粹是为了广大技术爱好者提供技术内容和技术思想的分享性交流网站。

专注分享技术,共同学习,共同进步。侵权联系[81616952@qq.com]

Copyright (C)ICode9.com, All Rights Reserved.

ICode9版权所有