提交 eb39ced0 authored 作者: Olivier Breuleux's avatar Olivier Breuleux

cleaned up and tested pprint

上级 4573a825
import unittest
from pprint import *
import tensor as T
import gof
class _test_pp(unittest.TestCase):
def check(self, expr, expected):
s = pp.process(expr)
self.failUnless(s == expected, "for (%s), pp produced (%s)" % (expected, s))
def test_operator_precedence(self):
x, y, z = T.matrices('xyz')
self.check(x + y * z, "x + y * z")
self.check((x + y) * z, "(x + y) * z")
self.check(x * y ** z, "x * y ** z")
self.check(z ** x * y, "z ** x * y")
self.check((x * y) ** z, "(x * y) ** z")
self.check(z ** (x * y), "z ** (x * y)")
def test_unary_minus_precedence(self):
x, y, z = T.matrices('xyz')
self.check(-x+y, "-x + y")
self.check(-(x*y), "-(x * y)")
self.check((-x)*y, "-x * y")
self.check(x*-y, "x * -y")
self.check(x/-y, "x / -y")
self.check(-x**y, "-x ** y")
self.check((-x)**y, "(-x) ** y")
self.check(-(x**y), "-x ** y")
self.check(x**-y, "x ** (-y)")
def test_parenthesizing(self):
x, y, z = T.matrices('xyz')
self.check(x * (y * z), "x * y * z")
self.check(x / (y / z) / x, "x / (y / z) / x")
self.check((x ** y) ** z, "(x ** y) ** z")
self.check(x / (y * z), "x / (y * z)")
self.check(x * (y / z), "x * y / z")
self.check(x / y * z, "x / y * z")
if __name__ == '__main__':
unittest.main()
import tensor as T
import gof import gof
from copy import copy
class PrinterState(gof.utils.scratchpad): class PrinterState(gof.utils.scratchpad):
...@@ -31,8 +33,6 @@ class OperatorPrinter: ...@@ -31,8 +33,6 @@ class OperatorPrinter:
outer_assoc = getattr(pstate, 'assoc', 'none') outer_assoc = getattr(pstate, 'assoc', 'none')
if outer_precedence > self.precedence: if outer_precedence > self.precedence:
parenthesize = True parenthesize = True
#elif outer_assoc != self.assoc:
# parenthesize = True
else: else:
parenthesize = False parenthesize = False
input_strings = [] input_strings = []
...@@ -81,12 +81,10 @@ class DimShufflePrinter: ...@@ -81,12 +81,10 @@ class DimShufflePrinter:
def process(self, r, pstate): def process(self, r, pstate):
if r.owner is None: if r.owner is None:
raise TypeError("Can only print DimShuffle.") raise TypeError("Can only print DimShuffle.")
elif isinstance(r.owner.op, ShuffleRule): elif isinstance(r.owner.op, T.ShuffleRule):
#print r, r.owner.op new_r = r.owner.op.expand(r.owner)[0]
new_r = r.owner.op.expand(r.owner) return pstate.pprinter.process(new_r, pstate)
#print new_r.owner, isinstance(new_r.owner.op, ShuffleRule) elif isinstance(r.owner.op, T.DimShuffle):
return self.process(new_r, pstate)
elif isinstance(r.owner.op, DimShuffle):
ord = r.owner.op.new_order ord = r.owner.op.new_order
return self.__p(ord, pstate, r.owner.inputs[0]) return self.__p(ord, pstate, r.owner.inputs[0])
else: else:
...@@ -114,26 +112,6 @@ class LeafPrinter: ...@@ -114,26 +112,6 @@ class LeafPrinter:
return str(r) return str(r)
special = dict(middle_dot = u"\u00B7",
big_sigma = u"\u03A3")
greek = dict(alpha = u"\u03B1",
beta = u"\u03B2",
gamma = u"\u03B3",
delta = u"\u03B4",
epsilon = u"\u03B5")
ppow = OperatorPrinter('**', 0, 'right')
pmul = OperatorPrinter('*', -1, 'either')
pdiv = OperatorPrinter('/', -1, 'left')
padd = OperatorPrinter('+', -2, 'either')
psub = OperatorPrinter('-', -2, 'left')
pdot = OperatorPrinter(special['middle_dot'], -1, 'left')
psum = OperatorPrinter(special['big_sigma']+' ', -2, 'left')
plog = FunctionPrinter('log')
class PPrinter: class PPrinter:
def __init__(self): def __init__(self):
...@@ -163,39 +141,41 @@ class PPrinter: ...@@ -163,39 +141,41 @@ class PPrinter:
return cp return cp
special = dict(middle_dot = u"\u00B7",
big_sigma = u"\u03A3")
from tensor import * greek = dict(alpha = u"\u03B1",
from elemwise import Sum, ShuffleRule beta = u"\u03B2",
gamma = u"\u03B3",
x, y, z = matrices('xyz') delta = u"\u03B4",
epsilon = u"\u03B5")
pp = PPrinter()
pp.assign(lambda pstate, r: True, DefaultPrinter())
pp.assign(add, padd)
pp.assign(mul, pmul)
pp.assign(sub, psub)
pp.assign(neg, psub)
pp.assign(div, pdiv)
pp.assign(pow, ppow)
pp.assign(dot, pdot)
pp.assign(Sum(), FunctionPrinter('sum'))
pp.assign(sgrad, FunctionPrinter('d'))
pp.assign(lambda pstate, r: r.owner and isinstance(r.owner.op, DimShuffle), DimShufflePrinter())
pp.assign(lambda pstate, r: r.owner and isinstance(r.owner.op, ShuffleRule), DimShufflePrinter())
# print pp.process(x + y * z) ppow = OperatorPrinter('**', 1, 'right')
# print pp.process((x + y) * z) pneg = OperatorPrinter('-', 0, 'either')
# print pp.process(x * (y * z)) pmul = OperatorPrinter('*', -1, 'either')
# print pp.process(x / (y / z) / x) pdiv = OperatorPrinter('/', -1, 'left')
# print pp.process((x ** y) ** z) padd = OperatorPrinter('+', -2, 'either')
# print pp.process(-x+y) psub = OperatorPrinter('-', -2, 'left')
# print pp.process(-x*y) pdot = OperatorPrinter(special['middle_dot'], -1, 'left')
# print pp.process(sum(x)) psum = OperatorPrinter(special['big_sigma']+' ', -2, 'left')
# print pp.process(sum(x * 10)) plog = FunctionPrinter('log')
# a = Tensor(broadcastable=(False,False,False), dtype='float64')('alpha') def make_default_pp():
# print a.type pp = PPrinter()
# print pp.process(DimShuffle((False,)*2, [1, 0])(x) + a) pp.assign(lambda pstate, r: True, DefaultPrinter())
pp.assign(T.add, padd)
pp.assign(T.mul, pmul)
pp.assign(T.sub, psub)
pp.assign(T.neg, pneg)
pp.assign(T.div, pdiv)
pp.assign(T.pow, ppow)
pp.assign(T.dot, pdot)
pp.assign(T.Sum(), FunctionPrinter('sum'))
pp.assign(T.grad, FunctionPrinter('d'))
pp.assign(lambda pstate, r: r.owner and isinstance(r.owner.op, T.DimShuffle), DimShufflePrinter())
pp.assign(lambda pstate, r: r.owner and isinstance(r.owner.op, T.ShuffleRule), DimShufflePrinter())
return pp
pp = make_default_pp()
# print pp.process(x / (y * z))
Markdown 格式
0%
您添加了 0 到此讨论。请谨慎行事。
请先完成此评论的编辑!
注册 或者 后发表评论