func (A *Matrix) ScaleBLAS(v float64) { // Normal matrices. if A.stride == A.width { blas.Dscal(len(A.data), v, A.data, 1) return } // Submatrices. for i := 0; i < A.height; i++ { blas.Dscal(A.width, v, A.Row(i), 1) } }
// MVMul multiplies a matrix with a vector // y = alpha*A*x + y func MVMul(N, m, n int, alpha float64, A, x, y []float64) { if N != n { panic("vec: Dimensions invalid. A.Cols() != Vec.Len()") } if alpha == 0 { return } if alpha == 1 { goto alg } blas.Dscal(N, alpha, A, 1) goto alg alg: { if N < 8 { idx, ptr := 0, 0 extra := n - n%4 ub := ((n / 4) * 4) - 1 for i := 0; i < m; i++ { idx = i * n for j := 0; j < ub; j += 4 { ptr = idx + j y[i] += A[ptr]*x[j] + A[ptr+1]*x[j+1] + A[ptr+2]*x[j+2] + A[ptr+3]*x[j+3] } for j := extra; j < n; j++ { y[i] += A[idx+j] * x[j] } } return } for i := 0; i < m; i++ { y[i] += blas.Ddot(N, A[n*i:n*i+n], 1, x, 1) } } }
// Scale multiples vector, x with scalar alpha func Scale(n int, alpha float64, x []float64) { if n == 3 { x[0] *= alpha x[1] *= alpha x[2] *= alpha } else if n == 4 { x[0] *= alpha x[1] *= alpha x[2] *= alpha x[3] *= alpha } else if n == 6 { x[0] *= alpha x[1] *= alpha x[2] *= alpha x[3] *= alpha x[4] *= alpha x[5] *= alpha } else { blas.Dscal(n, alpha, x, 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 scal64(alpha float64, X []float64) { blas.Dscal(len(X), alpha, X, 1) }