Problem "Linear Equations"

Objective

Implement functions for solving linear equations, calculating matrix inverse and matrix determinant.

Introduction

To work with matrices you can use our matlib/matrix/matrix.cs[] that implements some basic matrix operations. Alternatively, you can creaite your own matrix class the way you like it. At the bare minimum your class should look something like

public class matrix{
	public readonly int size1,size2;
	private double[] data; // keep data in one big array
	public matrix(int n,int m){// constructor
		size1=n;
		size2=m;
		data = new double[size1*size2];
		}
	public double this[int i,int j]{     // indexer
		get{return data[i+j*size1];} // getter
		set{data[i+j*size1]=value;}  // setter
		}
	public void set(int i, int j, double x){ // just in case, Java style
		data[i+j*size1]=x;
		}
	public double get(int i, int j){
		return data[i+j*size1];
		}
}
Tasks
  1. (6 points) Solving linear equations using QR-decomposition by modified Gram-Schmidt orthogonalization

    The Gram-Schmidt orthogonalization process, even modified, is less stable and accurate than the Givens roation algorithm. On the other hand, the Gram-Schmidt process produces the j-th orthogonalized vector after the j-th iteration, while orthogonalization using Givens rotations produces all the vectors only at the end. This makes the Gram–Schmidt process applicable for iterative methods like the Arnoldi iteration.

    Also its ease of implementation makes it a useful exercise for the students.

    1. Implement a function, say

      void qr_gs_decomp(matrix A, matrix R)
      
      that performs in-place modified Gram-Schmidt orthogonalization of an n×m (n≥m) matrix A: on exit matrix A is replaced with Q and the m×m matrix R holds the computed matrix R.

      Check that you function works as intended:

      • generate a random tall (n>m) matrix A (of a modest size);
      • factorize it into QR;
      • check that R is upper triangular;
      • check that QTQ=1;
      • check that QR=A;
    2. Implement a function, say

      vector qr_gs_solve(matrix Q, matrix R, vector b)
      
      that—given the matrices Q and R from qr_gs_decomp—solves the equation QRx=b by applying QT to the vector b (saving the result in a new vector x) and then performing in-place back-substitution on x and returning it.

      Check that you function works as intended:

      • generate a random square matrix A (of a modest size);
      • generate a random vector b (of the same size);
      • factorize A into QR;
      • solve QRx=b;
      • check that Ax=b;
  2. (3 points) Matrix inverse by Gram-Schmidt QR factorization

    Implement a function, say

    matrix qr_gs_inverse(matrix Q, matrix R)
    
    that—given the matrices Q and R from qr_gs_decomp—calculates the inverse of the matrix A into a new matrix and returns it.

    Check that you function works as intended:

    • generate a random square matrix A (of a modest size);
    • factorize A into QR;
    • calculate the inverse B;
    • check that AB=I, where I is the identity matrix;
  3. (1 point) QR-decomposition by Givens rotations

    Implement the Givens rotation algorithm instead of Gram-Schmidt algorithm.

    Do not actually calculate and store the Q-matrix but rather store the rotation θ-angles in the places of the corresponding zeroed elements of matrix A. Neither should the back-substitution routine build the Q-matrix explicitly, but rather apply consecutively the stored individual Given's rotations.