Arrow#
- class discopy.cat.Arrow(inside, dom, cod, _scan=True)[source]#
Bases:
Composable
[Ob
]An arrow is a tuple of composable boxes
inside
with a pair of objectsdom
andcod
as domain and codomain.- Parameters:
inside (tuple[Box, ...]) – The tuple of boxes inside an arrow.
dom (T) – The domain of an arrow, i.e. its input.
cod (T) – The codomain of an arrow, i.e. output
_scan (bool) – Whether to check composition.
Summary
Tip
For code clarity, it is recommended not to initialise arrows directly but to use
Arrow.id()
andArrow.then()
instead. For example:>>> x, y, z = map(Ob, "xyz") >>> f, g = Box('f', x, y), Box('g', y, z) >>> arrow = Arrow.id(x).then(f, g) # Do this... >>> arrow_ = Arrow((f, g), x, z) # ...rather than that! >>> assert arrow == arrow_
Note
Arrows can be indexed and sliced using square brackets. Indexing behaves like that of strings, i.e. when we index an arrow we get an arrow back.
>>> assert (f >> g)[0] == f and (f >> g)[1] == g >>> assert f[:0] == Arrow.id(f.dom) >>> assert f[1:] == Arrow.id(f.cod)
Note
If
dom
orcod
are not instances ofty_factory
, they are automatically cast. This means one can use e.g.int
instead ofOb
, seemonoidal.PRO
.- classmethod id(dom=None)[source]#
The identity arrow with the empty tuple inside, called with
Id
.Note
If
dom
is not provided, we use the default value ofty_factory
.Example
>>> assert Arrow.id() == Id() == Id(Ob()) >>> assert Arrow.id('x') == Id('x') == Id(Ob('x'))
- classmethod zero(dom, cod)[source]#
Return the empty sum with a given domain and codomain.
- Parameters:
dom – The domain of the empty sum.
cod – The codomain of the empty sum.
- property free_symbols: set[sympy.Symbol]#
The free
sympy
symbols in an arrow.Example
>>> from sympy.abc import phi, psi >>> x, y = Ob('x'), Ob('y') >>> f = Box('f', x, y, data={"Alice": [phi + 1]}) >>> g = Box('g', y, x, data={"Bob": [psi / 2]}) >>> diagram = (f >> g).bubble() + Id(x) >>> assert diagram.free_symbols == {phi, psi}
- subs(*args)[source]#
Substitute a variable by an expression.
- Parameters:
var (sympy.Symbol) – The subtituted variable.
expr (sympy.Expr) – The substituting expression.
- Return type:
Tip
You can give a list of
(var, expr)
for multiple substitution.Example
>>> from sympy.abc import phi, psi >>> x, y = Ob('x'), Ob('y') >>> f = Box('f', x, y, data={"Alice": [phi + 1]}) >>> g = Box('g', y, x, data={"Bob": [psi / 2]}) >>> assert (f >> g).subs(phi, phi + 1) == f.subs(phi, phi + 1) >> g >>> assert (f >> g).subs(phi, 1) == f.subs(phi, 1) >> g >>> assert (f >> g).subs(psi, 1) == f >> g.subs(psi, 1)
- lambdify(*symbols, **kwargs)[source]#
Turn a symbolic diagram into a function from parameters to diagram.
- Parameters:
symbols (sympy.Symbol) – The inputs of the function.
kwargs – Passed to
sympy.lambdify
.
- Return type:
Callable
Example
>>> from sympy.abc import phi, psi >>> x, y, z = Ob('x'), Ob('y'), Ob('z') >>> f, g = Box('f', x, y, data=phi), Box('g', y, z, data=psi) >>> assert f.lambdify(psi)(42) == f >>> assert (f >> g).lambdify(phi, psi)(42, 43)\ ... == Box('f', x, y, data=42) >> Box('g', y, z, data=43)
- to_tree()[source]#
Serialise a DisCoPy arrow, see
discopy.utils.dumps()
.Example
>>> from pprint import PrettyPrinter >>> pprint = PrettyPrinter(indent=4, width=70, sort_dicts=False).pprint >>> f = Box('f', 'x', 'y', data=42) >>> pprint((f >> f[::-1]).to_tree()) { 'factory': 'cat.Arrow', 'inside': [ { 'factory': 'cat.Box', 'name': 'f', 'dom': {'factory': 'cat.Ob', 'name': 'x'}, 'cod': {'factory': 'cat.Ob', 'name': 'y'}, 'data': 42}, { 'factory': 'cat.Box', 'name': 'f', 'dom': {'factory': 'cat.Ob', 'name': 'y'}, 'cod': {'factory': 'cat.Ob', 'name': 'x'}, 'is_dagger': True, 'data': 42}], 'dom': {'factory': 'cat.Ob', 'name': 'x'}, 'cod': {'factory': 'cat.Ob', 'name': 'x'}}
- Return type:
dict