微信公众号搜"智元新知"关注
微信扫一扫可直接关注哦!

c – BLAS中的元素向量向量乘法?

有没有办法用BLAS,GSL或任何其他高性能库进行元素向量向量乘法?

解决方法

(以字面上的题目标题…)

是的,可以单独使用BLAS(尽管它可能不是最有效的方式).

诀窍在于将一个输入向量作为对角线矩阵:

⎡a    ⎤ ⎡x⎤    ⎡ax⎤
⎢  b  ⎥ ⎢y⎥ =  ⎢by⎥
⎣    c⎦ ⎣z⎦    ⎣cz⎦

然后,您可以使用矩阵向量乘法函数中的一个,该函数可以将对角矩阵作为输入,而不需要填充,例如. SBMV

例:

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 int *lda,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,b,c);
    printf("Result: [%f %f %f]\n",c[0],c[1],c[2]);
    return 0;
}

结果:[1.000000 30.000000 500.000000]

版权声明:本文内容由互联网用户自发贡献,该文观点与技术仅代表作者本人。本站仅提供信息存储空间服务,不拥有所有权,不承担相关法律责任。如发现本站有涉嫌侵权/违法违规的内容, 请发送邮件至 dio@foxmail.com 举报,一经查实,本站将立刻删除。

相关推荐