Exercise: convergence and cost
Goal
By the end of this exercise you should be able to:
- separate truncation error (from the formula) and rounding error (from floating point) in a numerical derivative, and predict the step \(h\) where their sum is smallest,
- read the order of accuracy \(p\) in error \(\approx C h^p\) from a table or from the slope of a log–log plot, and check it without knowing the exact answer,
- tell linear from quadratic convergence of an iteration by the digits gained per step,
- count operations for vector and matrix computations and use big-O to predict how the run time grows,
- run a doubling experiment, read the cost exponent from a log–log plot of time against \(n\), and extrapolate to a full single-cell data set,
- estimate the memory of a matrix (8 bytes per double) and say when sparse storage saves you.
Do the pen-and-paper problems first. The code problems measure the same things at sizes where only code works.
Background
Truncation vs rounding. The forward difference \(D(h) = \dfrac{f(t+h) - f(t)}{h}\) approximates \(f'(t)\). Taylor’s theorem gives
\[D(h) = f'(t) + \tfrac{h}{2} f''(\xi), \qquad \text{so the truncation error} \approx \tfrac{|f''|}{2}\,h.\]
Each value of \(f\) is stored with a relative error up to \(\varepsilon \approx 2.2\times10^{-16}\); the subtraction keeps that absolute error and the division by \(h\) amplifies it:
\[\text{rounding error} \approx \frac{\varepsilon\,|f(t)|}{h}.\]
The total error is V-shaped on a log–log plot, with its minimum near \(h \approx \sqrt{\varepsilon} \approx 10^{-8}\) for the forward difference.
Order of accuracy. A method has order \(p\) if its error \(\approx C h^p\), written \(O(h^p)\). On a log–log plot, \(\log_{10}\text{err} = \log_{10}C + p\log_{10}h\): a straight line with slope \(p\). From two points: \(p = \dfrac{\log_{10}(\text{err}_1/\text{err}_2)}{\log_{10}(h_1/h_2)}\). Without the exact answer: \(\dfrac{D(h) - D(h/2)}{D(h/2) - D(h/4)} \approx 2^p\).
Convergence of iterations. Linear: \(e_{k+1} \approx c\,e_k\) (\(0 < c < 1\)), a fixed \(-\log_{10}c\) digits per step. Quadratic: \(e_{k+1} \approx C e_k^2\), the number of correct digits roughly doubles each step.
Cost. For vectors of length \(n\) and \(n\times n\) matrices: dot product \(\approx 2n\) operations, matrix–vector \(\approx 2n^2\), matrix–matrix \(\approx 2n^3\). A cost \(O(n^p)\) is multiplied by \(2^p\) when \(n\) doubles. Time it at several \(n\) and fit the slope of \(\log(\text{time})\) against \(\log n\).
Memory. One double = 8 bytes. A dense \(r \times c\) matrix takes \(8rc\) bytes. A sparse matrix (compressed sparse column) stores about 12 bytes per non-zero: 8 for the value, 4 for its row index.
Timing and measuring memory in R, Python and Rust:
x <- runif(1e6)
system.time(sort(x))["elapsed"] # seconds, wall clock
object.size(x) # bytes
# 8000048 bytesimport time
import numpy as np
x = np.random.default_rng(1).random(10**6)
start = time.perf_counter()
np.sort(x)
print(time.perf_counter() - start) # seconds, wall clock
print(x.nbytes) # bytes
# 8000000// rust/examples/convergence_cost_background.rs — cargo run --release --example convergence_cost_background
// Timing and measuring memory in Rust.
// Run in release mode for honest timings:
// cargo run -q --release --example convergence_cost_background
// Times depend on your machine; the random numbers differ from R and Python.
use rand::{rngs::StdRng, RngExt, SeedableRng};
use std::hint::black_box;
use std::time::Instant;
fn main() {
let mut rng = StdRng::seed_from_u64(1);
let mut x: Vec<f64> = (0..1_000_000).map(|_| rng.random::<f64>()).collect();
let bytes = x.len() * std::mem::size_of::<f64>(); // bytes of the data
let start = Instant::now();
x.sort_by(f64::total_cmp); // f64 has no plain sort(): NaN has no order
black_box(&x); // stop the optimiser from skipping the sort
println!("{}", start.elapsed().as_secs_f64()); // seconds, wall clock
println!("{bytes}"); // bytes
}
// Output:
// 0.043023958
// 8000000R: system.time(), object.size(). Python: time.perf_counter(), ndarray.nbytes (also the timeit module for repeated timings). Timings depend on your machine: in the solutions below, the times are examples from one machine; your numbers will differ, the ratios and slopes should not. Rust: std::time::Instant, with std::hint::black_box so the compiler cannot skip the timed work; memory is len() * size_of::<f64>().
Rust prints numbers in its own format (e.g. 1e-3 rather than 1e-03); the values are the same unless the text says otherwise. Run the timing examples with --release: debug builds are much slower.
By hand
B1. (two difference formulas) (a) For \(f(x) = x^2\) at \(x = 1\) (true derivative \(2\)), compute the forward difference \(\dfrac{f(1+h) - f(1)}{h}\) for \(h = 0.1\) and \(h = 0.01\), and the errors. Show that the error is exactly \(h\), and connect this to \(\tfrac{h}{2}f''\). (b) Compute the central difference \(\dfrac{f(1+h) - f(1-h)}{2h}\) for \(h = 0.1\). What is its error? (c) Repeat (a) and (b) for \(f(x) = x^3\) at \(x = 1\) (true derivative \(3\)). Expand \((1 \pm h)^3\) to get each error as a polynomial in \(h\). What happens to each error when \(h\) is divided by 10? What are the two orders?
B2. (reading the order from a table) Three methods, three error tables:
| \(h\) | method A | method B | method C |
|---|---|---|---|
| \(0.2\) | \(4.0\times10^{-3}\) | \(8.0\times10^{-2}\) | |
| \(0.1\) | \(1.0\times10^{-3}\) | \(1.0\times10^{-2}\) | |
| \(0.05\) | \(2.5\times10^{-4}\) | \(1.25\times10^{-3}\) | |
| \(10^{-10}\) | \(1.0\times10^{-6}\) | ||
| \(5\times10^{-11}\) | \(2.1\times10^{-6}\) | ||
| \(2.5\times10^{-11}\) | \(3.9\times10^{-6}\) |
- Find the order \(p\) of methods A and B, and the constant \(C\) in error \(\approx C h^p\).
- For each of A and B, how small must \(h\) be for an error of \(10^{-8}\) (ignoring rounding)?
- What is happening in method C? What would you change?
B3. (the best step) The total error of the forward difference is about \(E(h) = a\,h + \dfrac{b}{h}\) with \(a = |f''|/2\) and \(b = \varepsilon|f|\). (a) Set \(E'(h) = 0\) and show that the best step is \(h^* = \sqrt{b/a}\) and the smallest error is \(E(h^*) = 2\sqrt{ab}\). (b) For the OD curve of the lecture at \(t = 3\) h: \(f = 0.389\), \(|f''| = 0.0592\), \(\varepsilon = 2.2\times10^{-16}\). Compute \(h^*\) and \(E(h^*)\). (The lecture measured an error of \(2.0\times10^{-9}\) at \(h = 10^{-7}\) and \(3.0\times10^{-9}\) at \(h = 10^{-8}\).) (c) For the central difference, \(E(h) \approx c\,h^2 + b/h\). Show that \(h^* \propto \varepsilon^{1/3}\), and estimate it.
B4. (digits per step) Errors of three iterations:
- \(0.1,\ 0.05,\ 0.025,\ 0.0125, \ldots\)
- \(10^{-1},\ 10^{-2},\ 10^{-4},\ 10^{-8}, \ldots\)
- \(0.5,\ 0.45,\ 0.405,\ 0.3645, \ldots\)
For each: linear or quadratic? Give \(c\) (linear) or \(C\) (quadratic). How many steps does (a) need to get from \(0.1\) below \(10^{-10}\)? What is the next error in (b)? How many digits does (c) gain per step, and roughly how many steps does it need to get below \(10^{-10}\)?
B5. (counting operations) Assume \(10^9\) operations per second. (a) For \(n\times n\) matrices \(A\), \(B\) and a vector \(\mathbf x\) with \(n = 20\,000\), count the operations for \((AB)\mathbf x\) and for \(A(B\mathbf x)\). Both give the same vector. How long does each take? (b) How many gene pairs are there for 100 genes, and for 20 000 genes? By what factor does an all-pairs correlation get slower? (c) A clustering step needs the Euclidean distance between every pair of 100 000 cells, each described by 50 principal components. Count the pairs and the operations per distance. How long does it take, and how much memory does a full \(100\,000 \times 100\,000\) distance matrix need?
B6. (doubling experiments) (a) A method takes \(0.40\), \(1.6\) and \(6.5\) s for \(n = 1000, 2000, 4000\). What is \(p\)? Predict the time for \(n = 32\,000\). (b) Another method takes \(0.010\), \(0.011\) and \(0.013\) s for \(n = 10, 20, 40\). What can you conclude about its cost? (c) A colleague’s \(O(n^3)\) method takes 2 s for \(n = 1000\). How long for \(n = 20\,000\)?
B7. (memory) A single-cell atlas has 30 000 genes × 1 000 000 cells. (a) How much memory does it need as a dense matrix of doubles? (b) As a sparse matrix with 5 % non-zeros, at 12 bytes per non-zero? (c) How large is the gene–gene correlation matrix? The cell–cell distance matrix? (d) Why does centring each gene (subtracting its mean) turn (b) into (a)?
In code
Write and run each task in R, Python or Rust. The setup for C1 and C2 is the OD curve of the lecture:
K <- 1.2; N0 <- 0.05; r <- 0.8 # OD units, per hour
od <- function(t) K / (1 + (K / N0 - 1) * exp(-r * t))
t0 <- 3
exact <- r * od(t0) * (1 - od(t0) / K) # logistic: N' = r N (1 - N/K)import numpy as np
K, N0, r = 1.2, 0.05, 0.8 # OD units, per hour
def od(t):
return K / (1 + (K / N0 - 1) * np.exp(-r * t))
t0 = 3.0
exact = r * od(t0) * (1 - od(t0) / K) # logistic: N' = r N (1 - N/K)// rust/examples/convergence_cost_setup.rs — cargo run --example convergence_cost_setup
// Setup for C1 and C2: the OD curve of the lecture.
// (The C1 and C2 files repeat these lines, so each runs on its own.)
const K: f64 = 1.2; // OD units
const N0: f64 = 0.05; // OD units
const R: f64 = 0.8; // per hour
fn od(t: f64) -> f64 {
K / (1.0 + (K / N0 - 1.0) * (-R * t).exp())
}
fn main() {
let t0 = 3.0;
let exact = R * od(t0) * (1.0 - od(t0) / K); // logistic: N' = r N (1 - N/K)
println!("{exact}"); // check: the exact derivative at t0
}
// Output:
// 0.21025973485171418C1. (the V) Compute the forward-difference error for \(h = 10^{-1}, 10^{-2}, \ldots, 10^{-15}\) in one vectorised line. Print a table and plot the error against \(h\) on log–log axes. Which \(h\) gives the smallest error? Compare with \(\sqrt{\varepsilon}\) and with your \(h^*\) from B3.
C2. (measure the order) For \(h = 10^{-1}, 10^{-1.5}, \ldots, 10^{-4}\), fit the slope of \(\log_{10}\) error against \(\log_{10} h\) for the forward and for the central difference. Then, pretending you do not know the exact answer, compute the ratios \(\dfrac{D(h) - D(h/2)}{D(h/2) - D(h/4)}\) for the central difference starting at \(h_0 = 0.1\) and at \(h_0 = 10^{-8}\). What do they tell you?
C3. (linear vs quadratic) Run 60 steps of bisection on \([1, 2]\) and of Newton \(x_{k+1} = \tfrac12(x_k + 2/x_k)\) from \(x_0 = 1\) for \(\sqrt{2}\). For bisection use the bracket width as the error; for Newton use \(|x_k - \sqrt2|\). After how many steps is each error below \(10^{-12}\)? Compute \(e_{k+1}/e_k\) for bisection and \(e_{k+1}/e_k^2\) for Newton. Print the number of correct digits, \(-\log_{10} e_k\), for the first five Newton steps.
C4. (doubling experiment: gene correlation) Simulate a cells × genes count matrix with 1000 cells and 500, 1000, 2000, 4000 genes (Poisson counts with mean 2). Time the gene–gene correlation matrix for each size. Print the ratios of successive times, fit the slope on a log–log scale, and extrapolate to 20 000 genes × 100 000 cells.
C5. (order of operations) With \(n = 2000\) random \(n\times n\) matrices \(A\), \(B\) and a random vector \(\mathbf x\), time \((AB)\mathbf x\) and \(A(B\mathbf x)\). Check that the results agree. Compare the speed-up with your prediction from B5(a).
C6. (loops vs vectorised) Compute the dot product of two random vectors of length \(10^7\) with an explicit loop, with an element-wise product and sum, and with a library dot product. Time all three. What is the cost order of each? How much does vectorising buy you in your language?
C7. (memory and sparsity) (a) Check that a \(1000\times1000\) matrix of zeros takes 8 MB. Compute the size of the dense 20 000 × 100 000 matrix and of the 20 000 × 20 000 correlation matrix in GB. (b) Make a random sparse 2000 × 10 000 (genes × cells) matrix with 10 % non-zero counts. Compare its memory with the same matrix stored dense. (c) Centre each gene (subtract its row mean). What happens to the storage?
Explore
The demo below computes the growth rate of the lecture’s OD curve at \(t = 3\) h (\(K = 1.2\), \(N_0 = 0.05\), \(r = 0.8\)) in your browser. Panel (a) shows the curve, the tangent (the true growth rate) and the secant whose slope is the difference formula. Panel (b) shows the actual error for every \(h\) from \(1\) down to \(10^{-16}\), with the truncation model \(C h^p\) (dashed), the rounding model \(\varepsilon |N| / h\) (dotted) and a marker at the current \(h\). The readout gives the estimate, the true derivative \(r N (1 - N/K)\), the errors, and the slope measured between \(h\) and \(h/2\), \(\log_2\big(\text{err}(h) / \text{err}(h/2)\big)\). The second tab, Cost and memory, turns operation counts into times and matrix sizes into gigabytes.
The browser computes in the same IEEE doubles as R, Python and Rust, so the numbers agree with the lecture to the digits shown; deep in the rounding regime the last digits can differ, because exp may be rounded differently.
E1. (the truncation side) With the forward formula, click \(h = 1\), then \(10^{-1}\), then \(10^{-4}\). For each, write down the estimate, the absolute error and the measured slope. In panel (a), why does the secant lie above the tangent at \(h = 1\)? By what factor does the error fall per factor 10 in \(h\), and which constant from the lecture does \(\text{err}/h\) give?
\(h = 1\): estimate \(0.2306\), error \(0.020\) (9.65 %), slope \(0.67\). \(h = 0.1\): \(0.2131\), error \(2.9\times10^{-3}\), slope \(0.98\). \(h = 10^{-4}\): \(0.21026\), error \(3.0\times10^{-6}\), slope \(1.00\). The curve is convex at \(t = 3\) (it bends upwards, \(N'' > 0\)), so the point at \(t + h\) lies above the tangent, and so does the secant. Each factor 10 in \(h\) divides the error by 10: order 1. \(\text{err}/h \to 0.0296 = |N''|/2\), the constant of the dashed line. At \(h = 1\) the slope is not yet 1: \(h\) is too large for the leading Taylor term to dominate.
E2. (the rounding side) Keep going: \(10^{-8}\), \(10^{-12}\), \(10^{-15}\), then drag the slider to the far left (\(10^{-16}\)). Record the errors and the measured slopes. What does the estimate become at the end, and what does the readout tell you?
\(h = 10^{-8}\): error \(3.0\times10^{-9}\). \(h = 10^{-12}\): \(3.9\times10^{-5}\). \(h = 10^{-15}\): estimate \(0.111\), error \(0.099\), 47 % off. The errors now grow as \(h\) shrinks and follow the dotted line \(\varepsilon|N|/h\) with jagged scatter; the measured slopes jump around (\(0.00\), \(-1.28\), \(-1.08\)) instead of settling: the sign of the rounding regime. At \(h = 10^{-16}\) the estimate is exactly \(0\) (100 % error): \(3 + 10^{-16}\) rounds to \(3\), so both OD values are the same double and their difference is \(0\). Already at \(h \approx 3\times10^{-16}\) the two OD values are identical, although there \(t + h \ne t\).
E3. (the bottom of the V) Click best h*. Read off \(h\), the error and the relative error. Compare with \(h^*\) and \(E(h^*)\) from B3. How many significant digits of the growth rate are correct?
The button jumps to \(h = 5.6\times10^{-8}\), the slider position closest to \(h^* = 5.4\times10^{-8}\) from B3 (the green line): error \(9.9\times10^{-10}\), relative error \(4.7\times10^{-9}\). B3 predicted \(E(h^*) \approx 3.2\times10^{-9}\); the actual error is a little smaller, because the rounding errors partly cancel and the model gives their typical size, not their exact value. About 8 significant digits are correct, half of the 16 a double carries. Moving \(h\) a factor 10 either way makes the error larger.
E4. (a second-order formula) Switch to central. Repeat \(h = 10^{-1}\) and \(10^{-4}\) and note the slopes. Then click best h* and \(h = 10^{-5}\), and compare with \(h = 10^{-8}\), the best step for the forward formula. What changed in panel (b), and what stayed the same?
\(h = 0.1\): error \(7.0\times10^{-5}\), slope \(2.00\); \(h = 10^{-4}\): \(7.0\times10^{-11}\), slope \(2.03\). Dividing \(h\) by 10 now gains two digits. The green line moves to \(h^* \approx 1.8\times10^{-5}\), of the size \(\varepsilon^{1/3}\); at \(h = 10^{-5}\) the error is \(9.3\times10^{-13}\), about 12 correct digits. At \(h = 10^{-8}\) the central formula is worse (\(2.3\times10^{-10}\)): that \(h\) is already on its rounding side. The truncation line is steeper (slope 2, constant \(|N'''|/6 = 0.0070\)), so the V moves to a larger \(h\) and a lower error. The rounding side is the same for both formulas: the faint curve of the forward formula lies on top of it.
E5. (cost and memory) Open the Cost and memory tab. Click \(n = 100\) genes and read the four times; click \(n \times 2\) a few times and watch how each time grows; then click \(20\,000\). Which operation goes from milliseconds to hours? With \(20\,000\) genes, at how many cells does the dense matrix fill a 16 GB laptop, and up to what fraction of non-zeros is the sparse matrix smaller than the dense one?
At \(n = 100\): dot product 200 ns, matrix–vector 20 µs, gene–gene correlation over 100 000 cells (\(n^2 m = 10^9\) operations) 1 s, matrix–matrix 2 ms. Each doubling of \(n\) multiplies these by 2, 4, 4 and 8. At \(n = 20\,000\): 40 µs, 0.8 s, 11 h and 4.4 h. The matrix–matrix product goes from 2 ms to 4.4 hours, \(200^3 = 8\) million times longer. With 20 000 genes the dense matrix reaches 16 GB at 100 000 cells (\(8 \times 2\times10^4 \times 10^5\) bytes). Sparse storage takes 12 bytes per non-zero against 8 bytes per entry, so it is smaller below \(8/12 = 67\) % non-zeros; at 10 % it needs 2.4 GB.
Hints
\((1+h)^2 = 1 + 2h + h^2\) and \((1+h)^3 = 1 + 3h + 3h^2 + h^3\). For \((1-h)^3\) flip the sign of the odd powers. In the central difference the even powers cancel.
Halving \(h\) divides an \(O(h^p)\) error by \(2^p\): a factor 4 means \(p = 2\), a factor 8 means \(p = 3\). Then \(C = \text{err}/h^p\) from any row, and \(h = (\text{target}/C)^{1/p}\). If the error grows as \(h\) shrinks, you are on the rounding side of the V.
\(E'(h) = a - b/h^2\). For the central difference \(E'(h) = 2ch - b/h^2\), so \(h^3 = b/(2c)\). Since \(b \propto \varepsilon\), \(h^* \propto \varepsilon^{1/3}\); with \(|f|\) and \(c\) of order one, \(h^* \approx \varepsilon^{1/3}\).
Compute \(e_{k+1}/e_k\): constant means linear. If not constant, compute \(e_{k+1}/e_k^2\). Digits per step for linear convergence: \(-\log_{10} c\). Steps needed: (digits to gain) / (digits per step).
A matrix–vector product is \(2n^2\) operations, a matrix–matrix product \(2n^3\). The number of pairs among \(n\) items is \(n(n-1)/2\). A Euclidean distance in \(d\) dimensions needs \(d\) subtractions, \(d\) multiplications and \(d-1\) additions (the square root is negligible).
Ratio \(= 2^p\), so \(p = \log_2(\text{ratio})\). Going from \(n\) to \(8n\) is three doublings: the time is multiplied by \((2^p)^3\). When the times barely change, ask what else could take 10 ms.
od(t0 + h) works for a vector h in R and Python, so the whole table is one line; in Rust, h.iter().map(...) does the same. R: plot(h, err, log = "xy"); slope with lm(log10(err) ~ log10(h)). Python: plt.loglog; slope with np.polyfit(np.log10(h), np.log10(err), 1)[0]. Machine epsilon: .Machine$double.eps / np.finfo(float).eps.
R: system.time(expr)["elapsed"]; correlation of columns with cor(); counts with rpois; library dot product crossprod. Python: start = time.perf_counter() before and time.perf_counter() - start after; np.corrcoef(X, rowvar=False) correlates columns; counts with rng.poisson; dot product x @ y. Rust: let start = Instant::now(); before and start.elapsed().as_secs_f64() after; Poisson counts with rand_distr::Poisson; matrix and dot products with nalgebra (&a * &b, x.dot(&y)); write the correlation as a loop over gene pairs. If a run takes less than ~0.1 s, the time is mostly overhead.
R: the Matrix package ships with R; rsparsematrix() makes a random sparse matrix, as.matrix() makes it dense. Python: scipy.sparse.random with format="csc"; the storage is S.data.nbytes + S.indices.nbytes + S.indptr.nbytes; S.toarray() makes it dense. Rust: sprs has no random-matrix generator, so build the CSC arrays (indptr, indices, data) yourself and pass them to CsMatI::new_csc; the storage is the length of each array times its element size.
Solutions
B1. (a) \(h = 0.1\): \((1.21 - 1)/0.1 = 2.1\), error \(0.1\). \(h = 0.01\): \((1.0201 - 1)/0.01 = 2.01\), error \(0.01\). In general \(\big((1+h)^2 - 1\big)/h = 2 + h\), so the error is exactly \(h\). With \(f'' = 2\): \(\tfrac{h}{2}f'' = h\). Taylor’s remainder is exact here because \(f''\) is constant. (b) \((1.21 - 0.81)/0.2 = 0.4/0.2 = 2\): error \(0\). In general \(\big((1+h)^2 - (1-h)^2\big)/(2h) = 4h/(2h) = 2\): the central difference is exact for any quadratic. (c) Forward: \(\big((1+h)^3 - 1\big)/h = 3 + 3h + h^2\), error \(3h + h^2\): \(0.31\) for \(h = 0.1\), \(0.0301\) for \(h = 0.01\). Dividing \(h\) by 10 divides the error by about 10: order 1. Central: \(\big((1+h)^3 - (1-h)^3\big)/(2h) = (6h + 2h^3)/(2h) = 3 + h^2\), error \(h^2\): \(0.01\) for \(h = 0.1\) (\(= (1.331 - 0.729)/0.2 - 3\)), \(10^{-4}\) for \(h = 0.01\). Dividing \(h\) by 10 divides the error by 100: order 2.
B2. (a) A: each halving divides the error by 4, so \(p = 2\); \(C = 4.0\times10^{-3}/0.2^2 = 0.1\). B: each halving divides by 8, so \(p = 3\); \(C = 8.0\times10^{-2}/0.2^3 = 10\). (b) A: \(0.1\,h^2 = 10^{-8}\) gives \(h = \sqrt{10^{-7}} \approx 3.2\times10^{-4}\). B: \(10\,h^3 = 10^{-8}\) gives \(h = (10^{-9})^{1/3} = 10^{-3}\). The higher-order method reaches the target with a 3 times larger step. (c) The error roughly doubles when \(h\) halves: slope \(-1\), error \(\propto 1/h\). Method C is on the rounding side of the V. Use a larger \(h\) (for a forward difference around \(10^{-8}\)).
B3. (a) \(E'(h) = a - b/h^2 = 0\) gives \(h^* = \sqrt{b/a}\). Then \(E(h^*) = a\sqrt{b/a} + b\sqrt{a/b} = \sqrt{ab} + \sqrt{ab} = 2\sqrt{ab}\). At the optimum the two errors are equal. (b) \(a = 0.0592/2 = 0.0296\), \(b = 2.2\times10^{-16} \times 0.389 = 8.6\times10^{-17}\). \(h^* = \sqrt{8.6\times10^{-17}/0.0296} = \sqrt{2.9\times10^{-15}} \approx 5.4\times10^{-8}\). \(E(h^*) = 2\sqrt{0.0296 \times 8.6\times10^{-17}} = 2\sqrt{2.5\times10^{-18}} \approx 3.2\times10^{-9}\). The measured errors, \(2\times10^{-9}\) and \(3\times10^{-9}\) on either side of \(h^*\), match. Only about 8 of the 16 digits of a double survive. (c) \(E'(h) = 2ch - b/h^2 = 0\) gives \(h^{*3} = b/(2c)\), so \(h^* = (b/2c)^{1/3} \propto \varepsilon^{1/3} \approx (2.2\times10^{-16})^{1/3} \approx 6\times10^{-6}\). The best error is then of order \(\varepsilon^{2/3} \approx 4\times10^{-11}\): about 10–11 digits instead of 8.
B4. (a) Ratio \(0.5\) every step: linear, \(c = 1/2\) (this is bisection). \(0.1 \times 2^{-k} < 10^{-10}\) needs \(2^k > 10^9\), so \(k = 30\) steps (\(2^{30} \approx 1.07\times10^9\)). (b) \(e_{k+1} = e_k^2\): quadratic with \(C = 1\). Next error \(10^{-16}\), i.e. machine precision after 5 steps. (c) Ratio \(0.9\) every step: linear, \(c = 0.9\). Digits per step \(-\log_{10}0.9 \approx 0.046\). From \(0.5\) (0.3 digits) to \(10^{-10}\) (10 digits) is \(9.7\) digits: \(9.7/0.046 \approx 210\) steps. Linear convergence with \(c\) close to 1 is very slow.
B5. (a) \((AB)\mathbf x\): \(2n^3 + 2n^2 \approx 2 \times 8\times10^{12} = 1.6\times10^{13}\) operations, \(\approx 16\,000\) s \(\approx 4.4\) hours. \(A(B\mathbf x)\): two matrix–vector products, \(4n^2 = 1.6\times10^9\) operations, \(\approx 1.6\) s. Same answer, a factor \(n/2 = 10\,000\) in work. Brackets matter. (b) \(100 \times 99/2 = 4950\) pairs; \(20\,000 \times 19\,999/2 = 199\,990\,000 \approx 2\times10^8\) pairs. Factor \(\approx 40\,400\), i.e. \((20\,000/100)^2 = 40\,000\). (c) Pairs: \(100\,000 \times 99\,999/2 \approx 5\times10^9\). Per distance \(50 + 50 + 49 \approx 150\) operations. Total \(\approx 7.5\times10^{11}\) operations, \(\approx 750\) s \(\approx 12\) minutes. The full distance matrix has \(10^{10}\) entries \(\times 8\) bytes \(= 80\) GB (40 GB if only one triangle is stored): it does not fit on a laptop. This is why tools that build nearest-neighbour graphs for clustering and UMAP never store all distances.
B6. (a) Ratios \(1.6/0.40 = 4.0\) and \(6.5/1.6 \approx 4.1\): \(p = \log_2 4 = 2\). From 4000 to 32 000 is three doublings: \(6.5 \times 4^3 = 416\) s \(\approx 7\) minutes. (b) Almost nothing. The times barely change because they are dominated by fixed overhead (starting the function, allocating memory, the clock itself), not by the work that grows with \(n\). The apparent slope (\(\approx 0.2\)) is meaningless; time it at much larger \(n\), until each run takes at least ~0.1 s. (c) \(2 \times 20^3 = 16\,000\) s \(\approx 4.4\) hours.
B7. (a) \(3\times10^4 \times 10^6 = 3\times10^{10}\) entries \(\times 8\) bytes \(= 2.4\times10^{11}\) bytes \(= 240\) GB. (b) \(5\,\% \times 3\times10^{10} = 1.5\times10^9\) non-zeros \(\times 12\) bytes \(= 18\) GB (plus 4 MB of column pointers). Large, but workstation-sized. (c) Gene–gene: \((3\times10^4)^2 \times 8 = 7.2\) GB. Cell–cell: \((10^6)^2 \times 8 = 8\times10^{12}\) bytes \(= 8\) TB: impossible to store. (d) Subtracting the gene mean (positive for any gene that is detected at all) turns every zero into a non-zero negative number. Every entry must then be stored: 240 GB dense, or even more as a “sparse” matrix with no zeros (12 instead of 8 bytes per entry).
C1.
# uses od(), t0 and exact from the setup above C1
h <- 10^-(1:15)
err <- abs((od(t0 + h) - od(t0)) / h - exact)
for (i in seq_along(h)) cat(sprintf("%.0e %.3g\n", h[i], err[i]))
# 1e-01 0.00289
# 1e-02 0.000295
# 1e-03 2.96e-05
# 1e-04 2.96e-06
# 1e-05 2.96e-07
# 1e-06 2.96e-08
# 1e-07 1.99e-09
# 1e-08 3e-09
# 1e-09 1.92e-08
# 1e-10 1.47e-07
# 1e-11 5.7e-06
# 1e-12 3.9e-05
# 1e-13 0.000428
# 1e-14 0.00487
# 1e-15 0.0992
h[which.min(err)]
# [1] 1e-07
sqrt(.Machine$double.eps)
# [1] 1.490116e-08
plot(h, err, log = "xy", type = "b", xlab = "h", ylab = "|error|")# uses od(), t0 and exact from the setup above C1
import matplotlib.pyplot as plt
h = 10.0 ** -np.arange(1, 16)
err = np.abs((od(t0 + h) - od(t0)) / h - exact)
for hi, e in zip(h, err):
print(f"{hi:.0e} {e:.3g}")
# 1e-01 0.00289
# 1e-02 0.000295
# 1e-03 2.96e-05
# 1e-04 2.96e-06
# 1e-05 2.96e-07
# 1e-06 2.96e-08
# 1e-07 1.99e-09
# 1e-08 3e-09
# 1e-09 1.92e-08
# 1e-10 1.47e-07
# 1e-11 5.7e-06
# 1e-12 3.9e-05
# 1e-13 0.000428
# 1e-14 0.00487
# 1e-15 0.0992
print(h[np.argmin(err)])
# 1e-07
print(np.sqrt(np.finfo(float).eps))
# 1.4901161193847656e-08
plt.loglog(h, err, "o-"); plt.xlabel("h"); plt.ylabel("|error|"); plt.show()// rust/examples/convergence_cost_c1.rs — cargo run --example convergence_cost_c1
// C1: the V-shaped error of the forward difference.
const K: f64 = 1.2;
const N0: f64 = 0.05;
const R: f64 = 0.8;
fn od(t: f64) -> f64 {
K / (1.0 + (K / N0 - 1.0) * (-R * t).exp())
}
fn main() {
let t0 = 3.0;
let exact = R * od(t0) * (1.0 - od(t0) / K);
let h: Vec<f64> = (1..=15).map(|k| 10f64.powi(-k)).collect();
let err: Vec<f64> = h.iter().map(|&h| ((od(t0 + h) - od(t0)) / h - exact).abs()).collect();
for (hi, e) in h.iter().zip(&err) {
println!("{hi:.0e} {e:.2e}"); // Rust writes 1e-1, not 1e-01
}
// index of the smallest error (total_cmp: a total order on f64)
let best = (0..err.len()).min_by(|&i, &j| err[i].total_cmp(&err[j])).unwrap();
println!("{:e}", h[best]);
println!("{:e}", f64::EPSILON.sqrt());
// No plotting here: write h and err to a file and plot in R or Python.
}
// Output:
// 1e-1 2.89e-3
// 1e-2 2.95e-4
// 1e-3 2.96e-5
// 1e-4 2.96e-6
// 1e-5 2.96e-7
// 1e-6 2.96e-8
// 1e-7 1.99e-9
// 1e-8 3.00e-9
// 1e-9 1.92e-8
// 1e-10 1.47e-7
// 1e-11 5.70e-6
// 1e-12 3.90e-5
// 1e-13 4.28e-4
// 1e-14 4.87e-3
// 1e-15 9.92e-2
// 1e-7
// 1.4901161193847656e-8The error falls by a factor 10 per row down to \(h = 10^{-7}\), then rises again. The best step on this grid is \(h = 10^{-7}\) (error \(2\times10^{-9}\)), with \(10^{-8}\) almost as good; both are close to \(\sqrt\varepsilon = 1.5\times10^{-8}\) and to \(h^* = 5.4\times10^{-8}\) from B3. The plot is the V of the lecture: slope \(+1\) on the right, about \(-1\) (jagged) on the left.
C2.
# uses od(), t0 and exact from the setup above C1
h <- 10^-seq(1, 4, by = 0.5)
fwd <- abs((od(t0 + h) - od(t0)) / h - exact)
cen <- abs((od(t0 + h) - od(t0 - h)) / (2 * h) - exact)
coef(lm(log10(fwd) ~ log10(h)))
# (Intercept) log10(h)
# -1.5380002 0.9971424
coef(lm(log10(cen) ~ log10(h)))
# (Intercept) log10(h)
# -2.150500 2.000896
D <- function(h) (od(t0 + h) - od(t0 - h)) / (2 * h)
ratios <- function(h0) {
d <- diff(sapply(h0 / 2^(0:5), D)) # D(h) - D(h/2), D(h/2) - D(h/4), ...
d[-length(d)] / d[-1] # should be 2^p
}
ratios(0.1)
# [1] 3.999289 3.999823 3.999956 3.999990
ratios(1e-8)
# [1] 0.05555556 2.25000000 0.50000000 -0.33333333# uses od(), t0 and exact from the setup above C1
h = 10.0 ** -np.arange(1, 4.01, 0.5)
fwd = np.abs((od(t0 + h) - od(t0)) / h - exact)
cen = np.abs((od(t0 + h) - od(t0 - h)) / (2 * h) - exact)
print(np.polyfit(np.log10(h), np.log10(fwd), 1)) # [slope, intercept]
# [ 0.99714241 -1.53800023]
print(np.polyfit(np.log10(h), np.log10(cen), 1))
# [ 2.00089632 -2.15049963]
def D(h):
return (od(t0 + h) - od(t0 - h)) / (2 * h)
def ratios(h0):
d = np.diff(D(h0 / 2.0 ** np.arange(6))) # D(h) - D(h/2), D(h/2) - D(h/4), ...
return d[:-1] / d[1:] # should be 2^p
print(ratios(0.1))
# [3.99928944 3.99982251 3.99995562 3.99998952]
print(ratios(1e-8))
# [ 0.05555556 2.25 0.5 -0.33333333]// rust/examples/convergence_cost_c2.rs — cargo run --example convergence_cost_c2
// C2: measure the order from the slope, and with the ratio test.
const K: f64 = 1.2;
const N0: f64 = 0.05;
const R: f64 = 0.8;
fn od(t: f64) -> f64 {
K / (1.0 + (K / N0 - 1.0) * (-R * t).exp())
}
// Least-squares line y = a + b x; returns (intercept, slope), like coef(lm(y ~ x)).
fn fit_line(x: &[f64], y: &[f64]) -> (f64, f64) {
let n = x.len() as f64;
let (mx, my) = (x.iter().sum::<f64>() / n, y.iter().sum::<f64>() / n);
let sxy: f64 = x.iter().zip(y).map(|(xi, yi)| (xi - mx) * (yi - my)).sum();
let sxx: f64 = x.iter().map(|xi| (xi - mx).powi(2)).sum();
let b = sxy / sxx;
(my - b * mx, b)
}
fn main() {
let t0 = 3.0;
let exact = R * od(t0) * (1.0 - od(t0) / K);
let h: Vec<f64> = (0..7).map(|k| 10f64.powf(-(1.0 + 0.5 * k as f64))).collect();
let fwd: Vec<f64> = h.iter().map(|&h| ((od(t0 + h) - od(t0)) / h - exact).abs()).collect();
let cen: Vec<f64> = h.iter().map(|&h| ((od(t0 + h) - od(t0 - h)) / (2.0 * h) - exact).abs()).collect();
let log10 = |v: &[f64]| v.iter().map(|x| x.log10()).collect::<Vec<f64>>();
println!("{:?}", fit_line(&log10(&h), &log10(&fwd))); // (intercept, slope)
println!("{:?}", fit_line(&log10(&h), &log10(&cen)));
let d = |h: f64| (od(t0 + h) - od(t0 - h)) / (2.0 * h); // a closure: a small inline function
let ratios = |h0: f64| -> Vec<f64> {
let dv: Vec<f64> = (0..6).map(|k| d(h0 / 2f64.powi(k))).collect();
let diff: Vec<f64> = dv.windows(2).map(|w| w[0] - w[1]).collect(); // D(h) - D(h/2), ...
diff.windows(2).map(|w| w[0] / w[1]).collect() // should be 2^p
};
println!("{:?}", ratios(0.1));
println!("{:?}", ratios(1e-8));
}
// Output:
// (-1.5380002333321863, 0.9971424076034727)
// (-2.1504996336281605, 2.0008963190521207)
// [3.999289441395284, 3.9998225111031513, 3.999955624191741, 3.999989521640287]
// [0.05555555555555555, 2.25, 0.5, -0.3333333333333333]Slopes \(1.00\) (forward) and \(2.00\) (central): first and second order. The intercept \(10^{-1.54} \approx 0.029\) is \(|f''|/2\). The ratios from \(h_0 = 0.1\) approach \(4 = 2^2\): second order, and these steps are in the truncation regime. From \(h_0 = 10^{-8}\) the ratios are noise (even negative): those steps are on the rounding side of the V for the central difference, whose best step is near \(\varepsilon^{1/3} \approx 6\times10^{-6}\) (B3c). The ratio test needs no exact answer, so you can run it on any method with a step size.
C3.
nstep <- 60
lo <- 1; hi <- 2; x <- 1
e_bis <- e_new <- numeric(nstep)
for (k in 1:nstep) {
mid <- (lo + hi) / 2
if (mid^2 > 2) hi <- mid else lo <- mid
e_bis[k] <- hi - lo # bracket width: a guaranteed error bound
x <- (x + 2 / x) / 2
e_new[k] <- abs(x - sqrt(2))
}
which(e_bis < 1e-12)[1] # first step below 1e-12
# [1] 40
which(e_new < 1e-12)[1]
# [1] 5
e_bis[2:6] / e_bis[1:5] # linear: e[k+1] / e[k]
# [1] 0.5 0.5 0.5 0.5 0.5
e_new[2:4] / e_new[1:3]^2 # quadratic: e[k+1] / e[k]^2
# [1] 0.3333333 0.3529412 0.3535224
-log10(e_new[1:5]) # correct digits, Newton
# [1] 1.066581 2.610284 5.672866 11.797314 15.653560nstep = 60
lo, hi, x = 1.0, 2.0, 1.0
e_bis = np.zeros(nstep); e_new = np.zeros(nstep)
for k in range(nstep):
mid = (lo + hi) / 2
if mid**2 > 2:
hi = mid
else:
lo = mid
e_bis[k] = hi - lo # bracket width: a guaranteed error bound
x = (x + 2 / x) / 2
e_new[k] = abs(x - np.sqrt(2))
print(np.argmax(e_bis < 1e-12) + 1) # first step below 1e-12 (counting from 1)
# 40
print(np.argmax(e_new < 1e-12) + 1)
# 5
print(e_bis[1:6] / e_bis[0:5]) # linear: e[k+1] / e[k]
# [0.5 0.5 0.5 0.5 0.5]
print(e_new[1:4] / e_new[0:3]**2) # quadratic: e[k+1] / e[k]^2
# [0.33333333 0.35294118 0.35352238]
print(-np.log10(e_new[0:5])) # correct digits, Newton
# [ 1.06658137 2.61028399 5.67286565 11.79731437 15.65355977]// rust/examples/convergence_cost_c3.rs — cargo run --example convergence_cost_c3
// C3: linear (bisection) vs quadratic (Newton) convergence to sqrt(2).
fn main() {
let nstep = 60;
let (mut lo, mut hi, mut x) = (1.0_f64, 2.0_f64, 1.0_f64); // `mut`: these change
let mut e_bis = vec![0.0; nstep];
let mut e_new = vec![0.0; nstep];
for k in 0..nstep {
let mid = (lo + hi) / 2.0;
if mid * mid > 2.0 { hi = mid } else { lo = mid }
e_bis[k] = hi - lo; // bracket width: a guaranteed error bound
x = (x + 2.0 / x) / 2.0;
e_new[k] = (x - 2f64.sqrt()).abs();
}
// first step below 1e-12 (counting from 1)
println!("{}", e_bis.iter().position(|&e| e < 1e-12).unwrap() + 1);
println!("{}", e_new.iter().position(|&e| e < 1e-12).unwrap() + 1);
let lin: Vec<f64> = (0..5).map(|k| e_bis[k + 1] / e_bis[k]).collect(); // linear: e[k+1] / e[k]
let quad: Vec<f64> = (0..3).map(|k| e_new[k + 1] / e_new[k].powi(2)).collect(); // quadratic: e[k+1] / e[k]^2
let digits: Vec<f64> = e_new[0..5].iter().map(|e| -e.log10()).collect(); // correct digits, Newton
println!("{lin:?}");
println!("{quad:?}");
println!("{digits:?}");
}
// Output:
// 40
// 5
// [0.5, 0.5, 0.5, 0.5, 0.5]
// [0.33333333333330084, 0.35294117643144196, 0.35352238456116425]
// [1.066581366339708, 2.6102839873991206, 5.672865645841041, 11.797314373736969, 15.653559774527022]Bisection needs 40 steps for \(10^{-12}\), Newton 5. Bisection’s ratio is exactly \(1/2\) (linear, \(0.3\) digits per step). Newton’s \(e_{k+1}/e_k^2\) settles near \(0.354 = 1/(2\sqrt2)\) (quadratic), and the correct digits go 1, 2.6, 5.7, 11.8, 15.7: roughly doubling, until machine precision stops them. Newton and secant methods explains where the constant comes from: it is \(|f''/(2f')|\) at the root.
C4. Your times will differ (machine, BLAS library, other programs running). These are example outputs from one Linux server.
set.seed(1)
cells <- 1000
genes <- c(500, 1000, 2000, 4000)
secs <- numeric(length(genes))
for (i in seq_along(genes)) {
X <- matrix(rpois(cells * genes[i], 2), nrow = cells) # cells x genes
secs[i] <- system.time(cor(X))["elapsed"]
}
data.frame(genes, secs, ratio = c(NA, secs[-1] / secs[-length(secs)]))
# genes secs ratio
# 1 500 0.167 NA
# 2 1000 0.654 3.916168
# 3 2000 2.594 3.966361
# 4 4000 10.946 4.219738
coef(lm(log10(secs) ~ log10(genes)))[2]
# log10(genes)
# 2.009105
secs[4] * (20000 / 4000)^2 * (100000 / cells) / 3600 # hours
# [1] 7.601389import time
rng = np.random.default_rng(1)
cells = 1000
genes = np.array([500, 1000, 2000, 4000])
secs = np.zeros(len(genes))
for i, g in enumerate(genes):
X = rng.poisson(2, size=(cells, g)).astype(float) # cells x genes
start = time.perf_counter()
np.corrcoef(X, rowvar=False)
secs[i] = time.perf_counter() - start
print(secs.round(3))
# [0.014 0.018 0.062 0.199]
print((secs[1:] / secs[:-1]).round(2))
# [1.28 3.39 3.22]
print(np.polyfit(np.log10(genes), np.log10(secs), 1)[0])
# 1.3163755080288322
print(secs[-1] * (20000 / genes[-1])**2 * (100000 / cells) / 3600) # hours
# 0.13850991462176251// rust/examples/convergence_cost_c4.rs — cargo run --release --example convergence_cost_c4
// C4: doubling experiment for the gene-gene correlation matrix.
// Run in release mode, or the timings mean nothing:
// cargo run -q --release --example convergence_cost_c4
// The correlation is a hand-written loop over all gene pairs (no BLAS, one core),
// so the n^2 scaling shows cleanly. Times depend on your machine; the
// random counts differ from R and Python.
use rand::{rngs::StdRng, SeedableRng};
use rand_distr::{Distribution, Poisson};
use std::hint::black_box;
use std::time::Instant;
// Correlation of the columns of a cells x genes matrix, stored as one Vec per gene.
fn cor(genes: &[Vec<f64>]) -> Vec<Vec<f64>> {
// centre each gene and scale it to length 1; then correlation = dot product
let z: Vec<Vec<f64>> = genes.iter().map(|g| {
let m = g.iter().sum::<f64>() / g.len() as f64;
let c: Vec<f64> = g.iter().map(|v| v - m).collect();
let norm = c.iter().map(|v| v * v).sum::<f64>().sqrt();
c.iter().map(|v| v / norm).collect()
}).collect();
let n = z.len();
let mut r = vec![vec![0.0; n]; n];
for i in 0..n {
for j in i..n { // each pair once: n(n+1)/2 dot products of length `cells`
let s: f64 = z[i].iter().zip(&z[j]).map(|(a, b)| a * b).sum();
r[i][j] = s;
r[j][i] = s;
}
}
r
}
fn main() {
let mut rng = StdRng::seed_from_u64(1);
let pois = Poisson::new(2.0).unwrap();
let cells = 1000;
let genes = [500, 1000, 2000, 4000];
let mut secs = vec![0.0; genes.len()];
for (i, &g) in genes.iter().enumerate() {
let x: Vec<Vec<f64>> = (0..g)
.map(|_| (0..cells).map(|_| pois.sample(&mut rng)).collect())
.collect(); // cells x genes, one Vec per gene
let start = Instant::now();
black_box(cor(black_box(&x)));
secs[i] = start.elapsed().as_secs_f64();
}
println!("{:?}", secs.iter().map(|s| (s * 1000.0).round() / 1000.0).collect::<Vec<_>>());
println!("{:?}", secs.windows(2).map(|w| (w[1] / w[0] * 100.0).round() / 100.0).collect::<Vec<_>>());
// slope of log10(secs) against log10(genes), by least squares
let lx: Vec<f64> = genes.iter().map(|&g| (g as f64).log10()).collect();
let ly: Vec<f64> = secs.iter().map(|s| s.log10()).collect();
let (mx, my) = (lx.iter().sum::<f64>() / 4.0, ly.iter().sum::<f64>() / 4.0);
let slope = lx.iter().zip(&ly).map(|(a, b)| (a - mx) * (b - my)).sum::<f64>()
/ lx.iter().map(|a| (a - mx).powi(2)).sum::<f64>();
println!("{slope}");
println!("{}", secs[3] * (20000.0 / 4000.0_f64).powi(2) * (100000.0 / cells as f64) / 3600.0); // hours
}
// Output:
// [0.156, 0.625, 2.756, 10.618]
// [4.01, 4.41, 3.85]
// 2.0411800531595934
// 7.373645167361112In R the ratios are close to 4 and the slope is \(2.01\): quadratic in genes, as predicted by the pair count. The extrapolation (quadratic in genes, linear in cells) gives about 7.6 hours for 20 000 genes × 100 000 cells. For 100 genes the same rule gives \(0.167 \times (100/500)^2 \times 100 \approx 0.7\) s.
In Python, np.corrcoef uses an optimised multi-threaded matrix product and is far faster, but the fitted slope (\(1.3\)) is too low: the smallest runs take about 10 ms, where overhead dominates, and more cores join in as \(n\) grows. Larger sizes push the slope towards 2; with genes = [1000, 2000, 4000, 8000] the same machine gave times [0.019 0.119 0.254 0.711] and slope \(1.68\). Trust only an exponent from runs long enough to measure, and use \(p = 2\) from the operation count for the extrapolation: about 8 minutes on this machine. R’s cor() is slow because it does not use the optimised library; a correlation via crossprod(scale(X)) was about 20 times faster here. A better constant, the same exponent.
The Rust version is a hand-written loop over gene pairs on a single core (no BLAS). It gave ratios near 4, slope \(2.04\) and about 7.4 hours for the full data set: close to R’s cor(). Its times, like all of these, depend on the machine.
C5.
set.seed(1)
n <- 2000
A <- matrix(runif(n * n), n); B <- matrix(runif(n * n), n); x <- runif(n)
system.time(y1 <- (A %*% B) %*% x)["elapsed"]
# elapsed
# 0.107
system.time(y2 <- A %*% (B %*% x))["elapsed"]
# elapsed
# 0.014
max(abs(y1 - y2)) / max(abs(y1))
# [1] 4.818009e-15import time
rng = np.random.default_rng(1)
n = 2000
A = rng.random((n, n)); B = rng.random((n, n)); x = rng.random(n)
start = time.perf_counter(); y1 = (A @ B) @ x; print(round(time.perf_counter() - start, 3))
# 0.081
start = time.perf_counter(); y2 = A @ (B @ x); print(round(time.perf_counter() - start, 3))
# 0.007
print(np.max(np.abs(y1 - y2)) / np.max(np.abs(y1)))
# 2.443153098537417e-15// rust/examples/convergence_cost_c5.rs — cargo run --release --example convergence_cost_c5
// C5: the order of operations, (AB)x against A(Bx).
// Run in release mode, or the timings mean nothing:
// cargo run -q --release --example convergence_cost_c5
// Matrix products with nalgebra (its own optimised kernel, one core, no BLAS).
// Times depend on your machine; the random numbers differ from R and Python.
use nalgebra::{DMatrix, DVector};
use rand::{rngs::StdRng, RngExt, SeedableRng};
use std::time::Instant;
fn main() {
let mut rng = StdRng::seed_from_u64(1);
let n = 2000;
let a = DMatrix::from_fn(n, n, |_, _| rng.random::<f64>());
let b = DMatrix::from_fn(n, n, |_, _| rng.random::<f64>());
let x = DVector::from_fn(n, |_, _| rng.random::<f64>());
let start = Instant::now();
let y1 = (&a * &b) * &x; // `&`: borrow the matrices, don't move them
println!("{:.3}", start.elapsed().as_secs_f64());
let start = Instant::now();
let y2 = &a * (&b * &x);
println!("{:.3}", start.elapsed().as_secs_f64());
println!("{:e}", (&y1 - &y2).amax() / y1.amax()); // amax: largest absolute value
}
// Output:
// 0.433
// 0.007
// 5.843762499194107e-15The results agree to rounding error (\(\sim 10^{-15}\) relative). \(A(B\mathbf x)\) is about 8–12 times faster in the example runs above, not the \(n/2 = 1000\) times the operation count predicts for \(n = 2000\). The reason: the matrix–matrix product is the most optimised routine in numerical computing (it reuses data in the cache and uses all cores), so it runs at many more operations per second than a matrix–vector product. Operation counts predict how the time grows, not the constant. With one thread (OPENBLAS_NUM_THREADS=1) the R gap on this machine grew to about 25×, and it grows with \(n\): at \(n = 20\,000\) the difference is hours against a second. Rust with nalgebra (single-threaded, no BLAS) showed about 60× between \((AB)\mathbf x\) and \(A(B\mathbf x)\): closer to the 1000× of the operation count, because no multithreaded BLAS speeds up the matrix–matrix product.
C6.
set.seed(1)
x <- runif(1e7); y <- runif(1e7)
dot_loop <- function(x, y) {
s <- 0
for (i in seq_along(x)) s <- s + x[i] * y[i]
s
}
system.time(dot_loop(x, y))["elapsed"]
# elapsed
# 0.619
system.time(sum(x * y))["elapsed"]
# elapsed
# 0.071
system.time(crossprod(x, y))["elapsed"]
# elapsed
# 0.032import time
rng = np.random.default_rng(1)
x = rng.random(10**7); y = rng.random(10**7)
def dot_loop(x, y):
s = 0.0
for i in range(len(x)):
s += x[i] * y[i]
return s
def elapsed(f): # seconds for one call of f()
start = time.perf_counter(); f(); return time.perf_counter() - start
print(round(elapsed(lambda: dot_loop(x, y)), 3))
# 3.035
print(round(elapsed(lambda: np.sum(x * y)), 3))
# 0.034
print(round(elapsed(lambda: x @ y), 3))
# 0.005// rust/examples/convergence_cost_c6.rs — cargo run --release --example convergence_cost_c6
// C6: dot product with a loop, with an iterator, and with a library call.
// Run in release mode, or the timings mean nothing:
// cargo run -q --release --example convergence_cost_c6
// Times depend on your machine; the random numbers differ from R and Python.
use nalgebra::DVector;
use rand::{rngs::StdRng, RngExt, SeedableRng};
use std::hint::black_box;
use std::time::Instant;
fn dot_loop(x: &[f64], y: &[f64]) -> f64 {
let mut s = 0.0;
for i in 0..x.len() {
s += x[i] * y[i];
}
s
}
// seconds for one call of f(); black_box keeps the optimiser from skipping it
fn elapsed(f: impl Fn() -> f64) -> f64 {
let start = Instant::now();
black_box(f());
start.elapsed().as_secs_f64()
}
fn main() {
let mut rng = StdRng::seed_from_u64(1);
let x: Vec<f64> = (0..10_000_000).map(|_| rng.random::<f64>()).collect();
let y: Vec<f64> = (0..10_000_000).map(|_| rng.random::<f64>()).collect();
let (xv, yv) = (DVector::from_vec(x.clone()), DVector::from_vec(y.clone()));
println!("{:.3}", elapsed(|| dot_loop(black_box(&x), black_box(&y))));
println!("{:.3}", elapsed(|| black_box(&x).iter().zip(&y).map(|(a, b)| a * b).sum()));
println!("{:.3}", elapsed(|| black_box(&xv).dot(&yv))); // nalgebra
}
// Output:
// 0.016
// 0.016
// 0.014All three are \(O(n)\): double \(n\) and each time doubles. The constants differ: in R the loop was about 9–20 times slower than the vectorised versions; in Python about 90–600 times. In compiled Rust the loop was as fast as the iterator and nalgebra versions (all about 0.015 s): vectorising buys nothing there; the slow loop is a property of interpreted R and Python. Vectorising is worth a large constant factor, especially in Python, but it never changes the exponent: an \(O(n^2)\) all-pairs computation stays \(O(n^2)\) when vectorised.
C7.
X <- matrix(0, nrow = 1000, ncol = 1000)
object.size(X)
# 8000216 bytes
c(20000 * 100000 * 8 / 1e9, 20000^2 * 8 / 1e9) # GB
# [1] 16.0 3.2
library(Matrix) # ships with R
set.seed(1)
S <- rsparsematrix(2000, 10000, density = 0.1,
rand.x = function(k) rpois(k, 2) + 1) # genes x cells
D <- as.matrix(S)
c(dense = object.size(D), sparse = object.size(S)) / 1e6 # MB
# dense sparse
# 160.0002 24.0415
Sc <- S - rowMeans(S) # centre each gene
class(Sc)
# [1] "dgeMatrix"
# attr(,"package")
# [1] "Matrix"
c(centred = object.size(Sc)) / 1e6
# centred
# 160.0012import scipy.sparse as sp
X = np.zeros((1000, 1000))
print(X.nbytes)
# 8000000
print(20000 * 100000 * 8 / 1e9, 20000**2 * 8 / 1e9) # GB
# 16.0 3.2
rng = np.random.default_rng(1)
S = sp.random(2000, 10000, density=0.1, format="csc", rng=rng,
data_rvs=lambda k: rng.poisson(2, k) + 1.0) # genes x cells
D = S.toarray()
sparse_bytes = S.data.nbytes + S.indices.nbytes + S.indptr.nbytes
print(D.nbytes / 1e6, sparse_bytes / 1e6) # MB
# 160.0 24.040004
Sc = S - S.mean(axis=1) # centre each gene
print(type(Sc).__name__, Sc.nbytes / 1e6)
# matrix 160.0// rust/examples/convergence_cost_c7.rs — cargo run --example convergence_cost_c7
// C7: memory of dense and sparse matrices.
// Sparse storage with sprs (compressed sparse column, 4-byte u32 indices as in R/scipy).
// The random matrix differs from R and Python, but the sizes do not.
use rand::{rngs::StdRng, seq::index, SeedableRng};
use rand_distr::{Distribution, Poisson};
use sprs::CsMatI;
use std::mem::size_of;
fn main() {
let x = vec![0.0_f64; 1000 * 1000]; // a 1000 x 1000 matrix of zeros, stored as one Vec
println!("{}", x.len() * size_of::<f64>());
println!("{} {}", 20000.0 * 100000.0 * 8.0 / 1e9, 20000.0_f64.powi(2) * 8.0 / 1e9); // GB
// genes x cells, 10 % non-zeros: in each cell, 200 random genes get count Poisson(2) + 1
let (genes, cells, per_cell) = (2000, 10000, 200);
let mut rng = StdRng::seed_from_u64(1);
let pois = Poisson::new(2.0).unwrap();
let mut indptr = vec![0u32];
let (mut indices, mut data) = (Vec::new(), Vec::new());
for _ in 0..cells {
let mut rows = index::sample(&mut rng, genes, per_cell).into_vec();
rows.sort(); // CSC wants sorted row indices within a column
for r in rows {
indices.push(r as u32);
data.push(pois.sample(&mut rng) + 1.0);
}
indptr.push(indices.len() as u32);
}
let s = CsMatI::new_csc((genes, cells), indptr, indices, data);
let sparse_bytes = s.data().len() * size_of::<f64>()
+ s.indices().len() * size_of::<u32>()
+ s.indptr().raw_storage().len() * size_of::<u32>();
let dense_bytes = genes * cells * size_of::<f64>(); // what s.to_dense() would take
println!("{} {}", dense_bytes as f64 / 1e6, sparse_bytes as f64 / 1e6); // MB
// centre each gene: every zero becomes -mean, so the result must be dense
let mut row_sum = vec![0.0; genes];
for (&v, (r, _c)) in s.iter() {
row_sum[r as usize] += v; // u32 index -> usize to index a Vec
}
let mut sc = vec![0.0_f64; genes * cells]; // dense, column-major
for c in 0..cells {
for r in 0..genes {
sc[c * genes + r] = -row_sum[r] / cells as f64;
}
}
for (&v, (r, c)) in s.iter() {
sc[c as usize * genes + r as usize] += v;
}
println!("{}", sc.len() * size_of::<f64>() / 1_000_000); // MB
}
// Output:
// 8000000
// 16 3.2
// 160 24.040004
// 160- \(10^6\) entries × 8 bytes = 8 MB (R adds a 216-byte header). Full size: 16 GB dense, 3.2 GB for the correlation matrix.
- \(2\times10^6\) non-zeros × 12 bytes = 24 MB sparse, against 160 MB dense: 6.7 times smaller.
- After centring, the result is an ordinary dense matrix (R:
dgeMatrix, Python:numpy.matrix, Rust: a plainVec<f64>) of 160 MB. Every zero became \(-\text{mean}\). At full size that is 2.4 GB → 16 GB, from one line of code. Checkclass()/type()and the memory after each step of a pipeline.
Where this leads
- Numerical differentiation (Part 7): forward, backward and central differences, the optimal step, Richardson extrapolation, and what to do with noisy measured OD data, where the “rounding” error is measurement noise and is far larger than \(\varepsilon\).
- Solving nonlinear equations (Part 2, Bisection and fixed-point iteration and Newton and secant methods): why bisection converges linearly and Newton quadratically, and how to stop an iteration.
- Linear systems (Part 3): the \(O(n^3)\) cost of elimination, factor once and solve many times (Gaussian elimination as LU), and sparse storage and solvers for count matrices (Structure: symmetric, banded, sparse).
- Large eigenvalue problems (Part 5, How
eigen()works, and big problems): PCA of 20 000 cells × 2 000 genes without forming the centred or the covariance matrix.