numpy / numpy/numpy

BUG: clongdouble_t element access inconsistent with other complex types

Open
#20,273 4 comments 0 reactions 0 assignees View on GitHub

Nobody has claimed this yet.

00 - Bug
Dominant language
Python
Stars
32.8k
Forks
12.8k
Avg merge
1d 7h
Merged PRs (30d)
197

Description

Describe the issue:

There seems to be some inconsistency in the Numpy Cython complex _t definitions:

https://github.com/numpy/numpy/blob/main/numpy/__init__.cython-30.pxd

Note the difference between complex128_t and clongdouble_t:

ctypedef double complex complex128_t
...
ctypedef npy_clongdouble clongdouble_t

with previous:

    ctypedef struct npy_clongdouble:
        long double real
        long double imag

As a result of these definitions, when a complex array has type clongdouble_t, indexing for an element returns a struct, thence a dict with keys real and imag. This is also true of the alias complex256 (on a 64 bit system).
All other complex types generate the complex value itself, and not a dict.

Consider the following Cython file:

import numpy as np
cimport numpy as cnp


def one_long_double():
    cdef:
        cnp.ndarray[cnp.npy_longdouble] output

    output = np.array([1], dtype=np.longdouble)
    print(output[0])


def one_c128():
    cdef:
        cnp.ndarray[cnp.complex128_t] output

    output = np.array([1 + 1j], dtype=np.complex128)
    print(output[0])


def one_c256():
    cdef:
        cnp.ndarray[cnp.npy_complex256] output

    output = np.array([1 + 1j], dtype=np.complex256)
    print(output[0])


def one_clong_double():
    cdef:
        cnp.ndarray[cnp.clongdouble_t] output

    output = np.array([1 + 1j], dtype=np.clongdouble)
    print(output[0])


def one_long_double_complex():
    cdef:
        cnp.ndarray[long double complex] output

    output = np.array([1 + 1j], dtype=np.clongdouble)
    print(output[0])


print('long double')
one_long_double()
print('c128')
one_c128()
print('c256')
one_c256()
print('clongdouble')
one_clong_double()
print('long double complex')
one_long_double_complex()

Code at: https://github.com/matthew-brett/cyext

This gives output:

long double
1.0
c128
(1+1j)
c256
{'real': 1.0, 'imag': 1.0}
clongdouble
{'real': 1.0, 'imag': 1.0}
long double complex
(1+1j)

This seems confusing to me - that the <type>_t definitions are of different form for clongdouble than for the other complex types. Was it intended? Would it be reasonable to use:

ctypedef long double complex clongdouble_t

instead? (and for complex256_t)?

Reproduce the code example:
See above.
Error message:
None.
NumPy/Python version information:
1.21.3 3.9.7 (default, Oct 13 2021, 06:45:31) 
[Clang 13.0.0 (clang-1300.0.29.3)]

Contributor guide

Open the contributing guide

First steps

  1. Read the whole issue, then the project's contributing guide.
  2. Comment on the issue to say you are picking it up — it saves two people doing the same work.
  3. Fork the repository and make your change on a branch.
  4. Open a pull request that references the issue number.

Research direction

Start with numpy/init.cython-30.pxd and compare the complex128_t and clongdouble_t definitions, including the npy_clongdouble struct. Reproduce the issue using the Cython example in the report and compare indexed values for the listed complex types. Done means clongdouble_t and complex256 access behave consistently with the other complex types.

Written by the indexing model from the issue text.

Assessment

Tech stack
python
Domain
tooling
Issue type
Bug
Difficulty
3/5
Estimated time
1-2 days
Activity status
Stale
Clarity
Mostly clear
Newbie friendliness
40/100

Get new issues in your inbox

A short digest of beginner-friendly GitHub issues.