Last active
October 7, 2022 00:35
-
-
Save oakaigh/d8c152ce5fc3a8f34dc7b92c36f3df91 to your computer and use it in GitHub Desktop.
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
import itertools | |
import collections | |
import numpy as np | |
def isiter(a): | |
return isinstance(a, collections.abc.Iterable) | |
def iter_to_str(a, seps=itertools.repeat(' ')): | |
def _impl(_a, _sep, _nseps): | |
if not isiter(_a): | |
return str(_a) | |
curr_sep = _sep | |
next_sep = next(_nseps) | |
return curr_sep.join([ | |
_impl(_a=el, _sep=next_sep, _nseps=_nseps) | |
for el in _a | |
]) | |
return _impl(a, _sep=next(seps), _nseps=seps) | |
def matrix_to_tex(a, | |
typ='matrix', | |
prefix='', suffix='' | |
): | |
a = np.matrix(a) | |
prefix = fr'\begin{{{typ}}}{prefix}' | |
body = iter_to_str(a, seps=itertools.chain([r'\\'], itertools.repeat(r'&'))) | |
suffix = fr'{suffix}\end{{{typ}}}' | |
return fr'{prefix}{body}{suffix}' |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment