Convert Python complex values in symbolic interface expressions - #42618
Convert Python complex values in symbolic interface expressions#42618cxzhong wants to merge 3 commits into
Conversation
|
Documentation preview for this PR (built with commit 94b6cb0; changes) is ready! 🎉 |
|
@cxzhong The fix converts Python complex through CDF before serializing to Maxima. But CDF uses double precision (53-bit). Since this is a symbolic operation, wouldn't it be more semantically correct to use CC (which is also 53-bit but respects Sage's precision model) or even SR's own complex conversion to avoid any potential precision loss when the complex values come from arbitrary-precision contexts? Also, what happens if someone passes complex(1e-400, 1e-400)—does CDF underflow before Maxima sees it? |
|
@vivek41-glitch Thanks for checking this. This branch only handles built-in Python Similarly, For these reasons, I think |
|
That make sense and also could u plss tell me whether the CI faillings r related to this file ? And one suggestions from my side is given that the fix routes through CDF's maxima_init(), would it be worth adding a doctest that explicitly shows SR(complex(1,2)).maxima() returns the expected %i format? That would make the behavior explicit for future developers and catch any regressions. |
|
@vivek41-glitch I checked both failing jobs. Neither is related to this PR:
In that failing macOS job, Regarding the additional doctest, the existing regression test already constructs |
|
@vivek41-glitch So do you agree this? If you agree this, I can change the label |
|
Yes, I agree—your reasoning is sound. The existing doctest covers the serializer path, and the CI failures are clearly unrelated to this PR. Thanks for checking the logs and explaining the rationale. |
|
the real brother of CDF is RDF. For consistency one might want Python reals to go to RDF, and Python complex numbers to go to CDF |
Fixes #42608.
What changed
complexvalues wrapped in symbolic expressions throughCDFbefore generating interface input.Why
NumPy converts complex scalars to built-in Python
complexobjects when a complex array becomes an object array during symbolic multiplication.InterfaceInit.pyobject()previously fell back torepr(), which emitted Python'sjnotation. Maxima cannot parse that notation, so symbolic matrix inversion failed while testing a pivot.Converting the value through
CDFreuses the existing interface-specific complex-number serialization, including Maxima's%inotation.User impact
Symbolic expressions containing Python complex values can now be passed safely to external interfaces. In particular,
matrix(x * numpy_array).inverse()no longer raises theMACSYMA-QUITerror for NumPy complex arrays.Testing
./sage -t src/sage/symbolic/expression_conversions.py(422 tests passed).venv/bin/ruff check src/sage/symbolic/expression_conversions.py.venv/bin/relint -c src/.relint.yml src/sage/symbolic/expression_conversions.py