modelparameters.sympy.parsing package

Submodules

modelparameters.sympy.parsing.ast_parser module

This module implements the functionality to take any Python expression as a string and fix all numbers and other things before evaluating it, thus

1/2

returns

Integer(1)/Integer(2)

We use the Python ast module for that, which is in python2.6 and later. It is well documented at docs.python.org.

Some tips to understand how this works: use dump() to get a nice representation of any node. Then write a string of what you want to get, e.g. “Integer(1)”, parse it, dump it and you’ll see that you need to do “Call(Name(‘Integer’, Load()), [node], [], None, None)”. You don’t need to bother with lineno and col_offset, just call fix_missing_locations() before returning the node.

class modelparameters.sympy.parsing.ast_parser.Transform(local_dict, global_dict)[source]

Bases: NodeTransformer

visit_Lambda(node)[source]
visit_Name(node)[source]
visit_Num(node)[source]
modelparameters.sympy.parsing.ast_parser.parse_expr(s, local_dict)[source]

Converts the string “s” to a SymPy expression, in local_dict.

It converts all numbers to Integers before feeding it to Python and automatically creates Symbols.

modelparameters.sympy.parsing.mathematica module

modelparameters.sympy.parsing.mathematica.mathematica(s)[source]
modelparameters.sympy.parsing.mathematica.parse(s)[source]
modelparameters.sympy.parsing.mathematica.translateFunction(s)[source]
modelparameters.sympy.parsing.mathematica.translateOperator(s)[source]

modelparameters.sympy.parsing.maxima module

class modelparameters.sympy.parsing.maxima.MaximaHelpers[source]

Bases: object

maxima_csc()[source]
maxima_expand()[source]
maxima_float()[source]
maxima_product(a2, a3, a4)[source]
maxima_sec()[source]
maxima_sum(a2, a3, a4)[source]
maxima_trigexpand()[source]
modelparameters.sympy.parsing.maxima.parse_maxima(str, globals=None, name_dict={})[source]

modelparameters.sympy.parsing.sympy_parser module

Transform a string with Python-like source code into SymPy expression.

class modelparameters.sympy.parsing.sympy_parser.AppliedFunction(function, args, exponent=None)[source]

Bases: object

A group of tokens representing a function and its arguments.

exponent is for handling the shorthand sin^2, ln^2, etc.

expand()[source]

Return a list of tokens representing the function

class modelparameters.sympy.parsing.sympy_parser.EvaluateFalseTransformer[source]

Bases: NodeTransformer

flatten(args, func)[source]
operators = {<class '_ast.Add'>: 'Add', <class '_ast.Mult'>: 'Mul', <class '_ast.Pow'>: 'Pow', <class '_ast.Sub'>: 'Add', <class '_ast.Div'>: 'Mul', <class '_ast.BitOr'>: 'Or', <class '_ast.BitAnd'>: 'And', <class '_ast.BitXor'>: 'Not'}
visit_BinOp(node)[source]
class modelparameters.sympy.parsing.sympy_parser.ParenthesisGroup(iterable=(), /)[source]

Bases: list

List of tokens representing an expression in parentheses.

modelparameters.sympy.parsing.sympy_parser.auto_number(tokens, local_dict, global_dict)[source]

Converts numeric literals to use SymPy equivalents.

Complex numbers use I; integer literals use Integer, float literals use Float, and repeating decimals use Rational.

modelparameters.sympy.parsing.sympy_parser.auto_symbol(tokens, local_dict, global_dict)[source]

Inserts calls to Symbol for undefined variables.

modelparameters.sympy.parsing.sympy_parser.convert_equals_signs(result, local_dict, global_dict)[source]

Transforms all the equals signs = to instances of Eq.

Parses the equals signs in the expression and replaces them with appropriate Eq instances.Also works with nested equals signs.

Does not yet play well with function arguments. For example, the expression (x=y) is ambiguous and can be interpreted as x being an argument to a function and convert_equals_signs won’t work for this.

See also

convert_equality_operators

>>> from .sympy_parser import (parse_expr,
... standard_transformations, convert_equals_signs)
>>> parse_expr("1*2=x", transformations=(
... standard_transformations + (convert_equals_signs,)))
Eq(2, x)
>>> parse_expr("(1*2=x)=False", transformations=(
... standard_transformations + (convert_equals_signs,)))
Eq(Eq(2, x), False)
modelparameters.sympy.parsing.sympy_parser.convert_xor(tokens, local_dict, global_dict)[source]

Treats XOR, ^, as exponentiation, **.

modelparameters.sympy.parsing.sympy_parser.eval_expr(code, local_dict, global_dict)[source]

Evaluate Python code generated by stringify_expr.

Generally, parse_expr should be used.

modelparameters.sympy.parsing.sympy_parser.evaluateFalse(s)[source]

Replaces operators with the SymPy equivalent and sets evaluate=False.

modelparameters.sympy.parsing.sympy_parser.factorial_notation(tokens, local_dict, global_dict)[source]

Allows standard notation for factorial.

modelparameters.sympy.parsing.sympy_parser.function_exponentiation(tokens, local_dict, global_dict)[source]

Allows functions to be exponentiated, e.g. cos**2(x).

Examples

>>> from .sympy_parser import (parse_expr,
... standard_transformations, function_exponentiation)
>>> transformations = standard_transformations + (function_exponentiation,)
>>> parse_expr('sin**4(x)', transformations=transformations)
sin(x)**4
modelparameters.sympy.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 like sin 2x will be parsed as x * sin(2) rather than sin(2*x).

Examples

>>> from .sympy_parser import (parse_expr,
... standard_transformations, implicit_application)
>>> transformations = standard_transformations + (implicit_application,)
>>> parse_expr('cot z + csc z', transformations=transformations)
cot(z) + csc(z)
modelparameters.sympy.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 like sin 2x will be parsed as x * sin(2) rather than sin(2*x).

Examples

>>> from .sympy_parser import (parse_expr,
... standard_transformations, implicit_multiplication)
>>> transformations = standard_transformations + (implicit_multiplication,)
>>> parse_expr('3 x y', transformations=transformations)
3*x*y
modelparameters.sympy.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

>>> from .sympy_parser import (parse_expr,
... standard_transformations, implicit_multiplication_application)
>>> 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)
modelparameters.sympy.parsing.sympy_parser.iskeyword()

x.__contains__(y) <==> y in x.

modelparameters.sympy.parsing.sympy_parser.lambda_notation(tokens, local_dict, global_dict)[source]

Substitutes “lambda” with its Sympy equivalent Lambda(). However, the conversion doesn’t take place if only “lambda” is passed because that is a syntax error.

modelparameters.sympy.parsing.sympy_parser.parse_expr(s, local_dict=None, transformations=(<function lambda_notation>, <function auto_symbol>, <function auto_number>, <function factorial_notation>), global_dict=None, evaluate=True)[source]

Converts the string s to a SymPy expression, in local_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 .. 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 SymPy equivalents, convert undefined variables into SymPy symbols, and allow the use of standard mathematical factorial notation (e.g. x!).

  • 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

>>> from .sympy_parser import parse_expr
>>> parse_expr("1/2")
1/2
>>> type(_)
<class 'sympy.core.numbers.Half'>
>>> from .sympy_parser import standard_transformations,\
... implicit_multiplication_application
>>> 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)
modelparameters.sympy.parsing.sympy_parser.rationalize(tokens, local_dict, global_dict)[source]

Converts floats into Rational. Run AFTER auto_number.

modelparameters.sympy.parsing.sympy_parser.split_symbols(tokens, local_dict, global_dict)

Splits symbol names for implicit multiplication.

Intended to let expressions like xyz be parsed as x*y*z. Does not split Greek character names, so theta will not become t*h*e*t*a. Generally this should be used with implicit_multiplication.

modelparameters.sympy.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:

>>> from .sympy_parser import (parse_expr, _token_splittable,
... standard_transformations, implicit_multiplication,
... split_symbols_custom)
>>> 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
modelparameters.sympy.parsing.sympy_parser.standard_transformations = (<function lambda_notation>, <function auto_symbol>, <function auto_number>, <function factorial_notation>)

Standard transformations for parse_expr(). Inserts calls to Symbol, Integer, and other SymPy datatypes and allows the use of standard factorial notation (e.g. x!).

modelparameters.sympy.parsing.sympy_parser.stringify_expr(s, local_dict, global_dict, transformations)[source]

Converts the string s to Python code, in local_dict

Generally, parse_expr should be used.

modelparameters.sympy.parsing.sympy_tokenize module

Tokenization help for Python programs.

generate_tokens(readline) is a generator that breaks a stream of text into Python tokens. It accepts a readline-like method which is called repeatedly to get the next line of input (or “” for EOF). It generates 5-tuples with these members:

the token type (see token.py) the token (a string) the starting (row, column) indices of the token (a 2-tuple of ints) the ending (row, column) indices of the token (a 2-tuple of ints) the original line (string)

It is designed to match the working of the Python tokenizer exactly, except that it produces COMMENT tokens for comments and gives type OP for all operators

Older entry points

tokenize_loop(readline, tokeneater) tokenize(readline, tokeneater=printtoken)

are the same, except instead of generating tokens, tokeneater is a callback function to which the 5 fields described above are passed as 5 arguments, each time a new token is found.

modelparameters.sympy.parsing.sympy_tokenize.ISEOF(x)[source]
modelparameters.sympy.parsing.sympy_tokenize.ISNONTERMINAL(x)[source]
modelparameters.sympy.parsing.sympy_tokenize.ISTERMINAL(x)[source]
modelparameters.sympy.parsing.sympy_tokenize.generate_tokens(readline)[source]

The generate_tokens() generator requires one argment, readline, which must be a callable object which provides the same interface as the readline() method of built-in file objects. Each call to the function should return one line of input as a string. Alternately, readline can be a callable function terminating with StopIteration:

readline = open(myfile).next    # Example of alternate readline

The generator produces 5-tuples with these members: the token type; the token string; a 2-tuple (srow, scol) of ints specifying the row and column where the token begins in the source; a 2-tuple (erow, ecol) of ints specifying the row and column where the token ends in the source; and the line on which the token was found. The line passed is the logical line; continuation lines are included.

modelparameters.sympy.parsing.sympy_tokenize.tokenize(readline, tokeneater=<function printtoken>)[source]

The tokenize() function accepts two parameters: one representing the input stream, and one providing an output mechanism for tokenize().

The first parameter, readline, must be a callable object which provides the same interface as the readline() method of built-in file objects. Each call to the function should return one line of input as a string.

The second parameter, tokeneater, must also be a callable object. It is called once for each token, with five arguments, corresponding to the tuples generated by generate_tokens().

modelparameters.sympy.parsing.sympy_tokenize.untokenize(iterable)[source]

Transform tokens back into Python source code.

Each element returned by the iterable must be a token sequence with at least two elements, a token number and token value. If only two tokens are passed, the resulting output is poor.

Round-trip invariant for full input:

Untokenized source will match input source exactly

Round-trip invariant for limited intput:

# Output text will tokenize the back to the input
t1 = [tok[:2] for tok in generate_tokens(f.readline)]
newcode = untokenize(t1)
readline = iter(newcode.splitlines(1)).next
t2 = [tok[:2] for tok in generate_tokens(readline)]
if t1 != t2:
    raise ValueError("t1 should be equal to t2")

Module contents

Used for translating a string into a SymPy expression.