Skip to content
Snippets Groups Projects
Unverified Commit 3113a429 authored by Lucas Ondel Yang's avatar Lucas Ondel Yang
Browse files

added test for closure

parent dd79ff45
Branches
Tags
No related merge requests found
......@@ -89,6 +89,14 @@ Return the indices of the final states `I` and their associated value
"""
finalstates(A) = findnz(ω(A))
"""
emptystring(A)
Return the weight of the empty string "ϵ" in `A`.
"""
emptystring(A) = ρ(A)
Base.parent(A::AbstractFSA) = A
"""
......
......@@ -27,6 +27,8 @@ T(fsa::FSA) = fsa.T
function Base.convert(f::Function, A::AbstractFSA{K,L}) where {K,L}
l = λ(A)
ρ = f(emptystring(A), one(L))
U = typeof(ρ)
α = sparsevec(
initstates(A)[1],
[f(v, l[i]) for (i, v) in zip(initstates(A)...)],
......@@ -45,6 +47,6 @@ function Base.convert(f::Function, A::AbstractFSA{K,L}) where {K,L}
nstates(A)
)
FSA(α, T, ω, iszero(ρ(A)) ? zero(eltype(α)) : f(ρ(A), one(L)), l)
FSA{U,L}(α, T, ω, ρ, l)
end
......@@ -18,7 +18,11 @@ Ls = [
f(L, w, l) = begin
S = UnionConcatSemiring{L}
ProductSemiring((w, S(Set(L[l]))))
if iszero(w)
return ProductSemiring((w, zero(S)))
else
return ProductSemiring((w, S(Set(L[l]))))
end
end
@testset "FSA" begin
......@@ -39,6 +43,9 @@ end
A2 = FSA(α2, T2, ω2, ρ2, λ2)
B2 = convert((w, l) -> f(L, w, l), A2)
= FSA(spzeros(K, 0), spzeros(K, 0, 0), spzeros(K, 0), one(K), L[])
= convert((w, l) -> f(L, w, l), )
@test all(α(A1) . α1)
@test all(T(A1) . T1)
@test all(ω(A1) . ω1)
......@@ -65,6 +72,24 @@ end
@test cs_B12.tval[1] cs_B1_B2.tval[1]
@test cs_B12.tval[2] == cs_B1_B2.tval[2]
# The number of iteration for the cumulative sum depends on the
# structure of the FSA for the test to pass.
# It should be 3 times the maximum path length of B2.
n = 6
# closure
B2p_n3 = B2 cat(B2, B2) cat(B2, B2, B2)
B2_n3 = B2 cat(B2, B2) cat(B2, B2, B2)
cB2p = closure(B2; plus = true)
cB2 = closure(B2; plus = false)
cs_B2p_n3 = cumsum(B2p_n3; n = n)
cs_B2_n3 = cumsum(B2_n3; n = n)
cs_cB2p = cumsum(cB2p; n = n)
cs_cB2 = cumsum(cB2; n = n)
@test cs_cB2p.tval[1] cs_B2p_n3.tval[1]
@test cs_cB2p.tval[2] == cs_B2p_n3.tval[2]
@test cs_cB2.tval[1] cs_B2_n3.tval[1]
@test cs_cB2.tval[2] == cs_B2_n3.tval[2]
end
end
0% Loading or .
You are about to add 0 people to the discussion. Proceed with caution.
Please register or to comment