前言
前面介绍了各种向量-向量,矩阵-向量,矩阵-矩阵的函数简介。根据自身目前状况,主要使用实数域的操作,也就是说关注单精度float类型的s
和双精度double类型的d
。还有就是用的基本都是全矩阵,没有经过压缩,也不是对称、三角、带状的某一种情况。所以主要还是总结一般的乘法、加法操作。
【注】代码都以单精度float的情况书写,主要流程要记住,使用mkl_malloc
申请内存,使用mkl_free
释放内存。n年没用过C++了,凑合看看吧。
学MKL的肯定对编程有一定程度了解,智能提示是一个很好的工具,在VS中,输入cblas_s
以后,会自动补全所有的单精度操作函数,那么根据常规经验,就能判断出它到底用于做什么以及需要的参数;比如提示axpby
意思就是a*x
加b*y
。还有就是函数有两种,一种是有返回值,一种无返回值,怎么办,只能提示看函数的声明是void
还是float
或者是double
类型即可。
向量-向量
加法
运算
代码
#include<stdio.h>
#include<stdlib.h>
#include<mkl.h>
int main()
{float *A, *B;//两个向量int a=1, b=1;//标量int n = 5;//向量大小A = (float *)mkl_malloc(n * 1 * sizeof(float), 64);B = (float *)mkl_malloc(n * 1 * sizeof(float), 64);printf("The 1st vector is ");for (int i = 0; i < n; i++){A[i] = i;printf("%2.0f", A[i]);}printf("\n");printf("The 2st vector is ");for (int i = 0; i < n; i++){B[i] =i+1;printf("%2.0f", B[i]);}printf("\n");//计算a*A+b*Bcblas_saxpby(n, a, A, 1, b, B, 1);printf("The a*A+b*B is ");for (int i = 0; i < n; i++){printf("%2.0f", B[i]);}printf("\n");mkl_free(A);mkl_free(B);getchar();return 0;
}
结果
The 1st vector is 0 1 2 3 4
The 2st vector is 1 2 3 4 5
The a*A+b*B is 1 3 5 7 9
乘法
运算:向量点乘
代码:
//乘法
#include<stdio.h>
#include<stdlib.h>
#include <mkl.h>int main()
{float *A, *B;//两个向量int a = 1, b = 1;//标量int n = 5;//向量大小float res;A = (float *)mkl_malloc(n * 1 * sizeof(float), 64);B = (float *)mkl_malloc(n * 1 * sizeof(float), 64);printf("The 1st vector is ");for (int i = 0; i < n; i++){A[i] = i;printf("%2.0f", A[i]);}printf("\n");printf("The 2st vector is ");for (int i = 0; i < n; i++){B[i] = i + 1;printf("%2.0f", B[i]);}printf("\n");//乘法:对应元素乘积的加和res=cblas_sdot(n, A, 1, B, 1);printf("点乘结果: %2.0f",res);printf("\n");mkl_free(A);mkl_free(B);getchar();return 0;
}
结果:
The 1st vector is 0 1 2 3 4
The 2st vector is 1 2 3 4 5
点乘结果: 40
二范数
运算:二范数或者欧几里得范数,是所有元素平方和开根号
代码
//计算向量二范数
#include<stdio.h>
#include<stdlib.h>
#include<mkl.h>
void main()
{float *A;int n = 5;float res;A = (float *)mkl_malloc(n*sizeof(float), 64);printf("The original vector:\n");for (int i = 0; i < n; i++){A[i] = i + 1;printf("%2.0f ", A[i]);}printf("\n");res = cblas_snrm2(n, A, 1);//计算二范数printf("The norm2 of vector is:%2.6f", res);mkl_free(A);getchar();
}
结果:
The original vector:1 2 3 4 5
The norm2 of vector is:7.416198
旋转
运算:将空间中一个点,绕原点旋转的角度
代码:以二维坐标点(2,0)绕原点旋转45°为例。代码有点问题,一释放内存就出错,具体原因是对两个向量开辟空间以后又让它们指向了别的地址,造成了开辟空间无用。所以调用Cblas
函数,可以直接把指向数组的指针丢进去。暂时先这样理解吧,等把C++复习一遍再来看看分析的对不对。
//旋转,以二维空间中的一个点(2,0)绕原点旋转45°
#include<stdio.h>
#include<stdlib.h>
#include<mkl.h>
#include<math.h>
#define M_PI 3.14159265358979323846int main()
{float *A, *B;//A是坐标点,B是旋转矩阵float point1[] = { 2 };//旋转点x坐标float point2[] = { 0 };//旋转点y坐标float rotpoint[] = { cos(45.0*M_PI / 180), sin(45.0*M_PI / 180) };//A = (float *)mkl_malloc(1 * sizeof(float), 64);//B = (float *)mkl_malloc(1 * sizeof(float), 64);A = point1;B = point2;printf("The point is (%2.0f,%2.0f)",point1[0],point2[0]);printf("\n");//计算旋转后的点cblas_srot(1, A, 1, B, 1, rotpoint[0], rotpoint[1]);printf("The rotated is (%2.6f,%2.6f)", A[0], B[0]);printf("\n");//mkl_free(A);//mkl_free(B);getchar();return 0;
}
结果:还是比较正确的
The point is ( 2, 0)
The rotated is (1.414214,-1.414214)
缩放
运算
代码
//计算向量缩放
#include<stdio.h>
#include<stdlib.h>
#include<mkl.h>
void main()
{float *A;int n = 5;float scal=0.1;A = (float *)mkl_malloc(n*sizeof(float), 64);printf("The original vector:\n");for (int i = 0; i < n; i++){A[i] = i + 1;printf("%2.0f ", A[i]);}printf("\n");cblas_sscal(n, scal, A, 1);//缩放printf("The scaled vector:\n");for (int i = 0; i < n; i++){printf("%2.1f ", A[i]);}mkl_free(A);getchar();
}
结果
The original vector:1 2 3 4 5
The scaled vector:
0.1 0.2 0.3 0.4 0.5
交换
运算:交换两个向量
代码
//交换
#include<stdio.h>
#include<stdlib.h>
#include<mkl.h>int main()
{float *A, *B;//两个向量int a = 1, b = 1;//标量int n = 5;//向量大小A = (float *)mkl_malloc(n * 1 * sizeof(float), 64);B = (float *)mkl_malloc(n * 1 * sizeof(float), 64);printf("The 1st vector is ");for (int i = 0; i < n; i++){A[i] = i;printf("%2.0f", A[i]);}printf("\n");printf("The 2st vector is ");for (int i = 0; i < n; i++){B[i] = i + 1;printf("%2.0f", B[i]);}printf("\n");//交换ABcblas_sswap(n, A, 1, B, 1);printf("The 1st swapped vctor is");for (int i = 0; i < n; i++){printf("%2.0f", A[i]);}printf("\n");printf("The 2st swapped vctor is");for (int i = 0; i < n; i++){printf("%2.0f", B[i]);}printf("\n");mkl_free(A);mkl_free(B);getchar();return 0;
}
结果
The 1st vector is 0 1 2 3 4
The 2st vector is 1 2 3 4 5
The 1st swapped vctor is 1 2 3 4 5
The 2st swapped vctor is 0 1 2 3 4
最值
运算:求最大最小值
代码
//求最大最小值
#include<stdlib.h>
#include<stdio.h>
#include<mkl.h>
void main()
{float *A;//向量int n = 5;//向量大小int max, min;A = (float *)mkl_malloc(n * 1 * sizeof(float), 64);printf("The 1st vector is ");for (int i = 0; i < n; i++){A[i] = i;printf("%2.0f", A[i]);}printf("\n");//计算最值位置max=cblas_isamax(n, A, 1);min = cblas_isamin(n, A, 1);printf("The max value is %2.0f, position is %d\n", A[max], max + 1);printf("The min value is %2.0f, position is %d\n", A[min], min + 1);mkl_free(A);getchar();
}
结果
The 1st vector is 0 1 2 3 4
The max value is 4, position is 5
The min value is 0, position is 1
矩阵-向量
乘法1
运算:
代码
//矩阵-向量乘积
#include<stdio.h>
#include<stdlib.h>
#include<mkl.h>
int main()
{float *A, *B,*C;//A是矩阵,B是向量,C是向量int m = 2;//矩阵行数int n = 5;//向量维度,矩阵列数int a = 1, b = 1;//缩放因子A = (float *)mkl_malloc(m*n*sizeof(float), 64);B = (float *)mkl_malloc(n*sizeof(float), 64);C = (float *)mkl_malloc(m*sizeof(float), 64);//赋值,按行存储?printf("数组为\n");for (int i = 0; i < m*n; i++){if (i%n == 0 && i != 0)printf("\n");A[i] = i;printf("%2.0f",A[i]);} printf("\n");printf("向量为\n");for (int i = 0; i < n; i++){B[i] = i + 1;printf("%2.0f", B[i]);}printf("\n");for (int i = 0; i < m*n; i++)C[i] = 0;//2*5的矩阵与5*1的向量相乘cblas_sgemv(CblasRowMajor, CblasNoTrans, m, n, a, A, n, B, 1, b, C, 1);printf("矩阵-向量乘法结果\n");for (int i = 0; i < m; i++){printf("%2.0f ", C[i]);}mkl_free(A);mkl_free(B);mkl_free(C);getchar();return 0;
}
结果
数组为0 1 2 3 45 6 7 8 9
向量为1 2 3 4 5
矩阵-向量乘法结果
40 115
乘法2
运算
代码
//矩阵-向量乘积
#include<stdio.h>
#include<stdlib.h>
#include<mkl.h>
int main()
{float *A, *B, *C;//A是矩阵,B是向量int m=2,n = 5;//B,C向量维度int a = 1;//缩放因子A = (float *)mkl_malloc(m*n*sizeof(float), 64);B = (float *)mkl_malloc(m*sizeof(float), 64);C = (float *)mkl_malloc(n*sizeof(float), 64);//赋值,按行存储?printf("数组为\n");for (int i = 0; i < m*n; i++){if (i%n == 0 && i != 0)printf("\n");A[i] = 1;printf("%2.0f", A[i]);}printf("\n");printf("向量1为\n");for (int i = 0; i < m; i++){B[i] = i + 1;printf("%2.0f", B[i]);}printf("\n");printf("向量2为\n");for (int i = 0; i < n; i++){C[i] = i+2;printf("%2.0f", C[i]);}printf("\n");//5*1向量乘以1*5向量,加上5*5矩阵cblas_sger(CblasRowMajor, m, n, a, B, 1, C, 1, A, n);printf("向量-向量相乘+矩阵的结果\n");for (int i = 0; i < m*n; i++){if (i%n == 0 && i != 0)printf("\n");printf("%2.0f ", A[i]);}mkl_free(A);mkl_free(B);mkl_free(C);getchar();return 0;
}
结果
数组为1 1 1 1 11 1 1 1 1
向量1为1 2
向量2为2 3 4 5 6
向量-向量相乘+矩阵的结果3 4 5 6 75 7 9 11 13
矩阵-矩阵
乘法1
运算
代码
#include<stdio.h>
#include<stdlib.h>
#include<mkl.h>
int main()
{float *A, *B, *C;int m = 2, n = 3, k = 2;//A维度2*3,B维度2*3(计算时候转置),C维度2*2int a = 1, b = 1;//缩放因子A = (float *)mkl_malloc(m*n*sizeof(float), 64);B = (float *)mkl_malloc(n*k*sizeof(float), 64);C = (float *)mkl_malloc(m*k*sizeof(float), 64);printf("矩阵1为\n");for (int i = 0; i < m*n; i++){if (i != 0 && i%n == 0)printf("\n");A[i] = i + 1;printf("%2.0f", A[i]);}printf("\n");printf("矩阵2为\n");for (int i = 0; i < n*k; i++){if (i != 0 && i%k == 0)printf("\n");B[i] = 1;printf("%2.0f", B[i]);}printf("\n");printf("矩阵3为\n");for (int i = 0; i < m*k; i++){if (i != 0 && i%k == 0)printf("\n");C[i] = i;printf("%2.0f", C[i]);}printf("\n");printf("结果矩阵\n");cblas_sgemm(CblasRowMajor, CblasNoTrans, CblasNoTrans, m, k, n, a, A, n, B, k, b, C, k);//注意mkn的顺序☆for (int i = 0; i < m*k; i++){if (i != 0 && i%k == 0)printf("\n");printf("%2.0f", C[i]);}printf("\n");mkl_free(A);mkl_free(B);mkl_free(C);getchar();return 0;
}
结果
矩阵1为1 2 34 5 6
矩阵2为1 11 11 1
矩阵3为0 12 3
结果矩阵6 7
1718
乘法2
依旧是上述的功能,此处尝试一下
#include<stdio.h>
#include<stdlib.h>
#include<mkl.h>
int main()
{float *A, *B, *C;int m = 2, n = 3, k = 2;//A维度2*3,B维度2*3(计算时候转置),C维度2*2int a = 1, b = 1;//缩放因子A = (float *)mkl_malloc(m*n*sizeof(float), 64);B = (float *)mkl_malloc(k*n*sizeof(float), 64);C = (float *)mkl_malloc(m*k*sizeof(float), 64);printf("矩阵1为\n");for (int i = 0; i < m*n; i++){if (i != 0 && i%n == 0)printf("\n");A[i] = i + 1;printf("%2.0f", A[i]);}printf("\n");printf("矩阵2为\n");for (int i = 0; i < k*n; i++){if (i != 0 && i%n == 0)printf("\n");B[i] = 1;printf("%2.0f", B[i]);}printf("\n");printf("矩阵3为\n");for (int i = 0; i < m*k; i++){if (i != 0 && i%k == 0)printf("\n");C[i] = i;printf("%2.0f", C[i]);}printf("\n");printf("结果矩阵\n");cblas_sgemm(CblasRowMajor, CblasNoTrans, CblasTrans, m, k, n, a, A, n, B, n, b, C, k);//注意mkn的顺序☆for (int i = 0; i < m*k; i++){if (i != 0 && i%k == 0)printf("\n");printf("%2.0f", C[i]);}printf("\n");mkl_free(A);mkl_free(B);mkl_free(C);getchar();return 0;}
结果
矩阵1为1 2 34 5 6
矩阵2为1 1 11 1 1
矩阵3为0 12 3
结果矩阵6 7
1718
注意点
最主要的是记住参数顺序,首先是矩阵op(A)和的C行数,op代表操作,乘法2中的op就是转置;然后是矩阵op(B)和C的列数;随后才是
而且丢入函数的参数,并不一定是mkl_malloc
开辟的空间,也可以是其它数组,用指针指向数组地址,然后丢到函数就行了。
奉上code(vs2013): MKL -C++基本操作