From 32e6ca3de6e8ab683954af40886e7ed2f737d187 Mon Sep 17 00:00:00 2001 From: Lucas ONDEL YANG <lucas.ondel@cnrs.fr> Date: Fri, 31 Mar 2023 12:32:41 +0200 Subject: [PATCH] fixed reverse dense fsa and added connect function --- sandbox/GNAT.jl | 39 ++++++++++---------------------------- src/FiniteStateAutomata.jl | 1 + src/dense_fsa.jl | 5 +++++ src/ops.jl | 33 +++++++++++++++++++++++++++++++- 4 files changed, 48 insertions(+), 30 deletions(-) diff --git a/sandbox/GNAT.jl b/sandbox/GNAT.jl index 4f95a03..167f19c 100644 --- a/sandbox/GNAT.jl +++ b/sandbox/GNAT.jl @@ -1,5 +1,5 @@ ### A Pluto.jl notebook ### -# v0.19.22 +# v0.19.20 using Markdown using InteractiveUtils @@ -73,17 +73,11 @@ C ∩ D # â•”â•â•¡ b37d3f48-0b23-4700-bb45-050b08209669 @time L ∩ C -# â•”â•â•¡ 26a8c2c5-27da-4389-bfd8-ca3793dd994e -I = FSA( - kron(L.α, C.α), - kron(L.T, C.T), - kron(L.ω, C.ω), - L.Ï * C.Ï, - kron(L.λ, C.λ) -) - # â•”â•â•¡ 183f8031-b384-408b-8cd6-c84f23693dfc +# â• â•â•¡ disabled = true +#=â• â•â•¡ M = sparse([1,1,2,3,4,4,5,6,7,7,8,9],[1,3,2,4,1,3,2,4,1,3,2,4], one(K), nstates(L), nstates(C)) + â• â•â•¡ =# # â•”â•â•¡ b605383a-4080-4916-857c-2528f6ad172c M' * L.ω @@ -122,20 +116,11 @@ G = DenseFSA(H, sort(collect(Σ)), zero(K)) # â•”â•â•¡ 54888ef8-6ee9-446d-9b78-364fe35e52b6 G, L -# â•”â•â•¡ b2290feb-24a1-4f7d-8216-7c69ea363a67 -L - -# â•”â•â•¡ 5c3893f7-b64f-410f-9be2-5c2023fd9093 -# C = A ∩ B - -# â•”â•â•¡ 5ecadd35-feea-459f-9776-3a2762b0de9f -# FSA(C) - -# â•”â•â•¡ 2ea1644a-8836-443b-a1ee-8d13dbd4a534 -H[:, 2] +# â•”â•â•¡ 40ba3c52-84e0-4d70-8d8e-840ec2225baf +G ∩ C -# â•”â•â•¡ 68d8f4b3-48e1-49ca-b910-c65c2ec19768 -ones(K, 3) * H[:,2]' +# â•”â•â•¡ 91b52b46-b8d8-4d14-9f9c-732de9d906ff +G ∩ C |> connect # â•”â•â•¡ Cell order: # â• â•510f7d8a-ce11-11ed-2832-cdc20fa6f700 @@ -153,15 +138,11 @@ ones(K, 3) * H[:,2]' # â• â•b37d3f48-0b23-4700-bb45-050b08209669 # â• â•049d12b4-0107-4ecc-89e4-45d3955bdd0b # â• â•54888ef8-6ee9-446d-9b78-364fe35e52b6 -# â• â•26a8c2c5-27da-4389-bfd8-ca3793dd994e # â• â•183f8031-b384-408b-8cd6-c84f23693dfc # â• â•b605383a-4080-4916-857c-2528f6ad172c # â• â•e91b7104-7149-4770-8abe-fb0a968bd197 # â• â•49fb8a23-bae8-4c66-8490-01d7e28f25f9 # â• â•29a16022-6ebc-4e74-8245-e7293fcb160a # â• â•ada373ae-0e4e-42ca-80a5-98be29aca610 -# â• â•b2290feb-24a1-4f7d-8216-7c69ea363a67 -# â• â•5c3893f7-b64f-410f-9be2-5c2023fd9093 -# â• â•5ecadd35-feea-459f-9776-3a2762b0de9f -# â• â•2ea1644a-8836-443b-a1ee-8d13dbd4a534 -# â• â•68d8f4b3-48e1-49ca-b910-c65c2ec19768 +# â• â•40ba3c52-84e0-4d70-8d8e-840ec2225baf +# â• â•91b52b46-b8d8-4d14-9f9c-732de9d906ff diff --git a/src/FiniteStateAutomata.jl b/src/FiniteStateAutomata.jl index 0d9da08..3e2beca 100644 --- a/src/FiniteStateAutomata.jl +++ b/src/FiniteStateAutomata.jl @@ -34,6 +34,7 @@ export # FSA operations addskipedges, closure, + connect, determinize, globalrenorm, minimize, diff --git a/src/dense_fsa.jl b/src/dense_fsa.jl index dbef9e2..a044b3e 100644 --- a/src/dense_fsa.jl +++ b/src/dense_fsa.jl @@ -39,6 +39,8 @@ end Ï(G::DenseFSA) = G.Ï Î»(G::DenseFSA) = repeat(G.Σ, size(G.H, 2) - 1) +Base.reverse(A::DenseFSA) = DenseFSA(A.H[:, end:-1:1], A.Σ, A.Ï) + # Intersection with DenseFSA struct IntersectedDenseFSA{K, L, T<:AbstractFSA{K, L}} <: IntersectedAbstractFSA{K, L, DenseFSA{K, L}, T} A::DenseFSA{K, L} @@ -72,6 +74,8 @@ end Base.intersect(A::DenseFSA, B::AbstractFSA) = IntersectedDenseFSA(A, B) Base.intersect(A::AbstractFSA, B::DenseFSA) = IntersectedDenseFSA(B, A) # we assume ∩ to be commutative w.r.t any K +Base.reverse(A::IntersectedDenseFSA) = IntersectedDenseFSA(A.A |> reverse, A.B |> reverse, A.C) + α(I::IntersectedDenseFSA) = begin h1 = I.A.H[:, 1] Q = nstates(I.B) * (size(I.A.H, 2) - 1) @@ -113,3 +117,4 @@ end λ(I::IntersectedDenseFSA) = repeat(λ(I.B), size(I.A.H, 2) - 1) Ï(I::IntersectedDenseFSA) = Ï(I.B) * Ï(I.A) + diff --git a/src/ops.jl b/src/ops.jl index f44d17e..400b06b 100644 --- a/src/ops.jl +++ b/src/ops.jl @@ -224,7 +224,38 @@ end 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) +function coaccessible(A::AbstractFSA) + 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 + +""" + connect(A::AbstractFSA) + +Return a FSA ``B`` equivalent of ``A`` such that all states in ``B`` +are accessible and coaccessible. +""" +function connect(A::AbstractFSA{K}) where K + m = accessible(A) .* coaccessible(A) + I = findall(m) + M = sparse(I, 1:length(I), one(K), nstates(A), length(I)) + FSA( + M' * α(A), + M' * T(A) * M, + M' * ω(A), + Ï(A), + λ(A)[I] + ) +end """ renorm(A::FSA) -- GitLab