// Distance computes Cosine distance.
// It will return distance which represented as 1-cos() (ranged from 0 to 2).
3504
func (c *Cosine) Distance(vectorX *mat.Dense, vectorY *mat.Dense) float64 {
	dotXY := c.Dot(vectorX, vectorY)
	lengthX := math.Sqrt(c.Dot(vectorX, vectorX))
	lengthY := math.Sqrt(c.Dot(vectorY, vectorY))

	cos := dotXY / (lengthX * lengthY)

	return 1 - cos
}
379108
func (p *Point) DistanceFrom(point *Point) float64 {
	d0 := (point[0] - p[0])
	d1 := (point[1] - p[1])
	return math.Sqrt(d0*d0 + d1*d1)
}
1079924
func Distance(point1, point2 geom.Coord) float64 {
	// default to 2D distance if either Z is not set
	if math.IsNaN(point1[2]) || math.IsNaN(point2[2]) {
		return xy.Distance(point1, point2)
	}

	dx := point1[0] - point2[0]
	dy := point1[1] - point2[1]
	dz := point1[2] - point2[2]
	return math.Sqrt(dx*dx + dy*dy + dz*dz)
}
917477
func (cd CentroidDistance) ClusterDistance(c1, c2 *Cluster) float64 {
	return c1.Centroid.DistanceFrom(c2.Centroid)
}
254648
func (ps PointSet) GeoDistanceFrom(point *Point) (float64, int) {
	dist := math.Inf(1)
	index := 0

	for i := range ps {
		if d := ps[i].GeoDistanceFrom(point); d < dist {
			dist = d
			index = i
		}
	}

	return dist, index
}
40924
func Cosine(x, y Vector) (float64, error) {
	d, err := DotProduct(x, y)
	if err != nil {
		return NA, err
	}

	xnorm := Norm(x, 2.0)
	ynorm := Norm(y, 2.0)

	return d / (xnorm * ynorm), nil
}
38368
func (p *PolyKernel) Distance(vectorX *mat.Dense, vectorY *mat.Dense) float64 {
	subVector := new(mat.Dense)
	subVector.Sub(vectorX, vectorY)
	result := p.InnerProduct(subVector, subVector)

	return math.Sqrt(result)
}
724239
func (p *Point) PointDistance(p2 Point) float32 {
	return math.Sqrt(p.PointDistanceSquared(p2))
}
1048553
func cos(a float64) float64 {
	if a < 0.0 {
		a = -a
	}
	if a > sinCosMaxValueForIntModulo {
		return math.Cos(a)
	}
	// index: possibly outside tables range.
	index := int(a*sinCosIndexer + 0.5)
	delta := (a - float64(index)*sinCosDeltaHi) - float64(index)*sinCosDeltaLo
	// Making sure index is within tables range.
	// Last value of each table is the same than first, so we ignore it (tabs size minus one) for modulo.
	index &= (sinCosTabsSize - 2) // index % (SIN_COS_TABS_SIZE-1)
	indexCos := cosTab[index]
	indexSin := sinTab[index]
	return indexCos + delta*(-indexSin+delta*(-indexCos*oneDivF2+delta*(indexSin*oneDivF3+delta*indexCos*oneDivF4)))
}
114467
func positiveDistance(a, b float64) float64 {
	d := b - a
	if d >= 0 {
		return d
	}
	return (b + math.Pi) - (a - math.Pi)
}
824877
func (ll LatLng) Distance(ll2 LatLng) s1.Angle {
	// Haversine formula, as used in C++ S2LatLng::GetDistance.
	lat1, lat2 := ll.Lat.Radians(), ll2.Lat.Radians()
	lng1, lng2 := ll.Lng.Radians(), ll2.Lng.Radians()
	dlat := math.Sin(0.5 * (lat2 - lat1))
	dlng := math.Sin(0.5 * (lng2 - lng1))
	x := dlat*dlat + dlng*dlng*math.Cos(lat1)*math.Cos(lat2)
	return s1.Angle(2*math.Atan2(math.Sqrt(x), math.Sqrt(math.Max(0, 1-x)))) * s1.Radian
}
1157807
func (p *Polyline) Length() s1.Angle {
	var length s1.Angle

	for i := 1; i < len(*p); i++ {
		length += (*p)[i-1].Distance((*p)[i])
	}
	return length
}
72290
func (l *Line) SquaredDistanceFrom(point *Point) float64 {
	x := l.a[0]
	y := l.a[1]
	dx := l.b[0] - x
	dy := l.b[1] - y

	if dx != 0 || dy != 0 {
		t := ((point[0]-x)*dx + (point[1]-y)*dy) / (dx*dx + dy*dy)

		if t > 1 {
			x = l.b[0]
			y = l.b[1]
		} else if t > 0 {
			x += dx * t
			y += dy * t
		}
	}

	dx = point[0] - x
	dy = point[1] - y

	return dx*dx + dy*dy
}
566127
func (c1 Color) DistanceRgb(c2 Color) float64 {
	return math.Sqrt(sq(c1.R-c2.R) + sq(c1.G-c2.G) + sq(c1.B-c2.B))
}
9805
func (g *MultiLineString) Length() float64 {
	return length2(g.flatCoords, 0, g.ends, g.stride)
}
773613
func VectorLength(vector geom.Coord) float64 {
	return math.Sqrt(vector[0]*vector[0] + vector[1]*vector[1] + vector[2]*vector[2])
}
342512
func (ps PointSet) DistanceFrom(point *Point) (float64, int) {
	dist := math.Inf(1)
	index := 0

	for i := range ps {
		if d := ps[i].SquaredDistanceFrom(point); d < dist {
			dist = d
			index = i
		}
	}

	return math.Sqrt(dist), index
}