Think back to that point in time when you were in middle school or high school and you learned how to use algebraic methods for solving systems of linear equations. By systems of linear equations, I mean something like the following.
$x + 3y = 6 \\ 2x + 8y = -12$
(When I learned algebra, this was referred to as a pair of simultaneous equations.) The objective of finding a solution to a system of equations is described here.
You may have learned several methods for solving this system of linear equations for $x$ and $y$. If you correctly apply one of those methods, you will find that the solution is:
$x = 42 \\ y = -12$
If you need to refresh your memory on how to solve this system of linear equations, you might want to refer to the following web pages.
Before continuing, you need to make certain that you understand the content of the first two resources listed above. Hopefully you can understand the other materal as well.
Now let's explore another solution that you may or may not be familiar with. (If you are not familiar with matrices, you should have no difficulty learning about the topic by studying the free online resources listed below.)
Please begin by studying the following material. Once you understand that material, please proceed to the next section where I will show you how to use the matrix capabilities of linear algebra and the SciPy library to solve the same system of equations as that shown in the two videos identified as A and B.
In the first video mentioned above, the instructor derives the following matrix equation to represent a system of two equations in two unknowns. Note that this solution requires you to invert the matrix shown with the exponent of -1.
$\begin{equation*} \begin{vmatrix} \mathbf{s} \\ \mathbf{t} \end{vmatrix} = \begin{vmatrix} \mathbf{2} & \mathbf{-5}\\ \mathbf{-2} & \mathbf{4} \end{vmatrix}^{-1} \begin{vmatrix} \mathbf{7} \\ \mathbf{-6} \end{vmatrix} \end{equation*}$
Let's solve for the values of $s$ and $t$ using the linear algebra capabilities of SciPy.
Begin by importing the necessary libraries.
import numpy as np
from scipy import linalg
Now create and display the required matrices (as arrays) using the same nomenclature as in the videos.
A = np.array([[2,-5],[-2,4]])
print(A)
b = np.array([[7],[-6]])
print(b)
Now invert and display the inverse of the matrix A.
Ainv = linalg.inv(A)
print(Ainv)
Now compute the dot product of Ainv and b to produce a column matrix containing the values of the unknown variables $s$ and $t$.
st = Ainv.dot(b)
print(st)
Thus, the solution is
$s = 1 \\ t = -1$
This matches the soution in the second video mentioned above.
We can confirm that this is the correct solution by computing the dot product of the solution vector and the vector A and comparing the result with the original column vector b.
print(A.dot(st))
We have a match, so our solution is correct.
The above procedure follows the procedure outlined in the video, and there is nothing wrong with that procedure. However, SciPy offers a higher-level (more abstract) capability for doing the same thing with less code and more speed. While more cryptic, this procedure eliminates the need to perform the matrix inversion and the dot product.
A = np.array([[2,-5],[-2,4]])
b = np.array([[7],[-6]])
st = np.linalg.solve(A,b)
print(st)
As you can see, this approach requires only three statements and produces the same correct solution as the longer procedure shown above.
By now you are probably thinking that we went to a lot of trouble to solve a simple problem involving only two equations with two unknowns. However, let's make the problem more difficult by specifying three equations with three unknowns as shown below. (When doing the job by hand, the solution to a system of three equations is significantly more difficult than the solution of two equarions.)
$\begin{equation*} \begin{vmatrix} \mathbf{s} \\ \mathbf{t} \\ \mathbf{u} \end{vmatrix} = \begin{vmatrix} \mathbf{1} & \mathbf{3} & \mathbf{5} \\ \mathbf{2} & \mathbf{5} & \mathbf{1} \\ \mathbf{2} & \mathbf{3} & \mathbf{8} \end{vmatrix}^{-1} \begin{vmatrix} \mathbf{10} \\ \mathbf{8} \\ \mathbf{3} \end{vmatrix} \end{equation*}$
As before, begin by creating and displaying the required matrices.
A = np.array([[1,3,5],[2,5,1],[2,3,8]])
print(A)
b = np.array([[10],[8],[3]])
print(b)
Now call the solve function to compute and display the solution.
stu = np.linalg.solve(A,b)
print(stu)
Thus,
$s=-9.28 \\ t=5.16 \\ u=0.76$
If you want, you can plug those values into the original equations to confirm that this is the correct solution.
Let's step it up one more notch and solve the following system of four equations with four unknowns. This is an very difficult problem to solve by hand.
$\begin{equation*} \begin{vmatrix} \mathbf{s} \\ \mathbf{t} \\ \mathbf{u} \\ \mathbf{v} \end{vmatrix} = \begin{vmatrix} \mathbf{1} & \mathbf{3} & \mathbf{5} & \mathbf{7} \\ \mathbf{2} & \mathbf{5} & \mathbf{1} & \mathbf{3} \\ \mathbf{2} & \mathbf{3} & \mathbf{8} & \mathbf{1} \\ \mathbf{1} & \mathbf{2} & \mathbf{3} & \mathbf{4} \end{vmatrix}^{-1} \begin{vmatrix} \mathbf{10} \\ \mathbf{8} \\ \mathbf{3} \\ \mathbf{1} \end{vmatrix} \end{equation*}$
A = np.array([[1,3,5,7],[2,5,1,3],[2,3,8,1],[1,2,3,4]])
print(A)
b = np.array([[10],[8],[3],[1]])
print(b)
stuv = np.linalg.solve(A,b)
print(stuv)
The solution is,
$s=-17.4 \\ t=8.83333333 \\ u=1.53333333 \\ v=-0.96666667$
We can confirm that this is the correct solution by computing the dot product between the A matrix and the solution matrix and comparing the result with the original b matrix.
print(A.dot(stuv))
We have a match, so the solution is correct.
Author: Prof. Richard G. Baldwin
Affiliation: Professor of Computer Information Technology at Austin Community College in Austin, TX.
File: LinearAlgebra01.html
Revised: 05/22/18
Copyright 2018 Richard G. Baldwin