matrix.c 4.1 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168
  1. /*
  2. * Original program and various modifications:
  3. * Lubos Mitas
  4. *
  5. * GRASS4.1 version of the program and GRASS4.2 modifications:
  6. * H. Mitasova,
  7. * I. Kosinovsky, D. Gerdes
  8. * D. McCauley
  9. *
  10. * Copyright 1993, 1995:
  11. * L. Mitas ,
  12. * H. Mitasova ,
  13. * I. Kosinovsky,
  14. * D.Gerdes
  15. * D. McCauley
  16. *
  17. * modified by McCauley in August 1995
  18. * modified by Mitasova in August 1995, Nov. 1996
  19. */
  20. #include <stdio.h>
  21. #include <math.h>
  22. #include <unistd.h>
  23. #include <grass/gis.h>
  24. #include <grass/interpf.h>
  25. #include <grass/gmath.h>
  26. /*!
  27. * \brief Creates system of linear equations from interpolated points
  28. *
  29. * Creates system of linear equations represented by matrix using given
  30. * points and interpolating function interp()
  31. *
  32. * \param params struct interp_params *
  33. * \param points struct triple * : points for interpolation
  34. * \param n_points int : number of points
  35. * \param matrix double **
  36. *
  37. * \return -1 on failure, 1 on success
  38. */
  39. int IL_matrix_create(struct interp_params *params,
  40. struct triple *points, /* points for interpolation */
  41. int n_points, /* number of points */
  42. double **matrix, /* matrix */
  43. int *indx)
  44. /*
  45. Creates system of linear equations represented by matrix using given points
  46. and interpolating function interp()
  47. */
  48. {
  49. double xx, yy;
  50. double rfsta2, r;
  51. double d;
  52. int n1, k1, k2, k, i1, l, m, i, j;
  53. double fstar2 = params->fi * params->fi / 4.;
  54. static double *A = NULL;
  55. double RO, amaxa;
  56. double rsin = 0, rcos = 0, teta, scale = 0; /*anisotropy parameters - added by JH 2002 */
  57. double xxr, yyr;
  58. if (params->theta) {
  59. teta = params->theta * (M_PI / 180); /* deg to rad */
  60. rsin = sin(teta);
  61. rcos = cos(teta);
  62. }
  63. if (params->scalex)
  64. scale = params->scalex;
  65. n1 = n_points + 1;
  66. if (!A) {
  67. if (!
  68. (A =
  69. G_alloc_vector((params->KMAX2 + 2) * (params->KMAX2 + 2) + 1))) {
  70. fprintf(stderr, "Cannot allocate memory for A\n");
  71. return -1;
  72. }
  73. }
  74. /*
  75. C GENERATION OF MATRIX
  76. C FIRST COLUMN
  77. */
  78. A[1] = 0.;
  79. for (k = 1; k <= n_points; k++) {
  80. i1 = k + 1;
  81. A[i1] = 1.;
  82. }
  83. /*
  84. C OTHER COLUMNS
  85. */
  86. RO = -params->rsm;
  87. /* fprintf (stderr, "sm[%d] = %f, ro=%f\n", 1, points[1].smooth, RO); */
  88. for (k = 1; k <= n_points; k++) {
  89. k1 = k * n1 + 1;
  90. k2 = k + 1;
  91. i1 = k1 + k;
  92. if (params->rsm < 0.) { /*indicates variable smoothing */
  93. A[i1] = -points[k - 1].sm; /* added by Mitasova nov. 96 */
  94. /* G_debug(5, "sm[%d]=%f, a=%f", k, points[k-1].sm, A[i1]); */
  95. }
  96. else {
  97. A[i1] = RO; /* constant smoothing */
  98. }
  99. /* if (i1 == 100) fprintf (stderr,i "A[%d] = %f\n", i1, A[i1]); */
  100. /* A[i1] = RO; */
  101. for (l = k2; l <= n_points; l++) {
  102. xx = points[k - 1].x - points[l - 1].x;
  103. yy = points[k - 1].y - points[l - 1].y;
  104. if ((params->theta) && (params->scalex)) {
  105. /* re run anisotropy */
  106. xxr = xx * rcos + yy * rsin;
  107. yyr = yy * rcos - xx * rsin;
  108. xx = xxr;
  109. yy = yyr;
  110. r = scale * xx * xx + yy * yy;
  111. rfsta2 = fstar2 * (scale * xx * xx + yy * yy);
  112. }
  113. else {
  114. r = xx * xx + yy * yy;
  115. rfsta2 = fstar2 * (xx * xx + yy * yy);
  116. }
  117. if (rfsta2 == 0.) {
  118. fprintf(stderr, "ident. points in segm.\n");
  119. fprintf(stderr, "x[%d]=%f, x[%d]=%f, y[%d]=%f, y[%d]=%f\n",
  120. k - 1, points[k - 1].x, l - 1, points[l - 1].x, k - 1,
  121. points[k - 1].y, l - 1, points[l - 1].y);
  122. return -1;
  123. }
  124. i1 = k1 + l;
  125. A[i1] = params->interp(r, params->fi);
  126. }
  127. }
  128. /* C SYMMETRISATION */
  129. amaxa = 1.;
  130. for (k = 1; k <= n1; k++) {
  131. k1 = (k - 1) * n1;
  132. k2 = k + 1;
  133. for (l = k2; l <= n1; l++) {
  134. m = (l - 1) * n1 + k;
  135. A[m] = A[k1 + l];
  136. amaxa = amax1(A[m], amaxa);
  137. }
  138. }
  139. m = 0;
  140. for (i = 0; i <= n_points; i++) {
  141. for (j = 0; j <= n_points; j++) {
  142. m++;
  143. matrix[i][j] = A[m];
  144. }
  145. }
  146. G_debug(3, "calling G_ludcmp() n=%d indx=%d", n_points, *indx);
  147. if (G_ludcmp(matrix, n_points + 1, indx, &d) <= 0) {
  148. /* find the inverse of the matrix */
  149. fprintf(stderr, "G_ludcmp() failed! n=%d d=%.2f\n", n_points, d);
  150. return -1;
  151. }
  152. /* G_free_vector(A); */
  153. return 1;
  154. }