Is there a means to do element-wise vector-vector multiplication with BLAS, GSL or any other high performance library ?
for(int i=0;i<v. size();++i) v[i] = v[i] * k; This is the most basic way to multiply vector by a scalar.
First, multiply Row 1 of the matrix by Column 1 of the vector. Next, multiply Row 2 of the matrix by Column 1 of the vector. Finally multiply Row 3 of the matrix by Column 1 of the vector.
Matrix-vector multiplication is an operation between a matrix and a vector that produces a new vector. Notably, matrix-vector multiplication is only defined between a matrix and a vector where the length of the vector equals the number of columns of the matrix.
(Taking the title of the question literally...)
Yes it can be done with BLAS alone (though it is probably not the most efficient way.)
The trick is to treat one of the input vectors as a diagonal matrix:
⎡a ⎤ ⎡x⎤ ⎡ax⎤
⎢ b ⎥ ⎢y⎥ = ⎢by⎥
⎣ c⎦ ⎣z⎦ ⎣cz⎦
You can then use one of the matrix-vector multiply functions that can take a diagonal matrix as input without padding, e.g. SBMV
Example:
void ebeMultiply(const int n, const double *a, const double *x, double *y)
{
extern void dsbmv_(const char *uplo,
const int *n,
const int *k,
const double *alpha,
const double *a,
const int *lda,
const double *x,
const int *incx,
const double *beta,
double *y,
const int *incy);
static const int k = 0; // Just the diagonal; 0 super-diagonal bands
static const double alpha = 1.0;
static const int lda = 1;
static const int incx = 1;
static const double beta = 0.0;
static const int incy = 1;
dsbmv_("L", &n, &k, &alpha, a, &lda, x, &incx, &beta, y, &incy);
}
// Test
#define N 3
static const double a[N] = {1,3,5};
static const double b[N] = {1,10,100};
static double c[N];
int main(int argc, char **argv)
{
ebeMultiply(N, a, b, c);
printf("Result: [%f %f %f]\n", c[0], c[1], c[2]);
return 0;
}
Result: [1.000000 30.000000 500.000000]
I found that MKL has a whole set of mathematical operations on vector, in its Vector Mathematical Functions Library (VML), including v?Mul, which does what I want. It works with c++ arrays, so it's more convenient for me than GSL.
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With