Ticket #12922: 16191_review.patch

File 16191_review.patch, 2.3 KB (added by sjg10, 9 years ago)

adds error handler

  • sage/calculus/all.py

    # HG changeset patch
    # User Miguel Marco <mmarco@unizar.es>
    # Date 1336477058 -7200
    # Node ID c649b4ae3f82e1612db74b2efab50330b644826e
    # Parent  45c4da67d2340847b680e925242a630bf387cc8c
    Trac 12922: Added implicit_derivative, better error handling
    
    diff --git a/sage/calculus/all.py b/sage/calculus/all.py
    a b  
    44
    55from functional import (diff, derivative,
    66                        expand,
    7                         taylor, simplify)
     7                        taylor, simplify, implicit_derivative)
    88
    99from functions import (wronskian,jacobian)
    1010
  • sage/calculus/functional.py

    diff --git a/sage/calculus/functional.py b/sage/calculus/functional.py
    a b  
    3232
    3333from calculus import SR
    3434from sage.symbolic.expression import Expression
     35from sage.symbolic.function_factory import SymbolicFunction
    3536
    3637def simplify(f):
    3738    r"""
     
    433434        return x.expand(*args, **kwds)
    434435    except AttributeError:
    435436        return x
     437
     438
     439def 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