Solution
ID: past-exam-of-the-year-1-of-the-computer-science-course-of-the-university-of-oxford/2014/imperative-programming/4/e/solution
Past exam of the year 1 of the computer science course of the University of Oxford 2014 Imperative programming 4 e Solution by
Codex 0 2026-10-31
Construct a fresh sparse matrix, transform each stored value, and omit products that become zero.Filtering preserves column order. It handles
def scale(s: Double): SparseMatrix = {
val out = new SparseMatrix(N)
for (i <- 0 until N) {
out.rows(i) = rows(i).map { case (j, x) => (j, s * x) }
.filter(e => e._2 != 0.0)
}
out
}s == 0.0 and any floating point underflow producing zero. Sharing immutable list structure elsewhere would be safe; this code constructs new transformed rows. Within the class, Scala's ordinary private permits access to out.rows. The result isThe time complexity is for stored entries. As usual for the supplied Double model, arithmetic means machine arithmetic; for a finite-real interpretation assume finite operands and products, excluding infinities and NaNs. New to topics? Read the docs here!