// Dot4 returns the 4 element vdot product of two vectors.
974506
func Dot4(a, b *T) float64 {
	return a[0]*b[0] + a[1]*b[1] + a[2]*b[2] + a[3]*b[3]
}
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
}
806657
func (m Mat4) Rows() (row0, row1, row2, row3 Vec4) {
	return m.Row(0), m.Row(1), m.Row(2), m.Row(3)
}
256209
func Dot2d(m1, m2 [][]float32) (float32, error) {
	sum := float32(0.0)
	for i := 0; i < len(m1); i++ {
		partial, err := Dot(m1[i], m2[i])
		if err != nil {
			return 0.0, err
		}
		sum += partial
	}
	return sum, nil
}
489346
func (m1 Mat2x4) Mul4x1(m2 Vec4) Vec2 {
	return Vec2{
		m1[0]*m2[0] + m1[2]*m2[1] + m1[4]*m2[2] + m1[6]*m2[3],
		m1[1]*m2[0] + m1[3]*m2[1] + m1[5]*m2[2] + m1[7]*m2[3],
	}
}
258492
func At4d(x, y, z, w float64) float64 {
	// Noise contributions from the five corners
	var n0, n1, n2, n3, n4 float64

	// Skew the (x,y,z,w) space to determine which cell of 24 simplices we're in
	var s float64 = (x + y + z + w) * F4 // Factor for 4D skewing
	var i int = fastfloor(x + s)
	var j int = fastfloor(y + s)
	var k int = fastfloor(z + s)
	var l int = fastfloor(w + s)

	var t = float64(i+j+k+l) * G4 // Factor for 4D unskewing

	// Unskew the cell origin back to (x,y,z,w) space
	var ox0 = float64(i) - t
	var oy0 = float64(j) - t
	var oz0 = float64(k) - t
	var ow0 = float64(l) - t

	// The x,y,z,w distances from the cell origin
	var x0 float64 = x - ox0
	var y0 float64 = y - oy0
	var z0 float64 = z - oz0
	var w0 float64 = w - ow0

	// For the 4D case, the simplex is a 4D shape I won't even try to describe.
	// To find out which of the 24 possible simplices we're in, we need to
	// determine the magnitude ordering of x0, y0, z0 and w0.
	var rankx, ranky, rankz, rankw int

	// Six pair-wise comparisons are performed between each possible pair
	// of the four coordinates, and the results are used to rank the numbers.
	if x0 > y0 {
		rankx++
	} else {
		ranky++
	}
	if x0 > z0 {
		rankx++
	} else {
		rankz++
	}
	if x0 > w0 {
		rankx++
	} else {
		rankw++
	}
	if y0 > z0 {
		ranky++
	} else {
		rankz++
	}
	if y0 > w0 {
		ranky++
	} else {
		rankw++
	}
	if z0 > w0 {
		rankz++
	} else {
		rankw++
	}

	// The integer offsets for the second, third and fourth simplex corner
	var i1, j1, k1, l1 int
	var i2, j2, k2, l2 int
	var i3, j3, k3, l3 int

	// simplex[c] is a 4-vector with the numbers 0, 1, 2 and 3 in some order.
	// Many values of c will never occur, since e.g. x>y>z>w makes x<z, y<w and x<w
	// impossible. Only the 24 indices which have non-zero entries make any sense.
	// We use a thresholding to set the coordinates in turn from the largest magnitude.

	// Rank 3 denotes the largest coordinate.
	if rankx >= 3 {
		i1 = 1
	} else {
		i1 = 0
	}
	if ranky >= 3 {
		j1 = 1
	} else {
		j1 = 0
	}
	if rankz >= 3 {
		k1 = 1
	} else {
		k1 = 0
	}
	if rankw >= 3 {
		l1 = 1
	} else {
		l1 = 0
	}

	// Rank 2 denotes the second largest coordinate.
	if rankx >= 2 {
		i2 = 1
	} else {
		i2 = 0
	}
	if ranky >= 2 {
		j2 = 1
	} else {
		j2 = 0
	}
	if rankz >= 2 {
		k2 = 1
	} else {
		k2 = 0
	}
	if rankw >= 2 {
		l2 = 1
	} else {
		l2 = 0
	}

	// Rank 1 denotes the second smallest coordinate.
	if rankx >= 1 {
		i3 = 1
	} else {
		i3 = 0
	}
	if ranky >= 1 {
		j3 = 1
	} else {
		j3 = 0
	}
	if rankz >= 1 {
		k3 = 1
	} else {
		k3 = 0
	}
	if rankw >= 1 {
		l3 = 1
	} else {
		l3 = 0
	}

	// The fifth corner has all coordinate offsets = 1, so no need to compute that.

	// Offsets for second corner in (x,y,z,w) coords
	var x1 = x0 - float64(i1) + G4
	var y1 = y0 - float64(j1) + G4
	var z1 = z0 - float64(k1) + G4
	var w1 = w0 - float64(l1) + G4

	// Offsets for third corner in (x,y,z,w) coords
	var x2 = x0 - float64(i2) + 2.0*G4
	var y2 = y0 - float64(j2) + 2.0*G4
	var z2 = z0 - float64(k2) + 2.0*G4
	var w2 = w0 - float64(l2) + 2.0*G4

	// Offsets for fourth corner in (x,y,z,w) coords
	var x3 = x0 - float64(i3) + 3.0*G4
	var y3 = y0 - float64(j3) + 3.0*G4
	var z3 = z0 - float64(k3) + 3.0*G4
	var w3 = w0 - float64(l3) + 3.0*G4

	// Offsets for last corner in (x,y,z,w) coords
	var x4 = x0 - 1.0 + 4.0*G4
	var y4 = y0 - 1.0 + 4.0*G4
	var z4 = z0 - 1.0 + 4.0*G4
	var w4 = w0 - 1.0 + 4.0*G4

	// Work out the hashed gradient indices of the five simplex corners
	var ii int = i & 0xff
	var jj int = j & 0xff
	var kk int = k & 0xff
	var ll int = l & 0xff
	var gi0 int = perm[ii+perm[jj+perm[kk+perm[ll]]]] % 32
	var gi1 int = perm[ii+i1+perm[jj+j1+perm[kk+k1+perm[ll+l1]]]] % 32
	var gi2 int = perm[ii+i2+perm[jj+j2+perm[kk+k2+perm[ll+l2]]]] % 32
	var gi3 int = perm[ii+i3+perm[jj+j3+perm[kk+k3+perm[ll+l3]]]] % 32
	var gi4 int = perm[ii+1+perm[jj+1+perm[kk+1+perm[ll+1]]]] % 32

	// Calculate the contribution from the five corners
	var t0 float64 = 0.5 - x0*x0 - y0*y0 - z0*z0 - w0*w0
	if t0 < 0 {
		n0 = 0.0
	} else {
		t0 *= t0
		n0 = t0 * t0 * dot4(grad4[gi0], x0, y0, z0, w0)
	}

	var t1 float64 = 0.5 - x1*x1 - y1*y1 - z1*z1 - w1*w1
	if t1 < 0 {
		n1 = 0.0
	} else {
		t1 *= t1
		n1 = t1 * t1 * dot4(grad4[gi1], x1, y1, z1, w1)
	}

	var t2 float64 = 0.5 - x2*x2 - y2*y2 - z2*z2 - w2*w2
	if t2 < 0 {
		n2 = 0.0
	} else {
		t2 *= t2
		n2 = t2 * t2 * dot4(grad4[gi2], x2, y2, z2, w2)
	}

	var t3 float64 = 0.5 - x3*x3 - y3*y3 - z3*z3 - w3*w3
	if t3 < 0 {
		n3 = 0.0
	} else {
		t3 *= t3
		n3 = t3 * t3 * dot4(grad4[gi3], x3, y3, z3, w3)
	}

	var t4 float64 = 0.5 - x4*x4 - y4*y4 - z4*z4 - w4*w4
	if t4 < 0 {
		n4 = 0.0
	} else {
		t4 *= t4
		n4 = t4 * t4 * dot4(grad4[gi4], x4, y4, z4, w4)
	}

	// Sum and scale to [-1,1]
	return 27.0 * (n0 + n1 + n2 + n3 + n4)
}
535594
func (e Edge) DOTAttributes() []dot.Attribute {
	return []dot.Attribute{{Key: "label", Value: fmt.Sprintf("%q", strings.Join(e.Kinds().List(), ","))}}
}
400072
func V4() (UUID, error) {
	u := UUID{}

	_, err := rand.Read(u[:])
	if err != nil {
		return u, err
	}

	u[8] = (u[8] | 0x80) & 0xBF
	u[6] = (u[6] | 0x40) & 0x4F

	return u, nil
}
51334
func (vn *VecN) Len() float32 {
	if vn == nil {
		return float32(math.NaN())
	}
	if len(vn.vec) == 0 {
		return 0
	}

	return float32(math.Sqrt(float64(vn.Dot(vn))))
}
684838
func GraphDot(g *Graph, opts *dag.DotOpts) (string, error) {
	return string(g.Dot(opts)), nil
}
364560
func (v Vec2) Vec4(z, w float32) Vec4 {
	return Vec4{v[0], v[1], z, w}
}
725844
func (p *Point) Dot(v *Point) float64 {
	return p[0]*v[0] + p[1]*v[1]
}
460879
func (v Vector) DotProduct(v2 Vector) (result float64, err error) {
	if len(v) != len(v2) {
		err = ErrDimensionMismatch
		return
	}

	for i := 0; i < len(v); i++ {
		result = result + (v[i] * v2[i])
	}
	return
}
1274848
func (q1 Quat) Dot(q2 Quat) float32 {
	return q1.W*q2.W + q1.V[0]*q2.V[0] + q1.V[1]*q2.V[1] + q1.V[2]*q2.V[2]
}
433146
func WriteDot(w io.Writer, nodes []Node) error {
	w.Write([]byte("digraph {\n"))
	defer w.Write([]byte("}\n"))

	for _, n := range nodes {
		nodeLine := fmt.Sprintf("\t\"%s\";\n", n)

		w.Write([]byte(nodeLine))

		for _, edge := range n.Edges() {
			target := edge.Tail()
			line := fmt.Sprintf("\t\"%s\" -> \"%s\" [label=\"%s\"];\n",
				n, target, edge)
			w.Write([]byte(line))
		}
	}

	return nil
}
697527
func (vn *VecN) Dot(other *VecN) float32 {
	if vn == nil || other == nil || len(vn.vec) != len(other.vec) {
		return float32(math.NaN())
	}

	var result float32 = 0.0
	for i, el := range vn.vec {
		result += el * other.vec[i]
	}

	return result
}
189041
func Join(vectors ...Vector) Vector {
	// figure out how big to make the resulting vector so we can
	// allocate efficiently
	n := 0
	for _, vector := range vectors {
		n += vector.Len()
	}

	i := 0
	v := make(Vector, n)
	for _, vector := range vectors {
		for _, value := range vector {
			v[i] = value
			i++
		}
	}

	return v
}