// DivMod returns x / d = q, and remainder r,
// see https://en.wikipedia.org/wiki/Division_algorithm
778158
func (x Pol) DivMod(d Pol) (Pol, Pol) {
	if x == 0 {
		return 0, 0
	}

	if d == 0 {
		panic("division by zero")
	}

	D := d.Deg()
	diff := x.Deg() - D
	if diff < 0 {
		return 0, x
	}

	var q Pol
	for diff >= 0 {
		m := d << uint(diff)
		q |= (1 << uint(diff))
		x = x.Add(m)

		diff = x.Deg() - D
	}

	return q, x
}
1312037
func (c C) Moddif(y, mod Input) Input {
	var (
		diff    = c.Absdif(y).Modulo(mod)
		modhalf = mod.Mul(C(0.5))
	)
	return modhalf.Add(diff.Absdif(modhalf).Neg())
}
322342
func Remainder(x, y float32) float32 {
	return float32(math.Remainder(float64(x), float64(y)))
}
468651
func Div(real0, real1 []float64) []float64 {
	var outBegIdx C.int
	var outNBElement C.int
	outReal := make([]float64, len(real0))
	C.TA_DIV(0, C.int(len(real0)-1), (*C.double)(unsafe.Pointer(&real0[0])), (*C.double)(unsafe.Pointer(&real1[0])), &outBegIdx, &outNBElement, (*C.double)(unsafe.Pointer(&outReal[0])))
	return outReal[:outNBElement]
}
311617
func (ta *TrackAttributes) MinMode(mode int) *TrackAttributes {
	ta.intAttributes["min_mode"] = mode
	return ta
}
567721
func (ins Inputs) Moddif(y, mod Input) Input {
	im := make([]Input, len(ins))
	for i, in := range ins {
		im[i] = in.Moddif(y, mod)
	}
	return Inputs(im)
}
1082510
func (d *SequenceDiffer) Solve() *DiffSolution {
	s := &DiffSolution{}
	m := modeBeginning

	// right only?
	if len(d.a) == 1 && d.a[0] == "" {
		for _, l := range d.b {
			s.addLineB(l)
		}
		return s
	}

	// left only?
	if len(d.b) == 1 && d.b[0] == "" {
		for _, l := range d.a {
			s.addLineA(l)
		}
		return s
	}

	// copy over shared prefix
	var i int
	for ; i < len(d.a) && i < len(d.b); i++ {
		if !d.isLineEqual(d.a[i], d.b[i]) {
			break
		}
		s.addLine(d.a[i], d.b[i], LineFromBoth)
	}
	if i > 0 {
		m = modeMatch
	}

	// compute optimal
	_ = d.computeOptimal(i, i, m)
	d.getSolution(s, i, i)
	return s
}
924514
func (x Int) Div(y Int) Int {
	// http://python-history.blogspot.com/2010/08/why-pythons-integer-division-floors.html
	var quo, rem big.Int
	quo.QuoRem(x.bigint, y.bigint, &rem)
	if (x.bigint.Sign() < 0) != (y.bigint.Sign() < 0) && rem.Sign() != 0 {
		quo.Sub(&quo, one.bigint)
	}
	return Int{&quo}
}
114999
func invMod(x *big.Int, p *big.Int) (z *big.Int) {
	z = new(big.Int).ModInverse(x, p)
	return z
}
353710
func (s *FieldSort) SortMode(sortMode string) *FieldSort {
	s.sortMode = &sortMode
	return s
}
762623
func VertexAttribDivisor(index uint32, divisor uint32) {
	C.glowVertexAttribDivisor(gpVertexAttribDivisor, (C.GLuint)(index), (C.GLuint)(divisor))
}
243606
func recursiveChmod(path string, mode os.FileMode) error {
	walker := func(p string, fi os.FileInfo, err error) error {
		if _, err := os.Stat(p); err == nil {
			errPerm := os.Chmod(p, mode)
			if errPerm != nil {
				return errors.Trace(errPerm)
			}
		}
		return nil
	}
	if err := filepath.Walk(path, walker); err != nil {
		return errors.Trace(err)
	}
	return nil
}
1058758
func Chmod(filePath string, mode os.FileMode) error {
	stat, err := os.Lstat(filePath)
	if err != nil {
		return err
	}

	if stat.IsDir() {
		files, err := ioutil.ReadDir(filePath)
		if err != nil {
			return err
		}
		for _, file := range files {
			if err := Chmod(filePath+"/"+file.Name(), mode); err != nil {
				return err
			}
		}

		// Set executable rights for folders if read or write is allowed
		execRights := 0
		if mode&0600 != 0 {
			execRights |= 0100
		}
		if mode&0060 != 0 {
			execRights |= 0010
		}
		if mode&0006 != 0 {
			execRights |= 0001
		}

		return os.Chmod(filePath, mode|os.FileMode(execRights))
	}

	if stat.Mode()&os.ModeSymlink != 0 {
		// TODO: os.Chmod fails on symlinks
		return nil
	}

	return os.Chmod(filePath, mode)
}
421853
func (fn *GaussDecayFunction) MultiValueMode(mode string) *GaussDecayFunction {
	fn.multiValueMode = mode
	return fn
}
1271217
func FileModAgeFromInfo(fi os.FileInfo) time.Duration {
	return time.Now().Sub(fi.ModTime())
}
981732
func (x Pol) GCD(f Pol) Pol {
	if f == 0 {
		return x
	}

	if x == 0 {
		return f
	}

	if x.Deg() < f.Deg() {
		x, f = f, x
	}

	return f.GCD(x.Mod(f))
}
598712
func Div(inReal0 []float64, inReal1 []float64) []float64 {
	outReal := make([]float64, len(inReal0))
	for i := 0; i < len(inReal0); i++ {
		outReal[i] = inReal0[i] / inReal1[i]
	}
	return outReal
}