Commit 44e7082e authored by Joseph Weston's avatar Joseph Weston

Merge branch 'fixup/ipython' into 'stable-1.2'

allow 'display_family' to work even in the absence of IPython

See merge request !9
parents 82c780a2 81c8104f
Pipeline #23489 passed with stage
in 2 minutes and 46 seconds
......@@ -298,9 +298,11 @@ def hamiltonian_from_family(family, coeffs=None, nsimplify=True, tosympy=True):
def display_family(family, summed=False, coeffs=None, nsimplify=True):
"""Helper function to display a Hamiltonian family.
Supports LaTeX display through Sympy in a Jupyter notebook, which may be enabled
by running sympy.init_printing(print_builtin=True).
"""Display a Hamiltonian family in a Jupyter notebook
If this function is used from a Jupyter notebook then it uses the notebook's
rich LaTeX display features. If used from a console or script, then this
function just uses :func:`print`.
Parameters
-----------
......@@ -316,6 +318,11 @@ def display_family(family, summed=False, coeffs=None, nsimplify=True):
floating point numbers with simpler expressions, e.g. fractions.
"""
try:
from IPython.display import display
except ImportError:
display = print
if not summed:
# print each member in the family separately
for term in family:
......
......@@ -6,7 +6,7 @@ import scipy.linalg as la
from .. import kwant_rmt
from ..hamiltonian_generator import continuum_hamiltonian, check_symmetry, \
bloch_family, make_basis_pretty, constrain_family, continuum_variables, \
continuum_pairing, remove_duplicates, subtract_family
continuum_pairing, remove_duplicates, subtract_family, display_family
from ..groups import PointGroupElement, Model, time_reversal, chiral, rotation
from ..model import _commutative_momenta, Model, BlochModel
from ..linalg import nullspace, family_to_vectors
......@@ -248,3 +248,7 @@ def test_pretty_basis():
else:
assert np.allclose(mat/mat[0, 0], sz), 'Sparsification failed.'
def test_display_family_works():
family = [Model({"k_x": np.identity(2)})]
display_family(family)
Markdown is supported
0%
or
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment