首页 > 解决方案 > 矩阵向量乘法,迷失在 Lapack 中

问题描述

我想计算

x^TA x* (x 转置乘以矩阵 A 乘以 x 共轭复数)

使用 Lapack。我没有找到这个功能。还是有一个?最快的方法是什么?

标签: matrixvectormultiplicationlapack

解决方案


您可以分两个单独的步骤计算操作:

  1. 计算y <- A x*
  2. 计算res <- x^T y

我在以下程序中实现了这一点

program main

  use blas95

  implicit none

  integer, parameter :: n = 2
  complex :: x(n), y(n), A(n,n), res

  complex :: cdotu

  ! init vector x, matrix A by some data
  x = [(1, 1), (-1, 2)]
  A = reshape([1, 2, 3, 4], [n, n])

  ! compute: y <- A * conjg(x)
  call cgemv('N', n, n, (1, 0), A, n, conjg(x), 1, (0, 0), y, 1)    ! standard BLAS
  call gemv(A, conjg(x), y)                                         ! BLAS95 from intel MKL

  ! compute: res <- x^T * y
  res = cdotu(n, x, 1, y, 1)                                        ! standard BLAS
  res = dotu(x, y)                                                  ! BLAS95 from intel MKL
end program

注意:我包括了标准的 BLAS 例程以及通过英特尔的 MKL 提供的 BLAS95 例程。


链接到英特尔的文档


推荐阅读