Opened 6 years ago

Closed 6 years ago

# .is_zero() method raises error for symbolic expression involving derivative

Reported by: Owned by: gmhossain major sage-4.3 symbolics mhansen sage-4.3.rc0 Burcin Erocal Mike Hansen N/A

If a symbolic expression contains  symbolic derivative then
checking whether it is zero, raises error:

```sage: x.diff(x,2).is_zero()
True

sage: f(x) = function('f',x)
sage: f(x).diff(x).is_zero()
....
NotImplementedError: derivative
```

This fails because new symbolics tries to convert it to maxima
expression for checking the relation.

Update:

* A patch to fix the issue is attached. The patch
adds a new method ".has_fderivative()" for symbolic expressions
and in __nonzero__ method adds a check whether it has fderivative.

A simple timing comparison that illustrates why we should
avoid calling maxima to assert nonzero even for symbolic
functions

```sage: f(x) = function('f',x)
sage: timeit('sin(f(x)).is_zero()')
5 loops, best of 3: 85.8 ms per loop
sage: timeit('sin(f(x).diff(x)).is_zero()')
625 loops, best of 3: 132 µs per loop
```

It seems pynac is 400 times faster than maxima in this case.

### comment:1 Changed 6 years ago by gmhossain

• Description modified (diff)
• Summary changed from .is_zero() method raises error for symbolic expression involving derivative to [with patch, needs review] .is_zero() method raises error for symbolic expression involving derivative

### comment:2 Changed 6 years ago by burcin

• Milestone set to sage-4.1.1
• Summary changed from [with patch, needs review] .is_zero() method raises error for symbolic expression involving derivative to .is_zero() method raises error for symbolic expression involving derivative

Unfortunately, the fact that an expression contains a symbolic derivative doesn't guarantee that it is nonzero:

```sage: t = f(x).derivative(x)
sage: (x*t +(1-x)*t - t)
-(x - 1)*D[1](f)(x) + x*D[1](f)(x) - D[1](f)(x)
sage: (x*t +(1-x)*t - t).collect(x)
0
```

The right fix for this is to either implement the .derivative() method in sage/symbolic/expression_conversions.py or to change pynac to allow different parents in evalf(), so that conversion to CIF can be done without the code in expression_conversions.pyx.

I was planning to do this for #6243, but ended up using a different/better fix there.

### comment:3 Changed 6 years ago by burcin

• Authors set to Burcin Erocal
• Status changed from new to needs_review

This is fixed by #7490, since we don't use the expression_conversions module to convert to RIF any more.

attachment:trac_6523-derivative_is_zero.patch adds a doctest with the example in the description.

### comment:4 Changed 6 years ago by mhansen

• Merged in set to sage-4.3.rc0
• Report Upstream set to N/A
• Resolution set to fixed
• Reviewers set to Mike Hansen
• Status changed from needs_review to closed

Looks good to me.

Merged trac_6523-derivative_is_zero.patch.

Note: See TracTickets for help on using tickets.