예제 #1
0
        public static MetaNum Min(this IList <MetaNum> self)
        {
            if (self.Count == 0)
            {
                return(TypeTrait <MetaNum> .GetNaNOrRaiseException("No elements"));
            }

            var i = 0;

            while (TypeTrait <MetaNum> .IsNaN(self[0]) && i < self.Count - 1)
            {
                ++i;
            }

            MetaNum min = self[i];

            for (++i; i < self.Count; ++i)
            {
                if (self[i] < min)
                {
                    min = self[i];
                }
            }

            return(min);
        }
예제 #2
0
        public static MetaNum Mode(this IList <MetaNum> self, bool skipNaN = true)
        {
            if (self.Count == 0)
            {
                return(TypeTrait <MetaNum> .GetNaNOrRaiseException("No elements"));
            }

            var values = self.ToArray();

            Array.Sort(values);

            MetaNum currentValue = values[0];
            MetaNum bestValue    = currentValue;
            var     currentCount = 1;
            var     bestCount    = 1;

            int i = 1;

            if (skipNaN)
            {
                // After sort, NaNs should be collected to the first location of the sequence.
                while (TypeTrait <MetaNum> .IsNaN(values[i]))
                {
                    ++i;
                }
            }

            for (; i < values.Length; ++i)
            {
                if (currentValue == values[i])
                {
                    currentCount += 1;
                }
                else
                {
                    currentValue = values[i];
                    currentCount = 1;
                }

                if (currentCount > bestCount)
                {
                    bestCount = currentCount;
                    bestValue = currentValue;
                }
            }

            return(bestValue);
        }