# Vectorised Computation and Libraries

Mathematics I · Scientific Programming · https://tryals.app/learn/mathematics-i/vectorised-computation-and-libraries

## Vectorised Computation

Scientific computing rests on two pillars: **vectorised computation** and **numerical libraries**. Modern code avoids explicit element loops.

**Vectorisation** expresses operations on whole arrays at once. Instead of `c[i] = a[i] + b[i]`, you write $c = a + b$.

This is dramatically **faster**. Python loops are slow; vectorised operations dispatch to compiled C or Fortran code using CPU SIMD parallel instructions.

| Approach | Execution | Readability |
| --- | --- | --- |
| **Loop** | Slow, element-by-element | Verbose, low-level |
| **Vector** | Fast, compiled SIMD | Matches math, concise |

## Numerical Libraries

**Numerical libraries** provide tested routines so you never reimplement fundamentals. They handle stability and pivoting correctly.

| Library Type | Core Purpose |
| --- | --- |
| **Array Library** | NumPy-style multidimensional data |
| **Linear Algebra** | BLAS/LAPACK solvers and transforms |

**Common Pitfall:** Writing **explicit element-by-element loops** or **reimplementing** standard solvers. Always prefer robust library routines over reinventing the wheel.

## Practice questions

6 of this lesson's 11 practice questions, with answers. The full set is in the app.

### 1. Monte Carlo error falls like $1/\sqrt{N}$ regardless of the dimension, which is why it wins in high dimensions.

**Answer:** True

**Why:** True. Grid-based quadrature needs $k^{d}$ points in $d$ dimensions, hopeless past a handful, while Monte Carlo's $1/\sqrt{N}$ rate is dimension-free. It is slow in one dimension and unbeatable in twenty, which is why it dominates in finance, physics and rendering.

Page: https://tryals.app/practice/mathematics-i/vectorised-computation-and-libraries/monte-carlo-error-falls-like-1-n-regardless-of-the-dimension

### 2. A Monte Carlo simulation run twice with different random seeds gives two different answers. Why is that a feature of a correct implementation rather than a bug?

A. Because the output is itself a random variable with a spread.
B. Because Monte Carlo results should be averaged over runs, and a single run is meaningless.
C. Because the differences come from floating-point round-off rather than from the sampling.
D. Because the random number generator is faulty, and a correct one would give identical results.

**Answer:** A. Because the output is itself a random variable with a spread.

**Why:** The estimate is an average over randomly drawn samples, so it is itself a random variable with spread about σ/√N, the standard error the method predicts. That variation is diagnostic: rerunning and looking at the spread is the standard way to estimate uncertainty. Record the seed for reproducibility, rather than quoting eight digits from a method whose error is in the third.

Page: https://tryals.app/practice/mathematics-i/vectorised-computation-and-libraries/a-monte-carlo-simulation-run-twice-with-different-random-seeds-gives

### 3. An interpreted loop processes 2 million elements per second while the vectorised version processes 500 million per second. How many seconds does the loop need for 100 million elements?

**Answer:** 50

**Why:** 100/2 = 50 seconds, against 0.2 seconds for the vectorised version. A minute versus a fifth of a second on identical arithmetic is what 'orders of magnitude slower' actually means in practice.

Page: https://tryals.app/practice/mathematics-i/vectorised-computation-and-libraries/an-interpreted-loop-processes-2-million-elements-per-second-while-the

### 4. Sort each code pattern by its style.

**Answer:**

- Vectorised: c = a + b, total = sum(a), d = 2 * a
- Explicit loop: for each index i, set c[i] = a[i] + b[i], for each x in a, set total = total + x

**Why:** Vectorised code names whole arrays and no indices; the loop versions compute identical results far more slowly. The pairs are deliberately equivalent so that the only difference is the idiom, and hence the speed.

Page: https://tryals.app/practice/mathematics-i/vectorised-computation-and-libraries/sort-each-code-pattern-by-its-style

### 5. Complete the reason vectorised code is faster.

**Answer:** A vectorised array operation beats an explicit loop because it **hands the whole array to optimised compiled code**.

**Why:** The arithmetic is identical; the gain comes from replacing interpreted per-element work with compiled code that can process many elements per cycle. Believing that fewer operations are performed is the standard misunderstanding.

Page: https://tryals.app/practice/mathematics-i/vectorised-computation-and-libraries/complete-the-reason-vectorised-code-is-faster

### 6. Monte Carlo error falls like 1/√N. To gain one more decimal digit of accuracy, what does that cost?

A. A tenfold increase, matching the digit gained
B. A hundredfold increase in samples, since error falls like 1/√N
C. A doubling, since error halves with each doubling of N
D. No increase, since the estimate converges regardless of N

**Answer:** B. A hundredfold increase in samples, since error falls like 1/√N

**Why:** √N must grow tenfold, so N grows a hundredfold, brutal for high precision. It is tolerated because the rate is independent of dimension, so in 300 dimensions where grid methods are impossible it is the only option, and three or four digits is usually all anyone needs.

Page: https://tryals.app/practice/mathematics-i/vectorised-computation-and-libraries/monte-carlo-error-falls-like-1-n-to-gain-one-more-decimal-digit-of
