Bubble#
- class discopy.tensor.Bubble(inside, func=<function Bubble.<lambda>>, **params)[source]#
-
Bubble in a tensor diagram, applies a function elementwise.
- Parameters:
inside (tensor.Diagram) – The diagram inside the bubble.
func (callable) – The function to apply, default is
lambda x: int(not x)
.
Examples
>>> men = Box("men", Dim(1), Dim(2), [0, 1]) >>> mortal = Box("mortal", Dim(2), Dim(1), [1, 1]) >>> men_are_mortal = (men >> mortal.bubble()).bubble() >>> assert men_are_mortal.eval() >>> men_are_mortal.draw(draw_type_labels=False, ... path='docs/_static/imgs/tensor/men-are-mortal.png')
>>> from sympy.abc import x >>> f = Box('f', Dim(2), Dim(2), [1, 0, 0, x]) >>> g = Box('g', Dim(2), Dim(2), [-x, 0, 0, 1]) >>> def grad(diagram, var): ... return diagram.bubble( ... func=lambda x: getattr(x, "diff", lambda _: 0)(var), ... drawing_name="d${}$".format(var)) >>> lhs = grad(f >> g, x) >>> rhs = (grad(f, x) >> g) + (f >> grad(g, x)) >>> assert lhs.eval() == rhs.eval() >>> from discopy import drawing >>> drawing.equation(lhs, rhs, figsize=(5, 2), draw_type_labels=False, ... path='docs/_static/imgs/tensor/product-rule.png')
- grad(var, **params)[source]#
The gradient of a bubble is given by the chain rule.
>>> from sympy.abc import x >>> from discopy import drawing >>> g = Box('g', Dim(2), Dim(2), [2 * x, 0, 0, x + 1]) >>> f = lambda d: d.bubble(func=lambda x: x ** 2, drawing_name="f") >>> lhs, rhs = Box.grad(f(g), x), f(g).grad(x) >>> drawing.equation(lhs, rhs, draw_type_labels=False, ... path='docs/_static/imgs/tensor/chain-rule.png')