2222
2323weight (wv:: WeightVector ) = wv. weight
2424vector (wv:: WeightVector ) = wv. vector
25+ Base. setindex! (wv:: WeightVector , n:: Number , i:: Integer ) = wv. vector[i] = n
2526
2627mutable struct WeightSpace
2728 weight:: Vector{Int}
28- space :: Matrix{ComplexF64}
29+ matrix :: Matrix{ComplexF64}
2930end
3031
3132WeightSpace (wv:: WeightVector ) = WeightSpace (weight (wv), V2M (vector (wv)))
32- WeightSpace (weight:: AbstractVector , space :: AbstractVector ) = WeightSpace (weight, V2M (space ))
33+ WeightSpace (weight:: AbstractVector , v :: AbstractVector ) = WeightSpace (weight, V2M (v ))
3334
3435weight (ws:: WeightSpace ) = ws. weight
3536weight_size (ws:: WeightSpace ) = length (weight (ws))
36- space (ws:: WeightSpace ) = ws. space
37- Base. size (ws:: WeightSpace ) = size (ws. space, 1 )
38- dim (ws:: WeightSpace ) = size (ws. space, 2 )
37+ matrix (ws:: WeightSpace ) = ws. matrix
38+ Base. size (ws:: WeightSpace ) = size (ws. matrix, 1 )
39+ dim (ws:: WeightSpace ) = size (ws. matrix, 2 )
40+ Base. getindex (ws:: WeightSpace , inds... ) = ws. matrix[inds... ]
41+ Base. setindex! (ws:: WeightSpace , M:: AbstractVecOrMat{T} , inds... ) where {T <: Number } = ws. matrix[inds... ] = M
42+
43+ function Base. hcat (ws1:: WeightSpace , ws2:: WeightSpace )
44+ if weight (ws1) != weight (ws2)
45+ throw (ArgumentError (" Weights of the two spaces must be equal" ))
46+ end
47+ if size (ws1) != size (ws2)
48+ throw (ArgumentError (" Sizes of the two spaces must be equal" ))
49+ end
50+ return WeightSpace (weight (ws1), hcat (matrix (ws1), matrix (ws2)))
51+ end
3952
4053function Base. show (io:: IO , :: MIME"text/plain" , ws:: WeightSpace )
4154 println (io, " WeightSpace of dimension $(dim (ws)) " )
4861
4962function Base. iterate (ws:: WeightSpace , state= 1 )
5063 state > dim (ws) && return nothing
51- return (WeightVector (weight (ws), space (ws)[:, state]), state+ 1 )
64+ return (WeightVector (weight (ws), matrix (ws)[:, state]), state+ 1 )
5265end
5366
54- function intersect_spaces (A:: AbstractMatrix{T} , B:: AbstractMatrix{T} ) where {T <: Number }
67+ function Base.: ∩ (A:: AbstractMatrix{T} , B:: AbstractMatrix{T} ) where {T <: Number }
5568 N = nullspace (hcat (A, B))
5669 N = hcat ([div_by_lowest_magnitude (N[:,i], 1e-8 ) for i in 1 : size (N, 2 )]. .. )
5770 sparsify! (N, 1e-8 )
6073
6174function Base.:∩ (ws1:: WeightSpace , ws2:: WeightSpace )
6275 new_weight = vcat (weight (ws1), weight (ws2))
63- new_space = intersect_spaces (space (ws1), space (ws2))
64- iszero (size (new_space, 2 )) && return nothing
65- return WeightSpace (new_weight, new_space)
66- end
67-
68- function Base.:∩ (ws1:: Vector{WeightSpace} , ws2:: Vector{WeightSpace} )
69- new_spaces = [ws1[i] ∩ ws2[j] for i in 1 : length (ws1), j in 1 : length (ws2)]
70- return [ws for ws in new_spaces if ! isnothing (ws)]
76+ new_matrix = ∩ (matrix (ws1), matrix (ws2))
77+ iszero (size (new_matrix, 2 )) && return nothing
78+ return WeightSpace (new_weight, new_matrix)
7179end
7280
73- Base.:∩ (ws:: Vector{Vector{WeightSpace}} ) = reduce (∩ , ws)
74-
7581
7682struct WeightStructure
77- weight_spaces :: Vector{WeightSpace }
78- dict:: Dict{Vector{Int}, Matrix{ComplexF64}}
83+ weights :: Vector{Vector{Int} }
84+ dict:: Dict{Vector{Int}, WeightSpace} # weight => space
7985end
8086
8187WeightStructure (
8288 weights:: Vector{Vector{Int}} ,
8389 weight_spaces:: Vector{Matrix{T}} where T <: Number
8490) = WeightStructure (
85- [ WeightSpace (w, M) for (w, M) in zip ( weights, weight_spaces)] ,
86- Dict (zip (weights, weight_spaces))
91+ weights,
92+ Dict (zip (weights, [ WeightSpace (w, M) for (w, M) in zip (weights, weight_spaces)] ))
8793)
8894
8995WeightStructure (
@@ -103,20 +109,30 @@ WeightStructure(
103109 weight_vectors:: Vector{Vector{T}} where T <: Number
104110) = WeightStructure (weights, [V2M (v) for v in weight_vectors])
105111
112+ function WeightStructure (w_spaces:: Vector{WeightSpace} )
113+ ws = WeightStructure ()
114+ for w_space in w_spaces
115+ push! (ws, w_space)
116+ end
117+ return ws
118+ end
119+
106120weight_spaces (
107121 ws:: WeightStructure ;
108122 as_matrices:: Bool = false
109- ) = as_matrices ? [space (wsp) for wsp in ws. weight_spaces] : ws. weight_spaces
123+ ) = as_matrices ? [matrix (ws[w]) for w in weights (ws)] : [ws[w] for w in weights (ws)]
124+
110125weight_spaces (
111126 ws:: WeightStructure ,
112127 inds... ;
113128 as_matrices:: Bool = false
114129) = weight_spaces (ws; as_matrices= as_matrices)[inds... ]
115- weight_size (ws:: WeightStructure ) = weight_size (ws[1 ])
116- space_dim (ws:: WeightStructure ) = size (ws[1 ])
117- weights (ws:: WeightStructure ) = [weight (ws) for ws in weight_spaces (ws)]
118- weight (ws:: WeightStructure , i:: Integer ) = weight (weight_spaces (ws)[i])
119- nweights (ws:: WeightStructure ) = length (weight_spaces (ws))
130+
131+ weights (ws:: WeightStructure ) = ws. weights
132+ weight_size (ws:: WeightStructure ) = length (first (weights (ws)))
133+ space_dim (ws:: WeightStructure ) = size (ws[first (weights (ws))])
134+ weight (ws:: WeightStructure , i:: Integer ) = weights (ws)[i]
135+ nweights (ws:: WeightStructure ) = length (weights (ws))
120136dims (ws:: WeightStructure ) = [dim (M) for M in weight_spaces (ws)]
121137
122138function Base. show (io:: IO , ws:: WeightStructure )
@@ -126,30 +142,46 @@ function Base.show(io::IO, ws::WeightStructure)
126142 print (io, " max weight space dimension: " , maximum (dims (ws)))
127143end
128144
129- Base. getindex (ws:: WeightStructure , i :: Integer ) = ws . weight_spaces[i]
145+ Base. length (ws:: WeightStructure ) = nweights (ws)
130146Base. getindex (ws:: WeightStructure , weight:: Vector{Int} ) = ws. dict[weight]
147+ Base. getindex (ws:: WeightStructure , i:: Integer ) = ws[weight (ws, i)]
148+ Base. setindex! (ws:: WeightStructure , ws_new:: WeightSpace , weight:: Vector{Int} ) = ws. dict[weight] = ws_new
131149Base. haskey (ws:: WeightStructure , weight:: Vector{Int} ) = haskey (ws. dict, weight)
132150
133- function Base. push! (ws:: WeightStructure , wv:: WeightVector )
134- if haskey (ws, weight (wv))
135- ws. dict[weight (wv)] = hcat (ws[weight (wv)], space (wv))
151+ function Base. iterate (ws:: WeightStructure , state= 1 )
152+ state > nweights (ws) && return nothing
153+ return (ws[state], state+ 1 )
154+ end
155+
156+ function Base. push! (ws:: WeightStructure , w_space:: WeightSpace )
157+ if haskey (ws, weight (w_space))
158+ ws[weight (w_space)] = hcat (ws[weight (w_space)], w_space)
136159 else
137- wsp = WeightSpace (wv)
138- push! (ws. weight_spaces, wsp)
139- ws. dict[weight (wv)] = space (wsp)
160+ push! (weights (ws), weight (w_space))
161+ ws[weight (w_space)] = w_space
140162 end
163+ return ws
141164end
142165
143- function sum_weight_structure (ws:: WeightStructure , d:: Integer )
144- new_weight_spaces = [zeros (ComplexF64, space_dim (ws)* d, size (M, 2 )* d) for M in weight_spaces (ws)]
145- for (M, new_M) in zip (weight_spaces (ws), new_weight_spaces)
146- for j in 1 : d
147- new_M[(1 : space_dim (ws)) .+ (j- 1 )* space_dim (ws), (1 : size (M,2 )) .+ (j- 1 )* size (M,2 )] = M
148- end
149- end
150- return WeightStructure (weights (ws), new_weight_spaces)
166+ Base. push! (ws:: WeightStructure , wv:: WeightVector ) = push! (ws, WeightSpace (wv))
167+
168+ function Base.:∩ (ws1:: WeightStructure , ws2:: WeightStructure )
169+ new_spaces = [w_space₁ ∩ w_space₂ for w_space₁ in ws1, w_space₂ in ws2]
170+ return WeightStructure ([ws for ws in new_spaces if ! isnothing (ws)])
151171end
152172
173+ Base.:∩ (ws:: Vector{WeightStructure} ) = reduce (∩ , ws)
174+
175+ # function sum_weight_structure(ws::WeightStructure, d::Integer)
176+ # new_weight_spaces = [zeros(ComplexF64, space_dim(ws)*d, size(M, 2)*d) for M in weight_spaces(ws)]
177+ # for (M, new_M) in zip(weight_spaces(ws), new_weight_spaces)
178+ # for j in 1:d
179+ # new_M[(1:space_dim(ws)) .+ (j-1)*space_dim(ws), (1:size(M,2)) .+ (j-1)*size(M,2)] = M
180+ # end
181+ # end
182+ # return WeightStructure(weights(ws), new_weight_spaces)
183+ # end
184+
153185function sym_weight_structure (ws:: WeightStructure , d:: Integer , mexps:: Vector{<:SparseVector} )
154186 d = Int (d)
155187 d == 0 && return WeightStructure ([zeros (Int, weight_size (ws))], [[1 ;;]])
@@ -241,5 +273,4 @@ coefficients(we::WeightExpression) = we.coeffs
241273basis (we:: WeightExpression ) = we. basis
242274weight (we:: WeightExpression ) = we. weight
243275expression (we:: WeightExpression ) = dot (coefficients (we), to_expressions (basis (we)))
244-
245276WeightVector (we:: WeightExpression ) = WeightVector (weight (we), coefficients (we))
0 commit comments