Practical abstract algebra for engineers

Kris Brown


(press s for speaker notes, available online here)

11/1/23

About Me

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

Two kinds of scientific modeling problems


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

from scipy.integrate import solve_ivp 

def python_code_to_solve_ODE(time):
  def yprime(t, species):
    cH2O, cH2, cO2, cH = species
    k1, k2f, k2r = [3e-7, 4e-9, 2e-5]
    f1 = k1 * cH20**2
    f2 = k2f * cH2
    r2 = k2r * cH**2
    return [-2*f1, 2*f1-f2, f1, 2*f2-2*r2]

  solve_ivp(yprime, [0, time], [.1,.4,.3,.5])

Motivating example: a complex system

def two_reactor(time):
  V1, V2 = 20.0, 30.0 # L
  in_concs = [996, 0.4, 0.05, 0.05] # g/L
  def yprime(t, concs):
    H2O2_1, H2O_1, H_1, ... = concs
    ... # math based on reaction network
  
  solve_ivp(yprime, [0, time], [.1,.4,.3,.5])

Lecture 2 motivating example: system composition

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])


 
apply_twice = . . . # the wiring diagram

four_reactor = compose(apply_twice, 
                       [two_reactor, 
                        two_reactor])

Lecture 1 motivating problem: computing “sameness”

\[ 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 python_code_to_solve_ODE(time):
  def yprime(t, species):
    cA, cB, cC, cD = species
    fab = cA**2+cB
    fac = cA*cD
    fbc = cB
    fcd = cC**2  
    return [-2*fab - fac + fcd, ...]

  solve_ivp(yprime, [0, time], [.1,.1,.1,.1])
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

Lecture Outline

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

  1. Sets and functions between them
  2. Graphs and maps between them
  3. Petri Nets and maps between them

What is a set?

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}\)

What is a function?

A function is a map between sets.

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


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

What is a function?

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, ...\}\)

Sets and functions: comprehension check

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\).

Identity functions, composition of functions


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}\)

Isomorphisms between sets

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

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

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

Worked examples

[see whiteboard]

What is a graph?

What is a graph?

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:

What is a graph?

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:

Worked examples

[see whiteboard]

Graph example: representing chemical reaction networks

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?

Graph mappings

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\).

Worked examples

[see whiteboard]

Graph isomorphisms

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

Why does graph isomorphism matter?

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

from scipy.integrate import solve_ivp 

def python_code_to_solve_ODE(time):
  def yprime(t, species):
    cA, cB, cC, cD = species
    fab = cA
    fac = cA
    fbc = cB
    fcd = cC  
    return [-fab - fbc, fab - fbc, fac + fbc - fcd, fcd]

  solve_ivp(yprime, [0, time], [.1,.1,.1,.1])
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.

What is a Petri net?

Problem with graphs

Chemical reactions contain multiple inputs and outputs.

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

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

What is a Petri net?

Problem with graphs

Chemical reactions contain multiple inputs and outputs.

A Petri net consists of:

  • Four sets: Species, Transitions Inputs, and Outputs
  • Four functions: \(is: I\rightarrow S\), \(it: I \rightarrow T\), \(os: O\rightarrow S\), \(ot: O \rightarrow T\).

What is a Petri net?

A Petri net consists of:

  • Four sets: Species, Transitions Inputs, and Outputs
  • 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:

Worked examples

[see whiteboard]

Petri nets: Example

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\).

Petri nets: maps and isomorphisms

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.

Petri nets: maps and isomorphisms

Lecture 1 motivating problem: computing “sameness”

\[ 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 python_code_to_solve_ODE(time):
  def yprime(t, species):
    cA, cB, cC, cD = species
    fab = cA**2+cB
    fac = cA*cD
    fbc = cB
    fcd = cC**2  
    return [-2*fab - fac + fcd, ...]

  solve_ivp(yprime, [0, time], [.1,.1,.1,.1])
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

Lecture 1 Conclusions

  • 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:

Resources

Applied category theory textbooks

  1. Category Theory for the Sciences
  2. Seven Sketches in Compositionality

Category theory textbooks

Blogs

  1. AlgebraicJulia
  2. Math3ma
  3. Graphical Linear Algebra
  4. Kittenlab

Practical abstract algebra for engineers: Lecture 2

Lecture 1 Review: sets



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

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.

Lecture 1 Review: graphs

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\))

Lecture 1 Review: Petri nets

A Petri net consists of:

  • Four sets: Species, Transitions Inputs, and Outputs
  • 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\).

Motivating problem


def rxn_network_1(t):
  def yprime(t, species):
    cH2O, cH, cOH = species
    k1, k2 = [0.02, 0.01]
    f1 = k1 * cH20
    f2 = k2 * cH * cOH
    return [-f1,f1-f2,f1-f2]

  solve_ivp(yprime, [0, t], 
            [.1,.4,.3])


def rxn_network_2(t):
  def yprime(t, sp):
    cH2O2, cH2, cWater = sp
    k1, k2 = [0.01, 0.3]
    f1 = k1 * cH2O2 * cH2
    f2 = k2 * cWater
    return [-f2,-f2,2*f2-f1]

  solve_ivp(yprime, [0, t], 
            [.5,.5,.1])
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)

Motivating problem

Gluing together graphs

Gluing together Petri nets


Gluing together dynamical systems

Reinvent the wheel each time? Or general solution possible?

Generalizability

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.

Two intuitions we will make precise today

Part 1. The notion of being empty

Part 2. Placing things side-by-side

Part 1: Emptiness for sets

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:

Emptiness for graphs

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.



Initial object for graphs

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

Initial object for Petri nets

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.

Emptiness in general characterized by 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\).

Worked examples

[See whiteboard]

Part 2: Placing sets side-by-side

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\).

Property of disjoint union

Fun fact

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

Worked examples

[See whiteboard]

Coproduct as universal property

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.

Can you prove that 1+1 is not equal to 1?

Coproduct for graphs

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

Coproduct for Petri nets and beyond

Worked examples

[See whiteboard]

(BONUS MATERIAL) Pushouts: a notion of gluing

Universal property of a pushout:

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

Lecture 1&2 Conclusions

  • 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.

Resources

Applied category theory textbooks

  1. Category Theory for the Sciences
  2. Seven Sketches in Compositionality

Category theory textbooks

Blogs

  1. AlgebraicJulia
  2. Math3ma
  3. Graphical Linear Algebra
  4. Kittenlab

(BONUS MATERIAL) Coproduct for graphs

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?

(BONUS MATERIAL) Coproduct for graphs

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?