PropertyT.jl/src/orbitdata.jl

299 lines
8.9 KiB
Julia
Raw Normal View History

2018-09-05 09:56:04 +02:00
###############################################################################
#
# OrbitData
#
###############################################################################
2018-09-05 09:56:04 +02:00
struct OrbitData{T<:AbstractArray{Float64, 2}, GEl<:GroupElem, P<:perm}
2018-08-20 03:54:03 +02:00
orbits::Vector{Vector{Int}}
2018-09-05 09:56:04 +02:00
preps::Dict{GEl, P}
2018-08-20 03:54:03 +02:00
Uπs::Vector{T}
2018-01-01 14:06:33 +01:00
dims::Vector{Int}
2017-06-22 14:12:35 +02:00
end
function OrbitData(RG::GroupRing, autS::Group, verbose=true)
2019-02-24 00:17:20 +01:00
verbose && @info "Decomposing basis of RG into orbits of" autS
2018-09-05 09:56:04 +02:00
@time orbs = orbit_decomposition(autS, RG.basis, RG.basis_dict)
@assert sum(length(o) for o in orbs) == length(RG.basis)
2019-02-24 00:17:20 +01:00
verbose && @info "The action has $(length(orbs)) orbits"
2019-02-24 00:17:20 +01:00
verbose && @info "Finding projections in the Group Ring of" autS
@time autS_mps = Projections.rankOne_projections(GroupRing(autS, collect(autS)))
2019-02-24 00:17:20 +01:00
verbose && @info "Finding AutS-action matrix representation"
@time preps = perm_reps(autS, RG.basis[1:size(RG.pm,1)], RG.basis_dict)
@time mreps = matrix_reps(preps)
2019-02-24 00:17:20 +01:00
verbose && @info "Computing the projection matrices Uπs"
2018-09-05 09:56:04 +02:00
@time Uπs = [orthSVD(matrix_repr(p, mreps)) for p in autS_mps]
2018-09-05 09:56:04 +02:00
multiplicities = size.(Uπs,2)
2019-07-05 18:55:25 +02:00
dimensions = [Int(p[one(autS)]*Int(order(autS))) for p in autS_mps]
2019-02-21 14:52:20 +01:00
if verbose
info_strs = ["",
lpad("multiplicities", 14) * " =" * join(lpad.(multiplicities, 4), ""),
lpad("dimensions", 14) * " =" * join(lpad.(dimensions, 4), "")
]
2019-02-24 00:17:20 +01:00
@info join(info_strs, "\n")
2019-02-21 14:52:20 +01:00
end
2018-09-05 09:56:04 +02:00
@assert dot(multiplicities, dimensions) == size(RG.pm,1)
2018-09-05 09:56:04 +02:00
return OrbitData(orbs, preps, Uπs, dimensions)
2017-06-22 14:12:35 +02:00
end
2018-09-05 09:56:04 +02:00
function decimate(od::OrbitData)
nzros = [i for i in 1:length(od.Uπs) if size(od.Uπs[i],2) !=0]
2017-11-05 20:55:53 +01:00
2018-09-05 09:56:04 +02:00
Us = map(x -> PropertyT.sparsify!(x, eps(Float64)*1e3, verbose=true), od.Uπs[nzros])
#dimensions of the corresponding πs:
dims = od.dims[nzros]
2017-11-05 20:55:53 +01:00
2019-01-11 06:32:09 +01:00
return OrbitData(od.orbits, od.preps, Array{Float64}.(Us), dims);
2017-06-22 15:11:14 +02:00
end
2018-09-05 10:40:08 +02:00
function orthSVD(M::AbstractMatrix{T}) where {T<:AbstractFloat}
2019-01-11 06:32:09 +01:00
M = Matrix(M)
fact = svd(M)
M_rank = sum(fact.S .> maximum(size(M))*eps(T))
return fact.U[:,1:M_rank]
2018-09-05 10:40:08 +02:00
end
orbit_decomposition(G::Group, E::AbstractVector, rdict=GroupRings.reverse_dict(E)) = orbit_decomposition(collect(G), E, rdict)
function orbit_decomposition(elts::AbstractVector{<:GroupElem}, E::AbstractVector, rdict=GroupRings.reverse_dict(E))
2019-01-11 06:32:09 +01:00
tovisit = trues(size(E));
orbits = Vector{Vector{Int}}()
orbit = zeros(Int, length(elts))
2019-01-11 06:32:09 +01:00
for i in eachindex(E)
if tovisit[i]
g = E[i]
2019-01-11 06:32:09 +01:00
Threads.@threads for j in eachindex(elts)
orbit[j] = rdict[elts[j](g)]
end
2019-01-11 06:32:09 +01:00
tovisit[orbit] .= false
push!(orbits, unique(orbit))
end
end
return orbits
end
2018-09-05 09:10:58 +02:00
###############################################################################
#
# Sparsification
#
###############################################################################
dens(M::SparseMatrixCSC) = nnz(M)/length(M)
2019-01-11 06:32:09 +01:00
dens(M::AbstractArray) = count(!iszero, M)/length(M)
2018-01-02 03:19:57 +01:00
2019-01-11 06:32:09 +01:00
function sparsify!(M::SparseMatrixCSC{Tv,Ti}, eps=eps(Tv); verbose=false) where {Tv,Ti}
2018-09-05 09:06:00 +02:00
2018-09-05 09:10:58 +02:00
densM = dens(M)
for i in eachindex(M.nzval)
if abs(M.nzval[i]) < eps
M.nzval[i] = zero(Tv)
end
end
dropzeros!(M)
2018-09-05 09:06:00 +02:00
2018-09-05 09:10:58 +02:00
if verbose
2019-01-11 06:32:09 +01:00
@info("Sparsified density:", rpad(densM, 20), "", rpad(dens(M), 20), " ($(nnz(M)) non-zeros)")
2018-01-02 03:19:57 +01:00
end
2018-01-01 14:06:33 +01:00
2018-09-05 09:10:58 +02:00
return M
end
2018-01-01 14:06:33 +01:00
2019-01-11 06:32:09 +01:00
function sparsify!(M::AbstractArray{T}, eps=eps(T); verbose=false) where T
2018-09-05 09:10:58 +02:00
densM = dens(M)
2019-01-10 04:48:30 +01:00
clamp_small!(M, eps)
2018-01-01 14:06:33 +01:00
2018-09-05 09:10:58 +02:00
if verbose
2019-02-21 21:10:36 +01:00
@info("Sparsifying $(size(M))-matrix... \n $(rpad(densM, 20))$(rpad(dens(M),20))), ($(count(!iszero, M)) non-zeros)")
2018-09-05 09:10:58 +02:00
end
2018-01-01 14:06:33 +01:00
2018-09-05 09:10:58 +02:00
return sparse(M)
2017-06-22 14:12:35 +02:00
end
2018-09-05 09:10:58 +02:00
2019-01-10 04:48:30 +01:00
function clamp_small!(M::AbstractArray{T}, eps=eps(T)) where T
for n in eachindex(M)
if abs(M[n]) < eps
M[n] = zero(T)
end
end
return M
end
2019-01-11 06:32:09 +01:00
function sparsify(U::AbstractArray{T}, tol=eps(T); verbose=false) where T
return sparsify!(deepcopy(U), tol, verbose=verbose)
end
###############################################################################
#
# perm-, matrix-, representations
#
###############################################################################
function perm_repr(g::GroupElem, E::Vector, E_dict)
2019-01-11 06:32:09 +01:00
p = Vector{Int}(undef, length(E))
for (i,elt) in enumerate(E)
p[i] = E_dict[g(elt)]
end
return p
end
function perm_reps(G::Group, E::Vector, E_rdict=GroupRings.reverse_dict(E))
elts = collect(G)
l = length(elts)
2019-01-11 06:32:09 +01:00
preps = Vector{perm}(undef, l)
permG = PermutationGroup(length(E))
Threads.@threads for i in 1:l
preps[i] = permG(PropertyT.perm_repr(elts[i], E, E_rdict), false)
end
return Dict(elts[i]=>preps[i] for i in 1:l)
end
function matrix_repr(x::GroupRingElem, mreps::Dict)
2019-01-11 06:32:09 +01:00
nzeros = findall(!iszero, x.coeffs)
return sum(x[i].*mreps[parent(x).basis[i]] for i in nzeros)
end
function matrix_reps(preps::Dict{T,perm{I}}) where {T<:GroupElem, I<:Integer}
kk = collect(keys(preps))
2019-01-11 06:32:09 +01:00
mreps = Vector{SparseMatrixCSC{Float64, Int}}(undef, length(kk))
Threads.@threads for i in 1:length(kk)
mreps[i] = AbstractAlgebra.matrix_repr(preps[kk[i]])
end
return Dict(kk[i] => mreps[i] for i in 1:length(kk))
end
###############################################################################
#
# actions
#
###############################################################################
2019-06-28 00:20:30 +02:00
function (g::GroupRingElem)(y::GroupRingElem)
res = parent(y)()
for elt in GroupRings.supp(g)
res += g[elt]*elt(y)
end
return res
end
###############################################################################
#
# perm actions
#
###############################################################################
function (g::perm)(y::GroupRingElem)
RG = parent(y)
result = zero(RG, eltype(y.coeffs))
2019-04-12 23:18:48 +02:00
for (idx, c) in enumerate(y.coeffs)
if c!= zero(eltype(y.coeffs))
result[g(RG.basis[idx])] = c
end
end
return result
end
function (g::perm)(y::GroupRingElem{T, <:SparseVector}) where T
RG = parent(y)
index = [RG.basis_dict[g(RG.basis[idx])] for idx in y.coeffs.nzind]
result = GroupRingElem(sparsevec(index, y.coeffs.nzval, y.coeffs.n), RG)
return result
end
function (p::perm)(A::MatAlgElem)
length(p.d) == size(A, 1) == size(A,2) || throw("Can't act via $p on matrix of size $(size(A))")
result = similar(A)
@inbounds for i in 1:size(A, 1)
for j in 1:size(A, 2)
result[p[i], p[j]] = A[i,j] # action by permuting rows and colums/conjugation
end
2019-06-28 01:34:37 +02:00
end
return result
end
###############################################################################
#
# WreathProductElems action on MatAlgElem
#
###############################################################################
2019-06-28 00:20:30 +02:00
function (g::WreathProductElem)(y::GroupRingElem)
RG = parent(y)
result = zero(RG, eltype(y.coeffs))
for (idx, c) in enumerate(y.coeffs)
if c!= zero(eltype(y.coeffs))
result[g(RG.basis[idx])] = c
end
end
return result
end
function (g::WreathProductElem{N})(A::MatAlgElem) where N
2019-06-25 17:37:08 +02:00
# @assert N == size(A,1) == size(A,2)
flips = ntuple(i->(g.n[i].d[1]==1 && g.n[i].d[2]==2 ? 1 : -1), N)
result = similar(A)
2019-06-28 09:15:01 +02:00
R = base_ring(parent(A))
tmp = R(1)
2019-06-25 17:37:08 +02:00
@inbounds for i = 1:size(A,1)
for j = 1:size(A,2)
x = A[i, j]
2019-06-28 09:15:01 +02:00
if flips[i]*flips[j] == 1
result[g.p[i], g.p[j]] = x
2019-06-28 09:15:01 +02:00
else
result[g.p[i], g.p[j]] = -x
2019-06-28 09:15:01 +02:00
end
# result[i, j] = AbstractAlgebra.mul!(x, x, flips[i]*flips[j])
# this mul! needs to be separately defined, but is 2x faster
2019-06-25 17:37:08 +02:00
end
end
return result
end
###############################################################################
#
# Action of WreathProductElems on AutGroupElem
#
###############################################################################
function AutFG_emb(A::AutGroup, g::WreathProductElem)
isa(A.objectGroup, FreeGroup) || throw("Not an Aut(Fₙ)")
parent(g).P.n == length(A.objectGroup.gens) || throw("No natural embedding of $(parent(g)) into $A")
2019-07-05 18:55:25 +02:00
elt = one(A)
Id = one(parent(g.n.elts[1]))
flips = Groups.AutSymbol[Groups.flip_autsymbol(i) for i in 1:length(g.p.d) if g.n.elts[i] != Id]
Groups.r_multiply!(elt, flips, reduced=false)
Groups.r_multiply!(elt, [Groups.perm_autsymbol(g.p)])
return elt
end
function (g::WreathProductElem)(a::Groups.Automorphism)
A = parent(a)
g_emb = AutFG_emb(A,g)
res = deepcopy(g_emb)
res = Groups.r_multiply!(res, a.symbols, reduced=false)
res = Groups.r_multiply!(res, [inv(s) for s in reverse!(g_emb.symbols)])
return res
end
function (p::perm)(a::Groups.Automorphism)
res = parent(a)(Groups.perm_autsymbol(p))
res = Groups.r_multiply!(res, a.symbols, reduced=false)
res = Groups.r_multiply!(res, [Groups.perm_autsymbol(inv(p))])
return res
end