In the following, we give a brief introduction to finding the reduced row echelon form of a matrix using Linear Algebra Toolkit, R, Octave and SageMath. This document is originally written in Japanese as a feedback to Homework 1 of Linear Algebra I that I taught at International Christian University in Autumn, 2017.
Linear Algebra Toolkit is an educational tool on the web. Please visit Linear Algebra Toolkit. No explanation is necessary, I believe.
The following is taken from David Lay’s “Linear Algebra and its Application” Exercise 1.6.4.
M for Mining, L for Lumber, E for Energy and T for Transportation.
\[ \begin{array}{ccccc} \hline\hline \mbox{Distribution of Output}\\ \hline \mbox{Mining} & \mbox{Lumber} & \mbox{Energy} & \mbox{Transportation} & \mbox{Purchased by}\\ .30 & .15 & .20 & .20 & \mbox{Mining}\\ .10 & .15 & .15 & .10 & \mbox{Lumber}\\ .60 & .50 & .45 & .50 & \mbox{Energy}\\ 0 & .20 & .20 & .20 & \mbox{Transportation}\\ \hline\hline \end{array} \] Now the equation of computing equilibrium yields as follows. \[ \left\{\begin{array}{ccl} p_{M} & = & 0.30 p_{M} + 0.15 p_{L} + 0.20 p_{E} + 0.20 p_{T}\\ p_{L} & = & 0.10 p_{M} + 0.15 p_{L} + 0.15 p_{E} + 0.10 p_{T}\\ p_{E} & = & 0.60 p_{M} + 0.50 p_{L} + 0.45 p_{E} + 0.50 p_{T}\\ p_{T} & = & 0.00 p_{M} + 0.20 p_{L} + 0.20 p_{E} + 0.20 p_{T} \end{array} \right. \] Since Linear Algebra Toolkit can handle only fractions, we convert the equations in the following way and use “Solving a linear system of equations”. \[ \left\{\begin{array}{ccl} 0 & = & 70 p_{M} - 15 p_{L} - 20 p_{E} - 20 p_{T}\\ 0 & = & -10 p_{M} + 85 p_{L} - 15 p_{E} - 10 p_{T}\\ 0 & = & -60 p_{M} - 50 p_{L} + 55 p_{E} - 50 p_{T}\\ 0 & = & 0 p_{M} - 20 p_{L} - 20 p_{E} + 80 p_{T} \end{array} \right. \] You will see the augmented matrix of the system of linear equations. If you choose the sequence of elementary row operations, then you will find the solution. \[ p_{M} = \frac{193}{141}p_{T},\quad p_{L} = \frac{118}{141}p_{T}, \quad p_{E} = \frac{446}{141}p_{T}, \quad p_{T}: \mbox{ arbitrary } \] Unknowns are \(x_{1}, x_{2}, x_{3}, x_{4}\). It is touch if you compute by hand. Free vaiables are written as arbitrary.
“Row operation calculator” is also available. You can perform each step of the row reduction by Elementary Row Operations using pull-down menu. You will see what “Transforming a matrix to row echelon form” and “Transforming a matrix to reduced row echelon form” will do. “Row operation calculator” is handy and I use only this in this site. I often check the solution to exercises and problems in Final Exam using it.
R is a free software environment for statistical computing and graphics. You can download the software from the site: R-Project. I use it with RStudio. RStudio Window is divided into four panes and very convenient. I am using Free RStudio Desktop downloaded from Rstudio.
If you want to try online, it is possible. However, there are huge number of packages for R and it is not allowed to install packages for your own in online sites. There is a site to install packages for your own with some restriction.
There is an R package called Rmarkdown that can be used with R and RSudio. With Rmarkdown, you can write a document with r-codes. This document is typeset by Rmarkdown and output is in HTML format for web browser. You can also get an MS Word file as an output.
I also use TeX to typeset math equations in Rmarkdown. Most of the papers in Mathematics, Computer Science, Theoretical Physics, Economics and Lingustics are written by LaTeX that is an extended version of TeX. All of my lecturenotes and slides are written by LaTeX.
At ICU, there is a support site of TeX TeX_Support and a support site of R R_Support in Moodle. Both sites are set to allow guest log-in without keys, and the keys are “tex” and “r”, respectively.
Since tables and matrices have the same structure, R can handle matrices perfectly. However, in order to calculate Reduced Row Echelon Forms we need “pracma” or other packages unless you write your own program in r language.
First install the package by the following command.
install.packages("pracma")
When installed, you call the package by the following.
library("pracma")
There are several ways to input matrices.
A<-matrix(
c(-0.70,0.15,0.20,0.20,0.00,
0.10,-0.85,0.15,0.10,0.00,
0.60,0.50,-0.55,0.50,0.00,
0.00,0.20,0.20,-0.80,0.00),
nrow=4,ncol=5,byrow=TRUE)
A
## [,1] [,2] [,3] [,4] [,5]
## [1,] -0.7 0.15 0.20 0.2 0
## [2,] 0.1 -0.85 0.15 0.1 0
## [3,] 0.6 0.50 -0.55 0.5 0
## [4,] 0.0 0.20 0.20 -0.8 0
The parameter “nrow” is “the number of rows”, and “ncol” for “the number of columns”. You can also write 4,5 in place of “nrow=4,ncol=5” but it is also good to have longer form to clarify the meaning of parameters.
The part “c( )” is the data column. Since the data is read vertically in many cases, you need
“byrow=TRUE”. Otherwise you will have a different array of numbers. The command A calls the matrix \(A\) and print (display) it out. The next command is “rref(A)”, which is to find the reduced row echelon form of the matrix \(A\).
rref(A)
## [,1] [,2] [,3] [,4] [,5]
## [1,] 1 0 0 -1.3687943 0
## [2,] 0 1 0 -0.8368794 0
## [3,] 0 0 1 -3.1631206 0
## [4,] 0 0 0 0.0000000 0
Gnu Octave is a MatLab compatible Free software and you can download from the site Gnu Octave. If you want to try online, Sage Cell is handy. I also found Octave-Online. I will use SageCell to give a brief explanation.
The following is the example given above. Please note that the format of matrices are different from that of R.
A=[
-0.70,0.15,0.20,0.20,0.00;
0.10,-0.85,0.15,0.10,0.00;
0.60,0.50,-0.55,0.50,0.00;
0.00,0.20,0.20,-0.80,0.00
]
rref(A)
Do you know Mathematica or Maple? In SageMath the following is written.
Mission: Creating a viable free open source alternative to Magma, Maple, Mathematica and Matlab.
Now SageMathCloud has a new name CoCalc. You can use Jupyter = Julia + Python + R, LaTeX Editor and much more in CoCalc. Since it allows collaboration, I use it for senior seminars and collaborations in research. You can download SageMath and it is convenient to install it in you computer. But I use Cocalc, online cloud computing, most of the time.
You can use R and Octave in CoCalc. Please read the manual.
You need to make your own account in CoCalc. Here, I give an example using SageCell introduced above.
There are several ways to input matrices in SageMath. For the detail, please check the manual.
Input the following in SageCell.
a=matrix(QQ,[
[-0.70,0.15,0.20,0.20,0.00],
[0.10,-0.85,0.15,0.10,0.00],
[0.60,0.50,-0.55,0.50,0.00],
[0.00,0.20,0.20,-0.80,0.00]
])
an=matrix([
[-0.70,0.15,0.20,0.20,0.00],
[0.10,-0.85,0.15,0.10,0.00],
[0.60,0.50,-0.55,0.50,0.00],
[0.00,0.20,0.20,-0.80,0.00]
])
print a.rref()
print an.rref()
Do you see something odd? Try also
a.rref().n(digits=3)
an.rref().n(digits=3)
You can find Japanese explanation in my homepage 日本語による SageMath 紹介の頁:リンク. You will find useful links there.
I introduced Linear Algebra Toolkit, R, Octave, Sagemath. I recommend you to try. It is nice if you can use at least one. There are various tools on the web and mostly a cloud based and you can find smart phone applications for Octave and SageCell.