1
1
function partialchol (H:: Hankel )
2
2
# Assumes positive definite
3
- σ= Array ( eltype (H), 0 )
3
+ σ= eltype (H)[]
4
4
n= size (H,1 )
5
- C= Array ( eltype (H),n,n)
5
+ C= Vector{ eltype (H)}[]
6
6
v= [H[:,1 ];vec (H[end ,2 : end ])]
7
7
d= diag (H)
8
-
9
8
@assert length (v) ≥ 2 n- 1
10
-
11
- tol= 1E-14
9
+ tol= eps (eltype (H))* log (n)
12
10
for k= 1 : n
13
11
mx,idx= findmax (d)
14
- if mx ≤ tol
15
- break
12
+ if mx ≤ tol break end
13
+ push! (σ,inv (mx))
14
+ push! (C,v[idx: n+ idx- 1 ])
15
+ for j= 1 : k- 1
16
+ nCjidxσj = - C[j][idx]* σ[j]
17
+ Base. axpy! (nCjidxσj, C[j], C[k])
16
18
end
17
- push! (σ,1 / mx)
18
19
@simd for p= 1 : n
19
- @inbounds C[p,k] = v[p + idx - 1 ] # H[p,idx]
20
+ @inbounds d[p] -= C[k][p] ^ 2 / mx
20
21
end
21
- for j= 1 : k- 1 ,p= 1 : n
22
- @inbounds C[p,k]-= C[p,j]* C[idx,j]* σ[j]
23
- end
24
- @simd for p= 1 : n
25
- @inbounds d[p]-= C[p,k]^ 2 / mx
26
- end
27
- end
28
- for k= 1 : length (σ),p= 1 : n
29
- @inbounds C[p,k]*= sqrt (σ[k])
30
22
end
31
- C[:,1 : length (σ)]
23
+ for k= 1 : length (σ) scale! (C[k],sqrt (σ[k])) end
24
+ C
32
25
end
33
26
34
27
function toeplitzcholmult (T,C,v)
35
- ret= C[:,1 ]. * (T* (C[:,1 ]. * v))
36
- for j= 2 : size (C,2 )
37
- ret+= C[:,j]. * (T* (C[:,j]. * v))
28
+ n,K = length (v),length (C)
29
+ ret,temp1,temp2 = zero (v),zero (v),zero (v)
30
+ un,ze = one (eltype (v)),zero (eltype (v))
31
+ broadcast! (* , temp1, C[K], v)
32
+ A_mul_B! (un, T, temp1, ze, temp2)
33
+ broadcast! (* , ret, C[K], temp2)
34
+ for k= K- 1 : - 1 : 1
35
+ broadcast! (* , temp1, C[k], v)
36
+ A_mul_B! (un, T, temp1, ze, temp2)
37
+ broadcast! (* , temp1, C[k], temp2)
38
+ broadcast! (+ , ret, ret, temp1)
38
39
end
39
40
ret
40
41
end
43
44
# Plan a multiply by DL*(T.*H)*DR
44
45
immutable ToeplitzHankelPlan{TT}
45
46
T:: TriangularToeplitz{TT}
46
- C:: Matrix{TT } # A cholesky factorization of H: H=CC'
47
+ C:: Vector{Vector{TT} } # A cholesky factorization of H: H=CC'
47
48
DL:: Vector{TT}
48
49
DR:: Vector{TT}
49
50
50
51
ToeplitzHankelPlan (T,C,DL,DR)= new (T,C,DL,DR)
51
52
end
52
53
53
54
54
- function ToeplitzHankelPlan (T:: TriangularToeplitz ,C:: Matrix ,DL:: AbstractVector ,DR:: AbstractVector )
55
- TT= promote_type (eltype (T),eltype (C),eltype (DL),eltype (DR))
55
+ function ToeplitzHankelPlan (T:: TriangularToeplitz ,C:: Vector ,DL:: AbstractVector ,DR:: AbstractVector )
56
+ TT= promote_type (eltype (T),eltype (C[ 1 ] ),eltype (DL),eltype (DR))
56
57
ToeplitzHankelPlan {TT} (T,C,collect (TT,DL),collect (TT,DR))
57
58
end
58
59
ToeplitzHankelPlan (T:: TriangularToeplitz ,C:: Matrix ) =
@@ -66,7 +67,9 @@ ToeplitzHankelPlan(T::TriangularToeplitz,H::Hankel,D...) =
66
67
67
68
# Legendre transforms
68
69
70
+ Λ = Cx
69
71
72
+ #=
70
73
Λ{T}(::Type{T},z)=z<5?gamma(z+one(T)/2)/gamma(z+one(T)):exp(lgamma(z+one(T)/2)-lgamma(z+one(T)))
71
74
72
75
# use recurrence to construct Λ fast on a range of values
94
97
95
98
Λ(z::Number)=Λ(typeof(z),z)
96
99
Λ(z::AbstractArray)=Λ(eltype(z),z)
97
-
100
+ =#
98
101
99
102
function leg2chebuTH {TT} (:: Type{TT} ,n)
100
- λ= Λ (TT, 0 : 0.5 : n- 1 )
103
+ λ= Λ (0 : half (TT) : n- 1 )
101
104
t= zeros (TT,n)
102
105
t[1 : 2 : end ]= λ[1 : 2 : n]. / (((1 : 2 : n)- 2 ))
103
106
T= TriangularToeplitz (- 2 / π* t,:U )
108
111
th_leg2chebuplan {TT} (:: Type{TT} ,n)= ToeplitzHankelPlan (leg2chebuTH (TT,n)... ,1 : n,ones (TT,n))
109
112
110
113
function leg2chebTH {TT} (:: Type{TT} ,n)
111
- λ= Λ (TT, 0 : 0.5 : n- 1 )
114
+ λ= Λ (0 : half (TT) : n- 1 )
112
115
t= zeros (TT,n)
113
116
t[1 : 2 : end ]= λ[1 : 2 : n]
114
117
T= TriangularToeplitz (2 / π* t,:U )
0 commit comments