GSL 系列 5 — 向量和矩阵 4 — 基本线性代数运算 (BLAS)

写在前面

关于向量,矩阵的定义参考

若无特别说明,本篇代码均来自头文件 gsl_blas.h

概述

将基本的线代运算分为三个层次:

  • 层次1,向量运算,比如 α x + y \alpha x+y αx+y
  • 层次2,矩阵和向量的运算,比如 α A x + β y \alpha Ax+\beta y αAx+βy
  • 层次3,矩阵和矩阵的运算,比如 α A B + C \alpha AB+C αAB+C

将如下矩阵类型做名字简单的名字标记:

名字标记 矩阵类型 名字标记 矩阵类型
GE 一般矩阵 GB 一般带矩阵
SY 对称矩阵 SB 对称带矩阵
SP 对称矩阵包 HE 共轭矩阵
HB 共轭带矩阵 HP 共轭矩阵包
TR 三角矩阵 TB 三角带矩阵
TP 三角矩阵包

以下名字标记代表线代运算

名字标记 矩阵运算
DOT 标量乘 x T y x^Ty xTy
AXPY 向量加 α x + y \alpha x+y αx+y
MV 矩阵向量乘 A x Ax Ax
SV 矩阵向量求解 A − 1 b A^{-1}b A1b
MM 矩阵矩阵乘 A A AA AA
SM 矩阵矩阵求解 A − 1 B A^{-1}B A1B

以下名字标记代表数据类型

名字标记 数据类型
S 单精度实数
D 双精度实数
C 单精度复数
Z 双精度复数

层次 1 运算

α + x T y \alpha +x^Ty α+xTy

int gsl_blas_sdsdot (float alpha,
                     const gsl_vector_float * X,
                     const gsl_vector_float * Y,
                     float * result
                     );

x T y x^Ty xTy

int gsl_blas_dsdot (const gsl_vector_float * X,
                    const gsl_vector_float * Y,
                    double * result
                    );
int gsl_blas_sdot (const gsl_vector_float * X,
                   const gsl_vector_float * Y,
                   float * result
                   );
int gsl_blas_ddot (const gsl_vector * X,
                   const gsl_vector * Y,
                   double * result
                   );
int  gsl_blas_cdotu (const gsl_vector_complex_float * X,
                     const gsl_vector_complex_float * Y,
                     gsl_complex_float * dotu);
int  gsl_blas_zdotu (const gsl_vector_complex * X,
                     const gsl_vector_complex * Y,
                     gsl_complex * dotu);                                        

x H y x^Hy xHy

int  gsl_blas_cdotc (const gsl_vector_complex_float * X,
                     const gsl_vector_complex_float * Y,
                     gsl_complex_float * dotc);
int  gsl_blas_zdotc (const gsl_vector_complex * X,
                     const gsl_vector_complex * Y,
                     gsl_complex * dotc);                     

∥ x ∥ 2 \|x\|_2 x2

float  gsl_blas_snrm2  (const gsl_vector_float * X);
double gsl_blas_dnrm2  (const gsl_vector * X);
float  gsl_blas_scnrm2 (const gsl_vector_complex_float * X);
double gsl_blas_dznrm2 (const gsl_vector_complex * X);

∑ ∣ x i ∣ \sum|x_i| xi

float  gsl_blas_sasum  (const gsl_vector_float * X);
double gsl_blas_dasum  (const gsl_vector * X);

∑ ( ∣ ℜ ( x i ) ∣ + ∣ ℑ ( x i ) ∣ ) \sum\left(\left|\Re\left(x_{i}\right)\right|+\left|\Im\left(x_{i}\right)\right|\right) ((xi)+(xi))

float  gsl_blas_scasum (const gsl_vector_complex_float * X);
double gsl_blas_dzasum (const gsl_vector_complex * X);

max ⁡ i ∣ x i ∣ \max_i|x_i| maxixi

CBLAS_INDEX_t gsl_blas_isamax (const gsl_vector_float * X);
CBLAS_INDEX_t gsl_blas_idamax (const gsl_vector * X);

// gsl_blas_types.h
typedef  CBLAS_INDEX  CBLAS_INDEX_t;
// gsl_cblas.h
#define CBLAS_INDEX size_t

max ⁡ i ( ∣ ℜ ( x i ) ∣ + ∣ ℑ ( x i ) ∣ ) \max_i(\left|\Re\left(x_{i}\right)\right|+\left|\Im\left(x_{i}\right)\right|) maxi((xi)+(xi))

CBLAS_INDEX_t gsl_blas_
  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
下面是一个基于GSL线性代数实例,演示如何使用GSL库来解决线性方程组和求逆矩阵问题。 ```c #include <stdio.h> #include <gsl/gsl_linalg.h> int main() { // 定义矩阵向量 double mdata[] = {1.0, 2.0, 3.0, 2.0, 4.0, 5.0, 3.0, 5.0, 6.0}; double vdata[] = {1.0, 2.0, 3.0}; gsl_matrix_view m = gsl_matrix_view_array(mdata, 3, 3); gsl_vector_view v = gsl_vector_view_array(vdata, 3); // 解决线性方程组 Ax = b gsl_vector *x = gsl_vector_alloc(3); gsl_permutation *p = gsl_permutation_alloc(3); int signum; gsl_linalg_LU_decomp(&m.matrix, p, &signum); gsl_linalg_LU_solve(&m.matrix, p, &v.vector, x); printf("Solution to Ax = b:\n"); gsl_vector_fprintf(stdout, x, "%g"); // 求矩阵的逆 gsl_matrix *inv = gsl_matrix_alloc(3, 3); gsl_matrix *mcopy = gsl_matrix_alloc(3, 3); gsl_matrix_memcpy(mcopy, &m.matrix); gsl_linalg_LU_decomp(mcopy, p, &signum); gsl_linalg_LU_invert(mcopy, p, inv); printf("Inverse of A:\n"); gsl_matrix_fprintf(stdout, inv, "%g"); // 释放内存 gsl_vector_free(x); gsl_permutation_free(p); gsl_matrix_free(inv); gsl_matrix_free(mcopy); return 0; } ``` 该程序首先定义了一个3x3的矩阵和一个长度为3的向量,并将它们存储在GSL矩阵向量视图中。然后,程序使用GSL库中的LU分解和求解线性方程组函数来解决线性方程组Ax=b,并打印解向量x的值。接下来,程序使用GSL库中的LU分解和求逆矩阵函数来求解矩阵A的逆,并打印逆矩阵的值。最后,程序释放了动态分配的内存。 需要注意的是,在编译该程序时,需要链接GSL库,例如: ``` gcc -o example example.c -lgsl -lgslcblas -lm ``` 此处,-lgsl和-lgslcblas选项用于链接GSL库和BLAS库,-lm选项用于链接数学库。

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值