Parsing
Parsing Functions Reference
- diofant.parsing.sympy_parser.parse_expr(s, local_dict=None, transformations=(<function lambda_notation>, <function auto_symbol>, <function auto_number>), global_dict=None, evaluate=True)[source]
Converts the string
s
to a Diofant expression, inlocal_dict
- Parameters:
s (str) – The string to parse.
local_dict (dict, optional) – A dictionary of local variables to use when parsing.
global_dict (dict, optional) – A dictionary of global variables. By default, this is initialized with
from diofant import *
; provide this parameter to override this behavior (for instance, to parse"Q & S"
).transformations (tuple, optional) – A tuple of transformation functions used to modify the tokens of the parsed expression before evaluation. The default transformations convert numeric literals into their Diofant equivalents, convert undefined variables into Diofant symbols.
evaluate (bool, optional) – When False, the order of the arguments will remain as they were in the string and automatic simplification that would normally occur is suppressed. (see examples)
Examples
>>> parse_expr('1/2') 1/2 >>> type(_) <class 'diofant.core.numbers.Half'> >>> transformations = (standard_transformations + ... (implicit_multiplication_application,)) >>> parse_expr('2x', transformations=transformations) 2*x
When evaluate=False, some automatic simplifications will not occur:
>>> parse_expr('2**3'), parse_expr('2**3', evaluate=False) (8, 2**3)
In addition the order of the arguments will not be made canonical. This feature allows one to tell exactly how the expression was entered:
>>> a = parse_expr('1 + x', evaluate=False) >>> b = parse_expr('x + 1', evaluate=0) >>> a == b False >>> a.args (1, x) >>> b.args (x, 1)
- diofant.parsing.sympy_parser.stringify_expr(s, local_dict, global_dict, transformations)[source]
Converts the string
s
to Python code, inlocal_dict
Generally,
parse_expr
should be used.
Parsing Transformations Reference
A transformation is a function that accepts the arguments tokens,
local_dict, global_dict
and returns a list of transformed tokens. They can
be used by passing a list of functions to
parse_expr()
and are
applied in the order given.
- diofant.parsing.sympy_parser.standard_transformations = (<function lambda_notation>, <function auto_symbol>, <function auto_number>)
Standard transformations for
parse_expr()
. Inserts calls toSymbol
,Integer
, and other Diofant datatypes.
- diofant.parsing.sympy_parser.split_symbols(tokens, local_dict, global_dict)[source]
Splits symbol names for implicit multiplication.
Intended to let expressions like
xyz
be parsed asx*y*z
. Does not split Greek character names, sotheta
will not becomet*h*e*t*a
. Generally this should be used withimplicit_multiplication
.
- diofant.parsing.sympy_parser.split_symbols_custom(predicate)[source]
Creates a transformation that splits symbol names.
predicate
should return True if the symbol name is to be split.For instance, to retain the default behavior but avoid splitting certain symbol names, a predicate like this would work:
>>> def can_split(symbol): ... if symbol not in ('list', 'of', 'unsplittable', 'names'): ... return _token_splittable(symbol) ... return False ... >>> transformation = split_symbols_custom(can_split) >>> parse_expr('unsplittable', transformations=standard_transformations + ... (transformation, implicit_multiplication)) unsplittable
- diofant.parsing.sympy_parser.implicit_multiplication(result, local_dict, global_dict)[source]
Makes the multiplication operator optional in most cases.
Use this before
implicit_application()
, otherwise expressions likesin 2x
will be parsed asx * sin(2)
rather thansin(2*x)
.Examples
>>> transformations = standard_transformations + (implicit_multiplication,) >>> parse_expr('3 x y', transformations=transformations) 3*x*y
- diofant.parsing.sympy_parser.implicit_application(result, local_dict, global_dict)[source]
Makes parentheses optional in some cases for function calls.
Use this after
implicit_multiplication()
, otherwise expressions likesin 2x
will be parsed asx * sin(2)
rather thansin(2*x)
.Examples
>>> transformations = standard_transformations + (implicit_application,) >>> parse_expr('cot z + csc z', transformations=transformations) cot(z) + csc(z)
- diofant.parsing.sympy_parser.function_exponentiation(tokens, local_dict, global_dict)[source]
Allows functions to be exponentiated, e.g.
cos**2(x)
.Examples
>>> transformations = standard_transformations + (function_exponentiation,) >>> parse_expr('sin**4(x)', transformations=transformations) sin(x)**4
- diofant.parsing.sympy_parser.implicit_multiplication_application(result, local_dict, global_dict)[source]
Allows a slightly relaxed syntax.
Parentheses for single-argument method calls are optional.
Multiplication is implicit.
Symbol names can be split (i.e. spaces are not needed between symbols).
Functions can be exponentiated.
Examples
>>> parse_expr('10sin**2 x**2 + 3xyz + tan theta', ... transformations=(standard_transformations + ... (implicit_multiplication_application,))) 3*x*y*z + 10*sin(x**2)**2 + tan(theta)
- diofant.parsing.sympy_parser.rationalize(tokens, local_dict, global_dict)[source]
Converts floats into
Rational
. Run AFTERauto_number
.
- diofant.parsing.sympy_parser.convert_xor(tokens, local_dict, global_dict)[source]
Treats XOR,
^
, as exponentiation,**
.
These are included in
:data:diofant.parsing.sympy_parser.standard_transformations
and generally
don’t need to be manually added by the user.