Skip to content

Commit

Permalink
ArrayLayouts 0.2 (#99)
Browse files Browse the repository at this point in the history
* ArrayLayouts 0.2

* v0.16

* Update runtests.jl

* Update .travis.yml
  • Loading branch information
dlfivefifty authored Mar 27, 2020
1 parent 66b3a40 commit 224f504
Show file tree
Hide file tree
Showing 9 changed files with 38 additions and 59 deletions.
3 changes: 0 additions & 3 deletions .travis.yml
Original file line number Diff line number Diff line change
Expand Up @@ -5,9 +5,6 @@ os:
- osx
julia:
- 1.0
- 1.1
- 1.2
- 1.3
- 1.4
- nightly
matrix:
Expand Down
4 changes: 2 additions & 2 deletions Project.toml
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
name = "LazyArrays"
uuid = "5078a376-72f3-5289-bfd5-ec5146d43c02"
version = "0.15.1"
version = "0.16"

[deps]
ArrayLayouts = "4c555306-a7a7-4459-81d9-ec55ddd5c99a"
Expand All @@ -10,7 +10,7 @@ MacroTools = "1914dd2f-81c6-5fcd-8719-6d5c9610ff09"
StaticArrays = "90137ffa-7385-5640-81b9-e52037218182"

[compat]
ArrayLayouts = "0.1"
ArrayLayouts = "0.2"
FillArrays = "0.8"
MacroTools = "0.5"
StaticArrays = "0.11, 0.12"
Expand Down
4 changes: 2 additions & 2 deletions src/LazyArrays.jl
Original file line number Diff line number Diff line change
Expand Up @@ -37,7 +37,7 @@ import Base.Broadcast: BroadcastStyle, AbstractArrayStyle, Broadcasted, broadcas
materialize!, eltypes

import LinearAlgebra: AbstractTriangular, AbstractQ, checksquare, pinv, fill!, tilebufsize, Abuf, Bbuf, Cbuf, dot, factorize, qr, lu, cholesky,
norm2, norm1, normInf, normp, normMinusInf, det, tr, AdjOrTrans
norm2, norm1, normInf, normp, normMinusInf, det, tr, AdjOrTrans, triu, tril

import LinearAlgebra.BLAS: BlasFloat, BlasReal, BlasComplex

Expand All @@ -49,7 +49,7 @@ import ArrayLayouts: MatMulVecAdd, MatMulMatAdd, MulAdd, Lmul, Rmul, Ldiv,
transposelayout, conjlayout, sublayout, triangularlayout, triangulardata,
reshapedlayout, diagonallayout, adjointlayout, sub_materialize,
check_mul_axes, _mul_eltype, check_ldiv_axes, ldivaxes, colsupport, rowsupport,
_fill_lmul!, @lazylmul, scalarone, scalarzero, fillzeros, zero!, lazy_getindex
_fill_lmul!, scalarone, scalarzero, fillzeros, zero!, layout_getindex

if VERSION < v"1.2-"
import Base: has_offset_axes
Expand Down
2 changes: 1 addition & 1 deletion src/cache.jl
Original file line number Diff line number Diff line change
Expand Up @@ -73,7 +73,7 @@ function getindex(A::CachedArray, I...)
A.data[I...]
end

@inline getindex(A::CachedMatrix, kr::AbstractUnitRange, jr::AbstractUnitRange) = lazy_getindex(A, kr, jr)
@inline getindex(A::CachedMatrix, kr::AbstractUnitRange, jr::AbstractUnitRange) = layout_getindex(A, kr, jr)

getindex(A::CachedVector, ::Colon) = copy(A)
getindex(A::CachedVector, ::Slice) = copy(A)
Expand Down
23 changes: 16 additions & 7 deletions src/lazyapplying.jl
Original file line number Diff line number Diff line change
Expand Up @@ -143,7 +143,7 @@ getindex(A::Applied, kj...) = materialize(A)[kj...]
Wrap a lazy object that wraps a computation producing an array to an
array.
"""
abstract type LazyArray{T,N} <: AbstractArray{T,N} end
abstract type LazyArray{T,N} <: LayoutArray{T,N} end

const LazyMatrix{T} = LazyArray{T,2}
const LazyVector{T} = LazyArray{T,1}
Expand Down Expand Up @@ -267,12 +267,6 @@ end
# on the memory layout
###

@inline getindex(A::LazyMatrix, kr::Colon, jr::Colon) = lazy_getindex(A, kr, jr)
@inline getindex(A::LazyMatrix, kr::Colon, jr::AbstractUnitRange) = lazy_getindex(A, kr, jr)
@inline getindex(A::LazyVector, kr::AbstractUnitRange) = lazy_getindex(A, kr)
@inline getindex(A::LazyMatrix, kr::AbstractUnitRange, jr::Colon) = lazy_getindex(A, kr, jr)
@inline getindex(A::LazyMatrix, kr::AbstractUnitRange, jr::AbstractUnitRange) = lazy_getindex(A, kr, jr)

@inline copyto!(dest::AbstractArray{T,N}, src::ApplyArray{T,N}) where {T,N} = copyto!(dest, Applied(src))
@inline copyto!(dest::AbstractArray, src::ApplyArray) = copyto!(dest, Applied(src))

Expand All @@ -294,6 +288,8 @@ for tri in (:tril, :triu)
@eval begin
ndims(::Applied{<:Any,typeof($tri)}) = 2
eltype(A::Applied{<:Any,typeof($tri)}) = eltype(first(A.args))
$tri(A::LazyMatrix) = ApplyMatrix($tri, A)
$tri(A::LazyMatrix, k::Integer) = ApplyMatrix($tri, A, k)
end
end

Expand All @@ -303,7 +299,20 @@ getindex(A::ApplyMatrix{T,typeof(triu),<:Tuple{<:AbstractMatrix}}, k::Integer, j
getindex(A::ApplyMatrix{T,typeof(triu),<:Tuple{<:AbstractMatrix,<:Integer}}, k::Integer, j::Integer) where T =
j  k+A.args[2] ? A.args[1][k,j] : zero(T)

getindex(A::ApplyMatrix{T,typeof(tril),<:Tuple{<:AbstractMatrix}}, k::Integer, j::Integer) where T =
j  k ? A.args[1][k,j] : zero(T)

getindex(A::ApplyMatrix{T,typeof(tril),<:Tuple{<:AbstractMatrix,<:Integer}}, k::Integer, j::Integer) where T =
j  k+A.args[2] ? A.args[1][k,j] : zero(T)


replace_in_print_matrix(A::ApplyMatrix{<:Any,typeof(triu),<:Tuple{<:AbstractMatrix}}, i::Integer, j::Integer, s::AbstractString) =
j  i ? replace_in_print_matrix(A.args[1], i, j, s) : replace_with_centered_mark(s)
replace_in_print_matrix(A::ApplyMatrix{<:Any,typeof(triu),<:Tuple{<:AbstractMatrix,<:Integer}}, i::Integer, j::Integer, s::AbstractString) =
j  i+A.args[2] ? replace_in_print_matrix(A.args[1], i, j, s) : replace_with_centered_mark(s)


replace_in_print_matrix(A::ApplyMatrix{<:Any,typeof(tril),<:Tuple{<:AbstractMatrix}}, i::Integer, j::Integer, s::AbstractString) =
j  i ? replace_in_print_matrix(A.args[1], i, j, s) : replace_with_centered_mark(s)
replace_in_print_matrix(A::ApplyMatrix{<:Any,typeof(tril),<:Tuple{<:AbstractMatrix,<:Integer}}, i::Integer, j::Integer, s::AbstractString) =
j  i+A.args[2] ? replace_in_print_matrix(A.args[1], i, j, s) : replace_with_centered_mark(s)
4 changes: 2 additions & 2 deletions src/lazyconcat.jl
Original file line number Diff line number Diff line change
Expand Up @@ -662,7 +662,7 @@ sublayout(::PaddedLayout{L}, ::Type{I}) where {L,I<:Tuple{AbstractUnitRange}} =
sublayout(::PaddedLayout{L}, ::Type{I}) where {L,I<:Tuple{AbstractUnitRange,AbstractUnitRange}} =
PaddedLayout{typeof(sublayout(L(), I))}()

_lazy_getindex(dat, kr2) = lazy_getindex(dat, kr2)
_lazy_getindex(dat, kr2) = layout_getindex(dat, kr2)
_lazy_getindex(dat::Number, kr2) = dat

function sub_materialize(::PaddedLayout, v::AbstractVector{T}) where T
Expand All @@ -678,7 +678,7 @@ function sub_materialize(::PaddedLayout, v::AbstractMatrix{T}) where T
dat = paddeddata(A)
kr,jr = parentindices(v)
kr2 = kr axes(dat,1)
Vcat(lazy_getindex(dat, kr2, jr), Zeros{T}(length(kr) - length(kr2), length(jr)))
Vcat(layout_getindex(dat, kr2, jr), Zeros{T}(length(kr) - length(kr2), length(jr)))
end

## print
Expand Down
1 change: 0 additions & 1 deletion src/linalg/lazymul.jl
Original file line number Diff line number Diff line change
Expand Up @@ -86,7 +86,6 @@ macro lazyldiv(Typ)
end

@lazymul LazyMatrix
@lazylmul LazyMatrix
@lazyldiv LazyMatrix


Expand Down
42 changes: 1 addition & 41 deletions test/lazymultests.jl
Original file line number Diff line number Diff line change
@@ -1,5 +1,5 @@
using LazyArrays, LinearAlgebra
import LazyArrays: @lazymul, @lazylmul, @lazyldiv, materialize!, MemoryLayout, triangulardata, LazyLayout, LazyArrayApplyStyle, UnknownLayout
import LazyArrays: @lazymul, @lazyldiv, materialize!, MemoryLayout, triangulardata, LazyLayout, LazyArrayApplyStyle, UnknownLayout

# used to test general matrix backends
struct MyMatrix{T} <: AbstractMatrix{T}
Expand Down Expand Up @@ -29,35 +29,6 @@ LinearAlgebra.factorize(A::MyMatrix) = factorize(A.A)
@lazymul MyMatrix
@lazyldiv MyMatrix

struct MyUpperTriangular{T} <: AbstractMatrix{T}
A::UpperTriangular{T,Matrix{T}}
end

MyUpperTriangular{T}(::UndefInitializer, n::Int, m::Int) where T = MyUpperTriangular{T}(UpperTriangular(Array{T}(undef, n, m)))
MyUpperTriangular(A::AbstractMatrix{T}) where T = MyUpperTriangular{T}(UpperTriangular(Matrix{T}(A)))
Base.convert(::Type{MyUpperTriangular{T}}, A::MyUpperTriangular{T}) where T = A
Base.convert(::Type{MyUpperTriangular{T}}, A::MyUpperTriangular) where T = MyUpperTriangular(convert(AbstractArray{T}, A.A))
Base.convert(::Type{MyUpperTriangular}, A::MyUpperTriangular)= A
Base.convert(::Type{AbstractArray{T}}, A::MyUpperTriangular) where T = MyUpperTriangular(convert(AbstractArray{T}, A.A))
Base.convert(::Type{AbstractMatrix{T}}, A::MyUpperTriangular) where T = MyUpperTriangular(convert(AbstractArray{T}, A.A))
Base.convert(::Type{MyUpperTriangular{T}}, A::AbstractArray{T}) where T = MyUpperTriangular{T}(A)
Base.convert(::Type{MyUpperTriangular{T}}, A::AbstractArray) where T = MyUpperTriangular{T}(convert(AbstractArray{T}, A))
Base.convert(::Type{MyUpperTriangular}, A::AbstractArray{T}) where T = MyUpperTriangular{T}(A)
Base.getindex(A::MyUpperTriangular, kj...) = A.A[kj...]
Base.getindex(A::MyUpperTriangular, ::Colon, j::AbstractVector) = MyUpperTriangular(A.A[:,j])
Base.setindex!(A::MyUpperTriangular, v, kj...) = setindex!(A.A, v, kj...)
Base.size(A::MyUpperTriangular) = size(A.A)
Base.similar(::Type{MyUpperTriangular{T}}, m::Int, n::Int) where T = MyUpperTriangular{T}(undef, m, n)
Base.similar(::MyUpperTriangular{T}, m::Int, n::Int) where T = MyUpperTriangular{T}(undef, m, n)
Base.similar(::MyUpperTriangular, ::Type{T}, m::Int, n::Int) where T = MyUpperTriangular{T}(undef, m, n)
LinearAlgebra.factorize(A::MyUpperTriangular) = factorize(A.A)

MemoryLayout(::Type{MyUpperTriangular{T}}) where T = MemoryLayout(UpperTriangular{T,Matrix{T}})
triangulardata(A::MyUpperTriangular) = triangulardata(A.A)

@lazylmul MyUpperTriangular


struct MyLazyArray{T,N} <: AbstractArray{T,N}
data::Array{T,N}
end
Expand Down Expand Up @@ -120,17 +91,6 @@ LinearAlgebra.factorize(A::MyLazyArray) = factorize(A.data)
@test BroadcastArray(exp,A) * BroadcastArray(exp,B) apply(*, BroadcastArray(exp,A),BroadcastArray(exp,B)) exp.(A)*exp.(B)
end

@testset "lmul!" begin
A = randn(5,5)
B = randn(5,5)
x = randn(5)

@test lmul!(MyUpperTriangular(A), copy(x)) MyUpperTriangular(A) * x
@test lmul!(MyUpperTriangular(A), copy(B)) MyUpperTriangular(A) * B

@test_skip lmul!(MyUpperTriangular(A),view(copy(B),collect(1:5),1:5)) MyUpperTriangular(A) * B
end

@testset "\\" begin
A = randn(5,5)
B = randn(5,5)
Expand Down
14 changes: 14 additions & 0 deletions test/runtests.jl
Original file line number Diff line number Diff line change
Expand Up @@ -189,16 +189,30 @@ end
@testset "triu/tril" begin
A = ApplyArray(triu,randn(2,2))
@test A isa ApplyArray{Float64}
@test A[2,1] == 0
@test A[1,1] == A.args[1][1,1]
@test A == triu(A.args[1])
A = ApplyArray(tril,randn(2,2))
@test A isa ApplyArray{Float64}
@test A[1,2] == 0
@test A[1,1] == A.args[1][1,1]
@test A == tril(A.args[1])
A = ApplyArray(triu,randn(2,2),1)
@test A[1,1] == 0
@test A[1,2] == A.args[1][1,2]
@test A isa ApplyArray{Float64}
@test A == triu(A.args[1],1)
A = ApplyArray(tril,randn(2,2),-1)
@test A isa ApplyArray{Float64}
@test A[1,1] == 0
@test A[2,1] == A.args[1][2,1]
@test A == tril(A.args[1],-1)

A = ApplyMatrix(exp,randn(2,2))
@test triu(A) isa ApplyMatrix{Float64,typeof(triu)}
@test tril(A) isa ApplyMatrix{Float64,typeof(tril)}
@test triu(A,1) isa ApplyMatrix{Float64,typeof(triu)}
@test tril(A,1) isa ApplyMatrix{Float64,typeof(tril)}
end

@testset "BroadcastArray" begin
Expand Down

2 comments on commit 224f504

@dlfivefifty
Copy link
Member Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

@JuliaRegistrator
Copy link

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Registration pull request created: JuliaRegistries/General/11652

After the above pull request is merged, it is recommended that a tag is created on this repository for the registered package version.

This will be done automatically if the Julia TagBot GitHub Action is installed, or can be done manually through the github interface, or via:

git tag -a v0.16.0 -m "<description of version>" 224f504c9a003a5f52f1a86df5c8b5f0b70657fa
git push origin v0.16.0

Please sign in to comment.