pythonnet icon indicating copy to clipboard operation
pythonnet copied to clipboard

Add support for complex type

Open jjwallman opened this issue 2 years ago • 1 comments

Environment

  • Pythonnet version: 3.0.1
  • Python version: 3.9.13
  • Operating System: Windows 10
  • .NET Runtime: Microsoft.AspNetCore.App 6.0.15

Details

  • I would like to be able to use complex numbers through pythonnet. To achieve this, encoders/decoders should translate between a PyComplex class based on the https://docs.python.org/3/c-api/complex.html#c.PyComplex_Type and the System.Numerics.Complex class.

  • I created a minimal class to create a System.Numerics.Complex from real and imaginary parts and return it to Python and to print a System.Numerics.Complex passed to a function (see attached source code).

  • I compiling the solution and copied the resulting dll into a Complex folder with the __init__.py and clr_runtime.json files in the attached zip file.

  • I ran ipython in the directory containing Complex and ran the following commands:

import Complex
test = Complex.ComplexNS().Test()
test.makeComplex(1.0, 1.0) # test 1
test.convertComplex(1j) # test 2

- The result of test.makeComplex(1.0, 1.0) was <System.Numerics.Complex object at 0x000001A8F1C03380>, whereas I was expecting 1 + 1j

-   The traceback is
- In [5]: test.convertComplex(1j)
---------------------------------------------------------------------------
PythonException                           Traceback (most recent call last)
PythonException: 'complex' value cannot be converted to System.Numerics.Complex

The above exception was the direct cause of the following exception:

ArgumentException                         Traceback (most recent call last)
ArgumentException: 'complex' value cannot be converted to System.Numerics.Complex in method System.String convertComplex(System.Numerics.Complex)
 ---> Python.Runtime.PythonException: 'complex' value cannot be converted to System.Numerics.Complex
   --- End of inner exception stack trace ---

The above exception was the direct cause of the following exception:

AggregateException                        Traceback (most recent call last)
AggregateException: One or more errors occurred. ('complex' value cannot be converted to System.Numerics.Complex in method System.String convertComplex(System.Numerics.Complex))
 ---> System.ArgumentException: 'complex' value cannot be converted to System.Numerics.Complex in method System.String convertComplex(System.Numerics.Complex)
 ---> Python.Runtime.PythonException: 'complex' value cannot be converted to System.Numerics.Complex
   --- End of inner exception stack trace ---
   --- End of inner exception stack trace ---

The above exception was the direct cause of the following exception:

TypeError                                 Traceback (most recent call last)
Cell In [5], line 1
----> 1 test.convertComplex(1j)

[Complex.zip](https://github.com/pythonnet/pythonnet/files/11063123/Complex.zip)

jjwallman avatar Mar 24 '23 14:03 jjwallman

You can create your own encoders and decoders. https://github.com/pythonnet/pythonnet/wiki/Codecs:-customizing-object-marshalling-between-.NET-and-Python

lostmsu avatar May 05 '23 04:05 lostmsu