Skip to content

provide default argument for monomial_coefficients#39044

Merged
vbraun merged 5 commits intosagemath:developfrom
mantepse:polynomials/monomial_coefficients_default_argument
Dec 8, 2024
Merged

provide default argument for monomial_coefficients#39044
vbraun merged 5 commits intosagemath:developfrom
mantepse:polynomials/monomial_coefficients_default_argument

Conversation

@mantepse
Copy link
Copy Markdown
Contributor

This is a followup to #38767, where we put polynomials into the category of modules with basis. Back then, we missed the fact that monomial_coefficients should take an optional argument copy, which we fix here.

Fixes #39037

@github-actions
Copy link
Copy Markdown

github-actions bot commented Nov 27, 2024

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

@mezzarobba
Copy link
Copy Markdown
Member

Thanks for looking into the issue! The fix looks good to me except for the typo revealed by the tests.

@mezzarobba
Copy link
Copy Markdown
Member

Would you mind tests that leading_monomial() and leading_term() work correctly on multivariate polynomials, though?

@user202729
Copy link
Copy Markdown
Contributor

Would also be useful to have test for both cases (copy=False and copy=True), as well as adding documentation what it means (ideally just point all of them to one place)

@mantepse
Copy link
Copy Markdown
Contributor Author

Would also be useful to have test for both cases (copy=False and copy=True), as well as adding documentation what it means (ideally just point all of them to one place)

It's not clear to me how to do this without a lot of boring work which I'd rather not do. There is not really a benefit, I think.

@mantepse
Copy link
Copy Markdown
Contributor Author

@fchapoton, the linter is complaining, should I fix these here or separately? Also, what is the correct invocation at home? If I do ./sage -tox -e pycodestyle-minimal -- src/sage/rings/polynomial/multi_polynomial.pyx it is selecting the wrong set of rules:

pycodestyle-minimal: commands[0] /home/martin/sage-trac/src> pycodestyle --select E111,E21,E221,E222,E225,E227,E228,E25,E271,E275,E303,E305,E306,E401,E502,E701,E702,E703,E71,E72,W291,W293,W391,W605 sage/rings/polynomial/multi_polynomial.pyx
sage/rings/polynomial/multi_polynomial.pyx:483:22: E225 missing whitespace around operator
sage/rings/polynomial/multi_polynomial.pyx:536:21: E225 missing whitespace around operator
sage/rings/polynomial/multi_polynomial.pyx:536:25: E225 missing whitespace around operator
sage/rings/polynomial/multi_polynomial.pyx:546:25: E225 missing whitespace around operator
sage/rings/polynomial/multi_polynomial.pyx:546:36: E225 missing whitespace around operator
sage/rings/polynomial/multi_polynomial.pyx:547:21: E225 missing whitespace around operator
sage/rings/polynomial/multi_polynomial.pyx:547:33: E225 missing whitespace around operator
sage/rings/polynomial/multi_polynomial.pyx:568:34: E225 missing whitespace around operator
sage/rings/polynomial/multi_polynomial.pyx:778:16: E225 missing whitespace around operator
sage/rings/polynomial/multi_polynomial.pyx:778:28: E225 missing whitespace around operator
sage/rings/polynomial/multi_polynomial.pyx:778:51: E225 missing whitespace around operator
sage/rings/polynomial/multi_polynomial.pyx:778:63: E225 missing whitespace around operator
sage/rings/polynomial/multi_polynomial.pyx:2131:19: E225 missing whitespace around operator
sage/rings/polynomial/multi_polynomial.pyx:2137:23: E225 missing whitespace around operator
14      E225 missing whitespace around operator
pycodestyle-minimal: exit 1 (0.15 seconds) /home/martin/sage-trac/src> pycodestyle --select E111,E21,E221,E222,E225,E227,E228,E25,E271,E275,E303,E305,E306,E401,E502,E701,E702,E703,E71,E72,W291,W293,W391,W605 sage/rings/polynomial/multi_polynomial.pyx pid=27666
  pycodestyle-minimal: FAIL code 1 (0.21=setup[0.07]+cmd[0.15] seconds)
  evaluation failed :( (0.37 seconds)

@fchapoton
Copy link
Copy Markdown
Contributor

fchapoton commented Nov 27, 2024

the linter must pass. You just need to start with r""" the block containing the new \Z

note that pyx files and py file are treated with different rules. In particular E225 is not applied to pyx files

@user202729
Copy link
Copy Markdown
Contributor

Thinking about it…

It might be much better for the user to just deprecate the copy feature and requires copy=True everywhere. It's better for elements to be immutable anyway, and especially not by mutating an unrelated object.

But then grepping for monomial_coefficients(copy=False) in the code base, it is used in quite a lot of places as a small performance optimization. Not sure if it's worth it (we can always move copy=False to an internal implementation detail e.g. def _monomial_coefficients_nocopy() which classes can define if it has a special no-copy version)

@tscrim
Copy link
Copy Markdown
Collaborator

tscrim commented Nov 28, 2024

No, we definitely should not get rid of copy input. It is a very important performance feature for sparse implementations already in that dict form. Specifically IndexedFreeModuleElement.

@mantepse
Copy link
Copy Markdown
Contributor Author

Alternatively, would it make sense to return an immutable dict?

(But, please, not in this PR)

@user202729
Copy link
Copy Markdown
Contributor

Python doesn't have immutable dict. Maybe a dict view would be reasonable but that would be significant change… yes, for this PR we should not consider that issue.

@tscrim
Copy link
Copy Markdown
Collaborator

tscrim commented Nov 28, 2024

Indeed Python doesn't, and sometimes we do want to modify the internal dict.

@mantepse
Copy link
Copy Markdown
Contributor Author

mantepse commented Dec 8, 2024

Would you mind tests that leading_monomial() and leading_term() work correctly on multivariate polynomials, though?

I did add doctests, involving different term orderings, see for example src/sage/categories/modules_with_basis.py lines 1906ff.

Is this what you had in mind, @mezzarobba ?

@mezzarobba
Copy link
Copy Markdown
Member

Yes, thank you!

@vbraun vbraun merged commit b5d7b78 into sagemath:develop Dec 8, 2024
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.

leading_coefficient() for multivariate polynomials exists but fails with a strange error

6 participants