Skip to content

Implement reverse Cholesky for symmetric Tridiagonal matrices #179

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 5 commits into from
Jun 5, 2024

Conversation

DanielVandH
Copy link
Contributor

@DanielVandH DanielVandH commented Jun 3, 2024

This PR implements reverse Cholesky for symmetric Tridiagonal matrices. The algorithm is based on Section 3.2 here. In particular, letting

$$\boldsymbol A = \begin{bmatrix} \boldsymbol A_N & b_N\boldsymbol E_{N1} \\ b_N\boldsymbol E_{1N} & \boldsymbol A_{\infty} \end{bmatrix},$$

where $\boldsymbol A_N \in \mathbb R^{N \times N}$ and $\boldsymbol E_{ij}$ is the matrix whose only non-zero entry is $e_{ij} = 1$, we can show that

$$\|\boldsymbol L_N^\top \boldsymbol P_n\boldsymbol L_N^{-\top} \boldsymbol E_{N1}\| = \|\boldsymbol \xi_n\|,$$

where $\boldsymbol A_N = \boldsymbol L_N^\top \boldsymbol L_N$, $\boldsymbol P_n = \mathrm{diag}(\boldsymbol I_n, \boldsymbol 0)$, $n$ is the size of the finite section we need to access, and

$$\xi_n = [\boldsymbol L_N]_{n, n}\nu_n, \quad \xi_i = [\boldsymbol L_N]_{i,i}\nu_i + [\boldsymbol L_N]_{i+1,i}\boldsymbol \nu_{i+1}, \quad i=1,2,\ldots,n-1,$$

where

$$\nu_N = \frac{1}{[\boldsymbol L_N]_{N,N}}, \quad \nu_i = -\left(\frac{[\boldsymbol L_N]_{i+1,i}}{[\boldsymbol L_N]_{i,i}}\right)\nu_{i+1}, \quad i=1,2,\ldots,N-1.$$

Thus, for a given $\varepsilon > 0$ (here I default to $\varepsilon=|b_N|\sqrt{10^{-5}})$, if $|\boldsymbol \xi_n| < \varepsilon$ then we say that the $n \times n$ finite section has been well approximated and the code for expanding stops. When the factor is indexed at a greater $n$ than previously requested, the factor expands as needed. The reverse Cholesky for finite matrices is computed simply according to

$$\boldsymbol A_n = \begin{bmatrix} a_1 & b_1\boldsymbol e_1^\top \\ b_1\boldsymbol e_1 & \boldsymbol A_{n-1} \end{bmatrix} = \begin{bmatrix} \sqrt{a_1-b_1\ell_{n-1,11}^{-2}} & b_1\ell_{n-1,11}^{-1}\boldsymbol e_1^\top \\ \boldsymbol 0 & \boldsymbol L_{n-1}^\top \end{bmatrix}\begin{bmatrix} \sqrt{a_1 - b_1^2\ell_{n-1,11}^{-2}} & \boldsymbol 0^\top \\ b_1\ell_{n-1,11}^{-1}\boldsymbol e_1 & \boldsymbol L_{n-1} \end{bmatrix},$$

where $\boldsymbol A_{n-1} = \boldsymbol L_{n-1}^\top\boldsymbol L_{n-1}$.

Copy link

codecov bot commented Jun 3, 2024

Codecov Report

Attention: Patch coverage is 95.45455% with 4 lines in your changes missing coverage. Please review.

Project coverage is 83.03%. Comparing base (f2fe965) to head (b4a38a7).
Report is 1 commits behind head on master.

Files Patch % Lines
src/banded/infreversecholeskytridiagonal.jl 95.45% 4 Missing ⚠️
Additional details and impacted files
@@            Coverage Diff             @@
##           master     #179      +/-   ##
==========================================
+ Coverage   82.32%   83.03%   +0.71%     
==========================================
  Files          11       12       +1     
  Lines        1420     1509      +89     
==========================================
+ Hits         1169     1253      +84     
- Misses        251      256       +5     

☔ View full report in Codecov by Sentry.
📢 Have feedback on the report? Share it here.

@dlfivefifty
Copy link
Member

@TSGut @MikaelSlevinsky FYI

Copy link
Member

@dlfivefifty dlfivefifty left a comment

Choose a reason for hiding this comment

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

Here are some comments.

I think the unifying the different designs can happen later, separately as a different PR, but it should be on our radar.

I'm leaning towards having the factors be basic types (BandedMatrix, Bidiagonal) that wrap a data type that stores the data but probably it needs more thought.

@DanielVandH
Copy link
Contributor Author

Not sure what's up with the tests failing. I can't reproduce that error locally...

@DanielVandH
Copy link
Contributor Author

DanielVandH commented Jun 4, 2024

Oh, I can now reproduce it (actually, a different error?) after upgrading to LazyArrays 2.0.4, e.g.

Expression: exp.(T)[2:∞, 3:∞] isa SubArray
  MethodError: BroadcastStyle(::Type{SubArray{Float64, 2, LazyBandedMatrices.Tridiagonal{Float64, Fill{Float64, 1, Tuple{OneToInf{Int64}}}, Zeros{Float64, 1, Tuple{OneToInf{Int64}}}, Fill{Float64, 1, Tuple{OneToInf{Int64}}}}, Tuple{InfiniteArrays.InfUnitRange{Int64}, InfiniteArrays.InfUnitRange{Int64}}, false}}) is ambiguous.

  Candidates:
    BroadcastStyle(::Type{<:SubArray{<:Any, 2, <:BandedMatrices.AbstractBandedMatrix, <:Tuple{AbstractUnitRange{Int64}, AbstractUnitRange{Int64}}}})
      @ BandedMatrices C:\Users\User\.julia\packages\BandedMatrices\as2PJ\src\generic\broadcast.jl:40
    BroadcastStyle(::Type{<:SubArray{<:Any, 2, <:Any, <:Tuple{var"#s2", var"#s1"} where {var"#s2"<:(Union{Base.Slice{OneToInf{T}}, InfiniteArrays.AbstractInfUnitRange{T}, InfiniteArrays.InfStepRange{T}} where T<:Integer), var"#s1"<:(Union{Base.Slice{OneToInf{T}}, InfiniteArrays.AbstractInfUnitRange{T}, InfiniteArrays.InfStepRange{T}} where T<:Integer)}}})
      @ InfiniteArrays C:\Users\User\.julia\packages\InfiniteArrays\7v9AI\src\infrange.jl:489

  Possible fix, define
    BroadcastStyle(::Type{<:SubArray{<:Any, 2, <:BandedMatrices.AbstractBandedMatrix, <:Tuple{var"#s2", var"#s1"} where {var"#s2"<:Union{InfiniteArrays.AbstractInfUnitRange{Int64}, Base.Slice{OneToInf{T}} where T<:Integer}, var"#s1"<:Union{InfiniteArrays.AbstractInfUnitRange{Int64}, Base.Slice{OneToInf{T}} where T<:Integer}}}})

  Stacktrace:
    [1] combine_styles(c::SubArray{Float64, 2, LazyBandedMatrices.Tridiagonal{Float64, Fill{Float64, 1, Tuple{OneToInf{Int64}}}, Zeros{Float64, 1, Tuple{OneToInf{Int64}}}, Fill{Float64, 1, Tuple{OneToInf{Int64}}}}, Tuple{InfiniteArrays.InfUnitRange{Int64}, InfiniteArrays.InfUnitRange{Int64}}, false})
      @ Base.Broadcast .\broadcast.jl:460
    [2] broadcasted
      @ .\broadcast.jl:1341 [inlined]
    [3] _broadcastarray2broadcasted
      @ C:\Users\User\.julia\packages\LazyArrays\MLFsy\src\lazybroadcasting.jl:73 [inlined]
    [4] _broadcastarray2broadcasted
      @ C:\Users\User\.julia\packages\LazyArrays\MLFsy\src\lazybroadcasting.jl:79 [inlined]
    [5] _broadcasted
      @ C:\Users\User\.julia\packages\LazyArrays\MLFsy\src\lazybroadcasting.jl:80 [inlined]
    [6] sub_materialize(::LazyArrays.BroadcastLayout{typeof(exp)}, A::SubArray{Float64, 2, BroadcastMatrix{Float64, typeof(exp), Tuple{LazyBandedMatrices.Tridiagonal{Float64, Fill{Float64, 1, Tuple{OneToInf{Int64}}}, Zeros{Float64, 1, Tuple{OneToInf{Int64}}}, Fill{Float64, 1, Tuple{OneToInf{Int64}}}}}}, Tuple{InfiniteArrays.InfUnitRange{Int64}, InfiniteArrays.InfUnitRange{Int64}}, false})
      @ LazyArrays C:\Users\User\.julia\packages\LazyArrays\MLFsy\src\lazybroadcasting.jl:97
    [7] sub_materialize
      @ C:\Users\User\.julia\packages\ArrayLayouts\feBnP\src\ArrayLayouts.jl:126 [inlined]
    [8] layout_getindex
      @ C:\Users\User\.julia\packages\ArrayLayouts\feBnP\src\ArrayLayouts.jl:132 [inlined]
    [9] getindex(A::BroadcastMatrix{Float64, typeof(exp), Tuple{LazyBandedMatrices.Tridiagonal{Float64, Fill{Float64, 1, Tuple{OneToInf{Int64}}}, Zeros{Float64, 1, Tuple{OneToInf{Int64}}}, Fill{Float64, 1, Tuple{OneToInf{Int64}}}}}}, kr::InfiniteArrays.InfUnitRange{Int64}, jr::InfiniteArrays.InfUnitRange{Int64})
      @ ArrayLayouts C:\Users\User\.julia\packages\ArrayLayouts\feBnP\src\ArrayLayouts.jl:147
   [10] macro expansion
      @ C:\Users\User\.julia\juliaup\julia-1.10.3+0.x64.w64.mingw32\share\julia\stdlib\v1.10\Test\src\Test.jl:669 [inlined]
   [11] macro expansion
      @ C:\Users\User\.julia\dev\InfiniteLinearAlgebra.jl\test\test_infbanded.jl:66 [inlined]
   [12] macro expansion
      @ C:\Users\User\.julia\juliaup\julia-1.10.3+0.x64.w64.mingw32\share\julia\stdlib\v1.10\Test\src\Test.jl:1577 [inlined]
   [13] macro expansion
      @ C:\Users\User\.julia\dev\InfiniteLinearAlgebra.jl\test\test_infbanded.jl:35 [inlined]
   [14] macro expansion
      @ C:\Users\User\.julia\juliaup\julia-1.10.3+0.x64.w64.mingw32\share\julia\stdlib\v1.10\Test\src\Test.jl:1577 [inlined]
   [15] top-level scope
      @ C:\Users\User\.julia\dev\InfiniteLinearAlgebra.jl\test\test_infbanded.jl:7-Toeplitz: Error During Test at C:\Users\User\.julia\dev\InfiniteLinearAlgebra.jl\test\test_infbanded.jl:71
  Test threw exception
  Expression: exp.(B)[2:∞, 3:∞] isa SubArray
  MethodError: BroadcastStyle(::Type{SubArray{Float64, 2, LazyBandedMatrices.Bidiagonal{Float64, Fill{Float64, 1, Tuple{OneToInf{Int64}}}, Zeros{Float64, 1, Tuple{OneToInf{Int64}}}}, Tuple{InfiniteArrays.InfUnitRange{Int64}, InfiniteArrays.InfUnitRange{Int64}}, false}}) is ambiguous.

  Candidates:
    BroadcastStyle(::Type{<:SubArray{<:Any, 2, <:BandedMatrices.AbstractBandedMatrix, <:Tuple{AbstractUnitRange{Int64}, AbstractUnitRange{Int64}}}})
      @ BandedMatrices C:\Users\User\.julia\packages\BandedMatrices\as2PJ\src\generic\broadcast.jl:40
    BroadcastStyle(::Type{<:SubArray{<:Any, 2, <:Any, <:Tuple{var"#s2", var"#s1"} where {var"#s2"<:(Union{Base.Slice{OneToInf{T}}, InfiniteArrays.AbstractInfUnitRange{T}, InfiniteArrays.InfStepRange{T}} where T<:Integer), var"#s1"<:(Union{Base.Slice{OneToInf{T}}, InfiniteArrays.AbstractInfUnitRange{T}, InfiniteArrays.InfStepRange{T}} where T<:Integer)}}})
      @ InfiniteArrays C:\Users\User\.julia\packages\InfiniteArrays\7v9AI\src\infrange.jl:489

  Possible fix, define
    BroadcastStyle(::Type{<:SubArray{<:Any, 2, <:BandedMatrices.AbstractBandedMatrix, <:Tuple{var"#s2", var"#s1"} where {var"#s2"<:Union{InfiniteArrays.AbstractInfUnitRange{Int64}, Base.Slice{OneToInf{T}} where T<:Integer}, var"#s1"<:Union{InfiniteArrays.AbstractInfUnitRange{Int64}, Base.Slice{OneToInf{T}} where T<:Integer}}}})

  Stacktrace:
    [1] combine_styles(c::SubArray{Float64, 2, LazyBandedMatrices.Bidiagonal{Float64, Fill{Float64, 1, Tuple{OneToInf{Int64}}}, Zeros{Float64, 1, Tuple{OneToInf{Int64}}}}, Tuple{InfiniteArrays.InfUnitRange{Int64}, InfiniteArrays.InfUnitRange{Int64}}, false})
      @ Base.Broadcast .\broadcast.jl:460
    [2] broadcasted
      @ .\broadcast.jl:1341 [inlined]
    [3] _broadcastarray2broadcasted
      @ C:\Users\User\.julia\packages\LazyArrays\MLFsy\src\lazybroadcasting.jl:73 [inlined]
    [4] _broadcastarray2broadcasted
      @ C:\Users\User\.julia\packages\LazyArrays\MLFsy\src\lazybroadcasting.jl:79 [inlined]
    [5] _broadcasted
      @ C:\Users\User\.julia\packages\LazyArrays\MLFsy\src\lazybroadcasting.jl:80 [inlined]
    [6] sub_materialize(::LazyArrays.BroadcastLayout{typeof(exp)}, A::SubArray{Float64, 2, BroadcastMatrix{Float64, typeof(exp), Tuple{LazyBandedMatrices.Bidiagonal{Float64, Fill{Float64, 1, Tuple{OneToInf{Int64}}}, Zeros{Float64, 1, Tuple{OneToInf{Int64}}}}}}, Tuple{InfiniteArrays.InfUnitRange{Int64}, InfiniteArrays.InfUnitRange{Int64}}, false})
      @ LazyArrays C:\Users\User\.julia\packages\LazyArrays\MLFsy\src\lazybroadcasting.jl:97
    [7] sub_materialize
      @ C:\Users\User\.julia\packages\ArrayLayouts\feBnP\src\ArrayLayouts.jl:126 [inlined]
    [8] layout_getindex
      @ C:\Users\User\.julia\packages\ArrayLayouts\feBnP\src\ArrayLayouts.jl:132 [inlined]
    [9] getindex(A::BroadcastMatrix{Float64, typeof(exp), Tuple{LazyBandedMatrices.Bidiagonal{Float64, Fill{Float64, 1, Tuple{OneToInf{Int64}}}, Zeros{Float64, 1, Tuple{OneToInf{Int64}}}}}}, kr::InfiniteArrays.InfUnitRange{Int64}, jr::InfiniteArrays.InfUnitRange{Int64})
      @ ArrayLayouts C:\Users\User\.julia\packages\ArrayLayouts\feBnP\src\ArrayLayouts.jl:147
   [10] macro expansion
      @ C:\Users\User\.julia\juliaup\julia-1.10.3+0.x64.w64.mingw32\share\julia\stdlib\v1.10\Test\src\Test.jl:669 [inlined]
   [11] macro expansion
      @ C:\Users\User\.julia\dev\InfiniteLinearAlgebra.jl\test\test_infbanded.jl:71 [inlined]
   [12] macro expansion
      @ C:\Users\User\.julia\juliaup\julia-1.10.3+0.x64.w64.mingw32\share\julia\stdlib\v1.10\Test\src\Test.jl:1577 [inlined]
   [13] macro expansion
      @ C:\Users\User\.julia\dev\InfiniteLinearAlgebra.jl\test\test_infbanded.jl:35 [inlined]
   [14] macro expansion
      @ C:\Users\User\.julia\juliaup\julia-1.10.3+0.x64.w64.mingw32\share\julia\stdlib\v1.10\Test\src\Test.jl:1577 [inlined]
   [15] top-level scope
      @ C:\Users\User\.julia\dev\InfiniteLinearAlgebra.jl\test\test_infbanded.jl:7

@dlfivefifty
Copy link
Member

I guess you figured out that it was fixed on master?

a change in lazy arrays triggered an ambiguity error unfortunately

@DanielVandH
Copy link
Contributor Author

Yeah, didn't notice that it was updated. I brought the changes in but it didn't merge as I expected so I just applied it again in b4a38a7.

@dlfivefifty dlfivefifty merged commit 038c020 into JuliaLinearAlgebra:master Jun 5, 2024
6 checks passed
@TSGut TSGut mentioned this pull request Jun 6, 2024
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

Successfully merging this pull request may close these issues.

2 participants