func main() { floats := []float64{1.4, 3.14, 9.81, 13.2, 23.42, 33.7, 44.11, 51} array := stack.Lifo() array.Push(fmt.Sprintf("Lorem %v", ansi.Color("Ipsum", ansi.Blue))) array.Push(fmt.Sprintf("Dolor %v", ansi.Bold("sit Amet"))) array.Push(fmt.Sprintf("5th Prime: %v", xmath.Prime(5))) array.Push(fmt.Sprintf("\n\tMin: %v\n\tMax: %v\n\tMedian: %v\n\tArithmetic: %v\n\tHarmonic: %v\n\tGeometric: %v", xmath.Min(floats), xmath.Max(floats), xmath.Median(floats), xmath.Arithmetic(floats), xmath.Harmonic(floats), xmath.Geometric(floats))) array.Push(fmt.Sprintf("Date: %v", as.Time("11.01.2015"))) for array.Len() > 0 { log.Println(array.Pop()) } array.Push(string(as.Bytes(regex.ReplaceAllString("foobar", "o+", "u")))) array.Push(as.Int(23.0000)) array.Push(as.Float(13.37)) array.Push(as.String(23.0)) array.Push(as.Bool(111111)) array.Push(as.Bytes(12.34)) array.Push(as.String(strconv.ParseInt("42", 10, 0))) array.Push(as.FloatFromXString("2,3")) array.Push(as.FloatFromXString(".23")) array.Push(as.String("\r\n\t\n")) for array.Len() > 0 { log.Println(array.Pop()) } }
// Geometric returns the geometric mean from a slice of Values as float64. // The geometric mean is a type of mean or average, which indicates the central // tendency or typical value of a set of numbers by using the product of their // values (as opposed to the arithmetic mean which uses their sum). The // geometric mean is defined as the nth root of the product of n numbers. // It uses "as" (simonwaldherr.de/go/golibs/as) to // convert given values to floats. func Geometric(val interface{}) float64 { slice := reflect.ValueOf(val) c := slice.Len() var m float64 = 1 for i := 0; i < c; i++ { m = m * as.Float(slice.Index(i).Interface()) } return float64(math.Pow(float64(m), 1/float64(c))) }
// Harmonic returns the harmonic mean from a slice of Values as float64. // The arithmetic mean or simply the mean or average when the context is clear, // is the sum of a list of numbers divided by the number of numbers // in the list. // It uses "as" (simonwaldherr.de/go/golibs/as) to // convert given values to floats. func Harmonic(val interface{}) float64 { slice := reflect.ValueOf(val) c := slice.Len() s := float64(0) for i := 0; i < c; i++ { s = s + 1/as.Float(slice.Index(i).Interface()) } return (float64(c) * 1 / s) }
// Arithmetic returns the arithmetic mean from a slice of Values as float64. // The arithmetic mean or simply the mean or average when the context is clear, // is the sum of a list of numbers divided by the number of numbers // in the list. // It uses "as" (simonwaldherr.de/go/golibs/as) to // convert given values to floats. func Arithmetic(val interface{}) float64 { slice := reflect.ValueOf(val) c := slice.Len() out := make([]float64, c) for i := 0; i < c; i++ { out[i] = as.Float(slice.Index(i).Interface()) } return (Sum(out) / float64(len(out))) }
// Rootmeansquare returns the root mean square from a slice of Values as float64. // The root mean square is the root value of the sum of the squared value of a // list of numbers divided by the number of numbers in the list. // It uses "as" (simonwaldherr.de/go/golibs/as) to // convert given values to floats. func Rootmeansquare(val interface{}) float64 { slice := reflect.ValueOf(val) c := slice.Len() out := make([]float64, c) for i := 0; i < c; i++ { out[i] = math.Pow(as.Float(slice.Index(i).Interface()), 2) } return math.Sqrt(Sum(out) / float64(len(out))) }
// Sum returns the sum from a slice of Values as float64. // It uses "as" (simonwaldherr.de/go/golibs/as) to // convert given values to floats. func Sum(val interface{}) float64 { slice := reflect.ValueOf(val) c := slice.Len() out := make([]float64, c) for i := 0; i < c; i++ { out[i] = as.Float(slice.Index(i).Interface()) } var sum float64 for _, value := range out { sum = sum + value } return sum }
// Median returns the median from a slice of Values as float64. // The median is the numerical value separating the higher half // of a data sample from the lower half. The median of a list of // numbers can be found by arranging all the observations from // lowest value to highest value and picking the middle one. // It uses "as" (simonwaldherr.de/go/golibs/as) to // convert given values to floats. func Median(val interface{}) float64 { slice := reflect.ValueOf(val) c := slice.Len() out := make([]float64, c) for i := 0; i < c; i++ { out[i] = as.Float(slice.Index(i).Interface()) } sort.Float64s(out) if c%2 == 1 { return out[c/2] } return (out[c/2] + out[c/2]) / 2 }
// Max returns the greatest number from a slice of Values as float64. // It uses "as" (simonwaldherr.de/go/golibs/as) to // convert given values to floats. func Max(val interface{}) float64 { slice := reflect.ValueOf(val) c := slice.Len() out := make([]float64, c) for i := 0; i < c; i++ { out[i] = as.Float(slice.Index(i).Interface()) } max := out[0] for _, value := range out { if value > max { max = value } } return max }