matrix.c 3.7 KB

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