Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Invalid C generated for some numpy complex number multiplications: typedef types with _Complex keyword #1404

Open
robertwb opened this issue May 13, 2015 · 4 comments

Comments

@robertwb
Copy link
Contributor

This bug is closely related to ticket http://trac.cython.org/ticket/386, however it is slightly different in that it reports a realistic use case that in my opinion would qualify it as a bug rather than an enhancement request. Hence I've raised a new ticket, but I'm new round here - maybe it should be merged.

The discovery of the bug stems from answering a question on Stack Overflow : http://stackoverflow.com/questions/30054019/complex-numbers-in-cython. It affects numpy multiplication of complex numbers by real valued floats

In its most concise form - this code produces C that will not compile:

cimport numpy as np

cdef extern from "complex.h":
    pass

cdef:
    np.complex128_t varc128 = 1j
    np.float64_t varf64 = 1.

varc128 = varc128 * varf64

whereas the same code produces error free C code with only order of multiplicands reversed. (i.e. substitute varc128 = varf64 * varc128 for varc128 = varc128 * varf64. This appears to be because there are different routes for achieving the multiplication depending on the order of operands, in the first (failing) example it is done via __pyx_t_npy_float64_complex types, whereas in the latter __pyx_t_npy_double_complex

As far as I could investigate - the offending line of generated C code fails to compile because only float, double or long double are allowed before _Complex (http://www.gnu.org/software/gnu-c-manual/gnu-c-manual.html#Standard-Complex-Number-Types).

The offending line of C code seems to be generated by the code here:

typedef %(real_type)s _Complex %(type_name)s;

We can see here that the offending type is basically an alias for double

ctypedef double npy_float64

I haven't got a full, generalisable fix / patch as I'm totally new to the codebase, but ''possibly'' the code here should check whether it's valid to append the _Complex and, if not, search to see whether the type can be resolved to an allowable base type.

type = PyrexTypes.CComplexType(type)

Migrated from http://trac.cython.org/ticket/850

@pums974
Copy link

pums974 commented Apr 12, 2019

This bug is still present with cython 0.29.6
Is there an easy workaround ?

@xaverm
Copy link

xaverm commented Jul 6, 2019

This bug is still around in Cython version 0.29.11 (July 2019)
It makes development combining complex functions from complex.h with complex numbered numpy arrays completely ill-defined.
PLS fix.

@jakecolston
Copy link

@pums974 did you manage to find a suitable workaround?
Still present in 0.29.13

@pums974
Copy link

pums974 commented Oct 26, 2019

Nope, sorry

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Projects
None yet
Development

No branches or pull requests

5 participants