| 437 | |
| 438 | |
| 439 | def implicit_derivative(Y,X,F,n=1): |
| 440 | """ |
| 441 | Computes the n'th derivative of Y with respect to X given implicitly by F. |
| 442 | |
| 443 | EXAMPLES: |
| 444 | |
| 445 | :: |
| 446 | |
| 447 | sage: var('x,y') |
| 448 | (x, y) |
| 449 | sage: implicit_derivative(y,x,cos(x)*sin(y)) |
| 450 | sin(x)*sin(y)/(cos(x)*cos(y)) |
| 451 | sage: implicit_derivative(y,x,x*y^2,3) |
| 452 | -1/4*(y + 2*y/x)/x^2 + 1/4*(2*y^2/x - y^2/x^2)/(x*y) - 3/4*y/x^3 |
| 453 | |
| 454 | It is an error to not include a Y term in the expression F:: |
| 455 | |
| 456 | sage: implicit_derivative(y,x,cos(x)*sin(x)) |
| 457 | Traceback (most recent call last): |
| 458 | ... |
| 459 | ValueError: Expression F contains no y terms |
| 460 | """ |
| 461 | |
| 462 | if not F.has(Y): |
| 463 | raise ValueError, "Expression F contains no %s terms" % str(Y) |
| 464 | x=SR.symbol('x') |
| 465 | yy=SR.symbol('yy') |
| 466 | y=SymbolicFunction('y',1)(x) |
| 467 | f=SymbolicFunction('f',2)(x,yy) |
| 468 | Fx=f.diff(x) |
| 469 | Fy=f.diff(yy) |
| 470 | G=-(Fx/Fy) |
| 471 | G=G.subs_expr({yy:y}) |
| 472 | di={y.diff(x):-F.diff(X)/F.diff(Y)} |
| 473 | R=G |
| 474 | S=G.diff(x,n-1) |
| 475 | for i in range(n+1): |
| 476 | di[y.diff(x,i+1)(x=x)]=R |
| 477 | S=S.subs_expr(di) |
| 478 | R=G.diff(x,i) |
| 479 | for j in range(n+1-i): |
| 480 | di[f.diff(x,i,yy,j)(x=x,yy=y)]=F.diff(X,i,Y,j) |
| 481 | S=S.subs_expr(di) |
| 482 | return S |