Пример #1
0
        /// <summary>
        ///     Sums a series of long integers.
        /// </summary>
        /// <param name="vars">Zero or more long integers to sum.</param>
        /// <returns>The sum of the long integers.</returns>
        /// <exception cref="GetRealGood.Utils.MathsException">
        ///     You should catch this exception, it denotes any exceptional
        ///     condition within the method.
        /// </exception>
        public static long Sum(params long[] vars)
        {
            if (Objects.IsNull(vars))
            {
                throw new MathsException("Null value passed in");
            }

            if (vars.Length == 0)
            {
                return(0);
            }

            long result = 0;

            try
            {
                checked
                {
                    foreach (var v in vars)
                    {
                        result += v;
                    }
                }
            }
            catch (OverflowException soEx)
            {
                throw new MathsException(soEx.Message);
            }
            catch (Exception ex)
            {
                throw new MathsException(ex.Message);
            }

            return(result);
        }
Пример #2
0
        /// <summary>
        ///     Multiplies a double float by a sum of long integers.
        /// </summary>
        /// <param name="value">The double float to put to the power of.</param>
        /// <param name="vars">The sum of long integers of the power of.</param>
        /// <returns>The power of double float to the sum of long integers.</returns>
        /// <exception cref="GetRealGood.Utils.MathsException">
        ///     You should catch this exception, it denotes any exceptional
        ///     condition within the method.
        /// </exception>
        public static double PowerOf(double value, params long[] toThePowersOf)
        {
            if (Objects.IsNull(toThePowersOf))
            {
                throw new MathsException("Null value passed in");
            }

            if (toThePowersOf.Length == 0)
            {
                throw new MathsException("Empty value passed in");
            }

            double result;

            try
            {
                checked
                {
                    long?totalPowerOf = Sum(toThePowersOf);

                    if (Objects.IsNull(totalPowerOf))
                    {
                        throw new Exception();
                    }

                    switch (totalPowerOf)
                    {
                    case 0:
                        return(1);

                    case 1:
                        return(value);

                    default:
                        result = Math.Pow(value, totalPowerOf.Value);
                        break;
                    }
                }
            }
            catch (OverflowException oEx)
            {
                throw new MathsException(oEx.Message);
            }
            catch (Exception ex)
            {
                throw new MathsException(ex.Message);
            }

            if (double.IsPositiveInfinity(result))
            {
                throw new MathsException("Positive overflow");
            }
            if (double.IsNegativeInfinity(result))
            {
                throw new MathsException("Negative overflow");
            }


            return(result);
        }
Пример #3
0
        /// <summary>
        ///     Subtracts a series of long integers from a long integer.
        /// </summary>
        /// <param name="value">The long integer to subtract from.</param>
        /// <param name="vars">Zero or more long integers to subtract.</param>
        /// <returns>The subtraction of the long integers.</returns>
        /// <exception cref="GetRealGood.Utils.MathsException">
        ///     You should catch this exception, it denotes any exceptional
        ///     condition within the method.
        /// </exception>
        public static long Subtract(long value, params long[] vars)
        {
            if (Objects.IsNull(vars))
            {
                throw new MathsException("Null vars parameter passed in");
            }

            if (vars.Length == 0)
            {
                return(value);
            }

            var result = value;

            try
            {
                checked
                {
                    foreach (var v in vars)
                    {
                        result -= v;
                    }
                }
            }
            catch (OverflowException soEx)
            {
                throw new MathsException(soEx.Message);
            }
            catch (Exception ex)
            {
                throw new MathsException(ex.Message);
            }

            return(result);
        }
        /// <summary>
        ///
        /// </summary>
        /// <param name="coll"></param>
        /// <typeparam name="T"></typeparam>
        /// <returns></returns>
        /// <exception cref="CollectionsException"></exception>
        public static IList <T> Peaks <T>(IEnumerable <T> coll) where T : IComparable <T>
        {
            if (Objects.IsNull(coll))
            {
                throw new CollectionsException("Null parameter passed in for coll");
            }

            if (!coll.Any())
            {
                throw new CollectionsException("Empty parameter passed in for coll");
            }

            T         max   = default(T);
            IList <T> peaks = new List <T>();

            if (coll.Count() == 1)
            {
                var iter = coll.GetEnumerator();
                iter.MoveNext();
                max = iter.Current;
                return(new List <T>()
                {
                    max
                });
            }

            int i = 0;

            foreach (T t in coll)
            {
                if (Objects.IsNull(t))
                {
                    continue;
                }

                if (i++ == 0)
                {
                    max = t;
                    peaks.Add(max);
                }
                else if (t.CompareTo(max) == 0)
                {
                    peaks.Add(t);
                }
                else if (t.CompareTo(max) == 1)
                {
                    peaks.Clear();
                    max = t;
                    peaks.Add(max);
                }
            }

            if (peaks.Count() == 0)
            {
                throw new CollectionsException("Array contains only nulls");
            }

            return(peaks);
        }
Пример #5
0
        /// <summary>
        ///     Multiplies a double float by a sum of double floats.
        /// </summary>
        /// <param name="value">The double float to put to the power of.</param>
        /// <param name="vars">The sum of double floats of the power of.</param>
        /// <returns>The power of double float to the sum of double floats.</returns>
        /// <exception cref="GetRealGood.Utils.MathsException">
        ///     You should catch this exception, it denotes any exceptional
        ///     condition within the method.
        /// </exception>
        public static double PowerOf(double value, params double[] toThePowersOf)
        {
            if (Objects.IsNull(toThePowersOf))
            {
                throw new MathsException("Null value passed in");
            }

            if (toThePowersOf.Length == 0)
            {
                throw new MathsException("Empty value passed in");
            }

            var result = value;

            try
            {
                checked
                {
                    double?totalPowerOf = Sum(toThePowersOf);

                    if (Objects.IsNull(totalPowerOf))
                    {
                        throw new Exception();
                    }

                    if (totalPowerOf == 0)
                    {
                        return(1);
                    }

                    if (totalPowerOf == 1)
                    {
                        return(value);
                    }

                    result = Math.Pow(value, totalPowerOf.Value);
                }
            }
            catch (OverflowException oEx)
            {
                throw new MathsException(oEx.Message);
            }
            catch (Exception ex)
            {
                throw new MathsException(ex.Message);
            }

            if (double.IsPositiveInfinity(result))
            {
                throw new MathsException("Positive overflow");
            }
            if (double.IsNegativeInfinity(result))
            {
                throw new MathsException("Negative overflow");
            }

            return(result);
        }
Пример #6
0
        /// <summary>
        ///
        /// </summary>
        /// <param name="coll"></param>
        /// <typeparam name="T"></typeparam>
        /// <returns></returns>
        /// <exception cref="ArraysException"></exception>
        public static IList <T> Troughs <T>(IEnumerable <T> coll) where T : IComparable <T>
        {
            if (Objects.IsNull(coll))
            {
                throw new ArraysException("Null parameter passed in for coll");
            }

            if (!coll.Any())
            {
                throw new ArraysException("Empty parameter passed in for coll");
            }

            T         min     = default(T);
            IList <T> troughs = new List <T>();

            if (coll.Count() == 1)
            {
                using var iter = coll.GetEnumerator();
                iter.MoveNext();
                min = iter.Current;
                return(new List <T>()
                {
                    min
                });
            }

            int i = 0;

            foreach (T t in coll)
            {
                if (Objects.IsNull(t))
                {
                    continue;
                }
                if (i++ == 0)
                {
                    min = t;
                    troughs.Add(min);
                }
                else if (t.CompareTo(min) == 0)
                {
                    troughs.Add(t);
                }
                else if (t.CompareTo(min) == -1)
                {
                    troughs.Clear();
                    min = t;
                    troughs.Add(min);
                }
            }

            if (troughs.Count() == 0)
            {
                throw new ArraysException("Array contains only nulls");
            }

            return(troughs);
        }
Пример #7
0
        /// <summary>
        ///
        /// </summary>
        /// <param name="s"></param>
        /// <param name="orig"></param>
        /// <param name="w"></param>
        /// <returns></returns>
        /// <exception cref="StringsException"></exception>
        public static string Replace(string s, char orig, char w)
        {
            if (Objects.IsNull(s))
            {
                throw new StringsException("Null value passed in for s");
            }

            return(Replace(s, orig, w, false));
        }
        /// <summary>
        ///
        /// </summary>
        /// <param name="o"></param>
        /// <returns></returns>
        /// /// <exception cref="CollectionsException"></exception>
        public static bool IsNotEmpty(IEnumerable <object> o)
        {
            if (Objects.IsNull(o))
            {
                throw new CollectionsException("Null parameter passed in for o");
            }

            return(o.Any());
        }
        /// <summary>
        ///
        /// </summary>
        /// <typeparam name="T"></typeparam>
        /// <param name="coll"></param>
        /// <returns></returns>
        /// <exception cref="CollectionsException"></exception>
        public static long HasNullsCount <T>(IEnumerable <T> coll)
        {
            if (Objects.IsNull(coll))
            {
                throw new CollectionsException("Null parameter passed in for coll");
            }

            return(HasNullsAt(coll).Count());
        }
Пример #10
0
        /// <summary>
        ///
        /// </summary>
        /// <param name="s"></param>
        /// <returns></returns>
        /// <exception cref="StringsException"></exception>
        public static string ToLower(string s)
        {
            if (Objects.IsNull(s))
            {
                throw new StringsException("Null value passed in for s");
            }

            return(s.ToLowerInvariant());
        }
        /// <summary>
        ///
        /// </summary>
        /// <param name="coll"></param>
        /// <typeparam name="T"></typeparam>
        /// <returns></returns>
        /// <exception cref="CollectionsException"></exception>
        public static T Max <T>(IEnumerable <T> coll) where T : IComparable <T>
        {
            if (Objects.IsNull(coll))
            {
                throw new CollectionsException("Null parameter passed in for coll");
            }

            if (!coll.Any())
            {
                throw new CollectionsException("Empty enumerable passed in for coll");
            }

            T max;


            var iter = coll.GetEnumerator();

            if (iter.MoveNext())
            {
                max = iter.Current; // first location
            }
            else
            {
                throw new CollectionsException("Empty collection passed in");
            }

            if (coll.Count() == 1)
            {
                return(max);
            }

            iter.Reset();

            foreach (T t in coll)
            {
                if (Objects.IsNull(t))
                {
                    continue;
                }
                if (t.CompareTo(max) == 0)
                {
                    continue;
                }
                if (t.CompareTo(max) == 1)
                {
                    max = t;
                }
            }

            if (Objects.IsNull(max))
            {
                throw new CollectionsException("Array contains only nulls");
            }

            return(max);
        }
Пример #12
0
        /// <summary>
        ///     Divides a double float by a series of double floats.
        /// </summary>
        /// <param name="value">The double float to divide.</param>
        /// <param name="vars">Zero or more double floats to divide by.</param>
        /// <returns>The division of the double floats.</returns>
        /// <exception cref="GetRealGood.Utils.MathsException">
        ///     You should catch this exception, it denotes any exceptional
        ///     condition within the method.
        /// </exception>
        public static double Divide(double value, params double[] vars)
        {
            if (Objects.IsNull(vars))
            {
                throw new MathsException("Null value passed in");
            }

            if (vars.Length == 0)
            {
                return(value);
            }

            var result = value;

            try
            {
                foreach (var v in vars)
                {
                    if (v == 0.00D)
                    {
                        throw new DivideByZeroException();
                    }
                    result /= v;
                }
            }
            catch (OverflowException soEx)
            {
                throw new MathsException(soEx.Message);
            }
            catch (DivideByZeroException ex)
            {
                throw new MathsException(ex.Message);
            }
            catch (Exception ex)
            {
                throw new MathsException(ex.Message);
            }

            if (double.IsPositiveInfinity(result))
            {
                throw new MathsException("Positive overflow");
            }
            if (double.IsNegativeInfinity(result))
            {
                throw new MathsException("Negative overflow");
            }

            return(result);
        }
Пример #13
0
        /// <summary>
        ///
        /// </summary>
        /// <param name="s"></param>
        /// <param name="token"></param>
        /// <param name="trim"></param>
        /// <returns></returns>
        /// <exception cref="StringsException"></exception>
        public static string Concat(IList <string> s, string token, bool trim)
        {
            if (Objects.IsNull(s))
            {
                throw new StringsException("Null value passed in for s");
            }

            string result  = new string("");
            int    counter = 0;
            int    length  = s.Count() - 1;

            foreach (var e in s)
            {
                if (trim)
                {
                    if (e.Length == 0)
                    {
                        continue;
                    }
                    result += e.Trim();
                }
                else
                {
                    if (e.Length == 0)
                    {
                        continue;
                    }
                    result += e;
                }

                if (Objects.IsNull(token))
                {
                    continue;
                }
                else
                {
                    if (counter < length)
                    {
                        result += token;
                        counter++;
                    }
                }
            }

            return(result);
        }
        /// <summary>
        ///
        /// </summary>
        /// <typeparam name="T"></typeparam>
        /// <param name="coll"></param>
        /// <returns></returns>
        /// <exception cref="CollectionsException"></exception>
        public static bool HasNulls <T>(IEnumerable <T> coll)
        {
            if (Objects.IsNull(coll))
            {
                throw new CollectionsException("Null parameter passed in for coll");
            }



            foreach (var v in coll)
            {
                if (Objects.IsNull(v))
                {
                    return(true);
                }
            }

            return(false);
        }
Пример #15
0
        /// <summary>
        ///     Sums a series of double floats.
        /// </summary>
        /// <param name="vars">Zero or more double floats to sum.</param>
        /// <returns>The sum of the double floats.</returns>
        /// <exception cref="GetRealGood.Utils.MathsException">
        ///     You should catch this exception, it denotes any exceptional
        ///     condition within the method.
        /// </exception>
        public static double Sum(params double[] vars)
        {
            if (Objects.IsNull(vars))
            {
                throw new MathsException("Null value passed in");
            }

            if (vars.Length == 0)
            {
                return(0);
            }

            double result = 0;

            try
            {
                foreach (var v in vars)
                {
                    result += v;
                }
            }
            catch (OverflowException soEx)
            {
                throw new MathsException(soEx.Message);
            }
            catch (Exception ex)
            {
                throw new MathsException(ex.Message);
            }

            if (double.IsPositiveInfinity(result))
            {
                throw new MathsException("Positive overflow");
            }
            if (double.IsNegativeInfinity(result))
            {
                throw new MathsException("Negative overflow");
            }

            return(result);
        }
Пример #16
0
        /// <summary>
        ///
        /// </summary>
        /// <param name="s"></param>
        /// <param name="token"></param>
        /// <param name="trim"></param>
        /// <returns></returns>
        /// <exception cref="StringsException"></exception>
        public static IList <string> Split(string s, char token, bool trim)
        {
            if (Objects.IsNull(s))
            {
                throw new StringsException("Null value passed in for s");
            }

            if (s.Length == 0)
            {
                return(new List <string>(0));
            }

            IList <string> result = s.Split(token).ToList();

            if (trim)
            {
                result = result.Select(a => a.Trim()).ToList();
            }

            return(result);
        }
        /// <summary>
        ///
        /// </summary>
        /// <typeparam name="T"></typeparam>
        /// <param name="coll"></param>
        /// <returns></returns>
        /// <exception cref="CollectionsException"></exception>
        public static IList <long> HasNullsAt <T>(IEnumerable <T> coll)
        {
            if (Objects.IsNull(coll))
            {
                throw new CollectionsException("Null parameter passed in for coll");
            }

            long         i = 0;
            IList <long> s = new List <long>(0);

            foreach (var v in coll)
            {
                if (Objects.IsNull(v))
                {
                    s.Add(i);
                }
                i++;
            }

            return(s);
        }
Пример #18
0
        /// <summary>
        ///     Divides a long integer by a series of long integers.
        /// </summary>
        /// <param name="value">The long integer to divide.</param>
        /// <param name="vars">Zero or more long integers to divide by.</param>
        /// <returns>The division of the long integers.</returns>
        /// <exception cref="GetRealGood.Utils.MathsException">
        ///     You should catch this exception, it denotes any exceptional
        ///     condition within the method.
        /// </exception>
        public static long Divide(long value, params long[] vars)
        {
            if (Objects.IsNull(vars))
            {
                throw new MathsException("Null value passed in");
            }

            if (vars.Length == 0)
            {
                return(value);
            }

            var result = value;

            try
            {
                foreach (var v in vars)
                {
                    if (v == 0L)
                    {
                        throw new DivideByZeroException();
                    }
                    result /= v;
                }
            }
            catch (OverflowException soEx)
            {
                throw new MathsException(soEx.Message);
            }
            catch (DivideByZeroException ex)
            {
                throw new MathsException(ex.Message);
            }
            catch (Exception ex)
            {
                throw new MathsException(ex.Message);
            }

            return(result);
        }
Пример #19
0
        /// <summary>
        ///
        /// </summary>
        /// <param name="s"></param>
        /// <param name="orig"></param>
        /// <param name="w"></param>
        /// /// <param name="trim"></param>
        /// <returns></returns>
        /// <exception cref="StringsException"></exception>
        public static string Replace(string s, char orig, char w, bool trim)
        {
            if (Objects.IsNull(s))
            {
                throw new StringsException("Null value passed in for s");
            }

            if (trim)
            {
                s = s.Trim();
            }

            char[] chars = s.ToCharArray();

            for (var i = 0; i < chars.Length; i++)
            {
                if (chars[i] == orig)
                {
                    chars[i] = w;
                }
            }

            return(new string(chars));
        }
Пример #20
0
        /// <summary>
        /// </summary>
        /// <param name="a"></param>
        /// <param name="b"></param>
        /// <param name="c"></param>
        /// <returns></returns>
        /// /// <exception cref="MathsException"></exception>
        public static IList <double?> Quadratic(double a, double?b, double?c)
        {
            var result = new double?[2];

            try
            {
                // a is never null or zero
                if (a == 0.00D)
                {
                    throw new MathsException("Coefficient a is zero");
                }

                b = Objects.IsNull(b) ? 0.00D : b;
                c = Objects.IsNull(c) ? 0.00D : c;

                if (b == 0.00D && c == 0.00D)
                {
                    // only one solution
                    return new List <double?> {
                               0.00D, null
                    }
                }
                ;

                checked
                {
                    var partOne = -b;

                    if (double.IsInfinity(partOne.Value))
                    {
                        throw new OverflowException("Overflow detected");
                    }

                    if (double.IsNegativeInfinity(partOne.Value))
                    {
                        throw new OverflowException("Underflow detected");
                    }

                    var bSquare = b.Value * b.Value;

                    if (double.IsInfinity(bSquare))
                    {
                        throw new OverflowException("Overflow detected");
                    }

                    if (double.IsNegativeInfinity(bSquare))
                    {
                        throw new OverflowException("Underflow detected");
                    }

                    var fourAC = 4.00D * a * c.Value;

                    if (double.IsInfinity(fourAC))
                    {
                        throw new OverflowException("Overflow detected");
                    }

                    if (double.IsNegativeInfinity(fourAC))
                    {
                        throw new OverflowException("Underflow detected");
                    }

                    var minus = bSquare - fourAC;

                    if (double.IsInfinity(minus))
                    {
                        throw new OverflowException("Overflow detected");
                    }

                    if (double.IsNegativeInfinity(minus))
                    {
                        throw new OverflowException("Underflow detected");
                    }

                    var partTwo = Math.Sqrt(minus);

                    if (double.IsInfinity(partTwo))
                    {
                        throw new OverflowException("Overflow detected");
                    }

                    if (double.IsNegativeInfinity(partTwo))
                    {
                        throw new OverflowException("Underflow detected");
                    }

                    var partThree = 2.00D * a;

                    if (double.IsInfinity(partThree))
                    {
                        throw new OverflowException("Overflow detected");
                    }

                    if (double.IsNegativeInfinity(partThree))
                    {
                        throw new OverflowException("Underflow detected");
                    }

                    result[0] = (partOne + partTwo) / partThree;
                    result[1] = (partOne - partTwo) / partThree;

                    if (double.IsInfinity(result[0].Value))
                    {
                        throw new OverflowException("Overflow detected");
                    }

                    if (double.IsNegativeInfinity(result[0].Value))
                    {
                        throw new OverflowException("Underflow detected");
                    }

                    if (double.IsInfinity(result[1].Value))
                    {
                        throw new OverflowException("Overflow detected");
                    }

                    if (double.IsNegativeInfinity(result[1].Value))
                    {
                        throw new OverflowException("Underflow detected");
                    }
                }
            }
            catch (OverflowException ovEx)
            {
                throw new MathsException(ovEx.Message);
            }
            catch (Exception ex)
            {
                throw new MathsException(ex.Message);
            }

            return(result);
        }