@@ -9,9 +9,9 @@ using BlockSparseArrays:
9
9
using LinearAlgebra: Adjoint
10
10
using TypeParameterAccessors: similartype, unwrap_array_type
11
11
12
- const GradedArray{T,M,A, Blocks, Axes} = BlockSparseArray{
13
- T,M ,A,Blocks,Axes
14
- } where {Axes <: Tuple{AbstractGradedUnitRange,Vararg{AbstractGradedUnitRange}} }
12
+ const GradedArray{T,N,A <: AbstractArray{T,N} , Blocks<: AbstractArray{A,N} , Axes<: Tuple{AbstractGradedUnitRange{<:Integer},Vararg{AbstractGradedUnitRange{<:Integer}}} } = BlockSparseArray{
13
+ T,N ,A,Blocks,Axes
14
+ }
15
15
const GradedMatrix{T,A,Blocks,Axes} = GradedArray{
16
16
T,2 ,A,Blocks,Axes
17
17
} where {Axes<: Tuple{AbstractGradedUnitRange,AbstractGradedUnitRange} }
@@ -119,3 +119,77 @@ function Base.getindex(
119
119
end
120
120
121
121
ungrade (a:: GradedArray ) = sparsemortar (blocks (a), ungrade .(axes (a)))
122
+
123
+ # Copy of `Base.dims2string` defined in `show.jl`.
124
+ function dims_to_string (d)
125
+ isempty (d) && return " 0-dimensional"
126
+ length (d) == 1 && return " $(d[1 ]) -element"
127
+ return join (map (string, d), ' ×' )
128
+ end
129
+
130
+ # Copy of `BlockArrays.block2string` from `BlockArrays.jl`.
131
+ block_to_string (b, s) = string (join (map (string, b), ' ×' ), " -blocked " , dims_to_string (s))
132
+
133
+ using TypeParameterAccessors: type_parameters, unspecify_type_parameters
134
+ function base_type_and_params (type:: Type )
135
+ alias = Base. make_typealias (type)
136
+ base_type, params = if isnothing (alias)
137
+ unspecify_type_parameters (type), type_parameters (type)
138
+ else
139
+ base_type_globalref, params_svec = alias
140
+ base_type_globalref. name, params_svec
141
+ end
142
+ return base_type, params
143
+ end
144
+
145
+ function base_type_and_params (type:: Type{<:GradedArray} )
146
+ return :GradedArray , type_parameters (type)
147
+ end
148
+ function base_type_and_params (type:: Type{<:GradedVector} )
149
+ params = type_parameters (type)
150
+ params′ = [params[1 : 1 ]. .. , params[3 : end ]. .. ]
151
+ return :GradedVector , params′
152
+ end
153
+ function base_type_and_params (type:: Type{<:GradedMatrix} )
154
+ params = type_parameters (type)
155
+ params′ = [params[1 : 1 ]. .. , params[3 : end ]. .. ]
156
+ return :GradedMatrix , params′
157
+ end
158
+
159
+ # Modified version of `BlockSparseArrays.concretetype_to_string_truncated`.
160
+ # This accounts for the fact that the GradedArray alias is not defined in
161
+ # BlockSparseArrays so for the sake of printing, Julia doesn't show it as
162
+ # an alias: https://github.com/JuliaLang/julia/issues/40448
163
+ function concretetype_to_string_truncated (type:: Type ; param_truncation_length= typemax (Int))
164
+ isconcretetype (type) || throw (ArgumentError (" Type must be concrete." ))
165
+ base_type, params = base_type_and_params (type)
166
+ str = string (base_type)
167
+ if isempty (params)
168
+ return str
169
+ end
170
+ str *= ' {'
171
+ param_strings = map (params) do param
172
+ param_string = string (param)
173
+ if length (param_string) > param_truncation_length
174
+ return " …"
175
+ end
176
+ return param_string
177
+ end
178
+ str *= join (param_strings, " , " )
179
+ str *= ' }'
180
+ return str
181
+ end
182
+
183
+ using BlockArrays: blocksize
184
+ function Base. summary (io:: IO , a:: GradedArray )
185
+ print (io, block_to_string (blocksize (a), size (a)))
186
+ print (io, ' ' )
187
+ print (io, concretetype_to_string_truncated (typeof (a); param_truncation_length= 40 ))
188
+ return nothing
189
+ end
190
+
191
+ function Base. showarg (io:: IO , a:: GradedArray , toplevel:: Bool )
192
+ ! toplevel && print (io, " ::" )
193
+ print (io, concretetype_to_string_truncated (typeof (a); param_truncation_length= 40 ))
194
+ return nothing
195
+ end
0 commit comments