예제 #1
0
        public static M Sum <M>(this IAdditiveMonoid <M> g, IEnumerable <M> source)
        {
            var sum = g.Zero();

            foreach (var value in source)
            {
                sum = g.Add(sum, value);
            }
            return(sum);
        }
예제 #2
0
        //For this definition one should usually assume that IAdditiveMonoid<M> is abelian
        public static M ScalarMult <M>(this IAdditiveMonoid <M> g, int n, M a)
        {
            if (n < 0)
            {
                throw new ArgumentException($"n={n} < 0!");
            }
            var res = g.Zero();

            for (var i = 0; i < n; i++)
            {
                res = g.Add(res, a);
            }
            return(res);
        }
예제 #3
0
 public static M Sum <M>(this IAdditiveMonoid <M> g, params M[] source)
 {
     return(g.Sum(source.ToList()));
 }
예제 #4
0
        public static M SumNSmallest <M>(this IAdditiveMonoid <M> g, IEnumerable <M> source, int n)
        {
            var nsmallest = g.GetNSmallest(source, n);

            return(g.Sum(nsmallest));
        }