Implement a Matrix Solver
Thomas J. Kennedy
1 Overview
In this module we discussed Least Squares approximation. Part of the discussion included designing pseudocode for matrix multiplication. Revisit the pseudocode.
Example 1: Matrix Multiplication Pseudocodedef multiply(lhs, rhs): # let result be a lhs.rows by rhs.columns matrix for i in 0...result.rows for j in 0...result.cols for k in 0...lhs.cols result[i][j] += lhs[i][k] * rhs[k][j] return result
If you did not finish implementing this code.
In this assignment you will:
- Select a programming language (C++14+ or Python 3.7+ are strongly encouraged).
- Reformulate pseudocode for use with your selected language.
- Implement the refined pseudocode in your selected language.
2 Purpose
As a Computer Science student, you have been various software design methodologies (e.g., stepwise refinement) and mathematical problems (e.g., systems of equations as matrices). In this exercise you will take pseudocode, implement it, and analyze its complexity (temporal and spatial).
3 What to Do
-
Start by creating a ReadMe file (either
.md
or.txt
). Use the provided template to start your required ReadMe:Example 2: Sample ReadMe.md# Getting Started **Language:** {selected_language_here:} # My Pseudocode {insert pseudocode here} # Requirements {List dependencies and libraries here} # Compilation & Execution Instructions {list required commands}
Each
{}
represents a placeholder for your content. -
Complete, document, and test your matrix multiplication code.
-
Take the Gaussian Elimination Pseudocode discussed in The Initial Pseudocode section and rewrite it to work with your selected language and data structures.
-
Place your finalized pseudocode under
# My Pseudocode
in your ReadMe file. -
Implement a matrix solver, based on your updated pseudocode.
-
When you are done… save this code. If you are careful, you will have a significant chunk of code you need for the semester project! A library you can use with a quick C++
#include {}
or Javaimport {}
or Pythonfrom {} import {}
is your goal. -
Package your ReadMe file, source code, and build configuration into a single zip file or tarball.
3.1 Input
For this exercise you may hardcode the initialization of your $X$, $X^T$, and $Y$ matrices. Use the matrices from Quadratic Example as your inputs.
3.2 Output
Your output should be a single line in the form
phi_hat = c_0 + c_1 * x + c_2 * x^2
where c_0
, c_1
, and c_2
are the coefficients computed by your code.
4 Grading
Revisit the standard requirements for Programming Exercises (defined in the syllabus). In this assignment you will be graded on:
- Quality of your pseudocode
- Quality of your implementation
- Analysis of your implementation
5 Submission
You will submit your solution through Canvas in accordance with the requirements for Programming Exercises (defined in the syllabus).
6 The Initial Pseudocode
Example 3: Initial PseudocodeStart
- Pivot
- Search for max $r_{i,j}$
- Swap row i with row largest
- Scale
- Eliminate
Example 4: Top Level PseudocodeLet A be an n by n matrix with an augmented vector in col n # Iterate through all rows for every i in 0 to n-1: # Pivot idx <- find_largest_row_by_col(A, col_index, num_rows) swap_row(A, i, idx) scale(A, i, num_cols, A[i][i]) A[i][i] = 1 eliminate(A, i, num_rows)
Example 5: Scaledef scale_row(A, row_idx, num_cols, s): for every j in 0 to num_cols: A[row_idx][j] = A[row_idx][j] / s
Example 6: Eliminatedef eliminate(A, src_row_idx, num_cols, num_rows): start_col <- src_row_idx for every i in (src_row_idx + 1) to num_rows: s <- A[i][start_col] for every j in start_col to num_cols: A[i][j] = A[i][j] - s * A[src_row_idx][j] A[i][start_col] = 0
Example 7: Backsolvedef back_solve(matrix): augColIdx <- matrix.cols() # the augmented column lastRow = matrix.rows() - 1 for i in lastRow down to 1: for j <- (i - 1) down to 0: s <- matrix[j][i] matrix[j][i] -= (s * matrix[i][i]) matrix[j][augCol] -= (s * matrix[i][augColIdx])