test_matrix_conversion.c 3.8 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168
  1. /*****************************************************************************
  2. *
  3. * MODULE: Grass PDE Numerical Library
  4. * AUTHOR(S): Soeren Gebbert, Berlin (GER) Dec 2006
  5. * soerengebbert <at> gmx <dot> de
  6. *
  7. * PURPOSE: Unit tests for les solving
  8. *
  9. * COPYRIGHT: (C) 2000 by the GRASS Development Team
  10. *
  11. * This program is free software under the GNU General Public
  12. * License (>=v2). Read the file COPYING that comes with GRASS
  13. * for details.
  14. *
  15. *****************************************************************************/
  16. #include <stdio.h>
  17. #include <stdlib.h>
  18. #include <string.h>
  19. #include <grass/glocale.h>
  20. #include <grass/gmath.h>
  21. #include "test_gmath_lib.h"
  22. #define EPSILON_DIRECT 1.0E-10
  23. #define EPSILON_ITER 1.0E-4
  24. /* prototypes */
  25. static int test_matrix_conversion(void);
  26. /* ************************************************************************* */
  27. /* Performe the solver unit tests ****************************************** */
  28. /* ************************************************************************* */
  29. int unit_test_matrix_conversion(void)
  30. {
  31. int sum = 0;
  32. G_message(_("\n++ Running matrix conversion unit tests ++"));
  33. sum += test_matrix_conversion();
  34. if (sum > 0)
  35. G_warning(_("\n-- Matrix conversion unit tests failure --"));
  36. else
  37. G_message(_("\n-- Matrix conversion unit tests finished successfully --"));
  38. return sum;
  39. }
  40. /* ************************************************************************* */
  41. /* ************************************************************************* */
  42. /* ************************************************************************* */
  43. static void print_matrix(double **A, int rows, int cols)
  44. {
  45. int i, j;
  46. for(i = 0; i < rows; i++) {
  47. for(j = 0; j < cols; j++) {
  48. printf("%g ", A[i][j]);
  49. }
  50. printf("\n");
  51. }
  52. }
  53. /* *************************************************************** */
  54. /* Test all implemented solvers for sparse and normal matrix *** */
  55. /* *************************************************************** */
  56. int test_matrix_conversion(void)
  57. {
  58. int sum = 0;
  59. int i, j;
  60. double val = 0.0;
  61. double **A;
  62. double **B;
  63. double **C;
  64. double **D;
  65. double **E;
  66. double **F;
  67. G_math_spvector **Asp;
  68. G_math_spvector **Asp2;
  69. A = G_alloc_matrix(5,5);
  70. F = G_alloc_matrix(5,5);
  71. G_message("\t * Creating symmetric matrix\n");
  72. A[0][0] = 8;
  73. A[1][1] = 7;
  74. A[2][2] = 6;
  75. A[3][3] = 5;
  76. A[4][4] = 4;
  77. A[0][1] = 3;
  78. A[0][2] = 0;
  79. A[0][3] = 1;
  80. A[0][4] = 0;
  81. A[1][2] = 3;
  82. A[1][3] = 0;
  83. A[1][4] = 1;
  84. A[2][3] = 3;
  85. A[3][4] = 3;
  86. for(i = 0; i < 5; i++)
  87. for(j = 0; j < 5; j++)
  88. A[j][i] = A[i][j];
  89. print_matrix(A, 5, 5);
  90. G_message("\t * Test matrix to band matrix conversion\n");
  91. B = G_math_matrix_to_sband_matrix(A, 5, 4);
  92. print_matrix(B, 5, 4);
  93. G_message("\t * Test matrix to sparse matrix conversion\n");
  94. Asp = G_math_A_to_Asp(A, 5, 0.0);
  95. G_math_print_spmatrix(Asp, 5);
  96. G_message("\t * Test sparse matrix to matrix conversion\n");
  97. C = G_math_Asp_to_A(Asp, 5);
  98. print_matrix(C, 5, 5);
  99. G_message("\t * Test sparse matrix to band matrix conversion\n");
  100. D = G_math_Asp_to_sband_matrix(Asp, 5, 4);
  101. print_matrix(D, 5, 4);
  102. /*Check the band matrix results*/
  103. G_math_d_aA_B(B, D, -1, F, 5, 4);
  104. G_math_d_asum_norm(F[0], &val, 20);
  105. if (val != 0)
  106. {
  107. G_warning("Error in band matrix conversion");
  108. sum++;
  109. }
  110. G_message("\t * Test band matrix to matrix conversion\n");
  111. E = G_math_sband_matrix_to_matrix(D, 5, 4);
  112. print_matrix(E, 5, 5);
  113. /*Check the matrix results*/
  114. G_math_d_aA_B(A, E, -1, F, 5, 5);
  115. G_math_d_asum_norm(F[0], &val, 25);
  116. if (val != 0)
  117. {
  118. G_warning("Error in matrix conversion");
  119. sum++;
  120. }
  121. G_message("\t * Test band matrix to sparse matrix conversion\n");
  122. Asp2 = G_math_sband_matrix_to_Asp(D, 5, 4, 0.0);
  123. G_math_print_spmatrix(Asp2, 5);
  124. return sum;
  125. }