// Minimize determines if the model should be fit to the data using a
// cost minimizing algorithm.
19636
func (a *MovAvgAggregation) Minimize(minimize bool) *MovAvgAggregation {
	a.minimize = &minimize
	return a
}
1239976
func minimum(startNode *node) *node {
	current := startNode
	for current.left != nil {
		current = current.left
	}
	return current
}
370352
func (sf64 *SliceFloat64) Min() (float64, error) {
	if len(sf64.Elements) == 0 {
		return 0, errors.New("List is empty")
	}
	if !sort.Float64sAreSorted(sf64.Elements) {
		sort.Float64s(sf64.Elements)
	}
	return sf64.Elements[0], nil
}
710983
func (s *decorrelateSolver) optimize(p LogicalPlan) (LogicalPlan, error) {
	if apply, ok := p.(*LogicalApply); ok {
		outerPlan := apply.children[0]
		innerPlan := apply.children[1]
		apply.corCols = extractCorColumnsBySchema(apply.children[1], apply.children[0].Schema())
		if len(apply.corCols) == 0 {
			// If the inner plan is non-correlated, the apply will be simplified to join.
			join := &apply.LogicalJoin
			join.self = join
			p = join
		} else if sel, ok := innerPlan.(*LogicalSelection); ok {
			// If the inner plan is a selection, we add this condition to join predicates.
			// Notice that no matter what kind of join is, it's always right.
			newConds := make([]expression.Expression, 0, len(sel.Conditions))
			for _, cond := range sel.Conditions {
				newConds = append(newConds, cond.Decorrelate(outerPlan.Schema()))
			}
			apply.attachOnConds(newConds)
			innerPlan = sel.children[0]
			apply.SetChildren(outerPlan, innerPlan)
			return s.optimize(p)
		} else if m, ok := innerPlan.(*LogicalMaxOneRow); ok {
			if m.children[0].MaxOneRow() {
				innerPlan = m.children[0]
				apply.SetChildren(outerPlan, innerPlan)
				return s.optimize(p)
			}
		} else if proj, ok := innerPlan.(*LogicalProjection); ok {
			for i, expr := range proj.Exprs {
				proj.Exprs[i] = expr.Decorrelate(outerPlan.Schema())
			}
			apply.columnSubstitute(proj.Schema(), proj.Exprs)
			innerPlan = proj.children[0]
			apply.SetChildren(outerPlan, innerPlan)
			if apply.JoinType != SemiJoin && apply.JoinType != LeftOuterSemiJoin && apply.JoinType != AntiSemiJoin && apply.JoinType != AntiLeftOuterSemiJoin {
				proj.SetSchema(apply.Schema())
				proj.Exprs = append(expression.Column2Exprs(outerPlan.Schema().Clone().Columns), proj.Exprs...)
				apply.SetSchema(expression.MergeSchema(outerPlan.Schema(), innerPlan.Schema()))
				np, err := s.optimize(p)
				if err != nil {
					return nil, err
				}
				proj.SetChildren(np)
				return proj, nil
			}
			return s.optimize(p)
		} else if agg, ok := innerPlan.(*LogicalAggregation); ok {
			if apply.canPullUpAgg() && agg.canPullUp() {
				innerPlan = agg.children[0]
				apply.JoinType = LeftOuterJoin
				apply.SetChildren(outerPlan, innerPlan)
				agg.SetSchema(apply.Schema())
				agg.GroupByItems = expression.Column2Exprs(outerPlan.Schema().Keys[0])
				newAggFuncs := make([]*aggregation.AggFuncDesc, 0, apply.Schema().Len())

				outerColsInSchema := make([]*expression.Column, 0, outerPlan.Schema().Len())
				for i, col := range outerPlan.Schema().Columns {
					first := aggregation.NewAggFuncDesc(agg.ctx, ast.AggFuncFirstRow, []expression.Expression{col}, false)
					newAggFuncs = append(newAggFuncs, first)

					outerCol, _ := outerPlan.Schema().Columns[i].Clone().(*expression.Column)
					outerCol.RetType = first.RetTp
					outerColsInSchema = append(outerColsInSchema, outerCol)
				}
				newAggFuncs = append(newAggFuncs, agg.AggFuncs...)
				agg.AggFuncs = newAggFuncs
				apply.SetSchema(expression.MergeSchema(expression.NewSchema(outerColsInSchema...), innerPlan.Schema()))
				np, err := s.optimize(p)
				if err != nil {
					return nil, err
				}
				agg.SetChildren(np)
				// TODO: Add a Projection if any argument of aggregate funcs or group by items are scalar functions.
				// agg.buildProjectionIfNecessary()
				agg.collectGroupByColumns()
				return agg, nil
			}
			// We can pull up the equal conditions below the aggregation as the join key of the apply, if only
			// the equal conditions contain the correlated column of this apply.
			if sel, ok := agg.children[0].(*LogicalSelection); ok && apply.JoinType == LeftOuterJoin {
				var (
					eqCondWithCorCol []*expression.ScalarFunction
					remainedExpr     []expression.Expression
				)
				// Extract the equal condition.
				for _, cond := range sel.Conditions {
					if expr := apply.deCorColFromEqExpr(cond); expr != nil {
						eqCondWithCorCol = append(eqCondWithCorCol, expr.(*expression.ScalarFunction))
					} else {
						remainedExpr = append(remainedExpr, cond)
					}
				}
				if len(eqCondWithCorCol) > 0 {
					originalExpr := sel.Conditions
					sel.Conditions = remainedExpr
					apply.corCols = extractCorColumnsBySchema(apply.children[1], apply.children[0].Schema())
					// There's no other correlated column.
					if len(apply.corCols) == 0 {
						join := &apply.LogicalJoin
						join.EqualConditions = append(join.EqualConditions, eqCondWithCorCol...)
						for _, eqCond := range eqCondWithCorCol {
							clonedCol := eqCond.GetArgs()[1]
							// If the join key is not in the aggregation's schema, add first row function.
							if agg.schema.ColumnIndex(eqCond.GetArgs()[1].(*expression.Column)) == -1 {
								newFunc := aggregation.NewAggFuncDesc(apply.ctx, ast.AggFuncFirstRow, []expression.Expression{clonedCol}, false)
								agg.AggFuncs = append(agg.AggFuncs, newFunc)
								agg.schema.Append(clonedCol.(*expression.Column))
								agg.schema.Columns[agg.schema.Len()-1].RetType = newFunc.RetTp
							}
							// If group by cols don't contain the join key, add it into this.
							if agg.getGbyColIndex(eqCond.GetArgs()[1].(*expression.Column)) == -1 {
								agg.GroupByItems = append(agg.GroupByItems, clonedCol)
							}
						}
						agg.collectGroupByColumns()
						// The selection may be useless, check and remove it.
						if len(sel.Conditions) == 0 {
							agg.SetChildren(sel.children[0])
						}
						defaultValueMap := s.aggDefaultValueMap(agg)
						// We should use it directly, rather than building a projection.
						if len(defaultValueMap) > 0 {
							proj := LogicalProjection{}.Init(agg.ctx)
							proj.SetSchema(apply.schema)
							proj.Exprs = expression.Column2Exprs(apply.schema.Columns)
							for i, val := range defaultValueMap {
								pos := proj.schema.ColumnIndex(agg.schema.Columns[i])
								ifNullFunc := expression.NewFunctionInternal(agg.ctx, ast.Ifnull, types.NewFieldType(mysql.TypeLonglong), agg.schema.Columns[i], val)
								proj.Exprs[pos] = ifNullFunc
							}
							proj.SetChildren(apply)
							p = proj
						}
						return s.optimize(p)
					}
					sel.Conditions = originalExpr
					apply.corCols = extractCorColumnsBySchema(apply.children[1], apply.children[0].Schema())
				}
			}
		}
	}
	newChildren := make([]LogicalPlan, 0, len(p.Children()))
	for _, child := range p.Children() {
		np, err := s.optimize(child)
		if err != nil {
			return nil, err
		}
		newChildren = append(newChildren, np)
	}
	p.SetChildren(newChildren...)
	return p, nil
}
875871
func (t *LLRB) Min() Item {
	h := t.root
	if h == nil {
		return nil
	}
	for h.Left != nil {
		h = h.Left
	}
	return h.Item
}
1374374
func Min(values []int64) int64 {
	if len(values) == 0 {
		return 0
	}
	if isSorted(values) {
		return values[0]
	}
	min := values[0]
	for i := 1; i < len(values); i++ {
		v := values[i]
		if min > v {
			min = v
		}
	}
	return min
}
292952
func (it *Fixed) Optimize() (graph.Iterator, bool) {
	if len(it.values) == 1 && it.values[0] == nil {
		return NewNull(), true
	}

	return it, false
}
16505
func (c C) Min(other Input) Input {
	if v, ok := other.(C); ok {
		return C(minFloat32(float32(c), float32(v)))
	}
	return other.Min(c)
}
137469
func (iter *iterator) optimize() (Iterator, error) {
	if len(iter.cursors) != 1 {
		return iter, nil
	}

	cur := iter.cursors[0]

	if cur.ssIndex == -1 && cur.sc == nil {
		// Optimization to return lowerLevelIter directly.
		return iter.lowerLevelIter, nil
	}

	seg, ok := iter.ss.a[cur.ssIndex].(*segment)
	if !ok || seg == nil {
		return iter, nil
	}

	return &iteratorSingle{
		s:       seg,
		sc:      cur.sc,
		op:      cur.op,
		k:       cur.k,
		v:       cur.v,
		closer:  iter.closer,
		options: iter.ss.options,

		iteratorOptions: iter.iteratorOptions,
	}, nil
}
178868
func (v *view) min(filter *Row, bitDepth uint) (min, count uint64, err error) {
	var minHasValue bool
	for _, f := range v.allFragments() {
		fmin, fcount, err := f.min(filter, bitDepth)
		if err != nil {
			return min, count, err
		}
		// Don't consider a min based on zero columns.
		if fcount == 0 {
			continue
		}

		if !minHasValue {
			min = fmin
			minHasValue = true
			count += fcount
			continue
		}

		if fmin < min {
			min = fmin
			count += fcount
		}
	}
	return min, count, nil
}
942089
func (s Seq) Min() float64 {
	if s.Len() == 0 {
		return 0
	}
	min := s.GetValue(0)
	var value float64
	for i := 1; i < s.Len(); i++ {
		value = s.GetValue(i)
		if value < min {
			min = value
		}
	}
	return min
}
1262807
func (b *BaggedModel) Fit(from base.FixedDataGrid) {
	var wait sync.WaitGroup
	b.selectedAttributes = make(map[int][]base.Attribute)
	for i, m := range b.Models {
		wait.Add(1)
		go func(c base.Classifier, f base.FixedDataGrid, model int) {
			l := b.generateTrainingInstances(model, f)
			c.Fit(l)
			wait.Done()
		}(m, from, i)
	}
	wait.Wait()
	b.fitOn = base.NewStructuralCopy(from)
}
1193491
func (m *MinWise) Push(b []byte) {

	v1 := m.h1(b)
	v2 := m.h2(b)

	for i, v := range m.minimums {
		hv := v1 + uint64(i)*v2
		if hv < v {
			m.minimums[i] = hv
		}
	}
}
129401
func maximization(X *mat.Dense, y mat.Vector, p Params, n_comps int) Params {
	_, n_feats := X.Dims()

	// Initialize the new parameters
	var p_new Params
	p_new.Means = mat.NewDense(n_comps, n_feats, nil)
	p_new.Covs = make([]mat.Symmetric, n_comps)

	//Update the parameters
	for k := 0; k < n_comps; k++ {
		X_yk := where(X, y, k)
		n_obs, _ := X_yk.Dims()
		var covs_k mat.Symmetric

		if n_obs <= 1 {
			p_new.Means.SetRow(k, p.Means.RawRowView(k))
			covs_k = p.Covs[k]
		} else if n_obs < n_feats {
			p_new.Means.SetRow(k, means(X_yk))
			covs_k = shrunkCovariance(X_yk)
		} else {
			p_new.Means.SetRow(k, means(X_yk))
			covs_k = stat.CovarianceMatrix(nil, X_yk, nil)
		}
		reg := mat.NewSymDense(n_feats, symVals(n_feats, 0.000001))
		covs_k_reg := mat.NewSymDense(n_feats, nil)
		covs_k_reg.AddSym(covs_k, reg)
		p_new.Covs[k] = covs_k_reg
	}

	return p_new
}
465354
func (h *Histogram) Min() int {
	var min int
	if len(h.Bins) > 0 {
		min = h.Bins[0]
		for i := 1; i < len(h.Bins); i++ {
			if h.Bins[i] < min {
				min = h.Bins[i]
			}
		}
	}
	return min
}
182836
func (n *Node) Minimum() ([]byte, interface{}, bool) {
	for {
		if n.isLeaf() {
			return n.leaf.key, n.leaf.val, true
		}
		if len(n.edges) > 0 {
			n = n.edges[0].node
		} else {
			break
		}
	}
	return nil, nil, false
}
678506
func (pe *projectionEliminater) optimize(lp LogicalPlan) (LogicalPlan, error) {
	root := pe.eliminate(lp, make(map[string]*expression.Column), false)
	return root, nil
}