// Subtract calculates A = A - B and returns A. func (A *Matrix) SubBLAS(B *Matrix) *Matrix { // Normal matrices. if A.stride == A.width && B.stride == B.width { blas.Daxpy(len(A.data), -1.0, B.data, 1, A.data, 1) return A } // Submatrices. for i := 0; i < A.height; i++ { blas.Daxpy(A.width, -1.0, B.Row(i), 1, A.Row(i), 1) } return A }
// MulSubBLAS calculates C = C - A * B. func (C *Matrix) MulSubBLAS(A, B *Matrix) *Matrix { for i := 0; i < A.height; i++ { Ci := C.Row(i) for j, aij := range A.Row(i) { // Ci = Ci - aij * Bj blas.Daxpy(C.width, -aij, B.Row(j), 1, Ci, 1) } } return C }
// MulBLAS calculates C = A * B. func (C *Matrix) MulBLAS(A, B *Matrix) *Matrix { for i := 0; i < A.height; i++ { Ci := C.Row(i) for k := range Ci { Ci[k] = 0 } for j, aij := range A.Row(i) { // Ci += aij * Bj blas.Daxpy(C.width, aij, B.Row(j), 1, Ci, 1) } } return C }
// Sum computes the addition of two vectors, x&y and // puts the result in z func Sum(n int, alpha float64, x, y, z []float64) { if n == 3 { z[0] = alpha*x[0] + y[0] z[1] = alpha*x[1] + y[1] z[2] = alpha*x[2] + y[2] } else if n == 4 { z[0] = alpha*x[0] + y[0] z[1] = alpha*x[1] + y[1] z[2] = alpha*x[2] + y[2] z[3] = alpha*x[3] + y[3] } else if n == 6 { z[0] = alpha*x[0] + y[0] z[1] = alpha*x[1] + y[1] z[2] = alpha*x[2] + y[2] z[3] = alpha*x[3] + y[3] z[4] = alpha*x[4] + y[4] z[5] = alpha*x[5] + y[5] } else { copy(z, y) blas.Daxpy(n, alpha, x, 1, z, 1) } }
func Outer(N, m, n int, alpha float64, A, x, y []float64) { if N != n { panic("vec: Dimensions invalid. A.Cols() != x.Len()") } if alpha == 0 { return } if alpha == 1 { goto alg } blas.Dscal(N, alpha, x, 1) goto alg alg: { for i := 0; i < m; i++ { blas.Daxpy(N, x[i], y, 1, A[n*i:n*i+n], 1) } } }
func axpy64(alpha float64, X []float64, Y []float64) { blas.Daxpy(len(X), alpha, X, 1, Y, 1) }