Kris Brown

(press `s`

for speaker notes, available online here)

11/1/23

$$

$$

**Natural science**

- 2015, Bachelors at Dartmouth:
- Chemical engineering

- 2016, Visiting scholar at EPFL, Switzerland:
- synthesis of catalysts to extract fuel from waste biomass

- 2016-2021, PhD at Stanford in Chemical Engineering
- Computational chemistry renewable energy catalysts

**Computer science**

- 2019-2020, Google: Software engineering intern
- 2020: Research in proving programs are correct

**Math + CS + Natural science**

- 2022, UF: Postdoc in computational mathematics of scientific modeling
- 2023, Topos Institute: applied category theory and scientific modeling research

In the course so far, you’ve learned to:

- mathematically model scientific problems
- solve them by hand
- programming analytic and numerical soln’s

Physical system: chemical reaction network

\[ 2 H_2O \rightarrow 2 H_2 + O_2 \] \[ H_2 \leftrightarrow 2 H \]

Mathematical model: ODEs

\[\frac{d[H_2O]}{dt} = -k_1 \cdot [H_2O]^2 + ...\]

\[\frac{d[H_2]}{dt} = 2 k_1 \cdot [H_2O]^2 - k_{2f} [H_2] + ...\]

Computational implementation: Python

```
def four_reactor(time):
V1, V2, V3, V4 = 20.0, 30.0, 20.0, 30.0 # L
in_concs = [996, 0.4, 0.05, 0.05] # g/L
def yprime(t, concs):
# 4 species in four tanks: 1_1, 1_2, 2_1, 2_2
H2O2_1_1, H2O_1_1, H_1_1, H2O2_1_1, ...
H2O2_2_2, H2O_2_2, H_2_2, H2O2_2_2 = concs
# NEW MATH TO BE WORKED OUT
# FROM SCRATCH
solve_ivp(yprime, [0, time], [.1,.4,.3,.5])
```

\[ 2 A + B \rightarrow B + C \] \[ A + D \rightarrow B \]

\[ B \rightarrow C \] \[ 2 C \rightarrow A \]

Two students complete the same lab assignment but get different results.

```
from scipy.integrate import solve_ivp
def my_python_code_to_solve_ode(t):
def y_prime(_, specs):
c1, c2, c3, c4 = specs # corresponds to D, C, B, A
f1 = c3 # corresponds to bc
f3 = c1*c4 # corresponds to ac
f2 = c2*c2 # corresponds to cd
f4 = c3+c4**2 # corresponds to ab
return [-f3, ...]
solve_ivp(y_prime, [0, t], [.1, .1, .1, .1])
```

This is hard to verify, especially if the code is big.

- variable naming, declaration ordering, multiple valid ways of structuring programs

**Goal**: define a mathematical object which can represent a chemical reaction network.

- Sets and functions between them
- Graphs and maps between them
- Petri Nets and maps between them

A **set** is a collection of things.

**Membership notation**

\(red \in \{red,blue,green\}\)

\(pink \notin \{red,blue,green\}\)

\(false \in \mathbb{B}\)

\(35 \in \mathbb{N}\)

\(6.02 \times 10^{23} \in \mathbb{R}\)

\(2+3i \notin \mathbb{R}\)

A **function** is a *map* between sets.

\(\{red \mapsto \top, green \mapsto \top, blue \mapsto \bot\}\)

\(\{\bullet \mapsto \pi\}\)

A **function** (or a *map* between sets, written \(f: A \rightarrow B\)) assigns to each element of set \(A\) an element of set \(B\).

We denote the element that \(f\) sends \(a\) to as \(f(a)\). \(A\) the **domain** and \(B\) the **codomain** of \(f\).

We can explicitly write a function as a set of pairs \(\{a_1 \mapsto b_n, a_2 \mapsto b_m, ...\}\).

Some examples of functions:

\(\{red \mapsto \top, green \mapsto \top, blue \mapsto \bot\}\)

\(\{\bullet \mapsto \pi\}\)

Other functions:

**capital of**: \(U.S.\ States \rightarrow U.S.\ Cities\)**+1**: \(\mathbb{N}\rightarrow \mathbb{N}\)**Determinant**: \(Matrix^\mathbb{R}\rightarrow \mathbb{R}\)**Derivative**: \(Polynomial \rightarrow Polynomial\) \(\{x^2+1\mapsto 2x, ...\}\)

Which of the following is a function?

A **function** (\(f: A \rightarrow B\)) assigns to each element of set \(A\) an element of set \(B\).

The **identity function** for a set \(X\) sends each \(x \in X\) to itself.

The **composition** of functions \(f: A\rightarrow B\) and \(g: B \rightarrow C\) is a function \(f ; g: A \rightarrow C\) which sends each \(a \in A\) to \(g(f(a))\).

- Given \((+1): \mathbb{N}\rightarrow \mathbb{N}\) and \((+2): \mathbb{N}\rightarrow \mathbb{N}\)
- \((+1);(+1) = (+2)\) and \((+1);id_\mathbb{N}= (+1)\)

- Given \(not: \mathbb{B}\rightarrow \mathbb{B}\)
- \(not; not = id_\mathbb{B}\)

- Given \(stateOf: Cities \rightarrow States\) and \(capitolOf: States \rightarrow Cities\)
- \(capitalOf; stateOf = id_{States}\)
- \(stateOf; capitalOf \ne id_{Cities}\)

An **isomorphism** between sets \(A\) and \(B\) is a function \(f: A \rightarrow B\) such that there exists an *inverse function* \(f^{-1}: B \rightarrow A\) such that \(f;f^{-1}\) and \(f^{-1};f\) are identity functions.

**Quick way to check if function is an iso**

- Do two elements of the function domain ever get mapped to the same element of the codomain?
- Is there anything in the codomain that was not mapped to?

If both answers are “no”, then it’s an isomorphism!

[see whiteboard]

A **graph** is something which has the following components:

- a set of edges, \(E\)
- a set of vertices \(V\)
- two functions \(E\rightarrow V\) called
*src*and*tgt*.

To connect to earlier drawings of sets, we could draw a graph like this:

The graph itself looks like this:

A **graph** is something which has the following components:

- a set of edges, \(E\)
- a set of vertices \(V\)
- two functions \(E\rightarrow V\) called
*src*and*tgt*.

To connect to earlier drawings of sets, we could draw a graph like this:

The graph itself looks like this:

[see whiteboard]

Suppose we have the chemical reaction network:

\[ ab: A \rightarrow B \] \[ ac: A \rightarrow C \] \[ bc: B \rightarrow C \]

How might we represent this with a graph?

What would you have to do in order to solve that problem?

A **graph mapping** \(f: G_1 \rightarrow G_2\) is a pair of *compatible* functions:

- \(f_V: V_1 \rightarrow V_2\)
- \(f_E: E_1 \rightarrow E_2\)

*Compatible* means \(f_E;src_2 = src_1;f_V\) and \(f_E;tgt_2 = tgt_1;f_V\).

[see whiteboard]

A **graph isomorphism** is a graph mapping where \(f_V\) and \(f_E\) are isomorphisms.

Two students complete the same lab assignment but get different results.

```
from scipy.integrate import solve_ivp
def my_python_code_to_solve_ode(t):
def y_prime(tim, specs):
c1, c2, c3, c4 = specs # corresponds to D, C, B, A
f1 = c3 # corresponds to bc
f3 = c4 # corresponds to ac
f2 = c2 # corresponds to cd
f4 = c4 # corresponds to ab
return [f2, f3 + f1 - f2, f4 - f1, -f4 - f2]
solve_ivp(y_prime, [0, t], [.1, .1, .1, .1])
```

This is hard to verify, especially if the code is big.

- variable naming, declaration ordering, multiple valid ways of structuring programs

**Solution**

If the two students had first defined their models *as graphs*, then they could answer their question by computing whether or not their graphs are isomorphic.

**Problem with graphs**

Chemical reactions contain **multiple** inputs and outputs.

\[\square: A\rightarrow 2B\]

\[\blacksquare: C+D\rightarrow A\]

**Problem with graphs**

Chemical reactions contain **multiple** inputs and outputs.

A **Petri net** consists of:

- Four sets:
**S**pecies,**T**ransitions**I**nputs, and**O**utputs - Four functions: \(is: I\rightarrow S\), \(it: I \rightarrow T\), \(os: O\rightarrow S\), \(ot: O \rightarrow T\).

A **Petri net** consists of:

- Four sets:
**S**pecies,**T**ransitions**I**nputs, and**O**utputs - Four functions: \(is: I\rightarrow S\), \(it: I \rightarrow T\), \(os: O\rightarrow S\), \(ot: O \rightarrow T\).

E.g. the reaction network: \(\square: A\rightarrow 2B\) and \(\blacksquare: C+D\rightarrow A\).

The Petri net is drawn like this:

[see whiteboard]

Suppose we have the chemical reaction network:

\[ A + B \rightarrow B + C\] \[ A \rightarrow C \]

How might we represent this with a Petri net?

Reminder from last slide:

\(\square: A\rightarrow 2B\)

\(\blacksquare: C+D\rightarrow A\).

A map of Petri nets \(f: P_1 \rightarrow P_2\) consists of four compatible functions, \(f_S: S_1 \rightarrow S_2,\ f_T: T_1 \rightarrow T_2,\ f_I: I_1 \rightarrow I_2,\ f_O: O_1 \rightarrow O_2\).

- Compatibility:
**1.)**\(f_I;it_2= it_1;f_T\),**2.)**\(f_O;ot_2= ot_1;f_T\),**3.)**\(f_I;is_2= is_1;f_S\),**4.)**\(f_O;os_2= os_1;f_S\)

A **Petri net isomorphism** is a Petri net map where all four functions are iso.

\[ 2 A + B \rightarrow B + C \] \[ A + D \rightarrow B \]

\[ B \rightarrow C \] \[ 2 C \rightarrow A \]

Two students complete the same lab assignment but get different results.

```
from scipy.integrate import solve_ivp
def my_python_code_to_solve_ode(t):
def y_prime(_, specs):
c1, c2, c3, c4 = specs # corresponds to D, C, B, A
f1 = c3 # corresponds to bc
f3 = c1*c4 # corresponds to ac
f2 = c2*c2 # corresponds to cd
f4 = c3+c4**2 # corresponds to ab
return [-f3, ...]
solve_ivp(y_prime, [0, t], [.1, .1, .1, .1])
```

This is hard to verify, especially if the code is big.

- variable naming, declaration ordering, multiple valid ways of structuring programs

- For each of these data structures:
- maps between them
- isomorphisms between them

Rxn Net Representation | Advantage |
---|---|

Traditional text-based format | Communication |

Code representation | Computation of concentrations over time |

Petri nets (drawing) | Visualization |

Petri nets (four sets, four functions) | Computation on the network itself |

*Next lecture*:

**Applied category theory textbooks**

**Category theory textbooks**

- Emily Rhiel’s Category Theory in Context
- Bill Lawvere’s Conceptual Mathematics

**Blogs**

A **function** \(f: A \rightarrow B\) assigns to each element of set \(A\) an element of set \(B\).

**isomorphism** between sets \(A\) and \(B\) is a function \(f: A \rightarrow B\) such that there exists an *inverse function* \(f^{-1}: B \rightarrow A\) such that \(f;f^{-1}\) and \(f^{-1};f\) are identity functions.

A **graph** has a set of edges, \(E\), a set of vertices \(V\), and two functions \(E\rightarrow V\) called *src* and *tgt*.

The graph on the left is visualized like this:

A **graph mapping** \(f: G_1 \rightarrow G_2\) is a pair of *compatible* functions \(f_V: V_1 \rightarrow V_2\) and \(f_E: E_1 \rightarrow E_2\). (*Compatible* means \(f_E;src_2 = src_1;f_V\) and \(f_E;tgt_2 = tgt_1;f_V\))

A **Petri net** consists of:

- Four sets:
**S**pecies,**T**ransitions**I**nputs, and**O**utputs - Four functions: \(is: I\rightarrow S\), \(it: I \rightarrow T\), \(os: O\rightarrow S\), \(ot: O \rightarrow T\).

A **map** of Petri nets \(f: P_1 \rightarrow P_2\) consists of four *compatible* functions for \(S\), \(T\), \(I\), and \(O\).

```
def combined_rxn_network(t): # tedious to construct
def yprime(t, species):
cH2O, cOH, cH, cH2O2, cH2 = species
k1, k2, k3 = [0.02, 0.01, 0.3]
f1 = k1 * cH20
f2 = k2 * cH * cOH
f3 = k3 * cH2O2 * cH2
return [f3-f1, f1-f2, f1-f2, -f3, -f3]
solve_ivp(yprime, [0, t], [.1,.4,.3])
# Preferred alternative
overlap = Overlap(rxn_network1, rxn_network2, ...)
combined_rxn_network = glue(overlap)
```

Gluing together graphs

Gluing together Petri nets

Gluing together dynamical systems

**Reinvent the wheel each time? Or general solution possible?**

Beyond being precise, we want to be **generalizable**.

Ideally there is just *one* definition that works for:

- sets
- graphs
- Petri nets
- matrices
- electrical circuits
- heat flow problems
- etc.

We will do this with the technique of **universal properties**.

Part 1. The notion of **being empty**

Part 2. **Placing things side-by-side**

For any set \(X\), there exists *exactly one* function \(\{\} \rightarrow X\).

This is a function because it assigns a value of \(X\) for every element of \(\{\}\).

This function is unique for each \(X\) because there are no other ways to make this function.

\(\{\}\) is an

**initial object**because this is true for*any*set \(X\).

A set with even a single element doesn’t have this property:

What is special about defining ‘emptiness’ via the diagram \(0 \rightarrow X\) is that it can be interpreted in many contexts, not just sets and functions.

Is there a graph which plays the role of “initial object” in the context of graphs and graph mappings?

When \(E\) and \(V\) are empty, the resulting graph is an initial object.

When \(S\), \(T\), \(I\), and \(O\) are empty, the resulting Petri net is an initial object.

Universal property of being an initial object:

We can think of other contexts than the ones discussed so far:

- Consider natural numbers, \(\mathbb{N}= \{0, 1, 2, ...\}\), and an arrow \(n \rightarrow m\) whenever \(n \leq m\).
- Consider natural numbers, \(\mathbb{N}\), and an arrow \(n \rightarrow m\) whenever \(m\) is divisible by \(n\).

**Not every context has an initial object**

Consider the integers, \(\mathbb{Z}\), (…-2, -1, 0, 1, 2, …) and an arrow \(n \rightarrow m\) whenever \(n \leq m\).

[See whiteboard]

What does it mean to place two sets side-by-side?

The **disjoint union** of two sets \(A\) and \(B\), written \(A + B\) is a set which contains the elements of both \(A\) and \(B\) along with a label to distinguish whether the element came from \(A\) or from \(B\).

Sets are not allowed to have duplicates, so the label prevents collisions between the overlapping items of \(A\) and \(B\).

**Fun fact**

We can encode any pair of functions \(A \rightarrow X\) and \(B \rightarrow X\), as a function \(A+B\rightarrow X\).

[See whiteboard]

Disjoint union only makes sense for sets. We want to describe it purely in terms of abstract points and arrows in order to generalize to other contexts.

This is sufficient to define putting graphs side-by-side.

[See whiteboard]

Universal property of a pushout:

Two simple definitions yield all of the specifics of these lectures as special cases:

- For each of these data structures:
- maps between them
- isomorphisms between them

It’s *easy* to write code which manipulates sets/graphs/Petri nets.

It’s **hard** to write code which manipulates other pieces of code.

**Universal properties** allow us to make very **general** definitions (and write flexible code) which work in many contexts.

*sameness*(generalizing the notion of isomorphism)*emptiness*(generalizing the empty set)*side-by-side placement*(generalizing the disjoint union)

The field of **category theory** strives to do this with all concepts we care about, including *gluing*, *multiplication*, *optimization*, *substitution*, and *recursion*.

**Applied category theory textbooks**

**Category theory textbooks**

- Emily Rhiel’s Category Theory in Context
- Bill Lawvere’s Conceptual Mathematics

**Blogs**

The coproduct of graphs \(G_1 + G_2\) can also be described as the following graph:

- \(E = E_1 + E_2\)
- \(V = V_1 + V_2\)
- \(src\) and \(tgt\) functions \(E_1+E_2 \rightarrow V_1+V_2\) (
*derived from the original graphs*)

How to say more precisely what functions \(src\) and \(tgt\) are?

The coproduct of graphs \(G_1 + G_2\) can also be described as the following graph:

- \(E = E_1 + E_2\)
- \(V = V_1 + V_2\)
- \(src\) and \(tgt\) functions \(E_1+E_2 \rightarrow V_1+V_2\) (
*derived from the original graphs*)

How to say more precisely what functions \(src\) and \(tgt\) are?