Beispiel #1
0
        public static DenormalizedProbability <int> Get(int lb, int ub)
        {
#if DEBUG
            if (lb > ub)
            {
                throw new InvalidOperationException("lb > ub");
            }
#endif
            if (lb == ub)
            {
                return(ConstantDistribution <int> .Get(lb));
            }
            var index = new KeyValuePair <int, int>(lb, ub);
            if (_cache.TryGetValue(index, out var val))
            {
                return(new DenormalizedProbability <int>(val));
            }
            var raw = new Dictionary <int, float>();
            for (int x = lb; x <= ub; x++)
            {
                raw[x] = 1;
            }
            var ret = new DenormalizedProbability <int>(raw);
            ret.Normalize();
            _cache[index] = new DenormalizedProbability <int>(ret);
            return(ret);
        }
Beispiel #2
0
        public static DenormalizedProbability <T> Apply(DenormalizedProbability <KeyValuePair <T, T> > src, Func <T, T, T> op)
        {
#if DEBUG
            if (null == op)
            {
                throw new ArgumentNullException(nameof(op));
            }
            if (null == src)
            {
                throw new ArgumentNullException(nameof(src));
            }
#endif
            var ret = new DenormalizedProbability <T>();
            foreach (var x in src._weights)
            {
                ret[op(x.Key.Key, x.Key.Value)] += x.Value;
            }
            ret.Normalize();
            return(ret);
        }