# Torus as a two-comples¶

This is an introduction to the implementation of two-complexes, including issues with scala, ending with constructing a torus. To start with we load the compiled code.

In :
import $cp.bin.superficial-c3516ca6f4.jar import superficial._  Out: import$cp.$import superficial._ ## A vertex¶ The code for vertices is rather simple. class Vertex  Let us first create a vertex. As it has no abstract methods, we can just ask for a new one. In : val v0 = new Vertex  Out: v0: Vertex = superficial.Vertex@75085bd8 Alternatively, we can declare a new object. In : object v1 extends Vertex  Out: defined object v1 In : 3: Int  Out: res4: Int = 3 In : v1 : v1.type  Out: res3: v1.type = ammonite.$sess.cmd2$Helper$v1$@5328d611 ## Edges¶ Edges come in pairs, an edge and the same with the opposite orientation (i.e., flipped). Further, each edge has an initial and terminal vertex. We can also describe the boundary as a formal sum. This is trait, i.e., an abstract class. This means some of its fields are undefined, and have to be defined in a subclass/object with this trait. trait Edge { /** * the same edge with the opposite orientation. */ def flip: Edge def terminal: Vertex def initial: Vertex def del : FormalSum[Vertex] = FormalSum.reduced(Vector(terminal -> 1, initial -> -1)) }  ### A loop¶ We construct a loop at v0, i.e., an edge beginning and ending at v0. But note that we cannot construct just one, as we need to flip it. Instead we will construct a pair of edges, each a flip of the other. In : object E1 extends Edge{ lazy val flip = E2 val terminal = v0 val initial = v0 } object E2 extends Edge{ lazy val flip = E1 val terminal = v0 val initial = v0 }  Out: defined object E1 defined object E2 Note a key feature above - we have defined the flip of each of the edges as lazy val. This means this is not computed until it is needed. Otherwise defining each of E1 and E2 leads to an infinite loop. We can see some computations on demand. In : E1.flip  Out: res6: E2.type = ammonite.$sess.cmd5$Helper$E2$@444af6b3 In : E1.flip == E2  Out: res7: Boolean = true In : E1.flip.flip == E1  Out: res8: Boolean = true In : E1 == E2  Out: res9: Boolean = false ## Torus time¶ We now turn to constructing a torus. Recall that this has a single vertex, two edges that become loops and a single face. We will follow the same pattern as above but give things nicer names. We will also make objects case object definitions. This means the names are cleaner (as is equality). In : case object V extends Vertex  Out: defined object V In : case object VV extends Vertex  Out: defined object VV In : V == VV  Out: res12: Boolean = false In : case class VC() extends Vertex case class VVC() extends Vertex  Out: defined class VC defined class VVC In : VC() == VVC()  Out: res14: Boolean = false In : VC() == VC()  Out: res15: Boolean = true In : case object A extends Edge{ lazy val flip = Abar val terminal = V val initial = V } case object Abar extends Edge{ lazy val flip = A val terminal = V val initial = V }  Out: defined object A defined object Abar In : case object B extends Edge{ lazy val flip = Bbar val terminal = V val initial = V } case object Bbar extends Edge{ lazy val flip = B val terminal = V val initial = V }  Out: defined object B defined object Bbar In : A.flip  Out: res18: Abar.type = Abar In : Abar.flip  Out: res19: A.type = A Now we construct the face of the torus. Here is an extract from the code for defining a polygon. We have omitted concrete methods, i.e., methods in the trait that are defined, so need not be implemented in subclasses/objects. trait Polygon extends TwoComplex { val sides: Int val boundary: Vector[Edge] val vertices: Set[Vertex] }  Note that this is a two-complex, so we are constructing a torus. In : case object Torus extends Polygon{ val sides = 4 val boundary = Vector(A, B, Abar, Bbar) val vertices = Set(V) }  Out: defined object Torus In : Torus.faces  Out: res21: Set[Polygon] = Set(Torus) In : Torus.edges  Out: res22: Set[Edge] = Set(A, Abar, B, Bbar) In : Torus.boundary  Out: res24: Vector[Product with Edge with Serializable{def initial: ammonite.$sess.cmd10.instance.V.type;def terminal: ammonite.\$sess.cmd10.instance.V.type;def flip: Product with superficial.Edge with java.io.Serializable}] = Vector(
A,
B,
Abar,
Bbar
)

Note that there are subclasses that let us construct more directly, but for now (i.e. as of Jan 13, 2020) they are not sufficient. We took a more direct approach to illustrate the structures.