You signed in with another tab or window. Reload to refresh your session.You signed out in another tab or window. Reload to refresh your session.You switched accounts on another tab or window. Reload to refresh your session.Dismiss alert
PyTensor currently does not simplify (-x) ** 2 to x ** 2 during graph
rewrites, even though negation inside an even power is always algebraically
redundant.
Where it comes from
While building a hierarchical PyMC model, I noticed this pattern:
The Normal logp computes ((obs - mu) / sigma)^2. With mu = -theta,
PyTensor inserts an explicit Mul(-1, theta) node before the Pow(..., 2) —
which is pure overhead since (-x)^2 = x^2.
reacted with thumbs up emoji reacted with thumbs down emoji reacted with laugh emoji reacted with hooray emoji reacted with confused emoji reacted with heart emoji reacted with rocket emoji reacted with eyes emoji
Uh oh!
There was an error while loading. Please reload this page.
-
Idea
PyTensor currently does not simplify
(-x) ** 2tox ** 2during graphrewrites, even though negation inside an even power is always algebraically
redundant.
Where it comes from
While building a hierarchical PyMC model, I noticed this pattern:
The Normal logp computes
((obs - mu) / sigma)^2. Withmu = -theta,PyTensor inserts an explicit
Mul(-1, theta)node before thePow(..., 2)—which is pure overhead since
(-x)^2 = x^2.Reproducible code
Impact
Proposed rewrite
More generally:
Pow(Mul(-c, x), n) → Pow(Mul(c, x), n)for known evenn.Suggested location:
pytensor/tensor/rewriting/elemwise.pyHappy to implement this if the approach looks reasonable to maintainers.
Beta Was this translation helpful? Give feedback.
All reactions