Python sympy.srepr() Examples

The following are 6 code examples of sympy.srepr(). You can vote up the ones you like or vote down the ones you don't like, and go to the original project or source file by following the links above each example. You may also want to check out all available functions/classes of the module sympy , or try the search function .
Example #1
Source File: test_dot.py    From Computable with MIT License 5 votes vote down vote up
def test_labelfunc():
    text = dotprint(x + 2, labelfunc=srepr)
    assert "Symbol('x')" in text
    assert "Integer(2)" in text 
Example #2
Source File: test_hilbert.py    From sympsi with BSD 3-Clause "New" or "Revised" License 5 votes vote down vote up
def test_hilbert_space():
    hs = HilbertSpace()
    assert isinstance(hs, HilbertSpace)
    assert sstr(hs) == 'H'
    assert srepr(hs) == 'HilbertSpace()' 
Example #3
Source File: test_hilbert.py    From sympsi with BSD 3-Clause "New" or "Revised" License 5 votes vote down vote up
def test_complex_space():
    c1 = ComplexSpace(2)
    assert isinstance(c1, ComplexSpace)
    assert c1.dimension == 2
    assert sstr(c1) == 'C(2)'
    assert srepr(c1) == 'ComplexSpace(Integer(2))'

    n = Symbol('n')
    c2 = ComplexSpace(n)
    assert isinstance(c2, ComplexSpace)
    assert c2.dimension == n
    assert sstr(c2) == 'C(n)'
    assert srepr(c2) == "ComplexSpace(Symbol('n'))"
    assert c2.subs(n, 2) == ComplexSpace(2) 
Example #4
Source File: parameterexpression.py    From qiskit-terra with Apache License 2.0 5 votes vote down vote up
def __eq__(self, other):
        from sympy import srepr
        return (isinstance(other, ParameterExpression)
                and self.parameters == other.parameters
                and srepr(self._symbol_expr) == srepr(other._symbol_expr)) 
Example #5
Source File: _compat.py    From Cirq with Apache License 2.0 5 votes vote down vote up
def proper_repr(value: Any) -> str:
    """Overrides sympy and numpy returning repr strings that don't parse."""

    if isinstance(value, sympy.Basic):
        result = sympy.srepr(value)

        # HACK: work around https://github.com/sympy/sympy/issues/16074
        # (only handles a few cases)
        fixed_tokens = [
            'Symbol', 'pi', 'Mul', 'Pow', 'Add', 'Mod', 'Integer', 'Float',
            'Rational'
        ]
        for token in fixed_tokens:
            result = result.replace(token, 'sympy.' + token)

        return result

    if isinstance(value, np.ndarray):
        return 'np.array({!r}, dtype=np.{})'.format(value.tolist(), value.dtype)

    if isinstance(value, pd.MultiIndex):
        return (f'pd.MultiIndex.from_tuples({repr(list(value))}, '
                f'names={repr(list(value.names))})')

    if isinstance(value, pd.Index):
        return (f'pd.Index({repr(list(value))}, '
                f'name={repr(value.name)}, '
                f'dtype={repr(str(value.dtype))})')

    if isinstance(value, pd.DataFrame):
        cols = [value[col].tolist() for col in value.columns]
        rows = list(zip(*cols))
        return (f'pd.DataFrame('
                f'\n    columns={proper_repr(value.columns)}, '
                f'\n    index={proper_repr(value.index)}, '
                f'\n    data={repr(rows)}'
                f'\n)')

    return repr(value) 
Example #6
Source File: sympy.py    From qupulse with MIT License 5 votes vote down vote up
def substitute_with_eval(expression: sympy.Expr,
                         substitutions: Dict[str, Union[sympy.Expr, numpy.ndarray, str]]) -> sympy.Expr:
    """Substitutes only sympy.Symbols. Workaround for numpy like array behaviour. ~Factor 3 slower compared to subs"""
    substitutions = {k: v if isinstance(v, sympy.Expr) else sympify(v)
                     for k, v in substitutions.items()}

    for symbol in get_free_symbols(expression):
        symbol_name = str(symbol)
        if symbol_name not in substitutions:
            substitutions[symbol_name] = symbol

    string_representation = sympy.srepr(expression)
    return eval(string_representation, sympy.__dict__, {'Symbol': substitutions.__getitem__,
                                                        'Mul': numpy_compatible_mul})