- Notifications
You must be signed in to change notification settings - Fork752
Open
Labels
Description
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 thehttps://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
andclr_runtime.json
files in the attached zip file.I ran ipython in the directory containing
Complex
and ran the following commands:
importComplextest=Complex.ComplexNS().Test()test.makeComplex(1.0,1.0)# test 1test.convertComplex(1j)# test 2-Theresultoftest.makeComplex(1.0,1.0)was<System.Numerics.Complexobjectat0x000001A8F1C03380>,whereasIwasexpecting1+1j-Thetracebackis-In [5]:test.convertComplex(1j)---------------------------------------------------------------------------PythonExceptionTraceback (mostrecentcalllast)PythonException:'complex'valuecannotbeconvertedtoSystem.Numerics.ComplexTheaboveexceptionwasthedirectcauseofthefollowingexception:ArgumentExceptionTraceback (mostrecentcalllast)ArgumentException:'complex'valuecannotbeconvertedtoSystem.Numerics.ComplexinmethodSystem.StringconvertComplex(System.Numerics.Complex)--->Python.Runtime.PythonException:'complex'valuecannotbeconvertedtoSystem.Numerics.Complex---Endofinnerexceptionstacktrace---Theaboveexceptionwasthedirectcauseofthefollowingexception:AggregateExceptionTraceback (mostrecentcalllast)AggregateException:Oneormoreerrorsoccurred. ('complex'valuecannotbeconvertedtoSystem.Numerics.ComplexinmethodSystem.StringconvertComplex(System.Numerics.Complex))--->System.ArgumentException:'complex'valuecannotbeconvertedtoSystem.Numerics.ComplexinmethodSystem.StringconvertComplex(System.Numerics.Complex)--->Python.Runtime.PythonException:'complex'valuecannotbeconvertedtoSystem.Numerics.Complex---Endofinnerexceptionstacktrace------Endofinnerexceptionstacktrace---Theaboveexceptionwasthedirectcauseofthefollowingexception:TypeErrorTraceback (mostrecentcalllast)CellIn [5],line1---->1test.convertComplex(1j)[Complex.zip](https://github.com/pythonnet/pythonnet/files/11063123/Complex.zip)