Skip to content
项目
群组
代码片段
帮助
当前项目
正在载入...
登录 / 注册
切换导航面板
P
pytensor
项目
项目
详情
活动
周期分析
仓库
仓库
文件
提交
分支
标签
贡献者
图表
比较
统计图
议题
0
议题
0
列表
看板
标记
里程碑
合并请求
0
合并请求
0
CI / CD
CI / CD
流水线
作业
日程
统计图
Wiki
Wiki
代码片段
代码片段
成员
成员
折叠边栏
关闭边栏
活动
图像
聊天
创建新问题
作业
提交
问题看板
Open sidebar
testgroup
pytensor
Commits
abf20667
提交
abf20667
authored
5月 22, 2008
作者:
Olivier Breuleux
浏览文件
操作
浏览文件
下载
电子邮件补丁
差异文件
pulled pprint
上级
9a4fe7f8
隐藏空白字符变更
内嵌
并排
正在显示
2 个修改的文件
包含
0 行增加
和
223 行删除
+0
-223
_test_pprint.py
_test_pprint.py
+0
-47
pprint.py
pprint.py
+0
-176
没有找到文件。
_test_pprint.py
deleted
100644 → 0
浏览文件 @
9a4fe7f8
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
()
pprint.py
deleted
100644 → 0
浏览文件 @
9a4fe7f8
import
tensor
as
T
import
gof
from
copy
import
copy
class
PrinterState
(
gof
.
utils
.
scratchpad
):
def
__init__
(
self
,
props
=
{},
**
more_props
):
if
isinstance
(
props
,
gof
.
utils
.
scratchpad
):
self
.
__update__
(
props
)
else
:
self
.
__dict__
.
update
(
props
)
self
.
__dict__
.
update
(
more_props
)
def
clone
(
self
,
props
=
{},
**
more_props
):
return
PrinterState
(
self
,
**
dict
(
props
,
**
more_props
))
class
OperatorPrinter
:
def
__init__
(
self
,
operator
,
precedence
,
assoc
=
'left'
):
self
.
operator
=
operator
self
.
precedence
=
precedence
self
.
assoc
=
assoc
def
process
(
self
,
output
,
pstate
):
pprinter
=
pstate
.
pprinter
node
=
output
.
owner
if
node
is
None
:
raise
TypeError
(
"operator
%
s cannot represent a result with no associated operation"
%
self
.
operator
)
outer_precedence
=
getattr
(
pstate
,
'precedence'
,
-
999999
)
outer_assoc
=
getattr
(
pstate
,
'assoc'
,
'none'
)
if
outer_precedence
>
self
.
precedence
:
parenthesize
=
True
else
:
parenthesize
=
False
input_strings
=
[]
max_i
=
len
(
node
.
inputs
)
-
1
for
i
,
input
in
enumerate
(
node
.
inputs
):
if
self
.
assoc
==
'left'
and
i
!=
0
or
self
.
assoc
==
'right'
and
i
!=
max_i
:
s
=
pprinter
.
process
(
input
,
pstate
.
clone
(
precedence
=
self
.
precedence
+
1e-6
))
else
:
s
=
pprinter
.
process
(
input
,
pstate
.
clone
(
precedence
=
self
.
precedence
))
input_strings
.
append
(
s
)
if
len
(
input_strings
)
==
1
:
s
=
self
.
operator
+
input_strings
[
0
]
else
:
s
=
(
"
%
s "
%
self
.
operator
)
.
join
(
input_strings
)
if
parenthesize
:
return
"(
%
s)"
%
s
else
:
return
s
class
FunctionPrinter
:
def
__init__
(
self
,
*
names
):
self
.
names
=
names
def
process
(
self
,
output
,
pstate
):
pprinter
=
pstate
.
pprinter
node
=
output
.
owner
if
node
is
None
:
raise
TypeError
(
"function
%
s cannot represent a result with no associated operation"
%
self
.
function
)
names
=
self
.
names
idx
=
node
.
outputs
.
index
(
output
)
name
=
self
.
names
[
idx
]
return
"
%
s(
%
s)"
%
(
name
,
", "
.
join
([
pprinter
.
process
(
input
,
pstate
.
clone
(
precedence
=
-
1000
))
for
input
in
node
.
inputs
]))
class
DimShufflePrinter
:
def
__p
(
self
,
new_order
,
pstate
,
r
):
if
new_order
!=
()
and
new_order
[
0
]
==
'x'
:
return
"[
%
s]"
%
self
.
__p
(
new_order
[
1
:],
pstate
,
r
)
if
list
(
new_order
)
==
range
(
r
.
type
.
ndim
):
return
pstate
.
pprinter
.
process
(
r
)
if
list
(
new_order
)
==
list
(
reversed
(
range
(
r
.
type
.
ndim
))):
return
"
%
s.T"
%
pstate
.
pprinter
.
process
(
r
)
return
"DimShuffle{
%
s}(
%
s)"
%
(
", "
.
join
(
map
(
str
,
new_order
)),
pstate
.
pprinter
.
process
(
r
))
def
process
(
self
,
r
,
pstate
):
if
r
.
owner
is
None
:
raise
TypeError
(
"Can only print DimShuffle."
)
elif
isinstance
(
r
.
owner
.
op
,
T
.
DimShuffle
):
ord
=
r
.
owner
.
op
.
new_order
return
self
.
__p
(
ord
,
pstate
,
r
.
owner
.
inputs
[
0
])
else
:
raise
TypeError
(
"Can only print DimShuffle."
)
class
DefaultPrinter
:
def
__init__
(
self
):
pass
def
process
(
self
,
r
,
pstate
):
pprinter
=
pstate
.
pprinter
node
=
r
.
owner
if
node
is
None
:
return
LeafPrinter
()
.
process
(
r
,
pstate
)
return
"
%
s(
%
s)"
%
(
str
(
node
.
op
),
", "
.
join
([
pprinter
.
process
(
input
,
pstate
.
clone
(
precedence
=
-
1000
))
for
input
in
node
.
inputs
]))
class
LeafPrinter
:
def
process
(
self
,
r
,
pstate
):
if
r
.
name
in
greek
:
return
greek
[
r
.
name
]
else
:
return
str
(
r
)
class
PPrinter
:
def
__init__
(
self
):
self
.
printers
=
[]
def
assign
(
self
,
condition
,
printer
):
if
isinstance
(
condition
,
gof
.
Op
):
op
=
condition
condition
=
lambda
pstate
,
r
:
r
.
owner
is
not
None
and
r
.
owner
.
op
==
op
self
.
printers
.
insert
(
0
,
(
condition
,
printer
))
def
process
(
self
,
r
,
pstate
=
None
):
if
pstate
is
None
:
pstate
=
PrinterState
(
pprinter
=
self
)
for
condition
,
printer
in
self
.
printers
:
if
condition
(
pstate
,
r
):
return
printer
.
process
(
r
,
pstate
)
def
clone
(
self
):
cp
=
copy
(
self
)
cp
.
printers
=
list
(
self
.
printers
)
return
cp
def
clone_assign
(
self
,
condition
,
printer
):
cp
=
self
.
clone
()
cp
.
assign
(
condition
,
printer
)
return
cp
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
(
'**'
,
1
,
'right'
)
pneg
=
OperatorPrinter
(
'-'
,
0
,
'either'
)
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'
)
def
make_default_pp
():
pp
=
PPrinter
()
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
(
lambda
pstate
,
r
:
r
.
owner
and
isinstance
(
r
.
owner
.
op
,
T
.
DimShuffle
),
DimShufflePrinter
())
return
pp
pp
=
make_default_pp
()
编写
预览
Markdown
格式
0%
重试
或
添加新文件
添加附件
取消
您添加了
0
人
到此讨论。请谨慎行事。
请先完成此评论的编辑!
取消
请
注册
或者
登录
后发表评论