S Lazy-H
  • Home
  • About
  • Posts
  • Contact
  • Slide Rules
  • A Biker’s Tale

Fun R Functions

R Programming
mathematics
Author

Sam Hutchins

Published

October 9, 2023

This is something I have never done. And that is explore some of the commands/functions in several packages in the R programming language. Even though I have used R for quite a while in a casual way, I only scratched the surface with commands and functions I needed at the time.

Recently, when exploring Quantum Computation and Quantum Information through various sources, primarily the Internet and more recently Nielsen and Chuang (2010), I was delving into matrices and related functions, and stumbled across some useful functions. The functions noted here is not meant to be exhaustive, nor are the descriptions meant to replicate any help() available for these functions.

In earlier posts, I found where it was useful for determining Eigenvectors and Eigenvalues. To put it mildly, I was struggling to make those determinations. It turns out that with the base::eigen function, it’s easy. Also, there is the matlib::Eigen and the pracma::eig functions also. One thing I’ve noticed is where eigen(x) gives -/+ vectors, Eigen(x) gives +/- vectors, meaning although both can give the same digits, the sign of each value is reversed between the two.

\[\left[ \begin{array}{rrr} -0.25 & 0.00 & 0.97 \\ -0.54 & -0.83 & -0.14 \\ -0.81 & 0.55 & -0.21 \\ \end{array} \right] \quad vs. \quad \left[ \begin{array}{rrr} 0.25 & -0.00 & -0.97 \\ 0.54 & 0.83 & 0.14 \\ 0.81 & -0.55 & 0.21 \\ \end{array} \right]\]

For just Eigenvalues, the eig() function does only the values, and uses eigen().

The function I used to code the latex/MathJax matrices displayed just above was the matlib::matrix2latex function. By using that, I didn’t need to manually type the latex code directly, thus saving the possibility of having to edit the code to get it right.

Another useful function is matlib::echelon which, while not directly related to quantum calculations, is very useful in electronics for mesh network calculations or simultaneous equations. Essentially, it performs a reduced row echelon format (RREF) of the matrix, similar to the Solve() function in the same package. However, where Solve() gives only the solution, echelon() displays the entire matrix and is more instructive. For example, where we have the (left) matrix and want to add the vector to make it augmented, we could use cbind(x,y) to join the two, then use echelon() to determine the RREF,

\[\left[ \begin{array}{rrr} 1.00 & 2.00 & 3.00 \\ 2.00 & 5.00 & 6.00 \\ 3.00 & 6.00 & 10.00 \\ \end{array}\right] \left[ \begin{array}{r} 3.00 \\ 5.00 \\ 7.00 \\ \end{array}\right] \Longrightarrow \left[ \begin{array}{rrrr} 1.00 & 0.00 & 0.00 & 11.00 \\ 0.00 & 1.00 & 0.00 & -1.00 \\ -0.00 & -0.00 & 1.00 & -2.00 \\ \end{array}\right]\]

Now for some eye candy, where we can visually see the matrix displayed in 3D,

Augmented matrix in 3D

That wasn’t as clear as I was hoping; it needs blowing up to really see the values. The function plotEqn3d() allows rotation and zooming to get the full effect.

There are so many functions for manipulating matrices, it’s difficult knowing which is best, easier, more efficient or faster. This is just a smattering I have discovered. But, again, I am just a casual dabbler in R anyway, so may discover many more ways to do things. Hopefully, to make my aspirations to learn more about Quantum Computation and Quantum Information.

So, that’s all for this short post. May God Bless you and yours. If you haven’t accepted Jesus as your Lord and Savior, time’s getting short! Don’t wait until it’s too late!


References

Nielsen, Michael A., and Isaac L. Chuang. 2010. Quantum Computation and Quantum Information: 10th Anniversary Edition. Cambridge University Press. https://doi.org/10.1017/CBO9780511976667.
© S Lazy-H 2019 -