Skip to content

Commit 67e9965

Browse files
authored
sparse printing for triu (#74)
* sparse printing for triu * LazyArrayStyle for LazyLayout
1 parent a38a305 commit 67e9965

File tree

4 files changed

+33
-7
lines changed

4 files changed

+33
-7
lines changed

src/LazyArrays.jl

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -30,7 +30,7 @@ import Base: AbstractArray, AbstractMatrix, AbstractVector,
3030
AbstractArray, AbstractVector, axes, (:), _sub2ind_recurse, broadcast, promote_eltypeof,
3131
similar, @_gc_preserve_end, @_gc_preserve_begin,
3232
@nexprs, @ncall, @ntuple, tuple_type_tail,
33-
all, any, isbitsunion, issubset
33+
all, any, isbitsunion, issubset, replace_in_print_matrix, replace_with_centered_mark
3434

3535
import Base.Broadcast: BroadcastStyle, AbstractArrayStyle, Broadcasted, broadcasted,
3636
combine_eltypes, DefaultArrayStyle, instantiate, materialize,

src/cache.jl

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -151,8 +151,8 @@ colsupport(A::CachedVector, i) =
151151
rowsupport(A::CachedMatrix, i) =
152152
minimum(i)  A.datasize[1] ? convexunion(rowsupport(A.array, i),rowsupport(A.data,i) Base.OneTo(A.datasize[2])) : rowsupport(A.array, i)
153153

154-
Base.replace_in_print_matrix(A::CachedMatrix, i::Integer, j::Integer, s::AbstractString) =
155-
i in colsupport(A,j) ? s : Base.replace_with_centered_mark(s)
154+
replace_in_print_matrix(A::CachedMatrix, i::Integer, j::Integer, s::AbstractString) =
155+
i in colsupport(A,j) ? s : replace_with_centered_mark(s)
156156

157157

158158
###

src/lazyapplying.jl

Lines changed: 16 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -243,11 +243,12 @@ function show(io::IO, A::Applied)
243243
print(io, ')')
244244
end
245245

246-
applybroadcaststyle(_1, _2) = DefaultArrayStyle{2}()
246+
applybroadcaststyle(::Type{<:AbstractArray{<:Any,N}}, _2) where N = DefaultArrayStyle{N}()
247+
applybroadcaststyle(::Type{<:AbstractArray{<:Any,N}}, ::LazyLayout) where N = LazyArrayStyle{N}()
247248
BroadcastStyle(M::Type{<:ApplyArray}) = applybroadcaststyle(M, MemoryLayout(M))
248249

249-
Base.replace_in_print_matrix(A::LazyMatrix, i::Integer, j::Integer, s::AbstractString) =
250-
i in colsupport(A,j) ? s : Base.replace_with_centered_mark(s)
250+
replace_in_print_matrix(A::LazyMatrix, i::Integer, j::Integer, s::AbstractString) =
251+
i in colsupport(A,j) ? s : replace_with_centered_mark(s)
251252

252253
###
253254
# Number special cases
@@ -300,4 +301,15 @@ for tri in (:tril, :triu)
300301
ndims(::Applied{<:Any,typeof($tri)}) = 2
301302
eltype(A::Applied{<:Any,typeof($tri)}) = eltype(first(A.args))
302303
end
303-
end
304+
end
305+
306+
getindex(A::ApplyMatrix{T,typeof(triu),<:Tuple{<:AbstractMatrix}}, k::Integer, j::Integer) where T =
307+
j  k ? A.args[1][k,j] : zero(T)
308+
309+
getindex(A::ApplyMatrix{T,typeof(triu),<:Tuple{<:AbstractMatrix,<:Integer}}, k::Integer, j::Integer) where T =
310+
j  k+A.args[2] ? A.args[1][k,j] : zero(T)
311+
312+
replace_in_print_matrix(A::ApplyMatrix{<:Any,typeof(triu),<:Tuple{<:AbstractMatrix}}, i::Integer, j::Integer, s::AbstractString) =
313+
j  i ? replace_in_print_matrix(A.args[1], i, j, s) : replace_with_centered_mark(s)
314+
replace_in_print_matrix(A::ApplyMatrix{<:Any,typeof(triu),<:Tuple{<:AbstractMatrix,<:Integer}}, i::Integer, j::Integer, s::AbstractString) =
315+
j  i+A.args[2] ? replace_in_print_matrix(A.args[1], i, j, s) : replace_with_centered_mark(s)

src/lazyconcat.jl

Lines changed: 14 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -619,3 +619,17 @@ materialize!(M::MatMulMatAdd{<:AbstractColumnMajor,<:ApplyLayout{typeof(vcat)}})
619619
materialize!(MulAdd(M.α,M.A,Array(M.B),M.β,M.C))
620620
materialize!(M::MatMulVecAdd{<:AbstractColumnMajor,<:ApplyLayout{typeof(vcat)}}) =
621621
materialize!(MulAdd(M.α,M.A,Array(M.B),M.β,M.C))
622+
623+
624+
## print
625+
626+
function replace_in_print_matrix(f::Vcat{<:Any,1}, k::Integer, j::Integer, s::AbstractString)
627+
@assert j == 1
628+
κ = k
629+
for A in f.args
630+
n = length(A)
631+
κ  n && return replace_in_print_matrix(A, κ, 1, s)
632+
κ -= n
633+
end
634+
throw(BoundsError(f, k))
635+
end

0 commit comments

Comments
 (0)