Category Theory¶
Introduction¶
The category theory module for SymPy will allow manipulating diagrams within a single category, including drawing them in TikZ and deciding whether they are commutative or not.
The general reference work this module tries to follow is
 JoyOfCats
Adamek, H. Herrlich. G. E. Strecker: Abstract and Concrete Categories. The Joy of Cats.
The latest version of this book should be available for free download from
katmat.math.unibremen.de/acc/acc.pdf
The module is still in its preembryonic stage.
Base Class Reference¶
This section lists the classes which implement some of the basic notions in category theory: objects, morphisms, categories, and diagrams.

class
sympy.categories.
Object
(name, **assumptions)[source]¶ The base class for any kind of object in an abstract category.
While technically any instance of
Basic
will do, this class is the recommended way to create abstract objects in abstract categories.

class
sympy.categories.
Morphism
(domain, codomain)[source]¶ The base class for any morphism in an abstract category.
In abstract categories, a morphism is an arrow between two category objects. The object where the arrow starts is called the domain, while the object where the arrow ends is called the codomain.
Two morphisms between the same pair of objects are considered to be the same morphisms. To distinguish between morphisms between the same objects use
NamedMorphism
.It is prohibited to instantiate this class. Use one of the derived classes instead.
See also

property
codomain
¶ Returns the codomain of the morphism.
Examples
>>> from sympy.categories import Object, NamedMorphism >>> A = Object("A") >>> B = Object("B") >>> f = NamedMorphism(A, B, "f") >>> f.codomain Object("B")

compose
(other)[source]¶ Composes self with the supplied morphism.
The order of elements in the composition is the usual order, i.e., to construct \(g\circ f\) use
g.compose(f)
.Examples
>>> from sympy.categories import Object, NamedMorphism >>> A = Object("A") >>> B = Object("B") >>> C = Object("C") >>> f = NamedMorphism(A, B, "f") >>> g = NamedMorphism(B, C, "g") >>> g * f CompositeMorphism((NamedMorphism(Object("A"), Object("B"), "f"), NamedMorphism(Object("B"), Object("C"), "g"))) >>> (g * f).domain Object("A") >>> (g * f).codomain Object("C")

property
domain
¶ Returns the domain of the morphism.
Examples
>>> from sympy.categories import Object, NamedMorphism >>> A = Object("A") >>> B = Object("B") >>> f = NamedMorphism(A, B, "f") >>> f.domain Object("A")

property

class
sympy.categories.
NamedMorphism
(domain, codomain, name)[source]¶ Represents a morphism which has a name.
Names are used to distinguish between morphisms which have the same domain and codomain: two named morphisms are equal if they have the same domains, codomains, and names.
Examples
>>> from sympy.categories import Object, NamedMorphism >>> A = Object("A") >>> B = Object("B") >>> f = NamedMorphism(A, B, "f") >>> f NamedMorphism(Object("A"), Object("B"), "f") >>> f.name 'f'
See also

property
name
¶ Returns the name of the morphism.
Examples
>>> from sympy.categories import Object, NamedMorphism >>> A = Object("A") >>> B = Object("B") >>> f = NamedMorphism(A, B, "f") >>> f.name 'f'

property

class
sympy.categories.
CompositeMorphism
(*components)[source]¶ Represents a morphism which is a composition of other morphisms.
Two composite morphisms are equal if the morphisms they were obtained from (components) are the same and were listed in the same order.
The arguments to the constructor for this class should be listed in diagram order: to obtain the composition \(g\circ f\) from the instances of
Morphism
g
andf
useCompositeMorphism(f, g)
.Examples
>>> from sympy.categories import Object, NamedMorphism, CompositeMorphism >>> A = Object("A") >>> B = Object("B") >>> C = Object("C") >>> f = NamedMorphism(A, B, "f") >>> g = NamedMorphism(B, C, "g") >>> g * f CompositeMorphism((NamedMorphism(Object("A"), Object("B"), "f"), NamedMorphism(Object("B"), Object("C"), "g"))) >>> CompositeMorphism(f, g) == g * f True

property
codomain
¶ Returns the codomain of this composite morphism.
The codomain of the composite morphism is the codomain of its last component.
Examples
>>> from sympy.categories import Object, NamedMorphism >>> A = Object("A") >>> B = Object("B") >>> C = Object("C") >>> f = NamedMorphism(A, B, "f") >>> g = NamedMorphism(B, C, "g") >>> (g * f).codomain Object("C")

property
components
¶ Returns the components of this composite morphism.
Examples
>>> from sympy.categories import Object, NamedMorphism >>> A = Object("A") >>> B = Object("B") >>> C = Object("C") >>> f = NamedMorphism(A, B, "f") >>> g = NamedMorphism(B, C, "g") >>> (g * f).components (NamedMorphism(Object("A"), Object("B"), "f"), NamedMorphism(Object("B"), Object("C"), "g"))

property
domain
¶ Returns the domain of this composite morphism.
The domain of the composite morphism is the domain of its first component.
Examples
>>> from sympy.categories import Object, NamedMorphism >>> A = Object("A") >>> B = Object("B") >>> C = Object("C") >>> f = NamedMorphism(A, B, "f") >>> g = NamedMorphism(B, C, "g") >>> (g * f).domain Object("A")

flatten
(new_name)[source]¶ Forgets the composite structure of this morphism.
If
new_name
is not empty, returns aNamedMorphism
with the supplied name, otherwise returns aMorphism
. In both cases the domain of the new morphism is the domain of this composite morphism and the codomain of the new morphism is the codomain of this composite morphism.Examples
>>> from sympy.categories import Object, NamedMorphism >>> A = Object("A") >>> B = Object("B") >>> C = Object("C") >>> f = NamedMorphism(A, B, "f") >>> g = NamedMorphism(B, C, "g") >>> (g * f).flatten("h") NamedMorphism(Object("A"), Object("C"), "h")

property

class
sympy.categories.
IdentityMorphism
(domain)[source]¶ Represents an identity morphism.
An identity morphism is a morphism with equal domain and codomain, which acts as an identity with respect to composition.
Examples
>>> from sympy.categories import Object, NamedMorphism, IdentityMorphism >>> A = Object("A") >>> B = Object("B") >>> f = NamedMorphism(A, B, "f") >>> id_A = IdentityMorphism(A) >>> id_B = IdentityMorphism(B) >>> f * id_A == f True >>> id_B * f == f True
See also

class
sympy.categories.
Category
(name, objects=EmptySet, commutative_diagrams=EmptySet)[source]¶ An (abstract) category.
A category [JoyOfCats] is a quadruple \(\mbox{K} = (O, \hom, id, \circ)\) consisting of
a (settheoretical) class \(O\), whose members are called \(K\)objects,
for each pair \((A, B)\) of \(K\)objects, a set \(\hom(A, B)\) whose members are called \(K\)morphisms from \(A\) to \(B\),
for a each \(K\)object \(A\), a morphism \(id:A\rightarrow A\), called the \(K\)identity of \(A\),
a composition law \(\circ\) associating with every \(K\)morphisms \(f:A\rightarrow B\) and \(g:B\rightarrow C\) a \(K\)morphism \(g\circ f:A\rightarrow C\), called the composite of \(f\) and \(g\).
Composition is associative, \(K\)identities are identities with respect to composition, and the sets \(\hom(A, B)\) are pairwise disjoint.
This class knows nothing about its objects and morphisms. Concrete cases of (abstract) categories should be implemented as classes derived from this one.
Certain instances of
Diagram
can be asserted to be commutative in aCategory
by supplying the argumentcommutative_diagrams
in the constructor.Examples
>>> from sympy.categories import Object, NamedMorphism, Diagram, Category >>> from sympy import FiniteSet >>> A = Object("A") >>> B = Object("B") >>> C = Object("C") >>> f = NamedMorphism(A, B, "f") >>> g = NamedMorphism(B, C, "g") >>> d = Diagram([f, g]) >>> K = Category("K", commutative_diagrams=[d]) >>> K.commutative_diagrams == FiniteSet(d) True
See also

property
commutative_diagrams
¶ Returns the
FiniteSet
of diagrams which are known to be commutative in this category.>>> from sympy.categories import Object, NamedMorphism, Diagram, Category >>> from sympy import FiniteSet >>> A = Object("A") >>> B = Object("B") >>> C = Object("C") >>> f = NamedMorphism(A, B, "f") >>> g = NamedMorphism(B, C, "g") >>> d = Diagram([f, g]) >>> K = Category("K", commutative_diagrams=[d]) >>> K.commutative_diagrams == FiniteSet(d) True

property
name
¶ Returns the name of this category.
Examples
>>> from sympy.categories import Category >>> K = Category("K") >>> K.name 'K'

property
objects
¶ Returns the class of objects of this category.
Examples
>>> from sympy.categories import Object, Category >>> from sympy import FiniteSet >>> A = Object("A") >>> B = Object("B") >>> K = Category("K", FiniteSet(A, B)) >>> K.objects Class(FiniteSet(Object("A"), Object("B")))

class
sympy.categories.
Diagram
(*args)[source]¶ Represents a diagram in a certain category.
Informally, a diagram is a collection of objects of a category and certain morphisms between them. A diagram is still a monoid with respect to morphism composition; i.e., identity morphisms, as well as all composites of morphisms included in the diagram belong to the diagram. For a more formal approach to this notion see [Pare1970].
The components of composite morphisms are also added to the diagram. No properties are assigned to such morphisms by default.
A commutative diagram is often accompanied by a statement of the following kind: “if such morphisms with such properties exist, then such morphisms which such properties exist and the diagram is commutative”. To represent this, an instance of
Diagram
includes a collection of morphisms which are the premises and another collection of conclusions.premises
andconclusions
associate morphisms belonging to the corresponding categories with theFiniteSet
’s of their properties.The set of properties of a composite morphism is the intersection of the sets of properties of its components. The domain and codomain of a conclusion morphism should be among the domains and codomains of the morphisms listed as the premises of a diagram.
No checks are carried out of whether the supplied object and morphisms do belong to one and the same category.
Examples
>>> from sympy.categories import Object, NamedMorphism, Diagram >>> from sympy import pprint, default_sort_key >>> A = Object("A") >>> B = Object("B") >>> C = Object("C") >>> f = NamedMorphism(A, B, "f") >>> g = NamedMorphism(B, C, "g") >>> d = Diagram([f, g]) >>> premises_keys = sorted(d.premises.keys(), key=default_sort_key) >>> pprint(premises_keys, use_unicode=False) [g*f:A>C, id:A>A, id:B>B, id:C>C, f:A>B, g:B>C] >>> pprint(d.premises, use_unicode=False) {g*f:A>C: EmptySet, id:A>A: EmptySet, id:B>B: EmptySet, id:C>C: EmptyS et, f:A>B: EmptySet, g:B>C: EmptySet} >>> d = Diagram([f, g], {g * f: "unique"}) >>> pprint(d.conclusions) {g*f:A>C: {unique}}
References
[Pare1970] B. Pareigis: Categories and functors. Academic Press, 1970.

property
conclusions
¶ Returns the conclusions of this diagram.
Examples
>>> from sympy.categories import Object, NamedMorphism >>> from sympy.categories import IdentityMorphism, Diagram >>> from sympy import FiniteSet >>> A = Object("A") >>> B = Object("B") >>> C = Object("C") >>> f = NamedMorphism(A, B, "f") >>> g = NamedMorphism(B, C, "g") >>> d = Diagram([f, g]) >>> IdentityMorphism(A) in d.premises.keys() True >>> g * f in d.premises.keys() True >>> d = Diagram([f, g], {g * f: "unique"}) >>> d.conclusions[g * f] == FiniteSet("unique") True

hom
(A, B)[source]¶ Returns a 2tuple of sets of morphisms between objects A and B: one set of morphisms listed as premises, and the other set of morphisms listed as conclusions.
Examples
>>> from sympy.categories import Object, NamedMorphism, Diagram >>> from sympy import pretty >>> A = Object("A") >>> B = Object("B") >>> C = Object("C") >>> f = NamedMorphism(A, B, "f") >>> g = NamedMorphism(B, C, "g") >>> d = Diagram([f, g], {g * f: "unique"}) >>> print(pretty(d.hom(A, C), use_unicode=False)) ({g*f:A>C}, {g*f:A>C})

is_subdiagram
(diagram)[source]¶ Checks whether
diagram
is a subdiagram ofself
. Diagram \(D'\) is a subdiagram of \(D\) if all premises (conclusions) of \(D'\) are contained in the premises (conclusions) of \(D\). The morphisms contained both in \(D'\) and \(D\) should have the same properties for \(D'\) to be a subdiagram of \(D\).Examples
>>> from sympy.categories import Object, NamedMorphism, Diagram >>> A = Object("A") >>> B = Object("B") >>> C = Object("C") >>> f = NamedMorphism(A, B, "f") >>> g = NamedMorphism(B, C, "g") >>> d = Diagram([f, g], {g * f: "unique"}) >>> d1 = Diagram([f]) >>> d.is_subdiagram(d1) True >>> d1.is_subdiagram(d) False

property
objects
¶ Returns the
FiniteSet
of objects that appear in this diagram.Examples
>>> from sympy.categories import Object, NamedMorphism, Diagram >>> A = Object("A") >>> B = Object("B") >>> C = Object("C") >>> f = NamedMorphism(A, B, "f") >>> g = NamedMorphism(B, C, "g") >>> d = Diagram([f, g]) >>> d.objects FiniteSet(Object("A"), Object("B"), Object("C"))

property
premises
¶ Returns the premises of this diagram.
Examples
>>> from sympy.categories import Object, NamedMorphism >>> from sympy.categories import IdentityMorphism, Diagram >>> from sympy import pretty >>> A = Object("A") >>> B = Object("B") >>> f = NamedMorphism(A, B, "f") >>> id_A = IdentityMorphism(A) >>> id_B = IdentityMorphism(B) >>> d = Diagram([f]) >>> print(pretty(d.premises, use_unicode=False)) {id:A>A: EmptySet, id:B>B: EmptySet, f:A>B: EmptySet}

subdiagram_from_objects
(objects)[source]¶ If
objects
is a subset of the objects ofself
, returns a diagram which has as premises all those premises ofself
which have a domains and codomains inobjects
, likewise for conclusions. Properties are preserved.Examples
>>> from sympy.categories import Object, NamedMorphism, Diagram >>> from sympy import FiniteSet >>> A = Object("A") >>> B = Object("B") >>> C = Object("C") >>> f = NamedMorphism(A, B, "f") >>> g = NamedMorphism(B, C, "g") >>> d = Diagram([f, g], {f: "unique", g*f: "veryunique"}) >>> d1 = d.subdiagram_from_objects(FiniteSet(A, B)) >>> d1 == Diagram([f], {f: "unique"}) True

property
Diagram Drawing¶
This section lists the classes which allow automatic drawing of diagrams.

class
sympy.categories.diagram_drawing.
DiagramGrid
(diagram, groups=None, **hints)[source]¶ Constructs and holds the fitting of the diagram into a grid.
The mission of this class is to analyse the structure of the supplied diagram and to place its objects on a grid such that, when the objects and the morphisms are actually drawn, the diagram would be “readable”, in the sense that there will not be many intersections of moprhisms. This class does not perform any actual drawing. It does strive nevertheless to offer sufficient metadata to draw a diagram.
Consider the following simple diagram.
>>> from sympy.categories import Object, NamedMorphism >>> from sympy.categories import Diagram, DiagramGrid >>> from sympy import pprint >>> A = Object("A") >>> B = Object("B") >>> C = Object("C") >>> f = NamedMorphism(A, B, "f") >>> g = NamedMorphism(B, C, "g") >>> diagram = Diagram([f, g])
The simplest way to have a diagram laid out is the following:
>>> grid = DiagramGrid(diagram) >>> (grid.width, grid.height) (2, 2) >>> pprint(grid) A B C
Sometimes one sees the diagram as consisting of logical groups. One can advise
DiagramGrid
as to such groups by employing thegroups
keyword argument.Consider the following diagram:
>>> D = Object("D") >>> f = NamedMorphism(A, B, "f") >>> g = NamedMorphism(B, C, "g") >>> h = NamedMorphism(D, A, "h") >>> k = NamedMorphism(D, B, "k") >>> diagram = Diagram([f, g, h, k])
Lay it out with generic layout:
>>> grid = DiagramGrid(diagram) >>> pprint(grid) A B D C
Now, we can group the objects \(A\) and \(D\) to have them near one another:
>>> grid = DiagramGrid(diagram, groups=[[A, D], B, C]) >>> pprint(grid) B C A D
Note how the positioning of the other objects changes.
Further indications can be supplied to the constructor of
DiagramGrid
using keyword arguments. The currently supported hints are explained in the following paragraphs.DiagramGrid
does not automatically guess which layout would suit the supplied diagram better. Consider, for example, the following linear diagram:>>> E = Object("E") >>> f = NamedMorphism(A, B, "f") >>> g = NamedMorphism(B, C, "g") >>> h = NamedMorphism(C, D, "h") >>> i = NamedMorphism(D, E, "i") >>> diagram = Diagram([f, g, h, i])
When laid out with the generic layout, it does not get to look linear:
>>> grid = DiagramGrid(diagram) >>> pprint(grid) A B C D E
To get it laid out in a line, use
layout="sequential"
:>>> grid = DiagramGrid(diagram, layout="sequential") >>> pprint(grid) A B C D E
One may sometimes need to transpose the resulting layout. While this can always be done by hand,
DiagramGrid
provides a hint for that purpose:>>> grid = DiagramGrid(diagram, layout="sequential", transpose=True) >>> pprint(grid) A B C D E
Separate hints can also be provided for each group. For an example, refer to
tests/test_drawing.py
, and see the different ways in which the five lemma [FiveLemma] can be laid out.See also
References
[FiveLemma] https://en.wikipedia.org/wiki/Five_lemma

property
height
¶ Returns the number of rows in this diagram layout.
Examples
>>> from sympy.categories import Object, NamedMorphism >>> from sympy.categories import Diagram, DiagramGrid >>> A = Object("A") >>> B = Object("B") >>> C = Object("C") >>> f = NamedMorphism(A, B, "f") >>> g = NamedMorphism(B, C, "g") >>> diagram = Diagram([f, g]) >>> grid = DiagramGrid(diagram) >>> grid.height 2

property
morphisms
¶ Returns those morphisms (and their properties) which are sufficiently meaningful to be drawn.
Examples
>>> from sympy.categories import Object, NamedMorphism >>> from sympy.categories import Diagram, DiagramGrid >>> A = Object("A") >>> B = Object("B") >>> C = Object("C") >>> f = NamedMorphism(A, B, "f") >>> g = NamedMorphism(B, C, "g") >>> diagram = Diagram([f, g]) >>> grid = DiagramGrid(diagram) >>> grid.morphisms {NamedMorphism(Object("A"), Object("B"), "f"): EmptySet, NamedMorphism(Object("B"), Object("C"), "g"): EmptySet}

property
width
¶ Returns the number of columns in this diagram layout.
Examples
>>> from sympy.categories import Object, NamedMorphism >>> from sympy.categories import Diagram, DiagramGrid >>> A = Object("A") >>> B = Object("B") >>> C = Object("C") >>> f = NamedMorphism(A, B, "f") >>> g = NamedMorphism(B, C, "g") >>> diagram = Diagram([f, g]) >>> grid = DiagramGrid(diagram) >>> grid.width 2

property

class
sympy.categories.diagram_drawing.
ArrowStringDescription
(unit, curving, curving_amount, looping_start, looping_end, horizontal_direction, vertical_direction, label_position, label)[source]¶ Stores the information necessary for producing an Xypic description of an arrow.
The principal goal of this class is to abstract away the string representation of an arrow and to also provide the functionality to produce the actual Xypic string.
unit
sets the unit which will be used to specify the amount of curving and other distances.horizontal_direction
should be a string of"r"
or"l"
specifying the horizontal offset of the target cell of the arrow relatively to the current one.vertical_direction
should specify the vertical offset using a series of either"d"
or"u"
.label_position
should be either"^"
,"_"
, or""
to specify that the label should be positioned above the arrow, below the arrow or just over the arrow, in a break. Note that the notions “above” and “below” are relative to arrow direction.label
stores the morphism label.This works as follows (disregard the yet unexplained arguments):
>>> from sympy.categories.diagram_drawing import ArrowStringDescription >>> astr = ArrowStringDescription( ... unit="mm", curving=None, curving_amount=None, ... looping_start=None, looping_end=None, horizontal_direction="d", ... vertical_direction="r", label_position="_", label="f") >>> print(str(astr)) \ar[dr]_{f}
curving
should be one of"^"
,"_"
to specify in which direction the arrow is going to curve.curving_amount
is a number describing how manyunit
’s the morphism is going to curve:>>> astr = ArrowStringDescription( ... unit="mm", curving="^", curving_amount=12, ... looping_start=None, looping_end=None, horizontal_direction="d", ... vertical_direction="r", label_position="_", label="f") >>> print(str(astr)) \ar@/^12mm/[dr]_{f}
looping_start
andlooping_end
are currently only used for loop morphisms, those which have the same domain and codomain. These two attributes should store a valid Xypic direction and specify, correspondingly, the direction the arrow gets out into and the direction the arrow gets back from:>>> astr = ArrowStringDescription( ... unit="mm", curving=None, curving_amount=None, ... looping_start="u", looping_end="l", horizontal_direction="", ... vertical_direction="", label_position="_", label="f") >>> print(str(astr)) \ar@(u,l)[]_{f}
label_displacement
controls how far the arrow label is from the ends of the arrow. For example, to position the arrow label near the arrow head, use “>”:>>> astr = ArrowStringDescription( ... unit="mm", curving="^", curving_amount=12, ... looping_start=None, looping_end=None, horizontal_direction="d", ... vertical_direction="r", label_position="_", label="f") >>> astr.label_displacement = ">" >>> print(str(astr)) \ar@/^12mm/[dr]_>{f}
Finally,
arrow_style
is used to specify the arrow style. To get a dashed arrow, for example, use “{–>}” as arrow style:>>> astr = ArrowStringDescription( ... unit="mm", curving="^", curving_amount=12, ... looping_start=None, looping_end=None, horizontal_direction="d", ... vertical_direction="r", label_position="_", label="f") >>> astr.arrow_style = "{>}" >>> print(str(astr)) \ar@/^12mm/@{>}[dr]_{f}
Notes
Instances of
ArrowStringDescription
will be constructed byXypicDiagramDrawer
and provided for further use in formatters. The user is not expected to construct instances ofArrowStringDescription
themselves.To be able to properly utilise this class, the reader is encouraged to checkout the Xypic user guide, available at [Xypic].
See also
References
[Xypic] http://xypic.sourceforge.net/

class
sympy.categories.diagram_drawing.
XypicDiagramDrawer
[source]¶ Given a
Diagram
and the correspondingDiagramGrid
, produces the Xypic representation of the diagram.The most important method in this class is
draw
. Consider the following triangle diagram:>>> from sympy.categories import Object, NamedMorphism, Diagram >>> from sympy.categories import DiagramGrid, XypicDiagramDrawer >>> A = Object("A") >>> B = Object("B") >>> C = Object("C") >>> f = NamedMorphism(A, B, "f") >>> g = NamedMorphism(B, C, "g") >>> diagram = Diagram([f, g], {g * f: "unique"})
To draw this diagram, its objects need to be laid out with a
DiagramGrid
:>>> grid = DiagramGrid(diagram)
Finally, the drawing:
>>> drawer = XypicDiagramDrawer() >>> print(drawer.draw(diagram, grid)) \xymatrix{ A \ar[d]_{g\circ f} \ar[r]^{f} & B \ar[ld]^{g} \\ C & }
For further details see the docstring of this method.
To control the appearance of the arrows, formatters are used. The dictionary
arrow_formatters
maps morphisms to formatter functions. A formatter is accepts anArrowStringDescription
and is allowed to modify any of the arrow properties exposed thereby. For example, to have all morphisms with the propertyunique
appear as dashed arrows, and to have their names prepended with \(\exists !\), the following should be done:>>> def formatter(astr): ... astr.label = r"\exists !" + astr.label ... astr.arrow_style = "{>}" >>> drawer.arrow_formatters["unique"] = formatter >>> print(drawer.draw(diagram, grid)) \xymatrix{ A \ar@{>}[d]_{\exists !g\circ f} \ar[r]^{f} & B \ar[ld]^{g} \\ C & }
To modify the appearance of all arrows in the diagram, set
default_arrow_formatter
. For example, to place all morphism labels a little bit farther from the arrow head so that they look more centred, do as follows:>>> def default_formatter(astr): ... astr.label_displacement = "(0.45)" >>> drawer.default_arrow_formatter = default_formatter >>> print(drawer.draw(diagram, grid)) \xymatrix{ A \ar@{>}[d]_(0.45){\exists !g\circ f} \ar[r]^(0.45){f} & B \ar[ld]^(0.45){g} \\ C & }
In some diagrams some morphisms are drawn as curved arrows. Consider the following diagram:
>>> D = Object("D") >>> E = Object("E") >>> h = NamedMorphism(D, A, "h") >>> k = NamedMorphism(D, B, "k") >>> diagram = Diagram([f, g, h, k]) >>> grid = DiagramGrid(diagram) >>> drawer = XypicDiagramDrawer() >>> print(drawer.draw(diagram, grid)) \xymatrix{ A \ar[r]_{f} & B \ar[d]^{g} & D \ar[l]^{k} \ar@/_3mm/[ll]_{h} \\ & C & }
To control how far the morphisms are curved by default, one can use the
unit
anddefault_curving_amount
attributes:>>> drawer.unit = "cm" >>> drawer.default_curving_amount = 1 >>> print(drawer.draw(diagram, grid)) \xymatrix{ A \ar[r]_{f} & B \ar[d]^{g} & D \ar[l]^{k} \ar@/_1cm/[ll]_{h} \\ & C & }
In some diagrams, there are multiple curved morphisms between the same two objects. To control by how much the curving changes between two such successive morphisms, use
default_curving_step
:>>> drawer.default_curving_step = 1 >>> h1 = NamedMorphism(A, D, "h1") >>> diagram = Diagram([f, g, h, k, h1]) >>> grid = DiagramGrid(diagram) >>> print(drawer.draw(diagram, grid)) \xymatrix{ A \ar[r]_{f} \ar@/^1cm/[rr]^{h_{1}} & B \ar[d]^{g} & D \ar[l]^{k} \ar@/_2cm/[ll]_{h} \\ & C & }
The default value of
default_curving_step
is 4 units.See also

draw
(diagram, grid, masked=None, diagram_format='')[source]¶ Returns the Xypic representation of
diagram
laid out ingrid
.Consider the following simple triangle diagram.
>>> from sympy.categories import Object, NamedMorphism, Diagram >>> from sympy.categories import DiagramGrid, XypicDiagramDrawer >>> A = Object("A") >>> B = Object("B") >>> C = Object("C") >>> f = NamedMorphism(A, B, "f") >>> g = NamedMorphism(B, C, "g") >>> diagram = Diagram([f, g], {g * f: "unique"})
To draw this diagram, its objects need to be laid out with a
DiagramGrid
:>>> grid = DiagramGrid(diagram)
Finally, the drawing:
>>> drawer = XypicDiagramDrawer() >>> print(drawer.draw(diagram, grid)) \xymatrix{ A \ar[d]_{g\circ f} \ar[r]^{f} & B \ar[ld]^{g} \\ C & }
The argument
masked
can be used to skip morphisms in the presentation of the diagram:>>> print(drawer.draw(diagram, grid, masked=[g * f])) \xymatrix{ A \ar[r]^{f} & B \ar[ld]^{g} \\ C & }
Finally, the
diagram_format
argument can be used to specify the format string of the diagram. For example, to increase the spacing by 1 cm, proceeding as follows:>>> print(drawer.draw(diagram, grid, diagram_format="@+1cm")) \xymatrix@+1cm{ A \ar[d]_{g\circ f} \ar[r]^{f} & B \ar[ld]^{g} \\ C & }


sympy.categories.diagram_drawing.
xypic_draw_diagram
(diagram, masked=None, diagram_format='', groups=None, **hints)[source]¶ Provides a shortcut combining
DiagramGrid
andXypicDiagramDrawer
. Returns an Xypic presentation ofdiagram
. The argumentmasked
is a list of morphisms which will be not be drawn. The argumentdiagram_format
is the format string inserted after “xymatrix”.groups
should be a set of logical groups. Thehints
will be passed directly to the constructor ofDiagramGrid
.For more information about the arguments, see the docstrings of
DiagramGrid
andXypicDiagramDrawer.draw
.Examples
>>> from sympy.categories import Object, NamedMorphism, Diagram >>> from sympy.categories import xypic_draw_diagram >>> A = Object("A") >>> B = Object("B") >>> C = Object("C") >>> f = NamedMorphism(A, B, "f") >>> g = NamedMorphism(B, C, "g") >>> diagram = Diagram([f, g], {g * f: "unique"}) >>> print(xypic_draw_diagram(diagram)) \xymatrix{ A \ar[d]_{g\circ f} \ar[r]^{f} & B \ar[ld]^{g} \\ C & }
See also

sympy.categories.diagram_drawing.
preview_diagram
(diagram, masked=None, diagram_format='', groups=None, output='png', viewer=None, euler=True, **hints)[source]¶ Combines the functionality of
xypic_draw_diagram
andsympy.printing.preview
. The argumentsmasked
,diagram_format
,groups
, andhints
are passed toxypic_draw_diagram
, whileoutput
,viewer, and ``euler
are passed topreview
.Examples
>>> from sympy.categories import Object, NamedMorphism, Diagram >>> from sympy.categories import preview_diagram >>> A = Object("A") >>> B = Object("B") >>> C = Object("C") >>> f = NamedMorphism(A, B, "f") >>> g = NamedMorphism(B, C, "g") >>> d = Diagram([f, g], {g * f: "unique"}) >>> preview_diagram(d)
See also