You can not select more than 25 topics Topics must start with a chinese character,a letter or number, can include dashes ('-') and can be up to 35 characters long.

util.c 4.1 kB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116
  1. #include "util.h"
  2. #include <stdlib.h>
  3. #include <time.h>
  4. #include <math.h>
  5. #define MAX(a, b) ((a) > (b) ? (a) : (b))
  6. #define MIN(a, b) ((a) < (b) ? (a) : (b))
  7. ///////////////////////
  8. // matrix generation //
  9. ///////////////////////
  10. // Each routine x2matgen is passed the size (m, n) of the desired matrix and
  11. // geneartes two copies of such a matrix in in its output arguments A and B.
  12. // The generated matrices is filled with random entries in [0, 1[ (+i*[0, 1[ in
  13. // the complex case). Then m is added to the diagonal; this is numerically
  14. // favorable for routines working with triangular and symmetric matrices. For
  15. // the same reason the imaginary part of the diagonal is set to 0.
  16. void s2matgen(const int m, const int n, float *A, float *B) {
  17. srand(time(NULL) + (size_t) A);
  18. int i, j;
  19. for (i = 0; i < m; i++)
  20. for (j = 0; j < n; j++)
  21. A[i + m * j] = B[i + m * j] = (float) rand() / RAND_MAX + m * (i == j);
  22. }
  23. void d2matgen(const int m, const int n, double *A, double *B) {
  24. srand(time(NULL) + (size_t) A);
  25. int i, j;
  26. for (i = 0; i < m; i++)
  27. for (j = 0; j < n; j++)
  28. A[i + m * j] = B[i + m * j] = (double) rand() / RAND_MAX + m * (i == j);
  29. }
  30. void c2matgen(const int m, const int n, float *A, float *B) {
  31. srand(time(NULL) + (size_t) A);
  32. int i, j;
  33. for (i = 0; i < m; i++)
  34. for (j = 0; j < n; j++) {
  35. A[2* (i + m * j)] = B[2 * (i + m * j)] = (float) rand() / RAND_MAX + m * (i == j);
  36. A[2* (i + m * j) + 1] = B[2 * (i + m * j) + 1] = ((float) rand() / RAND_MAX) * (i != j);
  37. }
  38. }
  39. void z2matgen(const int m, const int n, double *A, double *B) {
  40. srand(time(NULL) + (size_t) A);
  41. int i, j;
  42. for (i = 0; i < m; i++)
  43. for (j = 0; j < n; j++) {
  44. A[2* (i + m * j)] = B[2 * (i + m * j)] = (double) rand() / RAND_MAX + m * (i == j);
  45. A[2* (i + m * j) + 1] = B[2 * (i + m * j) + 1] = ((double) rand() / RAND_MAX) * (i != j);
  46. }
  47. }
  48. ////////////////////////
  49. // error computations //
  50. ////////////////////////
  51. // Each routine x2vecerrr is passed a vector lengh n and two vectors x and y.
  52. // It returns the maximum of the element-wise error between these two vectors.
  53. // This error is the minimum of the absolute difference and the relative
  54. // differene with respect to y.
  55. double i2vecerr(const int n, const int *x, const int *y) {
  56. double error = 0;
  57. int i;
  58. for (i = 0; i < n; i++) {
  59. double nom = abs(x[i] - y[i]);
  60. double den = abs(y[i]);
  61. error = MAX(error, (den > 0) ? MIN(nom, nom / den) : nom);
  62. }
  63. return error;
  64. }
  65. double s2vecerr(const int n, const float *x, const float *y) {
  66. float error = 0;
  67. int i;
  68. for (i = 0; i < n; i++) {
  69. double nom = fabs((double) x[i] - y[i]);
  70. double den = fabs(y[i]);
  71. error = MAX(error, (den > 0) ? MIN(nom, nom / den) : nom);
  72. }
  73. return error;
  74. }
  75. double d2vecerr(const int n, const double *x, const double *y) {
  76. double error = 0;
  77. int i;
  78. for (i = 0; i < n; i++) {
  79. double nom = fabs(x[i] - y[i]);
  80. double den = fabs(y[i]);
  81. error = MAX(error, (den > 0) ? MIN(nom, nom / den) : nom);
  82. }
  83. return error;
  84. }
  85. double c2vecerr(const int n, const float *x, const float *y) {
  86. double error = 0;
  87. int i;
  88. for (i = 0; i < n; i++) {
  89. double nom = sqrt(((double) x[2 * i] - y[2 * i]) * ((double) x[2 * i] - y[2 * i]) + ((double) x[2 * i + 1] - y[2 * i + 1]) * ((double) x[2 * i + 1] - y[2 * i + 1]));
  90. double den = sqrt((double) y[2 * i] * y[2 * i] + (double) y[2 * i + 1] * y[2 * i + 1]);
  91. error = MAX(error, (den > 0) ? MIN(nom, nom / den) : nom);
  92. }
  93. return error;
  94. }
  95. double z2vecerr(const int n, const double *x, const double *y) {
  96. double error = 0;
  97. int i;
  98. for (i = 0; i < n; i++) {
  99. double nom = sqrt((x[2 * i] - y[2 * i]) * (x[2 * i] - y[2 * i]) + (x[2 * i + 1] - y[2 * i + 1]) * (x[2 * i + 1] - y[2 * i + 1]));
  100. double den = sqrt(y[2 * i] * y[2 * i] + y[2 * i + 1] * y[2 * i + 1]);
  101. error = MAX(error, (den > 0) ? MIN(nom, nom / den) : nom);
  102. }
  103. return error;
  104. }