From b5019c1a5f6cbf95447645b75f15fdc2a624b6da Mon Sep 17 00:00:00 2001 From: wangfeng Date: Thu, 2 Feb 2023 08:06:32 +0800 Subject: [PATCH 01/28] =?UTF-8?q?=E8=B0=83=E6=95=B4imports?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- algorithms/float64.go | 120 +++++++++++++++++++++--------------------- 1 file changed, 60 insertions(+), 60 deletions(-) diff --git a/algorithms/float64.go b/algorithms/float64.go index 465d7cb..40b4727 100644 --- a/algorithms/float64.go +++ b/algorithms/float64.go @@ -1,7 +1,7 @@ package algorithms import ( - m "math" + "math" ) // Round returns the nearest integer, rounding half away from zero. @@ -11,7 +11,7 @@ import ( // Round(±0) = ±0 // Round(±Inf) = ±Inf // Round(NaN) = NaN -func Round(x float64) float64 { return m.Round(x) } +func Round(x float64) float64 { return math.Round(x) } // RoundToEven returns the nearest integer, rounding ties to even. // @@ -20,19 +20,19 @@ func Round(x float64) float64 { return m.Round(x) } // RoundToEven(±0) = ±0 // RoundToEven(±Inf) = ±Inf // RoundToEven(NaN) = NaN -func RoundToEven(x float64) float64 { return m.RoundToEven(x) } +func RoundToEven(x float64) float64 { return math.RoundToEven(x) } // NaN returns an IEEE 754 “not-a-number” value. -func NaN() float64 { return m.NaN() } +func NaN() float64 { return math.NaN() } // IsNaN reports whether f is an IEEE 754 “not-a-number” value. -func IsNaN(x float64) bool { return m.IsNaN(x) } +func IsNaN(x float64) bool { return math.IsNaN(x) } // IsInf reports whether f is an infinity, according to sign. // If sign > 0, IsInf reports whether f is positive infinity. // If sign < 0, IsInf reports whether f is negative infinity. // If sign == 0, IsInf reports whether f is either infinity. -func IsInf(f float64, sign int) bool { return m.IsInf(f, sign) } +func IsInf(f float64, sign int) bool { return math.IsInf(f, sign) } // Sqrt returns the square root of x. // @@ -42,7 +42,7 @@ func IsInf(f float64, sign int) bool { return m.IsInf(f, sign) } // Sqrt(±0) = ±0 // Sqrt(x < 0) = NaN // Sqrt(NaN) = NaN -func Sqrt(x float64) float64 { return m.Sqrt(x) } +func Sqrt(x float64) float64 { return math.Sqrt(x) } // Sin returns the sine of the radian argument x. // @@ -51,7 +51,7 @@ func Sqrt(x float64) float64 { return m.Sqrt(x) } // Sin(±0) = ±0 // Sin(±Inf) = NaN // Sin(NaN) = NaN -func Sin(x float64) float64 { return float64(m.Sin(float64(x))) } +func Sin(x float64) float64 { return float64(math.Sin(float64(x))) } // Cos returns the cosine of the radian argument x. // @@ -59,7 +59,7 @@ func Sin(x float64) float64 { return float64(m.Sin(float64(x))) } // // Cos(±Inf) = NaN // Cos(NaN) = NaN -func Cos(x float64) float64 { return float64(m.Cos(float64(x))) } +func Cos(x float64) float64 { return float64(math.Cos(float64(x))) } // Abs returns the absolute value of x. // @@ -67,14 +67,14 @@ func Cos(x float64) float64 { return float64(m.Cos(float64(x))) } // // Abs(±Inf) = +Inf // Abs(NaN) = NaN -func Abs(x float64) float64 { return float64(m.Abs(float64(x))) } +func Abs(x float64) float64 { return float64(math.Abs(float64(x))) } // Acos returns the arccosine, in radians, of x. // // Special case is: // // Acos(x) = NaN if x < -1 or x > 1 -func Acos(x float64) float64 { return float64(m.Acos(float64(x))) } +func Acos(x float64) float64 { return float64(math.Acos(float64(x))) } // Asin returns the arcsine, in radians, of x. // @@ -82,7 +82,7 @@ func Acos(x float64) float64 { return float64(m.Acos(float64(x))) } // // Asin(±0) = ±0 // Asin(x) = NaN if x < -1 or x > 1 -func Asin(x float64) float64 { return float64(m.Asin(float64(x))) } +func Asin(x float64) float64 { return float64(math.Asin(float64(x))) } // Asinh returns the inverse hyperbolic sine of x. // @@ -91,7 +91,7 @@ func Asin(x float64) float64 { return float64(m.Asin(float64(x))) } // Asinh(±0) = ±0 // Asinh(±Inf) = ±Inf // Asinh(NaN) = NaN -func Asinh(x float64) float64 { return float64(m.Asinh(float64(x))) } +func Asinh(x float64) float64 { return float64(math.Asinh(float64(x))) } // Atan returns the arctangent, in radians, of x. // @@ -99,7 +99,7 @@ func Asinh(x float64) float64 { return float64(m.Asinh(float64(x))) } // // Atan(±0) = ±0 // Atan(±Inf) = ±Pi/2 -func Atan(x float64) float64 { return float64(m.Atan(float64(x))) } +func Atan(x float64) float64 { return float64(math.Atan(float64(x))) } // Atan2 returns the arc tangent of y/x, using // the signs of the two to determine the quadrant @@ -124,7 +124,7 @@ func Atan(x float64) float64 { return float64(m.Atan(float64(x))) } // Atan2(y<0, -Inf) = -Pi // Atan2(+Inf, x) = +Pi/2 // Atan2(-Inf, x) = -Pi/2 -func Atan2(y, x float64) float64 { return float64(m.Atan2(float64(y), float64(x))) } +func Atan2(y, x float64) float64 { return float64(math.Atan2(float64(y), float64(x))) } // Atanh returns the inverse hyperbolic tangent of x. // @@ -135,7 +135,7 @@ func Atan2(y, x float64) float64 { return float64(m.Atan2(float64(y), float64(x) // Atanh(-1) = -Inf // Atanh(x) = NaN if x < -1 or x > 1 // Atanh(NaN) = NaN -func Atanh(x float64) float64 { return float64(m.Atanh(float64(x))) } +func Atanh(x float64) float64 { return float64(math.Atanh(float64(x))) } // Cbrt returns the cube root of x. // @@ -144,7 +144,7 @@ func Atanh(x float64) float64 { return float64(m.Atanh(float64(x))) } // Cbrt(±0) = ±0 // Cbrt(±Inf) = ±Inf // Cbrt(NaN) = NaN -func Cbrt(x float64) float64 { return float64(m.Cbrt(float64(x))) } +func Cbrt(x float64) float64 { return float64(math.Cbrt(float64(x))) } // Ceil returns the least integer value greater than or equal to x. // @@ -153,11 +153,11 @@ func Cbrt(x float64) float64 { return float64(m.Cbrt(float64(x))) } // Ceil(±0) = ±0 // Ceil(±Inf) = ±Inf // Ceil(NaN) = NaN -func Ceil(x float64) float64 { return float64(m.Ceil(float64(x))) } +func Ceil(x float64) float64 { return float64(math.Ceil(float64(x))) } // Copysign returns a value with the magnitude // of x and the sign of y. -func Copysign(x, y float64) float64 { return float64(m.Copysign(float64(x), float64(y))) } +func Copysign(x, y float64) float64 { return float64(math.Copysign(float64(x), float64(y))) } // Cosh returns the hyperbolic cosine of x. // @@ -165,7 +165,7 @@ func Copysign(x, y float64) float64 { return float64(m.Copysign(float64(x), floa // Cosh(±0) = 1 // Cosh(±Inf) = +Inf // Cosh(NaN) = NaN -//func Cosh(x float64) float64 { return float64(m.Cosh(float64(x))) } +//func Cosh(x float64) float64 { return float64(math.Cosh(float64(x))) } // Dim returns the maximum of x-y or 0. // @@ -174,7 +174,7 @@ func Copysign(x, y float64) float64 { return float64(m.Copysign(float64(x), floa // Dim(+Inf, +Inf) = NaN // Dim(-Inf, -Inf) = NaN // Dim(x, NaN) = Dim(NaN, x) = NaN -func Dim(x, y float64) float64 { return float64(m.Dim(float64(x), float64(y))) } +func Dim(x, y float64) float64 { return float64(math.Dim(float64(x), float64(y))) } // Erf returns the error function of x. // @@ -183,7 +183,7 @@ func Dim(x, y float64) float64 { return float64(m.Dim(float64(x), float64(y))) } // Erf(+Inf) = 1 // Erf(-Inf) = -1 // Erf(NaN) = NaN -func Erf(x float64) float64 { return float64(m.Erf(float64(x))) } +func Erf(x float64) float64 { return float64(math.Erf(float64(x))) } // Erfc returns the complementary error function of x. // @@ -192,7 +192,7 @@ func Erf(x float64) float64 { return float64(m.Erf(float64(x))) } // Erfc(+Inf) = 0 // Erfc(-Inf) = 2 // Erfc(NaN) = NaN -func Erfc(x float64) float64 { return float64(m.Erfc(float64(x))) } +func Erfc(x float64) float64 { return float64(math.Erfc(float64(x))) } // Exp returns e**x, the base-e exponential of x. // @@ -203,12 +203,12 @@ func Erfc(x float64) float64 { return float64(m.Erfc(float64(x))) } // // Very large values overflow to 0 or +Inf. // Very small values underflow to 1. -func Exp(x float64) float64 { return float64(m.Exp(float64(x))) } +func Exp(x float64) float64 { return float64(math.Exp(float64(x))) } // Exp2 returns 2**x, the base-2 exponential of x. // // Special cases are the same as Exp. -func Exp2(x float64) float64 { return float64(m.Exp2(float64(x))) } +func Exp2(x float64) float64 { return float64(math.Exp2(float64(x))) } // Expm1 returns e**x - 1, the base-e exponential of x minus 1. // It is more accurate than Exp(x) - 1 when x is near zero. @@ -220,21 +220,21 @@ func Exp2(x float64) float64 { return float64(m.Exp2(float64(x))) } // Expm1(NaN) = NaN // // Very large values overflow to -1 or +Inf. -func Expm1(x float64) float64 { return float64(m.Expm1(float64(x))) } +func Expm1(x float64) float64 { return float64(math.Expm1(float64(x))) } // Float32bits returns the IEEE 754 binary representation of f. -func Float32bits(f float32) uint32 { return m.Float32bits(f) } +func Float32bits(f float32) uint32 { return math.Float32bits(f) } // Float32frombits returns the floating point number corresponding // to the IEEE 754 binary representation b. -func Float32frombits(b uint32) float32 { return m.Float32frombits(b) } +func Float32frombits(b uint32) float32 { return math.Float32frombits(b) } // Float64bits returns the IEEE 754 binary representation of f. -func Float64bits(f float64) uint64 { return m.Float64bits(f) } +func Float64bits(f float64) uint64 { return math.Float64bits(f) } // Float64frombits returns the floating point number corresponding // the IEEE 754 binary representation b. -func Float64frombits(b uint64) float64 { return m.Float64frombits(b) } +func Float64frombits(b uint64) float64 { return math.Float64frombits(b) } // Floor returns the greatest integer value less than or equal to x. // @@ -243,7 +243,7 @@ func Float64frombits(b uint64) float64 { return m.Float64frombits(b) } // Floor(±0) = ±0 // Floor(±Inf) = ±Inf // Floor(NaN) = NaN -func Floor(x float64) float64 { return float64(m.Floor(float64(x))) } +func Floor(x float64) float64 { return float64(math.Floor(float64(x))) } // Frexp breaks f into a normalized fraction // and an integral power of two. @@ -256,7 +256,7 @@ func Floor(x float64) float64 { return float64(m.Floor(float64(x))) } // Frexp(±Inf) = ±Inf, 0 // Frexp(NaN) = NaN, 0 func Frexp(f float64) (frac float64, exp int) { - fr, exp := m.Frexp(float64(f)) + fr, exp := math.Frexp(float64(f)) return float64(fr), exp } @@ -270,7 +270,7 @@ func Frexp(f float64) (frac float64, exp int) { // Gamma(x) = NaN for integer x < 0 // Gamma(-Inf) = NaN // Gamma(NaN) = NaN -func Gamma(x float64) float64 { return float64(m.Gamma(float64(x))) } +func Gamma(x float64) float64 { return float64(math.Gamma(float64(x))) } // Hypot returns Sqrt(p*p + q*q), taking care to avoid // unnecessary overflow and underflow. @@ -281,7 +281,7 @@ func Gamma(x float64) float64 { return float64(m.Gamma(float64(x))) } // Hypot(p, ±Inf) = +Inf // Hypot(NaN, q) = NaN // Hypot(p, NaN) = NaN -func Hypot(p, q float64) float64 { return float64(m.Hypot(float64(p), float64(q))) } +func Hypot(p, q float64) float64 { return float64(math.Hypot(float64(p), float64(q))) } // J0 returns the order-zero Bessel function of the first kind. // @@ -290,7 +290,7 @@ func Hypot(p, q float64) float64 { return float64(m.Hypot(float64(p), float64(q) // J0(±Inf) = 0 // J0(0) = 1 // J0(NaN) = NaN -func J0(x float64) float64 { return float64(m.J0(float64(x))) } +func J0(x float64) float64 { return float64(math.J0(float64(x))) } // J1 returns the order-one Bessel function of the first kind. // @@ -298,7 +298,7 @@ func J0(x float64) float64 { return float64(m.J0(float64(x))) } // // J1(±Inf) = 0 // J1(NaN) = NaN -func J1(x float64) float64 { return float64(m.J1(float64(x))) } +func J1(x float64) float64 { return float64(math.J1(float64(x))) } // Jn returns the order-n Bessel function of the first kind. // @@ -306,7 +306,7 @@ func J1(x float64) float64 { return float64(m.J1(float64(x))) } // // Jn(n, ±Inf) = 0 // Jn(n, NaN) = NaN -func Jn(n int, x float64) float64 { return float64(m.Jn(n, float64(x))) } +func Jn(n int, x float64) float64 { return float64(math.Jn(n, float64(x))) } // Ldexp is the inverse of Frexp. // It returns frac × 2**exp. @@ -316,7 +316,7 @@ func Jn(n int, x float64) float64 { return float64(m.Jn(n, float64(x))) } // Ldexp(±0, exp) = ±0 // Ldexp(±Inf, exp) = ±Inf // Ldexp(NaN, exp) = NaN -func Ldexp(frac float64, exp int) float64 { return float64(m.Ldexp(float64(frac), exp)) } +func Ldexp(frac float64, exp int) float64 { return float64(math.Ldexp(float64(frac), exp)) } // Lgamma returns the natural logarithm and sign (-1 or +1) of Gamma(x). // @@ -328,7 +328,7 @@ func Ldexp(frac float64, exp int) float64 { return float64(m.Ldexp(float64(frac) // Lgamma(-Inf) = -Inf // Lgamma(NaN) = NaN func Lgamma(x float64) (lgamma float64, sign int) { - l, sign := m.Lgamma(float64(x)) + l, sign := math.Lgamma(float64(x)) return float64(l), sign } @@ -340,11 +340,11 @@ func Lgamma(x float64) (lgamma float64, sign int) { // Log(0) = -Inf // Log(x < 0) = NaN // Log(NaN) = NaN -func Log(x float64) float64 { return float64(m.Log(float64(x))) } +func Log(x float64) float64 { return float64(math.Log(float64(x))) } // Log10 returns the decimal logarithm of x. // The special cases are the same as for Log. -func Log10(x float64) float64 { return float64(m.Log10(float64(x))) } +func Log10(x float64) float64 { return float64(math.Log10(float64(x))) } // Log1p returns the natural logarithm of 1 plus its argument x. // It is more accurate than Log(1 + x) when x is near zero. @@ -356,11 +356,11 @@ func Log10(x float64) float64 { return float64(m.Log10(float64(x))) } // Log1p(-1) = -Inf // Log1p(x < -1) = NaN // Log1p(NaN) = NaN -func Log1p(x float64) float64 { return float64(m.Log1p(float64(x))) } +func Log1p(x float64) float64 { return float64(math.Log1p(float64(x))) } // Log2 returns the binary logarithm of x. // The special cases are the same as for Log. -func Log2(x float64) float64 { return float64(m.Log2(float64(x))) } +func Log2(x float64) float64 { return float64(math.Log2(float64(x))) } // Max returns the larger of x or y. // @@ -370,7 +370,7 @@ func Log2(x float64) float64 { return float64(m.Log2(float64(x))) } // Max(x, NaN) = Max(NaN, x) = NaN // Max(+0, ±0) = Max(±0, +0) = +0 // Max(-0, -0) = -0 -func Max(x, y float64) float64 { return float64(m.Max(float64(x), float64(y))) } +func Max(x, y float64) float64 { return float64(math.Max(float64(x), float64(y))) } // Min returns the smaller of x or y. // @@ -379,7 +379,7 @@ func Max(x, y float64) float64 { return float64(m.Max(float64(x), float64(y))) } // Min(x, -Inf) = Min(-Inf, x) = -Inf // Min(x, NaN) = Min(NaN, x) = NaN // Min(-0, ±0) = Min(±0, -0) = -0 -func Min(x, y float64) float64 { return float64(m.Min(float64(x), float64(y))) } +func Min(x, y float64) float64 { return float64(math.Min(float64(x), float64(y))) } // Mod returns the floating-point remainder of x/y. // The magnitude of the result is less than y and its @@ -392,7 +392,7 @@ func Min(x, y float64) float64 { return float64(m.Min(float64(x), float64(y))) } // Mod(x, 0) = NaN // Mod(x, ±Inf) = x // Mod(x, NaN) = NaN -func Mod(x, y float64) float64 { return float64(m.Mod(float64(x), float64(y))) } +func Mod(x, y float64) float64 { return float64(math.Mod(float64(x), float64(y))) } // Modf returns integer and fractional floating-point numbers // that sum to f. Both values have the same sign as f. @@ -402,7 +402,7 @@ func Mod(x, y float64) float64 { return float64(m.Mod(float64(x), float64(y))) } // Modf(±Inf) = ±Inf, NaN // Modf(NaN) = NaN, NaN func Modf(f float64) (int float64, frac float64) { - i, fr := m.Modf(float64(f)) + i, fr := math.Modf(float64(f)) return float64(i), float64(fr) } @@ -413,7 +413,7 @@ func Modf(f float64) (int float64, frac float64) { // Nextafter(x, x) = x // Nextafter(NaN, y) = NaN // Nextafter(x, NaN) = NaN -func Nextafter(x, y float64) (r float64) { return m.Nextafter(x, y) } +func Nextafter(x, y float64) (r float64) { return math.Nextafter(x, y) } // Pow returns x**y, the base-x exponential of y. // @@ -439,7 +439,7 @@ func Nextafter(x, y float64) (r float64) { return m.Nextafter(x, y) } // Pow(+Inf, y) = +0 for y < 0 // Pow(-Inf, y) = Pow(-0, -y) // Pow(x, y) = NaN for finite x < 0 and finite non-integer y -func Pow(x, y float64) float64 { return float64(m.Pow(float64(x), float64(y))) } +func Pow(x, y float64) float64 { return float64(math.Pow(float64(x), float64(y))) } // Pow10 returns 10**e, the base-10 exponential of e. // @@ -447,7 +447,7 @@ func Pow(x, y float64) float64 { return float64(m.Pow(float64(x), float64(y))) } // // Pow10(e) = +Inf for e > 309 // Pow10(e) = 0 for e < -324 -func Pow10(e int) float64 { return float64(m.Pow10(e)) } +func Pow10(e int) float64 { return float64(math.Pow10(e)) } // Remainder returns the IEEE 754 floating-point remainder of x/y. // @@ -458,10 +458,10 @@ func Pow10(e int) float64 { return float64(m.Pow10(e)) } // Remainder(x, 0) = NaN // Remainder(x, ±Inf) = x // Remainder(x, NaN) = NaN -func Remainder(x, y float64) float64 { return float64(m.Remainder(float64(x), float64(y))) } +func Remainder(x, y float64) float64 { return float64(math.Remainder(float64(x), float64(y))) } // Signbit returns true if x is negative or negative zero. -func Signbit(x float64) bool { return m.Signbit(float64(x)) } +func Signbit(x float64) bool { return math.Signbit(float64(x)) } // Sincos returns Sin(x), Cos(x). // @@ -471,7 +471,7 @@ func Signbit(x float64) bool { return m.Signbit(float64(x)) } // Sincos(±Inf) = NaN, NaN // Sincos(NaN) = NaN, NaN func Sincos(x float64) (sin, cos float64) { - s, c := m.Sincos(float64(x)) + s, c := math.Sincos(float64(x)) return float64(s), float64(c) } @@ -482,7 +482,7 @@ func Sincos(x float64) (sin, cos float64) { // Sinh(±0) = ±0 // Sinh(±Inf) = ±Inf // Sinh(NaN) = NaN -func Sinh(x float64) float64 { return float64(m.Sinh(float64(x))) } +func Sinh(x float64) float64 { return float64(math.Sinh(float64(x))) } // Tan returns the tangent of the radian argument x. // @@ -491,7 +491,7 @@ func Sinh(x float64) float64 { return float64(m.Sinh(float64(x))) } // Tan(±0) = ±0 // Tan(±Inf) = NaN // Tan(NaN) = NaN -func Tan(x float64) float64 { return float64(m.Tan(float64(x))) } +func Tan(x float64) float64 { return float64(math.Tan(float64(x))) } // Tanh returns the hyperbolic tangent of x. // @@ -500,7 +500,7 @@ func Tan(x float64) float64 { return float64(m.Tan(float64(x))) } // Tanh(±0) = ±0 // Tanh(±Inf) = ±1 // Tanh(NaN) = NaN -func Tanh(x float64) float64 { return float64(m.Tanh(float64(x))) } +func Tanh(x float64) float64 { return float64(math.Tanh(float64(x))) } // Trunc returns the integer value of x. // @@ -509,7 +509,7 @@ func Tanh(x float64) float64 { return float64(m.Tanh(float64(x))) } // Trunc(±0) = ±0 // Trunc(±Inf) = ±Inf // Trunc(NaN) = NaN -func Trunc(x float64) float64 { return float64(m.Trunc(float64(x))) } +func Trunc(x float64) float64 { return float64(math.Trunc(float64(x))) } // Y0 returns the order-zero Bessel function of the second kind. // @@ -519,7 +519,7 @@ func Trunc(x float64) float64 { return float64(m.Trunc(float64(x))) } // Y0(0) = -Inf // Y0(x < 0) = NaN // Y0(NaN) = NaN -func Y0(x float64) float64 { return float64(m.Y0(float64(x))) } +func Y0(x float64) float64 { return float64(math.Y0(float64(x))) } // Y1 returns the order-one Bessel function of the second kind. // @@ -529,7 +529,7 @@ func Y0(x float64) float64 { return float64(m.Y0(float64(x))) } // Y1(0) = -Inf // Y1(x < 0) = NaN // Y1(NaN) = NaN -func Y1(x float64) float64 { return float64(m.Y1(float64(x))) } +func Y1(x float64) float64 { return float64(math.Y1(float64(x))) } // Yn returns the order-n Bessel function of the second kind. // @@ -540,4 +540,4 @@ func Y1(x float64) float64 { return float64(m.Y1(float64(x))) } // Yn(n < 0, 0) = +Inf if n is odd, -Inf if n is even // Y1(n, x < 0) = NaN // Y1(n, NaN) = NaN -func Yn(n int, x float64) float64 { return float64(m.Yn(n, float64(x))) } +func Yn(n int, x float64) float64 { return float64(math.Yn(n, float64(x))) } -- Gitee From eb5ce10b56f83d741c3f88058684ab076a0f2b0c Mon Sep 17 00:00:00 2001 From: wangfeng Date: Thu, 2 Feb 2023 08:07:10 +0800 Subject: [PATCH 02/28] =?UTF-8?q?=E8=B0=83=E6=95=B4imports?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- series_float64.go | 1 - 1 file changed, 1 deletion(-) diff --git a/series_float64.go b/series_float64.go index 4f2a4fa..400f24c 100644 --- a/series_float64.go +++ b/series_float64.go @@ -3,7 +3,6 @@ package pandas import ( "gitee.com/quant1x/pandas/algorithms" "gitee.com/quant1x/pandas/algorithms/avx2" - //"github.com/huandu/go-clone" "gonum.org/v1/gonum/stat" "reflect" ) -- Gitee From 666493c9a70d644308f129ce5f1d1264eaa5066e Mon Sep 17 00:00:00 2001 From: wangfeng Date: Thu, 2 Feb 2023 08:07:54 +0800 Subject: [PATCH 03/28] =?UTF-8?q?=E8=B0=83=E6=95=B4=E6=96=87=E4=BB=B6?= =?UTF-8?q?=E5=90=8D=E4=B8=8E=E5=BC=95=E7=94=A8package=E5=90=8C=E5=90=8D?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- algorithms/avx2/{vek_float64.go => vek.go} | 8 +++++--- algorithms/avx2/{vek_float64_test.go => vek_test.go} | 0 2 files changed, 5 insertions(+), 3 deletions(-) rename algorithms/avx2/{vek_float64.go => vek.go} (88%) rename algorithms/avx2/{vek_float64_test.go => vek_test.go} (100%) diff --git a/algorithms/avx2/vek_float64.go b/algorithms/avx2/vek.go similarity index 88% rename from algorithms/avx2/vek_float64.go rename to algorithms/avx2/vek.go index 53fbdde..4582375 100644 --- a/algorithms/avx2/vek_float64.go +++ b/algorithms/avx2/vek.go @@ -3,6 +3,7 @@ package avx2 import "github.com/viterin/vek" // 初始化 avx2 +// 可以参考另一个实现库 gonum.org/v1/gonum/stat func init() { // 开启加速选项 vek.SetAcceleration(true) @@ -27,9 +28,10 @@ func Round(x []float64) { vek.Round_Inplace(x) } func Ceil(x []float64) { vek.Ceil_Inplace(x) } func Floor(x []float64) { vek.Floor_Inplace(x) } -func Min(x []float64) float64 { return vek.Min(x) } -func Max(x []float64) float64 { return vek.Max(x) } -func Mean(x []float64) float64 { return vek.Mean(x) } +func Min(x []float64) float64 { return vek.Min(x) } +func Max(x []float64) float64 { return vek.Max(x) } +func Mean(x []float64) float64 { return vek.Mean(x) } +func Median(x []float64) float64 { return vek.Median(x) } func Dot(x []float64, y []float64) float64 { return vek.Dot(x, y) } diff --git a/algorithms/avx2/vek_float64_test.go b/algorithms/avx2/vek_test.go similarity index 100% rename from algorithms/avx2/vek_float64_test.go rename to algorithms/avx2/vek_test.go -- Gitee From 3b82a7a99b282cf81382a6523faac774a91a6cea Mon Sep 17 00:00:00 2001 From: wangfeng Date: Thu, 2 Feb 2023 12:43:02 +0800 Subject: [PATCH 04/28] =?UTF-8?q?=E5=A2=9E=E5=8A=A0=E6=8C=87=E9=92=88?= =?UTF-8?q?=E7=9A=84=E5=88=A4=E6=96=AD=E5=87=BD=E6=95=B0?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- builtin.go | 10 ++++++++++ builtin_test.go | 17 ++++++++++++++++- 2 files changed, 26 insertions(+), 1 deletion(-) diff --git a/builtin.go b/builtin.go index a996824..6373134 100644 --- a/builtin.go +++ b/builtin.go @@ -3,6 +3,7 @@ package pandas import ( gc "github.com/huandu/go-clone" "math" + "reflect" "strings" ) @@ -13,10 +14,14 @@ import ( var ( // Nil2Float nil指针转换float64 Nil2Float = float64(0) + // Nil2Float32 nil指针转换float32 + Nil2Float32 = float32(0) ) func init() { Nil2Float = math.NaN() + // 这个转换是对的, NaN对float32也有效 + Nil2Float32 = float32(Nil2Float) } // NaN returns an IEEE 754 “not-a-number” value. @@ -42,3 +47,8 @@ func IsEmpty(s string) bool { func clone(v any) any { return gc.Clone(v) } + +func isPoint(v any) bool { + kind := reflect.ValueOf(v).Kind() + return reflect.Pointer == kind +} diff --git a/builtin_test.go b/builtin_test.go index 2ed9525..543cb06 100644 --- a/builtin_test.go +++ b/builtin_test.go @@ -1,6 +1,9 @@ package pandas -import "testing" +import ( + "fmt" + "testing" +) func TestIsEmpty(t *testing.T) { type args struct { @@ -69,3 +72,15 @@ func TestIsEmpty(t *testing.T) { }) } } + +func TestPoint(t *testing.T) { + var p1 *int + fmt.Printf("*int = nil, result=%v\n", isPoint(p1)) + a := 1 + p1 = &a + fmt.Printf("*int = nil, result=%v\n", isPoint(p1)) + + var p2 *BigFloat + fmt.Printf("*BigFloat = nil, result=%v\n", isPoint(p2)) + +} -- Gitee From 66ebc487094c500b9f3470e518075c5442688551 Mon Sep 17 00:00:00 2001 From: wangfeng Date: Thu, 2 Feb 2023 12:44:52 +0800 Subject: [PATCH 05/28] =?UTF-8?q?=E5=A2=9E=E5=8A=A0float32=E7=B1=BB?= =?UTF-8?q?=E5=9E=8B=E7=9A=84=E8=BD=AC=E6=8D=A2?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- type_float32.go | 183 +++++++++++++++++++++ type_float32_test.go | 381 +++++++++++++++++++++++++++++++++++++++++++ 2 files changed, 564 insertions(+) create mode 100644 type_float32.go create mode 100644 type_float32_test.go diff --git a/type_float32.go b/type_float32.go new file mode 100644 index 0000000..6ce3bb9 --- /dev/null +++ b/type_float32.go @@ -0,0 +1,183 @@ +package pandas + +import ( + "fmt" + "github.com/mymmsc/gox/logger" + "math" + "strconv" +) + +const ( + MaxFloat32 = math.MaxFloat32 // float32最大值 + StringTrue2Float32 float32 = float32(1) // 字符串true转float32 + StringFalse2Float32 float32 = float32(0) // 字符串false转float32 +) + +// ParseFloat32 字符串转float32 +func ParseFloat32(s string, v any) float32 { + defer func() { + if err := recover(); err != nil { + logger.Errorf("ParseFloat32 %+v, error=%+v\n", v, err) + } + }() + f, err := strconv.ParseFloat(s, 32) + if err == nil { + return float32(f) + } + if IgnoreParseExceptions { + return Nil2Float32 + } + _ = v.(float32) // Intentionally panic + return float32(0) +} + +func float32ToString(v float32) string { + if isNaN(float64(v)) { + return StringNaN + } + return fmt.Sprintf("%f", v) +} + +func value_to_float32(v any) float32 { + switch val := v.(type) { + case float32: // 直接转换的放在第一位判断 + return val + case int8: + return float32(val) + case uint8: + return float32(val) + case int16: + return float32(val) + case uint16: + return float32(val) + case int32: + return float32(val) + case uint32: + return float32(val) + case int64: + return float32(val) + case uint64: + return float32(val) + case int: + return float32(val) + case uint: + return float32(val) + case float64: + return float32(val) + case bool: + if val == true { + return True2Float32 + } + return False2Float32 + case string: + if IsEmpty(val) { + // TODO:NaN是针对64位, 这样直接转换应该有问题, 需要进一步确认 + return Nil2Float32 + } + if isTrue(val) { + return StringTrue2Float32 + } else if isFalse(val) { + return StringFalse2Float32 + } + f := ParseFloat32(val, v) + return f + } + return float32(0) +} + +// 指针转float32 +func point_to_float32(v any) float32 { + switch val := v.(type) { + case *float32: + if val == nil { + return Nil2Float32 + } + return float32(*val) + case *int8: + if val == nil { + return Nil2Float32 + } + return float32(*val) + case *uint8: + if val == nil { + return Nil2Float32 + } + return float32(*val) + case *int16: + if val == nil { + return Nil2Float32 + } + return float32(*val) + case *uint16: + if val == nil { + return Nil2Float32 + } + return float32(*val) + case *int32: + if val == nil { + return Nil2Float32 + } + return float32(*val) + case *uint32: + if val == nil { + return Nil2Float32 + } + return float32(*val) + case *int64: + if val == nil { + return Nil2Float32 + } + return float32(*val) + case *uint64: + if val == nil { + return Nil2Float32 + } + return float32(*val) + case *int: + if val == nil { + return Nil2Float32 + } + return float32(*val) + case *uint: + if val == nil { + return Nil2Float32 + } + return float32(*val) + case *float64: + if val == nil { + return Nil2Float32 + } + return float32(*val) + case *bool: + if val == nil { + return Nil2Float32 + } + if *val == true { + return True2Float32 + } + return False2Float32 + case *string: + if val == nil { + return Nil2Float32 + } + if IsEmpty(*val) { + // TODO: NaN是针对64位, 这样直接转换应该有问题, 需要进一步确认 + return Nil2Float32 + } + if isTrue(*val) { + return StringTrue2Float32 + } else if isFalse(*val) { + return StringFalse2Float32 + } + f := ParseFloat32(*val, v) + return f + } + return float32(0) +} + +func AnyToFloat32(v any) float32 { + if isPoint(v) { + return point_to_float32(v) + } + return value_to_float32(v) +} diff --git a/type_float32_test.go b/type_float32_test.go new file mode 100644 index 0000000..7dea016 --- /dev/null +++ b/type_float32_test.go @@ -0,0 +1,381 @@ +package pandas + +import ( + "fmt" + "testing" +) + +func Test_point_to_1float32(t *testing.T) { + var p1 *int8 + f1 := point_to_float32(p1) + fmt.Printf("*int8 to float32=%f\n", f1) + + var v1 int8 = 1 + p1 = &v1 + f1 = point_to_float32(p1) + fmt.Printf("*int8 to float32=%f\n", f1) +} + +func Test_point_to_float32(t *testing.T) { + type args struct { + v any + } + + // 指针声明 + var pInt8 *int8 + var pUint8 *uint8 + var pInt16 *int16 + var pUint16 *uint16 + var pInt32 *int32 + var pUint32 *uint32 + var pInt64 *int64 + var pUint64 *uint64 + var pInt *int + var pUint *uint + var pFloat32 *float32 + var pFloat64 *float64 + var pBoolTrue *bool + var pBoolFalse *bool + var pStr1 *string + var pStr2 *string + + vInt8 := int8(1) + pInt8 = &vInt8 + vUint8 := uint8(1) + pUint8 = &vUint8 + + vInt16 := int16(1) + pInt16 = &vInt16 + vUint16 := uint16(1) + pUint16 = &vUint16 + + vInt32 := int32(1) + pInt32 = &vInt32 + vUint32 := uint32(1) + pUint32 = &vUint32 + + vInt64 := int64(1) + pInt64 = &vInt64 + vUint64 := uint64(1) + pUint64 = &vUint64 + + vInt := int(1) + pInt = &vInt + vUint := uint(1) + pUint = &vUint + + vFloat32 := float32(1) + pFloat32 = &vFloat32 + vFloat64 := float64(1) + pFloat64 = &vFloat64 + + vBoolTrue := true + pBoolTrue = &vBoolTrue + vBoolFalse := false + pBoolFalse = &vBoolFalse + + vStr1 := "abc" + pStr1 = &vStr1 + vStr2 := "1.23" + pStr2 = &vStr2 + + tests := []struct { + name string + args args + want float32 + }{ + { + name: "T01: int8", + args: args{ + pInt8, + }, + want: float32(1), + }, + { + name: "T02: uint8", + args: args{ + pUint8, + }, + want: float32(1), + }, + { + name: "T03: int16", + args: args{ + pInt16, + }, + want: float32(1), + }, + { + name: "T04: uint16", + args: args{ + pUint16, + }, + want: float32(1), + }, + { + name: "T05: int32", + args: args{ + pInt32, + }, + want: float32(1), + }, + { + name: "T06: uint32", + args: args{ + pUint32, + }, + want: float32(1), + }, + { + name: "T07: int64", + args: args{ + pInt64, + }, + want: float32(1), + }, + { + name: "T08: uint64", + args: args{ + pUint64, + }, + want: float32(1), + }, + { + name: "T09: int", + args: args{ + pInt, + }, + want: float32(1), + }, + { + name: "T10: uint", + args: args{ + pUint, + }, + want: float32(1), + }, + { + name: "T11: float32", + args: args{ + pFloat32, + }, + want: float32(1), + }, + { + name: "T12: float64", + args: args{ + pFloat64, + }, + want: float32(1), + }, + { + name: "T13: true", + args: args{ + pBoolTrue, + }, + want: float32(1), + }, + { + name: "T14: false", + args: args{ + pBoolFalse, + }, + want: float32(0), + }, + { + name: "T15: str1", + args: args{ + pStr1, + }, + want: Nil2Float32, + }, + { + name: "T16: str2", + args: args{ + pStr2, + }, + want: float32(1.23), + }, + } + for _, tt := range tests { + t.Run(tt.name, func(t *testing.T) { + //got := point_to_float32(tt.args.v) + //if got != tt.want { + // if !IsNaN(float64(tt.want)) { + // t.Errorf("point_to_float32() = %v, want %v", got, tt.want) + // } else if !IsNaN(float64(got)) { + // t.Errorf("point_to_float32() = %v, want %v", got, tt.want) + // } + //} + if got := point_to_float32(tt.args.v); got != tt.want && !(IsNaN(float64(tt.want)) && IsNaN(float64(got))) { + t.Errorf("point_to_float32() = %v, want %v", got, tt.want) + } + }) + } +} + +func Test_value_to_float32(t *testing.T) { + type args struct { + v any + } + + vInt8 := int8(1) + vUint8 := uint8(1) + + vInt16 := int16(1) + vUint16 := uint16(1) + + vInt32 := int32(1) + vUint32 := uint32(1) + + vInt64 := int64(1) + vUint64 := uint64(1) + + vInt := int(1) + vUint := uint(1) + + vFloat32 := float32(1) + vFloat64 := float64(1) + + //vBoolTrue := true + //vBoolFalse := false + + vStr1 := "abc" + vStr2 := "1.23" + + // 组装测试用例 + tests := []struct { + name string + args args + want float32 + }{ + { + name: "T01: int8", + args: args{ + vInt8, + }, + want: float32(1), + }, + { + name: "T02: uint8", + args: args{ + vUint8, + }, + want: float32(1), + }, + { + name: "T03: int16", + args: args{ + vInt16, + }, + want: float32(1), + }, + { + name: "T04: uint16", + args: args{ + vUint16, + }, + want: float32(1), + }, + { + name: "T05: int32", + args: args{ + vInt32, + }, + want: float32(1), + }, + { + name: "T06: uint32", + args: args{ + vUint32, + }, + want: float32(1), + }, + { + name: "T07: int64", + args: args{ + vInt64, + }, + want: float32(1), + }, + { + name: "T08: uint64", + args: args{ + vUint64, + }, + want: float32(1), + }, + { + name: "T09: int", + args: args{ + vInt, + }, + want: float32(1), + }, + { + name: "T10: uint", + args: args{ + vUint, + }, + want: float32(1), + }, + { + name: "T11: float32", + args: args{ + vFloat32, + }, + want: float32(1), + }, + { + name: "T12: float64", + args: args{ + vFloat64, + }, + want: float32(1), + }, + { + name: "T13: true", + args: args{ + true, + }, + want: float32(1), + }, + { + name: "T14: false", + args: args{ + false, + }, + want: float32(0), + }, + { + name: "T15: str1", + args: args{ + vStr1, + }, + want: Nil2Float32, + }, + { + name: "T16: str2", + args: args{ + vStr2, + }, + want: float32(1.23), + }, + } + + for _, tt := range tests { + t.Run(tt.name, func(t *testing.T) { + //got := value_to_float32(tt.args.v) + //if got != tt.want { + // if !IsNaN(float64(tt.want)) { + // t.Errorf("value_to_float32() = %v, want %v", got, tt.want) + // } else if !IsNaN(float64(got)) { + // t.Errorf("value_to_float32() = %v, want %v", got, tt.want) + // } + //} + if got := value_to_float32(tt.args.v); !(got == tt.want || (IsNaN(float64(tt.want)) && IsNaN(float64(got)))) { + t.Errorf("value_to_float32() = %v, want %v", got, tt.want) + } + }) + } +} -- Gitee From 17c806217c0443a9cc84e6314719656ce669c558 Mon Sep 17 00:00:00 2001 From: wangfeng Date: Thu, 2 Feb 2023 12:45:36 +0800 Subject: [PATCH 06/28] =?UTF-8?q?=E5=A2=9E=E5=8A=A0float32=E5=88=87?= =?UTF-8?q?=E7=89=87=E7=9A=84=E5=A4=84=E7=90=86=E6=96=B9=E6=B3=95?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- slice_float32.go | 65 ++++++++++++++++++++++++++++++++++++++++++++++++ 1 file changed, 65 insertions(+) create mode 100644 slice_float32.go diff --git a/slice_float32.go b/slice_float32.go new file mode 100644 index 0000000..e27acaf --- /dev/null +++ b/slice_float32.go @@ -0,0 +1,65 @@ +package pandas + +func slice_any_to_float32[T Number](s []T) []float32 { + count := len(s) + if count == 0 { + return []float32{} + } + d := make([]float32, count) + for idx, iv := range s { + // 强制转换 + d[idx] = float32(iv) + } + return d +} + +// any输入只能是一维slice或者数组 +func sliceToFloat32(v any) []float32 { + var vs []float32 + switch values := v.(type) { + case []int8: + return slice_any_to_float32(values) + case []uint8: + return slice_any_to_float32(values) + case []int16: + return slice_any_to_float32(values) + case []uint16: + return slice_any_to_float32(values) + case []int32: + return slice_any_to_float32(values) + case []uint32: + return slice_any_to_float32(values) + case []int64: + return slice_any_to_float32(values) + case []uint64: + return slice_any_to_float32(values) + case []int: + return slice_any_to_float32(values) + case []uint: + return slice_any_to_float32(values) + case []float32: + // TODO:直接返回会不会有问题 + return values + case []float64: + return slice_any_to_float32(values) + case []bool: + count := len(values) + if count == 0 { + return []float32{} + } + vs = make([]float32, count) + for idx, iv := range values { + vs[idx] = boolToFloat32(iv) + } + case []string: + count := len(values) + if count == 0 { + return []float32{} + } + vs = make([]float32, count) + for idx, iv := range values { + vs[idx] = float32(AnyToFloat64(iv)) + } + } + return []float32{} +} -- Gitee From 07e4d82b00982d7baa01c89a9777dd4dffd2d1c9 Mon Sep 17 00:00:00 2001 From: wangfeng Date: Thu, 2 Feb 2023 12:46:09 +0800 Subject: [PATCH 07/28] =?UTF-8?q?=E5=A2=9E=E5=8A=A0float64=E7=9A=84?= =?UTF-8?q?=E5=88=87=E7=89=87=E5=A4=84=E7=90=86=E6=96=B9=E6=B3=95?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- algorithms/aggregates.go | 77 ------ algorithms/float64.go | 543 --------------------------------------- algorithms/max.go | 39 --- algorithms/min.go | 39 --- slice_float64.go | 47 ++++ 5 files changed, 47 insertions(+), 698 deletions(-) delete mode 100644 algorithms/aggregates.go delete mode 100644 algorithms/float64.go delete mode 100644 algorithms/max.go delete mode 100644 algorithms/min.go create mode 100644 slice_float64.go diff --git a/algorithms/aggregates.go b/algorithms/aggregates.go deleted file mode 100644 index cad707a..0000000 --- a/algorithms/aggregates.go +++ /dev/null @@ -1,77 +0,0 @@ -package algorithms - -type Number interface { - ~int64 | float64 -} - -func Sum_Go[T Number](x []T) T { - sum := T(0) - for i := 0; i < len(x); i++ { - sum += x[i] - } - return sum -} - -func CumSum_Go[T Number](x []T) { - sum := T(0) - for i := 0; i < len(x); i++ { - sum += x[i] - x[i] = sum - } -} - -func Prod_Go[T Number](x []T) T { - prod := T(1) - for i := 0; i < len(x); i++ { - prod *= x[i] - } - return prod -} - -func CumProd_Go[T Number](x []T) { - prod := T(1) - for i := 0; i < len(x); i++ { - prod *= x[i] - x[i] = prod - } -} - -func Mean_Go[T Number](x []T) T { - return Sum_Go(x) / T(len(x)) -} - -//func Median_Go[T Number](x []T) T { -// if len(x)%2 == 1 { -// x = slices.Clone(x) -// i := len(x) / 2 -// partial.TopK(x, i+1) -// return x[i] -// } -// return Quantile_Go(x, T(0.5)) -//} -// -//func Quantile_Go[T Number](x []T, q T) T { -// if len(x) == 1 { -// return x[0] -// } -// if q == T(0) { -// return Min_Go(x) -// } -// if q == T(1) { -// return Max_Go(x) -// } -// x = slices.Clone(x) -// f := T(len(x)-1) * q -// i := int(math.Floor(float64(f))) -// if q < 0.5 { -// partial.TopK(x, i+2) -// a := Max_Go(x[:i+1]) -// b := x[i+1] -// return a + (b-a)*(f-T(i)) -// } else { -// partial.TopK(x, i+1) -// a := x[i] -// b := Min_Go(x[i+1:]) -// return a + (b-a)*(f-T(i)) -// } -//} diff --git a/algorithms/float64.go b/algorithms/float64.go deleted file mode 100644 index 40b4727..0000000 --- a/algorithms/float64.go +++ /dev/null @@ -1,543 +0,0 @@ -package algorithms - -import ( - "math" -) - -// Round returns the nearest integer, rounding half away from zero. -// -// Special cases are: -// -// Round(±0) = ±0 -// Round(±Inf) = ±Inf -// Round(NaN) = NaN -func Round(x float64) float64 { return math.Round(x) } - -// RoundToEven returns the nearest integer, rounding ties to even. -// -// Special cases are: -// -// RoundToEven(±0) = ±0 -// RoundToEven(±Inf) = ±Inf -// RoundToEven(NaN) = NaN -func RoundToEven(x float64) float64 { return math.RoundToEven(x) } - -// NaN returns an IEEE 754 “not-a-number” value. -func NaN() float64 { return math.NaN() } - -// IsNaN reports whether f is an IEEE 754 “not-a-number” value. -func IsNaN(x float64) bool { return math.IsNaN(x) } - -// IsInf reports whether f is an infinity, according to sign. -// If sign > 0, IsInf reports whether f is positive infinity. -// If sign < 0, IsInf reports whether f is negative infinity. -// If sign == 0, IsInf reports whether f is either infinity. -func IsInf(f float64, sign int) bool { return math.IsInf(f, sign) } - -// Sqrt returns the square root of x. -// -// Special cases are: -// -// Sqrt(+Inf) = +Inf -// Sqrt(±0) = ±0 -// Sqrt(x < 0) = NaN -// Sqrt(NaN) = NaN -func Sqrt(x float64) float64 { return math.Sqrt(x) } - -// Sin returns the sine of the radian argument x. -// -// Special cases are: -// -// Sin(±0) = ±0 -// Sin(±Inf) = NaN -// Sin(NaN) = NaN -func Sin(x float64) float64 { return float64(math.Sin(float64(x))) } - -// Cos returns the cosine of the radian argument x. -// -// Special cases are: -// -// Cos(±Inf) = NaN -// Cos(NaN) = NaN -func Cos(x float64) float64 { return float64(math.Cos(float64(x))) } - -// Abs returns the absolute value of x. -// -// Special cases are: -// -// Abs(±Inf) = +Inf -// Abs(NaN) = NaN -func Abs(x float64) float64 { return float64(math.Abs(float64(x))) } - -// Acos returns the arccosine, in radians, of x. -// -// Special case is: -// -// Acos(x) = NaN if x < -1 or x > 1 -func Acos(x float64) float64 { return float64(math.Acos(float64(x))) } - -// Asin returns the arcsine, in radians, of x. -// -// Special cases are: -// -// Asin(±0) = ±0 -// Asin(x) = NaN if x < -1 or x > 1 -func Asin(x float64) float64 { return float64(math.Asin(float64(x))) } - -// Asinh returns the inverse hyperbolic sine of x. -// -// Special cases are: -// -// Asinh(±0) = ±0 -// Asinh(±Inf) = ±Inf -// Asinh(NaN) = NaN -func Asinh(x float64) float64 { return float64(math.Asinh(float64(x))) } - -// Atan returns the arctangent, in radians, of x. -// -// Special cases are: -// -// Atan(±0) = ±0 -// Atan(±Inf) = ±Pi/2 -func Atan(x float64) float64 { return float64(math.Atan(float64(x))) } - -// Atan2 returns the arc tangent of y/x, using -// the signs of the two to determine the quadrant -// of the return value. -// -// Special cases are (in order): -// -// Atan2(y, NaN) = NaN -// Atan2(NaN, x) = NaN -// Atan2(+0, x>=0) = +0 -// Atan2(-0, x>=0) = -0 -// Atan2(+0, x<=-0) = +Pi -// Atan2(-0, x<=-0) = -Pi -// Atan2(y>0, 0) = +Pi/2 -// Atan2(y<0, 0) = -Pi/2 -// Atan2(+Inf, +Inf) = +Pi/4 -// Atan2(-Inf, +Inf) = -Pi/4 -// Atan2(+Inf, -Inf) = 3Pi/4 -// Atan2(-Inf, -Inf) = -3Pi/4 -// Atan2(y, +Inf) = 0 -// Atan2(y>0, -Inf) = +Pi -// Atan2(y<0, -Inf) = -Pi -// Atan2(+Inf, x) = +Pi/2 -// Atan2(-Inf, x) = -Pi/2 -func Atan2(y, x float64) float64 { return float64(math.Atan2(float64(y), float64(x))) } - -// Atanh returns the inverse hyperbolic tangent of x. -// -// Special cases are: -// -// Atanh(1) = +Inf -// Atanh(±0) = ±0 -// Atanh(-1) = -Inf -// Atanh(x) = NaN if x < -1 or x > 1 -// Atanh(NaN) = NaN -func Atanh(x float64) float64 { return float64(math.Atanh(float64(x))) } - -// Cbrt returns the cube root of x. -// -// Special cases are: -// -// Cbrt(±0) = ±0 -// Cbrt(±Inf) = ±Inf -// Cbrt(NaN) = NaN -func Cbrt(x float64) float64 { return float64(math.Cbrt(float64(x))) } - -// Ceil returns the least integer value greater than or equal to x. -// -// Special cases are: -// -// Ceil(±0) = ±0 -// Ceil(±Inf) = ±Inf -// Ceil(NaN) = NaN -func Ceil(x float64) float64 { return float64(math.Ceil(float64(x))) } - -// Copysign returns a value with the magnitude -// of x and the sign of y. -func Copysign(x, y float64) float64 { return float64(math.Copysign(float64(x), float64(y))) } - -// Cosh returns the hyperbolic cosine of x. -// -// Special cases are: -// Cosh(±0) = 1 -// Cosh(±Inf) = +Inf -// Cosh(NaN) = NaN -//func Cosh(x float64) float64 { return float64(math.Cosh(float64(x))) } - -// Dim returns the maximum of x-y or 0. -// -// Special cases are: -// -// Dim(+Inf, +Inf) = NaN -// Dim(-Inf, -Inf) = NaN -// Dim(x, NaN) = Dim(NaN, x) = NaN -func Dim(x, y float64) float64 { return float64(math.Dim(float64(x), float64(y))) } - -// Erf returns the error function of x. -// -// Special cases are: -// -// Erf(+Inf) = 1 -// Erf(-Inf) = -1 -// Erf(NaN) = NaN -func Erf(x float64) float64 { return float64(math.Erf(float64(x))) } - -// Erfc returns the complementary error function of x. -// -// Special cases are: -// -// Erfc(+Inf) = 0 -// Erfc(-Inf) = 2 -// Erfc(NaN) = NaN -func Erfc(x float64) float64 { return float64(math.Erfc(float64(x))) } - -// Exp returns e**x, the base-e exponential of x. -// -// Special cases are: -// -// Exp(+Inf) = +Inf -// Exp(NaN) = NaN -// -// Very large values overflow to 0 or +Inf. -// Very small values underflow to 1. -func Exp(x float64) float64 { return float64(math.Exp(float64(x))) } - -// Exp2 returns 2**x, the base-2 exponential of x. -// -// Special cases are the same as Exp. -func Exp2(x float64) float64 { return float64(math.Exp2(float64(x))) } - -// Expm1 returns e**x - 1, the base-e exponential of x minus 1. -// It is more accurate than Exp(x) - 1 when x is near zero. -// -// Special cases are: -// -// Expm1(+Inf) = +Inf -// Expm1(-Inf) = -1 -// Expm1(NaN) = NaN -// -// Very large values overflow to -1 or +Inf. -func Expm1(x float64) float64 { return float64(math.Expm1(float64(x))) } - -// Float32bits returns the IEEE 754 binary representation of f. -func Float32bits(f float32) uint32 { return math.Float32bits(f) } - -// Float32frombits returns the floating point number corresponding -// to the IEEE 754 binary representation b. -func Float32frombits(b uint32) float32 { return math.Float32frombits(b) } - -// Float64bits returns the IEEE 754 binary representation of f. -func Float64bits(f float64) uint64 { return math.Float64bits(f) } - -// Float64frombits returns the floating point number corresponding -// the IEEE 754 binary representation b. -func Float64frombits(b uint64) float64 { return math.Float64frombits(b) } - -// Floor returns the greatest integer value less than or equal to x. -// -// Special cases are: -// -// Floor(±0) = ±0 -// Floor(±Inf) = ±Inf -// Floor(NaN) = NaN -func Floor(x float64) float64 { return float64(math.Floor(float64(x))) } - -// Frexp breaks f into a normalized fraction -// and an integral power of two. -// It returns frac and exp satisfying f == frac × 2**exp, -// with the absolute value of frac in the interval [½, 1). -// -// Special cases are: -// -// Frexp(±0) = ±0, 0 -// Frexp(±Inf) = ±Inf, 0 -// Frexp(NaN) = NaN, 0 -func Frexp(f float64) (frac float64, exp int) { - fr, exp := math.Frexp(float64(f)) - return float64(fr), exp -} - -// Gamma returns the Gamma function of x. -// -// Special cases are: -// -// Gamma(+Inf) = +Inf -// Gamma(+0) = +Inf -// Gamma(-0) = -Inf -// Gamma(x) = NaN for integer x < 0 -// Gamma(-Inf) = NaN -// Gamma(NaN) = NaN -func Gamma(x float64) float64 { return float64(math.Gamma(float64(x))) } - -// Hypot returns Sqrt(p*p + q*q), taking care to avoid -// unnecessary overflow and underflow. -// -// Special cases are: -// -// Hypot(±Inf, q) = +Inf -// Hypot(p, ±Inf) = +Inf -// Hypot(NaN, q) = NaN -// Hypot(p, NaN) = NaN -func Hypot(p, q float64) float64 { return float64(math.Hypot(float64(p), float64(q))) } - -// J0 returns the order-zero Bessel function of the first kind. -// -// Special cases are: -// -// J0(±Inf) = 0 -// J0(0) = 1 -// J0(NaN) = NaN -func J0(x float64) float64 { return float64(math.J0(float64(x))) } - -// J1 returns the order-one Bessel function of the first kind. -// -// Special cases are: -// -// J1(±Inf) = 0 -// J1(NaN) = NaN -func J1(x float64) float64 { return float64(math.J1(float64(x))) } - -// Jn returns the order-n Bessel function of the first kind. -// -// Special cases are: -// -// Jn(n, ±Inf) = 0 -// Jn(n, NaN) = NaN -func Jn(n int, x float64) float64 { return float64(math.Jn(n, float64(x))) } - -// Ldexp is the inverse of Frexp. -// It returns frac × 2**exp. -// -// Special cases are: -// -// Ldexp(±0, exp) = ±0 -// Ldexp(±Inf, exp) = ±Inf -// Ldexp(NaN, exp) = NaN -func Ldexp(frac float64, exp int) float64 { return float64(math.Ldexp(float64(frac), exp)) } - -// Lgamma returns the natural logarithm and sign (-1 or +1) of Gamma(x). -// -// Special cases are: -// -// Lgamma(+Inf) = +Inf -// Lgamma(0) = +Inf -// Lgamma(-integer) = +Inf -// Lgamma(-Inf) = -Inf -// Lgamma(NaN) = NaN -func Lgamma(x float64) (lgamma float64, sign int) { - l, sign := math.Lgamma(float64(x)) - return float64(l), sign -} - -// Log returns the natural logarithm of x. -// -// Special cases are: -// -// Log(+Inf) = +Inf -// Log(0) = -Inf -// Log(x < 0) = NaN -// Log(NaN) = NaN -func Log(x float64) float64 { return float64(math.Log(float64(x))) } - -// Log10 returns the decimal logarithm of x. -// The special cases are the same as for Log. -func Log10(x float64) float64 { return float64(math.Log10(float64(x))) } - -// Log1p returns the natural logarithm of 1 plus its argument x. -// It is more accurate than Log(1 + x) when x is near zero. -// -// Special cases are: -// -// Log1p(+Inf) = +Inf -// Log1p(±0) = ±0 -// Log1p(-1) = -Inf -// Log1p(x < -1) = NaN -// Log1p(NaN) = NaN -func Log1p(x float64) float64 { return float64(math.Log1p(float64(x))) } - -// Log2 returns the binary logarithm of x. -// The special cases are the same as for Log. -func Log2(x float64) float64 { return float64(math.Log2(float64(x))) } - -// Max returns the larger of x or y. -// -// Special cases are: -// -// Max(x, +Inf) = Max(+Inf, x) = +Inf -// Max(x, NaN) = Max(NaN, x) = NaN -// Max(+0, ±0) = Max(±0, +0) = +0 -// Max(-0, -0) = -0 -func Max(x, y float64) float64 { return float64(math.Max(float64(x), float64(y))) } - -// Min returns the smaller of x or y. -// -// Special cases are: -// -// Min(x, -Inf) = Min(-Inf, x) = -Inf -// Min(x, NaN) = Min(NaN, x) = NaN -// Min(-0, ±0) = Min(±0, -0) = -0 -func Min(x, y float64) float64 { return float64(math.Min(float64(x), float64(y))) } - -// Mod returns the floating-point remainder of x/y. -// The magnitude of the result is less than y and its -// sign agrees with that of x. -// -// Special cases are: -// -// Mod(±Inf, y) = NaN -// Mod(NaN, y) = NaN -// Mod(x, 0) = NaN -// Mod(x, ±Inf) = x -// Mod(x, NaN) = NaN -func Mod(x, y float64) float64 { return float64(math.Mod(float64(x), float64(y))) } - -// Modf returns integer and fractional floating-point numbers -// that sum to f. Both values have the same sign as f. -// -// Special cases are: -// -// Modf(±Inf) = ±Inf, NaN -// Modf(NaN) = NaN, NaN -func Modf(f float64) (int float64, frac float64) { - i, fr := math.Modf(float64(f)) - return float64(i), float64(fr) -} - -// Nextafter64 returns the next representable float64 value after x towards y. -// -// Special cases are: -// -// Nextafter(x, x) = x -// Nextafter(NaN, y) = NaN -// Nextafter(x, NaN) = NaN -func Nextafter(x, y float64) (r float64) { return math.Nextafter(x, y) } - -// Pow returns x**y, the base-x exponential of y. -// -// Special cases are (in order): -// -// Pow(x, ±0) = 1 for any x -// Pow(1, y) = 1 for any y -// Pow(x, 1) = x for any x -// Pow(NaN, y) = NaN -// Pow(x, NaN) = NaN -// Pow(±0, y) = ±Inf for y an odd integer < 0 -// Pow(±0, -Inf) = +Inf -// Pow(±0, +Inf) = +0 -// Pow(±0, y) = +Inf for finite y < 0 and not an odd integer -// Pow(±0, y) = ±0 for y an odd integer > 0 -// Pow(±0, y) = +0 for finite y > 0 and not an odd integer -// Pow(-1, ±Inf) = 1 -// Pow(x, +Inf) = +Inf for |x| > 1 -// Pow(x, -Inf) = +0 for |x| > 1 -// Pow(x, +Inf) = +0 for |x| < 1 -// Pow(x, -Inf) = +Inf for |x| < 1 -// Pow(+Inf, y) = +Inf for y > 0 -// Pow(+Inf, y) = +0 for y < 0 -// Pow(-Inf, y) = Pow(-0, -y) -// Pow(x, y) = NaN for finite x < 0 and finite non-integer y -func Pow(x, y float64) float64 { return float64(math.Pow(float64(x), float64(y))) } - -// Pow10 returns 10**e, the base-10 exponential of e. -// -// Special cases are: -// -// Pow10(e) = +Inf for e > 309 -// Pow10(e) = 0 for e < -324 -func Pow10(e int) float64 { return float64(math.Pow10(e)) } - -// Remainder returns the IEEE 754 floating-point remainder of x/y. -// -// Special cases are: -// -// Remainder(±Inf, y) = NaN -// Remainder(NaN, y) = NaN -// Remainder(x, 0) = NaN -// Remainder(x, ±Inf) = x -// Remainder(x, NaN) = NaN -func Remainder(x, y float64) float64 { return float64(math.Remainder(float64(x), float64(y))) } - -// Signbit returns true if x is negative or negative zero. -func Signbit(x float64) bool { return math.Signbit(float64(x)) } - -// Sincos returns Sin(x), Cos(x). -// -// Special cases are: -// -// Sincos(±0) = ±0, 1 -// Sincos(±Inf) = NaN, NaN -// Sincos(NaN) = NaN, NaN -func Sincos(x float64) (sin, cos float64) { - s, c := math.Sincos(float64(x)) - return float64(s), float64(c) -} - -// Sinh returns the hyperbolic sine of x. -// -// Special cases are: -// -// Sinh(±0) = ±0 -// Sinh(±Inf) = ±Inf -// Sinh(NaN) = NaN -func Sinh(x float64) float64 { return float64(math.Sinh(float64(x))) } - -// Tan returns the tangent of the radian argument x. -// -// Special cases are: -// -// Tan(±0) = ±0 -// Tan(±Inf) = NaN -// Tan(NaN) = NaN -func Tan(x float64) float64 { return float64(math.Tan(float64(x))) } - -// Tanh returns the hyperbolic tangent of x. -// -// Special cases are: -// -// Tanh(±0) = ±0 -// Tanh(±Inf) = ±1 -// Tanh(NaN) = NaN -func Tanh(x float64) float64 { return float64(math.Tanh(float64(x))) } - -// Trunc returns the integer value of x. -// -// Special cases are: -// -// Trunc(±0) = ±0 -// Trunc(±Inf) = ±Inf -// Trunc(NaN) = NaN -func Trunc(x float64) float64 { return float64(math.Trunc(float64(x))) } - -// Y0 returns the order-zero Bessel function of the second kind. -// -// Special cases are: -// -// Y0(+Inf) = 0 -// Y0(0) = -Inf -// Y0(x < 0) = NaN -// Y0(NaN) = NaN -func Y0(x float64) float64 { return float64(math.Y0(float64(x))) } - -// Y1 returns the order-one Bessel function of the second kind. -// -// Special cases are: -// -// Y1(+Inf) = 0 -// Y1(0) = -Inf -// Y1(x < 0) = NaN -// Y1(NaN) = NaN -func Y1(x float64) float64 { return float64(math.Y1(float64(x))) } - -// Yn returns the order-n Bessel function of the second kind. -// -// Special cases are: -// -// Yn(n, +Inf) = 0 -// Yn(n > 0, 0) = -Inf -// Yn(n < 0, 0) = +Inf if n is odd, -Inf if n is even -// Y1(n, x < 0) = NaN -// Y1(n, NaN) = NaN -func Yn(n int, x float64) float64 { return float64(math.Yn(n, float64(x))) } diff --git a/algorithms/max.go b/algorithms/max.go deleted file mode 100644 index 478199f..0000000 --- a/algorithms/max.go +++ /dev/null @@ -1,39 +0,0 @@ -package algorithms - -func Max_Go[T Number](x []T) T { - max := x[0] - for _, v := range x[1:] { - if v > max { - max = v - } - } - return max -} - -func ArgMax_Go[T Number](x []T) int { - max := x[0] - idx := 0 - for i, v := range x[1:] { - if v > max { - max = v - idx = 1 + i - } - } - return idx -} - -func Maximum_Go[T Number](x, y []T) { - for i := 0; i < len(x); i++ { - if y[i] > x[i] { - x[i] = y[i] - } - } -} - -func MaximumNumber_Go[T Number](x []T, a T) { - for i := 0; i < len(x); i++ { - if a > x[i] { - x[i] = a - } - } -} diff --git a/algorithms/min.go b/algorithms/min.go deleted file mode 100644 index ca46745..0000000 --- a/algorithms/min.go +++ /dev/null @@ -1,39 +0,0 @@ -package algorithms - -func Min_Go[T Number](x []T) T { - min := x[0] - for _, v := range x[1:] { - if v < min { - min = v - } - } - return min -} - -func ArgMin_Go[T Number](x []T) int { - min := x[0] - idx := 0 - for i, v := range x[1:] { - if v < min { - min = v - idx = 1 + i - } - } - return idx -} - -func Minimum_Go[T Number](x, y []T) { - for i := 0; i < len(x); i++ { - if y[i] < x[i] { - x[i] = y[i] - } - } -} - -func MinimumNumber_Go[T Number](x []T, a T) { - for i := 0; i < len(x); i++ { - if a < x[i] { - x[i] = a - } - } -} diff --git a/slice_float64.go b/slice_float64.go new file mode 100644 index 0000000..80e9925 --- /dev/null +++ b/slice_float64.go @@ -0,0 +1,47 @@ +package pandas + +func slice_any_to_float64[T Number](s []T) []float64 { + count := len(s) + if count == 0 { + return []float64{} + } + d := make([]float64, count) + for idx, iv := range s { + d[idx] = float64(iv) + } + return d +} + +// any输入只能是一维slice或者数组 +func numberToFloat64(v any) []float64 { + var vs []float64 + switch values := v.(type) { + case []float64: + return values + case []int64: + return slice_any_to_float64(values) + case []int32: + return slice_any_to_float64(values) + case []int: + return slice_any_to_float64(values) + case []bool: + count := len(values) + if count == 0 { + return []float64{} + } + vs = make([]float64, count) + for idx, iv := range values { + vs[idx] = AnyToFloat64(iv) + } + case []string: + count := len(values) + if count == 0 { + return []float64{} + } + vs = make([]float64, count) + for idx, iv := range values { + vs[idx] = AnyToFloat64(iv) + } + } + return vs +} -- Gitee From ddbad6378c120211e47c1023509fab854cd457e0 Mon Sep 17 00:00:00 2001 From: wangfeng Date: Thu, 2 Feb 2023 12:47:09 +0800 Subject: [PATCH 08/28] =?UTF-8?q?math=E7=9B=B8=E5=85=B3=E5=9F=BA=E7=A1=80?= =?UTF-8?q?=E5=87=BD=E6=95=B0=E5=BD=92=E4=BA=8E=E7=B3=BB=E7=BB=9F=E5=BA=93?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- series_ewm.go | 3 +-- 1 file changed, 1 insertion(+), 2 deletions(-) diff --git a/series_ewm.go b/series_ewm.go index 4d09adf..0ece628 100644 --- a/series_ewm.go +++ b/series_ewm.go @@ -1,7 +1,6 @@ package pandas import ( - "gitee.com/quant1x/pandas/algorithms" "math" ) @@ -98,7 +97,7 @@ func (w ExponentialMovingWindow) Mean() Series { if w.param <= 0 { panic("halflife param must be > 0") } - alpha = 1 - algorithms.Exp(-math.Ln2/w.param) + alpha = 1 - math.Exp(-math.Ln2/w.param) } return w.applyMean(w.data, alpha) -- Gitee From d6c0163d2d4b242f11b4ded4755ba8796979b981 Mon Sep 17 00:00:00 2001 From: wangfeng Date: Thu, 2 Feb 2023 12:47:35 +0800 Subject: [PATCH 09/28] =?UTF-8?q?math=E7=9B=B8=E5=85=B3=E5=9F=BA=E7=A1=80?= =?UTF-8?q?=E5=87=BD=E6=95=B0=E5=BD=92=E4=BA=8E=E7=B3=BB=E7=BB=9F=E5=BA=93?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- series_float64.go | 4 ++-- 1 file changed, 2 insertions(+), 2 deletions(-) diff --git a/series_float64.go b/series_float64.go index 400f24c..a0019aa 100644 --- a/series_float64.go +++ b/series_float64.go @@ -1,9 +1,9 @@ package pandas import ( - "gitee.com/quant1x/pandas/algorithms" "gitee.com/quant1x/pandas/algorithms/avx2" "gonum.org/v1/gonum/stat" + "math" "reflect" ) @@ -132,7 +132,7 @@ func (self *SeriesFloat64) oldShift(periods int) *Series { src []float64 ) - if shlen := int(algorithms.Abs(float64(periods))); shlen < len(values) { + if shlen := int(math.Abs(float64(periods))); shlen < len(values) { if periods > 0 { naVals = values[:shlen] dst = values[shlen:] -- Gitee From df4c8878af1157cd8fbb65cab8d0f8fbe6eca945 Mon Sep 17 00:00:00 2001 From: wangfeng Date: Thu, 2 Feb 2023 12:48:30 +0800 Subject: [PATCH 10/28] =?UTF-8?q?=E8=B0=83=E6=95=B4avx2=E7=9A=84package?= =?UTF-8?q?=E8=B7=AF=E5=BE=84?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- series_number.go | 46 ++++++++++++++++++++++++++++++++++++++++++---- 1 file changed, 42 insertions(+), 4 deletions(-) diff --git a/series_number.go b/series_number.go index 78e42f8..f446bd4 100644 --- a/series_number.go +++ b/series_number.go @@ -1,9 +1,47 @@ package pandas -import "gitee.com/quant1x/pandas/algorithms" +import ( + "gitee.com/quant1x/pandas/algorithms/avx2" + "math/big" +) + +type BigFloat = big.Float // 预留将来可能扩展float + +type Number8 interface { + ~int8 | ~uint8 +} + +type Number16 interface { + ~int16 | ~uint16 +} + +type Number32 interface { + ~int32 | ~uint32 | float32 +} + +type Number64 interface { + ~int64 | ~uint64 | float64 +} + +type Float interface { + ~float32 | ~float64 +} + +// Number int和uint的长度取决于CPU是多少位 +type Number interface { + Number8 | Number16 | Number32 | Number64 | Float | int | uint +} + +//const ( +// True2Float32 float32 = float32(1) // true转float32 +// False2Float32 float32 = float32(0) // false转float32 +// StringTrue2Float32 float32 = float32(1) // 字符串true转float32 +// StringFalse2Float32 float32 = float32(0) // 字符串false转float32 +//) // Mean gonum.org/v1/gonum/stat不支持整型, 每次都要转换有点难受啊 -func Mean[T algorithms.Number](x []T) float64 { - d := algorithms.Mean_Go(x) - return float64(d) +func Mean[T Number](x []T) float64 { + d := numberToFloat64(x) + s := avx2.Mean(d) + return float64(s) } -- Gitee From fa141b940813c4a7fa62035e597096971024ff43 Mon Sep 17 00:00:00 2001 From: wangfeng Date: Thu, 2 Feb 2023 12:49:23 +0800 Subject: [PATCH 11/28] =?UTF-8?q?=E5=A2=9E=E5=8A=A0bool=E8=BD=ACfloat32?= =?UTF-8?q?=E7=9A=84=E5=87=BD=E6=95=B0?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- type_bool.go | 25 ++++++++++++++++++------- 1 file changed, 18 insertions(+), 7 deletions(-) diff --git a/type_bool.go b/type_bool.go index b0a546f..659ccb9 100644 --- a/type_bool.go +++ b/type_bool.go @@ -6,13 +6,16 @@ import ( ) const ( - Nil2Bool = false // 空指针转int64 - BoolNaN = false // int64 无效值 - True2Bool = true // true转int64 - False2Bool = false // false 转int64 - StringBad2Bool = false // 字符串解析int64异常 - StringTrue2Bool = true // 字符串true转int64 - StringFalse2Bool = false // 字符串false转int64 + Nil2Bool = false // 空指针转bool + BoolNaN = false // bool 无效值 + True2Bool = true // true转bool + False2Bool = false // false 转bool + True2Float32 float32 = float32(1) // true转float32 + False2Float32 float32 = float32(0) // false转float32 + + StringBad2Bool = false // 字符串解析bool异常 + StringTrue2Bool = true // 字符串true转bool + StringFalse2Bool = false // 字符串false转bool ) // AnyToBool any转换bool @@ -135,3 +138,11 @@ func float2Bool[T ~float32 | float64](f T) bool { } return true } + +// bool转float32 +func boolToFloat32(b bool) float32 { + if b { + return True2Float32 + } + return False2Float32 +} -- Gitee From 6fea3e625035d92b7c0666118d57725f171c89f4 Mon Sep 17 00:00:00 2001 From: wangfeng Date: Thu, 2 Feb 2023 12:51:52 +0800 Subject: [PATCH 12/28] =?UTF-8?q?=E4=BF=AE=E6=94=B9=E5=AD=97=E7=AC=A6?= =?UTF-8?q?=E4=B8=B2=E8=A7=A3=E6=9E=90float64=E7=9A=84=E5=87=BD=E6=95=B0?= =?UTF-8?q?=E5=90=8D=E7=A7=B0?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- type_float64.go | 10 +++++----- type_float64_test.go | 6 +++--- 2 files changed, 8 insertions(+), 8 deletions(-) diff --git a/type_float64.go b/type_float64.go index a437f49..c2e47e5 100644 --- a/type_float64.go +++ b/type_float64.go @@ -65,7 +65,7 @@ func AnyToFloat64(v any) float64 { } else if isFalse(*val) { return StringFalse2Float } - f := ParseFloat(*val, v) + f := ParseFloat64(*val, v) return f case string: if IsEmpty(val) { @@ -76,17 +76,17 @@ func AnyToFloat64(v any) float64 { } else if isFalse(val) { return StringFalse2Float } - f := ParseFloat(val, v) + f := ParseFloat64(val, v) return f default: - f := ParseFloat(fmt.Sprintf("%v", v), v) + f := ParseFloat64(fmt.Sprintf("%v", v), v) return f } } -// ParseFloat 字符串转float64 +// ParseFloat64 字符串转float64 // 任意组合的nan字符串都会被解析成NaN -func ParseFloat(s string, v any) float64 { +func ParseFloat64(s string, v any) float64 { f, err := strconv.ParseFloat(s, 64) if err != nil { if IgnoreParseExceptions { diff --git a/type_float64_test.go b/type_float64_test.go index cc8066c..a4a06cf 100644 --- a/type_float64_test.go +++ b/type_float64_test.go @@ -90,9 +90,9 @@ func TestParseFloat2Nan(t *testing.T) { } for _, tt := range tests { t.Run(tt.name, func(t *testing.T) { - //if got := ParseFloat(tt.args.s, tt.args.v); got != tt.want { - if got := ParseFloat(tt.args.s, tt.args.v); !math.IsNaN(got) { - t.Errorf("ParseFloat() = %v, want %v", got, tt.want) + //if got := ParseFloat64(tt.args.s, tt.args.v); got != tt.want { + if got := ParseFloat64(tt.args.s, tt.args.v); !math.IsNaN(got) { + t.Errorf("ParseFloat64() = %v, want %v", got, tt.want) } }) } -- Gitee From 1732437c2153485957bb3724dd8f54f9124d73cf Mon Sep 17 00:00:00 2001 From: wangfeng Date: Thu, 2 Feb 2023 12:53:52 +0800 Subject: [PATCH 13/28] =?UTF-8?q?=E4=BF=AE=E6=94=B9float64=E8=BD=ACstring?= =?UTF-8?q?=E7=9A=84=E5=87=BD=E6=95=B0=E5=90=8D?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- series_float64.go | 2 +- type_float64.go | 2 +- 2 files changed, 2 insertions(+), 2 deletions(-) diff --git a/series_float64.go b/series_float64.go index a0019aa..c7d74a0 100644 --- a/series_float64.go +++ b/series_float64.go @@ -179,7 +179,7 @@ func (self *SeriesFloat64) Records() []string { for i := 0; i < self.Len(); i++ { //e := self.elements.Elem(i) e := self.Data[i] - ret[i] = float2String(e) + ret[i] = float64ToString(e) } return ret } diff --git a/type_float64.go b/type_float64.go index c2e47e5..71abec0 100644 --- a/type_float64.go +++ b/type_float64.go @@ -98,7 +98,7 @@ func ParseFloat64(s string, v any) float64 { return f } -func float2String(v float64) string { +func float64ToString(v float64) string { if isNaN(v) { return StringNaN } -- Gitee From 8c4cbbbe556f0caa938506772310945f4a20c235 Mon Sep 17 00:00:00 2001 From: wangfeng Date: Thu, 2 Feb 2023 12:57:52 +0800 Subject: [PATCH 14/28] =?UTF-8?q?=E8=A7=84=E8=8C=83=E5=87=BD=E6=95=B0?= =?UTF-8?q?=E6=B3=A8=E9=87=8A?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- builtin.go | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/builtin.go b/builtin.go index 6373134..09472ca 100644 --- a/builtin.go +++ b/builtin.go @@ -29,7 +29,7 @@ func NaN() float64 { return math.NaN() } -// IsNan float64是否NaN +// IsNaN float64是否NaN func IsNaN(f float64) bool { return math.IsNaN(f) || math.IsInf(f, 0) } -- Gitee From a649b58303ff28557c300c17f1b276a08f80d2c6 Mon Sep 17 00:00:00 2001 From: wangfeng Date: Thu, 2 Feb 2023 12:59:18 +0800 Subject: [PATCH 15/28] =?UTF-8?q?=E5=A2=9E=E5=8A=A0float32=E5=88=A4?= =?UTF-8?q?=E6=96=AD=E6=98=AF=E5=90=A6NaN=E7=9A=84=E5=87=BD=E6=95=B0?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- type_float32.go | 5 +++++ 1 file changed, 5 insertions(+) diff --git a/type_float32.go b/type_float32.go index 6ce3bb9..3a75abb 100644 --- a/type_float32.go +++ b/type_float32.go @@ -13,6 +13,11 @@ const ( StringFalse2Float32 float32 = float32(0) // 字符串false转float32 ) +// Float32IsNaN 判断float32是否NaN +func Float32IsNaN(f float32) bool { + return IsNaN(float64(f)) +} + // ParseFloat32 字符串转float32 func ParseFloat32(s string, v any) float32 { defer func() { -- Gitee From 8ca37fd5845aec99cdcaaece14bd994dffefbbf3 Mon Sep 17 00:00:00 2001 From: wangfeng Date: Thu, 2 Feb 2023 13:00:40 +0800 Subject: [PATCH 16/28] =?UTF-8?q?=E5=A2=9E=E5=8A=A0float64=E5=88=A4?= =?UTF-8?q?=E6=96=AD=E6=98=AF=E5=90=A6NaN=E7=9A=84=E5=87=BD=E6=95=B0?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- type_float64.go | 5 +++++ 1 file changed, 5 insertions(+) diff --git a/type_float64.go b/type_float64.go index 71abec0..947d14b 100644 --- a/type_float64.go +++ b/type_float64.go @@ -14,6 +14,11 @@ const ( StringFalse2Float float64 = float64(0) // 字符串false转float64 ) +// Float64IsNaN 判断float64是否NaN +func Float64IsNaN(f float64) bool { + return IsNaN(f) +} + // AnyToFloat64 any转换float64 func AnyToFloat64(v any) float64 { switch val := v.(type) { -- Gitee From bf95949f3e4d0b7a9aa496de26c58532e495f2fd Mon Sep 17 00:00:00 2001 From: wangfeng Date: Thu, 2 Feb 2023 13:07:26 +0800 Subject: [PATCH 17/28] =?UTF-8?q?=E8=A7=A3=E6=9E=90string=E4=B8=BAfloat32?= =?UTF-8?q?=E5=A4=B1=E8=B4=A5=E5=90=8E=E8=BF=94=E5=9B=9ENaN?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- type_float32.go | 3 ++- 1 file changed, 2 insertions(+), 1 deletion(-) diff --git a/type_float32.go b/type_float32.go index 3a75abb..12368d4 100644 --- a/type_float32.go +++ b/type_float32.go @@ -21,6 +21,7 @@ func Float32IsNaN(f float32) bool { // ParseFloat32 字符串转float32 func ParseFloat32(s string, v any) float32 { defer func() { + // 解析失败以后输出日志, 以备检查 if err := recover(); err != nil { logger.Errorf("ParseFloat32 %+v, error=%+v\n", v, err) } @@ -33,7 +34,7 @@ func ParseFloat32(s string, v any) float32 { return Nil2Float32 } _ = v.(float32) // Intentionally panic - return float32(0) + return Nil2Float32 } func float32ToString(v float32) string { -- Gitee From 0819d9b10f1bb0ccb7ae1f52eab16cd946fdef09 Mon Sep 17 00:00:00 2001 From: wangfeng Date: Thu, 2 Feb 2023 13:13:51 +0800 Subject: [PATCH 18/28] =?UTF-8?q?=E8=A7=84=E8=8C=83NaN=E7=9A=84=E5=88=A4?= =?UTF-8?q?=E6=96=AD=E5=87=BD=E6=95=B0,=20=E5=BD=92=E4=BA=8E=E7=BB=9F?= =?UTF-8?q?=E4=B8=80=E7=94=A8=E6=B3=95?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- type_float32.go | 3 ++- 1 file changed, 2 insertions(+), 1 deletion(-) diff --git a/type_float32.go b/type_float32.go index 12368d4..3881002 100644 --- a/type_float32.go +++ b/type_float32.go @@ -37,8 +37,9 @@ func ParseFloat32(s string, v any) float32 { return Nil2Float32 } +// float32转string func float32ToString(v float32) string { - if isNaN(float64(v)) { + if Float32IsNaN(v) { return StringNaN } return fmt.Sprintf("%f", v) -- Gitee From 85538979ffa3f17d1b284e2c4c49b72f278bbfe7 Mon Sep 17 00:00:00 2001 From: wangfeng Date: Thu, 2 Feb 2023 13:14:31 +0800 Subject: [PATCH 19/28] =?UTF-8?q?=E4=BF=AE=E8=AE=A2=E8=A7=A3=E6=9E=90float?= =?UTF-8?q?64=E5=AD=97=E7=AC=A6=E4=B8=B2=E7=9A=84=E5=87=BD=E6=95=B0?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- type_float64.go | 35 +++++++++++++++++++++-------------- 1 file changed, 21 insertions(+), 14 deletions(-) diff --git a/type_float64.go b/type_float64.go index 947d14b..e519126 100644 --- a/type_float64.go +++ b/type_float64.go @@ -2,6 +2,7 @@ package pandas import ( "fmt" + "github.com/mymmsc/gox/logger" "strconv" ) @@ -19,6 +20,26 @@ func Float64IsNaN(f float64) bool { return IsNaN(f) } +// ParseFloat64 字符串转float64 +// 任意组合的nan字符串都会被解析成NaN +func ParseFloat64(s string, v any) float64 { + defer func() { + // 解析失败以后输出日志, 以备检查 + if err := recover(); err != nil { + logger.Errorf("ParseFloat64 %+v, error=%+v\n", v, err) + } + }() + f, err := strconv.ParseFloat(s, 64) + if err == nil { + return f + } + if IgnoreParseExceptions { + return Nil2Float + } + _ = v.(float64) // Intentionally panic + return Nil2Float +} + // AnyToFloat64 any转换float64 func AnyToFloat64(v any) float64 { switch val := v.(type) { @@ -89,20 +110,6 @@ func AnyToFloat64(v any) float64 { } } -// ParseFloat64 字符串转float64 -// 任意组合的nan字符串都会被解析成NaN -func ParseFloat64(s string, v any) float64 { - f, err := strconv.ParseFloat(s, 64) - if err != nil { - if IgnoreParseExceptions { - f = Nil2Float - } else { - _ = v.(float64) // Intentionally panic - } - } - return f -} - func float64ToString(v float64) string { if isNaN(v) { return StringNaN -- Gitee From f6d02dce2b4b18ad4602a582ef6709d864c006af Mon Sep 17 00:00:00 2001 From: wangfeng Date: Thu, 2 Feb 2023 13:16:43 +0800 Subject: [PATCH 20/28] =?UTF-8?q?=E8=A7=84=E8=8C=83float64=E7=9A=84nil?= =?UTF-8?q?=E5=85=A8=E5=B1=80=E5=8F=98=E9=87=8F=E5=90=8D?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- builtin.go | 8 ++++---- generic.go | 6 +++--- series.go | 2 +- series_float64.go | 4 ++-- type_float64.go | 18 +++++++++--------- 5 files changed, 19 insertions(+), 19 deletions(-) diff --git a/builtin.go b/builtin.go index 09472ca..94d8232 100644 --- a/builtin.go +++ b/builtin.go @@ -12,16 +12,16 @@ import ( // 全局变量定义 var ( - // Nil2Float nil指针转换float64 - Nil2Float = float64(0) + // Nil2Float64 nil指针转换float64 + Nil2Float64 = float64(0) // Nil2Float32 nil指针转换float32 Nil2Float32 = float32(0) ) func init() { - Nil2Float = math.NaN() + Nil2Float64 = math.NaN() // 这个转换是对的, NaN对float32也有效 - Nil2Float32 = float32(Nil2Float) + Nil2Float32 = float32(Nil2Float64) } // NaN returns an IEEE 754 “not-a-number” value. diff --git a/generic.go b/generic.go index 381bc4c..380be99 100644 --- a/generic.go +++ b/generic.go @@ -183,7 +183,7 @@ func (self *NDFrame) apply(f func(idx int, v any)) { vk := vv.Kind() switch vk { case reflect.Invalid: // {interface} nil - //series.assign(idx, size, Nil2Float) + //series.assign(idx, size, Nil2Float64) case reflect.Slice: // 切片, 不定长 for i := 0; i < vv.Len(); i++ { tv := vv.Index(i).Interface() @@ -300,7 +300,7 @@ func (self *NDFrame) Shift(periods int) Series { var d Series d = clone(self).(Series) //return Shift[float64](&d, periods, func() float64 { - // return Nil2Float + // return Nil2Float64 //}) switch values := self.values.(type) { case []bool: @@ -318,7 +318,7 @@ func (self *NDFrame) Shift(periods int) Series { }) default: //case []float64: return Shift[float64](&d, periods, func() float64 { - return Nil2Float + return Nil2Float64 }) } } diff --git a/series.go b/series.go index 3bfa2eb..25baa0c 100644 --- a/series.go +++ b/series.go @@ -89,7 +89,7 @@ func GenericSeries[T GenericType](name string, values ...T) *Series { vk := vv.Kind() switch vk { //case reflect.Invalid: // {interface} nil - // series.assign(idx, size, Nil2Float) + // series.assign(idx, size, Nil2Float64) //case reflect.Slice: // 切片, 不定长 // for i := 0; i < vv.Len(); i++ { // tv := vv.Index(i).Interface() diff --git a/series_float64.go b/series_float64.go index c7d74a0..00c06a6 100644 --- a/series_float64.go +++ b/series_float64.go @@ -37,7 +37,7 @@ func NewSeriesFloat64(name string, vals ...interface{}) *SeriesFloat64 { vk := vv.Kind() switch vk { case reflect.Invalid: // {interface} nil - series.assign(idx, size, Nil2Float) + series.assign(idx, size, Nil2Float64) case reflect.Slice: // 切片, 不定长 for i := 0; i < vv.Len(); i++ { tv := vv.Index(i).Interface() @@ -112,7 +112,7 @@ func (self *SeriesFloat64) Shift(periods int) Series { var d Series d = clone(self).(Series) return Shift[float64](&d, periods, func() float64 { - return Nil2Float + return Nil2Float64 }) } diff --git a/type_float64.go b/type_float64.go index e519126..8996bd2 100644 --- a/type_float64.go +++ b/type_float64.go @@ -34,10 +34,10 @@ func ParseFloat64(s string, v any) float64 { return f } if IgnoreParseExceptions { - return Nil2Float + return Nil2Float64 } _ = v.(float64) // Intentionally panic - return Nil2Float + return Nil2Float64 } // AnyToFloat64 any转换float64 @@ -47,7 +47,7 @@ func AnyToFloat64(v any) float64 { return nan() case *bool: if val == nil { - return Nil2Float + return Nil2Float64 } if *val == true { return True2Float @@ -60,31 +60,31 @@ func AnyToFloat64(v any) float64 { return False2Float case *int: if val == nil { - return Nil2Float + return Nil2Float64 } return float64(*val) case int: return float64(val) case *int64: if val == nil { - return Nil2Float + return Nil2Float64 } return float64(*val) case int64: return float64(val) case *float64: if val == nil { - return Nil2Float + return Nil2Float64 } return *val case float64: return val case *string: if val == nil { - return Nil2Float + return Nil2Float64 } if IsEmpty(*val) { - return Nil2Float + return Nil2Float64 } if isTrue(*val) { return StringTrue2Float @@ -95,7 +95,7 @@ func AnyToFloat64(v any) float64 { return f case string: if IsEmpty(val) { - return Nil2Float + return Nil2Float64 } if isTrue(val) { return StringTrue2Float -- Gitee From 137757f80329f6180f8d9902aeb1d45eedb09683 Mon Sep 17 00:00:00 2001 From: wangfeng Date: Thu, 2 Feb 2023 13:18:04 +0800 Subject: [PATCH 21/28] =?UTF-8?q?=E8=B0=83=E6=95=B4=E5=87=BD=E6=95=B0?= =?UTF-8?q?=E7=9A=84=E7=9B=B8=E5=AF=B9=E4=BD=8D=E7=BD=AE?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- type_float64.go | 15 ++++++++------- 1 file changed, 8 insertions(+), 7 deletions(-) diff --git a/type_float64.go b/type_float64.go index 8996bd2..159abe4 100644 --- a/type_float64.go +++ b/type_float64.go @@ -40,6 +40,14 @@ func ParseFloat64(s string, v any) float64 { return Nil2Float64 } +// float64转string +func float64ToString(v float64) string { + if isNaN(v) { + return StringNaN + } + return fmt.Sprintf("%f", v) +} + // AnyToFloat64 any转换float64 func AnyToFloat64(v any) float64 { switch val := v.(type) { @@ -109,10 +117,3 @@ func AnyToFloat64(v any) float64 { return f } } - -func float64ToString(v float64) string { - if isNaN(v) { - return StringNaN - } - return fmt.Sprintf("%f", v) -} -- Gitee From 7545c4338b755c6adf3a1c331dc97ee16977f872 Mon Sep 17 00:00:00 2001 From: wangfeng Date: Thu, 2 Feb 2023 13:20:36 +0800 Subject: [PATCH 22/28] =?UTF-8?q?=E6=96=B0=E5=A2=9E.gitignore?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- .gitignore | 19 +++++++++++++++++++ 1 file changed, 19 insertions(+) create mode 100644 .gitignore diff --git a/.gitignore b/.gitignore new file mode 100644 index 0000000..da13d98 --- /dev/null +++ b/.gitignore @@ -0,0 +1,19 @@ +.DS_Store +.idea/ +target/ +*.iml +assembly/version.env +assembly/version.properties + +# VS CODE +.vscode/ +.classpath +.factorypath +.project +.settings/ + +# go.test.sh +coverage.txt + +# test +tutorials.csv -- Gitee From 4d178f774479af97649fc04d1630b96e4e1b8416 Mon Sep 17 00:00:00 2001 From: wangfeng Date: Thu, 2 Feb 2023 13:37:00 +0800 Subject: [PATCH 23/28] =?UTF-8?q?=E4=BF=9D=E7=95=99=E5=8E=9Fcase=E5=88=A4?= =?UTF-8?q?=E6=96=AD=E7=9A=84=E4=BD=8D=E7=BD=AE,=20=E4=BB=A3=E7=A0=81?= =?UTF-8?q?=E6=B3=A8=E9=87=8A=E6=8E=89?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- type_float32.go | 7 +++++++ 1 file changed, 7 insertions(+) diff --git a/type_float32.go b/type_float32.go index 3881002..31691e2 100644 --- a/type_float32.go +++ b/type_float32.go @@ -69,6 +69,8 @@ func value_to_float32(v any) float32 { return float32(val) case uint: return float32(val) + //case float32: + // return val case float64: return float32(val) case bool: @@ -150,6 +152,11 @@ func point_to_float32(v any) float32 { return Nil2Float32 } return float32(*val) + //case *float32: + // if val == nil { + // return Nil2Float32 + // } + // return float32(*val) case *float64: if val == nil { return Nil2Float32 -- Gitee From f95b3e0ecbba1fe0bd3b1ff04a1c42c2f4d6ef0e Mon Sep 17 00:00:00 2001 From: wangfeng Date: Thu, 2 Feb 2023 13:39:19 +0800 Subject: [PATCH 24/28] =?UTF-8?q?=E8=B0=83=E6=95=B4float64=E7=9A=84?= =?UTF-8?q?=E8=BD=AC=E6=8D=A2=E5=87=BD=E6=95=B0?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- type_float64.go | 167 +++++++++++++++++++++++++++++++++++------------- 1 file changed, 124 insertions(+), 43 deletions(-) diff --git a/type_float64.go b/type_float64.go index 159abe4..5d57ecb 100644 --- a/type_float64.go +++ b/type_float64.go @@ -3,16 +3,18 @@ package pandas import ( "fmt" "github.com/mymmsc/gox/logger" + "math" "strconv" ) const ( - True2Float float64 = float64(1) // true转float64 - False2Float float64 = float64(0) - StringNil2Float float64 = float64(0) // deprecated: 字符串空指针转float64 - StringBad2Float float64 = float64(0) // deprecated: 字符串解析float64异常 - StringTrue2Float float64 = float64(1) // 字符串true转float64 - StringFalse2Float float64 = float64(0) // 字符串false转float64 + MaxFloat64 float64 = math.MaxFloat64 // float64最大值 + True2Float64 float64 = float64(1) // true转float64 + False2Float64 float64 = float64(0) // false转float64 + StringNil2Float float64 = float64(0) // deprecated: 字符串空指针转float64 + StringBad2Float float64 = float64(0) // deprecated: 字符串解析float64异常 + StringTrue2Float64 float64 = float64(1) // 字符串true转float64 + StringFalse2Float64 float64 = float64(0) // 字符串false转float64 ) // Float64IsNaN 判断float64是否NaN @@ -48,45 +50,130 @@ func float64ToString(v float64) string { return fmt.Sprintf("%f", v) } -// AnyToFloat64 any转换float64 -func AnyToFloat64(v any) float64 { +func value_to_float64(v any) float64 { switch val := v.(type) { - case nil: - return nan() - case *bool: + case float64: // 直接转换的放在第一位判断 + return float64(val) + case int8: + return float64(val) + case uint8: + return float64(val) + case int16: + return float64(val) + case uint16: + return float64(val) + case int32: + return float64(val) + case uint32: + return float64(val) + case int64: + return float64(val) + case uint64: + return float64(val) + case int: + return float64(val) + case uint: + return float64(val) + case float32: + return float64(val) + //case float64: + // return float64(val) + case bool: + if val == true { + return True2Float64 + } + return False2Float64 + case string: + if IsEmpty(val) { + return Nil2Float64 + } + if isTrue(val) { + return StringTrue2Float64 + } else if isFalse(val) { + return StringFalse2Float64 + } + f := ParseFloat64(val, v) + return f + } + return float64(0) +} + +// 指针转float64 +func point_to_float64(v any) float64 { + switch val := v.(type) { + case *float64: if val == nil { return Nil2Float64 } - if *val == true { - return True2Float + return *val + case *int8: + if val == nil { + return Nil2Float64 } - return False2Float - case bool: - if val == true { - return True2Float + return float64(*val) + case *uint8: + if val == nil { + return Nil2Float64 } - return False2Float - case *int: + return float64(*val) + case *int16: + if val == nil { + return Nil2Float64 + } + return float64(*val) + case *uint16: + if val == nil { + return Nil2Float64 + } + return float64(*val) + case *int32: + if val == nil { + return Nil2Float64 + } + return float64(*val) + case *uint32: if val == nil { return Nil2Float64 } return float64(*val) - case int: - return float64(val) case *int64: if val == nil { return Nil2Float64 } return float64(*val) - case int64: - return float64(val) - case *float64: + case *uint64: if val == nil { return Nil2Float64 } - return *val - case float64: - return val + return float64(*val) + case *int: + if val == nil { + return Nil2Float64 + } + return float64(*val) + case *uint: + if val == nil { + return Nil2Float64 + } + return float64(*val) + case *float32: + if val == nil { + return Nil2Float64 + } + return float64(*val) + //case *float64: + // if val == nil { + // return Nil2Float64 + // } + // return float64(*val) + case *bool: + if val == nil { + return Nil2Float64 + } + if *val == true { + return True2Float64 + } + return False2Float64 case *string: if val == nil { return Nil2Float64 @@ -95,25 +182,19 @@ func AnyToFloat64(v any) float64 { return Nil2Float64 } if isTrue(*val) { - return StringTrue2Float + return StringTrue2Float64 } else if isFalse(*val) { - return StringFalse2Float + return StringFalse2Float64 } f := ParseFloat64(*val, v) return f - case string: - if IsEmpty(val) { - return Nil2Float64 - } - if isTrue(val) { - return StringTrue2Float - } else if isFalse(val) { - return StringFalse2Float - } - f := ParseFloat64(val, v) - return f - default: - f := ParseFloat64(fmt.Sprintf("%v", v), v) - return f } + return float64(0) +} + +func AnyToFloat64(v any) float64 { + if isPoint(v) { + return point_to_float64(v) + } + return value_to_float64(v) } -- Gitee From 704e01754d1b667e55f3db92dec441424649d6a8 Mon Sep 17 00:00:00 2001 From: wangfeng Date: Thu, 2 Feb 2023 13:43:31 +0800 Subject: [PATCH 25/28] =?UTF-8?q?=E8=B0=83=E6=95=B4int64=E7=9A=84=E8=BD=AC?= =?UTF-8?q?=E6=8D=A2=E5=87=BD=E6=95=B0?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- series_int64.go | 2 +- type_int64.go | 54 ++++++++++++++++++++++++------------------------- 2 files changed, 28 insertions(+), 28 deletions(-) diff --git a/series_int64.go b/series_int64.go index 5880209..4b2dbb3 100644 --- a/series_int64.go +++ b/series_int64.go @@ -134,7 +134,7 @@ func (self *SeriesInt64) Records() []string { ret := make([]string, self.Len()) for i := 0; i < self.Len(); i++ { e := self.Data[i] - ret[i] = int2String(e) + ret[i] = int64ToString(e) } return ret } diff --git a/type_int64.go b/type_int64.go index 32ebd05..0de6421 100644 --- a/type_int64.go +++ b/type_int64.go @@ -16,6 +16,30 @@ const ( StringFalse2Int = int64(0) // 字符串false转int64 ) +// ParseInt64 解析int字符串, 尝试解析10进制和16进制 +func ParseInt64(s string, v any) int64 { + i, err := strconv.ParseInt(s, 10, 64) + if err != nil { + i, err = strconv.ParseInt(s, 16, 64) + if err != nil { + logger.Errorf("%s, error=%+v\n", s, err) + if IgnoreParseExceptions { + i = StringBad2Int + } else { + _ = v.(int64) // Intentionally panic + } + } + } + return i +} + +func int64ToString(v int64) string { + if Float64IsNaN(float64(v)) { + return StringNaN + } + return fmt.Sprint(v) +} + // AnyToInt64 any转换int64 func AnyToInt64(v any) int64 { switch val := v.(type) { @@ -74,7 +98,7 @@ func AnyToInt64(v any) int64 { } else if isFalse(*val) { return StringFalse2Int } - i := ParseInt(*val, v) + i := ParseInt64(*val, v) return i case string: if IsEmpty(val) { @@ -85,34 +109,10 @@ func AnyToInt64(v any) int64 { } else if isFalse(val) { return StringFalse2Int } - i := ParseInt(val, v) + i := ParseInt64(val, v) return i default: - i := ParseInt(fmt.Sprintf("%v", v), v) + i := ParseInt64(fmt.Sprintf("%v", v), v) return i } } - -// ParseInt 解析int字符串, 尝试解析10进制和16进制 -func ParseInt(s string, v any) int64 { - i, err := strconv.ParseInt(s, 10, 64) - if err != nil { - i, err = strconv.ParseInt(s, 16, 64) - if err != nil { - logger.Errorf("%s, error=%+v\n", s, err) - if IgnoreParseExceptions { - i = StringBad2Int - } else { - _ = v.(int64) // Intentionally panic - } - } - } - return i -} - -func int2String(v int64) string { - if isNaN(float64(v)) { - return StringNaN - } - return fmt.Sprint(v) -} -- Gitee From 056d5cf033e32aec1b2e8b4294d9308ef59a2299 Mon Sep 17 00:00:00 2001 From: wangfeng Date: Thu, 2 Feb 2023 14:01:16 +0800 Subject: [PATCH 26/28] =?UTF-8?q?=E8=B0=83=E6=95=B4int64=E7=9A=84=E8=BD=AC?= =?UTF-8?q?=E6=8D=A2=E5=87=BD=E6=95=B0?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- type_int64.go | 29 +++++++++++++++++++---------- 1 file changed, 19 insertions(+), 10 deletions(-) diff --git a/type_int64.go b/type_int64.go index 0de6421..c467d05 100644 --- a/type_int64.go +++ b/type_int64.go @@ -18,17 +18,26 @@ const ( // ParseInt64 解析int字符串, 尝试解析10进制和16进制 func ParseInt64(s string, v any) int64 { - i, err := strconv.ParseInt(s, 10, 64) - if err != nil { - i, err = strconv.ParseInt(s, 16, 64) - if err != nil { - logger.Errorf("%s, error=%+v\n", s, err) - if IgnoreParseExceptions { - i = StringBad2Int - } else { - _ = v.(int64) // Intentionally panic - } + defer func() { + // 解析失败以后输出日志, 以备检查 + if err := recover(); err != nil { + logger.Errorf("ParseInt64 %+v, error=%+v\n", v, err) } + }() + i, err := strconv.ParseInt(s, 10, 64) + if err == nil { + return i + } + // 解析失败继续解析16进制 + i, err = strconv.ParseInt(s, 16, 64) + if err == nil { + return i + } + logger.Errorf("%s, error=%+v\n", s, err) + if IgnoreParseExceptions { + i = StringBad2Int + } else { + _ = v.(int64) // Intentionally panic } return i } -- Gitee From 7f558592de457b50531099a17da171b3ff632257 Mon Sep 17 00:00:00 2001 From: wangfeng Date: Thu, 2 Feb 2023 15:10:39 +0800 Subject: [PATCH 27/28] =?UTF-8?q?=E9=87=8D=E6=96=B0=E6=A2=B3=E7=90=86?= =?UTF-8?q?=E7=B1=BB=E5=9E=8B=E8=BD=AC=E6=8D=A2=E5=87=BD=E6=95=B0?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- generic.go | 2 +- series_float64_test.go | 2 +- series_number.go | 112 +++++++++++++++++++++++++++++ type_bool.go | 15 ++++ type_float32.go | 160 ++++------------------------------------- type_float32_test.go | 12 ++-- type_float64.go | 155 +++------------------------------------ type_int64.go | 100 ++++++-------------------- type_string.go | 20 +++--- 9 files changed, 190 insertions(+), 388 deletions(-) diff --git a/generic.go b/generic.go index 380be99..54b03c3 100644 --- a/generic.go +++ b/generic.go @@ -314,7 +314,7 @@ func (self *NDFrame) Shift(periods int) Series { }) case []int64: return Shift[int64](&d, periods, func() int64 { - return Nil2Int + return Nil2Int64 }) default: //case []float64: return Shift[float64](&d, periods, func() float64 { diff --git a/series_float64_test.go b/series_float64_test.go index 920ca11..371c9fb 100644 --- a/series_float64_test.go +++ b/series_float64_test.go @@ -20,7 +20,7 @@ func TestNewSeriesFloat64(t *testing.T) { s3 := s1.Repeat(1, 2) fmt.Println(s3.Values()) - s4 := NewSeriesFloat64("x", []float64{1, 2, 3, 4, 5, 6, 7, 8, 9, 10}) + s4 := NewSeriesFloat64("x", []int64{1, 2, 3, 4, 5, 6, 7, 8, 9, 10}) d4 := s4.Rolling(5).Mean() fmt.Printf("d4 = %+v\n", d4.Values()) diff --git a/series_number.go b/series_number.go index f446bd4..7f247d9 100644 --- a/series_number.go +++ b/series_number.go @@ -45,3 +45,115 @@ func Mean[T Number](x []T) float64 { s := avx2.Mean(d) return float64(s) } + +// any转number +func value_to_number[T Number](v any, bool2t func(b bool) T, string2t func(s string, v any) T) T { + switch val := v.(type) { + case int8: + return T(val) + case uint8: + return T(val) + case int16: + return T(val) + case uint16: + return T(val) + case int32: + return T(val) + case uint32: + return T(val) + case int64: + return T(val) + case uint64: + return T(val) + case int: + return T(val) + case uint: + return T(val) + case float32: + return T(val) + case float64: + return T(val) + case bool: + return bool2t(val) + case string: + return string2t(val, v) + } + return T(0) +} + +// 指针转number +func point_to_number[T Number](v any, nil2t T, bool2t func(b bool) T, string2t func(s string, v any) T) T { + switch val := v.(type) { + case *int8: + if val == nil { + return nil2t + } + return T(*val) + case *uint8: + if val == nil { + return nil2t + } + return T(*val) + case *int16: + if val == nil { + return nil2t + } + return T(*val) + case *uint16: + if val == nil { + return nil2t + } + return T(*val) + case *int32: + if val == nil { + return nil2t + } + return T(*val) + case *uint32: + if val == nil { + return nil2t + } + return T(*val) + case *int64: + if val == nil { + return nil2t + } + return T(*val) + case *uint64: + if val == nil { + return nil2t + } + return T(*val) + case *int: + if val == nil { + return nil2t + } + return T(*val) + case *uint: + if val == nil { + return nil2t + } + return T(*val) + case *float32: + if val == nil { + return nil2t + } + return T(*val) + case *float64: + if val == nil { + return nil2t + } + return T(*val) + case *bool: + if val == nil { + return nil2t + } + return bool2t(*val) + case *string: + if val == nil { + return nil2t + } + return string2t(*val, v) + } + return T(0) +} diff --git a/type_bool.go b/type_bool.go index 659ccb9..3eee110 100644 --- a/type_bool.go +++ b/type_bool.go @@ -146,3 +146,18 @@ func boolToFloat32(b bool) float32 { } return False2Float32 } + +// bool转float32 +func boolToFloat64(b bool) float64 { + if b { + return True2Float64 + } + return False2Float64 +} + +func boolToInt64(b bool) int64 { + if b { + return True2Int64 + } + return False2Int64 +} diff --git a/type_float32.go b/type_float32.go index 31691e2..7b7e774 100644 --- a/type_float32.go +++ b/type_float32.go @@ -26,6 +26,17 @@ func ParseFloat32(s string, v any) float32 { logger.Errorf("ParseFloat32 %+v, error=%+v\n", v, err) } }() + + if IsEmpty(s) { + // TODO:NaN是针对64位, 这样直接转换应该有问题, 需要进一步确认 + return Nil2Float32 + } + if isTrue(s) { + return StringTrue2Float32 + } else if isFalse(s) { + return StringFalse2Float32 + } + f, err := strconv.ParseFloat(s, 32) if err == nil { return float32(f) @@ -45,153 +56,10 @@ func float32ToString(v float32) string { return fmt.Sprintf("%f", v) } -func value_to_float32(v any) float32 { - switch val := v.(type) { - case float32: // 直接转换的放在第一位判断 - return val - case int8: - return float32(val) - case uint8: - return float32(val) - case int16: - return float32(val) - case uint16: - return float32(val) - case int32: - return float32(val) - case uint32: - return float32(val) - case int64: - return float32(val) - case uint64: - return float32(val) - case int: - return float32(val) - case uint: - return float32(val) - //case float32: - // return val - case float64: - return float32(val) - case bool: - if val == true { - return True2Float32 - } - return False2Float32 - case string: - if IsEmpty(val) { - // TODO:NaN是针对64位, 这样直接转换应该有问题, 需要进一步确认 - return Nil2Float32 - } - if isTrue(val) { - return StringTrue2Float32 - } else if isFalse(val) { - return StringFalse2Float32 - } - f := ParseFloat32(val, v) - return f - } - return float32(0) -} - -// 指针转float32 -func point_to_float32(v any) float32 { - switch val := v.(type) { - case *float32: - if val == nil { - return Nil2Float32 - } - return float32(*val) - case *int8: - if val == nil { - return Nil2Float32 - } - return float32(*val) - case *uint8: - if val == nil { - return Nil2Float32 - } - return float32(*val) - case *int16: - if val == nil { - return Nil2Float32 - } - return float32(*val) - case *uint16: - if val == nil { - return Nil2Float32 - } - return float32(*val) - case *int32: - if val == nil { - return Nil2Float32 - } - return float32(*val) - case *uint32: - if val == nil { - return Nil2Float32 - } - return float32(*val) - case *int64: - if val == nil { - return Nil2Float32 - } - return float32(*val) - case *uint64: - if val == nil { - return Nil2Float32 - } - return float32(*val) - case *int: - if val == nil { - return Nil2Float32 - } - return float32(*val) - case *uint: - if val == nil { - return Nil2Float32 - } - return float32(*val) - //case *float32: - // if val == nil { - // return Nil2Float32 - // } - // return float32(*val) - case *float64: - if val == nil { - return Nil2Float32 - } - return float32(*val) - case *bool: - if val == nil { - return Nil2Float32 - } - if *val == true { - return True2Float32 - } - return False2Float32 - case *string: - if val == nil { - return Nil2Float32 - } - if IsEmpty(*val) { - // TODO: NaN是针对64位, 这样直接转换应该有问题, 需要进一步确认 - return Nil2Float32 - } - if isTrue(*val) { - return StringTrue2Float32 - } else if isFalse(*val) { - return StringFalse2Float32 - } - f := ParseFloat32(*val, v) - return f - } - return float32(0) -} - func AnyToFloat32(v any) float32 { if isPoint(v) { - return point_to_float32(v) + return point_to_number[float32](v, Nil2Float32, boolToFloat32, ParseFloat32) } - return value_to_float32(v) + f := value_to_number[float32](v, boolToFloat32, ParseFloat32) + return f } diff --git a/type_float32_test.go b/type_float32_test.go index 7dea016..aef3611 100644 --- a/type_float32_test.go +++ b/type_float32_test.go @@ -7,12 +7,12 @@ import ( func Test_point_to_1float32(t *testing.T) { var p1 *int8 - f1 := point_to_float32(p1) + f1 := AnyToFloat32(p1) fmt.Printf("*int8 to float32=%f\n", f1) var v1 int8 = 1 p1 = &v1 - f1 = point_to_float32(p1) + f1 = AnyToFloat32(p1) fmt.Printf("*int8 to float32=%f\n", f1) } @@ -207,8 +207,8 @@ func Test_point_to_float32(t *testing.T) { // t.Errorf("point_to_float32() = %v, want %v", got, tt.want) // } //} - if got := point_to_float32(tt.args.v); got != tt.want && !(IsNaN(float64(tt.want)) && IsNaN(float64(got))) { - t.Errorf("point_to_float32() = %v, want %v", got, tt.want) + if got := AnyToFloat32(tt.args.v); got != tt.want && !(IsNaN(float64(tt.want)) && IsNaN(float64(got))) { + t.Errorf("AnyToFloat32() = %v, want %v", got, tt.want) } }) } @@ -373,8 +373,8 @@ func Test_value_to_float32(t *testing.T) { // t.Errorf("value_to_float32() = %v, want %v", got, tt.want) // } //} - if got := value_to_float32(tt.args.v); !(got == tt.want || (IsNaN(float64(tt.want)) && IsNaN(float64(got)))) { - t.Errorf("value_to_float32() = %v, want %v", got, tt.want) + if got := AnyToFloat32(tt.args.v); !(got == tt.want || (IsNaN(float64(tt.want)) && IsNaN(float64(got)))) { + t.Errorf("AnyToFloat32() = %v, want %v", got, tt.want) } }) } diff --git a/type_float64.go b/type_float64.go index 5d57ecb..bd101c8 100644 --- a/type_float64.go +++ b/type_float64.go @@ -31,6 +31,14 @@ func ParseFloat64(s string, v any) float64 { logger.Errorf("ParseFloat64 %+v, error=%+v\n", v, err) } }() + if IsEmpty(s) { + return Nil2Float64 + } + if isTrue(s) { + return StringTrue2Float64 + } else if isFalse(s) { + return StringFalse2Float64 + } f, err := strconv.ParseFloat(s, 64) if err == nil { return f @@ -50,151 +58,10 @@ func float64ToString(v float64) string { return fmt.Sprintf("%f", v) } -func value_to_float64(v any) float64 { - switch val := v.(type) { - case float64: // 直接转换的放在第一位判断 - return float64(val) - case int8: - return float64(val) - case uint8: - return float64(val) - case int16: - return float64(val) - case uint16: - return float64(val) - case int32: - return float64(val) - case uint32: - return float64(val) - case int64: - return float64(val) - case uint64: - return float64(val) - case int: - return float64(val) - case uint: - return float64(val) - case float32: - return float64(val) - //case float64: - // return float64(val) - case bool: - if val == true { - return True2Float64 - } - return False2Float64 - case string: - if IsEmpty(val) { - return Nil2Float64 - } - if isTrue(val) { - return StringTrue2Float64 - } else if isFalse(val) { - return StringFalse2Float64 - } - f := ParseFloat64(val, v) - return f - } - return float64(0) -} - -// 指针转float64 -func point_to_float64(v any) float64 { - switch val := v.(type) { - case *float64: - if val == nil { - return Nil2Float64 - } - return *val - case *int8: - if val == nil { - return Nil2Float64 - } - return float64(*val) - case *uint8: - if val == nil { - return Nil2Float64 - } - return float64(*val) - case *int16: - if val == nil { - return Nil2Float64 - } - return float64(*val) - case *uint16: - if val == nil { - return Nil2Float64 - } - return float64(*val) - case *int32: - if val == nil { - return Nil2Float64 - } - return float64(*val) - case *uint32: - if val == nil { - return Nil2Float64 - } - return float64(*val) - case *int64: - if val == nil { - return Nil2Float64 - } - return float64(*val) - case *uint64: - if val == nil { - return Nil2Float64 - } - return float64(*val) - case *int: - if val == nil { - return Nil2Float64 - } - return float64(*val) - case *uint: - if val == nil { - return Nil2Float64 - } - return float64(*val) - case *float32: - if val == nil { - return Nil2Float64 - } - return float64(*val) - //case *float64: - // if val == nil { - // return Nil2Float64 - // } - // return float64(*val) - case *bool: - if val == nil { - return Nil2Float64 - } - if *val == true { - return True2Float64 - } - return False2Float64 - case *string: - if val == nil { - return Nil2Float64 - } - if IsEmpty(*val) { - return Nil2Float64 - } - if isTrue(*val) { - return StringTrue2Float64 - } else if isFalse(*val) { - return StringFalse2Float64 - } - f := ParseFloat64(*val, v) - return f - } - return float64(0) -} - func AnyToFloat64(v any) float64 { if isPoint(v) { - return point_to_float64(v) + return point_to_number[float64](v, Nil2Float64, boolToFloat64, ParseFloat64) } - return value_to_float64(v) + f := value_to_number[float64](v, boolToFloat64, ParseFloat64) + return f } diff --git a/type_int64.go b/type_int64.go index c467d05..510fd8f 100644 --- a/type_int64.go +++ b/type_int64.go @@ -7,13 +7,13 @@ import ( ) const ( - Nil2Int = int64(0) // 空指针转int64 - IntNaN = int64(0) // int64 无效值 - True2Int = int64(1) // true转int64 - False2Int = int64(0) // false 转int64 - StringBad2Int = int64(0) // 字符串解析int64异常 - StringTrue2Int = int64(1) // 字符串true转int64 - StringFalse2Int = int64(0) // 字符串false转int64 + Nil2Int64 = int64(0) // 空指针转int64 + IntNaN = int64(0) // int64 无效值 + True2Int64 = int64(1) // true转int64 + False2Int64 = int64(0) // false 转int64 + StringBad2Int64 = int64(0) // 字符串解析int64异常 + StringTrue2Int64 = int64(1) // 字符串true转int64 + StringFalse2Int64 = int64(0) // 字符串false转int64 ) // ParseInt64 解析int字符串, 尝试解析10进制和16进制 @@ -24,6 +24,14 @@ func ParseInt64(s string, v any) int64 { logger.Errorf("ParseInt64 %+v, error=%+v\n", v, err) } }() + if IsEmpty(s) { + return Nil2Int64 + } + if isTrue(s) { + return StringTrue2Int64 + } else if isFalse(s) { + return StringFalse2Int64 + } i, err := strconv.ParseInt(s, 10, 64) if err == nil { return i @@ -35,7 +43,7 @@ func ParseInt64(s string, v any) int64 { } logger.Errorf("%s, error=%+v\n", s, err) if IgnoreParseExceptions { - i = StringBad2Int + i = StringBad2Int64 } else { _ = v.(int64) // Intentionally panic } @@ -51,77 +59,9 @@ func int64ToString(v int64) string { // AnyToInt64 any转换int64 func AnyToInt64(v any) int64 { - switch val := v.(type) { - case nil: - return IntNaN - case *bool: - if val == nil { - return Nil2Int - } - if *val == true { - return True2Int - } - return False2Int - case bool: - if val == true { - return True2Int - } - return False2Int - case *int: - if val == nil { - return Nil2Int - } - return int64(*val) - case int: - return int64(val) - case *int64: - if val == nil { - return Nil2Int - } - return *val - case int64: - return val - case *float32: - if val == nil { - return Nil2Int - } - return int64(*val) - case float32: - return int64(val) - case *float64: - if val == nil { - return Nil2Int - } - return int64(*val) - case float64: - return int64(val) - case *string: - if val == nil { - return Nil2Int - } - if IsEmpty(*val) { - return Nil2Int - } - if isTrue(*val) { - return StringTrue2Int - } else if isFalse(*val) { - return StringFalse2Int - } - i := ParseInt64(*val, v) - return i - case string: - if IsEmpty(val) { - return Nil2Int - } - if isTrue(val) { - return StringTrue2Int - } else if isFalse(val) { - return StringFalse2Int - } - i := ParseInt64(val, v) - return i - default: - i := ParseInt64(fmt.Sprintf("%v", v), v) - return i + if isPoint(v) { + return point_to_number[int64](v, Nil2Int64, boolToInt64, ParseInt64) } + f := value_to_number[int64](v, boolToInt64, ParseInt64) + return f } diff --git a/type_string.go b/type_string.go index 30c8d3d..5309cb9 100644 --- a/type_string.go +++ b/type_string.go @@ -44,37 +44,37 @@ func AnyToString(v any) string { if val == nil { return Nil2String } - return []string{strconv.FormatFloat(*val, 'G', -1, 64)}[0] + return strconv.FormatFloat(*val, 'G', -1, 64) case float64: - return []string{strconv.FormatFloat(val, 'G', -1, 64)}[0] + return strconv.FormatFloat(val, 'G', -1, 64) case *float32: if val == nil { return Nil2String } - return []string{strconv.FormatFloat(float64(*val), 'G', -1, 64)}[0] + return strconv.FormatFloat(float64(*val), 'G', -1, 64) case float32: - return []string{strconv.FormatFloat(float64(val), 'G', -1, 64)}[0] + return strconv.FormatFloat(float64(val), 'G', -1, 64) case *int64: if val == nil { return Nil2String } - return []string{strconv.FormatInt(*val, 10)}[0] + return strconv.FormatInt(*val, 10) case int64: - return []string{strconv.FormatInt(val, 10)}[0] + return strconv.FormatInt(val, 10) case *int: if val == nil { return Nil2String } - return []string{strconv.Itoa(*val)}[0] + return strconv.Itoa(*val) case int: - return []string{strconv.Itoa(val)}[0] + return strconv.Itoa(val) case *int32: if val == nil { return Nil2String } - return []string{strconv.FormatInt(int64(*val), 10)}[0] + return strconv.FormatInt(int64(*val), 10) case int32: - return []string{strconv.FormatInt(int64(val), 10)}[0] + return strconv.FormatInt(int64(val), 10) default: logger.Errorf("%s, error=The type is not recognized\n", v) _ = v.(string) // Intentionally panic -- Gitee From 98155b869d88d68455375f5eed03c3182c9f5e3a Mon Sep 17 00:00:00 2001 From: wangfeng Date: Thu, 2 Feb 2023 16:05:15 +0800 Subject: [PATCH 28/28] =?UTF-8?q?=E5=AE=9E=E7=8E=B0=E4=BA=86=E6=9C=80?= =?UTF-8?q?=E5=A4=A7=E5=80=BC=E7=9A=84=E5=A4=84=E7=90=86=E6=96=B9=E5=BC=8F?= =?UTF-8?q?,=20=E7=9B=AE=E5=89=8D=E6=94=AF=E6=8C=81string,int64=E5=92=8Cfl?= =?UTF-8?q?oat64?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- generic.go | 55 +++++++++++++++++++++++++++++++++++++++++++++++ generic_test.go | 5 +++++ series.go | 2 ++ series_rolling.go | 25 +++++++++++++++++++++ 4 files changed, 87 insertions(+) diff --git a/generic.go b/generic.go index 54b03c3..6224c91 100644 --- a/generic.go +++ b/generic.go @@ -378,3 +378,58 @@ func (self *NDFrame) FillNa(v any, inplace bool) { } } } + +func (self *NDFrame) Max() any { + values := self.Values() + switch rows := values.(type) { + case []string: + max := "" + i := 0 + for idx, iv := range rows { + if StringIsNaN(iv) { + continue + } + if iv > max { + max = iv + i += 1 + } + _ = idx + } + if i > 0 { + return max + } + return StringNaN + case []int64: + max := int64(0) + //i := 0 + for idx, iv := range rows { + if Float64IsNaN(float64(iv)) { + continue + } + if iv > max { + max = iv + //i = idx + } + _ = idx + } + return max + case []float64: + max := float64(0) + i := 0 + for idx, iv := range rows { + if Float64IsNaN(iv) { + continue + } + if iv > max { + max = iv + i += 1 + } + _ = idx + } + if i > 0 { + return max + } + return Nil2Float64 + } + return Nil2Float64 +} diff --git a/generic_test.go b/generic_test.go index 5672e6b..9c702ac 100644 --- a/generic_test.go +++ b/generic_test.go @@ -61,6 +61,8 @@ func TestNDFrameNew(t *testing.T) { fmt.Println(nd1.Records()) nd11 := nd1.Subset(1, 2, true) fmt.Println(nd11.Records()) + fmt.Println(nd1.Max()) + fmt.Println(nd1.Rolling(5).Max()) nd12 := nd1.Rolling(5).Mean() d12 := nd12.Values() @@ -74,6 +76,9 @@ func TestNDFrameNew(t *testing.T) { // string d2 := []string{"1", "2", "3", "4", "5", "6", "7", "8", "9", "10", "nan", "12"} nd2 := NewNDFrame[string]("x", d2...) + fmt.Println(nd2) + nd21 := nd2.Rolling(5).Max() + fmt.Println(nd21) nd2.FillNa(0, true) fmt.Println(nd2) fmt.Println(nd2.Records()) diff --git a/series.go b/series.go index 25baa0c..bca22c3 100644 --- a/series.go +++ b/series.go @@ -58,6 +58,8 @@ type Series interface { StdDev() float64 // FillNa Fill NA/NaN values using the specified method. FillNa(v any, inplace bool) + // Max 找出最大值 + Max() any } // NewSeries 指定类型创建序列 diff --git a/series_rolling.go b/series_rolling.go index 7109c00..cad43ab 100644 --- a/series_rolling.go +++ b/series_rolling.go @@ -41,3 +41,28 @@ func (r RollingWindow) StdDev() (s Series) { return } + +func (r RollingWindow) Max() any { + var fs []float64 + var is []int64 + var ss []string + for _, block := range r.getBlocks() { + //d = append(d, block.Max()) + v := block.Max() + switch val := v.(type) { + case float64: + fs = append(fs, val) + case int64: + is = append(is, val) + case string: + ss = append(ss, val) + } + } + if len(ss) > 0 { + return ss + } else if len(is) > 0 { + return is + } else { + return fs + } +} -- Gitee