Beispiel #1
0
// Ddot computes the dot product of the two vectors
//  \sum_i x[i]*y[i]
func (Implementation) Ddot(n int, x []float64, incX int, y []float64, incY int) float64 {
	if n < 0 {
		panic(negativeN)
	}
	if incX == 0 {
		panic(zeroIncX)
	}
	if incY == 0 {
		panic(zeroIncY)
	}
	if incX == 1 && incY == 1 {
		if len(x) < n {
			panic(badLenX)
		}
		if len(y) < n {
			panic(badLenY)
		}
		return asm.DdotUnitary(x[:n], y)
	}
	var ix, iy int
	if incX < 0 {
		ix = (-n + 1) * incX
	}
	if incY < 0 {
		iy = (-n + 1) * incY
	}
	if ix >= len(x) || ix+(n-1)*incX >= len(x) {
		panic(badLenX)
	}
	if iy >= len(y) || iy+(n-1)*incY >= len(y) {
		panic(badLenY)
	}
	return asm.DdotInc(x, y, uintptr(n), uintptr(incX), uintptr(incY), uintptr(ix), uintptr(iy))
}
Beispiel #2
0
// Inner computes the generalized inner product
//   x^T A y
// between vectors x and y with matrix A. This is only a true inner product if
// A is symmetric positive definite, though the operation works for any matrix A.
//
// Inner panics if x.Len != m or y.Len != n when A is an m x n matrix.
func Inner(x *Vector, A Matrix, y *Vector) float64 {
	m, n := A.Dims()
	if x.Len() != m {
		panic(ErrShape)
	}
	if y.Len() != n {
		panic(ErrShape)
	}
	if m == 0 || n == 0 {
		return 0
	}

	var sum float64

	switch b := A.(type) {
	case RawSymmetricer:
		bmat := b.RawSymmetric()
		if bmat.Uplo != blas.Upper {
			// Panic as a string not a mat64.Error.
			panic(badSymTriangle)
		}
		for i := 0; i < x.Len(); i++ {
			xi := x.at(i)
			if xi != 0 {
				if y.mat.Inc == 1 {
					sum += xi * asm.DdotUnitary(
						bmat.Data[i*bmat.Stride+i:i*bmat.Stride+n],
						y.mat.Data[i:],
					)
				} else {
					sum += xi * asm.DdotInc(
						bmat.Data[i*bmat.Stride+i:i*bmat.Stride+n],
						y.mat.Data[i*y.mat.Inc:], uintptr(n-i),
						1, uintptr(y.mat.Inc),
						0, 0,
					)
				}
			}
			yi := y.at(i)
			if i != n-1 && yi != 0 {
				if x.mat.Inc == 1 {
					sum += yi * asm.DdotUnitary(
						bmat.Data[i*bmat.Stride+i+1:i*bmat.Stride+n],
						x.mat.Data[i+1:],
					)
				} else {
					sum += yi * asm.DdotInc(
						bmat.Data[i*bmat.Stride+i+1:i*bmat.Stride+n],
						x.mat.Data[(i+1)*x.mat.Inc:], uintptr(n-i-1),
						1, uintptr(x.mat.Inc),
						0, 0,
					)
				}
			}
		}
	case RawMatrixer:
		bmat := b.RawMatrix()
		for i := 0; i < x.Len(); i++ {
			xi := x.at(i)
			if xi != 0 {
				if y.mat.Inc == 1 {
					sum += xi * asm.DdotUnitary(
						bmat.Data[i*bmat.Stride:i*bmat.Stride+n],
						y.mat.Data,
					)
				} else {
					sum += xi * asm.DdotInc(
						bmat.Data[i*bmat.Stride:i*bmat.Stride+n],
						y.mat.Data, uintptr(n),
						1, uintptr(y.mat.Inc),
						0, 0,
					)
				}
			}
		}
	default:
		for i := 0; i < x.Len(); i++ {
			xi := x.at(i)
			for j := 0; j < y.Len(); j++ {
				sum += xi * A.At(i, j) * y.at(j)
			}
		}
	}
	return sum
}
Beispiel #3
0
// Dgemv computes
//  y = alpha * a * x + beta * y if tA = blas.NoTrans
//  y = alpha * A^T * x + beta * y if tA = blas.Trans or blas.ConjTrans
// where A is an m×n dense matrix, x and y are vectors, and alpha is a scalar.
func (Implementation) Dgemv(tA blas.Transpose, m, n int, alpha float64, a []float64, lda int, x []float64, incX int, beta float64, y []float64, incY int) {
	if tA != blas.NoTrans && tA != blas.Trans && tA != blas.ConjTrans {
		panic(badTranspose)
	}
	if m < 0 {
		panic(mLT0)
	}
	if n < 0 {
		panic(nLT0)
	}
	if lda < max(1, n) {
		panic(badLdA)
	}

	if incX == 0 {
		panic(zeroIncX)
	}
	if incY == 0 {
		panic(zeroIncY)
	}

	// Quick return if possible
	if m == 0 || n == 0 || (alpha == 0 && beta == 1) {
		return
	}

	// Set up indexes
	lenX := m
	lenY := n
	if tA == blas.NoTrans {
		lenX = n
		lenY = m
	}
	var kx, ky int
	if incX > 0 {
		kx = 0
	} else {
		kx = -(lenX - 1) * incX
	}
	if incY > 0 {
		ky = 0
	} else {
		ky = -(lenY - 1) * incY
	}

	// First form y := beta * y
	if incY > 0 {
		Implementation{}.Dscal(lenY, beta, y, incY)
	} else {
		Implementation{}.Dscal(lenY, beta, y, -incY)
	}

	if alpha == 0 {
		return
	}

	// Form y := alpha * A * x + y
	if tA == blas.NoTrans {
		if incX == 1 {
			for i := 0; i < m; i++ {
				y[i] += alpha * asm.DdotUnitary(a[lda*i:lda*i+n], x)
			}
			return
		}
		iy := ky
		for i := 0; i < m; i++ {
			y[iy] += alpha * asm.DdotInc(x, a[lda*i:lda*i+n], uintptr(n), uintptr(incX), 1, uintptr(kx), 0)
			iy += incY
		}
		return
	}
	// Cases where a is not transposed.
	if incX == 1 {
		for i := 0; i < m; i++ {
			tmp := alpha * x[i]
			if tmp != 0 {
				asm.DaxpyUnitary(tmp, a[lda*i:lda*i+n], y, y)
			}
		}
		return
	}
	ix := kx
	for i := 0; i < m; i++ {
		tmp := alpha * x[ix]
		if tmp != 0 {
			asm.DaxpyInc(tmp, a[lda*i:lda*i+n], y, uintptr(n), 1, uintptr(incY), 0, uintptr(ky))
		}
		ix += incX
	}
}