Skip to content

Commit 28049d1

Browse files
committed
refactor: improve error handling and logging in nullspace functions
1 parent 03de416 commit 28049d1

File tree

2 files changed

+38
-13
lines changed

2 files changed

+38
-13
lines changed

src/decompositions/irred-decomp.jl

Lines changed: 25 additions & 10 deletions
Original file line numberDiff line numberDiff line change
@@ -15,13 +15,13 @@ function ws_nullspace(
1515
if all(f -> isapprox(f, zero(f), atol=tol), Bₐ)
1616
return ws
1717
end
18-
dim(ws) == 1 && return nothing
18+
dim(ws) == 1 && return nothing # implied by lines 15-17
1919
Cs = zero_combinations(Bₐ; tol=tol, logging=logging)
2020
isempty(Cs) && return nothing
2121
return WeightSpace(
2222
weight(ws),
2323
PolySpace{field_type(ws)}([div_by_smallest_coeff(sum(c .* B)) for c in Cs])
24-
)
24+
)
2525
end
2626

2727
function common_nullspace_as_weight_vectors(
@@ -38,16 +38,16 @@ function common_nullspace_as_weight_vectors(
3838
for X in Xs
3939
wsₙ = ws_nullspace(X, a, wsₙ)
4040
if isnothing(wsₙ)
41-
logging && printstyled("Found no common nullspace, smth is wrong!\n", color=:red)
41+
error("Found no common nullspace, while looking for $(nhwv) highest weight vectors; dim(ws) = $(dim(ws))")
4242
return WeightVector[] # error("Didn't find any highest weight vectors, had to find $(nhwv)")
4343
end
4444
end
4545

4646
if dim(wsₙ) != nhwv
4747
error("Found $(dim(wsₙ)) highest weight vectors, expected $(nhwv)")
4848
end
49-
logging && printstyled("Found: ", color=:green)
50-
logging && printstyled("$(join(map(repr, basis(space(wsₙ))), ", "))\n", color=:green)
49+
# logging && printstyled("Found: ", color=:green)
50+
# logging && printstyled("$(join(map(repr, basis(space(wsₙ))), ", "))\n", color=:green)
5151

5252
return [WeightVector(weight(wsₙ), div_by_smallest_coeff(v)) for v in basis(space(wsₙ))] # TODO: remove div_by_smallest_coeff
5353
end
@@ -109,11 +109,19 @@ function irreducibles(
109109

110110
logging && printstyled("Applying Clebsch-Gordan to decompose $(to_string(V)) into irreducibles...\n", color=:blue)
111111
sym_ws, cg_decomp = sym_weight_struct(highest_weight(Vb), algebra(action(ρ)), power(V), ws)
112+
@assert Set(weights(sym_ws)) == keys(cg_decomp)
113+
112114
logging && printstyled(to_string(V), " = ", str_irr_decomp("V", cg_decomp), "\n", color=:blue)
113115

114116
Xs = positive_root_elements(algebra(action(ρ)))
115117
hw_vectors = common_nullspace_as_weight_vectors(Xs, action(ρ), sym_ws, cg_decomp; logging=logging)
116-
return [IrreducibleRepresentation(action(ρ), hwv) for hwv in hw_vectors]
118+
119+
irrs = [IrreducibleRepresentation(action(ρ), hwv) for hwv in hw_vectors]
120+
logging && println("sum = ", sum([dim(space(irr)) for irr in irrs]))
121+
logging && println("dim(V): ", dim(V), ", dim(Vb): ", dim(Vb), ", power(V): ", power(V))
122+
logging && println("dim(cg_decomp): ", weyl_dim(cg_decomp, algebra(action(ρ))))
123+
@assert sum([dim(space(irr)) for irr in irrs]) == dim(V)
124+
return irrs
117125
end
118126

119127
function irreducibles(
@@ -148,6 +156,8 @@ function irreducibles(
148156
ρᵢ = GroupRepresentation(action(ρ), Vᵢ)
149157
append!(irreds, irreducibles(ρᵢ; logging=logging, decomp_count=decomp_count))
150158
end
159+
160+
@assert sum([dim(space(irr)) for irr in irreds]) == dim(V)
151161
return irreds
152162
end
153163

@@ -173,6 +183,7 @@ function irreducibles(
173183

174184
logging && printstyled("Applying Clebsch-Gordan to decompose $(to_string(V)) into irreducibles...\n", color=:blue)
175185
cg_decomp = tensor(hw₁, hw₂, algebra(action(ρ)))
186+
176187
if length(cg_decomp) == 1
177188
logging && printstyled("The tensor product is irreducible, returning the tensor product of the highest weight vectors\n", color=:red)
178189
return [IrreducibleRepresentation(action(ρ), hw_vector(V₁)*hw_vector(V₂))]
@@ -184,7 +195,10 @@ function irreducibles(
184195

185196
Xs = positive_root_elements(algebra(action(ρ)))
186197
hw_vectors = common_nullspace_as_weight_vectors(Xs, action(ρ), tensor_ws, cg_decomp; logging=logging)
187-
return [IrreducibleRepresentation(action(ρ), hwv) for hwv in hw_vectors]
198+
199+
irrs = [IrreducibleRepresentation(action(ρ), hwv) for hwv in hw_vectors]
200+
@assert sum([dim(space(irr)) for irr in irrs]) == dim(V)
201+
return irrs
188202
end
189203

190204
function irreducibles(
@@ -216,12 +230,13 @@ function irreducibles(
216230

217231
irreds = IrreducibleRepresentation{A}[]
218232
for irr₁ in irrs₁, irr₂ in irrs₂
219-
V = TensorProduct([space(irr₁), space(irr₂)])
220-
ρV = GroupRepresentation(action(ρ), V)
233+
Vt = TensorProduct([space(irr₁), space(irr₂)])
234+
ρV = GroupRepresentation(action(ρ), Vt)
221235
irrs = irreducibles(ρV; logging=logging, decomp_count=decomp_count)
222-
@assert sum([dim(space(irr)) for irr in irrs]) == dim(V)
236+
@assert sum([dim(space(irr)) for irr in irrs]) == dim(Vt)
223237
append!(irreds, irrs)
224238
end
239+
@assert sum([dim(space(irr)) for irr in irreds]) == dim(V)
225240
return irreds
226241
end
227242

src/utils/basic.jl

Lines changed: 13 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -3,7 +3,7 @@ export rref, monomials, coeffs_matrix, polynomials
33
export num_mons
44
export superscript, subscript
55
export multiexponents, div_by_smallest_coeff
6-
export eye, a2p, p2a, rand_unit
6+
export eye, a2p, p2a, rand_unit, xx
77

88
a2p(M::AbstractMatrix{<:Number}) = [M; ones(eltype(M), 1, size(M, 2))]
99
p2a(M::AbstractMatrix{<:Number}) = (M./M[end:end,:])[1:end-1,:]
@@ -17,6 +17,7 @@ Base.rand(::Type{Rational{T}}) where T <: Integer = rand(T(-10):T(10)) // rand(T
1717
Base.rand(::Type{Complex{Rational{T}}}) where T <: Integer = rand(Rational{T}) + rand(Rational{T})*im
1818
Base.rand(T::DataType, n::Int) = [rand(T) for _ in 1:n]
1919

20+
xx(v) = [0 -v[3] v[2]; v[3] 0 -v[1]; -v[2] v[1] 0]
2021
eye(n::Integer) = eye(Float64, n)
2122
eye(T::Type, n::Integer) = Matrix{T}(I(n))
2223

@@ -161,11 +162,16 @@ function rref(
161162
F::Vector{<:AbstractPolynomial{T}};
162163
tol::Real=1e-5
163164
) where T
165+
printstyled("Computing rref, merging monomials...\n", color=:blue)
164166
mons = monomials(F)
167+
printstyled("Monomials merged\n", color=:blue)
168+
printstyled("Constructing coefficients matrix...\n", color=:blue)
165169
M = Matrix(transpose(coeffs_matrix(F, mons)))
170+
printstyled("Coefficients matrix constructed\n", color=:blue)
166171
rref!(M, tol)
167172
sparsify!(M, tol)
168173
N = filter(row -> !iszero(row), eachrow(M))
174+
printstyled("RREF computed\n", color=:blue)
169175
return polynomials(N, mons)
170176
end
171177

@@ -183,17 +189,21 @@ function zero_combinations(
183189
M = rand(T, size(M, 2), size(M, 1)) * M
184190
end
185191
logging && print(crayon"#f4d03f", "Computing nullspace of $(size(M)) matrix...\n")
186-
N = Matrix(transpose(nullspace(M; atol=tol)))
187-
logging && print(crayon"#f4d03f", "Nullspace is $(size(N, 1))-dimensional\n")
192+
Nc = Matrix(transpose(nullspace(M; atol=tol)))
193+
logging && print(crayon"#f4d03f", "Nullspace is $(size(Nc, 1))-dimensional\n")
194+
N = copy(Nc)
188195
sparsify!(N, tol)
189196
rref!(N, tol)
190197
for n in N
191198
if norm(n) > 1e3
199+
display(Nc)
192200
display(N)
193201
error("Nullspace contains large coefficients")
194202
end
195203
end
196204
sparsify!(N, tol)
205+
# println("Nc: ", "max = $(maximum(abs, Nc; init=-Inf))", ", min = $(minimum(abs, Nc; init=Inf))")
206+
# println("N: ", "max = $(maximum(abs, N; init=-Inf))", ", min = $(minimum(abs, N; init=Inf))")
197207
return eachrow(N)
198208
end
199209

0 commit comments

Comments
 (0)