Scientific Computing (Python)
Level: 2 (Recall / Standard problems) Time limit: 30 minutes Total marks: 40
Answer all questions. Show working where computation is required. Assume import numpy as np and relevant SciPy/SymPy submodules are available.
Q1. (4 marks)
For a NumPy ndarray of shape (3, 4) with dtype=np.float64 stored in C (row-major) order:
(a) State the total number of bytes used by the data buffer. (2)
(b) State its strides tuple in bytes. (2)
Q2. (4 marks)
Write the single line of output produced by each expression:
(a) np.arange(2, 11, 3) (2)
(b) np.linspace(0, 1, 5) (2)
Q3. (5 marks)
Given a = np.array([10, 20, 30, 40, 50]):
(a) Write the expression using boolean masking that returns all elements greater than 25. (2)
(b) Write the expression using fancy indexing that returns elements at positions 0, 2, 4. (2)
(c) What is the result of a[::-1]? (1)
Q4. (5 marks)
(a) State the two rules of NumPy broadcasting. (3)
(b) Two arrays have shapes (4, 1) and (3,). State the shape of their sum, or explain why it fails. (2)
Q5. (4 marks) Name the NumPy/SciPy function you would use for each task (one function name each): (a) Solve the linear system . (1) (b) Compute eigenvalues and eigenvectors. (1) (c) Numerically integrate a 1-D function over . (1) (d) Solve an initial-value ODE (modern interface). (1)
Q6. (4 marks)
Briefly state what each SciPy submodule is primarily used for:
(a) scipy.optimize (1)
(b) scipy.sparse (1)
(c) scipy.stats (1)
(d) scipy.signal (1)
Q7. (5 marks) (a) Write the update formula for one step of the Euler method for with step . (2) (b) Write the Newton–Raphson iteration formula for solving . (3)
Q8. (5 marks) Apply the trapezoidal rule with subintervals to estimate . Show the node values and the final numeric estimate. (5)
Q9. (4 marks)
Floating point:
(a) Define catastrophic cancellation in one sentence. (2)
(b) Evaluate whether 0.1 + 0.2 == 0.3 is True or False in IEEE-754 double precision, and state why. (2)
Answer keyMark scheme & solutions
Q1. (4)
(a) elements, each float64 = 8 bytes ⇒ bytes. (2)
(b) C-order strides: last axis contiguous. Row stride = 4 elements × 8 = 32, column stride = 8. Strides = (32, 8). (2)
Why: strides give the byte step to move one index along each axis; inner (column) axis moves 8 bytes, outer (row) moves a full row of 4×8.
Q2. (4)
(a) [2 5 8] — start 2, stop 11 (exclusive), step 3. (2)
(b) [0. 0.25 0.5 0.75 1. ] — 5 evenly spaced points including both endpoints. (2)
Q3. (5)
(a) a[a > 25] → array([30, 40, 50]). (2)
(b) a[[0, 2, 4]] → array([10, 30, 50]). (2)
(c) Reversed array: array([50, 40, 30, 20, 10]). (1)
Q4. (5) (a) Rules (3, 1 each):
- Align shapes from the trailing (rightmost) dimension. (1)
- Two dimensions are compatible if equal or one of them is 1. (1)
- A size-1 (or missing) dimension is stretched/broadcast to match the other. (1)
(b)
(4,1)and(3,): pad(3,)→(1,3). Compare with(4,1): axis0 → 4 vs 1 ⇒ 4; axis1 → 1 vs 3 ⇒ 3. Result shape(4, 3). (2)
Q5. (4)
(a) np.linalg.solve (1)
(b) np.linalg.eig (1)
(c) scipy.integrate.quad (1)
(d) scipy.integrate.solve_ivp (1)
Q6. (4)
(a) scipy.optimize — minimization, root-finding, curve fitting, linear programming. (1)
(b) scipy.sparse — sparse matrix storage (CSR/CSC etc.) and sparse solvers. (1)
(c) scipy.stats — probability distributions and statistical/hypothesis tests. (1)
(d) scipy.signal — signal processing: filtering, convolution, spectral analysis. (1)
Q7. (5) (a) Euler: . (2) (b) Newton–Raphson: . (3)
Q8. (5) , . Nodes: . (1) Values : . (2) Trapezoidal: . (2) (Exact value is ; estimate 3 is acceptable.)
Q9. (4)
(a) Catastrophic cancellation: subtracting two nearly-equal floating-point numbers destroys significant digits, greatly amplifying relative error. (2)
(b) False. Neither 0.1, 0.2 nor 0.3 is exactly representable in binary; the rounded sum differs from the rounded 0.3 by about . (2)
[
{"claim":"float64 (3,4) C-order buffer is 96 bytes and strides (32,8)","code":"import numpy as np; a=np.zeros((3,4),dtype=np.float64); result=(a.nbytes==96 and a.strides==(32,8))"},
{"claim":"broadcasting (4,1)+(3,) gives shape (4,3)","code":"import numpy as np; s=(np.zeros((4,1))+np.zeros((3,))).shape; result=(s==(4,3))"},
{"claim":"trapezoidal n=2 estimate of x^2 on [0,2] is 3","code":"import numpy as np; x=np.array([0.0,1.0,2.0]); y=x**2; h=1.0; est=h/2*(y[0]+2*y[1]+y[2]); result=(est==3.0)"},
{"claim":"0.1+0.2 != 0.3 in double precision","code":"result=(0.1+0.2 != 0.3)"}
]