Sign Up

Sign Up to our social questions and Answers Engine to ask questions, answer people’s questions, and connect with other people.

Have an account? Sign In

Have an account? Sign In Now

Sign In

Login to our social questions & Answers Engine to ask questions answer people’s questions & connect with other people.

Sign Up Here

Forgot Password?

Don't have account, Sign Up Here

Forgot Password

Lost your password? Please enter your email address. You will receive a link and will create a new password via email.

Have an account? Sign In Now

You must login to ask a question.

Forgot Password?

Need An Account, Sign Up Here

Please briefly explain why you feel this question should be reported.

Please briefly explain why you feel this answer should be reported.

Please briefly explain why you feel this user should be reported.

Sign InSign Up

The Archive Base

The Archive Base Logo The Archive Base Logo

The Archive Base Navigation

  • SEARCH
  • Home
  • About Us
  • Blog
  • Contact Us
Search
Ask A Question

Mobile menu

Close
Ask a Question
  • Home
  • Add group
  • Groups page
  • Feed
  • User Profile
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Buy Points
  • Users
  • Help
  • Buy Theme
  • SEARCH
Home/ Questions/Q 8354011
In Process

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: June 9, 20262026-06-09T09:25:43+00:00 2026-06-09T09:25:43+00:00

I want to call c written functions with complex numbered inputs and in python.

  • 0

I want to call c written functions with complex numbered inputs and in python. I’ve tried using SWIG to generate a wrapper – but it seems to fall down. I think i need to work out the proper ‘macro’ for use in numpy.i – but not sure what it is – anyone have any experience in this – or other ways I can work around this?

The numpy.i shows this at the bottom – although it has been commented out. I tried using these macros – but they fail, SWIG complains about a syntax error for the following macro expansions I tried:

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

These are my files:

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"],
       )

Error output from compiler

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
  • 1 1 Answer
  • 0 Views
  • 0 Followers
  • 0
Share
  • Facebook
  • Report

Leave an answer
Cancel reply

You must login to add an answer.

Forgot Password?

Need An Account, Sign Up Here

1 Answer

  • Voted
  • Oldest
  • Recent
  • Random
  1. Editorial Team
    Editorial Team
    2026-06-09T09:25:44+00:00Added an answer on June 9, 2026 at 9:25 am

    I’m not sure how this interacts with Numpy, but SWIG certainly includes support for C99’s complex types. I was able to verify this with the following example:

    %module test
    %{
    #include <complex.h>
    %}
    
    %include <complex.i>
    
    %inline %{
      double complex test(double complex X) {
        return X;
      }
    %}
    

    This uses the SWIG complex.i interface.

    (Note that here %inline is used to declare, define and wrap all in one place – handy for testing, but for ‘real’ code I tend to use %include).

    I compiled this (on Linux) with:

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

    Which built fine with only one warning. Once that was done I could then do:

    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)
    >>> 
    

    Which seems to be accepting and returning native Python complex types.

    I was also able to make the following interface compile:

    %{
    #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;
      }
    %}
    

    With the addition of %include <complex.i> and -std=c99 to the compiler flags. I think you can make distutils set that for you using something like these options.

    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

I have some cpp file where my functions are written. I want to call
I want to call a webservice using google closures, via jsonp since i am
i want to call the command prompt command using Process.Start and then using StandardOutput
I have written a set of Matlab functions and I want to distribute it
So I want to test one of my Functions in my Web Project, but
I want to call a rest service written in WCF (which can support both
I want to have a function written in PHP which can create anonymous functions
i have written a lot of javascript functions that i want to use in
I want to test a mutex semaphore written in Erlang. The exported functions are:
I have a rather complex decorator written by someone else. What I want to

Explore

  • Home
  • Add group
  • Groups page
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Users
  • Help
  • SEARCH

Footer

© 2021 The Archive Base. All Rights Reserved
With Love by The Archive Base

Insert/edit link

Enter the destination URL

Or link to existing content

    No search term specified. Showing recent items. Search or use up and down arrow keys to select an item.