示例#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);
        }
示例#2
0
        // overload operator *
        public static DenormalizedProbability <KeyValuePair <T, T> > operator *(DenormalizedProbability <T> lhs, DenormalizedProbability <T> rhs)
        {
#if DEBUG
            if (null == lhs)
            {
                throw new ArgumentNullException(nameof(lhs));
            }
            if (null == rhs)
            {
                throw new ArgumentNullException(nameof(rhs));
            }
#endif
            var ret = new DenormalizedProbability <KeyValuePair <T, T> >();
            foreach (var x in lhs._weights)
            {
                foreach (var y in rhs._weights)
                {
                    // \todo reality-check tmp before risking overflow
                    var tmp = x.Value * y.Value;
                    if (0f >= tmp)
                    {
                        continue;
                    }
                    ret[new KeyValuePair <T, T>(x.Key, y.Key)] = tmp;
                }
            }
            return(ret);
        }
示例#3
0
        static public float LessThan(this int lhs, DenormalizedProbability <int> rhs)
        {
            DenormalizedProbability <int> LHS = ConstantDistribution <int> .Get(lhs);

            DenormalizedProbability <int> ret_src = DenormalizedProbability <int> .Apply(LHS *rhs, _less_than);

            return(ret_src[1]);
        }
示例#4
0
        public DenormalizedProbability(DenormalizedProbability <T> src)
        {
#if DEBUG
            if (null == src)
            {
                throw new ArgumentNullException(nameof(src));
            }
#endif
            _weights = new Dictionary <T, float>(src._weights);
            Norm     = new Dataflow <Dictionary <T, float>, double>(_weights, _current_norm);
        }
示例#5
0
        public static DenormalizedProbability <T> Get(T x)
        {
            if (_cache.TryGetValue(x, out var val))
            {
                return(new DenormalizedProbability <T>(val));
            }
            var raw = new Dictionary <T, float> {
                [x] = 1
            };
            var ret = new DenormalizedProbability <T>(raw);

            _cache[x] = new DenormalizedProbability <T>(ret);
            return(ret);
        }
示例#6
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);
        }
示例#7
0
        static public float LessThan(this DenormalizedProbability <int> lhs, DenormalizedProbability <int> rhs)
        {
            DenormalizedProbability <int> ret_src = DenormalizedProbability <int> .Apply(lhs *rhs, _less_than);

            return(ret_src[1]);
        }