11export is_commutative_semiring
2- export Max2Poly, Polynomial, Tropical, CountingTropical, StaticBitVector , Mod, ConfigEnumerator, onehotv, ConfigSampler
3- export bitstringset_type, bitstringsampler_type
2+ export Max2Poly, Polynomial, Tropical, CountingTropical, StaticElementVector , Mod, ConfigEnumerator, onehotv, ConfigSampler
3+ export set_type, sampler_type
44
55using Polynomials: Polynomial
66using TropicalNumbers: Tropical, CountingTropical
@@ -102,56 +102,56 @@ function Base.show(io::IO, ::MIME"text/plain", x::Max2Poly)
102102 end
103103end
104104
105- struct ConfigEnumerator{N,C}
106- data:: Vector{StaticBitVector{N ,C}}
105+ struct ConfigEnumerator{N,S, C}
106+ data:: Vector{StaticElementVector{N,S ,C}}
107107end
108108
109109Base. length (x:: ConfigEnumerator{N} ) where N = length (x. data)
110- Base.:(== )(x:: ConfigEnumerator{N,C} , y:: ConfigEnumerator{N,C} ) where {N,C} = x. data == y. data
110+ Base.:(== )(x:: ConfigEnumerator{N,S, C} , y:: ConfigEnumerator{N,S, C} ) where {N,S ,C} = x. data == y. data
111111
112- function Base.:+ (x:: ConfigEnumerator{N,C} , y:: ConfigEnumerator{N,C} ) where {N,C}
112+ function Base.:+ (x:: ConfigEnumerator{N,S, C} , y:: ConfigEnumerator{N,S, C} ) where {N,S ,C}
113113 length (x) == 0 && return y
114114 length (y) == 0 && return x
115- return ConfigEnumerator {N,C} (vcat (x. data, y. data))
115+ return ConfigEnumerator {N,S, C} (vcat (x. data, y. data))
116116end
117117
118- function Base.:* (x:: ConfigEnumerator{L,C} , y:: ConfigEnumerator{L,C} ) where {L,C}
118+ function Base.:* (x:: ConfigEnumerator{L,S, C} , y:: ConfigEnumerator{L,S, C} ) where {L,S ,C}
119119 M, N = length (x), length (y)
120120 M == 0 && return x
121121 N == 0 && return y
122- z = Vector {StaticBitVector{L ,C}} (undef, M* N)
122+ z = Vector {StaticElementVector{L,S ,C}} (undef, M* N)
123123 @inbounds for j= 1 : N, i= 1 : M
124124 z[(j- 1 )* M+ i] = x. data[i] | y. data[j]
125125 end
126- return ConfigEnumerator {L,C} (z)
126+ return ConfigEnumerator {L,S, C} (z)
127127end
128128
129- Base. zero (:: Type{ConfigEnumerator{N,C}} ) where {N,C} = ConfigEnumerator {N,C} (StaticBitVector{N ,C}[])
130- Base. one (:: Type{ConfigEnumerator{N,C}} ) where {N,C} = ConfigEnumerator {N,C} ([staticfalses (StaticBitVector{N ,C})])
131- Base. zero (:: ConfigEnumerator{N,C} ) where {N,C} = zero (ConfigEnumerator{N,C})
132- Base. one (:: ConfigEnumerator{N,C} ) where {N,C} = one (ConfigEnumerator{N,C})
129+ Base. zero (:: Type{ConfigEnumerator{N,S, C}} ) where {N,S, C} = ConfigEnumerator {N,S, C} (StaticElementVector{N,S ,C}[])
130+ Base. one (:: Type{ConfigEnumerator{N,S, C}} ) where {N,S, C} = ConfigEnumerator {N,S, C} ([zero (StaticElementVector{N,S ,C})])
131+ Base. zero (:: ConfigEnumerator{N,S, C} ) where {N,S, C} = zero (ConfigEnumerator{N,S ,C})
132+ Base. one (:: ConfigEnumerator{N,S, C} ) where {N,S, C} = one (ConfigEnumerator{N,S ,C})
133133Base. show (io:: IO , x:: ConfigEnumerator ) = print (io, " {" , join (x. data, " , " ), " }" )
134134Base. show (io:: IO , :: MIME"text/plain" , x:: ConfigEnumerator ) = Base. show (io, x)
135135
136136# the algebra sampling one of the configurations
137- struct ConfigSampler{N,C}
138- data:: StaticBitVector{N ,C}
137+ struct ConfigSampler{N,S, C}
138+ data:: StaticElementVector{N,S ,C}
139139end
140140
141- Base.:(== )(x:: ConfigSampler{N,C} , y:: ConfigSampler{N,C} ) where {N,C} = x. data == y. data
141+ Base.:(== )(x:: ConfigSampler{N,S, C} , y:: ConfigSampler{N,S, C} ) where {N,S ,C} = x. data == y. data
142142
143- function Base.:+ (x:: ConfigSampler{N,C} , y:: ConfigSampler{N,C} ) where {N,C} # biased sampling: return `x`, maybe using random sampler is better.
143+ function Base.:+ (x:: ConfigSampler{N,S, C} , y:: ConfigSampler{N,S, C} ) where {N,S ,C} # biased sampling: return `x`, maybe using random sampler is better.
144144 return x
145145end
146146
147- function Base.:* (x:: ConfigSampler{L,C} , y:: ConfigSampler{L,C} ) where {L,C}
147+ function Base.:* (x:: ConfigSampler{L,S, C} , y:: ConfigSampler{L,S, C} ) where {L,S ,C}
148148 ConfigSampler (x. data | y. data)
149149end
150150
151- Base. zero (:: Type{ConfigSampler{N,C}} ) where {N,C} = ConfigSampler {N,C} (statictrues (StaticBitVector{N ,C}))
152- Base. one (:: Type{ConfigSampler{N,C}} ) where {N,C} = ConfigSampler {N,C} (staticfalses (StaticBitVector{N ,C}))
153- Base. zero (:: ConfigSampler{N,C} ) where {N,C} = zero (ConfigSampler{N,C})
154- Base. one (:: ConfigSampler{N,C} ) where {N,C} = one (ConfigSampler{N,C})
151+ Base. zero (:: Type{ConfigSampler{N,S, C}} ) where {N,S, C} = ConfigSampler {N,S, C} (statictrues (StaticElementVector{N,S ,C}))
152+ Base. one (:: Type{ConfigSampler{N,S, C}} ) where {N,S, C} = ConfigSampler {N,S, C} (staticfalses (StaticElementVector{N,S ,C}))
153+ Base. zero (:: ConfigSampler{N,S, C} ) where {N,S, C} = zero (ConfigSampler{N,S ,C})
154+ Base. one (:: ConfigSampler{N,S, C} ) where {N,S, C} = one (ConfigSampler{N,S ,C})
155155
156156# A patch to make `Polynomial{ConfigEnumerator}` work
157157function Base.:* (a:: Int , y:: ConfigEnumerator )
@@ -166,33 +166,34 @@ function Base.:*(a::Int, y::ConfigSampler)
166166end
167167
168168# convert from counting type to bitstring type
169- for (F,TP) in [(:bitstringset_type , :ConfigEnumerator ), (:bitstringsampler_type , :ConfigSampler )]
169+ for (F,TP) in [(:set_type , :ConfigEnumerator ), (:sampler_type , :ConfigSampler )]
170170 @eval begin
171- function $F (:: Type{T} , n:: Int ) where {OT, T<: Max2Poly{C,OT} where C}
172- Max2Poly{$ F (n),OT}
171+ function $F (:: Type{T} , n:: Int , nflavor :: Int ) where {OT, T<: Max2Poly{C,OT} where C}
172+ Max2Poly{$ F (n,nflavor ),OT}
173173 end
174- function $F (:: Type{T} , n:: Int ) where {TX, T<: Polynomial{C,TX} where C}
175- Polynomial{$ F (n),:x }
174+ function $F (:: Type{T} , n:: Int , nflavor :: Int ) where {TX, T<: Polynomial{C,TX} where C}
175+ Polynomial{$ F (n,nflavor ),:x }
176176 end
177- function $F (:: Type{T} , n:: Int ) where {TV, T<: CountingTropical{TV} }
178- CountingTropical{TV, $ F (n)}
177+ function $F (:: Type{T} , n:: Int , nflavor :: Int ) where {TV, T<: CountingTropical{TV} }
178+ CountingTropical{TV, $ F (n,nflavor )}
179179 end
180- function $F (n:: Integer )
181- C = _nints (n)
182- return $ TP{n, C}
180+ function $F (n:: Integer , nflavor:: Integer )
181+ s = ceil (Int, log2 (nflavor))
182+ c = _nints (n,s)
183+ return $ TP{n,s,c}
183184 end
184185 end
185186end
186187
187188# utilities for creating onehot vectors
188- function onehotv (:: Type{Polynomial{BS,X}} , x) where {BS,X}
189- Polynomial {BS,X} ([zero (BS), onehotv (BS, x)])
189+ function onehotv (:: Type{Polynomial{BS,X}} , x, v ) where {BS,X}
190+ Polynomial {BS,X} ([zero (BS), onehotv (BS, x, v )])
190191end
191- function onehotv (:: Type{Max2Poly{BS,OS}} , x) where {BS,OS}
192- Max2Poly {BS,OS} (zero (BS), onehotv (BS, x),one (OS))
192+ function onehotv (:: Type{Max2Poly{BS,OS}} , x, v ) where {BS,OS}
193+ Max2Poly {BS,OS} (zero (BS), onehotv (BS, x, v ),one (OS))
193194end
194- function onehotv (:: Type{CountingTropical{TV,BS}} , x) where {TV,BS}
195- CountingTropical {TV,BS} (one (TV), onehotv (BS, x))
195+ function onehotv (:: Type{CountingTropical{TV,BS}} , x, v ) where {TV,BS}
196+ CountingTropical {TV,BS} (one (TV), onehotv (BS, x, v ))
196197end
197- onehotv (:: Type{ConfigEnumerator{N,C}} , i:: Integer ) where {N,C} = ConfigEnumerator ([onehotv (StaticBitVector {N,C}, i)])
198- onehotv (:: Type{ConfigSampler{N,C}} , i:: Integer ) where {N,C} = ConfigSampler (onehotv (StaticBitVector {N,C}, i))
198+ onehotv (:: Type{ConfigEnumerator{N,S, C}} , i:: Integer , v ) where {N,S, C} = ConfigEnumerator ([onehotv (StaticElementVector {N,S, C}, i, v )])
199+ onehotv (:: Type{ConfigSampler{N,S, C}} , i:: Integer , v ) where {N,S, C} = ConfigSampler (onehotv (StaticElementVector {N,S, C}, i, v ))
0 commit comments