In [1]:
import sys

sys.path.append("../../code")
from init_mooc_nb import *

init_notebook()

from IPython.display import HTML
from nbconvert.filters.markdown import markdown2html_pandoc

displaymd = lambda markdown: display_html(HTML(markdown2html_pandoc(markdown)))

# Markdown tables are ugly, and Mathjax doesn't support \tabular,
# therefore we use math mode + \array + add a command \T to make
# the \array rows less dense.

table_header = r"""$$\require{color} colordefs \newcommand\T{\Rule{0pt}{1em}{.3em}} \begin{array}{fmt} \hline body\\ \hline \end{array}$$"""

replacements = [
("{", "{{"),
("}", "}}"),
("colordefs", "{colordefs}"),
("fmt", "{fmt}"),
("body", "{body}"),
]
for i, j in replacements:

# Symmetry classes names and their symmetry properties
symmetry_classes = ("A", "AIII", "AI", "BDI", "D", "DIII", "AII", "CII", "C", "CI")
chiralsym = 5 * ("", "1")
phs = 3 * ("",) + 3 * ("1",) + ("",) + 3 * ("-1",)
trs = 2 * ("",) + 2 * ("1",) + ("",) + 3 * ("-1",) + ("", "1")

# Locations of non-empty entries in the periodic table
primary_seq = lambda n: np.arange(n) % 2
z_descendant = lambda n: np.arange(n) % 8
z2_descendant = lambda n: np.arange(1, n + 1) % 8
z2_descendant2 = lambda n: np.arange(2, n + 2) % 8
twoz_descendant = lambda n: np.arange(4, n + 4) % 8

line_end = "\\T\\\\\n"
sep = " & "

def make_table(n=4, show_symmetries=True, sort_order=None):
"""Create an array of entries forming the periodic table.

Parameters:
-----------
n : int
Number of dimensions to show.
show_symmetries : bool
Show symmetry information for each symmetry class.
sort_order : int array or None
Ordering to apply to the symmetry classes
(can be trivially used to discard entries).

Returns:
--------
data : np.ndarray
A string array with the entries of the periodic table.
format_string : str
An alignment string that can be used to feed the resulting
table to a Latex \array environment.
"""

dimensions = np.array([[str(i) for i in range(n)]], dtype="S100")
if dimensions.shape[1]:
dimensions[0, 0] = r"d=" + dimensions[0, 0].decode("UTF-8")

complex_entries = np.zeros((2, n), dtype="S100")
complex_entries[primary_seq(n), np.arange(n)] = r"\mathbb{Z}"

real_entries = np.zeros((8, n), dtype="S100")
real_entries[z_descendant(n), np.arange(n)] = r"\mathbb{Z}"
real_entries[z2_descendant(n), np.arange(n)] = r"\mathbb{Z}_2"
real_entries[z2_descendant2(n), np.arange(n)] = r"\mathbb{Z}_2"
real_entries[twoz_descendant(n), np.arange(n)] = r"2\mathbb{Z}"

entries = np.r_[complex_entries, real_entries]

sym_classes_rm = tuple(r"\textrm{{{}}}".format(cl) for cl in symmetry_classes)

sym = np.array(
[sym_classes_rm] + show_symmetries * [chiralsym, phs, trs], dtype="S100"
).T
[
[r"\textrm{class}"]
+ show_symmetries * [r"\mathcal{C}", r"\mathcal{P}", r"\mathcal{T}"]
],
dtype="S100",
)

table = np.c_[sym, entries]
if sort_order is not None:
table = table[sort_order]

format_string = "c|" + show_symmetries * "rrr" + n * show_symmetries * "|" + n * "c"
return np.r_[header, table], format_string

def color_table(table, color_array):
"""Apply rgb colors to table entries.

color_array[i, j] is the rgb color of the entry (i, j).

Returns the string of color definitions required for coloring the table.
"""
apply_color = lambda text, color: r"\color{{{}}}{{{}}}".format(color, text)

colors = {}
for idx in np.indices(table.shape).reshape(2, -1).T:
idx = tuple(idx)
if not any(color_array[idx]):
pass
color = ",".join("{:1.2}".format(i) for i in color_array[idx])
val = str(abs(hash(color)))[:8]
colors[color] = val
table[idx] = apply_color(table[idx].decode("utf-8"), val)

defs = []
for color, code in list(colors.items()):
defs.append(r"\definecolor{{{}}}{{rgb}}{{{}}}".format(code, color))
return "\n".join(defs)

Populated the namespace with:
np, matplotlib, kwant, holoviews, init_notebook, SimpleNamespace, pprint_matrix, scientific_number, pretty_fmt_complex, plt, pf, display_html
from code/edx_components:
MoocVideo, MoocDiscussion, MoocCheckboxesAssessment, MoocMultipleChoiceAssessment, MoocSelfAssessment
from code/functions:
spectrum, hamiltonian_array, h_k, pauli
Using kwant 1.4.2 and holoviews 1.13.2
Executed on 2021-03-06 at 14:55:28.874012.