Ticket #10750: trac_10750-solve.patch

File trac_10750-solve.patch, 4.3 KB (added by jhpalmieri, 11 years ago)
  • sage/symbolic/relation.py

    # HG changeset patch
    # User J. H. Palmieri <palmieri@math.washington.edu>
    # Date 1315939094 25200
    # Node ID 356a6984b20ef87e0b0559246b5f512facd1b1cb
    # Parent  a48af224839e4eb2555175d15d703ecd81d0b4c1
    #10750: allow solving a single equation w.r.t. more than one variable
    
    diff --git a/sage/symbolic/relation.py b/sage/symbolic/relation.py
    a b def solve(f, *args, **kwds): 
    479479       are no solutions, return an empty list (rather than a list containing
    480480       an empty dictionary). Likewise, if there's only a single solution,
    481481       return a list containing one dictionary with that solution.
    482    
     482
     483    If solving a single equation, some additional keyword arguments are available:
     484
     485    - ``multiplicities`` - bool (default: False); if True, return
     486       corresponding multiplicities.  This keyword is incompatible
     487       with ``to_poly_solve=True`` and does not make any sense when
     488       solving an inequality.
     489
     490    - ``explicit_solutions`` - bool (default: False); require that all
     491      roots be explicit rather than implicit. Not used when solving
     492      an inequality.
     493
     494    - ``to_poly_solve`` - bool (default: False) or string; use
     495      Maxima's ``to_poly_solver`` package to search for more possible
     496      solutions, but possibly encounter approximate solutions.  This
     497      keyword is incompatible with ``multiplicities=True`` and is not
     498      used when solving an inequality. Setting ``to_poly_solve`` to
     499      the string 'force' omits Maxima's solve command (useful when
     500      finding solutions of some trigonometric equations).
     501
     502    For examples using these keywords, see the documentation for the
     503    :meth:`solve <sage.symbolic.expression.Expression.solve>` method
     504    for single equations.
     505
    483506    EXAMPLES::
    484    
     507
    485508        sage: x, y = var('x, y')
    486509        sage: solve([x+y==6, x-y==4], x, y)
    487510        [[x == 5, y == 1]]
    def solve(f, *args, **kwds): 
    522545
    523546    .. note::
    524547
    525         For more details about solving a single equations, see
    526         the documentation for its solve.
     548        For more details about solving a single equation, see the
     549        documentation for its :meth:`solve
     550        <sage.symbolic.expression.Expression.solve>` method.
    527551
    528552    ::
    529553
    def solve(f, *args, **kwds): 
    554578    a new variable.  In the following example, ``r1`` is a real free
    555579    variable (because of the ``r``)::
    556580
    557         sage: solve([x+y == 3, 2*x+2*y == 6],x,y)
     581        sage: solve(x+y == 3,x,y)
    558582        [[x == -r1 + 3, y == r1]]
    559583
     584    (This also illustrates solving a single equation with respect to
     585    more than one variable.)
     586
    560587    Especially with trigonometric functions, the dummy variable may
    561588    be implicitly an integer (hence the ``z``)::
    562589
    def solve(f, *args, **kwds): 
    646673        []
    647674    """
    648675    from sage.symbolic.expression import is_Expression
    649     if is_Expression(f): # f is a single expression
    650         ans = f.solve(*args,**kwds)
    651         return ans
     676    from sage.symbolic.ring import is_SymbolicVariable
     677    # f is a single expression and there is a single variable
     678    if is_Expression(f):
     679        if len(args) == 1 and is_SymbolicVariable(args[0]):
     680            ans = f.solve(*args,**kwds)
     681            return ans
     682        else:
     683            f = [f]
    652684
    653685    if not isinstance(f, (list, tuple)):
    654686        raise TypeError("The first argument must be a symbolic expression or a list of symbolic expressions.")
    655687
    656688    if len(f)==1 and is_Expression(f[0]):
    657689        # f is a list with a single expression
    658         return f[0].solve(*args,**kwds)
     690        if len(args) == 1 and is_SymbolicVariable(args[0]):
     691            ans = f[0].solve(*args,**kwds)
     692            return ans
    659693
    660694    # f is a list of such expressions or equations
    661     from sage.symbolic.ring import is_SymbolicVariable
    662695
    663696    if len(args)==0:
    664697        raise TypeError, "Please input variables to solve for."
    665698    if is_SymbolicVariable(args[0]):
    666699        variables = args
    667700    else:
    668         variables = tuple(args[0])
    669    
     701        try:
     702            variables = tuple(args[0])
     703        except TypeError:
     704            raise TypeError, "%s is not a valid variable."%args[0]
     705
    670706    for v in variables:
    671707        if not is_SymbolicVariable(v):
    672708            raise TypeError, "%s is not a valid variable."%v