Skip to content

fix(Mpolynomial_libsingular): support division over transcendental ex… #40076

New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Open
wants to merge 2 commits into
base: develop
Choose a base branch
from

Conversation

polykernel
Copy link

@polykernel polykernel commented May 9, 2025

…tension

Currently, dividing a Singular backed multivariate polynomial by a fractional field element(of the same type) is unsupported and triggers a conversion error on the Singular end indicating the denominator is nonconstant. This commit extracts the commit from Singular trunk which adds support for the use case1 to a patch to be applied on top of release 4.4.1.

fixes #39801

Relevant discussions on sage-devel: https://groups.google.com/g/sage-devel/c/Npn48ofmHF4

I have opted to not add doctests due to anticipated difficulty in coordinating adoption of the patch across different distribution channels. Notwithstanding this, I still think doctests should be added at some point to guard against regression. If this is important enough to warrant adding the relevant tests now, please let me know. Otherwise, I think the tests can be added after the next Singular release whenever that occurs.

This is my first time contributing to sage. It is possible I missed some details in the developer documentation and contributing guidelines. Please advise me if I am doing anything unidiomatic or incorrect.

📝 Checklist

  • The title is concise and informative.
  • The description explains in detail what this PR is about.
  • I have linked a relevant issue or discussion.
  • I have created tests covering the changes.
  • I have updated the documentation and checked the documentation preview.

⌛ Dependencies

#40033 - potential compatibility issues/breakage with Singular release 4.4.1 should be sorted out first

Footnotes

  1. https://github.com/Singular/Singular/commit/0a4bc0e2888e660d1a5229a7b4842539a46c912a

@polykernel polykernel force-pushed the fix/mpolynomial_libsingular-division-over-transext branch from 43fadd2 to c1cc3c7 Compare May 9, 2025 04:42
@fchapoton
Copy link
Contributor

you need to add a doctest showing what has been fixed

@polykernel polykernel force-pushed the fix/mpolynomial_libsingular-division-over-transext branch from c1cc3c7 to 161c2a9 Compare May 9, 2025 12:47
@dimpase
Copy link
Member

dimpase commented May 9, 2025

CI fails (e.g. https://github.com/sagemath/sage/actions/runs/14929279618/job/41946713381?pr=40076) with

sage -t --warn-long 5.0 --random-seed=12863317776592486881340390273080966829 src/sage/rings/polynomial/multi_polynomial_libsingular.pyx
**********************************************************************
Error: Failed example:: Exception raised:
Traceback (most recent call last):
  File "/Users/runner/miniconda3/envs/sage-dev/lib/python3.11/site-packages/sage/doctest/forker.py", line 730, in _run
    self.compile_and_execute(example, compiler, test.globs)
  File "/Users/runner/miniconda3/envs/sage-dev/lib/python3.11/site-packages/sage/doctest/forker.py", line 1154, in compile_and_execute
    exec(compiled, globs)
  File "<doctest sage.rings.polynomial.multi_polynomial_libsingular.MPolynomial_libsingular._div_[29]>", line 1, in <module>
    f / d
    ~~^~~
  File "element.pyx", line 1736, in sage.structure.element.Element.__truediv__
  File "coerce.pyx", line 1238, in sage.structure.coerce.CoercionModel.bin_op
  File "element.pyx", line 1734, in sage.structure.element.Element.__truediv__
  File "multi_polynomial_libsingular.pyx", line 2364, in sage.rings.polynomial.multi_polynomial_libsingular.MPolynomial_libsingular._div_
  File "parent.pyx", line 902, in sage.structure.parent.Parent.__call__
  File "coerce_maps.pyx", line 183, in sage.structure.coerce_maps.DefaultConvertMap_unique._call_with_args
  File "coerce_maps.pyx", line 176, in sage.structure.coerce_maps.DefaultConvertMap_unique._call_with_args
  File "/Users/runner/miniconda3/envs/sage-dev/lib/python3.11/site-packages/sage/rings/fraction_field.py", line 783, in _element_constructor_
    return self._element_class(self, x, y, coerce=coerce)
           ^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^
  File "fraction_field_element.pyx", line 130, in sage.rings.fraction_field_element.FractionFieldElement.__init__
ZeroDivisionError: fraction field element division by zero

    f / d
Exception raised:
    Traceback (most recent call last):
      File "/Users/runner/miniconda3/envs/sage-dev/lib/python3.11/site-packages/sage/doctest/forker.py", line 730, in _run
        self.compile_and_execute(example, compiler, test.globs)
      File "/Users/runner/miniconda3/envs/sage-dev/lib/python3.11/site-packages/sage/doctest/forker.py", line 1154, in compile_and_execute
        exec(compiled, globs)
      File "<doctest sage.rings.polynomial.multi_polynomial_libsingular.MPolynomial_libsingular._div_[29]>", line 1, in <module>
        f / d
        ~~^~~
      File "element.pyx", line 1736, in sage.structure.element.Element.__truediv__
      File "coerce.pyx", line 1238, in sage.structure.coerce.CoercionModel.bin_op
      File "element.pyx", line 1734, in sage.structure.element.Element.__truediv__
      File "multi_polynomial_libsingular.pyx", line 2364, in sage.rings.polynomial.multi_polynomial_libsingular.MPolynomial_libsingular._div_
      File "parent.pyx", line 902, in sage.structure.parent.Parent.__call__
      File "coerce_maps.pyx", line 183, in sage.structure.coerce_maps.DefaultConvertMap_unique._call_with_args
      File "coerce_maps.pyx", line 176, in sage.structure.coerce_maps.DefaultConvertMap_unique._call_with_args
      File "/Users/runner/miniconda3/envs/sage-dev/lib/python3.11/site-packages/sage/rings/fraction_field.py", line 783, in _element_constructor_
        return self._element_class(self, x, y, coerce=coerce)
               ^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^
      File "fraction_field_element.pyx", line 130, in sage.rings.fraction_field_element.FractionFieldElement.__init__
    ZeroDivisionError: fraction field element division by zero
**********************************************************************
Error: Failed example:: Got: 0
                                                                                                                                                                                                                                                                                                                                                                                                                                                                                          
    f // g
Expected:
    1/a
Got:
    0
**********************************************************************
Error: Failed example:: Got: doctest:warning
  File "<doctest sage.rings.polynomial.multi_polynomial_libsingular.MPolynomial_libsingular.factor[2]>", line 1, in <module>
    F = f.factor(); F
  File "/Users/runner/miniconda3/envs/sage-dev/lib/python3.11/warnings.py", line 110, in _showwarnmsg
    sw(msg.message, msg.category, msg.filename, msg.lineno,
:
UserWarning: error in Singular ignored: 
x * (x^2 + x + 1) * (x^2 + 2*y^2)
                                                            
    F = f.factor(); F
Expected:
    x * (x^2 + x + 1) * (x^2 + 2*y^2)
Got:
    doctest:warning
      File "<doctest sage.rings.polynomial.multi_polynomial_libsingular.MPolynomial_libsingular.factor[2]>", line 1, in <module>
        F = f.factor(); F
      File "/Users/runner/miniconda3/envs/sage-dev/lib/python3.11/warnings.py", line 110, in _showwarnmsg
        sw(msg.message, msg.category, msg.filename, msg.lineno,
    :
    UserWarning: error in Singular ignored: 
    x * (x^2 + x + 1) * (x^2 + 2*y^2)
**********************************************************************
3 items had failures:
   1 of  31 in sage.rings.polynomial.multi_polynomial_libsingular.MPolynomial_libsingular._div_
   1 of  36 in sage.rings.polynomial.multi_polynomial_libsingular.MPolynomial_libsingular._floordiv_
   1 of 135 in sage.rings.polynomial.multi_polynomial_libsingular.MPolynomial_libsingular.factor
    [1352 tests, 3 failures, 30.77s wall]

@polykernel
Copy link
Author

The first two CI failures (for _div_ and _floordiv_) appears to be caused by meson using Singular 4.4.1 from conda-forge instead of building from source with the patch. I don't know how to fix this part as it doesn't seem like the meson build system is hooked into build/pkgs at all and there doesn't seem to be any precedence with configuring subprojects and wraps.

I am not sure about the cause of the third error on factor.

@polykernel polykernel force-pushed the fix/mpolynomial_libsingular-division-over-transext branch from 10698a2 to 55259bf Compare May 9, 2025 21:33
@dimpase
Copy link
Member

dimpase commented May 12, 2025

We now have Singular 4.4.1 in the develop branch, so you'll need to update this.

@polykernel polykernel force-pushed the fix/mpolynomial_libsingular-division-over-transext branch from 55259bf to d866b47 Compare May 12, 2025 13:13
@polykernel
Copy link
Author

polykernel commented May 12, 2025

Thanks for the heads-up, I have rebased on top of the develop branch and squashed the fixup commit.

…tension

Currently, dividing a Singular backed multivariate polynomial by a fractional
field element(of the same type) is unsupported and triggers a conversion error
on the Singular end indicating the denominator is nonconstant. This commit
extracts the commit from Singular trunk which adds support for the use case[^1]
to a patch to be applied on top of release 4.4.1. We force an in-source build
of Singular if the system Singular package does not contain the patch.

[^1]: Singular/Singular@0a4bc0e
@polykernel polykernel force-pushed the fix/mpolynomial_libsingular-division-over-transext branch from d866b47 to fb8d1c5 Compare May 23, 2025 01:28
@polykernel
Copy link
Author

There still seems to be some mismatches with sonames. I am not very familiar with the sage build system, any pointers on how to resolve these would be greatly appreciated.

@dimpase
Copy link
Member

dimpase commented May 23, 2025

do you mean, in CI logs?
That's a current "feature" of our CI : 😢

@polykernel
Copy link
Author

I see. I have tested the changes locally and addressed the previous comments. There is one doctest failure I got when testing the changes to multi_polynomial_libsingular.pyx.

File "src/sage/rings/polynomial/multi_polynomial_libsingular.pyx", line 4202, in sage.rings.polynomial.multi_polynomial_libsingular.MPolynomial_libsingular._floordiv_
Failed example:
    ((x+y)^3+x+z)//(x+y)
Expected:
    x^2 + 2*x*y + y^2
Got:
    x^2 + 2*x*y + y^2 + 1
**********************************************************************
1 item had failures:
   1 of  36 in sage.rings.polynomial.multi_polynomial_libsingular.MPolynomial_libsingular._floordiv_
    [1352 tests, 1 failure, 19.72s wall]

I am not sure why the expected result is $x^2 + 2xy + y^2$. Under the default graded reverse lexicographic ordering, the quotient is indeed $x^2 + 2xy + y^2 + 1$ (verified with Macaulay2). I did a quick ./sage -br against the develop branch and the testcase also fails there so I don't think this is a regression caused by this PR. In any case, it would be great if someone else can verify this is indeed the case.

Is there anything else that I need to work on to get the PR to a mergeable state?

@dimpase
Copy link
Member

dimpase commented May 26, 2025

OK, so this doctest should be fixed. Please don't just correct the result, but add some text explaining why this is true.

@polykernel
Copy link
Author

Understood, should I open another PR for the doctest fix?

@dimpase
Copy link
Member

dimpase commented May 26, 2025

why - this one is still open, do it here

…d output

This commit updates the expected output for one
failing doctest under the _floordiv_ function, namely
the test for floor division over large prime
characteristic fields. The offending test computes ((x+y)^3+x+z)//(x+y)
with the expected answer x^2 + 2*x*y + y^2 under the default
grevlex order. However the actual quotient should be
x^2 + 2*x*y + y^2 + 1 as computed by Macaulay2 and verified
by hand calculation.
@polykernel
Copy link
Author

Ok, I thought it would be beneficial to track these changes separately since they are unrelated. I have just appended a commit fixing the doctest.

@dimpase
Copy link
Member

dimpase commented May 27, 2025

if this test result changes as a result of this PR changes, then it should be updated here. Otherwise it's up to you.

Copy link

Documentation preview for this PR (built with commit d6eb1e0; changes) is ready! 🎉
This preview will update shortly after each push to this PR.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Projects
None yet
Development

Successfully merging this pull request may close these issues.

<title>ZeroDivisionError when subtracting rational functions
3 participants