#!/usr/bin/env python # coding: utf-8 # # MatRepr - edge cases # In[1]: # so matrepr can be imported from the source tree. import sys sys.path.insert(0, '..') from matrepr import mdisplay import matrepr matrepr.params.width_str = 115 # Narrow enough for GitHub's nb viewer # ## SciPy # # * duplicate entries # * explicit zero # In[2]: import scipy row = [0, 0, 1, 2, 2, 3, 3, 3, 4] col = [0, 1, 0, 2, 2, 3, 3, 3, 4] val = [1, 12e34, 1e-6, 2.1, 2.2, 3.1, 3.2, 3.3, 0] A = scipy.sparse.coo_array((val, (row, col)), shape=(5, 5)) # In[3]: mdisplay(A, "html") # In[4]: mdisplay(A, "latex") # In[5]: mdisplay(A, "str") # ## Lists # # * uneven row lengths # * varying row types # * explicit zero # * contains nested lists # * contains other supported matrix types, like a SciPy matrix # * Strings elements with HTML and LaTeX control characters # * Complex numbers # * User-defined numpy dtypes # # In[6]: import numpy as np sps_mat = scipy.sparse.coo_array(([1, 2, 3, 4], ([0, 0, 1, 1], [0, 1, 0, 1])), shape=(2, 2)) dtype_a = np.dtype([("x", np.bool_), ("y", np.int64)], align=True) np_a = np.array([(False, 2)], dtype=dtype_a)[0] dtype_b = np.dtype("(3,)uint16") np_b = np.array([(1, 2, 3)], dtype=dtype_b)[0] np_2d = np.array([[11, 22], [33, 44]]) list_mat = [ (0, 12e34, 1e-6, None, 123456789), 1, [complex(1, 2), complex(123456, 0.123456)], [[1], sps_mat, [2.1, 2.2], [[1.1, 2.2], [3.3, 4.4]]], ["multiline\nstring", "", "\\begin{escape!}", {"a Python set"}], [np_a, np_b, np_2d] ] row_labels = ["sci", "single", "complex", "nested", "strings", "numpy"] col_labels = ["one", "two", "three", "four", "five"] # In[7]: mdisplay(list_mat, "html", row_labels=row_labels, col_labels=col_labels) # In[8]: mdisplay(list_mat, "latex", row_labels=row_labels, col_labels=col_labels) # In[9]: mdisplay(list_mat, "str", row_labels=row_labels, col_labels=col_labels) # In[9]: