11export is_commutative_semiring
2- export Max2Poly, Polynomial, Tropical, CountingTropical, ConfigTropical, StaticBitVector, Mod, ConfigEnumerator
2+ export Max2Poly, Polynomial, Tropical, CountingTropical, StaticBitVector, Mod, ConfigEnumerator, onehotv
33
44using Polynomials: Polynomial
5- using TropicalNumbers: Tropical, CountingTropical, ConfigTropical, StaticBitVector
5+ using TropicalNumbers: Tropical, CountingTropical, StaticBitVector
66using Mods, Primes
77
8+ # patch for Tropical numbers
9+ Base. isnan (x:: Tropical ) = isnan (x. n)
10+
811# pirate
912Base. abs (x:: Mod ) = x
1013Base. isless (x:: Mod{N} , y:: Mod{N} ) where N = mod (x. val, N) < mod (y. val, N)
@@ -120,10 +123,61 @@ Base.one(::ConfigEnumerator{N,C}) where {N,C} = one(ConfigEnumerator{N,C})
120123Base. show (io:: IO , x:: ConfigEnumerator ) = print (io, " {" , join (x. data, " , " ), " }" )
121124Base. show (io:: IO , :: MIME"text/plain" , x:: ConfigEnumerator ) = Base. show (io, x)
122125
123- # patch
126+ # the algebra sampling one of the configurations
127+ struct ConfigSampler{N,C}
128+ data:: StaticBitVector{N,C}
129+ end
130+
131+ Base.:(== )(x:: ConfigSampler{N,C} , y:: ConfigSampler{N,C} ) where {N,C} = x. data == y. data
132+
133+ function Base.:+ (x:: ConfigSampler{N,C} , y:: ConfigSampler{N,C} ) where {N,C} # biased sampling: return `x`, maybe using random sampler is better.
134+ return x
135+ end
136+
137+ function Base.:* (x:: ConfigSampler{L,C} , y:: ConfigSampler{L,C} ) where {L,C}
138+ ConfigSampler (x. data | y. data)
139+ end
140+
141+ Base. zero (:: Type{ConfigSampler{N,C}} ) where {N,C} = ConfigSampler {N,C} (TropicalNumbers. statictrues (StaticBitVector{N,C}))
142+ Base. one (:: Type{ConfigSampler{N,C}} ) where {N,C} = ConfigSampler {N,C} (TropicalNumbers. staticfalses (StaticBitVector{N,C}))
143+ Base. zero (:: ConfigSampler{N,C} ) where {N,C} = zero (ConfigSampler{N,C})
144+ Base. one (:: ConfigSampler{N,C} ) where {N,C} = one (ConfigSampler{N,C})
124145
146+ # A patch to make `Polynomial{ConfigEnumerator}` work
125147function Base.:* (a:: Int , y:: ConfigEnumerator )
126148 a == 0 && return zero (y)
127149 a == 1 && return y
128150 error (" multiplication between int and config enumerator is not defined." )
129- end
151+ end
152+
153+ # convert from counting type to bitstring type
154+ for (F,TP) in [(:bitstringset_type , :ConfigEnumerator ), (:bitstringsampler_type , :ConfigSampler )]
155+ @eval begin
156+ function $F (:: Type{T} , n:: Int ) where {T<: Max2Poly }
157+ Max2Poly{$ F (n)}
158+ end
159+ function $F (:: Type{T} , n:: Int ) where {TX, T<: Polynomial{C,TX} where C}
160+ Polynomial{$ F (n),:x }
161+ end
162+ function $F (:: Type{T} , n:: Int ) where {TV, T<: CountingTropical{TV} }
163+ CountingTropical{TV, $ F (n)}
164+ end
165+ function $F (n:: Integer )
166+ C = TropicalNumbers. _nints (n)
167+ return $ TP{n, C}
168+ end
169+ end
170+ end
171+
172+ # utilities for creating onehot vectors
173+ function onehotv (:: Type{Polynomial{BS,X}} , x) where {BS,X}
174+ Polynomial {BS,X} ([zero (BS), onehotv (BS, x)])
175+ end
176+ function onehotv (:: Type{Max2Poly{BS}} , x) where {BS}
177+ Max2Poly {BS} (zero (BS), onehotv (BS, x),1 )
178+ end
179+ function onehotv (:: Type{CountingTropical{TV,BS}} , x) where {TV,BS}
180+ CountingTropical {TV,BS} (one (TV), onehotv (BS, x))
181+ end
182+ onehotv (:: Type{ConfigEnumerator{N,C}} , i:: Integer ) where {N,C} = ConfigEnumerator ([TropicalNumbers. onehot (StaticBitVector{N,C}, i)])
183+ onehotv (:: Type{ConfigSampler{N,C}} , i:: Integer ) where {N,C} = ConfigSampler (TropicalNumbers. onehot (StaticBitVector{N,C}, i))
0 commit comments