using System; using System.Collections.Generic; using System.Linq; namespace bts.utils { static class MathUtils { public const double epsilon = 1E-12; public static readonly IEqualityComparer epsilon_eq_comparer = new EpsilonComparer(); public static readonly IComparer epsilon_comparer = new ErrorMarginComparer.DoubleImpl(epsilon); public static IComparer error_margin_comparer(double error_margin) => new ErrorMarginComparer.DoubleImpl(error_margin); public static T exchange_if_gt(ref T target, T value) where T : struct, IComparable { if (value.CompareTo(target) > 0) { var orig = target; target = value; return orig; } return target; } public static bool epsilon_equals(double x, double y) => Math.Abs(x - y) < epsilon; public static bool marginal_equals(double x, double y, double margin = epsilon) => Math.Abs(x - y) < margin; public static double GCD(IEnumerable nums) { var no_zeros = nums.Where(n => n != 0); if (!no_zeros.Any()) return 0; double rv = no_zeros.First(); foreach (var num in no_zeros.Skip(1)) rv = GCD(rv, num); return rv; } public static double GCD(double a, double b) { return (double)GCD((decimal)a, (decimal)b); } public static decimal GCD(decimal a, decimal b) { decimal small, large; if (a < b) { small = a; large = b; } else { small = b; large = a; } int i = 1; while (i < 9999) { decimal rv = small / i++; if (large % rv == 0) return rv; } return 0; } public static int LCM(IEnumerable nums) { var no_zeros = nums.Where(n => n != 0); if (!no_zeros.Any()) return 0; int rv = no_zeros.First(); foreach (var num in no_zeros.Skip(1)) rv = LCM(rv, num); return rv; } public static int LCM(int a, int b) { int num1, num2; if (a > b) { num1 = a; num2 = b; } else { num1 = b; num2 = a; } for (int i = 1; i < num2; i++) if ((num1 * i) % num2 == 0) return i * num1; return num1 * num2; } public static bool is_within_range(this double value, double min, double max) => value >= min && value <= max; public static double weighted_average(double x, double x_weight, double y, double y_weight) { if (double.IsNaN(x) || double.IsNaN(x_weight) || x_weight == 0) return y; if (double.IsNaN(y) || double.IsNaN(y_weight) || y_weight == 0) return x; return x + (y - x) * (x_weight / (x_weight + y_weight)); } public static int within_range(int min, int max, int value) { if (value < min) return min; if (value > max) return max; return value; } public static long within_range(long min, long max, long value) { if (value < min) return min; if (value > max) return max; return value; } public static double within_range(double min, double max, double value) { if (value < min) return min; if (value > max) return max; return value; } public static uint within_range(uint min, uint max, uint value) { if (value < min) return min; if (value > max) return max; return value; } public static int RoundFractionalUnits(double value, double fractional_unit) => (int)Math.Round(value / fractional_unit); public static double round_down_abs(double value, double margin = epsilon) { if (value < 0) { return round_up(value, margin); } else if (value > 0) { return round_down(value, margin); } return 0; } public static double round_down(double value, double margin = epsilon) { if (double.IsNaN(value) || double.IsInfinity(value)) return value; var result = Math.Floor(value); if (marginal_equals(value - result, 1, margin)) { result = Math.Ceiling(value); } return result; } public static double round_up_abs(double value, double margin = epsilon) { if (value < 0) { return round_down(value, margin); } else if (value > 0) { return round_up(value, margin); } return 0; } public static double round_up(double value, double margin = epsilon) { if (double.IsNaN(value) || double.IsInfinity(value)) return value; var result = Math.Ceiling(value); if (marginal_equals(result - value, 1, margin)) { result = Math.Floor(value); } return result; } public static bool is_int(double d) => epsilon_equals(d, Math.Round(d)); public static bool is_int(double d, out double with_int_value) { with_int_value = Math.Round(d); return epsilon_equals(with_int_value, d); } public static bool is_real(this double value) { switch (value) { case double.PositiveInfinity: case double.NegativeInfinity: case double.NaN: return false; default: return true; } } public static bool is_real_positive(this double value) => value > 0 && value != double.PositiveInfinity; public static bool is_real_negative(this double value) => value < 0 && value != double.NegativeInfinity; public static bool is_real_non_zero(this double value) => value > 0 ? !double.IsPositiveInfinity(value) : value < 0 && !double.IsNegativeInfinity(value); public static double factor_out(double value, params double[] factors) { foreach (double f in factors) value = factor_out(value, f, out int _); return value; } public static double factor_out(double value, double factor, out int n_factors_removed) { n_factors_removed = 0; if (value == 0) return value; double quotient = value / factor; while (is_int(quotient, out quotient)) { value = quotient; ++n_factors_removed; quotient /= factor; } return value; } public static int safe_divide(this int value, int divisor) => divisor == 0 ? value : value / divisor; public static int safe_divide(this int value, uint divisor) => divisor == 0 ? value : (int)(value / divisor); public static double safe_divide(this int value, double divisor) => divisor.is_real_non_zero() ? value / divisor : value; public static uint safe_divide(this uint value, uint divisor) => divisor == 0 ? value : value / divisor; public static double safe_divide(this uint value, double divisor) => divisor.is_real_non_zero() ? value / divisor : value; public static ulong safe_divide(this ulong value, uint divisor) => divisor == 0 ? value : value / divisor; public static double safe_divide(this double value, double divisor) => divisor.is_real_non_zero() && value.is_real_non_zero() ? value / divisor : value; /// /// Adds to the modulos result if negative. /// public static int positive_mod(int index, int mod) { int result = index % mod; return result < 0 ? result + mod : result; } public static (T min, T max) get_outliers(params T[] values) where T : struct, IComparable { if (values.Length == 0) { return (default, default); } switch (values.Length) { case 0: return (default, default); case 1: return (values[0], values[0]); default: T min = values[0], max = values[values.Length - 1]; if (min.CompareTo(max) > 0) _swap(ref min, ref max); for (int i = 1; i < values.Length; i++) { if (min.CompareTo(values[i]) > 0) { _swap(ref values[i], ref min); } else if (max.CompareTo(values[i]) < 0) { _swap(ref values[i], ref max); } } return (min, max); } } static void _swap(ref T a, ref T b) { var temp = a; a = b; b = temp; } public static class Primes { public const int P_1087 = 1087, P_2423 = 2423, P_17191 = 17191, P_25097 = 25097, P_48247 = 48247, P_65449 = 65449, // 2^16 (65536) between these 2 values P_68909 = 68909, P_83987 = 83987, P_356219 = 356219, P_919223 = 919223, P_1276777 = 1276777, P_2492003 = 2492003; } struct EpsilonComparer : IEqualityComparer { public bool Equals(double x, double y) => x.Equals(y) || Math.Abs(x - y) < epsilon; public int GetHashCode(double d) => Convert.ToInt32(d); } static class ErrorMarginComparer { public class DoubleImpl : IComparer { readonly double _error_margin; public DoubleImpl(double error_margin) => _error_margin = error_margin < 0 ? throw new ArgumentException("Error margin must be greater than or equal to zero.", nameof(error_margin)) : error_margin; public int Compare(double x, double y) { double diff = x - y; if (diff <= _error_margin && diff >= -_error_margin) return 0; return diff > 0 ? 1 : -1; } } } } }