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

PolynomialRing_field.lagrange_polynomial doesn't always return a polynomial #10304

Closed
mguaypaq opened this issue Nov 21, 2010 · 8 comments
Closed

Comments

@mguaypaq
Copy link
Contributor

The function PolynomialRing_field.lagrange_polynomial sometimes returns an element of the coefficient ring or even a python int instead of an element of the polynomial ring. This can lead to problems if the caller was expecting a polynomial.

sage: R, t = FractionField(QQ['t']).objgen(); R.rename('R')
sage: S, x = R['x'].objgen(); S.rename('S')
sage: S.lagrange_polynomial([(2, 3 * t), (4, t + 1)]).parent()
S
sage: S.lagrange_polynomial([(2, 3 * t)]).parent()
R
sage: type(S.lagrange_polynomial([]))
<type 'int'>

The return value is a python int if the list of points was empty, an element of the base ring if the list of points had one point, and an element of the polynomial ring if the list of points had more than one point.

There are similar problems with the base case for the algorithm='neville' version:

sage: S.lagrange_polynomial([(2, 3 * t), (4, t + 1)], algorithm='neville')
[t + 1, (-t + 1/2)*x + 5*t - 1]
sage: [poly.parent() for poly in _]
[R, S]
sage: S.lagrange_polynomial([(2, 3 * t)], algorithm='neville')
[3*t]
sage: [poly.parent() for poly in _]
[R]
sage: S.lagrange_polynomial([], algorithm='neville')
0
sage: type(_)
<type 'int'>

Note that in this case, the return value is not even a list when the list of points was empty.

Apply:

  1. attachment: trac_10304_lagrange_poly_in_self.patch
  2. attachment: trac-10304_reviewer.patch

CC: @sagetrac-mvngu

Component: algebra

Keywords: polynomial ring, lagrange polynomial

Author: Mathieu Guay-Paquet

Reviewer: Minh Van Nguyen

Merged: sage-4.6.1.alpha3

Issue created by migration from https://trac.sagemath.org/ticket/10304

@mguaypaq mguaypaq added this to the sage-4.6.1 milestone Nov 21, 2010
@mguaypaq mguaypaq self-assigned this Nov 21, 2010
@mguaypaq
Copy link
Contributor Author

Attachment: trac_10304_lagrange_poly_in_self.patch.gz

@mguaypaq
Copy link
Contributor Author

comment:1

I've put up a patch that should fix the problem, at least in the case of algorithm='divided_difference', and includes a new doctest to verify this.

For the algorithm='neville' case, I've taken the liberty of rewriting the code, to clean it up and unify it a bit in addition to fixing the original problem. However:

  • This changes the default value of previous_row from None to []. I doubt this would break anything, but it is a slight change in the function signature.
  • In the case of an empty list of points, I wasn't sure whether to return [] or [0]. The former is more consistent with the previous_row option, but the latter preserves the property that the last element of the returned list is the interpolating polynomial (as opposed to non-existent). I opted for the first one, but there's definitely a choice to be made here. (I'll note that neither choice is really backwards-incompatible, since the method used to fail in this case.)

Minh, I've put you on CC, since (I think) you wrote the original code for the algorithm='neville' part. Do you approve of the changes?

@mguaypaq
Copy link
Contributor Author

Author: Mathieu Guay-Paquet

@sagetrac-mvngu
Copy link
Mannequin

sagetrac-mvngu mannequin commented Nov 21, 2010

comment:2

Attachment: trac-10304_reviewer.patch.gz

I'm happy with most of attachment: trac_10304_lagrange_poly_in_self.patch, but here are some problems with that patch:

  • A standard idiom in Python is to avoid using a mutable object for a default value. Using a mutable object (such as a list) as a default value can introduce subtle and difficult to track bugs. See this blog post for an explanation of why one should avoid using mutable values for default arguments. See this section in the Python Language Reference for an official explanation about avoiding the use of mutable objects for default values.

  • When you provide doctests that purports to illustrate that a bug has been fixed, you need to provide the corresponding ticket number in the documentation of the doctests.

  • The docstring you introduce doesn't use proper ReST syntax.

The reviewer patch attachment: trac-10304_reviewer.patch fixes all of the above issues. If you're OK with my patch, then set the ticket to positive review. See the ticket description for instructions on which patches to apply and in which order.

@sagetrac-mvngu

This comment has been minimized.

@sagetrac-mvngu
Copy link
Mannequin

sagetrac-mvngu mannequin commented Nov 21, 2010

Reviewer: Minh Van Nguyen

@mguaypaq
Copy link
Contributor Author

comment:3

I thought the use of a mutable default was safe, since it is always copied, never modified. But since there's an idiom against this, I'm happy to comply! And thanks for fixing the documentation issues.

Positive review for the reviewer patch.

@jdemeyer
Copy link

jdemeyer commented Dec 2, 2010

Merged: sage-4.6.1.alpha3

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

No branches or pull requests

2 participants