Skip to content

Commit db8b207

Browse files
authored
Move Out MemoryLayout, MulAdd, and L/Rmul to ArrayLayouts (#77)
* Only use tiled for finite arrays, better print for Vcat * Move out code * Move macros * move _fill_lmul! * Move zero!, scalarone/zero * move factorizations * Update LazyArrays.jl * v0.14
1 parent aa84702 commit db8b207

19 files changed

+116
-1632
lines changed

Project.toml

Lines changed: 7 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -1,17 +1,20 @@
11
name = "LazyArrays"
22
uuid = "5078a376-72f3-5289-bfd5-ec5146d43c02"
3-
version = "0.13.1"
3+
version = "0.14"
4+
45

56
[deps]
7+
ArrayLayouts = "4c555306-a7a7-4459-81d9-ec55ddd5c99a"
68
FillArrays = "1a297f60-69ca-5386-bcde-b61e274b549b"
79
LinearAlgebra = "37e2e46d-f89d-539d-b4ee-838fcccc9c8e"
810
MacroTools = "1914dd2f-81c6-5fcd-8719-6d5c9610ff09"
911
StaticArrays = "90137ffa-7385-5640-81b9-e52037218182"
1012

1113
[compat]
12-
FillArrays = "0.7,0.8"
13-
MacroTools = "0.4.5,0.5"
14-
StaticArrays = "0.8,0.9,0.10,0.11,0.12"
14+
ArrayLayouts = "0.1"
15+
FillArrays = "0.8"
16+
MacroTools = "0.5"
17+
StaticArrays = "0.11, 0.12"
1518
julia = "1"
1619

1720
[extras]

src/LazyArrays.jl

Lines changed: 8 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -3,7 +3,7 @@ module LazyArrays
33
# Use README as the docstring of the module:
44
@doc read(joinpath(dirname(@__DIR__), "README.md"), String) LazyArrays
55

6-
using Base, Base.Broadcast, LinearAlgebra, FillArrays, StaticArrays
6+
using Base, Base.Broadcast, LinearAlgebra, FillArrays, StaticArrays, ArrayLayouts
77
import LinearAlgebra.BLAS
88

99
import Base: AbstractArray, AbstractMatrix, AbstractVector,
@@ -45,6 +45,12 @@ import FillArrays: AbstractFill, getindex_value
4545

4646
import StaticArrays: StaticArrayStyle
4747

48+
import ArrayLayouts: MatMulVecAdd, MatMulMatAdd, MulAdd, Lmul, Rmul, Ldiv,
49+
transposelayout, conjlayout, sublayout, triangularlayout, triangulardata,
50+
reshapedlayout, diagonallayout, adjointlayout,
51+
check_mul_axes, _mul_eltype, check_ldiv_axes, ldivaxes, colsupport, rowsupport,
52+
_fill_lmul!, @lazylmul, scalarone, scalarzero, fillzeros, zero!
53+
4854
if VERSION < v"1.2-"
4955
import Base: has_offset_axes
5056
require_one_based_indexing(A...) = !has_offset_axes(A...) || throw(ArgumentError("offset arrays are not supported but got an array with index other than 1"))
@@ -56,7 +62,7 @@ export Mul, Applied, MulArray, MulVector, MulMatrix, InvMatrix, PInvMatrix,
5662
Hcat, Vcat, Kron, BroadcastArray, BroadcastMatrix, BroadcastVector, cache, Ldiv, Inv, PInv, Diff, Cumsum,
5763
applied, materialize, materialize!, ApplyArray, ApplyMatrix, ApplyVector, apply, , @~, LazyArray
5864

59-
include("memorylayout.jl")
65+
6066
include("lazyapplying.jl")
6167
include("lazybroadcasting.jl")
6268
include("linalg/linalg.jl")

src/lazyapplying.jl

Lines changed: 2 additions & 12 deletions
Original file line numberDiff line numberDiff line change
@@ -68,7 +68,6 @@ similar(M::Applied) = similar(M, eltype(M))
6868

6969
axes(A::Applied, j) = axes(A)[j]
7070

71-
struct ApplyBroadcastStyle <: BroadcastStyle end
7271
struct ApplyArrayBroadcastStyle{N} <: Broadcast.AbstractArrayStyle{N} end
7372
ApplyArrayBroadcastStyle{N}(::Val{N}) where N = ApplyArrayBroadcastStyle{N}()
7473

@@ -87,10 +86,7 @@ BroadcastStyle(::ApplyArrayBroadcastStyle{M}, b::DefaultArrayStyle{N}) where {M,
8786
similar(bc::Broadcasted{ApplyArrayBroadcastStyle{N}}, ::Type{ElType}) where {N,ElType} =
8887
similar(Array{ElType}, axes(bc))
8988

90-
@inline function copyto!(dest::AbstractArray, bc::Broadcasted{ApplyBroadcastStyle})
91-
@assert length(bc.args) == 1
92-
copyto!(dest, first(bc.args))
93-
end
89+
9490
@inline function copyto!(dest::AbstractArray, bc::Broadcasted{ApplyArrayBroadcastStyle{N}}) where N
9591
if length(bc.args) == 1
9692
copyto!(dest, first(bc.args))
@@ -217,7 +213,7 @@ MemoryLayout(::Type{<:LazyArray}) = LazyArrayLayout()
217213

218214
transposelayout(L::LazyLayout) = L
219215
conjlayout(L::LazyLayout) = L
220-
subarraylayout(L::LazyLayout, _) = L
216+
sublayout(L::LazyLayout, _) = L
221217
reshapedlayout(::LazyLayout, _) = LazyLayout()
222218

223219
combine_mul_styles(::LazyLayout) = LazyArrayApplyStyle()
@@ -271,12 +267,6 @@ end
271267
# on the memory layout
272268
###
273269

274-
@inline sub_materialize(_, V) = Array(V)
275-
@inline sub_materialize(V::SubArray) = sub_materialize(MemoryLayout(typeof(V)), V)
276-
277-
@inline lazy_getindex(A, I...) = sub_materialize(view(A, I...))
278-
279-
280270
@inline getindex(A::LazyMatrix, kr::Colon, jr::Colon) = lazy_getindex(A, kr, jr)
281271
@inline getindex(A::LazyMatrix, kr::Colon, jr::AbstractUnitRange) = lazy_getindex(A, kr, jr)
282272
@inline getindex(A::LazyMatrix, kr::AbstractUnitRange, jr::Colon) = lazy_getindex(A, kr, jr)

src/lazyconcat.jl

Lines changed: 16 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -540,8 +540,8 @@ end
540540
# subarrays
541541
###
542542

543-
subarraylayout(::ApplyLayout{typeof(vcat)}, _) = ApplyLayout{typeof(vcat)}()
544-
subarraylayout(::ApplyLayout{typeof(hcat)}, _) = ApplyLayout{typeof(hcat)}()
543+
sublayout(::ApplyLayout{typeof(vcat)}, _) = ApplyLayout{typeof(vcat)}()
544+
sublayout(::ApplyLayout{typeof(hcat)}, _) = ApplyLayout{typeof(hcat)}()
545545

546546
arguments(::ApplyLayout{typeof(vcat)}, V::SubArray{<:Any,2,<:Any,<:Tuple{<:Slice,<:Any}}) =
547547
view.(arguments(parent(V)), Ref(:), Ref(parentindices(V)[2]))
@@ -623,13 +623,26 @@ materialize!(M::MatMulVecAdd{<:AbstractColumnMajor,<:ApplyLayout{typeof(vcat)}})
623623

624624
## print
625625

626+
_replace_in_print_matrix(A::AbstractArray, k, j, s) = replace_in_print_matrix(A, k, j, s)
627+
_replace_in_print_matrix(_, k, j, s) = s
628+
626629
function replace_in_print_matrix(f::Vcat{<:Any,1}, k::Integer, j::Integer, s::AbstractString)
627630
@assert j == 1
628631
κ = k
629632
for A in f.args
630633
n = length(A)
631-
κ  n && return replace_in_print_matrix(A, κ, 1, s)
634+
κ  n && return _replace_in_print_matrix(A, κ, 1, s)
632635
κ -= n
633636
end
634637
throw(BoundsError(f, k))
638+
end
639+
640+
function replace_in_print_matrix(f::Vcat{<:Any,2}, k::Integer, j::Integer, s::AbstractString)
641+
κ = k
642+
for A in f.args
643+
n = size(A,1)
644+
κ  n && return _replace_in_print_matrix(A, κ, j, s)
645+
κ -= n
646+
end
647+
throw(BoundsError(f, (k,j)))
635648
end

src/linalg/add.jl

Lines changed: 1 addition & 10 deletions
Original file line numberDiff line numberDiff line change
@@ -44,15 +44,6 @@ getindex(M::Add, k::Integer, j::Integer) = sum(getindex.(M.args, k, j))
4444
getindex(M::Add, k::CartesianIndex{1}) = M[convert(Int, k)]
4545
getindex(M::Add, kj::CartesianIndex{2}) = M[kj[1], kj[2]]
4646

47-
zero!(A::AbstractArray{T}) where T = fill!(A,zero(T))
48-
function zero!(A::AbstractArray{<:AbstractArray})
49-
for a in A
50-
zero!(a)
51-
end
52-
A
53-
end
54-
55-
_fill_lmul!(β, A::AbstractArray{T}) where T = iszero(β) ? zero!(A) : lmul!(β, A)
5647
for MulAdd_ in [MatMulMatAdd, MatMulVecAdd]
5748
# `MulAdd{ApplyLayout{typeof(+)}}` cannot "win" against
5849
# `MatMulMatAdd` and `MatMulVecAdd` hence `@eval`:
@@ -89,7 +80,7 @@ end
8980
_view_tuple(a, b::Tuple) = view(a, b...)
9081
for op in (:+, :-)
9182
@eval begin
92-
subarraylayout(a::ApplyLayout{typeof($op)}, _) = a
83+
sublayout(a::ApplyLayout{typeof($op)}, _) = a
9384
arguments(::ApplyLayout{typeof($op)}, a::SubArray) =
9485
_view_tuple.(arguments(parent(a)), Ref(parentindices(a)))
9586
call(::ApplyLayout{typeof($op)}, a::SubArray) = $op

src/linalg/diagonal.jl

Lines changed: 0 additions & 39 deletions
This file was deleted.

src/linalg/factorizations.jl

Lines changed: 0 additions & 49 deletions
This file was deleted.

src/linalg/ldiv.jl renamed to src/linalg/inv.jl

Lines changed: 0 additions & 71 deletions
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,3 @@
1-
2-
31
const PInv{Style, Typ} = Applied{Style, typeof(pinv), <:Tuple{Typ}}
42
const Inv{Style, Typ} = Applied{Style, typeof(inv), <:Tuple{Typ}}
53

@@ -30,18 +28,6 @@ size(A::InvOrPInv, k) = size(A)[k]
3028
axes(A::InvOrPInv, k) = axes(A)[k]
3129
eltype(A::InvOrPInv) = Base.promote_op(inv, eltype(parent(A)))
3230

33-
struct Ldiv{StyleA, StyleB, AType, BType}
34-
A::AType
35-
B::BType
36-
end
37-
38-
Ldiv{StyleA, StyleB}(A::AType, B::BType) where {StyleA,StyleB,AType,BType} =
39-
Ldiv{StyleA,StyleB,AType,BType}(A,B)
40-
41-
Ldiv(A::AType, B::BType) where {AType,BType} =
42-
Ldiv{typeof(MemoryLayout(AType)),typeof(MemoryLayout(BType)),AType,BType}(A, B)
43-
44-
struct LdivBroadcastStyle <: BroadcastStyle end
4531
struct LdivApplyStyle <: ApplyStyle end
4632

4733

@@ -52,73 +38,16 @@ ldivapplystyle(_, ::LazyLayout) = LazyArrayApplyStyle()
5238
ApplyStyle(::typeof(\), ::Type{A}, ::Type{B}) where {A<:AbstractArray,B<:AbstractArray} =
5339
ldivapplystyle(MemoryLayout(A), MemoryLayout(B))
5440

55-
size(L::Ldiv{<:Any,<:Any,<:Any,<:AbstractMatrix}) = (size(L.A, 2),size(L.B,2))
56-
size(L::Ldiv{<:Any,<:Any,<:Any,<:AbstractVector}) = (size(L.A, 2),)
57-
axes(L::Ldiv{<:Any,<:Any,<:Any,<:AbstractMatrix}) = (axes(L.A, 2),axes(L.B,2))
58-
axes(L::Ldiv{<:Any,<:Any,<:Any,<:AbstractVector}) = (axes(L.A, 2),)
59-
length(L::Ldiv{<:Any,<:Any,<:Any,<:AbstractVector}) =size(L.A, 2)
60-
61-
_ldivaxes(::Tuple{}, ::Tuple{}) = ()
62-
_ldivaxes(::Tuple{}, Bax::Tuple) = Bax
63-
_ldivaxes(::Tuple{<:Any}, ::Tuple{<:Any}) = ()
64-
_ldivaxes(::Tuple{<:Any}, Bax::Tuple{<:Any,<:Any}) = (OneTo(1),last(Bax))
65-
_ldivaxes(Aax::Tuple{<:Any,<:Any}, ::Tuple{<:Any}) = (last(Aax),)
66-
_ldivaxes(Aax::Tuple{<:Any,<:Any}, Bax::Tuple{<:Any,<:Any}) = (last(Aax),last(Bax))
67-
68-
@inline ldivaxes(A, B) = _ldivaxes(axes(A), axes(B))
6941

7042
axes(M::Applied{Style,typeof(\)}) where Style = ldivaxes(M.args...)
7143
axes(M::Applied{Style,typeof(\)}, p::Int) where Style = axes(M)[p]
7244
size(M::Applied{Style,typeof(\)}) where Style = length.(axes(M))
7345

74-
ndims(L::Ldiv) = ndims(last(L.args))
75-
eltype(M::Ldiv) = promote_type(Base.promote_op(inv, eltype(M.A)), eltype(M.B))
76-
7746
@inline eltype(M::Applied{Style,typeof(\)}) where Style = eltype(Ldiv(M.args...))
7847
@inline ndims(M::Applied{Style,typeof(\)}) where Style = ndims(last(M.args))
7948

80-
BroadcastStyle(::Type{<:Ldiv}) = ApplyBroadcastStyle()
81-
broadcastable(M::Ldiv) = M
82-
83-
similar(A::Ldiv, ::Type{T}) where T = similar(Array{T}, axes(A))
84-
similar(A::Ldiv) = similar(A, eltype(A))
85-
86-
function instantiate(L::Ldiv)
87-
check_ldiv_axes(L.A, L.B)
88-
Ldiv(instantiate(L.A), instantiate(L.B))
89-
end
90-
91-
92-
check_ldiv_axes(A, B) =
93-
axes(A,1) == axes(B,1) || throw(DimensionMismatch("First axis of A, $(axes(A,1)), and first axis of B, $(axes(B,1)) must match"))
94-
9549
check_applied_axes(A::Applied{<:Any,typeof(\)}) = check_ldiv_axes(A.args...)
9650

97-
copy(M::Ldiv) = copyto!(similar(M), M)
98-
materialize(M::Ldiv) = copy(instantiate(M))
99-
100-
101-
102-
_ldiv!(A, B) = ldiv!(factorize(A), B)
103-
_ldiv!(A::Factorization, B) = ldiv!(A, B)
104-
105-
_ldiv!(dest, A, B) = ldiv!(dest, factorize(A), B)
106-
_ldiv!(dest, A::Factorization, B) = ldiv!(dest, A, B)
107-
108-
109-
materialize!(M::Ldiv) = _ldiv!(M.A, M.B)
110-
if VERSION v"1.1-pre"
111-
copyto!(dest::AbstractArray, M::Ldiv) = _ldiv!(dest, M.A, M.B)
112-
else
113-
copyto!(dest::AbstractArray, M::Ldiv) = _ldiv!(dest, M.A, copy(M.B))
114-
end
115-
116-
const MatLdivVec{styleA, styleB, T, V} = Ldiv{styleA, styleB, <:AbstractMatrix{T}, <:AbstractVector{V}}
117-
const MatLdivMat{styleA, styleB, T, V} = Ldiv{styleA, styleB, <:AbstractMatrix{T}, <:AbstractMatrix{V}}
118-
const BlasMatLdivVec{styleA, styleB, T<:BlasFloat} = MatLdivVec{styleA, styleB, T, T}
119-
const BlasMatLdivMat{styleA, styleB, T<:BlasFloat} = MatLdivMat{styleA, styleB, T, T}
120-
121-
12251
######
12352
# PInv/Inv
12453
########

src/linalg/lazymul.jl

Lines changed: 0 additions & 9 deletions
Original file line numberDiff line numberDiff line change
@@ -84,15 +84,6 @@ macro lazymul(Typ)
8484
esc(ret)
8585
end
8686

87-
macro lazylmul(Typ)
88-
esc(quote
89-
LinearAlgebra.lmul!(A::$Typ, x::AbstractVector) = LazyArrays.materialize!(LazyArrays.Lmul(A,x))
90-
LinearAlgebra.lmul!(A::$Typ, x::AbstractMatrix) = LazyArrays.materialize!(LazyArrays.Lmul(A,x))
91-
LinearAlgebra.lmul!(A::$Typ, x::StridedVector) = LazyArrays.materialize!(LazyArrays.Lmul(A,x))
92-
LinearAlgebra.lmul!(A::$Typ, x::StridedMatrix) = LazyArrays.materialize!(LazyArrays.Lmul(A,x))
93-
end)
94-
end
95-
9687
macro lazyldiv(Typ)
9788
esc(quote
9889
LinearAlgebra.ldiv!(A::$Typ, x::AbstractVector) = LazyArrays.materialize!(LazyArrays.Ldiv(A,x))

src/linalg/linalg.jl

Lines changed: 5 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -1,10 +1,9 @@
11
include("mul.jl")
2-
include("lazymul.jl")
32
include("muladd.jl")
4-
include("lmul.jl")
5-
include("ldiv.jl")
3+
include("inv.jl")
4+
include("lazymul.jl")
65
include("add.jl")
7-
include("factorizations.jl")
86

9-
include("diagonal.jl")
10-
include("triangular.jl")
7+
8+
mulapplystyle(::TriangularLayout, ::AbstractStridedLayout) = LmulStyle()
9+
mulapplystyle(::AbstractStridedLayout, ::TriangularLayout) = RmulStyle()

0 commit comments

Comments
 (0)