Skip to content
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

Bandaid for polynomial evaluation #36127

Merged

Conversation

fchapoton
Copy link
Contributor

There was a problem when evaluating polynomials: the following was failing

sage: x,y=polygens(QQ,'x,y')
sage: t=PolynomialRing(x.parent(),'t').gen()
sage: F=x*y*t
sage: F(x=1)
y*t

I propose a fix that hopefully does not break anything else.
We look at a non-zero coefficient to try to guess the correct new base ring, instead of looking at the constant term.

This does not fix the following inconsistent behaviour:

sage: x.parent().zero()(x=1)
0
sage: parent(_)
Rational Field
sage: x.parent().one()(x=1)
1
sage: parent(_)
Multivariate Polynomial Ring in x, y over Rational Field

📝 Checklist

  • The title is concise, informative, and self-explanatory.
  • 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 accordingly.

@mezzarobba
Copy link
Member

Given that you call this a bandaid, it looks like we agree that the actual bug is that partial evaluations of multivariate polynomials sometimes land in the wrong parent. I don't mind working around this issue as you did, but I think it would be good to add a comment or two to explain what you are doing.


# Evaluate the coefficients, then fall through to evaluate the
# resulting univariate polynomial

if eval_coeffs:
new_base = parent(top)
# this value for the common parent of the evaluated
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

I don't understand what you mean here.

Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Well, I am not 100% sure what the code is doing exactly. It seem to try to guess a new base ring, depending not only on the existing base ring, but on the substituted coefficients. But it does that by looking only at the substitution in one of the coefficients. Before my changes, it was looking at the substitution in the constant term, and fails if this was zero. I changed it to use the substitution of the leading term. This works a little better.

Of course, the only thing making complete sense would be to look at the common parent of all substituted coeffs. But this may have a large speed penalty..

Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

But it does that by looking only at the substitution in one of the coefficients.

Yes, but for this whole business (of evaluating the coefficients of a polynomial and getting a new polynomial) to make sense, evaluation (of the coefficients) should be a ring morphism, so it should be enough to look at the evaluation of one coefficient, or in fact of any element of the base ring.

So as I said in the other comment, I think it is okay to work around the fact that some of the base rings we may want to work with have an evaluation function that does not respect this constraint. However, I'm still not sure what you mean by “this value [...] only works for monomials”.

@fchapoton
Copy link
Contributor Author

There may be a tension between landing in the "minimal parent" or the "best uniform parent". If you substitute into 0 in the ring QQ[x,y], you can always land in the ring QQ[x,y] itself, for any value of x and y.

As far as I know, we have no mechanism for substitution-coercion at the level of rings, do we ?

Do you want me to remove the comment, or write something else ?

@mezzarobba
Copy link
Member

There may be a tension between landing in the "minimal parent" or the "best uniform parent". If you substitute into 0 in the ring QQ[x,y], you can always land in the ring QQ[x,y] itself, for any value of x and y.

I think the minimal uniform parent (i.e., the smallest parent that makes the operations you are implementing a well-defined map) is almost always the right choice — and is what we are typically using. In other words: usually, the parent of the result should only depend on the parents of the inputs. For p∈ℚ[x,y], I would say p(x=0,y=0) is in ℚ, but p(x=0) == p(x=0, y=y) is in ℚ[y] regardless of the value of p.

As far as I know, we have no mechanism for substitution-coercion at the level of rings, do we ?

I'm not sure what kind of mechanism you have in mind. Perhaps Morphism objects can play this role in a sense?

Do you want me to remove the comment, or write something else ?

I think the comment can be removed, but feel free to leave it or reword it if you feel there is something important to say.

@fchapoton fchapoton force-pushed the bandaid_for_polynomial_evaluation branch from 75b6da6 to 956aaf0 Compare August 29, 2023 15:47
@fchapoton
Copy link
Contributor Author

ok, j'ai raccourci le commentaire

@github-actions
Copy link

Documentation preview for this PR (built with commit 956aaf0; changes) is ready! 🎉

@mezzarobba
Copy link
Member

ok, thanks

@vbraun vbraun merged commit 33c99ab into sagemath:develop Sep 1, 2023
9 of 12 checks passed
@mkoeppe mkoeppe added this to the sage-10.2 milestone Sep 1, 2023
@fchapoton fchapoton deleted the bandaid_for_polynomial_evaluation branch September 2, 2023 06:38
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.

4 participants