Everything on the site.

Big-O notationAlgorithms · Count the steps in real loops, then compare n, n log n, n² and 2ⁿ on a live chart.RecursionAlgorithms · Watch frames push and pop on the call stack for factorial and Fibonacci.BFS vs DFSAlgorithms · A queue against a stack: draw a graph and see which order each search visits it in.Sorting algorithmsAlgorithms · Seven sorts on the same input, with comparison, swap and write counts side by side.Binary searchAlgorithms · Halve the range, probe the middle, repeat: with lo, mid and hi drawn on the array.Sets, relations and functionsDiscrete maths · Shade any set expression on a Venn diagram and test a relation for its properties.Proof by inductionDiscrete maths · Four worked proofs revealed one step at a time, and a broken one to diagnose.CombinatoricsDiscrete maths · An exact nPr and nCr calculator, and a Pascal’s triangle that shows its own addition rule.Boolean algebraDiscrete maths · The laws, De Morgan, and normal forms, with a truth-table generator built in.Finite-state machinesTheory · Build a DFA in a table, see it drawn, and run strings through it one symbol at a time.Domain theoryTheory · Why recursion needs least fixed points, and how CPOs, Scott continuity and powerdomains supply them.Truth table generatorTools · Expression in, truth table out: with DNF, CNF, a minimal form and LaTeX.Graph traversal visualiserTools · Draw a graph, then animate BFS, DFS and Dijkstra with the frontier shown.Recursion tree visualiserTools · Paste a recursive function or a recurrence and see its call tree and call count.Gradient descentML maths · Drag the starting point, set the learning rate, and watch it converge or blow up.Vectors and embeddingsML maths · Bend the plane with a 2×2 matrix and measure how close toy word vectors are.Softmax and cross-entropyML maths · Edit the logits and watch the probabilities, the loss and the gradient update live.AttentionML maths · Q, K and V for three tokens, a live score heatmap, and the weighted sum of values.Formatting a CS paper in LaTeXLaTeX · Page limits, \documentclass, front matter, theorem environments and the bibliography.Camera-ready checklistLaTeX · Fonts embedded, PDF/A, copyright form, source bundle, and why final versions get rejected.LaTeX templatesLaTeX · Where the official classes live, plus a minimal article template you can copy.AlgorithmsSection · Step-through visualisations of sorting, searching, recursion, graph traversal and Big-O growth, each with pseudocode that highlights the line being run.Discrete mathsSection · Sets, relations and functions, proof by induction, counting, and Boolean algebra, explained with worked examples and small interactive checkers.TheorySection · Finite-state machines, automata and the mathematical semantics of programming languages, explained from first principles.ML mathsSection · Gradient descent, vectors and embeddings, softmax and cross-entropy, and attention, each with a formula, a worked numeric example and a live version.ToolsSection · A truth-table generator with CNF, DNF and LaTeX output, a graph-traversal visualiser for BFS, DFS and Dijkstra, and a recursion-tree visualiser.LaTeXSection · How to format a CS conference or workshop paper in LaTeX, a camera-ready checklist, and a minimal template you can adapt.
ML maths

Vectors, matrices and embeddings

Machine-learning models represent words, images and users as lists of numbers called vectors, and transform them with matrices. This page covers the handful of linear-algebra ideas you need: dot products, matrices as transformations, and cosine similarity between embeddings.

Interactive: 2D matrix-transform playground and cosine-similarity explorerUpdated

Vectors

A vector is an ordered list of numbers, v=(v1,v2,…,vn)\mathbf{v} = (v_1, v_2, \dots, v_n). In two or three dimensions you can draw it as an arrow from the origin; in machine learning it typically has hundreds or thousands of entries, and the arrow picture still guides intuition. Vectors add entry by entry, and multiplying by a number scales every entry:

(1,2)+(3,−1)=(4,1),2 (1,2)=(2,4).(1, 2) + (3, -1) = (4, 1), \qquad 2\,(1, 2) = (2, 4).

The length (Euclidean norm) is ∥v∥=v12+⋯+vn2\lVert \mathbf{v} \rVert = \sqrt{v_1^2 + \dots + v_n^2}, Pythagoras in nn dimensions.

The dot product

The dot product multiplies matching entries and adds them up:

a⋅b=a1b1+a2b2+⋯+anbn.\mathbf{a} \cdot \mathbf{b} = a_1 b_1 + a_2 b_2 + \dots + a_n b_n.

It has a geometric meaning that makes it the workhorse of machine learning:

a⋅b=∥a∥ ∥b∥cos⁡θ,\mathbf{a} \cdot \mathbf{b} = \lVert \mathbf{a} \rVert \, \lVert \mathbf{b} \rVert \cos\theta,

where θ\theta is the angle between the vectors. Large and positive means they point the same way; zero means they are perpendicular; negative means they point apart. A single neuron computes a dot product between its weights and its input, and attention scores are dot products between queries and keys.

Matrices are transformations

A matrix multiplied by a vector gives a new vector. For a 2×22 \times 2 matrix,

(abcd)(xy)=(ax+bycx+dy).\begin{pmatrix} a & b \\ c & d \end{pmatrix} \begin{pmatrix} x \\ y \end{pmatrix} = \begin{pmatrix} ax + by \\ cx + dy \end{pmatrix}.

The best way to understand this is to ask where the basis vectors go. (1,0)(1, 0) lands on (a,c)(a, c), the first column, and (0,1)(0, 1) lands on (b,d)(b, d), the second column. Every other vector (x,y)=x(1,0)+y(0,1)(x, y) = x(1,0) + y(0,1) lands on the same combination of the columns. So a matrix is completely described by where it sends the basis vectors, and the grid of the plane is carried along with them: lines stay straight and parallel, and the origin stays fixed. That is what “linear” means.

Worked example

Let A=(2113)A = \begin{pmatrix} 2 & 1 \\ 1 & 3 \end{pmatrix} and v=(1,1)\mathbf{v} = (1, 1). Then Av=(2⋅1+1⋅1,  1⋅1+3⋅1)=(3,4)A\mathbf{v} = (2 \cdot 1 + 1 \cdot 1,\; 1 \cdot 1 + 3 \cdot 1) = (3, 4). The determinant det⁡A=2⋅3−1⋅1=5\det A = 2 \cdot 3 - 1 \cdot 1 = 5 says that AA multiplies every area by 5: the unit square becomes a parallelogram of area 5. Choose “Example” in the playground and drag v\mathbf{v} to (1,1)(1, 1) to check.

2 × 2 matrix playground

A =

Drag the purple point to move the vector v. The red and blue arrows are where the basis vectors î = (1, 0) and ĵ = (0, 1) land: they are the columns of A.

Some things to try: a rotation has determinant 1 (areas are preserved); a reflection has a negative determinant (orientation flips, so the grid is mirrored); and “Project onto x” has determinant 0, squashing the whole plane onto a line. A zero determinant means information is lost, so the transformation has no inverse. Applying one matrix after another corresponds to multiplying them, B(Av)=(BA)vB(A\mathbf{v}) = (BA)\mathbf{v}, which is why a stack of linear layers with no nonlinearity in between is no more powerful than a single layer.

Embeddings

An embedding maps each item, such as a word, a product or a user, to a vector, chosen so that geometry reflects meaning: similar items get nearby vectors. The vectors are not designed by hand. They are parameters learned by gradient descent, typically while training a model to predict context, such as which words appear near which. Word2vec (2013) popularised the idea with word vectors of a few hundred dimensions; modern language models learn embeddings of every token as their first layer.

Cosine similarity

To compare embeddings we usually ignore length and look only at direction:

cos⁡θ=a⋅b∥a∥ ∥b∥.\cos\theta = \frac{\mathbf{a} \cdot \mathbf{b}}{\lVert \mathbf{a} \rVert \, \lVert \mathbf{b} \rVert}.

It ranges from −1-1 (opposite) through 0 (unrelated) to 1 (same direction). Length is set aside because in many embedding models it tracks things like how frequent a word is, rather than what it means.

Worked example

For a=(3,4)\mathbf{a} = (3, 4) and b=(4,3)\mathbf{b} = (4, 3): a⋅b=12+12=24\mathbf{a} \cdot \mathbf{b} = 12 + 12 = 24, and both lengths are 9+16=5\sqrt{9 + 16} = 5. So cos⁡θ=24/25=0.96\cos\theta = 24 / 25 = 0.96, an angle of about 16°. Nearly the same direction.

Cosine similarity on toy word vectors

These 2D vectors are made up for teaching: the horizontal axis loosely means “alive ↔ machine”, the vertical one “small ↔ large”. Real embeddings have hundreds of dimensions learned from data.

Nearest to word a

    Vector arithmetic

    −+

    In the toy space, cat and kitten have cosine similarity 0.946, while cat and truck have −0.910. The “vector arithmetic” box shows the famous analogy trick: kitten − cat + dog lands exactly on puppy here, because the toy vectors were built so that “young version of” is the same displacement for both animals. In real embeddings such analogies hold only approximately, and are known to reflect biases in the training data.

    Toy vectors, real ideas The ten vectors above are invented, and two dimensions with labelled axes is a big simplification. Learned embeddings have hundreds of dimensions with no human-readable meaning per axis. The geometry, nearness by angle, and the dot products used to measure it, are the same.

    References

    1. T. Mikolov, K. Chen, G. Corrado and J. Dean, “Efficient estimation of word representations in vector space”, 2013. arXiv:1301.3781
    2. G. Strang, Introduction to Linear Algebra, 6th ed., Wellesley-Cambridge Press, 2023.
    3. 3Blue1Brown, Essence of linear algebra, which develops the “matrices are transformations” view visually.