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

added filter, accessible and coaccessible function

parent 76313be2
No related branches found
No related tags found
No related merge requests found
......@@ -50,6 +50,9 @@ M1 = FSA(
["a", "b", "c"]
)
# ╔═╡ 3e34e635-3565-437d-acbe-6bd170bf4382
findall([true, false, true])
# ╔═╡ 028ff41d-f9a7-4ff8-b254-7399be161a48
Diagonal(Symmetric(randn(2, 2)))
......@@ -347,9 +350,6 @@ end
# ╔═╡ 09a701f0-ea44-419f-9de4-f5b4682e9d94
[1, 2, 3][[2, 1, 3]]
# ╔═╡ e77bb923-a6aa-4fd1-98fc-85d6483bf723
# ╔═╡ 8a653fd5-f5c5-4fc6-88d7-6eeb4da81216
topologicalorder(M1)
......@@ -611,6 +611,7 @@ C .* Z3
# ╟─0f68bc4e-3fcb-4d28-bba2-4d0032c80981
# ╠═87e1cf8e-da52-4c85-ab6a-bb352a84d779
# ╠═8602a2b5-16ca-4cec-8041-c6362aa66a48
# ╠═3e34e635-3565-437d-acbe-6bd170bf4382
# ╠═028ff41d-f9a7-4ff8-b254-7399be161a48
# ╠═fe7c8892-2439-4b1d-b661-92f8bb6825bc
# ╠═9400b791-605f-4cb5-a3d7-71af5a034504
......@@ -654,7 +655,6 @@ C .* Z3
# ╠═4977c8c7-8a43-4ef0-ad18-2aaa9c147706
# ╠═998c5d5a-a28f-43ac-900f-508da56e9d3b
# ╠═09a701f0-ea44-419f-9de4-f5b4682e9d94
# ╠═e77bb923-a6aa-4fd1-98fc-85d6483bf723
# ╠═8a653fd5-f5c5-4fc6-88d7-6eeb4da81216
# ╠═5a3f44fc-2016-49a9-9e32-30c9a2bff081
# ╠═67e55bab-f2cd-4181-b9a0-33b2181e207b
......
......@@ -20,6 +20,8 @@ export
ω,
ρ,
λ,
accessible,
coaccessible,
nstates,
nedges,
initstates,
......
......@@ -26,7 +26,7 @@ function addskipedges(M, states, weights)
end
"""
Base.cumsum(A[; init = initstates(A), n = nstates(A)])
Base.sum(A[; init = initstates(A), n = nstates(A)])
Accumulate the weight of all the paths of length less or equal to
`n`.
......@@ -42,24 +42,6 @@ function Base.sum(A::AbstractFSA; init = α(A), n = nstates(A) + 2)
end
"""
union(A1[, A2, ...])
A1 ∪ A2
Return the union of the given FSA.
"""
function Base.union(A1::AbstractFSA{K}, A2::AbstractFSA{K}) where K
FSA(
vcat(α(A1), α(A2)),
blockdiag(T(A1), T(A2)),
vcat(ω(A1), ω(A2)),
ρ(A1) + ρ(A2),
vcat(λ(A1), λ(A2))
)
end
Base.union(A1::AbstractFSA{K}, AN::AbstractFSA{K}...) where K =
foldl(union, AN, init = A1)
"""
cat(A1[, A2, ...])
......@@ -94,12 +76,43 @@ function closure(A::AbstractFSA{K}; plus = false) where K
)
end
function _filter(A::AbstractFSA{K}, x::AbstractVector{Bool}) where K
J = findall(x)
M = sparse(1:length(J), J, one(K), length(J), nstates(A))
FSA(M * α(A), M * T(A) * M', M * ω(A), ρ(A), λ(A)[J])
end
function Base.filter(f::Function, A::AbstractFSA)
_filter(A, f.(1:nstates(A)))
end
"""
reverse(A)
notaccessible(A::AbstractFSA{K}) where K
Return the reversal of A.
Returns a vector `x` of dimension `nstates(A)` where `x[i]` is `one(K)`
if the state `i` is not accessible, `zero(K)` otherwise.
"""
Base.reverse(A::AbstractFSA) = typeof(A)(ω(A), copy(T(A)'), α(A), ρ(A), λ(A))
function accessible(A::AbstractFSA{K}) where K
vₙ = α(A)
m = ones(Bool, nstates(A))
m[findnz(vₙ)[1]] .= false
while nnz(vₙ) > 0
uₙ = T(A)' * vₙ
vₙ = uₙ .* m
m[findnz(uₙ)[1]] .= false
end
.! m
end
"""
coaccessible(A::AbstractFSA{K}) where K
Returns a vector `x` of dimension `nstates(A)` where `x[i]` is `one(K)`
if the state `i` is not accessible, `zero(K)` otherwise.
"""
coaccessible(A::AbstractFSA) = accessible(A |> reverse)
"""
renorm(A::FSA)
......@@ -142,6 +155,31 @@ function Base.replace(new::Function, M::AbstractFSA)
replace(M, [new(i) for i in 1:nstates(M)])
end
"""
reverse(A)
Return the reversal of A.
"""
Base.reverse(A::AbstractFSA) = typeof(A)(ω(A), copy(T(A)'), α(A), ρ(A), λ(A))
"""
union(A1[, A2, ...])
A1 ∪ A2
Return the union of the given FSA.
"""
function Base.union(A1::AbstractFSA{K}, A2::AbstractFSA{K}) where K
FSA(
vcat(α(A1), α(A2)),
blockdiag(T(A1), T(A2)),
vcat(ω(A1), ω(A2)),
ρ(A1) + ρ(A2),
vcat(λ(A1), λ(A2))
)
end
Base.union(A1::AbstractFSA{K}, AN::AbstractFSA{K}...) where K =
foldl(union, AN, init = A1)
#= Functions for acyclic FSA =#
"""
......
......@@ -103,6 +103,17 @@ end
@test Base.isapprox(val(sum(A1 |> renorm; n = 100)), val(one(K)), atol=1e-6)
@test Base.isapprox(val(sum(A2 |> renorm; n = 100)), val(one(K)), atol=1e-6)
# (co)accessibility
A = FSA(
sparsevec([1, 2], one(K), 5),
sparse([2, 3], [3, 3], one(K), 5, 5),
sparsevec([3, 4], one(K), 5),
zero(K),
[L("$i") for i in 1:5]
)
@test all(accessible(A) .== [true, true, true, false, false])
@test all(coaccessible(A) .== [false, true, true, true, false])
# globalrenorm
@test Base.isapprox(val(sum(AcyclicFSA(A2) |> globalrenorm; n = 100)), val(one(K)), atol=1e-6)
end
......
0% Loading or .
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment