Skip to content

Commit 25da0ca

Browse files
authored
Move Diagonal support to InfiniteArrays.jl (#79)
* Move Diagonal support to InfiniteArrays.jl * hcat overloads * Update Project.toml * Update Project.toml
1 parent 004a4e4 commit 25da0ca

File tree

6 files changed

+111
-17
lines changed

6 files changed

+111
-17
lines changed

Project.toml

Lines changed: 6 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -1,12 +1,13 @@
11
name = "InfiniteLinearAlgebra"
22
uuid = "cde9dba0-b1de-11e9-2c62-0bab9446c55c"
3-
version = "0.5.7"
3+
version = "0.5.8"
44

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

1819
[compat]
19-
ArrayLayouts = "0.7"
20+
ArrayLayouts = "0.7.2"
2021
BandedMatrices = "0.16.9"
2122
BlockArrays = "0.15"
2223
BlockBandedMatrices = "0.10"
24+
DSP = "0.6, 0.7"
2325
FillArrays = "0.11"
24-
InfiniteArrays = "0.10.6"
26+
InfiniteArrays = "0.11"
2527
LazyArrays = "0.21"
26-
LazyBandedMatrices = "0.5"
28+
LazyBandedMatrices = "0.6"
2729
MatrixFactorizations = "0.8"
2830
SemiseparableMatrices = "0.2.3"
2931
julia = "1.5"

src/InfiniteLinearAlgebra.jl

Lines changed: 6 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,4 +1,5 @@
11
module InfiniteLinearAlgebra
2+
using InfiniteArrays: InfRanges
23
using BlockArrays, BlockBandedMatrices, BandedMatrices, LazyArrays, LazyBandedMatrices, SemiseparableMatrices,
34
FillArrays, InfiniteArrays, MatrixFactorizations, ArrayLayouts, LinearAlgebra
45

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

37+
import DSP: conv
38+
3639
import SemiseparableMatrices: AbstractAlmostBandedLayout, _almostbanded_qr!
3740

3841

@@ -58,6 +61,8 @@ end
5861

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

64+
include("infconv.jl")
65+
6166
include("banded/hessenbergq.jl")
6267

6368
include("banded/infbanded.jl")

src/banded/infbanded.jl

Lines changed: 10 additions & 11 deletions
Original file line numberDiff line numberDiff line change
@@ -32,13 +32,6 @@ end
3232

3333
sub_materialize(_, V::SubArray{<:Any,1,<:AbstractMatrix,Tuple{InfBandCartesianIndices}}, ::Tuple{InfAxes}) =
3434
_inf_banded_sub_materialize(MemoryLayout(parent(V)), V)
35-
###
36-
# Diagonal
37-
###
38-
39-
Base._unsafe_getindex(::IndexCartesian, D::Diagonal, k::InfAxes, j::InfAxes) = layout_getindex(D, k, j)
40-
Base._unsafe_getindex(::IndexCartesian, D::Diagonal, k::InfAxes, j::Union{Real,AbstractArray}) = layout_getindex(D, k, j)
41-
Base._unsafe_getindex(::IndexCartesian, D::Diagonal, k::Union{Real,AbstractArray}, j::InfAxes) = layout_getindex(D, k, j)
4235

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

369-
sub_materialize(::AbstractBandedLayout, V, ::Tuple{InfAxes,InfAxes}) = V
370-
sub_materialize(::AbstractBandedLayout, V, ::Tuple{OneTo{Int},InfAxes}) = V
371-
sub_materialize(::AbstractBandedLayout, V, ::Tuple{InfAxes,OneTo{Int}}) = V
372362

373363
@inline sub_materialize(::ApplyBandedLayout{typeof(*)}, V, ::Tuple{InfAxes,InfAxes}) = V
374364
@inline sub_materialize(::BroadcastBandedLayout, V, ::Tuple{InfAxes,InfAxes}) = V
375365
@inline sub_materialize(::BandedColumns, V, ::Tuple{InfAxes,InfAxes}) = BandedMatrix(V)
366+
@inline sub_materialize(::BandedColumns, V, ::Tuple{InfAxes,OneTo{Int}}) = BandedMatrix(V)
376367

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

513504
# copy for AdjOrTrans
514505
copy(A::Adjoint{T,<:BandedMatrix{T,<:Any,OneToInf{Int}}}) where T = copy(parent(A))'
515-
copy(A::Transpose{T,<:BandedMatrix{T,<:Any,OneToInf{Int}}}) where T = transpose(copy(parent(A)))
506+
copy(A::Transpose{T,<:BandedMatrix{T,<:Any,OneToInf{Int}}}) where T = transpose(copy(parent(A)))
507+
508+
509+
##
510+
# hcat
511+
##
512+
513+
Base.typed_hcat(::Type{T}, A::BandedMatrix{<:Any,<:Any,OneToInf{Int}}, B::AbstractVecOrMat...) where T = Hcat{T}(A, B...)
514+

src/infconv.jl

Lines changed: 70 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,70 @@
1+
2+
##
3+
# conv
4+
# This is useful for determining polynomial dimensions
5+
##
6+
7+
conv(a::AbstractFill, b::AbstractFill) = conv(collect(a), collect(b))
8+
conv(a::Vector, b::AbstractFill) = conv(a, collect(b))
9+
conv(a::AbstractFill, b::Vector) = conv(collect(a), b)
10+
11+
12+
conv(::Ones{T,1,<:Tuple{<:OneToInf}}, ::Ones{V,1,<:Tuple{<:OneToInf}}) where {T<:Integer,V<:Integer} =
13+
OneToInf{promote_type(T,V)}()
14+
conv(::Ones{Bool,1,<:Tuple{<:OneToInf}}, ::Ones{Bool,1,<:Tuple{<:OneToInf}}) =
15+
OneToInf()
16+
conv(::Ones{T,1,<:Tuple{<:OneToInf}}, ::Ones{V,1,<:Tuple{<:OneToInf}}) where {T,V} =
17+
one(promote_type(T,V)):
18+
19+
function conv(::Ones{T,1,<:Tuple{<:OneToInf}}, a::AbstractVector{V}) where {T,V}
20+
cs = cumsum(convert(AbstractVector{promote_type(T,V)}, a))
21+
Vcat(cs, Fill(last(cs), ∞))
22+
end
23+
24+
function conv(::Ones{T,1,<:Tuple{<:OneToInf}}, a::Vector{V}) where {T,V}
25+
cs = cumsum(convert(AbstractVector{promote_type(T,V)}, a))
26+
Vcat(cs, Fill(last(cs), ∞))
27+
end
28+
29+
function conv(a::AbstractVector{V}, ::Ones{T,1,<:Tuple{<:OneToInf}}) where {T,V}
30+
cs = cumsum(convert(AbstractVector{promote_type(T,V)}, a))
31+
Vcat(cs, Fill(last(cs), ∞))
32+
end
33+
34+
function conv(a::Vector{V}, ::Ones{T,1,<:Tuple{<:OneToInf}}) where {T,V}
35+
cs = cumsum(convert(AbstractVector{promote_type(T,V)}, a))
36+
Vcat(cs, Fill(last(cs), ∞))
37+
end
38+
39+
40+
function conv(r::InfRanges, x::AbstractVector)
41+
length(x)  1 && throw(ArgumentError("conv(::$(typeof(r)), ::$(typeof(x))) not implemented"))
42+
first(x)*r
43+
end
44+
function conv(x::AbstractVector, r::InfRanges)
45+
length(x)  1 && throw(ArgumentError("conv(::$(typeof(r)), ::$(typeof(x))) not implemented"))
46+
first(x)*r
47+
end
48+
49+
conv(r1::InfRanges, r2::AbstractFill{<:Any,1,<:Tuple{<:OneToInf}}) =
50+
cumsum(r1*getindex_value(r2))
51+
conv(r2::AbstractFill{<:Any,1,<:Tuple{<:OneToInf}}, r1::InfRanges) =
52+
cumsum(getindex_value(r2)*r1)
53+
54+
conv(r1::InfRanges, r2::Ones{<:Any,1,<:Tuple{<:OneToInf}}) = cumsum(r1)
55+
conv(r2::Ones{<:Any,1,<:Tuple{<:OneToInf}}, r1::InfRanges) = cumsum(r1)
56+
57+
conv(r1::InfRanges, r2::InfRanges) = throw(ArgumentError("conv(::$(typeof(r1)), ::$(typeof(r2))) not implemented"))
58+
59+
function conv(r1::AbstractFill{<:Any,1,<:Tuple{<:OneToInf}}, r2::AbstractFill{<:Any,1,<:Tuple{<:OneToInf}})
60+
a = getindex_value(r1) * getindex_value(r2)
61+
a:a:
62+
end
63+
function conv(r1::AbstractFill{<:Any,1,<:Tuple{<:OneToInf}}, r2::Ones{<:Any,1,<:Tuple{<:OneToInf}})
64+
a = getindex_value(r1) * getindex_value(r2)
65+
a:a:
66+
end
67+
function conv(r1::Ones{<:Any,1,<:Tuple{<:OneToInf}}, r2::AbstractFill{<:Any,1,<:Tuple{<:OneToInf}})
68+
a = getindex_value(r1) * getindex_value(r2)
69+
a:a:
70+
end

test/runtests.jl

Lines changed: 3 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -9,10 +9,12 @@ import BlockArrays: _BlockArray
99
import BlockBandedMatrices: isblockbanded, _BlockBandedMatrix
1010
import MatrixFactorizations: QLPackedQ
1111
import BandedMatrices: bandeddata, _BandedMatrix, BandedStyle
12-
import LazyArrays: colsupport, ApplyStyle, MemoryLayout, ApplyLayout, LazyArrayStyle, arguments
12+
import LazyArrays: colsupport, MemoryLayout, ApplyLayout, LazyArrayStyle, arguments
1313
import InfiniteArrays: OneToInf, oneto, RealInfinity
1414
import LazyBandedMatrices: BroadcastBandedBlockBandedLayout, BroadcastBandedLayout, LazyBandedLayout
1515

16+
include("test_infconv.jl")
17+
1618

1719
@testset "∞-banded" begin
1820
@testset "Diagonal and BandedMatrix" begin

test/test_infconv.jl

Lines changed: 16 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,16 @@
1+
using InfiniteLinearAlgebra, DSP, FillArrays, Test
2+
3+
@testset "conv" begin
4+
@test conv(1:∞, [2]) conv([2], 1:∞) 2:2:
5+
@test conv(1:2:∞, [2]) conv([2], 1:2:∞) 2:4:
6+
@test conv(1:∞, Ones(∞))[1:5] == conv(Ones(∞),1:∞)[1:5] == [1,3,6,10,15]
7+
@test conv(Ones(∞), Ones(∞)) 1.0:1.0:
8+
@test conv(Ones{Int}(∞), Ones{Int}(∞)) oneto(∞)
9+
@test conv(Ones{Bool}(∞), Ones{Bool}(∞)) oneto(∞)
10+
@test conv(Fill{Int}(2,∞), Fill{Int}(1,∞)) conv(Fill{Int}(2,∞), Ones{Int}(∞))
11+
conv(Ones{Int}(∞), Fill{Int}(2,∞)) 2:2:
12+
@test conv(Ones{Int}(∞), [1,5,8])[1:10] == conv([1,5,8], Ones{Int}(∞))[1:10] ==
13+
conv(fill(1,100),[1,5,8])[1:10] == conv([1,5,8], fill(1,100))[1:10]
14+
@test conv(Ones{Int}(∞), 1:4)[1:10] == conv(1:4, Ones{Int}(∞))[1:10] ==
15+
conv(fill(1,100),collect(1:4))[1:10] == conv(collect(1:4), fill(1,100))[1:10]
16+
end

0 commit comments

Comments
 (0)