| | | 1 | | using System.Linq.Expressions; |
| | | 2 | | |
| | | 3 | | #pragma warning disable IDE1006 // Naming Styles |
| | | 4 | | |
| | | 5 | | namespace Towel; |
| | | 6 | | |
| | | 7 | | /// <summary>Contains generic static numerical constant values.</summary> |
| | | 8 | | /// <typeparam name="T">The generic numeric type of the constants.</typeparam> |
| | | 9 | | public static class Constant<T> |
| | | 10 | | { |
| | | 11 | | internal static bool _zero_assigned; |
| | | 12 | | internal static bool _one_assigned; |
| | | 13 | | internal static bool _two_assigned; |
| | | 14 | | internal static bool _three_assigned; |
| | | 15 | | internal static bool _four_assigned; |
| | | 16 | | internal static bool _ten_assigned; |
| | | 17 | | internal static bool _negativeOne_assigned; |
| | | 18 | | internal static bool _pi_assigned; |
| | | 19 | | internal static bool _pi2_assigned; |
| | | 20 | | internal static bool _piOver2_assigned; |
| | | 21 | | internal static bool _pi3Over2_assigned; |
| | | 22 | | internal static bool _fourOverPiSquared_assigned; |
| | | 23 | | internal static bool _fourOverπSquared_assigned; |
| | | 24 | | internal static bool _negative4OverPiSquared_assigned; |
| | | 25 | | internal static bool _negative4OverπSquared_assigned; |
| | | 26 | | |
| | | 27 | | #pragma warning disable CS8618 // Non-nullable field must contain a non-null value when exiting constructor. Consider de |
| | | 28 | | |
| | | 29 | | internal static T _zero; |
| | | 30 | | internal static T _one; |
| | | 31 | | internal static T _two; |
| | | 32 | | internal static T _three; |
| | | 33 | | internal static T _four; |
| | | 34 | | internal static T _ten; |
| | | 35 | | internal static T _negativeOne; |
| | | 36 | | internal static T _pi; |
| | | 37 | | internal static T _pi2; |
| | | 38 | | internal static T _piOver2; |
| | | 39 | | internal static T _pi3Over2; |
| | | 40 | | internal static T _fourOverPiSquared; |
| | | 41 | | internal static T _fourOverπSquared; |
| | | 42 | | internal static T _negative4OverPiSquared; |
| | | 43 | | internal static T _negative4OverπSquared; |
| | | 44 | | |
| | | 45 | | #pragma warning restore CS8618 // Non-nullable field must contain a non-null value when exiting constructor. Consider de |
| | | 46 | | |
| | | 47 | | /// <summary>Zero [0]</summary> |
| | | 48 | | public static T Zero |
| | | 49 | | { |
| | | 50 | | get |
| | 1357656 | 51 | | { |
| | 1357656 | 52 | | if (!_zero_assigned) |
| | 4 | 53 | | { |
| | 4 | 54 | | _zero = Convert<int, T>(0); |
| | 4 | 55 | | _zero_assigned = true; |
| | 4 | 56 | | } |
| | 1357656 | 57 | | return _zero; |
| | 1357656 | 58 | | } |
| | | 59 | | set |
| | 4 | 60 | | { |
| | 4 | 61 | | _zero = value; |
| | 4 | 62 | | _zero_assigned = true; |
| | 4 | 63 | | } |
| | | 64 | | } |
| | | 65 | | |
| | | 66 | | /// <summary>One [1]</summary> |
| | | 67 | | public static T One |
| | | 68 | | { |
| | | 69 | | get |
| | 2325 | 70 | | { |
| | 2325 | 71 | | if (!_one_assigned) |
| | 4 | 72 | | { |
| | 4 | 73 | | _one = Convert<int, T>(1); |
| | 4 | 74 | | _one_assigned = true; |
| | 4 | 75 | | } |
| | 2325 | 76 | | return _one; |
| | 2325 | 77 | | } |
| | | 78 | | set |
| | 4 | 79 | | { |
| | 4 | 80 | | _one = value; |
| | 4 | 81 | | _one_assigned = true; |
| | 4 | 82 | | } |
| | | 83 | | } |
| | | 84 | | |
| | | 85 | | /// <summary>Two [2]</summary> |
| | | 86 | | public static T Two |
| | | 87 | | { |
| | | 88 | | get |
| | 1729655 | 89 | | { |
| | 1729655 | 90 | | if (!_two_assigned) |
| | 4 | 91 | | { |
| | 4 | 92 | | _two = Convert<int, T>(2); |
| | 4 | 93 | | _two_assigned = true; |
| | 4 | 94 | | } |
| | 1729655 | 95 | | return _two; |
| | 1729655 | 96 | | } |
| | | 97 | | set |
| | 4 | 98 | | { |
| | 4 | 99 | | _two = value; |
| | 4 | 100 | | _two_assigned = true; |
| | 4 | 101 | | } |
| | | 102 | | } |
| | | 103 | | |
| | | 104 | | /// <summary>Three [3]</summary> |
| | | 105 | | public static T Three |
| | | 106 | | { |
| | | 107 | | get |
| | 123282 | 108 | | { |
| | 123282 | 109 | | if (!_three_assigned) |
| | 4 | 110 | | { |
| | 4 | 111 | | _three = Convert<int, T>(3); |
| | 4 | 112 | | _three_assigned = true; |
| | 4 | 113 | | } |
| | 123282 | 114 | | return _three; |
| | 123282 | 115 | | } |
| | | 116 | | set |
| | 4 | 117 | | { |
| | 4 | 118 | | _three = value; |
| | 4 | 119 | | _three_assigned = true; |
| | 4 | 120 | | } |
| | | 121 | | } |
| | | 122 | | |
| | | 123 | | /// <summary>Four [4]</summary> |
| | | 124 | | public static T Four |
| | | 125 | | { |
| | | 126 | | get |
| | 4 | 127 | | { |
| | 4 | 128 | | if (!_four_assigned) |
| | 4 | 129 | | { |
| | 4 | 130 | | _four = Convert<int, T>(4); |
| | 4 | 131 | | _four_assigned = true; |
| | 4 | 132 | | } |
| | 4 | 133 | | return _four; |
| | 4 | 134 | | } |
| | | 135 | | set |
| | 4 | 136 | | { |
| | 4 | 137 | | _four = value; |
| | 4 | 138 | | _four_assigned = true; |
| | 4 | 139 | | } |
| | | 140 | | } |
| | | 141 | | |
| | | 142 | | /// <summary>Ten [10]</summary> |
| | | 143 | | public static T Ten |
| | | 144 | | { |
| | | 145 | | get |
| | 4 | 146 | | { |
| | 4 | 147 | | if (!_ten_assigned) |
| | 4 | 148 | | { |
| | 4 | 149 | | _ten = Convert<int, T>(10); |
| | 4 | 150 | | _ten_assigned = true; |
| | 4 | 151 | | } |
| | 4 | 152 | | return _ten; |
| | 4 | 153 | | } |
| | | 154 | | set |
| | 4 | 155 | | { |
| | 4 | 156 | | _ten = value; |
| | 4 | 157 | | _ten_assigned = true; |
| | 4 | 158 | | } |
| | | 159 | | } |
| | | 160 | | |
| | | 161 | | /// <summary>Negative One [-1]</summary> |
| | | 162 | | public static T NegativeOne |
| | | 163 | | { |
| | | 164 | | get |
| | 170 | 165 | | { |
| | 170 | 166 | | if (!_negativeOne_assigned) |
| | 4 | 167 | | { |
| | 4 | 168 | | _negativeOne = Convert<int, T>(-1); |
| | 4 | 169 | | _negativeOne_assigned = true; |
| | 4 | 170 | | } |
| | 170 | 171 | | return _negativeOne; |
| | 170 | 172 | | } |
| | | 173 | | set |
| | 4 | 174 | | { |
| | 4 | 175 | | _negativeOne = value; |
| | 4 | 176 | | _negativeOne_assigned = true; |
| | 4 | 177 | | } |
| | | 178 | | } |
| | | 179 | | |
| | | 180 | | /// <summary>π [3.14...]</summary> |
| | | 181 | | public static T Pi |
| | | 182 | | { |
| | | 183 | | get |
| | 66 | 184 | | { |
| | 66 | 185 | | if (!_pi_assigned) |
| | 4 | 186 | | { |
| | 4 | 187 | | _pi = ComputePi(); |
| | 4 | 188 | | _pi_assigned = true; |
| | 4 | 189 | | } |
| | 66 | 190 | | return _pi; |
| | 66 | 191 | | } |
| | | 192 | | set |
| | 4 | 193 | | { |
| | 4 | 194 | | _pi = value; |
| | 4 | 195 | | _pi_assigned = true; |
| | 4 | 196 | | } |
| | | 197 | | } |
| | | 198 | | |
| | | 199 | | /// <summary>π [3.14...]</summary> |
| | | 200 | | public static T π |
| | | 201 | | { |
| | | 202 | | get |
| | 0 | 203 | | { |
| | 0 | 204 | | if (!_pi_assigned) |
| | 0 | 205 | | { |
| | 0 | 206 | | _pi = ComputePi(); |
| | 0 | 207 | | _pi_assigned = true; |
| | 0 | 208 | | } |
| | 0 | 209 | | return _pi; |
| | 0 | 210 | | } |
| | | 211 | | set |
| | 0 | 212 | | { |
| | 0 | 213 | | _pi = value; |
| | 0 | 214 | | _pi_assigned = true; |
| | 0 | 215 | | } |
| | | 216 | | } |
| | | 217 | | |
| | | 218 | | /// <summary>2π [6.28...]</summary> |
| | | 219 | | public static T Pi2 |
| | | 220 | | { |
| | | 221 | | get |
| | 6 | 222 | | { |
| | 6 | 223 | | if (!_pi2_assigned) |
| | 4 | 224 | | { |
| | 4 | 225 | | _pi2 = Multiplication(Two, Pi); |
| | 4 | 226 | | _pi2_assigned = true; |
| | 4 | 227 | | } |
| | 6 | 228 | | return _pi2; |
| | 6 | 229 | | } |
| | | 230 | | set |
| | 4 | 231 | | { |
| | 4 | 232 | | _pi2 = value; |
| | 4 | 233 | | _pi2_assigned = true; |
| | 4 | 234 | | } |
| | | 235 | | } |
| | | 236 | | |
| | | 237 | | /// <summary>2π [6.28...]</summary> |
| | | 238 | | public static T π2 |
| | | 239 | | { |
| | | 240 | | get |
| | 0 | 241 | | { |
| | 0 | 242 | | if (!_pi2_assigned) |
| | 0 | 243 | | { |
| | 0 | 244 | | _pi2 = Multiplication(Two, Pi); |
| | 0 | 245 | | _pi2_assigned = true; |
| | 0 | 246 | | } |
| | 0 | 247 | | return _pi2; |
| | 0 | 248 | | } |
| | | 249 | | set |
| | 0 | 250 | | { |
| | 0 | 251 | | _pi2 = value; |
| | 0 | 252 | | _pi2_assigned = true; |
| | 0 | 253 | | } |
| | | 254 | | } |
| | | 255 | | |
| | | 256 | | /// <summary>π / 2</summary> |
| | | 257 | | public static T PiOver2 |
| | | 258 | | { |
| | | 259 | | get |
| | 4 | 260 | | { |
| | 4 | 261 | | if (!_piOver2_assigned) |
| | 4 | 262 | | { |
| | 4 | 263 | | _piOver2 = Division(Pi, Two); |
| | 4 | 264 | | _piOver2_assigned = true; |
| | 4 | 265 | | } |
| | 4 | 266 | | return _piOver2; |
| | 4 | 267 | | } |
| | | 268 | | set |
| | 4 | 269 | | { |
| | 4 | 270 | | _piOver2 = value; |
| | 4 | 271 | | _piOver2_assigned = true; |
| | 4 | 272 | | } |
| | | 273 | | } |
| | | 274 | | |
| | | 275 | | /// <summary>π / 2</summary> |
| | | 276 | | public static T πOver2 |
| | | 277 | | { |
| | | 278 | | get |
| | 0 | 279 | | { |
| | 0 | 280 | | if (!_piOver2_assigned) |
| | 0 | 281 | | { |
| | 0 | 282 | | _piOver2 = Division(Pi, Two); |
| | 0 | 283 | | _piOver2_assigned = true; |
| | 0 | 284 | | } |
| | 0 | 285 | | return _piOver2; |
| | 0 | 286 | | } |
| | | 287 | | set |
| | 0 | 288 | | { |
| | 0 | 289 | | _piOver2 = value; |
| | 0 | 290 | | _piOver2_assigned = true; |
| | 0 | 291 | | } |
| | | 292 | | } |
| | | 293 | | |
| | | 294 | | /// <summary>3π/2</summary> |
| | | 295 | | public static T Pi3Over2 |
| | | 296 | | { |
| | | 297 | | get |
| | 4 | 298 | | { |
| | 4 | 299 | | if (!_pi3Over2_assigned) |
| | 4 | 300 | | { |
| | 4 | 301 | | _pi3Over2 = Division(Multiplication(Three, Pi), Two); |
| | 4 | 302 | | _pi3Over2_assigned = true; |
| | 4 | 303 | | } |
| | 4 | 304 | | return _pi3Over2; |
| | 4 | 305 | | } |
| | | 306 | | set |
| | 4 | 307 | | { |
| | 4 | 308 | | _pi3Over2 = value; |
| | 4 | 309 | | _pi3Over2_assigned = true; |
| | 4 | 310 | | } |
| | | 311 | | } |
| | | 312 | | |
| | | 313 | | /// <summary>3π/2</summary> |
| | | 314 | | public static T π3Over2 |
| | | 315 | | { |
| | | 316 | | get |
| | 0 | 317 | | { |
| | 0 | 318 | | if (!_pi3Over2_assigned) |
| | 0 | 319 | | { |
| | 0 | 320 | | _pi3Over2 = Division(Multiplication(Three, Pi), Two); |
| | 0 | 321 | | _pi3Over2_assigned = true; |
| | 0 | 322 | | } |
| | 0 | 323 | | return _pi3Over2; |
| | 0 | 324 | | } |
| | | 325 | | set |
| | 0 | 326 | | { |
| | 0 | 327 | | _pi3Over2 = value; |
| | 0 | 328 | | _pi3Over2_assigned = true; |
| | 0 | 329 | | } |
| | | 330 | | } |
| | | 331 | | |
| | | 332 | | /// <summary>4/(π^2)</summary> |
| | | 333 | | public static T FourOverPiSquared |
| | | 334 | | { |
| | | 335 | | get |
| | 8 | 336 | | { |
| | 8 | 337 | | if (!_fourOverPiSquared_assigned) |
| | 4 | 338 | | { |
| | 4 | 339 | | _fourOverPiSquared = Division(Multiplication(Three, Pi), Two); |
| | 4 | 340 | | _fourOverPiSquared_assigned = true; |
| | 4 | 341 | | } |
| | 8 | 342 | | return _fourOverPiSquared; |
| | 8 | 343 | | } |
| | | 344 | | set |
| | 4 | 345 | | { |
| | 4 | 346 | | _fourOverPiSquared = value; |
| | 4 | 347 | | _fourOverPiSquared_assigned = true; |
| | 4 | 348 | | } |
| | | 349 | | } |
| | | 350 | | |
| | | 351 | | /// <summary>4/(π^2)</summary> |
| | | 352 | | public static T FourOverπSquared |
| | | 353 | | { |
| | | 354 | | get |
| | 4 | 355 | | { |
| | 4 | 356 | | if (!_fourOverPiSquared_assigned) |
| | 0 | 357 | | { |
| | 0 | 358 | | _fourOverPiSquared = Division(Multiplication(Three, Pi), Two); |
| | 0 | 359 | | _fourOverPiSquared_assigned = true; |
| | 0 | 360 | | } |
| | 4 | 361 | | return _fourOverPiSquared; |
| | 4 | 362 | | } |
| | | 363 | | set |
| | 4 | 364 | | { |
| | 4 | 365 | | _fourOverPiSquared = value; |
| | 4 | 366 | | _fourOverPiSquared_assigned = true; |
| | 4 | 367 | | } |
| | | 368 | | } |
| | | 369 | | |
| | | 370 | | /// <summary>-4/(π^2)</summary> |
| | | 371 | | public static T Negative4OverPiSquared |
| | | 372 | | { |
| | | 373 | | get |
| | 4 | 374 | | { |
| | 4 | 375 | | if (!_negative4OverPiSquared_assigned) |
| | 4 | 376 | | { |
| | 4 | 377 | | _negative4OverPiSquared = Negation(FourOverPiSquared); |
| | 4 | 378 | | _negative4OverPiSquared_assigned = true; |
| | 4 | 379 | | } |
| | 4 | 380 | | return _negative4OverPiSquared; |
| | 4 | 381 | | } |
| | | 382 | | set |
| | 4 | 383 | | { |
| | 4 | 384 | | _negative4OverPiSquared = value; |
| | 4 | 385 | | _negative4OverPiSquared_assigned = true; |
| | 4 | 386 | | } |
| | | 387 | | } |
| | | 388 | | |
| | | 389 | | /// <summary>-4/(π^2)</summary> |
| | | 390 | | public static T Negative4OverπSquared |
| | | 391 | | { |
| | | 392 | | get |
| | 4 | 393 | | { |
| | 4 | 394 | | if (!_negative4OverPiSquared_assigned) |
| | 0 | 395 | | { |
| | 0 | 396 | | _negative4OverPiSquared = Negation(FourOverPiSquared); |
| | 0 | 397 | | _negative4OverPiSquared_assigned = true; |
| | 0 | 398 | | } |
| | 4 | 399 | | return _negative4OverPiSquared; |
| | 4 | 400 | | } |
| | | 401 | | set |
| | 4 | 402 | | { |
| | 4 | 403 | | _negative4OverPiSquared = value; |
| | 4 | 404 | | _negative4OverPiSquared_assigned = true; |
| | 4 | 405 | | } |
| | | 406 | | } |
| | | 407 | | |
| | | 408 | | #region Pi |
| | | 409 | | |
| | | 410 | | /// <summary>Computes the value of pi for the provided generic type.</summary> |
| | | 411 | | /// <param name="predicate">The cancellation token for cutting off computation.</param> |
| | | 412 | | /// <returns>The computed value of pi.</returns> |
| | | 413 | | public static T ComputePi(Predicate<T>? predicate = null) |
| | 4 | 414 | | { |
| | | 415 | | // Series: PI = 2 * (1 + 1/3 * (1 + 2/5 * (1 + 3/7 * (...)))) |
| | | 416 | | // more terms in computation inproves accuracy |
| | | 417 | | |
| | 4 | 418 | | if (predicate is null) |
| | 4 | 419 | | { |
| | 4 | 420 | | int iterations = 0; |
| | 169 | 421 | | predicate = PI => ++iterations < 100; |
| | 4 | 422 | | } |
| | | 423 | | |
| | 4 | 424 | | T pi = Constant<T>.One; |
| | 4 | 425 | | T previous = Constant<T>.Zero; |
| | 338 | 426 | | for (int i = 1; Inequate(previous, pi) && predicate(pi); i++) |
| | 165 | 427 | | { |
| | 165 | 428 | | previous = pi; |
| | 165 | 429 | | pi = Constant<T>.One; |
| | 11822 | 430 | | for (int j = i; j >= 1; j--) |
| | 5746 | 431 | | { |
| | | 432 | | #region Without Custom Runtime Compilation |
| | | 433 | | |
| | | 434 | | // T J = FromInt32<T>(j); |
| | | 435 | | // T a = Add(Multiply(Constant<T>.Two, J), Constant<T>.One); |
| | | 436 | | // T b = Divide(J, a); |
| | | 437 | | // T c = Multiply(b, pi); |
| | | 438 | | // T d = Add(Constant<T>.One, c); |
| | | 439 | | // pi = d; |
| | | 440 | | |
| | | 441 | | #endregion |
| | | 442 | | |
| | 5746 | 443 | | pi = AddMultiplyDivideAddImplementation.Function(Convert<int, T>(j), pi); |
| | 5746 | 444 | | } |
| | 165 | 445 | | pi = Multiplication(Constant<T>.Two, pi); |
| | 165 | 446 | | } |
| | 4 | 447 | | pi = MaximumValue(pi, Constant<T>.Three); |
| | 4 | 448 | | return pi; |
| | 4 | 449 | | } |
| | | 450 | | |
| | | 451 | | internal static class AddMultiplyDivideAddImplementation |
| | | 452 | | { |
| | 4 | 453 | | internal static Func<T, T, T> Function = (j, pi) => |
| | 4 | 454 | | { |
| | 4 | 455 | | ParameterExpression J = Expression.Parameter(typeof(T)); |
| | 4 | 456 | | ParameterExpression PI = Expression.Parameter(typeof(T)); |
| | 4 | 457 | | Expression BODY = Expression.Add( |
| | 4 | 458 | | Expression.Constant(One), |
| | 4 | 459 | | Expression.Multiply( |
| | 4 | 460 | | PI, |
| | 4 | 461 | | Expression.Divide( |
| | 4 | 462 | | J, |
| | 4 | 463 | | Expression.Add( |
| | 4 | 464 | | Expression.Multiply( |
| | 4 | 465 | | Expression.Constant(Two), |
| | 4 | 466 | | J), |
| | 4 | 467 | | Expression.Constant(One))))); |
| | 4 | 468 | | Function = Expression.Lambda<Func<T, T, T>>(BODY, J, PI).Compile(); |
| | 4 | 469 | | return Function(j, pi); |
| | 8 | 470 | | }; |
| | | 471 | | } |
| | | 472 | | |
| | | 473 | | #endregion |
| | | 474 | | |
| | | 475 | | #region Golden Ratio |
| | | 476 | | #if false |
| | | 477 | | |
| | | 478 | | /// <summary>GoldenRatio [(1 + SquareRoot(5)) / 2]</summary> |
| | | 479 | | //public static readonly T GoldenRatio = Symbolics.ParseAndSimplifyToConstant<T>("(1 + SquareRoot(5)) / 2"); |
| | | 480 | | |
| | | 481 | | /// <summary>Epsilon (1.192092896...e-012f)</summary> |
| | | 482 | | //public static readonly T Epsilon = Compute.ComputeEpsilon<T>(); |
| | | 483 | | |
| | | 484 | | #endif |
| | | 485 | | #endregion |
| | | 486 | | |
| | | 487 | | #region Epsilon |
| | | 488 | | #if false |
| | | 489 | | // Note sure if this method will be necessary. |
| | | 490 | | |
| | | 491 | | //internal static T ComputeEpsilon<T>() |
| | | 492 | | //{ |
| | | 493 | | // if (typeof(T) == typeof(float)) |
| | | 494 | | // { |
| | | 495 | | // return (T)(object)float.Epsilon; |
| | | 496 | | // } |
| | | 497 | | // |
| | | 498 | | //} |
| | | 499 | | #endif |
| | | 500 | | #endregion |
| | | 501 | | } |
| | | 502 | | |
| | | 503 | | #pragma warning restore IDE1006 // Naming Styles |