// Normalize normalizes the source profile by multiplying each value in profile by the
// ratio of the sum of the base profile's values of that sample type to the sum of the
// source profile's value of that sample type.
488699
func (p *Profile) Normalize(pb *Profile) error {

	if err := p.compatible(pb); err != nil {
		return err
	}

	baseVals := make([]int64, len(p.SampleType))
	for _, s := range pb.Sample {
		for i, v := range s.Value {
			baseVals[i] += v
		}
	}

	srcVals := make([]int64, len(p.SampleType))
	for _, s := range p.Sample {
		for i, v := range s.Value {
			srcVals[i] += v
		}
	}

	normScale := make([]float64, len(baseVals))
	for i := range baseVals {
		if srcVals[i] == 0 {
			normScale[i] = 0.0
		} else {
			normScale[i] = float64(baseVals[i]) / float64(srcVals[i])
		}
	}
	p.ScaleN(normScale)
	return nil
}
1211786
func (i *Parameters_Job_Inputs) Normalize() (err error) {
	if i.Cipd == nil && len(i.Isolated) == 0 {
		return errors.New(
			"job.inputs: at least one of packages and isolated must be specified")
	}
	if i.Cipd != nil {
		if err = i.Cipd.Normalize(); err != nil {
			return
		}
	}
	return
}
225615
func Normalize(version string) string {

	// ignore aliases and just assume the alias is required instead of the source
	result := RegFind(`^([^,\s]+) +as +([^,\s]+)$`, version)
	if result != nil {
		version = result[1]
	}

	// match master-like branches
	if regexpMasterLikeBranches.MatchString(strings.ToLower(version)) {
		return "9999999-dev"
	}

	if strings.HasPrefix(strings.ToLower(version), "dev-") {
		return "dev-" + version[4:len(version)]
	}

	index := 0

	// match classical versioning
	result = RegFind(`(?i)^v?(\d{1,3})(\.\d+)?(\.\d+)?(\.\d+)?`+modifierRegex+`$`, version)
	if result != nil {
		version = ""
		for _, val := range result[1:5] {
			if val != "" {
				version = version + val
			} else {
				version = version + ".0"
			}
		}

		index = 5
	} else {
		// match date-based versioning
		result = RegFind(`(?i)^v?(\d{4}(?:[.:-]?\d{2}){1,6}(?:[.:-]?\d{1,3})?)`+modifierRegex+`$`, version)
		if result != nil {
			version = regexp.MustCompile(`\D`).ReplaceAllString(result[1], "-")
			index = 2
		}
	}

	if index != 0 {
		if result[index] != "" {
			if result[index] == "stable" {
				return version
			}

			version = version + "-" + expandStability(result[index])
			if result[index+1] != "" {
				version = version + result[index+1]
			}
		}

		if result[index+2] != "" {
			version = version + "-dev"
		}

		return version
	}

	result = RegFind(`(?i)(.*?)[.-]?dev$`, version)
	if result != nil {
		return normalizeBranch(result[1])
	}

	return version
}
366549
func (a *Execution_Auth) Normalize() error {
	if a == nil {
		return errors.New("Execution_Auth is nil")
	}
	if a.Id == nil {
		return errors.New("Execution_Auth.Id is nil")
	}
	return nil
}
870658
func (s Seq) Normalize() Seq {
	min, max := s.MinMax()

	delta := max - min
	output := make([]float64, s.Len())
	for i := 0; i < s.Len(); i++ {
		output[i] = (s.GetValue(i) - min) / delta
	}

	return Seq{Provider: Array(output)}
}
1333938
func normalize(i interface{}) (int, error) {
	switch v := i.(type) {
	case int:
		return v, nil
	case int32:
		return int(v), nil
	case int64:
		return int(v), nil
	case float32:
		return int(v + 0.5), nil
	case float64:
		// We need this check here because NaN can be a float. Casting NaN to
		// int results in a large negative number. Even after you add 0.5.
		if math.IsNaN(v) {
			return -1, fmt.Errorf("Could not normalize NaN %v", v)
		}
		return int(v + 0.5), nil
	case string:
		f, err := strconv.ParseFloat(v, 64)
		if err != nil {
			return -1, err
		}
		return normalize(f)
	default:
		return -1, fmt.Errorf("Could not normalize %q", v)
	}
}
316473
func normalize(key string) string {
	if key == "" {
		return ""
	}
	path := strings.Split(strings.Trim(key, "/"), "/")
	return "/" + strings.Join(path, "/")
}
681485
func (n *normalizer) Normalize(un *unstructured.Unstructured) error {
	matched := make([]normalizerPatch, 0)
	for _, patch := range n.patches {
		groupKind := un.GroupVersionKind().GroupKind()
		if groupKind == patch.groupKind &&
			(patch.name == "" || patch.name == un.GetName()) &&
			(patch.namespace == "" || patch.namespace == un.GetNamespace()) {

			matched = append(matched, patch)
		}
	}
	if len(matched) == 0 {
		return nil
	}

	docData, err := json.Marshal(un)
	if err != nil {
		return err
	}

	for _, patch := range matched {
		patchedData, err := patch.patch.Apply(docData)
		if err != nil {
			// bug?
			if strings.HasPrefix(err.Error(), "Unable to remove nonexistent key") {
				continue
			}
			return err
		}
		docData = patchedData
	}

	err = json.Unmarshal(docData, un)
	if err != nil {
		return err
	}
	return nil
}
1369015
func (z Zones) Normalize() {
	for i := range z {
		z[i] = Name(z[i]).Normalize()
	}
}
781399
func normalize(v reflect.Value) interface{} {
	k := v.Kind()

	switch {
	case isNumber(k):
		f, err := numberToFloat(v)
		if err == nil {
			return f
		}
	}

	return v.Interface()
}
801791
func (arc *AddressResourceConfig) Normalize() {
	testProto := strings.Trim(strings.ToLower(arc.Protocol), " ")
	arc.Protocol = testProto
}
582219
func (workflow *WorkflowModel) Normalize() error {
	for _, env := range workflow.Environments {
		if err := env.Normalize(); err != nil {
			return err
		}
	}

	for _, stepListItem := range workflow.Steps {
		stepID, step, err := GetStepIDStepDataPair(stepListItem)
		if err != nil {
			return err
		}
		if err := step.Normalize(); err != nil {
			return err
		}
		stepListItem[stepID] = step
	}

	return nil
}
1260704
func (quat *T) Normalize() *T {
	norm := quat.Norm()
	if norm != 1 && norm != 0 {
		ool := 1 / math.Sqrt(norm)
		quat[0] *= ool
		quat[1] *= ool
		quat[2] *= ool
		quat[3] *= ool
	}
	return quat
}
949348
func Normalize(angle float64) float64 {
	for angle > math.Pi {
		angle -= piTimes2
	}
	for angle <= -math.Pi {
		angle += piTimes2
	}
	return angle
}
986986
func (vn *VecN) Normalize(dst *VecN) *VecN {
	if vn == nil {
		return nil
	}

	return vn.Mul(dst, 1/vn.Len())
}
924507
func (d *Dependency) Normalize() error {
	if d.Shards == 0 {
		d.Shards = 1
	}
	if err := d.GetAttempts().Normalize(); err != nil {
		return err
	}
	return d.Phrase.Normalize()
}
1293711
func (k *Kernel) Normalized() Matrix {
	sum := k.Absum()
	w := k.Width
	h := k.Height
	nk := NewKernel(w, h)

	// avoid division by 0
	if sum == 0 {
		sum = 1
	}

	for i := 0; i < w*h; i++ {
		nk.Matrix[i] = k.Matrix[i] / sum
	}

	return nk
}