// Alpha controls the smoothing of the data. Alpha = 1 retains no memory
// of past values (e.g. a random walk), while alpha = 0 retains infinite
// memory of past values (e.g. the series mean). Useful values are somewhere
// in between. Defaults to 0.5.
488239
func (m *HoltLinearMovAvgModel) Alpha(alpha float64) *HoltLinearMovAvgModel {
	m.alpha = &alpha
	return m
}
367610
func (s *UniformSample) Values() []int64 {
	s.mutex.Lock()
	defer s.mutex.Unlock()
	values := make([]int64, len(s.values))
	copy(values, s.values)
	return values
}
1086621
func Median(input Float64Data) (median float64, err error) {

	// Start by sorting a copy of the slice
	c := sortedCopy(input)

	// No math is needed if there are no numbers
	// For even numbers we add the two middle numbers
	// and divide by two using the mean function above
	// For odd numbers we just use the middle number
	l := len(c)
	if l == 0 {
		return math.NaN(), EmptyInputErr
	} else if l%2 == 0 {
		median, _ = Mean(c[l/2-1 : l/2+1])
	} else {
		median = c[l/2]
	}

	return median, nil
}
877510
func (e Equalities) DeepDerivative(a1, a2 interface{}) bool {
	if a1 == nil {
		return true
	}
	v1 := reflect.ValueOf(a1)
	v2 := reflect.ValueOf(a2)
	if v1.Type() != v2.Type() {
		return false
	}
	return e.deepValueDerive(v1, v2, make(map[visit]bool), 0)
}
850360
func Bold(arg interface{}) Value {
	if val, ok := arg.(Value); ok {
		return val.Bold()
	}
	return value{value: arg, color: BoldFm}
}
5003
func (term Range) Value() (driver.Value, error) {
	if term.IsZero() {
		return "[,]", nil
	}
	if term.Start.IsZero() {
		return fmt.Sprintf("[,'%s']", term.End), nil
	}
	if term.End.IsZero() {
		return fmt.Sprintf("['%s',]", term.Start), nil
	}
	return fmt.Sprintf("['%s','%s']", term.Start, term.End), nil
}
1048705
func tryParseMetricValue(aggName string, rawVal []interface{}, targetValue *core.MetricValue, fieldIndex int, wasInt map[string]bool) error {
	// the Influx client decodes numeric fields to json.Number (a string), so we have to deal with that --
	// assume, starting off, that values may be either float or int.  Try int until we fail once, and always
	// try float.  At the end, figure out which is which.

	var rv string
	if rvN, ok := rawVal[fieldIndex].(json.Number); !ok {
		return fmt.Errorf("Value %q of metric %q was not a json.Number", rawVal[fieldIndex], aggName)
	} else {
		rv = rvN.String()
	}

	tryInt := false
	isInt, triedBefore := wasInt[aggName]
	tryInt = isInt || !triedBefore

	if tryInt {
		if err := json.Unmarshal([]byte(rv), &targetValue.IntValue); err != nil {
			wasInt[aggName] = false
		} else {
			wasInt[aggName] = true
		}
	}

	if err := json.Unmarshal([]byte(rv), &targetValue.FloatValue); err != nil {
		return err
	}

	return nil
}
850437
func FlatTop(L int) []float64 {
	const (
		alpha0 = float64(0.21557895)
		alpha1 = float64(0.41663158)
		alpha2 = float64(0.277263158)
		alpha3 = float64(0.083578947)
		alpha4 = float64(0.006947368)
	)

	r := make([]float64, L)

	if L == 1 {
		r[0] = 1
		return r
	}

	N := L - 1
	coef := 2 * math.Pi / float64(N)

	for n := 0; n <= N; n++ {
		factor := float64(n) * coef

		term0 := alpha0
		term1 := alpha1 * math.Cos(factor)
		term2 := alpha2 * math.Cos(2*factor)
		term3 := alpha3 * math.Cos(3*factor)
		term4 := alpha4 * math.Cos(4*factor)

		r[n] = term0 - term1 + term2 - term3 + term4
	}

	return r
}
1103491
func Variance(values []int64) float64 {
	if len(values) == 0 {
		return 0.0
	}
	m := Mean(values)
	var sum float64
	for _, v := range values {
		d := float64(v) - m
		sum += d * d
	}
	return float64(sum) / float64(len(values))
}
515322
func Beta(a, b float64) float64 {
	// B(x,y) = Γ(x)Γ(y) / Γ(x+y)
	return math.Exp(lgamma(a) + lgamma(b) - lgamma(a+b))
}
468255
func (vs Values) Normalize() []Value {
	var output []Value
	var total float64

	for _, v := range vs {
		total += v.Value
	}

	for _, v := range vs {
		if v.Value > 0 {
			output = append(output, Value{
				Style: v.Style,
				Label: v.Label,
				Value: util.Math.RoundDown(v.Value/total, 0.0001),
			})
		}
	}
	return output
}
759766
func (s *FloatEncoder) Write(v float64) {
	// Only allow NaN as a sentinel value
	if math.IsNaN(v) && !s.finished {
		s.err = fmt.Errorf("unsupported value: NaN")
		return
	}
	if s.first {
		// first point
		s.val = v
		s.first = false
		s.bw.WriteBits(math.Float64bits(v), 64)
		return
	}

	vDelta := math.Float64bits(v) ^ math.Float64bits(s.val)

	if vDelta == 0 {
		s.bw.WriteBit(bitstream.Zero)
	} else {
		s.bw.WriteBit(bitstream.One)

		leading := uint64(bits.LeadingZeros64(vDelta))
		trailing := uint64(bits.TrailingZeros64(vDelta))

		// Clamp number of leading zeros to avoid overflow when encoding
		leading &= 0x1F
		if leading >= 32 {
			leading = 31
		}

		// TODO(dgryski): check if it's 'cheaper' to reset the leading/trailing bits instead
		if s.leading != ^uint64(0) && leading >= s.leading && trailing >= s.trailing {
			s.bw.WriteBit(bitstream.Zero)
			s.bw.WriteBits(vDelta>>s.trailing, 64-int(s.leading)-int(s.trailing))
		} else {
			s.leading, s.trailing = leading, trailing

			s.bw.WriteBit(bitstream.One)
			s.bw.WriteBits(leading, 5)

			// Note that if leading == trailing == 0, then sigbits == 64.  But that
			// value doesn't actually fit into the 6 bits we have.
			// Luckily, we never need to encode 0 significant bits, since that would
			// put us in the other case (vdelta == 0).  So instead we write out a 0 and
			// adjust it back to 64 on unpacking.
			sigbits := 64 - leading - trailing
			s.bw.WriteBits(sigbits, 6)
			s.bw.WriteBits(vDelta>>trailing, int(sigbits))
		}
	}

	s.val = v
}
724616
func Hamming(L int) []float64 {
	r := make([]float64, L)
	alpha := 0.54
	beta := 1.0 - alpha
	Lf := float64(L)

	for i := 0; i < L; i++ {
		r[i] = alpha - (beta * math.Cos((twoPi*float64(i))/(Lf-1)))
	}
	return r
}
261648
func (v PRVersion) Compare(o PRVersion) int {
	if v.IsNum && !o.IsNum {
		return -1
	} else if !v.IsNum && o.IsNum {
		return 1
	} else if v.IsNum && o.IsNum {
		if v.VersionNum == o.VersionNum {
			return 0
		} else if v.VersionNum > o.VersionNum {
			return 1
		} else {
			return -1
		}
	} else { // both are Alphas
		if v.VersionStr == o.VersionStr {
			return 0
		} else if v.VersionStr > o.VersionStr {
			return 1
		} else {
			return -1
		}
	}
}
1244708
func SampleMax(values []int64) int64 {
	if 0 == len(values) {
		return 0
	}
	var max int64 = math.MinInt64
	for _, v := range values {
		if max < v {
			max = v
		}
	}
	return max
}
148814
func (grng GammaGenerator) Gamma(alpha, beta float64) float64 {
	if !(alpha > 0.0) || !(beta > 0.0) {
		panic(fmt.Sprintf("Invalid parameter alpha %.2f beta %.2f", alpha, beta))
	}

	return grng.gamma(alpha, beta)
}
863139
func Beta(real0, real1 []float64, timePeriod int32) []float64 {
	var outBegIdx C.int
	var outNBElement C.int
	outReal := make([]float64, len(real0))
	C.TA_BETA(0, C.int(len(real0)-1), (*C.double)(unsafe.Pointer(&real0[0])), (*C.double)(unsafe.Pointer(&real1[0])), C.int(timePeriod), &outBegIdx, &outNBElement, (*C.double)(unsafe.Pointer(&outReal[0])))
	return outReal[:outNBElement]
}