denseBlas.c 5.6 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251
  1. #include "denseBlas.h"
  2. /* 创造一个矩阵,分配新的矩阵空间 */
  3. demat* createDeMat(int m, int n) // m行,n列,nnz个非零元素
  4. {
  5. int numEle;
  6. numEle = m * n;
  7. double* v = (double*)malloc(numEle * sizeof(double)); // 全部元素
  8. return assembleDeMat(m, n, v);
  9. }
  10. /* 利用已有空间组装一个矩阵 */
  11. demat* assembleDeMat(int m, int n, double* v) // m行,n列,v为元素,
  12. {
  13. demat* M = (demat*)malloc(sizeof(demat));
  14. M->m = m;
  15. M->n = n;
  16. M->v = v;
  17. return M;
  18. }
  19. /* 释放一个稠密矩阵空间 */
  20. void freeDeMat(demat* M, int level)
  21. {
  22. if (M->v) {
  23. free(M->v);
  24. }
  25. if (level == 2) {
  26. free(M);
  27. }
  28. }
  29. /* 2范数 */
  30. double cmscp_norm2(double* v, int n) {
  31. int i;
  32. double normsquare = 0;
  33. for (i = 0; i < n; i++) {
  34. normsquare += v[i] * v[i];
  35. }
  36. return sqrt(normsquare);
  37. }
  38. /* 稠密矩阵与矩阵相乘 */
  39. void denseMM(demat* matOut, demat* matLeftIn, demat* matRightIn) {
  40. /* 变量类型 */
  41. int mMatOut, nMatOut, mMatLeftIn, nMatLeftIn, mMatRightIn, nMatRightIn, i, j, k, p1, p2, p3;
  42. double* vMatOut, * vMatLeftIn, * vMatRightIn;
  43. /* 维数信息 */
  44. mMatOut = matLeftIn->m;
  45. nMatOut = matRightIn->n;
  46. mMatLeftIn = matLeftIn->m;
  47. nMatLeftIn = matLeftIn->n;
  48. mMatRightIn = matRightIn->m;
  49. nMatRightIn = matRightIn->n;
  50. /* 矩阵元素 */
  51. vMatOut = matOut->v;
  52. vMatLeftIn = matLeftIn->v;
  53. vMatRightIn = matRightIn->v;
  54. /* 运算 */
  55. for (i = 0; i < mMatOut; i++) {
  56. for (j = 0; j < nMatOut; j++) {
  57. p1 = i * mMatOut + j; // matOut中第(i,j)元素在向量存储格式中的位置
  58. vMatOut[p1] = 0;
  59. for (k = 0; k < nMatLeftIn; k++) {
  60. p2 = i * mMatLeftIn + k; // matLeftIn中第(i,k)元素在向量存储格式中的位置
  61. p3 = k * mMatRightIn + j; // matRightIn中第(k,j)元素在向量存储格式中的位置
  62. vMatOut[p1] += vMatLeftIn[p2] * vMatRightIn[p3];
  63. }
  64. }
  65. }
  66. }
  67. /* 稠密矩阵转置与矩阵相乘 */
  68. void denseMtM(demat* matOut, demat* matLeftIn, demat* matRightIn) {
  69. /* 变量类型 */
  70. int mMatOut, nMatOut, mMatLeftIn, nMatLeftIn, mMatRightIn, nMatRightIn, i, j, k, p1, p2, p3;
  71. double* vMatOut, * vMatLeftIn, * vMatRightIn;
  72. /* 维数信息 */
  73. mMatOut = matLeftIn->m;
  74. nMatOut = matRightIn->n;
  75. mMatLeftIn = matLeftIn->m;
  76. nMatLeftIn = matLeftIn->n;
  77. mMatRightIn = matRightIn->m;
  78. nMatRightIn = matRightIn->n;
  79. /* 矩阵元素 */
  80. vMatOut = matOut->v;
  81. vMatLeftIn = matLeftIn->v;
  82. vMatRightIn = matRightIn->v;
  83. /* 运算 */
  84. for (i = 0; i < mMatOut; i++) {
  85. for (j = 0; j < nMatOut; j++) {
  86. p1 = i * mMatOut + j; // matOut中第(i,j)元素在向量存储格式中的位置
  87. vMatOut[p1] = 0;
  88. for (k = 0; k < nMatLeftIn; k++) {
  89. p2 = k * mMatLeftIn + i; // matLeftIn转置矩阵中第(i,k)元素在向量存储格式中的位置
  90. p3 = k * mMatRightIn + j; // matRightIn中第(k,j)元素在向量存储格式中的位置
  91. vMatOut[p1] += vMatLeftIn[p2] * vMatRightIn[p3];
  92. }
  93. }
  94. }
  95. }
  96. /* 稠密矩阵与矩阵转置相乘 */
  97. void denseMMt(demat* matOut, demat* matLeftIn, demat* matRightIn) {
  98. /* 变量类型 */
  99. int mMatOut, nMatOut, mMatLeftIn, nMatLeftIn, mMatRightIn, nMatRightIn, i, j, k, p1, p2, p3;
  100. double* vMatOut, * vMatLeftIn, * vMatRightIn;
  101. /* 维数信息 */
  102. mMatOut = matLeftIn->m;
  103. nMatOut = matRightIn->n;
  104. mMatLeftIn = matLeftIn->m;
  105. nMatLeftIn = matLeftIn->n;
  106. mMatRightIn = matRightIn->m;
  107. nMatRightIn = matRightIn->n;
  108. /* 矩阵元素 */
  109. vMatOut = matOut->v;
  110. vMatLeftIn = matLeftIn->v;
  111. vMatRightIn = matRightIn->v;
  112. /* 运算 */
  113. for (i = 0; i < mMatOut; i++) {
  114. for (j = 0; j < nMatOut; j++) {
  115. p1 = i * mMatOut + j; // matOut中第(i,j)元素在向量存储格式中的位置
  116. vMatOut[p1] = 0;
  117. for (k = 0; k < nMatLeftIn; k++) {
  118. p2 = i * mMatLeftIn + k; // matLeftIn矩阵中第(i,k)元素在向量存储格式中的位置
  119. p3 = j * mMatRightIn + k; // matRightIn转置中第(k,j)元素在向量存储格式中的位置
  120. vMatOut[p1] += vMatLeftIn[p2] * vMatRightIn[p3];
  121. }
  122. }
  123. }
  124. }
  125. /* 稠密矩阵与向量相乘 */
  126. void denseMV(double* vecOut, demat* matLeftIn, double* vecRightIn, int sign, int newVecOut) {
  127. /* 变量类型 */
  128. int mMatLeftIn, nMatLeftIn, i, j, p;
  129. double* vMatLeftIn;
  130. /* 维数信息 */
  131. mMatLeftIn = matLeftIn->m;
  132. nMatLeftIn = matLeftIn->n;
  133. /* 输入矩阵元素 */
  134. vMatLeftIn = matLeftIn->v;
  135. if (newVecOut == 1) //
  136. {
  137. for (i = 0; i < mMatLeftIn; i++)
  138. {
  139. vecOut[i] = 0;
  140. }
  141. }
  142. if (sign == 1) // 加法
  143. {
  144. for (i = 0; i < mMatLeftIn; i++)
  145. {
  146. for (j = 0; j < nMatLeftIn; j++)
  147. {
  148. p = i * mMatLeftIn + j;
  149. vecOut[i] += vMatLeftIn[p] * vecRightIn[j];
  150. }
  151. }
  152. }
  153. else // 减法
  154. {
  155. for (i = 0; i < mMatLeftIn; i++)
  156. {
  157. for (j = 0; j < nMatLeftIn; j++)
  158. {
  159. p = i * mMatLeftIn + j;
  160. vecOut[i] -= vMatLeftIn[p] * vecRightIn[j];
  161. }
  162. }
  163. }
  164. }
  165. /* 稠密矩阵与向量相乘 */
  166. void denseMtV(double* vecOut, demat* matLeftIn, double* vecRightIn, int sign, int newVecOut) {
  167. /* 变量类型 */
  168. int mMatLeftIn, nMatLeftIn, i, j, p;
  169. double* vMatLeftIn;
  170. /* 维数信息 */
  171. mMatLeftIn = matLeftIn->m;
  172. nMatLeftIn = matLeftIn->n;
  173. /* 输入矩阵元素 */
  174. vMatLeftIn = matLeftIn->v;
  175. if (newVecOut == 1) //
  176. {
  177. for (j = 0; j < mMatLeftIn; j++)
  178. {
  179. vecOut[j] = 0;
  180. }
  181. }
  182. if (sign == 1) // 加法
  183. {
  184. for (i = 0; i < mMatLeftIn; i++)
  185. {
  186. for (j = 0; j < nMatLeftIn; j++)
  187. {
  188. p = i * mMatLeftIn + j; // matLeftIn的转置矩阵的(j,i)元素
  189. vecOut[j] += vMatLeftIn[p] * vecRightIn[i];
  190. }
  191. }
  192. }
  193. else // 减法
  194. {
  195. for (i = 0; i < mMatLeftIn; i++)
  196. {
  197. for (j = 0; j < nMatLeftIn; j++)
  198. {
  199. p = i * mMatLeftIn + j; // matLeftIn的转置矩阵的(j,i)元素
  200. vecOut[j] -= vMatLeftIn[p] * vecRightIn[i];
  201. }
  202. }
  203. }
  204. }
  205. /* 常值与向量相乘 */
  206. void denseScaV(double* vecOut, double scaIn, double* vecIn, int length) {
  207. /* 变量类型 */
  208. int i;
  209. for (i = 0; i < length; i++)
  210. {
  211. vecOut[i] = scaIn * vecIn[i];
  212. }
  213. }
  214. /* 常值与向量相加 */
  215. void denseScaPlusVec(double* vecOut, double scaIn, double* vecIn, int length) {
  216. /* 变量类型 */
  217. int i;
  218. for (i = 0; i < length; i++)
  219. {
  220. vecOut[i] = scaIn + vecIn[i];
  221. }
  222. }