pythonnet / pythonnet/pythonnet

Add support for complex type

Open
#2,136 1 comment 0 reactions 0 assignees View on GitHub

Nobody has claimed this yet.

proposal
Dominant language
C#
Stars
5.5k
Forks
780
PR merge metrics
No merged PRs in 30d

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 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)

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 the attached Complex.zip and the encoder/decoder conversion path described in the issue. Verify how PyComplex and System.Numerics.Complex are represented and converted in both directions; done means Python complex values can be passed to .NET methods and System.Numerics.Complex values are returned as Python complex values.

Written by the indexing model from the issue text.

Assessment

Tech stack
csharp, python
Domain
backend-api-design
Issue type
Feature
Difficulty
4/5
Estimated time
3-5 days
Activity status
Stale
Clarity
Mostly clear
Newbie friendliness
35/100

Get new issues in your inbox

A short digest of beginner-friendly GitHub issues.