Direct Solutions to Linear Systems
Numerical Methods
David Mayerich
Scalable Tissue Imaging and Modeling (STIM) Laboratory
Department of Electrical and Computer Engineering
Cullen College of Engineering
University of Houston
David Mayerich
STIM Laboratory, University of Houston

Partial Pivoting
Roundoff in Gaussian Elimination
Partial Pivoting
Pivoting Strategies
David Mayerich
STIM Laboratory, University of Houston
Roundoff in Gaussian Elimination
-
What is the exact solution to the following linear system using 3 digits of precision?
David Mayerich
STIM Laboratory, University of Houston
Swapping Pivot Equations
-
What is the exact solution to the following linear system using 3 digits of precision?
David Mayerich
STIM Laboratory, University of Houston
Partial Pivoting
-
The pivot row is scaled to eliminate leading coefficients in other rows
David Mayerich
STIM Laboratory, University of Houston
-
Select the pivot equation with the largest leading coefficient and swap rows
-
Perform row reduction
Partial Pivoting (iteration 2)
-
The pivot row is scaled to eliminate leading coefficients in other rows
David Mayerich
STIM Laboratory, University of Houston
-
Select the pivot equation with the largest leading coefficient and swap rows
-
Perform row reduction
Comparison
David Mayerich
STIM Laboratory, University of Houston
Gaussian Elimination
Partial Pivoting
Implementation Strategies
-
Swapping is slow: introduces an \(O(n)\) memory copy every iteration:
David Mayerich
STIM Laboratory, University of Houston
Implementation Strategies
-
Swapping is slow: introduces an \(O(n)\) memory copy every iteration:
David Mayerich
STIM Laboratory, University of Houston
Implementation Strategies
-
Swapping is slow: introduces an \(O(n)\) memory copy every iteration:
David Mayerich
STIM Laboratory, University of Houston
Implementation Strategies
-
Swapping is slow: introduces an \(O(n)\) memory copy every iteration:
David Mayerich
STIM Laboratory, University of Houston
Implementation Strategies
-
Swapping is slow: introduces an \(O(n)\) memory copy every iteration:
David Mayerich
STIM Laboratory, University of Houston
-
Use an \(n\)-dimensional index vector \(\ell \in \mathbb{N}^n\)
int i = 2 // row index
int j = 1 // column index
// direct access
x = C[i * n + j]; // x = a_21
// indirect access
y = C[l[i] * n + j]; // y = a_n1
Partial Pivoting Algorithm
David Mayerich
STIM Laboratory, University of Houston
void gaussian(double* A, double* b, int N){
for(int k=0; k<N-1; k++){ // for each pivot
for(int i=k+1; i<N; i++){ // for each equation
float m=A[i*N+k] / A[k*N+k]; // calculate scale
for(int j=k; j<N; j++){ // for each coefficient
A[i*N+j] -= m * A[k*N+j]; // subtract
}
b[i] -= m * b[k]; // forward elimination
}
}
}Partial Pivoting Algorithm
David Mayerich
STIM Laboratory, University of Houston
void ppivot(double* A, double* b, int N){
int* L;
init(L); // initialize the index vector
for(int k=0; k<N-1; k++){ // for each pivot
for(int i=k+1; i<N; i++){ // for each equation
float m=A[i*N+k] / A[k*N+k]; // calculate scale
for(int j=k; j<N; j++){ // for each coefficient
A[i*N+j] -= m * A[k*N+j]; // subtract
}
b[i] -= m * b[k]; // forward elimination
}
}
}Partial Pivoting Algorithm
David Mayerich
STIM Laboratory, University of Houston
void ppivot(double* A, double* b, int N){
int* L;
init(L); // initialize the index vector
for(int k=0; k<N-1; k++){ // for each pivot
int p = select_pivot(A, L, k, N); // select the best pivot
for(int i=k+1; i<N; i++){ // for each equation
float m=A[i*N+k] / A[k*N+k]; // calculate scale
for(int j=k; j<N; j++){ // for each coefficient
A[i*N+j] -= m * A[k*N+j]; // subtract
}
b[i] -= m * b[k]; // forward elimination
}
}
}Partial Pivoting Algorithm
David Mayerich
STIM Laboratory, University of Houston
void ppivot(double* A, double* b, int N){
int* L;
init(L); // initialize the index vector
for(int k=0; k<N-1; k++){ // for each pivot
int p = select_pivot(A, L, k, N); // select the best pivot
swap(L, p, k); // swap indices
for(int i=k+1; i<N; i++){ // for each equation
float m=A[i*N+k] / A[k*N+k]; // calculate scale
for(int j=k; j<N; j++){ // for each coefficient
A[i*N+j] -= m * A[k*N+j]; // subtract
}
b[i] -= m * b[k]; // forward elimination
}
}
}Partial Pivoting Algorithm
David Mayerich
STIM Laboratory, University of Houston
void ppivot(double* A, double* b, int N){
int* L;
init(L); // initialize the index vector
for(int k=0; k<N-1; k++){ // for each pivot
int p = select_pivot(A, L, k, N); // select the best pivot
swap(L, p, k); // swap indices
for(int i=k+1; i<N; i++){ // for each equation
float m=A[L[i]*N+k] / A[L[k]*N+k];// calculate scale
for(int j=k; j<N; j++){ // for each coefficient
A[L[i]*N+j] -= m * A[L[k]*N+j]; // subtract
}
b[L[i]] -= m * b[L[k]]; // forward elimination
}
}
}Pivoting Strategies
Scaled Partial Pivoting
Full Pivoting
David Mayerich
STIM Laboratory, University of Houston
Scaling Challenges
David Mayerich
STIM Laboratory, University of Houston
-
Using partial pivoting, \(r_2\) will be selected as the first pivot, resulting in:
Scaled Partial Pivoting
-
Divide each equation by its largest coefficient:
David Mayerich
STIM Laboratory, University of Houston
-
Applying partial pivoting to this scaled matrix yields:
Is Scaling Necessary?
-
Use the scaled coefficient to select the pivot, but don't actually apply scaling:
David Mayerich
STIM Laboratory, University of Houston
w/ scaling
Full Pivoting
-
Swap rows and columns to optimize for the largest coefficient:
David Mayerich
STIM Laboratory, University of Houston
Pivoting Strategies
-
Partial pivoting:
David Mayerich
STIM Laboratory, University of Houston
-
Scaling with partial pivoting:
-
Scaled partial pivoting (use the scale to select the pivot):
-
Full pivoting:
LU Decomposition
Solving Multiple Right-Hand Sides
Matrix Factorization
Benefits for LU Decomposition
David Mayerich
STIM Laboratory, University of Houston
Row Reduction as Matrix Multiplication
-
Consider row reduction of the following matrix:
David Mayerich
STIM Laboratory, University of Houston
-
This can be encoded in the matrix multiplication:
unitriangular
matrix
Unitriangular Matrix
-
A matrix \(\mathbf{A}\) is unitriangular if it is:
-
all diagonal elements are one
-
all lower-left or upper-right are zero
-
David Mayerich
STIM Laboratory, University of Houston
- \(\mathbf{0}\) is a matrix of all zeros
- \(\mathbf{M}\) is a general matrix
- \(\mathbf{T}\) is a smaller unitriangular matrix
-
The inverse of a unitriangular matrix is:
(negate all non-zero off-diagonal elements)
Matrix Factorization
-
We can perform row reduction using a unidiagonal matrix of scale factors:
David Mayerich
STIM Laboratory, University of Houston
-
And invert that process using the inverse matrix:
LU Decomposition
LU Decomposition is the process of factorizing a matrix \(\mathbf{A}=\mathbf{L}\mathbf{U}\) into a lower unitriangular matrix \(\mathbf{L}\) and an upper triangular matrix \(\mathbf{U}\)
David Mayerich
STIM Laboratory, University of Houston
-
Given a matrix \(\mathbf{A}\), calculate \(\mathbf{L}\) and \(\mathbf{U}\):
-
Initialize \(\mathbf{L}_0 = \mathbf{I}\) with the identity matrix
-
Perform Gaussian elimination of \(\mathbf{A}\), which produces \(\mathbf{U}\)
-
For each iteration \(n\) of Gaussian elimination, store the scale factors in \(\mathbf{L}_n\)
-
LU Decomposition Algorithm
David Mayerich
STIM Laboratory, University of Houston
lu_decomposition(double* A, double* L, int N){
identity(L, N); // initialize L
for(int k=0; k<N-1; k++){ // for each pivot
for(int i=k+1; i<N; i++){ // for each equation
float m=A[i*N+k] / A[k*N+k]; // calculate scale
L[i*N+k] = m; // store the scale factor
for(int j=k; j<N; j++){ // for each coefficient
A[i*N+j] -= m * A[k*N+j]; // subtract
}
}
}
}Solving Multiple Right-Hand Sides
-
Solve the following two linear systems:
David Mayerich
STIM Laboratory, University of Houston
-
Using a direct method would require \(O(n)\) time for each system
-
What is the complexity for solving a linear system \(\mathbf{A}\mathbf{x}=\mathbf{b}\) if \(\mathbf{A}\) is triangular?
-
Factorize \(\mathbf{A}\) and solve:
LU decomposition:
Solve for \(\mathbf{z}=\mathbf{Ux}\):
Solve for \(\mathbf{x}\):
Solving Multiple Right-Hand Sides
David Mayerich
STIM Laboratory, University of Houston
E.2 Direct Solutions
By STIM Laboratory
E.2 Direct Solutions
- 1