Skip to content

Instantly share code, notes, and snippets.

@seanrostami
seanrostami / MATrix2LaTeX.py
Last active July 1, 2018 03:20
Converts a matrix from one of MATLAB's various slightly different formats (your own input to MATLAB, or saved to .m file, or copied/pasted from MATLAB's stdout) to AMS's LaTeX format. For details (e.g. usage), read the file's docstring.
"""This function, MATrix2LaTeX, converts any matrix in MATLAB's format(s) to AMS's LaTeX-format.
One example of the former is "
[ 0.53730, 0.33377, 0.53773; 0.34990, 0.38121, 0.16256 ]
".
Another example of the former is "
A = 0.53730 0.33377 0.53773
0.34990 0.38121 0.16256
".
@seanrostami
seanrostami / rSLZ.py
Last active June 25, 2018 13:50
rSLZ is a simple but useful tool that I used to help create examples for Linear Algebra classes that I taught over the years. In short, it generates random integer matrices with integer inverses and some measure of control over the sizes of the entries. For more details, see the docstring.
"""This is a small tool that I used myself while teaching Linear Algebra classes over the years. I frequently found myself in the following situation:
- I want a "random-looking" diagonalizable matrix with eigenvalues of my choice (including multiplicities), because I want to illustrate certain behavior in class or test certain ideas on an exam.
- But I also want the matrix to have small integer entries, and I possibly want to know eigenvectors in advance.
Fundamentally, all you need to do is conjugate (= "similarity transformation") a given diagonal matrix by a "small" integral matrix g whose inverse is also integral and "small".
A source of most, but not all, such g is GL(Z), the group of integer matrices whose determinants are either +1 or -1. The subgroup SL(Z) of those with determinant +1 is essentially the same thing, and the function rSLZ below produces random elements g of SL(Z). A typical call for classroom/exam use might be rSLZ(3,9) or rSLZ(4,5).
Besides the size of the matrix, you also provide