Monday, November 13, 2017

Exporting Numpy Arrays and Matrices to LaTeX

Over the past couple of years, a lot of my "numerical experimentation" work has moved from Octave to python/numpy.

I incorporate a lot of this work into my classes and presentations (made using beamer), and having a script to translate vectors and matrices to LaTeX format is handy.

In the past, I shared a Matlab/Octave script which does this.

Here is a python/numpy script which does something similar. The script

  • autodetects integers and floats
  • allows you to control the number of decimals for floats
  • allows you optionally render floats in scientific format
  • right-justify using the bmatrix* environment (good for -ve numbers)
  • suppress small values near zero (~ 1e-16)

4 comments:

Anonymous said...

The link for the python script is the same as the link for the Matlab/Octave script

Sachin Shanbhag said...

Thanks for pointing that out. I fixed the link.

Anonymous said...

I ran into trouble where large matrices would not be converted correctly because of extra line breaks.
Adding
np.set_printoptions(linewidth=np.inf)
before line 76 fixed the problem.

Sachin Shanbhag said...

Thanks for the pointer!