在所有值都是实值的情况下,我一直能够使用cblas_sgemv。但是,如果没有“cblas_cgemv”错误,我就不能使用EXC_BAD_ACCESS。对于这个函数,我是否正确地假设,复杂部分直接出现在函数作为参数的数组中的真实部分之后?例如,如果我有一个矩阵:
1 + 2i, 3 + 4i
5 + 6i, 7 + 8i然后表示为1.0,2.0,3.0,4.0,5.0,6.0,7.0,8.0。对于任何给定的矩阵和向量,关于它在数组中的表示,N,M,lda,incX和incY应该有什么值?
完整的示例:对于cblas_sgemv,函数原型是:
func cblas_sgemv(_ __Order: CBLAS_ORDER, _ __TransA: CBLAS_TRANSPOSE, _ __M: Int32, _ __N: Int32, _ __alpha: Float, _ __A: UnsafePointer<Float>, _ __lda: Int32, _ __X: UnsafePointer<Float>, _ __incX: Int32, _ __beta: Float, _ __Y: UnsafeMutablePointer<Float>, _ __incY: Int32)此代码适用于:
let matrix: [Float] = [1.0,2.0,3.0,4.0]
let vector: [Float] = [1.0,2.0]
let matrixConverted: UnsafePointer<Float> = UnsafePointer<Float>(matrix)
let vectorConverted: UnsafePointer<Float> = UnsafePointer<Float>(vector)
let matrixSize = sqrt(Double(matrix.count)
let total: [Float] = [Float](count: Int(matrixSize), repeatedValue: 0.0)
let totalConverted: UnsafeMutablePointer<Float> = UnsafeMutablePointer<Float>(total)
cblas_sgemv(CblasRowMajor, CblasNoTrans, Int32(matrixSize), Int32(matrixSize), 1.0, matrixConverted, Int32(matrixSize), vectorConverted, 1, 0, totalConverted, 1)答案全部存储在一起。那么,除了上面的复杂矩阵和带有prototype的函数cblas_cgemv,做同样的事情会是什么样子:
func cblas_cgemv(_ __Order: CBLAS_ORDER, _ __TransA: CBLAS_TRANSPOSE, _ __M: Int32, _ __N: Int32, _ __alpha: UnsafePointer<Void>, _ __A: UnsafePointer<Void>, _ __lda: Int32, _ __X: UnsafePointer<Void>, _ __incX: Int32, _ __beta: UnsafePointer<Void>, _ __Y: UnsafeMutablePointer<Void>, _ __incY: Int32) 更多的信息和参考可以在这里找到:ref/doc/uid/TP30000414-SW55
发布于 2015-12-05 11:32:33
您的现有代码有点过于复杂,并且有一个错误:
cblas_sgemv()函数,不需要matrixConverted和vectorConverted。total的可变指针。结果向量必须是可变的(而且也不需要totalConverted )。因此,您的代码可以简化为:
let matrix: [Float] = [1.0,2.0,3.0,4.0]
let vector: [Float] = [1.0,2.0]
let matrixSize = sqrt(Double(matrix.count))
var total = [Float](count: Int(matrixSize), repeatedValue: 0.0)
cblas_sgemv(CblasRowMajor, CblasNoTrans, Int32(matrixSize), Int32(matrixSize), 1.0, matrix, Int32(matrixSize), vector, 1, 0, &total, 1)<cblas.h>中记录了BLAS例程中复数的布局。
* A note on complex data layouts:
*
* In order to allow straightforward interoperation with other libraries and
* complex types in C and C++, complex data in BLAS is passed through an opaque
* pointer (void *). The layout requirements on this complex data are that
* the real and imaginary parts are stored consecutively in memory, and have
* the alignment of the corresponding real type (float or double). The BLAS
* complex interfaces are compatible with the following types:
*
* - The C complex types, defined in <complex.h>.
* - The C++ std::complex types, defined in <complex>.
* - The LAPACK complex types, defined in <Accelerate/vecLib/clapack.h>.
* - The vDSP types DSPComplex and DSPDoubleComplex, defined in <Accelerate/vecLib/vDSP.h>.
* - An array of size two of the corresponding real type.
* - A structure containing two elements, each of the corresponding real type.因此,要乘
| 1 + 2i 3 + 4i | | 1 + 2i |
| | * | |
| 5 + 6i 7 + 8i | | 3 + 4i |您可以将每个复数表示为连续存储的两个浮点数:
let matrix: [Float] = [1.0,2.0, 3.0,4.0, 5.0,6.0, 7.0,8.0]
let vector: [Float] = [1.0,2.0, 3.0,4.0]
let matrixSize = sqrt(Double(matrix.count/2))
var total = [Float](count: vector.count, repeatedValue: 0.0)
let alpha : [Float] = [1.0, 0.0]
let beta : [Float] = [1.0, 0.0]
cblas_cgemv(CblasRowMajor, CblasNoTrans, Int32(matrixSize), Int32(matrixSize), beta, matrix, Int32(matrixSize), vector, 1, alpha, &total, 1)也可以使用DSPComplex、COMPLEX或__CLPK_complex结构(它们都具有相同的布局)来表示复数:
let matrix = [DSPComplex(real: 1.0, imag: 2.0), DSPComplex(real: 3.0, imag: 4.0),
DSPComplex(real: 5.0, imag: 6.0), DSPComplex(real: 7.0, imag: 8.0)]
let vector = [DSPComplex(real: 1.0, imag: 2.0), DSPComplex(real: 3.0, imag: 4.0)]
let matrixSize = sqrt(Double(matrix.count))
var total = [DSPComplex](count: Int(matrixSize), repeatedValue: DSPComplex())
var alpha = [DSPComplex(real: 1.0, imag: 0.0)]
var beta = [DSPComplex(real: 1.0, imag: 0.0)]
cblas_cgemv(CblasRowMajor, CblasNoTrans, Int32(matrixSize), Int32(matrixSize), alpha, matrix, Int32(matrixSize), vector, 1, beta, &total, 1)在这两种情况下,维度M、N等都是指复数的计数,因此它们与实数示例中的值M=N=2相同,而alpha和beta也是表示复杂因子的数组。
https://stackoverflow.com/questions/34103818
复制相似问题