Skip to content

SimpleArrayComplex128[i] = numpy.complex128 fails with cast error #604

@ThreeMonth03

Description

@ThreeMonth03

How to reproduce

import modmesh as mm
import numpy as np

nc = np.array([1.0 + 0.1j])
mc = mm.SimpleArrayComplex128([1])
mc[0] = nc[0]          # <- fails

print(mc[0].real, mc[0].imag)

Actual

Traceback (most recent call last):
  ...
  mc[0] = nc[0]
RuntimeError: Unable to cast Python instance of type <class 'numpy.complex128'> to C++ type '?' 
(#define PYBIND11_DETAILED_ERROR_MESSAGES or compile in debug mode for details)

Expected

mc[0] should accept a numpy.complex128 (and Python complex) and store it as modmesh::Complex<double> without extra wrapping.

Notes

  • If we add additional initializer in WrapComplex.cpp:
           .def(
               py::init(
                   [](const std::complex<value_type> & c)
                   { return std::make_shared<wrapped_type>(wrapped_type{c.real(), c.imag()}); }),
               py::arg("np_complex"))

Constructing mm.complex128 from a Python/numpy complex could work, but assigning an element to SimpleArrayComplex128 doesn't work.

  mc = mm.complex128(np.complex128(1+0.1j))  # ok
  print(mc.real, mc.imag) # 1 0.1j
  ma = mm.SimpleArrayComplex128([1]) #ok  
  mc[0] = np.complex128(1+0.1j) # not ok

Metadata

Metadata

Assignees

No one assigned

    Labels

    arrayMulti-dimensional array implementation

    Type

    Projects

    No projects

    Milestone

    No milestone

    Relationships

    None yet

    Development

    No branches or pull requests

    Issue actions