Skip to content

Commit

Permalink
Support caching AbstractQ (#298)
Browse files Browse the repository at this point in the history
  • Loading branch information
dlfivefifty authored Apr 7, 2024
1 parent ddd39ac commit c8b68a8
Show file tree
Hide file tree
Showing 3 changed files with 26 additions and 5 deletions.
2 changes: 1 addition & 1 deletion 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 = "1.9.0"
version = "1.9.1"

[deps]
ArrayLayouts = "4c555306-a7a7-4459-81d9-ec55ddd5c99a"
Expand Down
19 changes: 16 additions & 3 deletions src/cache.jl
Original file line number Diff line number Diff line change
Expand Up @@ -5,11 +5,11 @@ const AbstractCachedVector{T} = AbstractCachedArray{T,1}
const AbstractCachedMatrix{T} = AbstractCachedArray{T,2}


mutable struct CachedArray{T,N,DM<:AbstractArray{T,N},M<:AbstractArray{T,N}} <: AbstractCachedArray{T,N}
mutable struct CachedArray{T,N,DM<:AbstractArray{T,N},M} <: AbstractCachedArray{T,N}
data::DM
array::M
datasize::NTuple{N,Int}
function CachedArray{T,N,DM,M}(data::DM, array::M, datasize::NTuple{N,Int}) where {T,N,DM<:AbstractArray{T,N},M<:AbstractArray{T,N}}
function CachedArray{T,N,DM,M}(data::DM, array::M, datasize::NTuple{N,Int}) where {T,N,DM<:AbstractArray{T,N},M}
for d in datasize
d < 0 && throw(ArgumentError("Datasize must be 0 or more"))
end
Expand All @@ -26,7 +26,7 @@ function CachedArray(data::AbstractArray{T,N}, array::AbstractArray{V,N}, datasi
end

const CachedVector{T,DM<:AbstractVector{T},M<:AbstractVector{T}} = CachedArray{T,1,DM,M}
const CachedMatrix{T,DM<:AbstractMatrix{T},M<:AbstractMatrix{T}} = CachedArray{T,2,DM,M}
const CachedMatrix{T,DM<:AbstractMatrix{T},M} = CachedArray{T,2,DM,M}



Expand Down Expand Up @@ -539,3 +539,16 @@ function cacheddata(V::SubArray)
data = cacheddata(P)
view(data, intersect.(axes(data), parentindices(V))...)
end


##
# AbstractQ
##
cache(A::AbstractQ) = _cache(MemoryLayout(A), A)
_cache(_, O::AbstractQ) = CachedArray(O)
CachedArray(array::AbstractQ{T}) where T = CachedArray(similar(Matrix{T}, (0,0)), array)
CachedArray(data::AbstractMatrix, array::AbstractQ) = CachedArray(data, array, size(data))
CachedArray(data::AbstractMatrix{T}, array::AbstractQ{T}, datasize::NTuple{2,Int}) where T =
CachedMatrix{T,typeof(data),typeof(array)}(data, array, datasize)

length(A::CachedMatrix{<:T,<:AbstractMatrix{T},<:AbstractQ{T}}) where T = prod(size(A.array))
10 changes: 9 additions & 1 deletion test/cachetests.jl
Original file line number Diff line number Diff line change
@@ -1,6 +1,7 @@
module CacheTests

using LazyArrays, FillArrays, LinearAlgebra, ArrayLayouts, StaticArrays, SparseArrays, Test
using LazyArrays, FillArrays, LinearAlgebra, ArrayLayouts, SparseArrays, Test
using StaticArrays
import LazyArrays: CachedArray, CachedMatrix, CachedVector, PaddedLayout, CachedLayout, resizedata!, zero!,
CachedAbstractArray, CachedAbstractVector, CachedAbstractMatrix, AbstractCachedArray, AbstractCachedMatrix

Expand Down Expand Up @@ -435,6 +436,13 @@ using Infinities
@test A'[1:3] == A.array'[1:3]
@test A'[1:11] == A.array'[1:11]
end

@testset "AbstractQ" begin
Q = qr(randn(5,5)).Q
C = cache(Q);
@test C[1:5,1:5] == Q[1:5,1:5]
@test length(C) == 25
end
end

end # module

2 comments on commit c8b68a8

@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/104417

Tip: Release Notes

Did you know you can add release notes too? Just add markdown formatted text underneath the comment after the text
"Release notes:" and it will be added to the registry PR, and if TagBot is installed it will also be added to the
release that TagBot creates. i.e.

@JuliaRegistrator register

Release notes:

## Breaking changes

- blah

To add them here just re-invoke and the PR will be updated.

Tagging

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 v1.9.1 -m "<description of version>" c8b68a8b0930cc4485f56615edaa9649851b23eb
git push origin v1.9.1

Please sign in to comment.