/tests/c/api/test_c_QcMatValues.c

https://gitlab.com/miro.ilias/qcmatrix · C · 245 lines · 200 code · 2 blank · 43 comment · 32 complexity · a1d1c6d98d286dea443c464737da320c MD5 · raw file

  1. /* QcMatrix: an abstract matrix library
  2. Copyright 2012-2015 Bin Gao
  3. QcMatrix is free software: you can redistribute it and/or modify
  4. it under the terms of the GNU Lesser General Public License as published by
  5. the Free Software Foundation, either version 3 of the License, or
  6. (at your option) any later version.
  7. QcMatrix is distributed in the hope that it will be useful,
  8. but WITHOUT ANY WARRANTY; without even the implied warranty of
  9. MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
  10. GNU Lesser General Public License for more details.
  11. You should have received a copy of the GNU Lesser General Public License
  12. along with QcMatrix. If not, see <http://www.gnu.org/licenses/>.
  13. This file tests the functions QcMatSetValues(), QcMatAddValues() and QcMatGetValues().
  14. 2014-08-22, Bin Gao:
  15. * adds the test of QcMatAddValues()
  16. 2014-03-25, Bin Gao:
  17. * first version
  18. */
  19. /* header file of QcMatrix library */
  20. #include "qcmatrix.h"
  21. /* parameters for test suite */
  22. #include "tests/qcmatrix_test_param.h"
  23. /*% \brief tests the functions QcMatSetValues(), QcMatAddValues() and QcMatGetValues()
  24. \author Bin Gao
  25. \date 2014-03-25
  26. \param[QInt]{in} dim_block the dimension of blocks
  27. */
  28. QVoid test_c_QcMatValues(const QInt dim_block)
  29. {
  30. QcMat A; /* matrix for test */
  31. QInt num_blocks; /* number of blocks, as \var{dim_block}*\var{dim_block} */
  32. QInt *idx_block_row; /* row indices of the blocks */
  33. QInt *idx_block_col; /* column indices of the blocks */
  34. QcDataType *data_type; /* data types of the blocks */
  35. const QInt dim_mat=6; /* dimension of each block */
  36. QInt size_mat; /* number of elements in each block */
  37. QInt idx_first_row; /* index of the first row from which the values are set/added */
  38. QInt num_row_set=dim_mat; /* number of rows to set/add */
  39. QInt idx_first_col; /* index of the first column from which the values are set/added */
  40. QInt num_col_set=dim_mat; /* number of columns to set/add */
  41. QReal *values_real; /* values of the real part */
  42. QBool is_equal; /* indicates if the matrix and array have the same values */
  43. QInt iblk, jblk, kblk; /* incremental recorders for blocks */
  44. QErrorCode ierr; /* error information */
  45. ierr = QcMatCreate(&A);
  46. if (ierr==QSUCCESS) {
  47. printf("test_c_QcMatValues>> QcMatCreate(A) passed ...\n");
  48. }
  49. else {
  50. printf("test_c_QcMatValues>> failed to call QcMatCreate(A)\n");
  51. exit(ierr);
  52. }
  53. ierr = QcMatBlockCreate(&A, dim_block);
  54. if (ierr==QSUCCESS) {
  55. printf("test_c_QcMatValues>> QcMatBlockCreate(A) passed ...\n");
  56. }
  57. else {
  58. printf("test_c_QcMatValues>> failed to call QcMatBlockCreate(A)\n");
  59. exit(ierr);
  60. }
  61. /* sets the data types of the blocks */
  62. num_blocks = dim_block*dim_block;
  63. idx_block_row = (QInt *)malloc(sizeof(QInt)*num_blocks);
  64. if (idx_block_row==NULL) {
  65. printf("test_c_QcMatValues>> failed to allocate idx_block_row\n");
  66. exit(QFAILURE);
  67. }
  68. idx_block_col = (QInt *)malloc(sizeof(QInt)*num_blocks);
  69. if (idx_block_col==NULL) {
  70. printf("test_c_QcMatValues>> failed to allocate idx_block_col\n");
  71. exit(QFAILURE);
  72. }
  73. data_type = (QcDataType *)malloc(sizeof(QcDataType)*num_blocks);
  74. if (data_type==NULL) {
  75. printf("test_c_QcMatValues>> failed to allocate data_type\n");
  76. exit(QFAILURE);
  77. }
  78. kblk = 0;
  79. #if defined(QCMATRIX_ZERO_BASED)
  80. for (iblk=0; iblk<dim_block; iblk++) {
  81. for (jblk=0; jblk<dim_block; jblk++) {
  82. #else
  83. for (iblk=1; iblk<=dim_block; iblk++) {
  84. for (jblk=1; jblk<=dim_block; jblk++) {
  85. #endif
  86. /* QcMatrix uses row major order for the blocks */
  87. idx_block_row[kblk] = iblk;
  88. idx_block_col[kblk] = jblk;
  89. data_type[kblk++] = QCMPLXMAT;
  90. }
  91. }
  92. ierr = QcMatSetDataType(&A, num_blocks, idx_block_row, idx_block_col, data_type);
  93. if (ierr!=QSUCCESS) {
  94. printf("test_c_QcMatValues>> failed to call QcMatSetDataType(A)\n");
  95. exit(ierr);
  96. }
  97. free(data_type);
  98. data_type = NULL;
  99. /* sets the dimension of each block */
  100. ierr = QcMatSetDimMat(&A, dim_mat, dim_mat);
  101. if (ierr!=QSUCCESS) {
  102. printf("test_c_QcMatValues>> failed to call QcMatSetDimMat(A)\n");
  103. exit(ierr);
  104. }
  105. /* assembles the matrix */
  106. ierr = QcMatAssemble(&A);
  107. if (ierr!=QSUCCESS) {
  108. printf("test_c_QcMatValues>> failed to call QcMatAssemble(A)\n");
  109. exit(ierr);
  110. }
  111. /* allocates memory for setting the elements of each block */
  112. size_mat = dim_mat*dim_mat;
  113. values_real = (QReal *)malloc(sizeof(QReal)*num_blocks*size_mat);
  114. if (values_real==NULL) {
  115. printf("test_c_QcMatValues>> failed to allocate values_real\n");
  116. exit(QFAILURE);
  117. }
  118. /* we sets all the elements as 1+i */
  119. for (iblk=0; iblk<num_blocks*size_mat; iblk++) {
  120. values_real[iblk] = 1;
  121. }
  122. /* sets the elements block by block */
  123. #if defined(QCMATRIX_ZERO_BASED)
  124. idx_first_row = 0;
  125. idx_first_col = 0;
  126. #else
  127. idx_first_row = 1;
  128. idx_first_col = 1;
  129. #endif
  130. for (iblk=0; iblk<num_blocks; iblk++) {
  131. ierr = QcMatSetValues(&A,
  132. idx_block_row[iblk],
  133. idx_block_col[iblk],
  134. idx_first_row,
  135. num_row_set,
  136. idx_first_col,
  137. num_col_set,
  138. values_real,
  139. values_real);
  140. if (ierr!=QSUCCESS) {
  141. printf("test_c_QcMatValues>> block (%"QINT_FMT", %"QINT_FMT")\n",
  142. idx_block_row[iblk],
  143. idx_block_col[iblk]);
  144. printf("test_c_QcMatValues>> failed to call QcMatSetValues(A)\n");
  145. exit(ierr);
  146. }
  147. }
  148. /* checks the elements set by QcMatSetValues() */
  149. ierr = QcMatCfArray(&A,
  150. QFALSE,
  151. num_blocks*size_mat,
  152. values_real,
  153. values_real,
  154. &is_equal);
  155. if (ierr==QSUCCESS) {
  156. if (is_equal==QTRUE) {
  157. printf("test_c_QcMatValues>> QcMatSetValues(A) passed ...\n");
  158. }
  159. else {
  160. #if defined(QCMATRIX_ENABLE_VIEW)
  161. ierr = QcMatWrite(&A, "QcMatValues_A", ASCII_VIEW);
  162. if (ierr!=QSUCCESS) {
  163. printf("test_c_QcMatValues>> failed to call QcMatWrite(A)\n");
  164. exit(ierr);
  165. }
  166. #endif
  167. printf("test_c_QcMatValues>> QcMatSetValues(A) failed\n");
  168. exit(is_equal);
  169. }
  170. }
  171. else {
  172. printf("test_c_QcMatValues>> failed to call QcMatCfArray(A)\n");
  173. exit(ierr);
  174. }
  175. /* adds values to the matrix */
  176. for (iblk=0; iblk<num_blocks; iblk++) {
  177. ierr = QcMatAddValues(&A,
  178. idx_block_row[iblk],
  179. idx_block_col[iblk],
  180. idx_first_row,
  181. num_row_set,
  182. idx_first_col,
  183. num_col_set,
  184. values_real,
  185. values_real);
  186. if (ierr!=QSUCCESS) {
  187. printf("test_c_QcMatValues>> block (%"QINT_FMT", %"QINT_FMT")\n",
  188. idx_block_row[iblk],
  189. idx_block_col[iblk]);
  190. printf("test_c_QcMatValues>> failed to call QcMatAddValues(A)\n");
  191. exit(ierr);
  192. }
  193. }
  194. /* all the elements become 2+2*i */
  195. for (iblk=0; iblk<num_blocks*size_mat; iblk++) {
  196. values_real[iblk] = 2;
  197. }
  198. /* checks the elements set by QcMatAddValues() */
  199. ierr = QcMatCfArray(&A,
  200. QFALSE,
  201. num_blocks*size_mat,
  202. values_real,
  203. values_real,
  204. &is_equal);
  205. if (ierr==QSUCCESS) {
  206. if (is_equal==QTRUE) {
  207. printf("test_c_QcMatValues>> QcMatAddValues(A) passed ...\n");
  208. }
  209. else {
  210. #if defined(QCMATRIX_ENABLE_VIEW)
  211. ierr = QcMatWrite(&A, "QcMatValues_A", ASCII_VIEW);
  212. if (ierr!=QSUCCESS) {
  213. printf("test_c_QcMatValues>> failed to call QcMatWrite(A)\n");
  214. exit(ierr);
  215. }
  216. #endif
  217. printf("test_c_QcMatValues>> QcMatAddValues(A) failed\n");
  218. exit(is_equal);
  219. }
  220. }
  221. else {
  222. printf("test_c_QcMatValues>> failed to call QcMatCfArray(A)\n");
  223. exit(ierr);
  224. }
  225. /* cleans up */
  226. free(idx_block_row);
  227. idx_block_row = NULL;
  228. free(idx_block_col);
  229. idx_block_col = NULL;
  230. free(values_real);
  231. values_real = NULL;
  232. ierr = QcMatDestroy(&A);
  233. if (ierr!=QSUCCESS) {
  234. printf("test_c_QcMatValues>> failed to call QcMatDestroy(A)\n");
  235. exit(ierr);
  236. }
  237. }