Skip to content

Move Diagonal support to InfiniteArrays.jl #79

New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Merged
merged 4 commits into from
Jun 22, 2021
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
10 changes: 6 additions & 4 deletions Project.toml
Original file line number Diff line number Diff line change
@@ -1,12 +1,13 @@
name = "InfiniteLinearAlgebra"
uuid = "cde9dba0-b1de-11e9-2c62-0bab9446c55c"
version = "0.5.7"
version = "0.5.8"

[deps]
ArrayLayouts = "4c555306-a7a7-4459-81d9-ec55ddd5c99a"
BandedMatrices = "aae01518-5342-5314-be14-df237901396f"
BlockArrays = "8e7c35d0-a365-5155-bbbb-fb81a777f24e"
BlockBandedMatrices = "ffab5731-97b5-5995-9138-79e8c1846df0"
DSP = "717857b8-e6f2-59f4-9121-6e50c889abd2"
FillArrays = "1a297f60-69ca-5386-bcde-b61e274b549b"
InfiniteArrays = "4858937d-0d70-526a-a4dd-2d5cb5dd786c"
LazyArrays = "5078a376-72f3-5289-bfd5-ec5146d43c02"
Expand All @@ -16,14 +17,15 @@ MatrixFactorizations = "a3b82374-2e81-5b9e-98ce-41277c0e4c87"
SemiseparableMatrices = "f8ebbe35-cbfb-4060-bf7f-b10e4670cf57"

[compat]
ArrayLayouts = "0.7"
ArrayLayouts = "0.7.2"
BandedMatrices = "0.16.9"
BlockArrays = "0.15"
BlockBandedMatrices = "0.10"
DSP = "0.6, 0.7"
FillArrays = "0.11"
InfiniteArrays = "0.10.6"
InfiniteArrays = "0.11"
LazyArrays = "0.21"
LazyBandedMatrices = "0.5"
LazyBandedMatrices = "0.6"
MatrixFactorizations = "0.8"
SemiseparableMatrices = "0.2.3"
julia = "1.5"
Expand Down
7 changes: 6 additions & 1 deletion src/InfiniteLinearAlgebra.jl
Original file line number Diff line number Diff line change
@@ -1,4 +1,5 @@
module InfiniteLinearAlgebra
using InfiniteArrays: InfRanges
using BlockArrays, BlockBandedMatrices, BandedMatrices, LazyArrays, LazyBandedMatrices, SemiseparableMatrices,
FillArrays, InfiniteArrays, MatrixFactorizations, ArrayLayouts, LinearAlgebra

Expand All @@ -15,7 +16,7 @@ import BandedMatrices: BandedMatrix, _BandedMatrix, AbstractBandedMatrix, banded
import FillArrays: AbstractFill, getindex_value, axes_print_matrix_row
import InfiniteArrays: OneToInf, InfUnitRange, Infinity, PosInfinity, InfiniteCardinal, InfStepRange, AbstractInfUnitRange, InfAxes, InfRanges
import LinearAlgebra: matprod, qr, AbstractTriangular, AbstractQ, adjoint, transpose, AdjOrTrans
import LazyArrays: applybroadcaststyle, CachedArray, CachedMatrix, CachedVector, DenseColumnMajor, FillLayout, ApplyMatrix, check_mul_axes, ApplyStyle, LazyArrayApplyStyle, LazyArrayStyle,
import LazyArrays: applybroadcaststyle, CachedArray, CachedMatrix, CachedVector, DenseColumnMajor, FillLayout, ApplyMatrix, check_mul_axes, LazyArrayStyle,
resizedata!, MemoryLayout,
factorize, sub_materialize, LazyLayout, LazyArrayStyle, layout_getindex,
applylayout, ApplyLayout, PaddedLayout, zero!, MulAddStyle,
Expand All @@ -33,6 +34,8 @@ import BlockBandedMatrices: _BlockSkylineMatrix, _BandedMatrix, _BlockSkylineMat
BlockSkylineSizes, BlockSkylineMatrix, BlockBandedMatrix, _BlockBandedMatrix, BlockTridiagonal,
AbstractBlockBandedLayout, _blockbanded_qr!, BlockBandedLayout

import DSP: conv

import SemiseparableMatrices: AbstractAlmostBandedLayout, _almostbanded_qr!


Expand All @@ -58,6 +61,8 @@ end

export Vcat, Fill, ql, ql!, ∞, ContinuousSpectrumError, BlockTridiagonal

include("infconv.jl")

include("banded/hessenbergq.jl")

include("banded/infbanded.jl")
Expand Down
21 changes: 10 additions & 11 deletions src/banded/infbanded.jl
Original file line number Diff line number Diff line change
Expand Up @@ -32,13 +32,6 @@ end

sub_materialize(_, V::SubArray{<:Any,1,<:AbstractMatrix,Tuple{InfBandCartesianIndices}}, ::Tuple{InfAxes}) =
_inf_banded_sub_materialize(MemoryLayout(parent(V)), V)
###
# Diagonal
###

Base._unsafe_getindex(::IndexCartesian, D::Diagonal, k::InfAxes, j::InfAxes) = layout_getindex(D, k, j)
Base._unsafe_getindex(::IndexCartesian, D::Diagonal, k::InfAxes, j::Union{Real,AbstractArray}) = layout_getindex(D, k, j)
Base._unsafe_getindex(::IndexCartesian, D::Diagonal, k::Union{Real,AbstractArray}, j::InfAxes) = layout_getindex(D, k, j)

const TriToeplitz{T} = Tridiagonal{T,Fill{T,1,Tuple{OneToInf{Int}}}}
const ConstRowMatrix{T} = ApplyMatrix{T,typeof(*),<:Tuple{<:AbstractVector,<:AbstractFill{<:Any,2,Tuple{OneTo{Int},OneToInf{Int}}}}}
Expand Down Expand Up @@ -366,13 +359,11 @@ _BandedMatrix(::PertToeplitzLayout, A::AbstractMatrix) =
# end
# end

sub_materialize(::AbstractBandedLayout, V, ::Tuple{InfAxes,InfAxes}) = V
sub_materialize(::AbstractBandedLayout, V, ::Tuple{OneTo{Int},InfAxes}) = V
sub_materialize(::AbstractBandedLayout, V, ::Tuple{InfAxes,OneTo{Int}}) = V

@inline sub_materialize(::ApplyBandedLayout{typeof(*)}, V, ::Tuple{InfAxes,InfAxes}) = V
@inline sub_materialize(::BroadcastBandedLayout, V, ::Tuple{InfAxes,InfAxes}) = V
@inline sub_materialize(::BandedColumns, V, ::Tuple{InfAxes,InfAxes}) = BandedMatrix(V)
@inline sub_materialize(::BandedColumns, V, ::Tuple{InfAxes,OneTo{Int}}) = BandedMatrix(V)

sub_materialize(_, V, ::Tuple{<:BlockedUnitRange{<:InfRanges}}) = V
sub_materialize(::AbstractBlockLayout, V, ::Tuple{<:BlockedUnitRange{<:InfRanges}}) = V
Expand Down Expand Up @@ -512,4 +503,12 @@ copyto!(dest::AbstractArray, L::Ldiv{BidiagonalToeplitzLayout,Lay}) where Lay =

# copy for AdjOrTrans
copy(A::Adjoint{T,<:BandedMatrix{T,<:Any,OneToInf{Int}}}) where T = copy(parent(A))'
copy(A::Transpose{T,<:BandedMatrix{T,<:Any,OneToInf{Int}}}) where T = transpose(copy(parent(A)))
copy(A::Transpose{T,<:BandedMatrix{T,<:Any,OneToInf{Int}}}) where T = transpose(copy(parent(A)))


##
# hcat
##

Base.typed_hcat(::Type{T}, A::BandedMatrix{<:Any,<:Any,OneToInf{Int}}, B::AbstractVecOrMat...) where T = Hcat{T}(A, B...)

70 changes: 70 additions & 0 deletions src/infconv.jl
Original file line number Diff line number Diff line change
@@ -0,0 +1,70 @@

##
# conv
# This is useful for determining polynomial dimensions
##

conv(a::AbstractFill, b::AbstractFill) = conv(collect(a), collect(b))
conv(a::Vector, b::AbstractFill) = conv(a, collect(b))
conv(a::AbstractFill, b::Vector) = conv(collect(a), b)


conv(::Ones{T,1,<:Tuple{<:OneToInf}}, ::Ones{V,1,<:Tuple{<:OneToInf}}) where {T<:Integer,V<:Integer} =
OneToInf{promote_type(T,V)}()
conv(::Ones{Bool,1,<:Tuple{<:OneToInf}}, ::Ones{Bool,1,<:Tuple{<:OneToInf}}) =
OneToInf()
conv(::Ones{T,1,<:Tuple{<:OneToInf}}, ::Ones{V,1,<:Tuple{<:OneToInf}}) where {T,V} =
one(promote_type(T,V)):∞

function conv(::Ones{T,1,<:Tuple{<:OneToInf}}, a::AbstractVector{V}) where {T,V}
cs = cumsum(convert(AbstractVector{promote_type(T,V)}, a))
Vcat(cs, Fill(last(cs), ∞))
end

function conv(::Ones{T,1,<:Tuple{<:OneToInf}}, a::Vector{V}) where {T,V}
cs = cumsum(convert(AbstractVector{promote_type(T,V)}, a))
Vcat(cs, Fill(last(cs), ∞))
end

function conv(a::AbstractVector{V}, ::Ones{T,1,<:Tuple{<:OneToInf}}) where {T,V}
cs = cumsum(convert(AbstractVector{promote_type(T,V)}, a))
Vcat(cs, Fill(last(cs), ∞))
end

function conv(a::Vector{V}, ::Ones{T,1,<:Tuple{<:OneToInf}}) where {T,V}
cs = cumsum(convert(AbstractVector{promote_type(T,V)}, a))
Vcat(cs, Fill(last(cs), ∞))
end


function conv(r::InfRanges, x::AbstractVector)
length(x) ≠ 1 && throw(ArgumentError("conv(::$(typeof(r)), ::$(typeof(x))) not implemented"))
first(x)*r
end
function conv(x::AbstractVector, r::InfRanges)
length(x) ≠ 1 && throw(ArgumentError("conv(::$(typeof(r)), ::$(typeof(x))) not implemented"))
first(x)*r
end

conv(r1::InfRanges, r2::AbstractFill{<:Any,1,<:Tuple{<:OneToInf}}) =
cumsum(r1*getindex_value(r2))
conv(r2::AbstractFill{<:Any,1,<:Tuple{<:OneToInf}}, r1::InfRanges) =
cumsum(getindex_value(r2)*r1)

conv(r1::InfRanges, r2::Ones{<:Any,1,<:Tuple{<:OneToInf}}) = cumsum(r1)
conv(r2::Ones{<:Any,1,<:Tuple{<:OneToInf}}, r1::InfRanges) = cumsum(r1)

conv(r1::InfRanges, r2::InfRanges) = throw(ArgumentError("conv(::$(typeof(r1)), ::$(typeof(r2))) not implemented"))

function conv(r1::AbstractFill{<:Any,1,<:Tuple{<:OneToInf}}, r2::AbstractFill{<:Any,1,<:Tuple{<:OneToInf}})
a = getindex_value(r1) * getindex_value(r2)
a:a:∞
end
function conv(r1::AbstractFill{<:Any,1,<:Tuple{<:OneToInf}}, r2::Ones{<:Any,1,<:Tuple{<:OneToInf}})
a = getindex_value(r1) * getindex_value(r2)
a:a:∞
end
function conv(r1::Ones{<:Any,1,<:Tuple{<:OneToInf}}, r2::AbstractFill{<:Any,1,<:Tuple{<:OneToInf}})
a = getindex_value(r1) * getindex_value(r2)
a:a:∞
end
4 changes: 3 additions & 1 deletion test/runtests.jl
Original file line number Diff line number Diff line change
Expand Up @@ -9,10 +9,12 @@ import BlockArrays: _BlockArray
import BlockBandedMatrices: isblockbanded, _BlockBandedMatrix
import MatrixFactorizations: QLPackedQ
import BandedMatrices: bandeddata, _BandedMatrix, BandedStyle
import LazyArrays: colsupport, ApplyStyle, MemoryLayout, ApplyLayout, LazyArrayStyle, arguments
import LazyArrays: colsupport, MemoryLayout, ApplyLayout, LazyArrayStyle, arguments
import InfiniteArrays: OneToInf, oneto, RealInfinity
import LazyBandedMatrices: BroadcastBandedBlockBandedLayout, BroadcastBandedLayout, LazyBandedLayout

include("test_infconv.jl")


@testset "∞-banded" begin
@testset "Diagonal and BandedMatrix" begin
Expand Down
16 changes: 16 additions & 0 deletions test/test_infconv.jl
Original file line number Diff line number Diff line change
@@ -0,0 +1,16 @@
using InfiniteLinearAlgebra, DSP, FillArrays, Test

@testset "conv" begin
@test conv(1:∞, [2]) ≡ conv([2], 1:∞) ≡ 2:2:∞
@test conv(1:2:∞, [2]) ≡ conv([2], 1:2:∞) ≡ 2:4:∞
@test conv(1:∞, Ones(∞))[1:5] == conv(Ones(∞),1:∞)[1:5] == [1,3,6,10,15]
@test conv(Ones(∞), Ones(∞)) ≡ 1.0:1.0:∞
@test conv(Ones{Int}(∞), Ones{Int}(∞)) ≡ oneto(∞)
@test conv(Ones{Bool}(∞), Ones{Bool}(∞)) ≡ oneto(∞)
@test conv(Fill{Int}(2,∞), Fill{Int}(1,∞)) ≡ conv(Fill{Int}(2,∞), Ones{Int}(∞)) ≡
conv(Ones{Int}(∞), Fill{Int}(2,∞)) ≡ 2:2:∞
@test conv(Ones{Int}(∞), [1,5,8])[1:10] == conv([1,5,8], Ones{Int}(∞))[1:10] ==
conv(fill(1,100),[1,5,8])[1:10] == conv([1,5,8], fill(1,100))[1:10]
@test conv(Ones{Int}(∞), 1:4)[1:10] == conv(1:4, Ones{Int}(∞))[1:10] ==
conv(fill(1,100),collect(1:4))[1:10] == conv(collect(1:4), fill(1,100))[1:10]
end