@@ -172,55 +172,50 @@ end
172172
173173Base. show (io:: IO , S:: SparseMatrixCSC ) = Base. show (convert (IOContext, io), S:: SparseMatrixCSC )
174174function Base. show (io:: IOContext , S:: SparseMatrixCSC )
175- if nnz (S) == 0
176- return show (io, MIME (" text/plain" ), S)
177- end
178- limit:: Bool = get (io, :limit , false )
179- rows = displaysize (io)[1 ] - 4 # -4 from [Prompt, header, newline after elements, new prompt]
180- will_fit = ! limit || rows >= nnz (S) # Will the whole matrix fit when printed?
181-
182- if rows <= 2 && ! will_fit
183- print (io, " \n \u 22ee" )
184- return
185- end
175+ nnz (S) == 0 && return show (io, MIME (" text/plain" ), S)
186176
187177 ioc = IOContext (io, :compact => true )
188- pad = ndigits (max (S. m, S. n))
189-
190- function _format_line (r, col)
191- print (ioc, " \n [" , rpad (S. rowval[r], pad), " , " , lpad (col, pad), " ] = " )
178+ function _format_line (r, col, padr, padc)
179+ print (ioc, " \n [" , rpad (S. rowval[r], padr), " , " , lpad (col, padc), " ] = " )
192180 if isassigned (S. nzval, Int (r))
193181 show (ioc, S. nzval[r])
194182 else
195183 print (ioc, Base. undef_ref_str)
196184 end
197185 end
198186
199- if will_fit
200- print_count = nnz (S)
201- else
202- print_count = div (rows- 1 , 2 )
203- end
204-
205- count = 0
206- for col = 1 : S. n, r = nzrange (S, col)
207- count += 1
208- _format_line (r, col)
209- count == print_count && break
187+ function _get_cols (from, to)
188+ idx = eltype (S. colptr)[]
189+ c = searchsortedlast (S. colptr, from)
190+ for i = from: to
191+ while i == S. colptr[c+ 1 ]
192+ c += 1
193+ end
194+ push! (idx, c)
195+ end
196+ idx
210197 end
211198
212- if ! will_fit
199+ rows = displaysize (io)[1 ] - 4 # -4 from [Prompt, header, newline after elements, new prompt]
200+ if ! get (io, :limit , false ) || rows >= nnz (S) # Will the whole matrix fit when printed?
201+ cols = _get_cols (1 , nnz (S))
202+ padr, padc = ndigits .((maximum (S. rowval[1 : nnz (S)]), cols[end ]))
203+ _format_line .(1 : nnz (S), cols, padr, padc)
204+ else
205+ if rows <= 2
206+ print (io, " \n \u 22ee" )
207+ return
208+ end
209+ s1, e1 = 1 , div (rows - 1 , 2 ) # -1 accounts for \vdots
210+ s2, e2 = nnz (S) - (rows - 1 - e1) + 1 , nnz (S)
211+ cols1, cols2 = _get_cols (s1, e1), _get_cols (s2, e2)
212+ padr = ndigits (max (maximum (S. rowval[s1: e1]), maximum (S. rowval[s2: e2])))
213+ padc = ndigits (cols2[end ])
214+ _format_line .(s1: e1, cols1, padr, padc)
213215 print (io, " \n \u 22ee" )
214- # find the column to start printing in for the last print_count elements
215- nextcol = searchsortedfirst (S. colptr, nnz (S) - print_count + 1 )
216- for r = (nnz (S) - print_count + 1 ) : (S. colptr[nextcol] - 1 )
217- _format_line (r, nextcol - 1 )
218- end
219- # print all of the remaining columns
220- for col = nextcol: S. n, r = nzrange (S, col)
221- _format_line (r, col)
222- end
216+ _format_line .(s2: e2, cols2, padr, padc)
223217 end
218+ return
224219end
225220
226221# # Reshape
0 commit comments