sagemath / sage

Main repository of SageMath
https://www.sagemath.org
Other
1.37k stars 466 forks source link

can not simplify derivative of erf #8568

Closed d5ad9730-0ff1-4f08-a6ec-3f5ef9e7f058 closed 14 years ago

d5ad9730-0ff1-4f08-a6ec-3f5ef9e7f058 commented 14 years ago

Sometimes sage can differentiate erf, but if there are two variables involved it gets confused in simplification and passes too many arguments to erf in maxima.

----------------------------------------------------------------------
| Sage Version 4.3.3, Release Date: 2010-02-21                       |
| Type notebook() for the GUI, and license() for information.        |
----------------------------------------------------------------------
sage: var('c x')
(c, x)
sage: diff(erf(x), x)
D[0](erf)(x)
sage: simplify(diff(erf(x), x))
2*e^(-x^2)/sqrt(pi)
sage: diff(erf(c * x), x)
c*D[0](erf)(c*x)
sage: simplify(diff(erf(c * x), x))
---------------------------------------------------------------------------
TypeError                                 Traceback (most recent call last)

/.../<ipython console> in <module>()

/.../sage-4.3.3-linux-64bit-ubuntu_9.10-x86_64-Linux/local/lib/python2.6/site-packages/sage/calculus/functional.pyc
in simplify(f)
     49     """
     50     try:
---> 51         return f.simplify()
     52     except AttributeError:
     53         return f

/.../sage-4.3.3-linux-64bit-ubuntu_9.10-x86_64-Linux/local/lib/python2.6/site-packages/sage/symbolic/expression.so
in sage.symbolic.expression.Expression.simplify (sage/symbolic/expression.cpp:21495)()

/.../sage-4.3.3-linux-64bit-ubuntu_9.10-x86_64-Linux/local/lib/python2.6/site-packages/sage/symbolic/expression.so
in sage.symbolic.expression.Expression._maxima_ (sage/symbolic/expression.cpp:3435)()

/.../sage-4.3.3-linux-64bit-ubuntu_9.10-x86_64-Linux/local/lib/python2.6/site-packages/sage/structure/sage_object.so
in sage.structure.sage_object.SageObject._interface_ (sage/structure/sage_object.c:3501)()

/.../sage-4.3.3-linux-64bit-ubuntu_9.10-x86_64-Linux/local/lib/python2.6/site-packages/sage/interfaces/expect.pyc
in __call__(self, x, name)
   1030             
   1031         if isinstance(x, basestring):
-> 1032             return cls(self, x, name=name)
   1033         try:
   1034             return self._coerce_from_special_method(x)

/.../sage-4.3.3-linux-64bit-ubuntu_9.10-x86_64-Linux/local/lib/python2.6/site-packages/sage/interfaces/expect.pyc
in __init__(self, parent, value, is_name, name)
   1449             except (TypeError, KeyboardInterrupt, RuntimeError, ValueError), x:
   1450                 self._session_number = -1
-> 1451                 raise TypeError, x
   1452         self._session_number = parent._session_number
   1453 

TypeError: Error executing code in Maxima
CODE:
        sage4 : (c)*(diff('erf(c, x), c, 1))$
Maxima ERROR:

Wrong number of arguments to erf
 -- an error. To debug this try: debugmode(true);

CC: @jasongrout @burcin

Component: calculus

Author: Burcin Erocal, Karl-Dieter Crisman

Reviewer: Karl-Dieter Crisman, Burcin Erocal

Merged: sage-4.6.alpha2

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

kcrisman commented 14 years ago
comment:1
CODE:
        sage4 : (c)*(diff('erf(c, x), c, 1))$
Maxima ERROR:

This looks suspicious. Why is it not erf(c*x)? I will investigate.

kcrisman commented 14 years ago
comment:2

Another problem... note that the variable being used in differentiation is c, not x!

kcrisman commented 14 years ago
comment:3

Somehow it must be related to this inconsistency.

sage: a
c*D[0](erf)(c*x)
sage: a._maxima_init_()
"(c)*(diff('erf(c, x), c, 1))"
sage: a.operands()[1].operands()[0]
c*x
sage: a.operands()[1].operands()[0]._maxima_init_()
'(c)*(x)'

Any ideas?

burcin commented 14 years ago

Attachment: trac_8568-diff_conversion.patch.gz

burcin commented 14 years ago

Author: Burcin Erocal

burcin commented 14 years ago
comment:4

I attached a patch which fixes the conversion to maxima. This doesn't fix the problem in the title of this ticket, "cannot simplify derivative of erf", since we cannot convert derivatives to maxima format if the arguments to the function are not variables.

In order to fix the problem reported here, we can add a _derivative_ method to the Function_erf() class defined in sage.functions.other.

kcrisman commented 14 years ago
comment:5

That sounds like a great idea. Patch coming up.

kcrisman commented 14 years ago

Apply only this.

kcrisman commented 14 years ago

Reviewer: Karl-Dieter Crisman

kcrisman commented 14 years ago
comment:6

Attachment: trac_8568-diff-conv-and-erf-deriv.patch.gz

This looks like a good change. I did the other thing, and put the test of the original bug report in that file, since it seemed like it belonged there now that it works :) Should pass all tests (only failures for me were related to Maxima upgrade).

kcrisman commented 14 years ago

Changed author from Burcin Erocal to Burcin Erocal, Karl-Dieter Crisman

burcin commented 14 years ago

Attachment: trac_8568-erf-deriv.patch.gz

burcin commented 14 years ago
comment:7

Attachment: trac_8568-diff_conversion.take2.patch.gz

Thanks for the quick fix!

Your patch removes my commit message, which I tried hard to make long and explanatory. :) Please submit a separate patch in the future.

I uploaded two new patches, one including your changes to the erf function, and the other my fixes for expression_conversions.py.

Patches to be applied:

burcin commented 14 years ago

Changed reviewer from Karl-Dieter Crisman to Karl-Dieter Crisman, Burcin Erocal

kcrisman commented 14 years ago
comment:8

Sorry, Burcin, truthfully I hadn't even noticed that! I was just trying to make a unified patch, because I hate all these five-patch tickets where they move things around from patch to patch - in this case, the actual test for the fix - since they are very hard for me to read. But I'll be more careful now :)

mwhansen commented 14 years ago

Merged: sage-4.4.4.alpha0

mwhansen commented 14 years ago
comment:10

This actually causes a failure in sage/interfaces/maxima.py

mwhansen commented 14 years ago

Changed merged from sage-4.4.4.alpha0 to none

kcrisman commented 14 years ago
comment:11

Here it is. I'll try to fix it quickly, hopefully this can still get in 4.4.4.

File "/mnt/usb1/scratch/kcrisman/sage-4.4.4.alpha0-boxen.math.washington.edu-x86_64-Linux/devel/sage/sage/interfaces/maxima.py", line 2245:
    sage: latex(maxima(derivative(ceil(x*y*d), d,x,x,y)))
Exception raised:
    Traceback (most recent call last):
      File "/mnt/usb1/scratch/kcrisman/sage-4.4.4.alpha0-boxen.math.washington.edu-x86_64-Linux/local/bin/ncadoctest.py", line 1231, in run_one_test
        self.run_one_example(test, example, filename, compileflags)
      File "/mnt/usb1/scratch/kcrisman/sage-4.4.4.alpha0-boxen.math.washington.edu-x86_64-Linux/local/bin/sagedoctest.py", line 38, in run_one_example
        OrigDocTestRunner.run_one_example(self, test, example, filename, compileflags)
      File "/mnt/usb1/scratch/kcrisman/sage-4.4.4.alpha0-boxen.math.washington.edu-x86_64-Linux/local/bin/ncadoctest.py", line 1172, in run_one_example
        compileflags, 1) in test.globs
      File "<doctest __main__.example_70[4]>", line 1, in <module>
        latex(maxima(derivative(ceil(x*y*d), d,x,x,y)))###line 2245:
    sage: latex(maxima(derivative(ceil(x*y*d), d,x,x,y)))
      File "/mnt/usb1/scratch/kcrisman/sage-4.4.4.alpha0-boxen.math.washington.edu-x86_64-Linux/local/lib/python/site-packages/sage/interfaces/expect.py", line 1034, in __call__
        return self._coerce_from_special_method(x)
      File "/mnt/usb1/scratch/kcrisman/sage-4.4.4.alpha0-boxen.math.washington.edu-x86_64-Linux/local/lib/python/site-packages/sage/interfaces/expect.py", line 1058, in _coerce_from_special_method
        return (x.__getattribute__(s))(self)
      File "expression.pyx", line 435, in sage.symbolic.expression.Expression._maxima_ (sage/symbolic/expression.cpp:3420)
      File "sage_object.pyx", line 379, in sage.structure.sage_object.SageObject._interface_ (sage/structure/sage_object.c:3374)
      File "sage_object.pyx", line 468, in sage.structure.sage_object.SageObject._maxima_init_ (sage/structure/sage_object.c:5083)
      File "expression.pyx", line 458, in sage.symbolic.expression.Expression._interface_init_ (sage/symbolic/expression.cpp:3510)
      File "/mnt/usb1/scratch/kcrisman/sage-4.4.4.alpha0-boxen.math.washington.edu-x86_64-Linux/local/lib/python/site-packages/sage/symbolic/expression_conversions.py", line 214, in __call__
        return self.arithmetic(ex, operator)
      File "/mnt/usb1/scratch/kcrisman/sage-4.4.4.alpha0-boxen.math.washington.edu-x86_64-Linux/local/lib/python/site-packages/sage/symbolic/expression_conversions.py", line 515, in arithmetic
        args = ["(%s)"%self(op) for op in ex.operands()]
      File "/mnt/usb1/scratch/kcrisman/sage-4.4.4.alpha0-boxen.math.washington.edu-x86_64-Linux/local/lib/python/site-packages/sage/symbolic/expression_conversions.py", line 214, in __call__
        return self.arithmetic(ex, operator)
      File "/mnt/usb1/scratch/kcrisman/sage-4.4.4.alpha0-boxen.math.washington.edu-x86_64-Linux/local/lib/python/site-packages/sage/symbolic/expression_conversions.py", line 515, in arithmetic
        args = ["(%s)"%self(op) for op in ex.operands()]
      File "/mnt/usb1/scratch/kcrisman/sage-4.4.4.alpha0-boxen.math.washington.edu-x86_64-Linux/local/lib/python/site-packages/sage/symbolic/expression_conversions.py", line 218, in __call__
        return self.derivative(ex, operator)
      File "/mnt/usb1/scratch/kcrisman/sage-4.4.4.alpha0-boxen.math.washington.edu-x86_64-Linux/local/lib/python/site-packages/sage/symbolic/expression_conversions.py", line 495, in derivative
        raise NotImplementedError, "arguments must be distinct variables"
    NotImplementedError: arguments must be distinct variables
**********************************************************************
1 items had failures:
   1 of   6 in __main__.example_70
***Test Failed*** 1 failures.
For whitespace errors, see the file /home/kcrisman/.sage//tmp/.doctest_maxima.py
     [14.2 s]

----------------------------------------------------------------------
The following tests failed:

    sage -t  "devel/sage/sage/interfaces/maxima.py"
kcrisman commented 14 years ago
comment:12

Or not.

sage: maxima(derivative(ceil(d),d))
'diff('ceil(d),d,1)
sage: maxima(derivative(ceil(x*d),d))
<same NotImplementedError>

So the problem is that Burcin's Maxima conversion change now doesn't work with

    493         if (not all(is_SymbolicVariable(v) for v in args) or
    494             len(args) != len(set(args))):
--> 495             raise NotImplementedError, "arguments must be distinct variables"
    496 
    497         f = operator.function()

in derivative(). In fact, he even included a doctest for it!

We can only convert to Maxima derivatives if the corresponding operand of the function is a variable:: 
sage: y = var('y') 
sage: t = f(x*y).diff(x) 
sage: m.derivative(t, t.operator()) 
Traceback (most recent call last): 
... 
NotImplementedError: arguments must be distinct variables 

This example could be fixed if one fixed

sage: derivative(ceil(x),x)
D[0](ceil)(x)

so that there isn't a derivative function defined for ceil. I'm not sure exactly what would count, though... just the zero function?

burcin commented 14 years ago

Attachment: trac_8568-fix_doctests.patch.gz

burcin commented 14 years ago
comment:13

I attached a new patch which fixes the doctest failures in interfaces/maxima.py. The patch changes the test to use a symbolic function f instead of ceil, adds the error message that we need distinct arguments to convert to maxima and a test with an expression where the variables are indeed distinct arguments.

Adding a function to represent the derivative of ceil() and floor() is now #9874.

Patches should be applied in this order:

kcrisman commented 14 years ago
comment:14

This all still applies fine to Sage 4.6.alpha1, and all doctests I could think of pass. Everything else is the same - positive review!

By the way, thanks for opening that other ticket, Burcin - sometimes I get paralyzed trying to decide what the best course of action is on reviews.

e14f4152-4982-4ace-8c95-73a0599b109b commented 14 years ago

Merged: sage-4.6.alpha2