Skip to content
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

reduce allocations in Toeplitz-Hankel #212

Open
wants to merge 1 commit into
base: master
Choose a base branch
from
Open
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
14 changes: 7 additions & 7 deletions src/toeplitzhankel.jl
Original file line number Diff line number Diff line change
Expand Up @@ -109,15 +109,15 @@ function hankel_partialchol(v::Vector, D::AbstractVector)
σ = T[]
n = isempty(v) ? 0 : (length(v)+2) ÷ 2
C = Matrix{T}(undef, n, 100)
d = v[1:2:end] .* D.^2 # diag of D .* H .* D'
@views d = v[1:2:end] .* D.^2 # diag of D .* H .* D'
@assert length(v) ≥ 2n-1
reltol = maximum(abs,d)*eps(T)*log(n)
r = 0
for k = 1:n
mx,idx = findmax(d)
if mx ≤ reltol break end
push!(σ, inv(mx))
C[:,k] .= view(v,idx:n+idx-1) .*D.*D[idx]
C[:,k] .= view(v, range(idx, length=n)) .* D .* D[idx]
for j = 1:k-1
nCjidxσj = -C[idx,j]*σ[j]
LinearAlgebra.axpy!(nCjidxσj, view(C,:,j), view(C,:,k))
Expand All @@ -127,7 +127,7 @@ function hankel_partialchol(v::Vector, D::AbstractVector)
end
r += 1
end
r == 100 && error("ranks more than 100 not yet supported")
r >= 100 && error("ranks more than 100 not yet supported")
for k=1:length(σ) rmul!(view(C,:,k), sqrt(σ[k])) end
C[:,1:r]
end
Expand Down Expand Up @@ -206,7 +206,7 @@ function _leg2chebuTH_TLC(::Type{S}, mn, d) where {S}
S̃ = real(S)
λ = Λ.(0:half(S̃):n-1)
t = zeros(S,n)
t[1:2:end] = λ[1:2:n]./(((1:2:n).-2))
@views t[1:2:end] .= λ[1:2:n]./(((1:2:n).-2))
h = λ./((1:2n-1).+1)
C = hankel_partialchol(h)
T = plan_uppertoeplitz!(-2t/π, (length(t), size(C,2)), 1)
Expand Down Expand Up @@ -237,7 +237,7 @@ function _cheb2legTH_TLC(::Type{S}, mn, d) where S
t = zeros(S,n-1)
S̃ = real(S)
if n > 1
t[1:2:end] = Λ.(0:one(S̃):div(n-2,2), -half(S̃), one(S̃))
t[1:2:end] .= Λ.(0:one(S̃):div(n-2,2), -half(S̃), one(S̃))
end
h = Λ.(1:half(S̃):n-1, zero(S̃), 3half(S̃))
D = 1:n-1
Expand All @@ -263,7 +263,7 @@ function plan_th_ultra2ultra!(::Type{S}, (n,)::Tuple{Int}, λ₁, λ₂) where {
DL = (zero(S̃):n-one(S̃)) .+ λ₂
jk = 0:half(S̃):n-1
t = zeros(S,n)
t[1:2:n] = Λ.(jk,λ₁-λ₂,one(S̃))[1:2:n]
@views t[1:2:n] = Λ.(jk,λ₁-λ₂,one(S̃))[1:2:n]
h = Λ.(jk,λ₁,λ₂+one(S̃))
lmul!(gamma(λ₂)/gamma(λ₁),h)
C = hankel_partialchol(h)
Expand Down Expand Up @@ -316,4 +316,4 @@ for f in (:th_leg2cheb, :th_cheb2leg, :th_leg2chebu)
end

th_ultra2ultra(v, λ₁, λ₂, dims...) = plan_th_ultra2ultra!(eltype(v),size(v),λ₁,λ₂, dims...)*copy(v)
th_jac2jac(v, α, β, γ, δ, dims...) = plan_th_jac2jac!(eltype(v),size(v),α,β,γ,δ, dims...)*copy(v)
th_jac2jac(v, α, β, γ, δ, dims...) = plan_th_jac2jac!(eltype(v),size(v),α,β,γ,δ, dims...)*copy(v)