/// <summary> /// [Thread-Unsafe] Get multiple normal distribution random values of <see cref="double"/>. /// </summary> /// <param name="count"></param> /// <returns></returns> public double[] NextNorm64(int count) { var half = count / 2; var retval = NextFloat64(count); for (var i = 0; i < half; ++i) { var j = i << 1; // 2 * i var x = retval[j]; var y = retval[j + 1]; var mag = NdMath.Sqrt(-2 * NdMath.Log(x)); var ang = 2 * NdMath.PI <double>() * y; retval[j] = mag * NdMath.Cos(ang); retval[j + 1] = mag * NdMath.Sin(ang); } if ((count & 1) == 1) // count % 2 == 1 { var x = retval[count - 1]; var y = NextFloat64(); retval[count - 1] = NdMath.Sqrt(-2 * NdMath.Log(x)) * NdMath.Cos(2 * NdMath.PI <double>() * y); } return(retval); }
/// <summary> /// [Thread-Unsafe] Get single normal distribution random value of <see cref="float"/>. /// </summary> /// <returns></returns> public float NextNorm32() { var x = NextFloat32(); var y = NextFloat32(); return(NdMath.Sqrt(-2 * NdMath.Log(x)) * NdMath.Cos(2 * NdMath.PI <float>() * y)); }
/// <summary> /// [Thread-Unsafe] Get single normal distribution random value of <see cref="double"/>. /// </summary> /// <returns></returns> public double NextNorm64() { var x = NextFloat64(); var y = NextFloat64(); return(NdMath.Sqrt(-2 * NdMath.Log(x)) * NdMath.Cos(2 * NdMath.PI <double>() * y)); }
/// <summary> /// [Pure] Returns the 2-D norm of all elements of <paramref name="ndArray"/>. /// </summary> /// <param name="ndArray"></param> /// <returns></returns> public static T Norm2 <T>(this INdArray <T> ndArray) { var len = ndArray.Shape.TotalLength; Guard.AssertOperation(len > 0, "ndArray has no elements."); var norm = ValueTrait.Zero <T>(); for (var i = 0; i < len; ++i) { var element = ndArray.GetItem(i); norm = ValueTrait.Add(norm, ValueTrait.Multiply(element, element)); } return(NdMath.Sqrt(norm)); }
/// <summary> /// Computes sample variance from all elements of the <paramref name="ndArray"/>. /// </summary> /// <typeparam name="T"></typeparam> /// <param name="ndArray"></param> /// <returns></returns> public static T StdDev <T>(this INdArray <T> ndArray) => NdMath.Sqrt(ndArray.SampleVar());