blas/{blas32,blas64,cblas64,cblas128}: check vector length agreement
diff --git a/blas/blas32/blas32.go b/blas/blas32/blas32.go
index c8018e9..73e5334 100644
--- a/blas/blas32/blas32.go
+++ b/blas/blas32/blas32.go
@@ -99,23 +99,38 @@
// Level 1
-const negInc = "blas32: negative vector increment"
+const (
+ negInc = "blas32: negative vector increment"
+ badLength = "blas32: vector length mismatch"
+)
// Dot computes the dot product of the two vectors:
// \sum_i x[i]*y[i].
+// Dot will panic if the lengths of x and y do not match.
func Dot(x, y Vector) float32 {
+ if x.N != y.N {
+ panic(badLength)
+ }
return blas32.Sdot(x.N, x.Data, x.Inc, y.Data, y.Inc)
}
// DDot computes the dot product of the two vectors:
// \sum_i x[i]*y[i].
+// DDot will panic if the lengths of x and y do not match.
func DDot(x, y Vector) float64 {
+ if x.N != y.N {
+ panic(badLength)
+ }
return blas32.Dsdot(x.N, x.Data, x.Inc, y.Data, y.Inc)
}
// SDDot computes the dot product of the two vectors adding a constant:
// alpha + \sum_i x[i]*y[i].
+// SDDot will panic if the lengths of x and y do not match.
func SDDot(alpha float32, x, y Vector) float32 {
+ if x.N != y.N {
+ panic(badLength)
+ }
return blas32.Sdsdot(x.N, alpha, x.Data, x.Inc, y.Data, y.Inc)
}
@@ -155,19 +170,31 @@
// Swap exchanges the elements of the two vectors:
// x[i], y[i] = y[i], x[i] for all i.
+// Swap will panic if the lengths of x and y do not match.
func Swap(x, y Vector) {
+ if x.N != y.N {
+ panic(badLength)
+ }
blas32.Sswap(x.N, x.Data, x.Inc, y.Data, y.Inc)
}
// Copy copies the elements of x into the elements of y:
// y[i] = x[i] for all i.
+// Copy will panic if the lengths of x and y do not match.
func Copy(x, y Vector) {
+ if x.N != y.N {
+ panic(badLength)
+ }
blas32.Scopy(x.N, x.Data, x.Inc, y.Data, y.Inc)
}
// Axpy adds x scaled by alpha to y:
// y[i] += alpha*x[i] for all i.
+// Axpy will panic if the lengths of x and y do not match.
func Axpy(alpha float32, x, y Vector) {
+ if x.N != y.N {
+ panic(badLength)
+ }
blas32.Saxpy(x.N, alpha, x.Data, x.Inc, y.Data, y.Inc)
}
diff --git a/blas/blas64/blas64.go b/blas/blas64/blas64.go
index 58d3d49..5871321 100644
--- a/blas/blas64/blas64.go
+++ b/blas/blas64/blas64.go
@@ -106,6 +106,7 @@
// Dot computes the dot product of the two vectors:
// \sum_i x[i]*y[i].
+// Dot will panic if the lengths of x and y do not match.
func Dot(x, y Vector) float64 {
if x.N != y.N {
panic(badLength)
@@ -149,6 +150,7 @@
// Swap exchanges the elements of the two vectors:
// x[i], y[i] = y[i], x[i] for all i.
+// Swap will panic if the lengths of x and y do not match.
func Swap(x, y Vector) {
if x.N != y.N {
panic(badLength)
@@ -158,7 +160,7 @@
// Copy copies the elements of x into the elements of y:
// y[i] = x[i] for all i.
-// Copy requires that the lengths of x and y match and will panic otherwise.
+// Copy will panic if the lengths of x and y do not match.
func Copy(x, y Vector) {
if x.N != y.N {
panic(badLength)
@@ -168,6 +170,7 @@
// Axpy adds x scaled by alpha to y:
// y[i] += alpha*x[i] for all i.
+// Axpy will panic if the lengths of x and y do not match.
func Axpy(alpha float64, x, y Vector) {
if x.N != y.N {
panic(badLength)
diff --git a/blas/cblas128/cblas128.go b/blas/cblas128/cblas128.go
index 83663b3..4b74584 100644
--- a/blas/cblas128/cblas128.go
+++ b/blas/cblas128/cblas128.go
@@ -108,19 +108,30 @@
// Level 1
-const negInc = "cblas128: negative vector increment"
+const (
+ negInc = "cblas128: negative vector increment"
+ badLength = "cblas128: vector length mismatch"
+)
// Dotu computes the dot product of the two vectors without
// complex conjugation:
// xᵀ * y.
+// Dotu will panic if the lengths of x and y do not match.
func Dotu(x, y Vector) complex128 {
+ if x.N != y.N {
+ panic(badLength)
+ }
return cblas128.Zdotu(x.N, x.Data, x.Inc, y.Data, y.Inc)
}
// Dotc computes the dot product of the two vectors with
// complex conjugation:
// xᴴ * y.
+// Dotc will panic if the lengths of x and y do not match.
func Dotc(x, y Vector) complex128 {
+ if x.N != y.N {
+ panic(badLength)
+ }
return cblas128.Zdotc(x.N, x.Data, x.Inc, y.Data, y.Inc)
}
@@ -163,20 +174,32 @@
// Swap exchanges the elements of two vectors:
// x[i], y[i] = y[i], x[i] for all i.
+// Swap will panic if the lengths of x and y do not match.
func Swap(x, y Vector) {
+ if x.N != y.N {
+ panic(badLength)
+ }
cblas128.Zswap(x.N, x.Data, x.Inc, y.Data, y.Inc)
}
// Copy copies the elements of x into the elements of y:
// y[i] = x[i] for all i.
+// Copy will panic if the lengths of x and y do not match.
func Copy(x, y Vector) {
+ if x.N != y.N {
+ panic(badLength)
+ }
cblas128.Zcopy(x.N, x.Data, x.Inc, y.Data, y.Inc)
}
// Axpy computes
// y = alpha * x + y,
// where x and y are vectors, and alpha is a scalar.
+// Axpy will panic if the lengths of x and y do not match.
func Axpy(alpha complex128, x, y Vector) {
+ if x.N != y.N {
+ panic(badLength)
+ }
cblas128.Zaxpy(x.N, alpha, x.Data, x.Inc, y.Data, y.Inc)
}
diff --git a/blas/cblas64/cblas64.go b/blas/cblas64/cblas64.go
index 2ad3e8d..29772f1 100644
--- a/blas/cblas64/cblas64.go
+++ b/blas/cblas64/cblas64.go
@@ -108,19 +108,30 @@
// Level 1
-const negInc = "cblas64: negative vector increment"
+const (
+ negInc = "cblas64: negative vector increment"
+ badLength = "cblas64: vector length mismatch"
+)
// Dotu computes the dot product of the two vectors without
// complex conjugation:
// xᵀ * y
+// Dotu will panic if the lengths of x and y do not match.
func Dotu(x, y Vector) complex64 {
+ if x.N != y.N {
+ panic(badLength)
+ }
return cblas64.Cdotu(x.N, x.Data, x.Inc, y.Data, y.Inc)
}
// Dotc computes the dot product of the two vectors with
// complex conjugation:
// xᴴ * y.
+// Dotc will panic if the lengths of x and y do not match.
func Dotc(x, y Vector) complex64 {
+ if x.N != y.N {
+ panic(badLength)
+ }
return cblas64.Cdotc(x.N, x.Data, x.Inc, y.Data, y.Inc)
}
@@ -163,20 +174,32 @@
// Swap exchanges the elements of two vectors:
// x[i], y[i] = y[i], x[i] for all i.
+// Swap will panic if the lengths of x and y do not match.
func Swap(x, y Vector) {
+ if x.N != y.N {
+ panic(badLength)
+ }
cblas64.Cswap(x.N, x.Data, x.Inc, y.Data, y.Inc)
}
// Copy copies the elements of x into the elements of y:
// y[i] = x[i] for all i.
+// Copy will panic if the lengths of x and y do not match.
func Copy(x, y Vector) {
+ if x.N != y.N {
+ panic(badLength)
+ }
cblas64.Ccopy(x.N, x.Data, x.Inc, y.Data, y.Inc)
}
// Axpy computes
// y = alpha * x + y,
// where x and y are vectors, and alpha is a scalar.
+// Axpy will panic if the lengths of x and y do not match.
func Axpy(alpha complex64, x, y Vector) {
+ if x.N != y.N {
+ panic(badLength)
+ }
cblas64.Caxpy(x.N, alpha, x.Data, x.Inc, y.Data, y.Inc)
}