/* Math Transform Functions */
// Acos - Vector Trigonometric ACOS
6684
func Acos(inReal []float64) []float64 {
	outReal := make([]float64, len(inReal))
	for i := 0; i < len(inReal); i++ {
		outReal[i] = math.Acos(inReal[i])
	}
	return outReal
}
406437
func (pca *PCA) Transform(X *mat.Dense) *mat.Dense {
	if pca.svd == nil {
		panic("You should to fit PCA model first")
	}

	num_samples, num_features := X.Dims()

	vTemp := new(mat.Dense)
	pca.svd.VTo(vTemp)
	//Compute to full data
	if pca.Num_components == 0 || pca.Num_components > num_features {
		return compute(X, vTemp)
	}

	X = compute(X, vTemp)
	result := mat.NewDense(num_samples, pca.Num_components, nil)
	result.Copy(X)
	return result
}
1166613
func (r Rect) Norm() Rect {
	return Rect{
		Min: Vec{
			math.Min(r.Min.X, r.Max.X),
			math.Min(r.Min.Y, r.Max.Y),
		},
		Max: Vec{
			math.Max(r.Min.X, r.Max.X),
			math.Max(r.Min.Y, r.Max.Y),
		},
	}
}
350024
func (as ArraySpec) Sin() Input {
	return as.proc(func(i Input) Input {
		return i.Sin()
	})
}
252081
func WriteAsQuads(w quad.Writer, o interface{}) (quad.Value, error) {
	return global.WriteAsQuads(w, o)
}
726982
func Product(x, y Vector) (Vector, error) {
	if len(x) != len(y) {
		return nil, fmt.Errorf("x and y have unequal lengths: %d / %d", len(x), len(y))
	}

	p := make(Vector, len(x))
	for i, _ := range x {
		p[i] = x[i] * y[i]
	}
	return p, nil
}
1277399
func QuatRotate(angle float32, axis Vec3) Quat {
	// angle = (float32(math.Pi) * angle) / 180.0

	c, s := float32(math.Cos(float64(angle/2))), float32(math.Sin(float64(angle/2)))

	return Quat{c, axis.Mul(s)}
}
107430
func Cos(z, x *decimal.Big) *decimal.Big {
	if z.CheckNaNs(x, nil) {
		return z
	}

	if x.IsInf(0) {
		z.Context.Conditions |= decimal.InvalidOperation
		return z.SetNaN(false)
	}

	ctx := decimal.Context{Precision: precision(z) + defaultExtraPrecision}

	negXSq, halved, ok := prepCosine(z, x, ctx)
	if !ok {
		z.Context.Conditions |= decimal.InvalidOperation
		return z.SetNaN(false)
	}

	ctx.Precision += halved
	z.Copy(BinarySplitDynamic(ctx,
		func(_ uint64) *decimal.Big { return one },
		getCosineP(negXSq),
		func(_ uint64) *decimal.Big { return one },
		getCosineQ(ctx),
	))

	// now undo the half angle bit
	for i := 0; i < halved; i++ {
		ctx.Mul(z, z, z)
		ctx.Mul(z, z, two)
		ctx.Sub(z, z, one)
	}
	ctx.Precision -= defaultExtraPrecision + halved
	return ctx.Round(z)
}
171362
func Sin(z, x *decimal.Big) *decimal.Big {
	if z.CheckNaNs(x, nil) {
		return z
	}
	if x.IsInf(0) {
		z.Context.Conditions |= decimal.InvalidOperation
		return z.SetNaN(false)
	}

	// Sin(x) = Cos(pi/2 - x)
	ctx := decimal.Context{Precision: precision(z) + defaultExtraPrecision}
	Cos(z, ctx.Sub(z, pi2(alias(z, x), ctx), x))
	ctx.Precision -= defaultExtraPrecision
	return ctx.Round(z)
}
642371
func (mat *T) TransformVec3(v *vec3.T) {
	// Use intermediate variables to not alter further computations.
	x := mat[0][0]*v[0] + mat[1][0]*v[1] + mat[2][0]*v[2]
	y := mat[0][1]*v[0] + mat[1][1]*v[1] + mat[2][1]*v[2]
	v[2] = mat[0][2]*v[0] + mat[1][2]*v[1] + mat[2][2]*v[2]
	v[0] = x
	v[1] = y
}
1106197
function unit_vector_angle(ux, uy, vx, vy) {
  var sign = (ux * vy - uy * vx < 0) ? -1 : 1;
  var dot  = ux * vx + uy * vy;

  // Add this to work with arbitrary vectors:
  // dot /= Math.sqrt(ux * ux + uy * uy) * Math.sqrt(vx * vx + vy * vy);

  // rounding errors, e.g. -1.0000000000000002 can screw up this
  if (dot >  1.0) { dot =  1.0; }
  if (dot < -1.0) { dot = -1.0; }

  return sign * Math.acos(dot);
}
514702
func (a Affine) Rotate(angle float64) Affine {
	s, c := math.Sincos(angle)
	return a.Mul(Affine{
		+c, +s, +0,
		-s, +c, +0,
		+0, +0, +1,
	})
}
654988
func Sincos(x float32) (sin, cos float32) {
	sin64, cos64 := math.Sincos(float64(x))
	sin = float32(sin64)
	cos = float32(cos64)
	return
}
113620
func (m Matrix) Rotated(around Vec, angle float64) Matrix {
	sint, cost := math.Sincos(angle)
	m[4], m[5] = m[4]-around.X, m[5]-around.Y
	m = m.Chained(Matrix{cost, sint, -sint, cost, 0, 0})
	m[4], m[5] = m[4]+around.X, m[5]+around.Y
	return m
}
888501
func ConvexHull(geometry geom.T) geom.T {
	// copy coords because the algorithm reorders them
	calc := convexHullCalculator{
		layout:   geometry.Layout(),
		stride:   geometry.Layout().Stride(),
		inputPts: geometry.FlatCoords(),
	}

	return calc.getConvexHull()
}
284364
func Cor(x, y Vector) (float64, error) {
	n := float64(len(x))
	xy, err := Product(x, y)
	if err != nil {
		return NA, err
	}

	sx := x.Sd()
	sy := y.Sd()

	mx := x.Mean()
	my := y.Mean()

	r := (xy.Sum() - n*mx*my) / ((n - 1) * sx * sy)
	return r, nil
}
605256
func CartesianToCylindical(coord Vec3) (rho, phi, z float32) {
	rho = float32(math.Hypot(float64(coord[0]), float64(coord[1])))

	phi = float32(math.Atan2(float64(coord[1]), float64(coord[0])))

	z = coord[2]

	return
}