You can not select more than 25 topics Topics must start with a letter or number, can include dashes ('-') and can be up to 35 characters long.

361 lines
13 KiB

4 years ago
4 years ago
4 years ago
4 years ago
4 years ago
4 years ago
4 years ago
4 years ago
4 years ago
4 years ago
4 years ago
4 years ago
4 years ago
4 years ago
4 years ago
4 years ago
4 years ago
4 years ago
4 years ago
4 years ago
4 years ago
4 years ago
4 years ago
4 years ago
4 years ago
4 years ago
4 years ago
4 years ago
4 years ago
4 years ago
4 years ago
4 years ago
4 years ago
4 years ago
4 years ago
4 years ago
4 years ago
4 years ago
4 years ago
4 years ago
4 years ago
4 years ago
4 years ago
4 years ago
4 years ago
4 years ago
  1. using Apewer.Internals;
  2. using System;
  3. using System.Collections.Generic;
  4. using System.Globalization;
  5. using System.Runtime.InteropServices;
  6. using System.Text;
  7. namespace Apewer
  8. {
  9. /// <summary>数值实用工具。</summary>
  10. public static class NumberUtility
  11. {
  12. #region 随机数。
  13. /// <summary>用于生成随机数的时钟位置。</summary>
  14. private static int RandomSeed = 327680;
  15. private static void RandomInit()
  16. {
  17. var now = DateTime.Now;
  18. var timer = (float)((double)(checked((60 * now.Hour + now.Minute) * 60 + now.Second)) + (double)now.Millisecond / 1000.0);
  19. int num1 = RandomSeed;
  20. int num2 = BitConverter.ToInt32(BitConverter.GetBytes(timer), 0);
  21. num2 = ((num2 & 65535) ^ num2 >> 16) << 8;
  22. num1 = (num1 & -16776961) | num2;
  23. RandomSeed = num1;
  24. }
  25. /// <summary>生成不大于 1 的随机数,最小为 0。</summary>
  26. public static float Random(float max = 1F)
  27. {
  28. RandomInit();
  29. int num1 = RandomSeed;
  30. if ((double)max != 0.0)
  31. {
  32. if ((double)max < 0.0)
  33. {
  34. num1 = BitConverter.ToInt32(BitConverter.GetBytes(max), 0);
  35. long vnum2 = (long)num1;
  36. vnum2 &= unchecked((long)((ulong)-1));
  37. num1 = checked((int)(vnum2 + (vnum2 >> 24) & 16777215L));
  38. }
  39. num1 = checked((int)(unchecked((long)num1) * 1140671485L + 12820163L & 16777215L));
  40. }
  41. RandomSeed = num1;
  42. return (float)num1 / 16777216f;
  43. }
  44. /// <summary>生成随机整数,包含最小值和最大值。</summary>
  45. /// <param name="min">最小值。</param>
  46. /// <param name="max">最大值。</param>
  47. /// <returns></returns>
  48. public static int Random(int max, int min = 0)
  49. {
  50. if (max == min) return max;
  51. if (max < min)
  52. {
  53. var temp = max;
  54. max = min;
  55. min = temp;
  56. }
  57. var rate = (max - min + 1) * Random();
  58. var result = min + (int)rate;
  59. return result;
  60. }
  61. #endregion
  62. #region Restrict 约束值范围。
  63. /// <summary>约束值范围,若源值不在范围中,则修改为接近的值。</summary>
  64. public static T Restrict<T>(T origin, T min, T max) where T : IComparable
  65. {
  66. try
  67. {
  68. // 检查条件 min > max : 返回原始值。
  69. var condition = min.CompareTo(max);
  70. if (condition > 0) return origin;
  71. if (origin.CompareTo(min) < 0) return min;
  72. if (origin.CompareTo(max) > 0) return max;
  73. return origin;
  74. }
  75. catch { }
  76. return origin;
  77. }
  78. #endregion
  79. /// <summary>平均值。</summary>
  80. public static double Average(params double[] values)
  81. {
  82. var amount = 0d;
  83. foreach (var i in values) amount += i;
  84. return amount / Convert.ToDouble(values.Length);
  85. }
  86. #region 最小值、最大值。
  87. private static T GetMost<T>(Func<T, T, bool> replace, T[] values)
  88. {
  89. if (values == null) return default;
  90. var length = values.LongLength;
  91. if (length == 0L) return default;
  92. if (length == 1L) return values[0L];
  93. var most = values[0];
  94. for (var i = 1L; i < length; i++)
  95. {
  96. var value = values[i];
  97. if (value == null) continue;
  98. if (most == null)
  99. {
  100. most = value;
  101. }
  102. else
  103. {
  104. var doReplace = replace(most, value);
  105. if (doReplace) most = value;
  106. }
  107. }
  108. return most;
  109. }
  110. // 无法输出正确结果。
  111. // public static T GetMin<T>(params IComparable[] values)
  112. // {
  113. // var result = GetMost((a, b) => a.CompareTo(b) == 1, values);
  114. // return result == null ? default : (T)result;
  115. // }
  116. // 无法输出正确结果。
  117. // public static T GetMin<T>(params IComparable<T>[] values)
  118. // {
  119. // var result = GetMost((a, b) => a.CompareTo((T)b) == 1, values);
  120. // return result == null ? default : (T)result;
  121. // }
  122. // 无法输出正确结果。
  123. // public static T GetMax<T>(params IComparable[] values)
  124. // {
  125. // var result = GetMost((a, b) => a.CompareTo(b) == -1, values);
  126. // return result == null ? default : (T)result;
  127. // }
  128. // 无法输出正确结果。
  129. // public static T GetMax<T>(params IComparable<T>[] values)
  130. // {
  131. // var result = GetMost((a, b) => a.CompareTo((T)b) == -1, values);
  132. // return result == null ? default : (T)result;
  133. // }
  134. /// <summary>最小值。</summary>
  135. public static byte Min(params byte[] values) => GetMost((a, b) => a > b, values);
  136. /// <summary>最小值。</summary>
  137. public static int Min(params int[] values) => GetMost((a, b) => a > b, values);
  138. /// <summary>最小值。</summary>
  139. public static long Min(params long[] values) => GetMost((a, b) => a > b, values);
  140. /// <summary>最小值。</summary>
  141. public static float Min(params float[] values) => GetMost((a, b) => a > b, values);
  142. /// <summary>最小值。</summary>
  143. public static double Min(params double[] values) => GetMost((a, b) => a > b, values);
  144. /// <summary>最小值。</summary>
  145. public static decimal Min(params decimal[] values) => GetMost((a, b) => a > b, values);
  146. /// <summary>最大值。</summary>
  147. public static byte Max(params byte[] values) => GetMost((a, b) => a < b, values);
  148. /// <summary>最大值。</summary>
  149. public static int Max(params int[] values) => GetMost((a, b) => a < b, values);
  150. /// <summary>最大值。</summary>
  151. public static long Max(params long[] values) => GetMost((a, b) => a < b, values);
  152. /// <summary>最大值。</summary>
  153. public static float Max(params float[] values) => GetMost((a, b) => a < b, values);
  154. /// <summary>最大值。</summary>
  155. public static double Max(params double[] values) => GetMost((a, b) => a < b, values);
  156. /// <summary>最大值。</summary>
  157. public static decimal Max(params decimal[] values) => GetMost((a, b) => a < b, values);
  158. #endregion
  159. /// <summary>Pearson Correlation Coefficient 皮尔逊相关系数。计算两个数组的相似度。数组长度不相等时,以 0 填充不足的长度。</summary>
  160. /// <remarks>
  161. /// <para>0.8 ~ 1.0 : 极强相关</para>
  162. /// <para>0.6 ~ 0.8 : 强相关</para>
  163. /// <para>0.4 ~ 0.6 : 中等程度相关</para>
  164. /// <para>0.2 ~ 0.4 : 弱相关</para>
  165. /// <para>0.0 ~ 0.2 : 极弱相关或无相关</para>
  166. /// </remarks>
  167. /// <exception cref="ArgumentException"></exception>
  168. public static double Pearson(double[] bytes1, double[] bytes2)
  169. {
  170. var length1 = bytes1 == null ? 0L : bytes1.LongLength;
  171. var length2 = bytes2 == null ? 0L : bytes2.LongLength;
  172. var length = Math.Max(length1, length2);
  173. if (length < 1) return 1D;
  174. var x = new double[length];
  175. var y = new double[length];
  176. for (var i = 0L; i < length; i++)
  177. {
  178. if (i < length1) x[i] = bytes1[i];
  179. if (i < length2) y[i] = bytes2[i];
  180. }
  181. int n = x.Length; // 数组长度。
  182. double s = 0d; // 分子。
  183. double m1 = 0d, m2 = 0d; // 分母。
  184. double xa = Average(x);
  185. double ya = Average(y);
  186. for (int i = 0; i < n; i++)
  187. {
  188. s += (x[i] - xa) * (y[i] - ya);
  189. m1 += Math.Pow(x[i] - xa, 2);
  190. m2 += Math.Pow(y[i] - ya, 2);
  191. }
  192. double r = s / (Math.Sqrt(m1) * Math.Sqrt(m2));
  193. return r;
  194. }
  195. #region 从 String 转为数值。
  196. // 修剪数值字符串。
  197. private static string Trim(string text)
  198. {
  199. if (string.IsNullOrEmpty(text)) return null;
  200. const string allowed = "0123456789.%+-Ee。";
  201. var input = text.Length;
  202. var output = 0;
  203. var chars = new char[input];
  204. for (var i = 0; i < text.Length; i++)
  205. {
  206. var c = text[i];
  207. if (c == '。') c = '.';
  208. else if (allowed.IndexOf(c) < 0) continue;
  209. chars[output] = c;
  210. output += 1;
  211. }
  212. if (output == input) return text;
  213. if (output < 1) return null;
  214. return new string(chars, 0, output);
  215. }
  216. private static T GetNumber<T>(object @object, Func<string, T> convert, Func<T, double, T> percent = null)
  217. {
  218. if (@object == null) return default(T);
  219. if (@object is T) return (T)@object;
  220. var text = (@object is string) ? (string)@object : "";
  221. var trim = Trim(text);
  222. if (trim == null) return default;
  223. // 获取百分号的个数。
  224. var pow = 0;
  225. while (trim.Length > 0 && trim.EndsWith("%"))
  226. {
  227. trim = trim.Substring(0, trim.Length - 1);
  228. pow += 1;
  229. }
  230. if (trim.Length < 1) return default;
  231. // 转换。
  232. try
  233. {
  234. var value = convert(trim);
  235. if (pow > 0 && percent != null)
  236. {
  237. var denominator = Math.Pow(100, pow);
  238. value = percent(value, denominator);
  239. }
  240. return value;
  241. }
  242. catch { return default; }
  243. }
  244. private static decimal DecimalAsFloat(string text)
  245. {
  246. try { return Convert.ToDecimal(text); } catch { }
  247. try { return decimal.Parse(text, NumberStyles.Float); } catch { }
  248. return default(decimal);
  249. }
  250. /// <summary>获取布尔对象。</summary>
  251. public static bool Boolean(object any)
  252. {
  253. if (any.IsNull()) return false;
  254. if (any is bool _bool) return _bool;
  255. if (any is byte _byte) return _byte == 1;
  256. if (any is sbyte _sbyte) return _sbyte == 1;
  257. if (any is short _short) return _short == 1;
  258. if (any is ushort _ushort) return _ushort == 1;
  259. if (any is int _int) return _int == 1;
  260. if (any is uint _uint) return _uint == 1;
  261. if (any is long _long) return _long == 1;
  262. if (any is ulong _ulong) return _ulong == 1;
  263. if (any is float _float) return _float == 1;
  264. if (any is double _double) return _double == 1;
  265. if (any is decimal _decimal) return _decimal == 1;
  266. if (any is string _string)
  267. {
  268. var lower = TextUtility.Lower(_string);
  269. if (lower == "true" || lower == "yes" || lower == "y" || lower == "1") return true;
  270. }
  271. return false;
  272. }
  273. /// <summary>获取单精度浮点对象。</summary>
  274. public static float Float(object number) => GetNumber(number, Convert.ToSingle, (v, d) => v / Convert.ToSingle(d));
  275. /// <summary>获取单精度浮点对象。</summary>
  276. public static float Single(object number) => GetNumber(number, Convert.ToSingle, (v, d) => v / Convert.ToSingle(d));
  277. /// <summary>获取双精度浮点对象。</summary>
  278. public static double Double(object number) => GetNumber(number, Convert.ToDouble, (v, d) => v / d);
  279. /// <summary>获取 Decimal 对象。</summary>
  280. public static decimal Decimal(object number) => GetNumber(number, DecimalAsFloat, (v, d) => v / Convert.ToDecimal(d));
  281. /// <summary>获取 Byte 对象。</summary>
  282. public static byte Byte(object number) => GetNumber(number, Convert.ToByte);
  283. /// <summary>获取 SByte 对象。</summary>
  284. public static sbyte SByte(object number) => GetNumber(number, Convert.ToSByte);
  285. /// <summary>获取 Int16 对象。</summary>
  286. public static short Int16(object number) => GetNumber(number, Convert.ToInt16);
  287. /// <summary>获取 UInt16 对象。</summary>
  288. public static ushort UInt16(object number) => GetNumber(number, Convert.ToUInt16);
  289. /// <summary>获取 Int32 对象。</summary>
  290. public static int Int32(object number) => GetNumber(number, Convert.ToInt32);
  291. /// <summary>获取 UInt32 对象。</summary>
  292. public static uint UInt32(object number) => GetNumber(number, Convert.ToUInt32);
  293. /// <summary>获取 Int64 对象。</summary>
  294. public static long Int64(object number) => GetNumber(number, Convert.ToInt64);
  295. /// <summary>获取 UInt64 对象。</summary>
  296. public static ulong UInt64(object number) => GetNumber(number, Convert.ToUInt64);
  297. #endregion
  298. }
  299. }