# Chapter 6 SageMath and WeBWorK

We can embed WeBWorK problems.

We can also embed SageMath Cells

Row Reduced Echelon Form Using SageMath

In an HTML output, we can embed a SageMath Cell and use sage, which is Python-based, to do numerical linear algebra. The same matrix from the pracma section above is defined in sage with the code A = matrix(3,3,[1, 2, 3, 1, 3, 2, 3, 2, 1]) and then the row reduced echelon form is computed by applying the method .rref() to the matrix. Once you open the HTML output in your browser (Chrome recommended), click the Evaluate button under the SageMath cell to see the result.

SageMath in CoCalc

One can use the free option for CoCalc to run sage on their server.

Row Reduced Echelon Form

The pracma package has many useful numerical math functions, including numerical linear algebra.

The pracma package can be installed in RStudio by clicking the Packages tab (on the right-hand side of the RStudio Editor), and then clicking the Install button. Typing the name of the package and clicking Install will install the package and its dependencies in no time, and that’s it.

The function rref(A) from the pracma package produces the reduced row echelon form of the matrix $$A$$ using Gauss-Jordan elimination. Here is an example:

library(pracma) # pracma must be installed first
A <- matrix(c(1, 2, 3, 1, 3, 2, 3, 2, 1), 3, 3, byrow = TRUE)
A
##      [,1] [,2] [,3]
## [1,]    1    2    3
## [2,]    1    3    2
## [3,]    3    2    1
rref(A) # Reduced Row Echelon Form
##      [,1] [,2] [,3]
## [1,]    1    0    0
## [2,]    0    1    0
## [3,]    0    0    1

Row Reduced Echelon Form Using the Matlib R Package

The matlib package provides matrix functions for teaching and learning linear algebra and multivariate statistics.

The matlib package can be installed in RStudio by clicking the Packages tab (on the right-hand side of the RStudio Editor), and then clicking the Install button. Typing the name of the package and clicking Install will install the package and its dependencies in no time. When the installation starts, you’ll get the question:

Do you want to install from sources the package which needs compilation? (Yes/no/cancel)

Make sure you type no and press Return.

Here is an example of solving the linear system $$A\boldsymbol{x}=\boldsymbol{b}$$ via Gaussian elimination, using the matlib package, where the matrix $$A$$ and the vector $$\boldsymbol{b}$$ are defined in the code chunk below.

library(matlib) # matlib must be installed first
A <- matrix(c(1,3,2,4,4,-3,5,1,2), nrow = 3, byrow = TRUE)
b <- c(13,3,13)
gaussianElimination(A, b)
##      [,1] [,2] [,3] [,4]
## [1,]    1    0    0    1
## [2,]    0    1    0    2
## [3,]    0    0    1    3

The gaussianElimination() function has an optional argument verbose = TRUE that prints the Gaussian elimination step by step for educational purposes. Here is the result below:

A <- matrix(c(1,3,2,4,4,-3,5,1,2), nrow = 3, byrow = TRUE)
b <- c(13,3,13)
gaussianElimination(A, b, verbose = TRUE)
##
## Initial matrix:
##      [,1] [,2] [,3] [,4]
## [1,]    1    3    2   13
## [2,]    4    4   -3    3
## [3,]    5    1    2   13
##
## row: 1
##
##  exchange rows 1 and 3
##      [,1] [,2] [,3] [,4]
## [1,]    5    1    2   13
## [2,]    4    4   -3    3
## [3,]    1    3    2   13
##
##  multiply row 1 by 0.2
##      [,1] [,2] [,3] [,4]
## [1,]    1  0.2  0.4  2.6
## [2,]    4  4.0 -3.0  3.0
## [3,]    1  3.0  2.0 13.0
##
##  multiply row 1 by 4 and subtract from row 2
##      [,1] [,2] [,3] [,4]
## [1,]    1  0.2  0.4  2.6
## [2,]    0  3.2 -4.6 -7.4
## [3,]    1  3.0  2.0 13.0
##
##  subtract row 1 from row 3
##      [,1] [,2] [,3] [,4]
## [1,]    1  0.2  0.4  2.6
## [2,]    0  3.2 -4.6 -7.4
## [3,]    0  2.8  1.6 10.4
##
## row: 2
##
##  multiply row 2 by 0.3125
##      [,1] [,2]    [,3]    [,4]
## [1,]    1  0.2  0.4000  2.6000
## [2,]    0  1.0 -1.4375 -2.3125
## [3,]    0  2.8  1.6000 10.4000
##
##  multiply row 2 by 0.2 and subtract from row 1
##      [,1] [,2]    [,3]    [,4]
## [1,]    1  0.0  0.6875  3.0625
## [2,]    0  1.0 -1.4375 -2.3125
## [3,]    0  2.8  1.6000 10.4000
##
##  multiply row 2 by 2.8 and subtract from row 3
##      [,1] [,2]    [,3]    [,4]
## [1,]    1    0  0.6875  3.0625
## [2,]    0    1 -1.4375 -2.3125
## [3,]    0    0  5.6250 16.8750
##
## row: 3
##
##  multiply row 3 by 0.1777778
##      [,1] [,2]    [,3]    [,4]
## [1,]    1    0  0.6875  3.0625
## [2,]    0    1 -1.4375 -2.3125
## [3,]    0    0  1.0000  3.0000
##
##  multiply row 3 by 0.6875 and subtract from row 1
##      [,1] [,2]    [,3]    [,4]
## [1,]    1    0  0.0000  1.0000
## [2,]    0    1 -1.4375 -2.3125
## [3,]    0    0  1.0000  3.0000
##
##  multiply row 3 by 1.4375 and add to row 2
##      [,1] [,2] [,3] [,4]
## [1,]    1    0    0    1
## [2,]    0    1    0    2
## [3,]    0    0    1    3