Skip to content

Commit fa5eec7

Browse files
committed
Document Transform.__add__ and .__sub__.
1 parent a6c3c22 commit fa5eec7

File tree

2 files changed

+15
-13
lines changed

2 files changed

+15
-13
lines changed

doc/api/transformations.rst

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -14,4 +14,5 @@
1414
BboxTransformFrom, ScaledTranslation, TransformedPath, nonsingular,
1515
interval_contains, interval_contains_open
1616
:show-inheritance:
17+
:special-members:
1718

lib/matplotlib/transforms.py

Lines changed: 14 additions & 13 deletions
Original file line numberDiff line numberDiff line change
@@ -1341,23 +1341,24 @@ def contains_branch_seperately(self, other_transform):
13411341
def __sub__(self, other):
13421342
"""
13431343
Return a transform stack which goes all the way down self's transform
1344-
stack, and then ascends back up other's stack. If it can, this is
1345-
optimised::
1344+
stack, and then ascends back up other's stack. If possible, identical
1345+
terms are cancelled::
13461346
1347-
# normally
1348-
A - B == a + b.inverted()
1347+
# In general:
1348+
A - B == A + B.inverted()
13491349
1350-
# sometimes, when A contains the tree B there is no need to
1351-
# descend all the way down to the base of A (via B), instead we
1352-
# can just stop at B.
1350+
# If A "ends with" B (i.e. A == A' + B for some A') we can cancel
1351+
# out B:
1352+
(A' + B) - B == A'
13531353
1354-
(A + B) - (B)^-1 == A
1354+
# Likewise, if B "starts with" A (B = A + B'), we can cancel out A:
1355+
A - (A + B') == B'.inverted() == B'^-1
13551356
1356-
# similarly, when B contains tree A, we can avoid descending A at
1357-
# all, basically:
1358-
A - (A + B) == ((B + A) - A).inverted() or B^-1
1359-
1360-
For clarity, the result of ``(A + B) - B + B == (A + B)``.
1357+
Cancellation is important not only for performance, but also because it
1358+
avoids floating-point inaccuracies when computing the inverse of B:
1359+
``B - B`` is guaranteed to cancel out exactly (resulting in the
1360+
identity transform), whereas ``B + B.inverted()`` may differ by a small
1361+
epsilon.
13611362
"""
13621363
# we only know how to do this operation if other is a Transform.
13631364
if not isinstance(other, Transform):

0 commit comments

Comments
 (0)