matrix.c 1.4 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859606162636465666768697071727374757677787980818283
  1. #include <math.h>
  2. #include <grass/gis.h>
  3. #include <grass/gmath.h>
  4. #include "local_proto.h"
  5. int print_matrix(double **matrix, int bands)
  6. {
  7. int i, j;
  8. for (i = 0; i < bands; i++)
  9. {
  10. for (j = 0; j < bands; j++) {
  11. printf("%g ", matrix[i][j]);
  12. }
  13. printf("\n");
  14. }
  15. return 0;
  16. }
  17. int product(double *vector, double factor, double **matrix1,
  18. int bands)
  19. {
  20. int i, j;
  21. for (i = 0; i < bands; i++)
  22. for (j = 0; j < bands; j++) {
  23. matrix1[i][j] = (double)factor *(vector[i] * vector[j]);
  24. }
  25. return 0;
  26. }
  27. int setdiag(double *eigval, int bands, double **l)
  28. {
  29. int i, j;
  30. for (i = 0; i < bands; i++)
  31. for (j = 0; j < bands; j++)
  32. if (i == j)
  33. l[i][j] = eigval[i];
  34. else
  35. l[i][j] = 0.0;
  36. return 0;
  37. }
  38. int
  39. getsqrt(double **w, int bands, double **l, double **eigmat)
  40. {
  41. int i;
  42. double **tmp;
  43. tmp = G_alloc_matrix(bands, bands);
  44. for (i = 0; i < bands; i++)
  45. l[i][i] = 1.0 / sqrt(l[i][i]);
  46. G_math_d_AB(eigmat, l, tmp, bands, bands, bands);
  47. G_math_d_A_T(eigmat, bands);
  48. G_math_d_AB(tmp, eigmat, w, bands, bands, bands);
  49. G_free_matrix(tmp);
  50. return 0;
  51. }
  52. int solveq(double **q, int bands, double **w, double **p)
  53. {
  54. double **tmp;
  55. tmp = G_alloc_matrix(bands, bands);
  56. G_math_d_AB(w, p, tmp, bands, bands, bands);
  57. G_math_d_AB(tmp, w, q, bands, bands, bands);
  58. G_free_matrix(tmp);
  59. return 0;
  60. }