#13836: Fix variable dependence in PiecewisePolynomial
----------------------------------------------+-----------------------------
       Reporter:  christiankuper              |         Owner:  burcin       
           Type:  defect                      |        Status:  needs_work   
       Priority:  major                       |     Milestone:  sage-5.6     
      Component:  symbolics                   |    Resolution:               
       Keywords:  Piecewise, critical_points  |   Work issues:               
Report Upstream:  N/A                         |     Reviewers:  Burcin Erocal
        Authors:  Andrew Fleckenstein         |     Merged in:               
   Dependencies:                              |      Stopgaps:               
----------------------------------------------+-----------------------------

Old description:

> When using the example in the documentation of the function the following
> results are displayed for critical points:
>
> {{{
> sage: R.<x> = QQ[]
> sage: f1 = x^0
> sage: f2 = 10*x - x^2
> sage: f3 = 3*x^4 - 156*x^3 + 3036*x^2 - 26208*x
> sage: f = Piecewise([[(0,3),f1],[(3,10),f2],[(10,20),f3]])
> sage: f.critical_points()
> [5.0, 12.000000000000124, 12.999999999999725, 14.000000000000151]
> }}}
>
> When doing the same with y instead of x as a variable an empty list is
> returned as a result:
>
> {{{
> sage: R.<y> = QQ[]
> sage: f1 = y^0
> sage: f2 = 10*y - y^2
> sage: f3 = 3*y^4 - 156*y^3 + 3036*y^2 - 26208*y
> sage: f = Piecewise([[(0,3),f1],[(3,10),f2],[(10,20),f3]])
> sage: f.critical_points()
> []
> }}}
>
> This behavior does not change even if y is explicitly defined as the
> variable:
>
> {{{
> sage: f = Piecewise([[(0,3),f1],[(3,10),f2],[(10,20),f3]],y)
> sage: f.critical_points()
> []
> }}}
>
> The wrong variable is also used (to varying effects) in the functions
> trapezoid() and derivative(). Multiple fourier series functions also use
> the wrong variable, but it doesn't affect the result. The Laplace
> transform also uses the wrong variable, but only if you aren't explicit
> about it.
>
> It should be possible to obtain correct results no matter what the name
> of the variable is.

New description:

 When using the example in the documentation of the function the following
 results are displayed for critical points:

 {{{
 sage: R.<x> = QQ[]
 sage: f1 = x^0
 sage: f2 = 10*x - x^2
 sage: f3 = 3*x^4 - 156*x^3 + 3036*x^2 - 26208*x
 sage: f = Piecewise([[(0,3),f1],[(3,10),f2],[(10,20),f3]])
 sage: f.critical_points()
 [5.0, 12.000000000000124, 12.999999999999725, 14.000000000000151]
 }}}

 When doing the same with y instead of x as a variable an empty list is
 returned as a result:

 {{{
 sage: R.<y> = QQ[]
 sage: f1 = y^0
 sage: f2 = 10*y - y^2
 sage: f3 = 3*y^4 - 156*y^3 + 3036*y^2 - 26208*y
 sage: f = Piecewise([[(0,3),f1],[(3,10),f2],[(10,20),f3]])
 sage: f.critical_points()
 []
 }}}

 This behavior does not change even if y is explicitly defined as the
 variable:

 {{{
 sage: f = Piecewise([[(0,3),f1],[(3,10),f2],[(10,20),f3]],y)
 sage: f.critical_points()
 []
 }}}

 A variable besides default_variable() is also used (to varying effects) in
 the functions trapezoid(), derivative(), tangent_line() and convolution().
 The fourier series functions that find coefficients use a variable other
 than default_variable() in the code, but it doesn't affect output, and
 probably doesn't need to be changed. However, the _fourier_series_helper()
 function probably should be changed.
 {{{
 sage: y = var('y')
 sage: f(y) = y^2
 sage: f = Piecewise([[(-1, 1),f]])
 sage: f._fourier_series_helper(3, 1, lambda n: 1)
 -4*cos(pi*x)/pi^2 + cos(2*pi*x)/pi^2 + 1/3
 }}}

 It should be possible to obtain consistent results no matter what the name
 of the variable is.

--

Comment (by afleckenstein):

 I was going to change laplace too, but after looking at it more closely I
 decided to leave it alone.
 I had no idea what to do with convolution().

 Replying to [comment:6 christiankuper]:
 > * I am not sure whether you intentionally put the patch and the doctest
 for the patch in two seperate patches
 No, it was a mistake, sorry.
 > * I think the second part of your doctest does not proove anything as y
 is the only variable in your PiecewisePolynomial.
 I put this in the doctest because Burcin suggested I made sure that
 default_variable() worked well, but it does seem a little redundant.
 > * From personal experience: I would avoid doctests with precion outputs
 like this because they might fail due to slightly different results on 32
 bit machines.
 Fixed this.
 > * The following modification results in an error, although e is a
 constant and not a variable. However, I think this is (also) a problem
 related to maxima. At least the exception raised currently is misleading.
 I modified the f2 in the example above:
 > {{{
 >
 > sage: f2(y) = e^2*y - y^2
 > sage: f = Piecewise([[(0,3),f1],[(3,10),f2],[(10,20),f3]], y)
 > sage: f.critical_points()
 > ...
 > ValueError: No differentiation variable specified.
 >
 > }}}
 >
 This also fails if you try to do it in the current version of SAGE (with x
 as a variable). So yes, it is a bug in maxima. Also, I get the error
 {{{
 TypeError: ECL says: Error executing code in Maxima: allroots: expected
 a polynomial in one variable; found variables [%e,x]
 }}}
 instead of a ValueError.
 > * I looked through the piecewise.py module to see whether there are
 other instances of forcing the variable to x. There are indeed, but the
 errors they are causing are different (see example below). Do you think it
 would be sensible to deal with these cases also in this patch (as they are
 related to the same root cause)? Here is an example were the output is
 basically correct but the variable is wrong (g is the function defined
 above):
 > {{{
 > sage: g.derivative()
 > Piecewise defined function with 3 parts, [[(0, 3), x |--> 0], [(3, 10),
 x |--> 0], [(10, 20), x |--> 0]]
 > }}}
 Changed the description to reflect this.

-- 
Ticket URL: <http://trac.sagemath.org/sage_trac/ticket/13836#comment:9>
Sage <http://www.sagemath.org>
Sage: Creating a Viable Open Source Alternative to Magma, Maple, Mathematica, 
and MATLAB

-- 
You received this message because you are subscribed to the Google Groups 
"sage-trac" group.
To post to this group, send email to [email protected].
To unsubscribe from this group, send email to 
[email protected].
For more options, visit this group at 
http://groups.google.com/group/sage-trac?hl=en.

Reply via email to