// Interpolate returns the index and delta of val in dimension dim.
168732
func (g *LineString) Interpolate(val float64, dim int) (int, float64) {
	n := len(g.flatCoords)
	if n == 0 {
		panic("geom: empty linestring")
	}
	if val <= g.flatCoords[dim] {
		return 0, 0
	}
	if g.flatCoords[n-g.stride+dim] <= val {
		return (n - 1) / g.stride, 0
	}
	low := 0
	high := n / g.stride
	for low < high {
		mid := (low + high) / 2
		if val < g.flatCoords[mid*g.stride+dim] {
			high = mid
		} else {
			low = mid + 1
		}
	}
	low--
	val0 := g.flatCoords[low*g.stride+dim]
	if val == val0 {
		return low, 0
	}
	val1 := g.flatCoords[(low+1)*g.stride+dim]
	return low, (val - val0) / (val1 - val0)
}
1132359
func (p Point) Normalize() Point {
	if p.X == 0 && p.Y == 0 {
		return p
	}
	return p.Mul(1 / p.Norm())
}
743363
func interpolationFuncMap() ast.Function {
	return ast.Function{
		ArgTypes:     []ast.Type{},
		ReturnType:   ast.TypeMap,
		Variadic:     true,
		VariadicType: ast.TypeAny,
		Callback: func(args []interface{}) (interface{}, error) {
			outputMap := make(map[string]ast.Variable)

			if len(args)%2 != 0 {
				return nil, fmt.Errorf("requires an even number of arguments, got %d", len(args))
			}

			var firstType *ast.Type
			for i := 0; i < len(args); i += 2 {
				key, ok := args[i].(string)
				if !ok {
					return nil, fmt.Errorf("argument %d represents a key, so it must be a string", i+1)
				}
				val := args[i+1]
				variable, err := hil.InterfaceToVariable(val)
				if err != nil {
					return nil, err
				}
				// Enforce map type homogeneity
				if firstType == nil {
					firstType = &variable.Type
				} else if variable.Type != *firstType {
					return nil, fmt.Errorf("all map values must have the same type, got %s then %s", firstType.Printable(), variable.Type.Printable())
				}
				// Check for duplicate keys
				if _, ok := outputMap[key]; ok {
					return nil, fmt.Errorf("argument %d is a duplicate key: %q", i+1, key)
				}
				outputMap[key] = variable
			}

			return outputMap, nil
		},
	}
}
967190
func InterpolateService(origSpec *api.ServiceSpec) *api.ServiceSpec {
	spec := origSpec.Copy()

	container := spec.Task.GetContainer()
	defaultContainer := Service.Task.GetContainer()
	if container != nil {
		if container.StopGracePeriod == nil {
			container.StopGracePeriod = &gogotypes.Duration{}
			deepcopy.Copy(container.StopGracePeriod, defaultContainer.StopGracePeriod)
		}
		if container.PullOptions == nil {
			container.PullOptions = defaultContainer.PullOptions.Copy()
		}
		if container.DNSConfig == nil {
			container.DNSConfig = defaultContainer.DNSConfig.Copy()
		}
	}

	if spec.Task.Resources == nil {
		spec.Task.Resources = Service.Task.Resources.Copy()
	}

	if spec.Task.Restart == nil {
		spec.Task.Restart = Service.Task.Restart.Copy()
	} else {
		if spec.Task.Restart.Delay == nil {
			spec.Task.Restart.Delay = &gogotypes.Duration{}
			deepcopy.Copy(spec.Task.Restart.Delay, Service.Task.Restart.Delay)
		}
	}

	if spec.Task.Placement == nil {
		spec.Task.Placement = Service.Task.Placement.Copy()
	}

	if spec.Update == nil {
		spec.Update = Service.Update.Copy()
	} else {
		if spec.Update.Monitor == nil {
			spec.Update.Monitor = &gogotypes.Duration{}
			deepcopy.Copy(spec.Update.Monitor, Service.Update.Monitor)
		}
	}

	if spec.Rollback == nil {
		spec.Rollback = Service.Rollback.Copy()
	} else {
		if spec.Rollback.Monitor == nil {
			spec.Rollback.Monitor = &gogotypes.Duration{}
			deepcopy.Copy(spec.Rollback.Monitor, Service.Rollback.Monitor)
		}
	}

	return spec
}
720679
func Decode(s string, dim int) ([]float64, error) {
	xs, err := DecodeInts(s)
	if err != nil {
		return nil, err
	}
	ys := make([]float64, len(xs))
	for j, i := range xs {
		ys[j] = float64(i) / 1e5
		if j >= dim {
			ys[j] += ys[j-dim]
		}
	}
	return ys, nil
}
803907
func (s *ExpDecaySample) update(t time.Time, v int64) {
	s.mutex.Lock()
	defer s.mutex.Unlock()
	s.count++
	if s.values.Size() == s.reservoirSize {
		s.values.Pop()
	}
	s.values.Push(expDecaySample{
		k: math.Exp(t.Sub(s.t0).Seconds()*s.alpha) / rand.Float64(),
		v: v,
	})
	if t.After(s.t1) {
		values := s.values.Values()
		t0 := s.t0
		s.values.Clear()
		s.t0 = t
		s.t1 = s.t0.Add(rescaleThreshold)
		for _, v := range values {
			v.k = v.k * math.Exp(-s.alpha*s.t0.Sub(t0).Seconds())
			s.values.Push(v)
		}
	}
}
1315774
func InterQuartileRange(input Float64Data) (float64, error) {
	if input.Len() == 0 {
		return math.NaN(), EmptyInputErr
	}
	qs, _ := Quartile(input)
	iqr := qs.Q3 - qs.Q1
	return iqr, nil
}
1187221
func interpolationFuncIndex() ast.Function {
	return ast.Function{
		ArgTypes:   []ast.Type{ast.TypeList, ast.TypeString},
		ReturnType: ast.TypeInt,
		Callback: func(args []interface{}) (interface{}, error) {
			haystack := args[0].([]ast.Variable)
			needle := args[1].(string)
			for index, element := range haystack {
				if needle == element.Value {
					return index, nil
				}
			}
			return nil, fmt.Errorf("Could not find '%s' in '%s'", needle, haystack)
		},
	}
}
1326682
func VarintSize(x int64) int {
	ux := uint64(x) << 1
	if x < 0 {
		ux = ^ux
	}
	return UvarintSize(ux)
}
871564
func Interpolate(config map[string]interface{}, opts Options) (map[string]interface{}, error) {
	if opts.LookupValue == nil {
		opts.LookupValue = os.LookupEnv
	}
	if opts.TypeCastMapping == nil {
		opts.TypeCastMapping = make(map[Path]Cast)
	}
	if opts.Substitute == nil {
		opts.Substitute = template.Substitute
	}

	out := map[string]interface{}{}

	for key, value := range config {
		interpolatedValue, err := recursiveInterpolate(value, NewPath(key), opts)
		if err != nil {
			return out, err
		}
		out[key] = interpolatedValue
	}

	return out, nil
}
256206
func Interpolate(key string, data *interface{}, environmentLookup EnvironmentLookup) error {
	return parseConfig(key, data, func(s string) string {
		values := environmentLookup.Lookup(s, nil)

		if len(values) == 0 {
			if val, ok := defaultValues[s]; ok {
				return val
			}
			logrus.Warnf("The %s variable is not set. Substituting a blank string.", s)
			return ""
		}

		if strings.SplitN(values[0], "=", 2)[1] == "" {
			if val, ok := defaultValues[s]; ok {
				return val
			}
		}

		// Use first result if many are given
		value := values[0]

		// Environment variables come in key=value format
		// Return everything past first '='
		return strings.SplitN(value, "=", 2)[1]
	})
}
165578
func (s *Matrix) Value(dims []int) complex128 {
	return s.list[s.offset(dims)]
}
74012
func interpolationFuncKeys(vs map[string]ast.Variable) ast.Function {
	return ast.Function{
		ArgTypes:   []ast.Type{ast.TypeMap},
		ReturnType: ast.TypeList,
		Callback: func(args []interface{}) (interface{}, error) {
			mapVar := args[0].(map[string]ast.Variable)
			keys := make([]string, 0)

			for k, _ := range mapVar {
				keys = append(keys, k)
			}

			sort.Strings(keys)

			// Keys are guaranteed to be strings
			return stringSliceToVariableValue(keys), nil
		},
	}
}
875910
func (c *ResourceConfig) interpolateForce() {
	if c.raw == nil {
		// If we don't have a lowercase "raw" but we _do_ have the uppercase
		// Raw populated then this indicates that we're recieving a shim
		// ResourceConfig created by NewResourceConfigShimmed, which is already
		// fully evaluated and thus this function doesn't need to do anything.
		if c.Raw != nil {
			return
		}

		var err error
		c.raw, err = config.NewRawConfig(make(map[string]interface{}))
		if err != nil {
			panic(err)
		}
	}

	c.ComputedKeys = c.raw.UnknownKeys()
	c.Raw = c.raw.RawMap()
	c.Config = c.raw.Config()
}
874658
func interpolationFuncPow() ast.Function {
	return ast.Function{
		ArgTypes:   []ast.Type{ast.TypeFloat, ast.TypeFloat},
		ReturnType: ast.TypeFloat,
		Callback: func(args []interface{}) (interface{}, error) {
			return math.Pow(args[0].(float64), args[1].(float64)), nil
		},
	}
}
1323718
func Normalize(v Vect) Vect {
	f := 1.0 / Length(v)
	return Vect{v.X * f, v.Y * f}
}
410489
func interpolationFuncDirname() ast.Function {
	return ast.Function{
		ArgTypes:   []ast.Type{ast.TypeString},
		ReturnType: ast.TypeString,
		Callback: func(args []interface{}) (interface{}, error) {
			return filepath.Dir(args[0].(string)), nil
		},
	}
}