Spaces:
Sleeping
Sleeping
File size: 4,648 Bytes
6a86ad5 |
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 113 114 115 116 117 118 119 120 121 122 123 124 125 126 127 128 129 130 131 132 133 134 135 |
from sympy.printing.dot import (purestr, styleof, attrprint, dotnode,
dotedges, dotprint)
from sympy.core.basic import Basic
from sympy.core.expr import Expr
from sympy.core.numbers import (Float, Integer)
from sympy.core.singleton import S
from sympy.core.symbol import (Symbol, symbols)
from sympy.printing.repr import srepr
from sympy.abc import x
def test_purestr():
assert purestr(Symbol('x')) == "Symbol('x')"
assert purestr(Basic(S(1), S(2))) == "Basic(Integer(1), Integer(2))"
assert purestr(Float(2)) == "Float('2.0', precision=53)"
assert purestr(Symbol('x'), with_args=True) == ("Symbol('x')", ())
assert purestr(Basic(S(1), S(2)), with_args=True) == \
('Basic(Integer(1), Integer(2))', ('Integer(1)', 'Integer(2)'))
assert purestr(Float(2), with_args=True) == \
("Float('2.0', precision=53)", ())
def test_styleof():
styles = [(Basic, {'color': 'blue', 'shape': 'ellipse'}),
(Expr, {'color': 'black'})]
assert styleof(Basic(S(1)), styles) == {'color': 'blue', 'shape': 'ellipse'}
assert styleof(x + 1, styles) == {'color': 'black', 'shape': 'ellipse'}
def test_attrprint():
assert attrprint({'color': 'blue', 'shape': 'ellipse'}) == \
'"color"="blue", "shape"="ellipse"'
def test_dotnode():
assert dotnode(x, repeat=False) == \
'"Symbol(\'x\')" ["color"="black", "label"="x", "shape"="ellipse"];'
assert dotnode(x+2, repeat=False) == \
'"Add(Integer(2), Symbol(\'x\'))" ' \
'["color"="black", "label"="Add", "shape"="ellipse"];', \
dotnode(x+2,repeat=0)
assert dotnode(x + x**2, repeat=False) == \
'"Add(Symbol(\'x\'), Pow(Symbol(\'x\'), Integer(2)))" ' \
'["color"="black", "label"="Add", "shape"="ellipse"];'
assert dotnode(x + x**2, repeat=True) == \
'"Add(Symbol(\'x\'), Pow(Symbol(\'x\'), Integer(2)))_()" ' \
'["color"="black", "label"="Add", "shape"="ellipse"];'
def test_dotedges():
assert sorted(dotedges(x+2, repeat=False)) == [
'"Add(Integer(2), Symbol(\'x\'))" -> "Integer(2)";',
'"Add(Integer(2), Symbol(\'x\'))" -> "Symbol(\'x\')";'
]
assert sorted(dotedges(x + 2, repeat=True)) == [
'"Add(Integer(2), Symbol(\'x\'))_()" -> "Integer(2)_(0,)";',
'"Add(Integer(2), Symbol(\'x\'))_()" -> "Symbol(\'x\')_(1,)";'
]
def test_dotprint():
text = dotprint(x+2, repeat=False)
assert all(e in text for e in dotedges(x+2, repeat=False))
assert all(
n in text for n in [dotnode(expr, repeat=False)
for expr in (x, Integer(2), x+2)])
assert 'digraph' in text
text = dotprint(x+x**2, repeat=False)
assert all(e in text for e in dotedges(x+x**2, repeat=False))
assert all(
n in text for n in [dotnode(expr, repeat=False)
for expr in (x, Integer(2), x**2)])
assert 'digraph' in text
text = dotprint(x+x**2, repeat=True)
assert all(e in text for e in dotedges(x+x**2, repeat=True))
assert all(
n in text for n in [dotnode(expr, pos=())
for expr in [x + x**2]])
text = dotprint(x**x, repeat=True)
assert all(e in text for e in dotedges(x**x, repeat=True))
assert all(
n in text for n in [dotnode(x, pos=(0,)), dotnode(x, pos=(1,))])
assert 'digraph' in text
def test_dotprint_depth():
text = dotprint(3*x+2, depth=1)
assert dotnode(3*x+2) in text
assert dotnode(x) not in text
text = dotprint(3*x+2)
assert "depth" not in text
def test_Matrix_and_non_basics():
from sympy.matrices.expressions.matexpr import MatrixSymbol
n = Symbol('n')
assert dotprint(MatrixSymbol('X', n, n)) == \
"""digraph{
# Graph style
"ordering"="out"
"rankdir"="TD"
#########
# Nodes #
#########
"MatrixSymbol(Str('X'), Symbol('n'), Symbol('n'))_()" ["color"="black", "label"="MatrixSymbol", "shape"="ellipse"];
"Str('X')_(0,)" ["color"="blue", "label"="X", "shape"="ellipse"];
"Symbol('n')_(1,)" ["color"="black", "label"="n", "shape"="ellipse"];
"Symbol('n')_(2,)" ["color"="black", "label"="n", "shape"="ellipse"];
#########
# Edges #
#########
"MatrixSymbol(Str('X'), Symbol('n'), Symbol('n'))_()" -> "Str('X')_(0,)";
"MatrixSymbol(Str('X'), Symbol('n'), Symbol('n'))_()" -> "Symbol('n')_(1,)";
"MatrixSymbol(Str('X'), Symbol('n'), Symbol('n'))_()" -> "Symbol('n')_(2,)";
}"""
def test_labelfunc():
text = dotprint(x + 2, labelfunc=srepr)
assert "Symbol('x')" in text
assert "Integer(2)" in text
def test_commutative():
x, y = symbols('x y', commutative=False)
assert dotprint(x + y) == dotprint(y + x)
assert dotprint(x*y) != dotprint(y*x)
|