sizeof(complex[double])

1,196 views
Skip to first unread message

Nick Foti

unread,
Mar 6, 2014, 1:20:11 PM3/6/14
to cython...@googlegroups.com
The code `sizeof(complex[double])` does not seem to work and causes a CompilerCrash.  I instead need to instantiate a variable of type `complex[double]` on the stack, call it `z` and use `sizeof(z)`.  The resulting code works, but `sizeof(complex[double])` should probably work too.

Thanks.

Robert Bradshaw

unread,
Mar 6, 2014, 2:55:23 PM3/6/14
to cython...@googlegroups.com
Hmm... this might be due to the fact that complex is already a keyword
in Cython (e.g. to make and use complex doubles).
> --
>
> ---
> You received this message because you are subscribed to the Google Groups
> "cython-users" group.
> To unsubscribe from this group and stop receiving emails from it, send an
> email to cython-users...@googlegroups.com.
> For more options, visit https://groups.google.com/groups/opt_out.

Sturla Molden

unread,
Mar 6, 2014, 7:24:21 PM3/6/14
to cython...@googlegroups.com
Did you try sizeof(complex double) ?


Sturla

Nick Foti

unread,
Mar 11, 2014, 10:29:37 AM3/11/14
to cython...@googlegroups.com
It turns out that sizeof(double complex) works.  From some preliminary searches it looks like double complex and std::complex<double> have the same representations now, however, I'm not sure it's a good idea to make people mix the C and C++ versions.

In C, `complex` is not a keyword unless complex.h is included (it's not actually a keyword, it's a macro for _Complex).  To be consistent with C and how Cython handles C++ complex numbers, shouldn't Cython wrap complex.h in libc and the user can then say something like `from libc cimport complex` (or `from libc cimport "double complex"`)?  This way you only get complex numbers when you want them (like in C), and C++ users don't have to work around C complex numbers.

Thanks.

Nils Bruin

unread,
Mar 11, 2014, 12:20:08 PM3/11/14
to cython...@googlegroups.com
On Tuesday, March 11, 2014 7:29:37 AM UTC-7, Nick Foti wrote:
In C, `complex` is not a keyword unless complex.h is included (it's not actually a keyword, it's a macro for _Complex).  To be consistent with C and how Cython handles C++ complex numbers, shouldn't Cython wrap complex.h in libc and the user can then say something like `from libc cimport complex` (or `from libc cimport "double complex"`)?  This way you only get complex numbers when you want them (like in C), and C++ users don't have to work around C complex numbers.

I think the problem is more fundamental than that, see  http://docs.cython.org/src/reference/extension_types.html
Normally the "complex" type in cython refers to __builtin__.complex , which is a python type. I guess "double complex" gets parsed differently, so that this meaning of "complex" doesn't get in the way, but if you want to refer to a type "complex" from C++ you just have a name conflict. Perhaps something like

extern ctypedef cpp_complex "complex"

works? If you can manage to get C++ complex exposed in cython under a different name, sizeof(cpp_complex[double]) should do the trick.

Stefan Behnel

unread,
Mar 11, 2014, 12:53:47 PM3/11/14
to cython...@googlegroups.com
Nick Foti, 06.03.2014 19:20:
Your pull request that declares std::complex in Cython seems like the right
way to go.

https://github.com/cython/cython/pull/278

That being said, the compiler shouldn't crash on this code.

Stefan

Robert Bradshaw

unread,
Mar 11, 2014, 4:49:39 PM3/11/14
to cython...@googlegroups.com
On Tue, Mar 11, 2014 at 7:29 AM, Nick Foti <nfo...@gmail.com> wrote:
> It turns out that sizeof(double complex) works. From some preliminary
> searches it looks like double complex and std::complex<double> have the same
> representations now, however, I'm not sure it's a good idea to make people
> mix the C and C++ versions.

When compiling as C++, double complex is a std::complex<double>.

There are three implementations of complex numbers, Cython's internal
implementation (used if you don't include complex.h, e.g. for non c99
compilers), C99 _Complex (if you include complex.h) and std::complex
(used if you're compiling in C++).

> In C, `complex` is not a keyword unless complex.h is included (it's not
> actually a keyword, it's a macro for _Complex). To be consistent with C and
> how Cython handles C++ complex numbers, shouldn't Cython wrap complex.h in
> libc and the user can then say something like `from libc cimport complex`
> (or `from libc cimport "double complex"`)? This way you only get complex
> numbers when you want them (like in C), and C++ users don't have to work
> around C complex numbers.

Cython is not necessarily trying to be like C. If anything, it's
trying to be like Python. Python has a builtin complex type, so we
wanted to support this in Cython efficiently as well. In particular,
for numpy/scientific users, we want to make it really easy to use
complex numbers in a portable way.

If you want to use std::complex<double> numbers in C++, just use
"double complex." I'm not sure how this gets in the way.

> On Thursday, March 6, 2014 7:24:21 PM UTC-5, Sturla Molden wrote:
>>
>> Nick Foti <nfo...@gmail.com> wrote:
>> > The code `sizeof(complex[double])` does not seem to work and causes a
>> > CompilerCrash. I instead need to instantiate a variable of type
>> > `complex[double]` on the stack, call it `z` and use `sizeof(z)`. The
>> > resulting code works, but `sizeof(complex[double])` should probably work
>> > too.
>>
>> Did you try sizeof(complex double) ?
>>
>>
>> Sturla
>>
> --
>
> ---
> You received this message because you are subscribed to the Google Groups
> "cython-users" group.
> To unsubscribe from this group and stop receiving emails from it, send an
> email to cython-users...@googlegroups.com.
> For more options, visit https://groups.google.com/d/optout.

Nick Foti

unread,
Mar 17, 2014, 11:17:48 AM3/17/14
to cython...@googlegroups.com
Using `double complex` gets in the way because the rest of the STL is accessed as `list[T]`, for example.  There is no reason that `complex[T]` should be an exception.  I can use `complex[double]` everywhere except in `sizeof` statements, why do I need to use special syntax for one case when `complex[double]` is a valid type?  Handling this case should be added to the parser.

I get that Cython is trying to be like Python, but a major use of Cython is wrapping libraries, and C++ libraries will be using `std::complex<double>` which the Cython documentation says should be accessed like `complex[double]`.  Consistency of the types and interface is important, the user is already writing in three languages (C++, Cython, and Python), and having consistent types will make their lives easier.

It appears that how Cython handles complex numbers and the developers' intentions need to be documented more clearly.

Sturla Molden

unread,
Mar 23, 2014, 5:30:25 AM3/23/14
to cython...@googlegroups.com

As noted, complex is a Cython keyword and a built-in Python function.

complex[double] should always be an error, as I see it.

std.complex[double] should be ok, OTOH.


Sturla
> > email to cython-users...@googlegroups.com <javascript:>.
> > For more options, visit https://groups.google.com/d/optout
> <https://groups.google.com/d/optout>.
>
> --
>
> ---
> You received this message because you are subscribed to the Google
> Groups "cython-users" group.
> To unsubscribe from this group and stop receiving emails from it, send
> an email to cython-users...@googlegroups.com
> <mailto:cython-users...@googlegroups.com>.

Sturla Molden

unread,
Mar 23, 2014, 6:05:14 AM3/23/14
to cython...@googlegroups.com
On 11/03/14 17:53, Stefan Behnel wrote:

> Your pull request that declares std::complex in Cython seems like the right
> way to go.
>
> https://github.com/cython/cython/pull/278

Indeed, std.complex[T] as wrapper for std::complex<T> should be fine.

complex[T] should not be expected to work without a .pyd file wrapping
the C++ std::complex class.


Sturla


Reply all
Reply to author
Forward
0 new messages