From c8b68a8b0930cc4485f56615edaa9649851b23eb Mon Sep 17 00:00:00 2001 From: Sheehan Olver Date: Sun, 7 Apr 2024 17:41:26 +0200 Subject: [PATCH] Support caching AbstractQ (#298) --- Project.toml | 2 +- src/cache.jl | 19 ++++++++++++++++--- test/cachetests.jl | 10 +++++++++- 3 files changed, 26 insertions(+), 5 deletions(-) diff --git a/Project.toml b/Project.toml index cbb0d4d5..62b272f6 100644 --- a/Project.toml +++ b/Project.toml @@ -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" diff --git a/src/cache.jl b/src/cache.jl index 07fa5cba..0fa3b5ad 100644 --- a/src/cache.jl +++ b/src/cache.jl @@ -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 @@ -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} @@ -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)) \ No newline at end of file diff --git a/test/cachetests.jl b/test/cachetests.jl index cefe6a79..f3f6c941 100644 --- a/test/cachetests.jl +++ b/test/cachetests.jl @@ -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 @@ -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