ó
¡¼™\c           @  sÑ   d  Z  d d l m Z m Z d d l m Z d d l m Z d d l	 m
 Z
 m Z m Z d d l m Z d e f d	 „  ƒ  YZ i  Z d
 e f d „  ƒ  YZ d „  Z e e e e e d „ Z d „  Z d „  Z d S(   s0   sympify -- convert objects SymPy internal formatiÿÿÿÿ(   t   print_functiont   division(   t   getmroi   (   t   all_classes(   t   iterablet   string_typest   range(   t   global_evaluatet   SympifyErrorc           B  s   e  Z d d  „ Z d „  Z RS(   c         C  s   | |  _  | |  _ d  S(   N(   t   exprt   base_exc(   t   selfR	   R
   (    (    s1   lib/python2.7/site-packages/sympy/core/sympify.pyt   __init__   s    	c         C  sC   |  j  d  k r d |  j f Sd |  j |  j  j j t |  j  ƒ f S(   Ns   SympifyError: %rsL   Sympify of expression '%s' failed, because of exception being raised:
%s: %s(   R
   t   NoneR	   t	   __class__t   __name__t   str(   R   (    (    s1   lib/python2.7/site-packages/sympy/core/sympify.pyt   __str__   s
    N(   R   t
   __module__R   R   R   (    (    (    s1   lib/python2.7/site-packages/sympy/core/sympify.pyR      s   t   CantSympifyc           B  s   e  Z d  Z RS(   s½  
    Mix in this trait to a class to disallow sympification of its instances.

    Examples
    ========

    >>> from sympy.core.sympify import sympify, CantSympify

    >>> class Something(dict):
    ...     pass
    ...
    >>> sympify(Something())
    {}

    >>> class Something(dict, CantSympify):
    ...     pass
    ...
    >>> sympify(Something())
    Traceback (most recent call last):
    ...
    SympifyError: SympifyError: {}

    (   R   R   t   __doc__(    (    (    s1   lib/python2.7/site-packages/sympy/core/sympify.pyR      s   c         K  sú   d d l  } t |  | j ƒ sW | j |  ƒ rA t t |  j ƒ  ƒ St |  j ƒ  |  SnŸ y{ d d l m	 } | j
 |  ƒ j d } t t | j | j |  ƒ d | j |  ƒ f ƒ d ƒ ƒ d d !}  | |  d | ƒSWn! t k
 rõ t d |  ƒ ‚ n Xd S(   sG   
    Converts a numpy datatype input to an appropriate sympy type.
    iÿÿÿÿN(   t   Floati   i    t	   precisions3   Translation for numpy float : %s is not implemented(   t   numpyt
   isinstancet   floatingt	   iscomplext	   convertert   complext   itemt   sympifyt   sympy.core.numbersR   t   finfot   nmantR   t   listt   reshapet   asarrayt   sizet   NotImplementedErrorR   (   t   at   sympify_argst   npR   t   prec(    (    s1   lib/python2.7/site-packages/sympy/core/sympify.pyt   _convert_numpy_types6   s    )c         C  sÜ  | d k r2 t d t k r) t d } q2 t } n  y |  t k rE |  SWn t k
 rY n Xt |  d d ƒ } | d k r‡ t |  ƒ } n  | t k r— |  S| t d ƒ k rÅ | r¾ t |  ƒ ‚ qÅ |  Sn  t |  ƒ j	 d k r d d l
 } | j |  ƒ r t |  d | d | d | d	 | d
 | ƒSn  y t | |  ƒ SWnQ t k
 r…xA t | ƒ D]/ } y t | |  ƒ SWqOt k
 r}qOqOXqOWn Xt |  t ƒ r¤t |  ƒ ‚ n  t |  d d ƒ }	 |	 d k	 rçy |  j ƒ  SWqçt k
 rãqçXn  | sRt |  d d ƒ }
 |
 d k	 rRt |  d d ƒ } | d k	 rOd d l m } | |  j |  j ƒ SqRn  t |  t ƒ sàx| t t f D]k } y | |  ƒ } Wn- t t f k
 r qnn t k
 r³qnn Xy t | ƒ SWqnt k
 rØqnqnXqnWn  | rõt |  ƒ ‚ n  t |  ƒ rWy? t |  ƒ g  |  D]$ } t | d | d | d	 | ƒ^ qƒ SWqWt k
 rSqWXn  t |  t ƒ rÂyE t |  ƒ g  |  j ƒ  D]$ } t | d | d | d	 | ƒ^ qƒ SWqÂt k
 r¾qÂXn  y  d d l  m! } | |  ƒ }  Wn" t" k
 r} t |  | ƒ ‚ n Xd d l# m$ } m% } m& } d d l# m' } d d l# m( } | } | r_| | f 7} n  | ru| | f 7} n  y4 |  j) d d ƒ }  | |  d | d | d
 | ƒ} Wn, | t* f k
 r×} t d |  | ƒ ‚ n X| S(   sO  Converts an arbitrary expression to a type that can be used inside SymPy.

    For example, it will convert Python ints into instances of sympy.Integer,
    floats into instances of sympy.Float, etc. It is also able to coerce symbolic
    expressions which inherit from Basic. This can be useful in cooperation
    with SAGE.

    It currently accepts as arguments:
       - any object defined in sympy
       - standard numeric python types: int, long, float, Decimal
       - strings (like "0.09" or "2e-19")
       - booleans, including ``None`` (will leave ``None`` unchanged)
       - lists, sets or tuples containing any of the above

    .. warning::
        Note that this function uses ``eval``, and thus shouldn't be used on
        unsanitized input.

    If the argument is already a type that SymPy understands, it will do
    nothing but return that value. This can be used at the beginning of a
    function to ensure you are working with the correct type.

    >>> from sympy import sympify

    >>> sympify(2).is_integer
    True
    >>> sympify(2).is_real
    True

    >>> sympify(2.0).is_real
    True
    >>> sympify("2.0").is_real
    True
    >>> sympify("2e-45").is_real
    True

    If the expression could not be converted, a SympifyError is raised.

    >>> sympify("x***2")
    Traceback (most recent call last):
    ...
    SympifyError: SympifyError: "could not parse u'x***2'"

    Locals
    ------

    The sympification happens with access to everything that is loaded
    by ``from sympy import *``; anything used in a string that is not
    defined by that import will be converted to a symbol. In the following,
    the ``bitcount`` function is treated as a symbol and the ``O`` is
    interpreted as the Order object (used with series) and it raises
    an error when used improperly:

    >>> s = 'bitcount(42)'
    >>> sympify(s)
    bitcount(42)
    >>> sympify("O(x)")
    O(x)
    >>> sympify("O + 1")
    Traceback (most recent call last):
    ...
    TypeError: unbound method...

    In order to have ``bitcount`` be recognized it can be imported into a
    namespace dictionary and passed as locals:

    >>> from sympy.core.compatibility import exec_
    >>> ns = {}
    >>> exec_('from sympy.core.evalf import bitcount', ns)
    >>> sympify(s, locals=ns)
    6

    In order to have the ``O`` interpreted as a Symbol, identify it as such
    in the namespace dictionary. This can be done in a variety of ways; all
    three of the following are possibilities:

    >>> from sympy import Symbol
    >>> ns["O"] = Symbol("O")  # method 1
    >>> exec_('from sympy.abc import O', ns)  # method 2
    >>> ns.update(dict(O=Symbol("O")))  # method 3
    >>> sympify("O + 1", locals=ns)
    O + 1

    If you want *all* single-letter and Greek-letter variables to be symbols
    then you can use the clashing-symbols dictionaries that have been defined
    there as private variables: _clash1 (single-letter variables), _clash2
    (the multi-letter Greek names) or _clash (both single and multi-letter
    names that are defined in abc).

    >>> from sympy.abc import _clash1
    >>> _clash1
    {'C': C, 'E': E, 'I': I, 'N': N, 'O': O, 'Q': Q, 'S': S}
    >>> sympify('I & Q', _clash1)
    I & Q

    Strict
    ------

    If the option ``strict`` is set to ``True``, only the types for which an
    explicit conversion has been defined are converted. In the other
    cases, a SympifyError is raised.

    >>> print(sympify(None))
    None
    >>> sympify(None, strict=True)
    Traceback (most recent call last):
    ...
    SympifyError: SympifyError: None

    Evaluation
    ----------

    If the option ``evaluate`` is set to ``False``, then arithmetic and
    operators will be converted into their SymPy equivalents and the
    ``evaluate=False`` option will be added. Nested ``Add`` or ``Mul`` will
    be denested first. This is done via an AST transformation that replaces
    operators with their SymPy equivalents, so if an operand redefines any
    of those operations, the redefined operators will not be used.

    >>> sympify('2**2 / 3 + 5')
    19/3
    >>> sympify('2**2 / 3 + 5', evaluate=False)
    2**2/3 + 5

    Extending
    ---------

    To extend ``sympify`` to convert custom objects (not derived from ``Basic``),
    just define a ``_sympy_`` method to your class. You can do that even to
    classes that you do not own by subclassing or adding the method at runtime.

    >>> from sympy import Matrix
    >>> class MyList1(object):
    ...     def __iter__(self):
    ...         yield 1
    ...         yield 2
    ...         return
    ...     def __getitem__(self, i): return list(self)[i]
    ...     def _sympy_(self): return Matrix(self)
    >>> sympify(MyList1())
    Matrix([
    [1],
    [2]])

    If you do not have control over the class definition you could also use the
    ``converter`` global dictionary. The key is the class and the value is a
    function that takes a single argument and returns the desired SymPy
    object, e.g. ``converter[MyList] = lambda x: Matrix(x)``.

    >>> class MyList2(object):   # XXX Do not do this if you control the class!
    ...     def __iter__(self):  #     Use _sympy_!
    ...         yield 1
    ...         yield 2
    ...         return
    ...     def __getitem__(self, i): return list(self)[i]
    >>> from sympy.core.sympify import converter
    >>> converter[MyList2] = lambda x: Matrix(x)
    >>> sympify(MyList2())
    Matrix([
    [1],
    [2]])

    Notes
    =====

    Sometimes autosimplification during sympification results in expressions
    that are very different in structure than what was entered. Until such
    autosimplification is no longer done, the ``kernS`` function might be of
    some use. In the example below you can see how an expression reduces to
    -1 by autosimplification, but does not do so when ``kernS`` is used.

    >>> from sympy.core.sympify import kernS
    >>> from sympy.abc import x
    >>> -2*(-(-x + 1/x)/(x*(x - 1/x)**2) - 1/(x*(x - 1/x))) - 1
    -1
    >>> s = '-2*(-(-x + 1/x)/(x*(x - 1/x)**2) - 1/(x*(x - 1/x))) - 1'
    >>> sympify(s)
    -1
    >>> kernS(s)
    -2*(-(-x + 1/x)/(x*(x - 1/x)**2) - 1/(x*(x - 1/x))) - 1

    i    R   R   iÿÿÿÿNt   localst   convert_xort   strictt   rationalt   evaluatet   _sympy_t   flatt   shapei   (   t   Arrayi   (   t   unicode(   t
   parse_exprt
   TokenErrort   standard_transformations(   R-   (   t   rationalizes   
t    t
   local_dictt   transformationss   could not parse %r(+   R   R   t   Falset   Truet   sympy_classest	   TypeErrort   getattrt   typeR   R   R   t   isscalarR+   R   t   KeyErrorR   R   R   R1   t   AttributeErrort   tensor.arrayR4   R2   R3   R   t   floatt   intt
   ValueErrorR   R   t   dictt   itemst   compatibilityR5   t	   Exceptiont   sympy.parsing.sympy_parserR6   R7   R8   R-   R9   t   replacet   SyntaxError(   R'   R,   R-   R.   R/   R0   t   clsR)   t
   superclassR1   R2   R3   R4   t   coercet   coercedt   xR5   t   excR6   R7   R8   t   t_convert_xort   t_rationalizeR<   R	   (    (    s1   lib/python2.7/site-packages/sympy/core/sympify.pyR   N   s¸    ¸	
39
"c         C  s   t  |  d t ƒS(   sI  
    Short version of sympify for internal usage for __add__ and __eq__ methods
    where it is ok to allow some things (like Python integers and floats) in
    the expression. This excludes things (like strings) that are unwise to
    allow into such an expression.

    >>> from sympy import Integer
    >>> Integer(1) == 1
    True

    >>> Integer(1) == '1'
    False

    >>> from sympy.abc import x
    >>> x + 1
    x + 1

    >>> x + '1'
    Traceback (most recent call last):
    ...
    TypeError: unsupported operand type(s) for +: 'Symbol' and 'str'

    see: sympify

    R.   (   R   R>   (   R'   (    (    s1   lib/python2.7/site-packages/sympy/core/sympify.pyt   _sympify‡  s    c           sÅ  d d l  } d d l m } d d l m } t } d |  k pG d |  k } d |  k r0| r0|  j d ƒ |  j d ƒ k rŠ t d	 ƒ ‚ n  d
 j |  j	 ƒ  ƒ }  |  } |  j
 d d ƒ }  |  j
 d d ƒ }  d } |  j
 d | ƒ }  d } }	 | j d ƒ s t ‚ xÊ t rÌ|  j | | ƒ }
 |
 d k r+Pn  |
 t | ƒ d 7}
 xg t |
 t |  ƒ ƒ D]P }
 |  |
 d k rx|	 d 7}	 n |  |
 d k r•|	 d 8}	 n  |	 d k rUPqUqUW|  |
  d |  |
 }  |
 d } qWd |  k r!d } x* | |  k r| | | j | j ƒ 7} qâW|  j
 d | ƒ }  n  | |  k } n  xS t d ƒ D]E } y t |  ƒ } PWq=| rr| }  t } q=n  t |  ƒ } q=Xq=W| s| Si d | | ƒ 6‰ ‡  ‡ f d †  ‰  ˆ  | ƒ } | S(   sÜ  Use a hack to try keep autosimplification from distributing a
    a number into an Add; this modification doesn't
    prevent the 2-arg Mul from becoming an Add, however.

    Examples
    ========

    >>> from sympy.core.sympify import kernS
    >>> from sympy.abc import x, y, z

    The 2-arg Mul distributes a number (or minus sign) across the terms
    of an expression, but kernS will prevent that:

    >>> 2*(x + y), -(x + 1)
    (2*x + 2*y, -x - 1)
    >>> kernS('2*(x + y)')
    2*(x + y)
    >>> kernS('-(x + 1)')
    -(x + 1)

    If use of the hack fails, the un-hacked string will be passed to sympify...
    and you get what you get.

    XXX This hack should not be necessary once issue 4596 has been resolved.
    iÿÿÿÿN(   t   choice(   t   Symbolt   "t   't   (t   )s   unmatched left parenthesisR:   s   *(s   * *(s   ** *s   **s   -( *(s   -(i    i   i   t    t   _c           sg   t  |  t t t f ƒ rA t |  ƒ g  |  D] } ˆ  | ƒ ^ q( ƒ St |  d ƒ rc |  j ˆ d t ƒS|  S(   Nt   subst   hack2(   R   R"   t   tuplet   setRB   t   hasattrRb   R>   (   R	   t   e(   t   _cleart   rep(    s1   lib/python2.7/site-packages/sympy/core/sympify.pyRh   ÿ  s
    )(   t   stringt   randomRZ   t   sympy.core.symbolR[   R=   t   countR   t   joint   splitRO   t   endswitht   AssertionErrorR>   t   findt   lenR   t   ascii_letterst   digitsR   (   t   sRj   RZ   R[   t   hitt   quotedt   oldst   targett   it   nestt   jt   kernR	   (    (   Rh   Ri   s1   lib/python2.7/site-packages/sympy/core/sympify.pyt   kernS¤  sh    
	N(   R   t
   __future__R    R   t   inspectR   t   coreR   R?   RL   R   R   R   R0   R   RI   R   R   t   objectR   R+   R   R>   R=   R   RY   R   (    (    (    s1   lib/python2.7/site-packages/sympy/core/sympify.pyt   <module>   s   	ÿ 9	