Opened 12 years ago

Closed 7 years ago

Derivative at a point is not translated into Maxima

Reported by: Owned by: robert.marik was minor sage-6.5 interfaces derivative, at, maxima eviatarbach Robert Mařík, Nils Bruin Karl-Dieter Crisman, Nils Bruin, Peter Bruin N/A 589ff62

Description

```Hi all:

I found some strange behavior in solve that's related to function
composition.  Check out this short example.

----------------------------------------------------------------------
| Sage Version 4.2, Release Date: 2009-10-24                         |
| Type notebook() for the GUI, and license() for information.        |
----------------------------------------------------------------------
sage: var('x,t')
(x, t)
sage: f= function('f',x)
sage: e= {x:exp(t)}
sage: ft= f.subs(e); ft
f(e^t)
sage: Ft = t^2 + ft^2; Ft
t^2 + f(e^t)^2
sage: a= diff(Ft,t); a
2*e^t*f(e^t)*D[0](f)(e^t) + 2*t
sage: solve(a==0,diff(f,x).subs(e))
[D[0](f)(t) == -t*e^(-t)/f(e^t)]

Did you spot the strangeness?  Somehow diff(f,x).subs(e) became diff
(f,x).subs({x:t}) after solving.  Does anybody know how to fix this?

Alex

```

Changed 12 years ago by robert.marik

apply together with the patch for #385 (if necessary)

comment:1 follow-up: ↓ 2 Changed 12 years ago by robert.marik

• Status changed from new to needs_review

Submitted patch solves the problem described above

limitation and disadvantage of this patch are:

• it works for univariable functions only
• introduces new dummy variable into Maxima session

All comments are welcomed and any help appretiated

comment:2 in reply to: ↑ 1 ; follow-up: ↓ 4 Changed 12 years ago by kcrisman

• Authors set to Robert Marik
• Status changed from needs_review to needs_work

Submitted patch solves the problem described above

Yes, this should do so. It would probably be best to add one more doctest, just to show that the original one works (i.e. without showing the Maxima internals).

limitation and disadvantage of this patch are:

• it works for univariable functions only

That's fine - that's still improving the previous situation, correct? Looking at the code, it would be helpful to know what happens if params has more than one element - particularly in

```return "at(diff('%s(dummy_var_der), %s),dummy_var_der=%s)"%(f.name(), ", ".join(params), opnds[0])
```

it seems like one could have more items in the substitution tuple than there are %s to fill up, even if for some reason len(args)==1. What is the relation between args and params - could there be more params than args?

Also, make sure to add a doctest showing that the NotImplementedError? comes into play. Search for 'Traceback' in the file to see how the doctests for errors look.

• introduces new dummy variable into Maxima session

This is unfortunate, but probably unavoidable currently. You should test whether this breaks anything in Maxima, but I don't see why it would.

comment:3 Changed 12 years ago by jason

Also, there needs to be a blank line after "TESTS::" for documentation formatting.

comment:4 in reply to: ↑ 2 Changed 12 years ago by robert.marik

This is unfortunate, but probably unavoidable currently. You should test whether this breaks anything in Maxima, but I don't see why it would.

Perhaps we can use variable from f.variables() instead of introducing dummy variable.

comment:5 follow-up: ↓ 6 Changed 12 years ago by kcrisman

Why not see if that works, then update the patch? One might have to be careful in testing for how many variables/args there are. I am never clear on the difference between those two things, anyway, in Sage.

comment:6 in reply to: ↑ 5 ; follow-up: ↓ 7 Changed 12 years ago by robert.marik

Why not see if that works, then update the patch? One might have to be careful in testing for how many variables/args there are. I am never clear on the difference between those two things, anyway, in Sage.

My suggestion related to f.variables() does not work, since I cannot acces the variables inside the function derivative.

The original patch does not work for something like diff(f(x,y),x).subs(x=3) so I updated it and tested for various combination how many variables are in the function, how many variables appear at the resulting expression, how many variables are substituted etc.

Perhaps could be improved for multivariable functions, but Maxima expression "at (expr, [eqn_1, ..., eqn_n])" cannot be translated to current Sage and both issues should be fixed together. Perhaps in this ticket? (leaving as needs_work, despite the fact that the original problem has been fixed and doctested)

Changed 12 years ago by robert.marik

Apply only this patch.

comment:7 in reply to: ↑ 6 ; follow-up: ↓ 8 Changed 12 years ago by kcrisman

Perhaps could be improved for multivariable functions, but Maxima expression "at (expr, [eqn_1, ..., eqn_n])" cannot be translated to current Sage and both issues should be fixed together. Perhaps in this ticket? (leaving as needs_work, despite the fact that the original problem has been fixed and doctested)

Can you give an example of Sage command -> Maxima command -> Maxima output that would give rise to such a situation? That should make it fairly straightforward to change the at function appropriately.

comment:8 in reply to: ↑ 7 Changed 12 years ago by robert.marik

• Status changed from needs_work to needs_review

Can you give an example of Sage command -> Maxima command -> Maxima output that would give rise to such a situation? That should make it fairly straightforward to change the at function appropriately.

I thought that this happens for taylor polynomial in more variables. However, it is not the case (btw. taylor polynomial in more variables seem to be not supported in Sage and I submited patch for #7472).

Hence I think that the problem is patched and if someone finds situation where she/he needs to enhace the functionallity, this can be solved in another ticket.

comment:9 Changed 12 years ago by robert.marik

The patch fixes also #6376.

comment:10 Changed 12 years ago by kcrisman

• Reviewers set to Karl-Dieter Crisman
• Status changed from needs_review to positive_review

Okay, positive review, and all relevant tests pass. It is not optimal because of the dummy variable, one variable, etc., but it fixes things and there are open tickets (some with patches!) for making it even better. Great. I will also put a comment on #6376.

comment:11 Changed 12 years ago by mhansen

• Merged in set to sage-4.3.alpha0
• Resolution set to fixed
• Status changed from positive_review to closed

comment:12 Changed 12 years ago by mhansen

This causes a failure in sage/interfaces/maxima.py with this example

```maxima(derivative(ceil(x*y*d), d,x,x,y))
```

since multivariate derivatives are not supported.

comment:13 Changed 12 years ago by mhansen

• Merged in sage-4.3.alpha0 deleted
• Status changed from closed to needs_work

I've backed this out for now.

comment:14 Changed 8 years ago by eviatarbach

• Report Upstream set to N/A

comment:15 Changed 8 years ago by jdemeyer

• Milestone changed from sage-5.11 to sage-5.12

comment:16 Changed 8 years ago by vbraun_spam

• Milestone changed from sage-6.1 to sage-6.2

comment:17 Changed 8 years ago by vbraun_spam

• Milestone changed from sage-6.2 to sage-6.3

comment:18 Changed 7 years ago by vbraun_spam

• Milestone changed from sage-6.3 to sage-6.4

comment:19 Changed 7 years ago by nbruin

• Milestone changed from sage-6.4 to sage-duplicate/invalid/wontfix
• Status changed from needs_work to needs_review

On sage 6.5beta1 I get

```sage: solve(a==0,diff(f,x).subs(e))
[D[0](f)(e^t) == -t*e^(-t)/f(e^t)]
```

so I think this problem has been resolved. Do we close it as (now) invalid?

comment:20 Changed 7 years ago by pbruin

• Authors Robert Marik deleted
• Reviewers changed from Karl-Dieter Crisman to Nils Bruin, Peter Bruin
• Status changed from needs_review to positive_review

All doctests in the patch now work without the patch, even the ones that were expected to fail.

comment:21 follow-up: ↓ 23 Changed 7 years ago by kcrisman

• Status changed from positive_review to needs_work

Awesome! I tested this last night but it must have been too late, because I didn't notice it worked. :P

In which case we should document that it is fixed so we don't get bitten if (say) we switch symbolics or something again.

comment:22 Changed 7 years ago by kcrisman

As you say, e.g.

```sage: x,y,=var('x y')
sage: a = function('f', x, y).diff(x).subs(x=4).subs(y=8)
sage: b=maxima(a); b
?%at('diff('f(t0,t1),t0,1),[t0=4,t1=8])
sage: b.sage()
D[0](f)(4, 8)
```

Huh. Maybe this is a side effect of making symbolic variables unique in Maxima? Or the library interface? I bet no one has looked at this for a long time.

comment:23 in reply to: ↑ 21 Changed 7 years ago by pbruin

• Authors set to Robert Marik
• Milestone changed from sage-duplicate/invalid/wontfix to sage-6.5
• Priority changed from major to minor
• Reviewers changed from Nils Bruin, Peter Bruin to Karl-Dieter Crisman, Nils Bruin, Peter Bruin
• Type changed from defect to task
• Work issues set to add doctests

Awesome! I tested this last night but it must have been too late, because I didn't notice it worked. :P

In which case we should document that it is fixed so we don't get bitten if (say) we switch symbolics or something again.

Good point, I guess we can just add the doctests from the patch.

comment:24 follow-up: ↓ 25 Changed 7 years ago by kcrisman

• Branch set to u/kcrisman/pointderivs
• Status changed from needs_work to needs_review

New commits:

 ​476d8c4 `Add doctests for #7401 about derivatives at a point`

comment:25 in reply to: ↑ 24 Changed 7 years ago by nbruin

New commits:

 ​476d8c4 `Add doctests for #7401 about derivatives at a point`

• use `a = function('f')(x,y).diff(x)` to avoid the confusing (and hopefully to deprecated) function('f',x,y).
• perhaps include an example that also tests the behaviour when the auxiliary variables aren't necessary:
```sage: a = function('f')(x,y).diff(x)
sage: a
D[0](f)(x, y)
sage: maxima(a)
'diff('f(_SAGE_VAR_x,_SAGE_VAR_y),_SAGE_VAR_x,1)
```

since that also illustrates a template how to arrive at more user-friendly printing.

comment:26 Changed 7 years ago by git

• Commit changed from 476d8c43f23185ea22c46a9b30fd37c9ead46668 to cc7fe0a9f49ded047e5d2ed8c2cbe57b8dbace1c

Branch pushed to git repo; I updated commit sha1. New commits:

 ​cc7fe0a `More doctests for #7401`

comment:27 Changed 7 years ago by kcrisman

• Authors changed from Robert Marik to Robert Marik, Nils Bruin

Since you basically wrote all the other tests, adding to authors.

comment:28 Changed 7 years ago by pbruin

• Branch changed from u/kcrisman/pointderivs to u/pbruin/7401-pointderivs
• Commit changed from cc7fe0a9f49ded047e5d2ed8c2cbe57b8dbace1c to 589ff620f904ed797b756543c48d83bb72745ad6
• Status changed from needs_review to positive_review
• Work issues add doctests deleted

The reviewer patch (typesetting, logical ordering of doctests, checking a few more intermediate results) adds/changes nothing essential.

comment:29 Changed 7 years ago by vbraun

• Branch changed from u/pbruin/7401-pointderivs to 589ff620f904ed797b756543c48d83bb72745ad6
• Status changed from positive_review to closed

comment:30 Changed 5 years ago by chapoton

• Authors changed from Robert Marik, Nils Bruin to Robert Mařík, Nils Bruin