Mathematical Foundations

VIII.8 · Numerical Computing

When does storing fewer entries actually help?

Edit a small matrix, inspect exact zero structure and compare dense storage with canonical COO and CSR. Follow sparse multiplication through its real column indices, products and row accumulators.

Edit dense structure and inspect COO and CSR

3×3, 3 structural nonzeros after exactly combining 0 duplicate coordinates and removing exact zeros. 0 nonzeros are small under the chosen diagnostic; they remain stored. CSR uses zero-based columns and repeated pointers for empty rows. Arithmetic here is exact rational reference arithmetic; byte counts model packed Float64 values and int32 indices, not the memory used by these rational objects.

Canonical structural nonzero count
3
Duplicate coordinates in raw input
0
Explicit raw zero entries
0
Small nonzeros retained
0
Exact matrix-vector product
(2, 6, 12)
Ideal dense packed bytes
72
Ideal canonical COO packed bytes
48
Ideal CSR packed bytes
52

= exact rationals or classifications · ≈ a formatted numerical display. Exceptional values retain their stated meaning.

(Ax)i=∑j=0n−1Aijxj(Ax)_i=\sum_{j=0}^{n-1}A_{ij}x_j
Bd=8mn,BCOO=16 nnz,BCSR=12 nnz+4(m+1)B_d=8mn,\quad B_{\mathrm{COO}}=16\,\mathrm{nnz},\quad B_{\mathrm{CSR}}=12\,\mathrm{nnz}+4(m+1)
Raw COO retains duplicate and explicit-zero entries; canonical COO sums duplicates and removes exact zeros.
recordrowcolumnexact value
raw 0002
raw 1113
raw 2224
canonical 0002
canonical 1113
canonical 2224
CSR arrays: indptr has rows+1 entries, with an exclusive end pointer.
arrayvalues
indptr0, 1, 2, 3
column indices0, 1, 2
data2, 3, 4
Only structural nonzeros contribute multiplications; all values use zero-based indices.
rowcolumnA[row,col]x[col]product
00212
11326
224312

Read raw COO entries

Zero-based coordinates are validated against shape. Explicit zeros and duplicate coordinates are retained in the input record.

Inspect the recorded values\text{Inspect the recorded values}
Actual intermediate values; = exact rationals, ≈ binary64 displays.
quantityvalue
shape3×3
rawEntries3
duplicates0
explicitZeros0

Zero-based indices; at most 18 triples and 1200 characters. Values are exact finite decimals within ±20. Empty text means an all-zero matrix.

The vector length must equal the column count; finite decimals within ±20.

Exact finite decimal in [0,1]. Structural zero remains an exact equality.

from 1 to 3, whole numbers only

from 1 to 3, whole numbers only

Toggle matrix entries or edit raw COO triples. Exact duplicate aggregation, CSR indices and actual sparse multiplication produce every displayed value.

Intuition: zeros can carry structure

A diagonal matrix has many zeros, yet only its diagonal values contribute products to a matrix-vector multiplication. A sparse representation records where the contributing values are rather than reserving a value slot for every coordinate. This can save space and arithmetic work when zeros are common.

The grid shows the aggregated dense matrix, with explicit zero-based [row,column] labels. Click or tap a cell to toggle its aggregate between structural zero and one. To enter another exact value, edit the COO triples. The same canonical state controls the grid, storage arrays, inspector and multiplication trace; there is no separate decorative matrix.

Sparse overhead matters. A nearly full 3×3 matrix can consume more ideal packed storage in COO or CSR than in dense form. Those representations also have different access patterns. Fewer stored values is not a universal wall-clock speed guarantee.

Definition: bounded dense, COO and CSR data

This explorer uses m rows and n columns, each from one through three. A dense representation has mn value slots. COO, coordinate storage, records triples (row,column,value). CSR, compressed sparse row storage, uses a value array, a parallel column-index array and a row-pointer array of length m+1.

For row i, CSR entries run from indptr[i] up to, but excluding, indptr[i+1]. Equal consecutive pointers describe an empty row. Indices are zero-based in these programs and views; in one-based mathematical notation they would be shifted by one.

Raw COO may contain duplicates and explicit zeros. This chapter’s canonical policy sums duplicates exactly, sorts by row then column, and removes only exact aggregated zeros. A near-zero diagnostic never alters the structure. Shape, coordinates, finite values and vector length are validated before deriving storage or products.

Mechanics: one matrix, two actual multiplication paths

The canonical state holds the declared shape, raw COO text, vector and diagnostic threshold. The model derives the dense exact rational matrix and canonical COO/CSR arrays. It computes the sparse result by actually traversing CSR nonzeros, and independently computes a dense result through the accepted exact matrix API.

Each sparse product record contains its row, column, coefficient, x[column], product, accumulator before and accumulator after. The column index selects a vector component; the row pointer selects where the output sum belongs. Swapping those meanings would give the wrong operation even if a storage diagram looked plausible.

For a grid toggle, all raw duplicates at the selected coordinate are replaced together. Removing a currently nonzero aggregate removes that coordinate; toggling an exact aggregate zero adds one. Other raw entries retain their exact decimal values. Empty COO text explicitly represents an all-zero matrix.

Derivation: sparse multiplication and ideal packed costs

The mathematical product is

(Ax)i=∑j=0n−1Aijxj.(Ax)_i=\sum_{j=0}^{n-1}A_{ij}x_j.

For a canonical sparse matrix, exact zero terms contribute nothing. CSR evaluates only the stored coefficients between the two row pointers, accumulating each result into that row. Its coefficient multiplications number nnz rather than mn; this counts a specific arithmetic operation, not all indexing, allocation or traversal overhead.

Assume numerical values occupy eight bytes and indices occupy four bytes. Ignoring container, alignment and shape metadata,

Bd=8mn,BCOO=16nnz⁡,B_d=8mn,\qquad B_{\mathrm{COO}}=16\operatorname{nnz}, BCSR=12nnz⁡+4(m+1).B_{\mathrm{CSR}}=12\operatorname{nnz}+4(m+1).

These are ideal Float64/int32 packed-array costs. The explorer computes exact rational references; its BigInt objects do not occupy eight bytes each. Converting those values to actual numerical storage can introduce the representation effects from earlier chapters.

Worked examples: a diagonal and an empty row

For A=diag(2,3,4) and x=(1,2,3), the exact output is (2,6,12). Canonical COO contains (0,0,2), (1,1,3), (2,2,4). CSR data=[2,3,4], column indices=[0,1,2], and indptr=[0,1,2,3]. There are three stored coefficient products rather than nine dense coefficient products.

The ideal costs are 72 dense bytes, 48 COO bytes and 52 CSR bytes. CSR pays four row pointers; that overhead is significant for a tiny matrix. A full 3×3 matrix with nine nonzeros instead costs 144 COO bytes or 124 CSR bytes, compared with 72 dense bytes.

Remove the middle diagonal entry. The middle output becomes zero and CSR indptr becomes [0,1,1,2]. The repeated one correctly describes row 1 as empty. It does not mean column 1 is absent from every other row.

The duplicate-cancellation preset has raw (0,0,2), (0,0,−2), (1,2,3) and an explicit zero. The first pair sums to exact zero. Only coordinate (1,2) survives, giving result (0,9,0) and row pointers [0,0,1,1].

Worked example: Edit dense structure and inspect COO and CSR

3×3, 3 structural nonzeros after exactly combining 0 duplicate coordinates and removing exact zeros. 0 nonzeros are small under the chosen diagnostic; they remain stored. CSR uses zero-based columns and repeated pointers for empty rows. Arithmetic here is exact rational reference arithmetic; byte counts model packed Float64 values and int32 indices, not the memory used by these rational objects.

Canonical structural nonzero count
3
Duplicate coordinates in raw input
0
Explicit raw zero entries
0
Small nonzeros retained
0
Exact matrix-vector product
(2, 6, 12)
Ideal dense packed bytes
72
Ideal canonical COO packed bytes
48
Ideal CSR packed bytes
52

= exact rationals or classifications · ≈ a formatted numerical display. Exceptional values retain their stated meaning.

(Ax)i=∑j=0n−1Aijxj(Ax)_i=\sum_{j=0}^{n-1}A_{ij}x_j
Bd=8mn,BCOO=16 nnz,BCSR=12 nnz+4(m+1)B_d=8mn,\quad B_{\mathrm{COO}}=16\,\mathrm{nnz},\quad B_{\mathrm{CSR}}=12\,\mathrm{nnz}+4(m+1)
Raw COO retains duplicate and explicit-zero entries; canonical COO sums duplicates and removes exact zeros.
recordrowcolumnexact value
raw 0002
raw 1113
raw 2224
canonical 0002
canonical 1113
canonical 2224
CSR arrays: indptr has rows+1 entries, with an exclusive end pointer.
arrayvalues
indptr0, 1, 2, 3
column indices0, 1, 2
data2, 3, 4
Only structural nonzeros contribute multiplications; all values use zero-based indices.
rowcolumnA[row,col]x[col]product
00212
11326
224312

Read raw COO entries

Zero-based coordinates are validated against shape. Explicit zeros and duplicate coordinates are retained in the input record.

Inspect the recorded values\text{Inspect the recorded values}
Actual intermediate values; = exact rationals, ≈ binary64 displays.
quantityvalue
shape3×3
rawEntries3
duplicates0
explicitZeros0

Select the recorded intermediate values. Previous, next and direct replay navigation preserve immutable numerical history.

Executable reference code

The pedagogical program aggregates raw coordinates with a Fraction dictionary, constructs CSR, and evaluates products through its pointers. The NumPy version builds an actual dense array with additive coordinate insertion and independently evaluates the product. No SciPy or compiled sparse backend is needed. Compare the indices, storage costs and matrix-vector products in the sparse and dense representations.

code/python/sparse_matrices.py
"""Bounded COO aggregation and actual CSR matvec; exact reference values use Fraction."""
from fractions import Fraction as F
def sparse(rows,cols,entries,vector):
    assert 1<=rows<=3 and 1<=cols<=3 and len(vector)==cols and len(entries)<=18
    coordinates={}
    for row,col,value in entries:
        assert 0<=row<rows and 0<=col<cols
        coordinates[row,col]=coordinates.get((row,col),F(0))+F(str(value))
    canonical=[(row,col,value) for (row,col),value in sorted(coordinates.items()) if value]
    data=[];indices=[];indptr=[0]
    for row in range(rows):
        for r,col,value in canonical:
            if r==row:data.append(value);indices.append(col)
        indptr.append(len(data))
    result=[]
    for row in range(rows):
        accumulator=F(0)
        for position in range(indptr[row],indptr[row+1]):
            term=data[position]*F(str(vector[indices[position]]))
            accumulator+=term
            print('row',row,'column',indices[position],'product',term,'accumulator',accumulator)
        result.append(accumulator)
    return canonical,data,indices,indptr,result
if __name__=='__main__':
    c,data,indices,ptr,result=sparse(3,3,[(0,0,'2'),(0,0,'-2'),(1,2,'3'),(2,2,'0')],[1,2,3])
    assert c==[(1,2,F(3))] and ptr==[0,0,1,1] and result==[0,9,0]
    assert sparse(3,3,[],[1,2,3])[-1]==[0,0,0]
    assert 8*3*3==72 and 16*3==48 and 12*3+4*4==52
    print('CSR',data,indices,ptr,'result',result)
code/python/sparse_matrices_numpy.py
"""NumPy dense cross-check of small COO/CSR; no SciPy backend or speed claim."""
import numpy as np
from sparse_matrices import sparse
if __name__=='__main__':
    entries=[(0,0,2),(0,0,-2),(1,2,3),(2,2,0)]
    a=np.zeros((3,3),dtype=np.float64)
    for row,col,value in entries:a[row,col]+=value
    x=np.array([1.,2.,3.]);answer=a@x
    exact=sparse(3,3,entries,[1,2,3])[-1]
    np.testing.assert_array_equal(answer,np.array(list(map(float,exact))))
    assert np.count_nonzero(a)==1 and np.all(a[0]==0) and np.all(a[2]==0)
    print('dense',a,'matvec',answer,'packed dense bytes',a.nbytes)
    # Exact-reference BigInt/Fraction objects and Python containers have separate memory costs.

Explore: structure is an equality, not a tolerance

Select small but nonzero: 1e-8 lies below the default diagnostic threshold 1e-6, yet its coordinate stays in canonical storage and contributes to the exact product. Increasing the diagnostic threshold can change the near-zero count without changing nnz, CSR pointers or the mathematical product.

Compare diagonal, empty middle row, duplicate cancellation, full and all-zero examples. Predict the pointers and byte counts before reading them. An all-zero matrix still needs m+1 CSR pointers in this cost model.

Edit a coordinate outside the declared shape or supply a vector of the wrong length. The state is rejected with a message rather than silently resizing or truncating. To change a dimension, first make the entries and vector compatible with the new shape; the explorer does not discard data to repair an edit.

Common mistakes and counterexamples

A duplicate coordinate does not necessarily overwrite a previous one. This chapter explicitly sums duplicates; another API must state its own policy. An explicit stored zero can consume space even though it contributes no mathematical product; canonicalization removes it here.

A value close to zero is not a structural zero. Thresholding it would change the matrix and its product, and should be a separate, justified approximation. CSR pointers are exclusive row boundaries, while column indices select input components. Confusing those two arrays breaks multiplication.

Numerical considerations

The small teaching model uses exact rational intended entries and products. It is suited to checking coordinate meaning and zero structure without accidental cancellation in a numerical accumulator. Actual Float64 sparse arrays would additionally require conversion and accumulation error analysis.

Storage costs explicitly describe hypothetical packed numerical arrays. They exclude exact-reference object memory, raw duplicate storage, allocator overhead and runtime bookkeeping. Sparse access can also have worse locality or indexing costs than a dense operation. Operations saved do not establish a performance improvement on an unspecified workload.

Engineering and ML connections

Sparse structure appears in graph adjacency, discretized differential equations, sparse features and selected model architectures. A format choice should follow the pattern of nonzeros, repeated operations and access needs. Preserve shape and index conventions across data loading and multiplication; numerical thresholding must be recorded as a changed mathematical object.

Inspect the corresponding representation and error policy before interpreting a computed value.

Exercises

Predict first, then calculate or test the stated claim. Every answer includes corrective feedback and a worked solution.

calculation

A diagonal product

For diag(2,3,4) times (1,2,3), give the three output components.

calculation

Duplicate cancellation

Raw COO has (0,1,2) and (0,1,−2). Give the aggregate value and canonical nonzero count.

calculation

Packed byte costs

A 3×3 matrix has three canonical nonzeros. Values are eight bytes and indices four. Give dense, COO and CSR bytes.

explanation

Small versus zero

A coefficient 1e-8 is below diagnostic threshold 1e-6. What does this explorer do?

Choose one answer

explanation

Empty CSR row

What does indptr=[0,1,1,2] say about row 1?

Choose one answer

Summary and checkpoints

Construct COO and CSR from a small dense matrix, identify an empty row from repeated pointers, combine duplicates under the stated exact policy and replay real sparse products. Explain the packed byte assumptions and why a small nonzero or fewer multiplications does not imply a free accuracy or speed guarantee.

The map of the book

97 of 97 chapters written · eight Parts

demo Foundation demo Try the live vector example Open as a page

Part I

Mathematical Language

The notation and mental objects everything else uses.

9 of 9 chapters written

  1. I.1 Scalars, quantities, units, powers and roots available
  2. I.2 Coordinates and points available
  3. I.3 Vectors: components, magnitude, and direction reference chapter available
  4. I.4 Algebraic expressions, equations and inequalities available
  5. I.5 Functions as input → transformation → output available
  6. I.6 Essential function families available
  7. I.7 Sets, intervals, and relations available
  8. I.8 Indexed quantities and summation available
  9. I.9 The notation map for ML available

Part II

Linear Algebra

Matrices as transformations: the strongest foundation in the book.

19 of 19 chapters written

  1. II.1 Vector addition and subtraction available
  2. II.2 Scalar multiplication and vector scaling available
  3. II.3 Norms, magnitude, and normalization available
  4. II.4 Dot product, angle, and orthogonality available
  5. II.5 Projection and distance to a direction available
  6. II.6 Linear combinations and span available
  7. II.7 Linear independence, basis, and coordinates available
  8. II.8 Matrices as linear transformations available
  9. II.9 Matrix × vector available
  10. II.10 Matrix × matrix as composition available
  11. II.11 Transpose and orthogonality available
  12. II.12 Determinant: orientation and area/volume scaling available
  13. II.13 Linear systems as intersecting constraints available
  14. II.14 Gaussian elimination and row operations available
  15. II.15 Rank and invertibility available
  16. II.16 Inverse matrices available
  17. II.17 Eigenvalues and eigenvectors available
  18. II.18 Orthogonal bases and least-squares geometry available
  19. II.19 Singular Value Decomposition available

Part III

Calculus

Change, local approximation, and sensitivity made visible.

15 of 15 chapters written

  1. III.1 Limits through approach available
  2. III.2 Continuity and local behavior available
  3. III.3 Derivative as secant → tangent available
  4. III.4 Derivative as rate of change and local linear model available
  5. III.5 Derivative rules available
  6. III.6 Chain rule available
  7. III.7 Multivariable functions and partial derivatives available
  8. III.8 Gradient and directional derivatives available
  9. III.9 Jacobian available
  10. III.10 Computational graphs and automatic differentiation available
  11. III.11 Hessian and curvature available
  12. III.12 Integrals as accumulation available
  13. III.13 Fundamental derivative/integral connection available
  14. III.14 Numerical differentiation available
  15. III.15 Numerical integration available

Part IV

Probability

Uncertainty through sample spaces, evidence, and repetition.

16 of 16 chapters written

  1. IV.1 Random experiments, outcomes, and sample spaces available
  2. IV.2 Events and probability mass available
  3. IV.3 Counting and finite probability where needed available
  4. IV.4 Conditional probability as restricted sample space available
  5. IV.5 Independence available
  6. IV.6 Bayes' theorem as probability-mass update available
  7. IV.7 Random variables available
  8. IV.8 PMF, PDF, and CDF available
  9. IV.9 Bernoulli and Binomial distributions available
  10. IV.10 Uniform and Gaussian distributions available
  11. IV.11 Expectation available
  12. IV.12 Variance and standard deviation available
  13. IV.13 Joint, marginal, and conditional distributions available
  14. IV.14 Covariance and correlation available
  15. IV.15 Law of Large Numbers available
  16. IV.16 Central Limit Theorem available

Part V

Statistics

From observed data to estimates, intervals, and likelihood.

10 of 10 chapters written

  1. V.1 Data summaries: mean, median, mode available
  2. V.2 Spread: variance, standard deviation, robust interpretation available
  3. V.3 Population, sample, and sampling variability available
  4. V.4 Estimators and estimation available
  5. V.5 Confidence intervals through repeated sampling available
  6. V.6 Covariance matrices and multivariate spread available
  7. V.7 Regression fundamentals available
  8. V.8 Likelihood available
  9. V.9 Maximum Likelihood Estimation available
  10. V.10 Maximum A Posteriori estimation available

Part VI

Optimization

Learning as repeated parameter updates governed by an objective.

10 of 10 chapters written

  1. VI.1 Objective functions and optimization landscapes available
  2. VI.2 Local vs global optima and stationary points available
  3. VI.3 Convexity available
  4. VI.4 Gradient descent flagship chapter available
  5. VI.5 Learning rate and convergence behavior available
  6. VI.6 Momentum available
  7. VI.7 Stochastic and mini-batch gradient descent available
  8. VI.8 Adam intuition available
  9. VI.9 Constraints and projected/penalized intuition available
  10. VI.10 Numerical stopping and optimization diagnostics available

Part VII

Information Theory

Logarithms, entropy, and the loss behind token prediction.

8 of 8 chapters written

  1. VII.1 Logarithms and log probabilities in computation available
  2. VII.2 Self-information available
  3. VII.3 Entropy available
  4. VII.4 Cross entropy and negative log-likelihood available
  5. VII.5 KL divergence available
  6. VII.6 Logits and softmax available
  7. VII.7 Numerical stability: log-sum-exp and stable softmax available
  8. VII.8 From token logits to language-model loss available

Part VIII

Numerical Computing

Why equivalent expressions behave differently on real machines.

10 of 10 chapters written

  1. VIII.1 Floating-point representation available
  2. VIII.2 Rounding and representation error available
  3. VIII.3 Error propagation and conditioning available
  4. VIII.4 Catastrophic cancellation available
  5. VIII.5 Overflow and underflow available
  6. VIII.6 Stable algorithms available
  7. VIII.7 Numerical linear algebra realities available
  8. VIII.8 Sparse matrices and sparse structure available
  9. VIII.9 Vectorized computation available
  10. VIII.10 Reproducibility, random seeds, and nondeterminism available