-
Notifications
You must be signed in to change notification settings - Fork 7
/
lib.jl
137 lines (116 loc) · 3.03 KB
/
lib.jl
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
# tests for library routines
import SPTK: agexp, gexp, glog, mseq, theq!, theq, toeplitz!, toeplitz
function test_agexp()
println("-- test_agexp")
@test agexp(1, 1, 1) ≈ 5.0
@test agexp(1, 2, 3) ≈ 18.0
@test agexp(2, 3, 5) ≈ 12.206555615733702
end
function test_gexp()
println("-- test_gexp")
@test gexp(1, 1) ≈ 2.0
@test gexp(2, 4) ≈ 3.0
end
function test_glog()
println("-- test_glog")
@test glog(1, 2) ≈ 1.0
@test glog(2, 3) ≈ 4.0
end
function test_mseq()
println("-- test_mseq")
for i = 1:1000 @test !isnan(mseq()) end
end
function fill_toeplitz!{T}(A::AbstractMatrix{T}, t::AbstractVector{T})
n = length(t)
for j=1:n, i=1:n
A[i,j] = i-j+1 >= 1 ? t[i-j+1] : t[j-i+1]
end
A
end
function fill_hankel!{T}(A::AbstractMatrix{T}, h::AbstractVector{T})
n = length(h)>>1 + 1
for j=1:n, i=1:n
A[i,j] = h[i+j-1]
end
A
end
# (T + H)a = b
function test_theq()
println("-- test_theq")
srand(98765)
n = 2
# toeplitz elements
t = [1.0, -1.0]
T = zeros(2,2)
fill_toeplitz!(T, t)
# hankel elements
h = zeros(2n-1)
h[1] = 1.0
h[3] = 1.0
H = zeros(2,2)
fill_hankel!(H, h)
b = ones(n)
a = zeros(n)
# solve (T + H)a = b
theq!(a, t, h, b)
@test a ≈ ones(n)
@test a ≈ theq(t, h, b)
@test a ≈ (T + H) \ b
# fail to solve toeplitz plus hankel matrix system
for m in [3, 5, 10]
a = ones(m)
t = ones(a)
h = ones(2length(a)-1)
b = ones(a)
@test_throws ErrorException theq!(a, t, h, b)
end
a = ones(5)
t = ones(a)
h = ones(2length(a)-1)
b = ones(a)
@test_throws ArgumentError theq!(a, t, h, b, min_det=-1.0)
@test_throws DimensionMismatch theq!(a, t, h, ones(length(b)-1))
@test_throws DimensionMismatch theq!(a, t, ones(length(h)-1), b)
@test_throws DimensionMismatch theq!(a, t, ones(length(h)-1), b)
@test_throws DimensionMismatch theq!(a, ones(length(t)-1), h, b)
@test_throws DimensionMismatch theq!(ones(length(a)-1), t, h, b)
end
# Ta = b
function test_toeplitz()
println("-- test_toeplitz")
srand(98765)
n = 2
# toeplitz elements
t = [2.0, -1.0]
T = zeros(2,2)
fill_toeplitz!(T, t)
b = [1.0, 1.0]
b = ones(n)
a = zeros(n)
# solve Ta = b
toeplitz!(a, t, b)
@test a ≈ ones(n)
@test a ≈ toeplitz(t, b)
@test a ≈ T \ b
# fail to solve toeplitz set of linear equations
for m in [3, 5, 10]
a = ones(m)
t = ones(a)
b = ones(a)
@test_throws ErrorException toeplitz!(a, t, b)
end
a = ones(5)
t = ones(a)
b = ones(a)
@test_throws ArgumentError toeplitz!(a, t, b, eps=-1.0)
@test_throws DimensionMismatch toeplitz!(a, t, ones(length(b)-1))
@test_throws DimensionMismatch toeplitz!(a, ones(length(t)-1), b)
@test_throws DimensionMismatch toeplitz!(ones(length(a)-1), t, b)
end
println("test library routines")
test_agexp()
test_gexp()
test_glog()
test_mseq()
test_theq()
test_toeplitz()