1
Instructor:
Welcome to BME 603/606�
Computational Methods for�Biological Modeling & Simulation
Assistant Professor of Biomedical Engineering�Affiliate, Data Science Institute�Affiliate, Biophysics Graduate Program�Affiliate, Center for Genomic Science Innovation�Affiliate, Wisconsin Institute for Translational Neuroengineering �University of Wisconsin-Madison
DHANANJAY ‘Jay’ BHASKAR
Course Website: https://bio-modeling-uw-madison-f26.github.io/
2
BME 603/606: Computational Methods for Biological Modeling & Simulation
Announcements
3
BME 603/606: Computational Methods for Biological Modeling & Simulation
Last week’s reflection
4
BME 603/606: Computational Methods for Biological Modeling & Simulation
A matrix is a linear transformation
Ax = b
A maps every input vector x to an output vector b
Linearity:
A(αx + βy) = α(Ax) + β(Ay)
Rotation/Reflection:
Orthogonal matrices are length preserving
Scaling:
Diagonal matrices stretch vectors
Projection:
Project to lower dimensions
5
BME 603/606: Computational Methods for Biological Modeling & Simulation
Creating matrices in numpy
import numpy as np
# From a list of lists
A = np.array([[1, 2, 3],
[4, 5, 6],
[7, 8, 9]])
print(A.shape) # (3, 3)
print(A.dtype) # int64
# Special matrices
I = np.eye(3) # 3×3 identity
Z = np.zeros((3, 3)) # all zeros
O = np.ones((2, 4)) # all ones
1
0
0
0
1
0
0
0
1
0
0
0
0
0
0
0
0
0
np.eye(3)
np.zeros((3,3))
np.shape returns a tuple.
A.shape[0] → rows
A.shape[1] → cols
6
BME 603/606: Computational Methods for Biological Modeling & Simulation
Diagonal and other special matrices
d = np.array([2., 5., 3.])
# Build diagonal matrix from vector
D = np.diag(d)
# [[2, 0, 0],
# [0, 5, 0],
# [0, 0, 3]]
# Extract diagonal from a matrix
np.diag(A) # → [a11, a22, a33]
# Triangular forms
np.triu(A) # upper triangle
np.tril(A) # lower triangle
D = np.diag(2,5,3)
2
0
0
0
5
0
0
0
3
7
BME 603/606: Computational Methods for Biological Modeling & Simulation
Matrix operations
A = np.array([[1., 2.],
[3., 4.]])
B = np.array([[5., 6.],
[7., 8.]])
A.T # transpose
A @ B # matrix multiply (compose)
A * B # element-wise multiply
np.dot(a, b) # dot product of 1-D vectors
np.trace(A) # sum of diagonal = 5
np.linalg.norm(A) # Frobenius norm
np.linalg.det(A) # determinant
Transpose
Flip rows ↔ columns
(Aᵀ)ᵢⱼ = Aⱼᵢ
Matrix Multiplication
Compose transformations
(AB)ᵢⱼ = Σₖ AᵢₖBₖⱼ
Element-wise Multiplication
Scale each element independently
Trace
Sum of diagonal = Σᵢ Aᵢᵢ
8
BME 603/606: Computational Methods for Biological Modeling & Simulation
Matrix inverse
A A⁻¹ = I
A = np.array([[2., 1.],
[5., 3.]])
A_inv = np.linalg.inv(A)
# Verify A @ A_inv ≈ I
print(np.round(A @ A_inv))
# Better: solve Ax = b directly
b = np.array([1., 2.])
x = np.linalg.solve(A, b)
# Check rank (invertible iff rank = n)
np.linalg.matrix_rank(A)
Ax = b
x = A⁻¹ b
9
BME 603/606: Computational Methods for Biological Modeling & Simulation
Application: balancing chemical reactions
Step 1: Write the unbalanced reaction
Step 2: Build the element matrix A
Step 3: Compute the nullspace of A
Step 4: Scale to integers
List every compound, reactants and products
Rows = chemical elements, Columns = compounds
Coefficients: reactants → + products → −
Find all x such that A x = 0
Normalize the nullspace vector to whole-number ratios
10
BME 603/606: Computational Methods for Biological Modeling & Simulation
Example: combustion of methane
_ CH₄ + _ O₂ → _ CO₂ + _ H₂O
import numpy as np
from scipy.linalg import null_space
# CH4 O2 CO2 H2O
A = np.array([
[ 1, 0, -1, 0], # C
[ 4, 0, 0, -2], # H
[ 0, 2, -2, -1], # O
], dtype=float)
ns = null_space(A)
v = ns[:, 0]
# Scale to smallest integers
v = v / np.min(np.abs(v[np.abs(v) > 1e-9]))
coeffs = np.round(np.abs(v)).astype(int)
print(coeffs) # [1, 2, 1, 2]
Element matrix A
CH₄
O₂
CO₂
H₂O
C
1
0
−1
0
H
4
0
0
−2
O
0
2
−2
−1
CH₄ + 2 O₂ → CO₂ + 2 H₂O
The nullspace vector [1, 2, 1, 2] gives the ratio of molecules: 1 CH₄ reacts with 2 O₂ to produce 1 CO₂ and 2 H₂O.
11
BME 603/606: Computational Methods for Biological Modeling & Simulation
Example: oxidation of iron
# Fe O2 Fe2O3
A = np.array([
[ 1, 0, -2], # Fe
[ 0, 2, -3], # O
], dtype=float)
ns = null_space(A)
v = ns[:, 0]
v = v / np.min(np.abs(v[np.abs(v) > 1e-9]))
coeffs = np.round(np.abs(v)).astype(int)
print(coeffs) # [4, 3, 2]
_ Fe + _ O₂ → _ Fe₂O₃
Element matrix A
Fe
O₂
Fe₂O₃
Fe
1
0
−2
O
0
2
−3
4 Fe + 3 O₂ → 2 Fe₂O₃
Alert! This calculation will be on a quiz!
12
BME 603/606: Computational Methods for Biological Modeling & Simulation
Hands-on Git/GitHub Tutorial
Please ask Colin to mark you for completion before end of class.