Beispiel #1
0
/*
 Returns the dot product of two one dimensional arrays
 Panics if either of the arrays is not one dimensional or if the lengths are not equal
*/
func Dot(x, y *GsArray) float64 {
	if len(x.shape) > 2 || len(y.shape) > 2 {
		panic("Invalid dimension for dot product!!")
	}
	if len(x.shape) == 2 && len(y.shape) == 2 {
		if x.shape[0] != 1 && x.shape[1] != 1 {
			if y.shape[0] != 1 && y.shape[1] != 1 {
				panic("Invalid dimension for dot product!!")
			}
		}
	} else if len(x.shape) == 1 && len(y.shape) == 2 {
		if (y.shape[0] != 1) && (y.shape[1] != 1) {
			panic("Invalid dimension for dot product!!")
		}
	} else if len(x.shape) == 2 && len(y.shape) == 1 {
		if (x.shape[0] != 1) && (x.shape[1] != 1) {
			panic("Invalid dimension for dot product!!")
		}
	}
	if len(x.data) != len(y.data) {
		panic("Vectors must be of the same lenght!!")
	}
	c_N := C.int(len(x.data))
	c_x := (*C.double)(unsafe.Pointer(&x.data[0]))
	c_incX := C.int(1)
	c_y := (*C.double)(unsafe.Pointer(&y.data[0]))
	c_incY := C.int(1)

	return float64(C.cblas_ddot(c_N, c_x, c_incX, c_y, c_incY))
}
Beispiel #2
0
/*
 double cblas_ddot(const int N, const double *X, const int incX, const double *Y, const int incY);
*/
func Ddot(x, y []float64) float64 {
	if len(x) != len(y) {
		panic("slices' size differ")
	}

	c_N := C.int(len(x))

	c_X := (*C.double)(unsafe.Pointer(&x[0]))
	c_incX := C.int(1)

	c_Y := (*C.double)(unsafe.Pointer(&y[0]))
	c_incY := C.int(1)

	return float64(
		C.cblas_ddot(c_N,
			c_X, c_incX,
			c_Y, c_incY))
}
Beispiel #3
0
// Dot product of v & o
func (v vector) dot(o vector) float64 {
	c1 := (*C.double)(unsafe.Pointer(&v[0]))
	c2 := (*C.double)(unsafe.Pointer(&o[0]))

	return float64(C.cblas_ddot(C.int(len(v)), c1, 1, c2, 1))
}