1
1
export 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
4
4
5
5
using Polynomials: Polynomial
6
6
using TropicalNumbers: Tropical, CountingTropical
@@ -102,56 +102,56 @@ function Base.show(io::IO, ::MIME"text/plain", x::Max2Poly)
102
102
end
103
103
end
104
104
105
- struct ConfigEnumerator{N,C}
106
- data:: Vector{StaticBitVector{N ,C}}
105
+ struct ConfigEnumerator{N,S, C}
106
+ data:: Vector{StaticElementVector{N,S ,C}}
107
107
end
108
108
109
109
Base. 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
111
111
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}
113
113
length (x) == 0 && return y
114
114
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))
116
116
end
117
117
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}
119
119
M, N = length (x), length (y)
120
120
M == 0 && return x
121
121
N == 0 && return y
122
- z = Vector {StaticBitVector{L ,C}} (undef, M* N)
122
+ z = Vector {StaticElementVector{L,S ,C}} (undef, M* N)
123
123
@inbounds for j= 1 : N, i= 1 : M
124
124
z[(j- 1 )* M+ i] = x. data[i] | y. data[j]
125
125
end
126
- return ConfigEnumerator {L,C} (z)
126
+ return ConfigEnumerator {L,S, C} (z)
127
127
end
128
128
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})
133
133
Base. show (io:: IO , x:: ConfigEnumerator ) = print (io, " {" , join (x. data, " , " ), " }" )
134
134
Base. show (io:: IO , :: MIME"text/plain" , x:: ConfigEnumerator ) = Base. show (io, x)
135
135
136
136
# 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}
139
139
end
140
140
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
142
142
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.
144
144
return x
145
145
end
146
146
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}
148
148
ConfigSampler (x. data | y. data)
149
149
end
150
150
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})
155
155
156
156
# A patch to make `Polynomial{ConfigEnumerator}` work
157
157
function Base.:* (a:: Int , y:: ConfigEnumerator )
@@ -166,33 +166,34 @@ function Base.:*(a::Int, y::ConfigSampler)
166
166
end
167
167
168
168
# 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 )]
170
170
@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}
173
173
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 }
176
176
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 )}
179
179
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}
183
184
end
184
185
end
185
186
end
186
187
187
188
# 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 )])
190
191
end
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))
193
194
end
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 ))
196
197
end
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