Display Short Form of Large Matrix

Here is my answer:

myshallow[mat_List, dims_: {20, 20}] :=
 Module[{matrix, rows, cols, matrows, matcols, splitrow, splitcol},
  If[! And @@ IntegerQ /@ dims, 
   Return[HoldForm[myshallow[mat, dims]]]];
  If[Length[Dimensions[mat]] == 1, matrix = {mat}, matrix = mat];
  Switch[
   Length[dims],
   0,
   rows = dims; cols = 20,
   1,
   cols = dims[[1]]; rows = 20,
   2,
   {rows, cols} = dims
   ];
  {matrows, matcols} = Dimensions[matrix][[;; 2]];
  {splitrow, splitcol} = {Ceiling[rows/2], Ceiling[cols/2]};
  Which[
   matrows <= rows \[And] matcols <= cols,
   Grid[
    matrix,
    Alignment -> {Center, Center}],
   matrows <= rows \[And] matcols > cols,
   Grid[
    Table[
     Which[
      row == 1 \[And] col == splitcol + 1,
      Skeleton[matcols - cols],
      row > 1 \[And] col == splitcol + 1,
      SpanFromAbove,
      col <= splitcol,
      matrix[[row, col]],
      col >= splitcol + 2,
      matrix[[row, col - (cols + 2)]]],
     {row, matrows}, {col, cols + 1}],
    Alignment -> {Center, Center}],
   matrows > rows \[And] matcols <= cols,
   Grid[
    Table[
     Which[
      row == splitrow + 1 \[And] col == 1,
      Skeleton[matrows - rows],
      row == splitrow + 1 \[And] col > 1,
      SpanFromLeft,
      row <= splitrow,
      matrix[[row, col]],
      row >= splitrow + 2,
      matrix[[row - (rows + 2), col]]],
     {row, rows + 1}, {col, matcols}],
    Alignment -> {Center, Center}],
   matrows > rows \[And] matcols > cols,
   Grid[
    Table[
     Which[
      row <= splitrow \[And] col <= splitcol,
      matrix[[row, col]],
      row == 1 \[And] col == splitcol + 1,
      Skeleton[matcols - cols],
      row <= splitrow \[And] col == splitcol + 1,
      SpanFromAbove,
      row <= splitrow \[And] col >= splitcol + 2,
      matrix[[row, col - (cols + 2)]],
      row == splitrow + 1 \[And] col == 1,
      Skeleton[matrows - rows],
      row == splitrow + 1 \[And] col <= splitcol,
      SpanFromLeft,
      row == splitrow + 1 \[And] col == splitcol + 1,
      "",
      row == splitrow + 1 \[And] col == splitcol + 2,
      Skeleton[matrows - rows],
      row == splitrow + 1 \[And] col > splitcol + 2,
      SpanFromLeft,
      row >= splitrow + 2 \[And] col <= splitcol,
      matrix[[row - (rows + 2), col]],
      row == splitrow + 2 \[And] col == splitcol + 1,
      Skeleton[matcols - cols],
      row > splitrow + 2 \[And] col == splitcol + 1,
      SpanFromAbove,
      row >= splitrow + 2 \[And] col >= splitcol + 2,
      matrix[[row - (rows + 2), col - (cols + 2)]]],
     {row, rows + 1}, {col, cols + 1}],
    Alignment -> {Center, Center}]
   ]
  ]

Essentially, I take any 1D or higher list followed by an optional number of dimensions. Based on other Mathematica functions, if you input 5 for the dimensions it specifies the number of rows and {5} specifies 5 columns. My plan is to place it in $UserBaseDirectory/Kernel/init.m to make it available for every session.

With the following test cases:

matrixhuge = Partition[Range[5*10^6], 1000];
matrixsmall = Partition[Range[25], 5];
matrixwide = Partition[Range[1000], 100];
matrixlong = Partition[Range[1000], 2];
matrixhuge // myshallow
matrixsmall // myshallow
matrixwide // myshallow
matrixlong // myshallow

I get the following:

Picture of output short matrices.


Short/@Partition[Range[10^6], 1000]

Shallow/@Partition[Range[10^6], 1000]

Tags:

Matrix

Display