pythonnet / pythonnet/pythonnet

Add support for complex type

未關閉
#2,136 1 則留言 0 個 reaction 已指派 0 人 在 GitHub 檢視

還沒有人認領這個 Issue。

proposal
主要語言
C#
星號
5.5k
分支
780
PR 合併指標
30 天內沒有已合併 PR

描述

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)

貢獻指南

開啟貢獻指南

從這裡開始

  1. 先讀完整個 Issue,再讀專案的貢獻指南。
  2. 在 Issue 下留言說明你要接手 —— 這能避免兩個人做同樣的事。
  3. Fork 儲存庫,在一個分支上完成修改。
  4. 送出 Pull Request,並在描述裡引用這個 Issue 編號。

研究方向

從附件 Complex.zip 以及 issue 中描述的 encoder/decoder 轉換路徑開始。驗證 PyComplex 和 System.Numerics.Complex 在兩個方向上是如何表示和轉換的;當 Python complex 值可以傳遞給 .NET 方法,並且 System.Numerics.Complex 值可以作為 Python complex 值返回時,即表示完成。

由索引模型根據 Issue 內容生成。

評估

技術堆疊
csharp, python
領域
backend-api-design
Issue 類型
功能
難度
4/5
預估耗時
3-5 天
活躍度
停滯
描述清晰度
基本清楚
新手友好度
35/100

把新 issue 寄到你的電子郵件信箱

精選適合新手參與的 GitHub issue 摘要。