g_matrix.c 4.3 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143
  1. /* This file is part of the GNU plotutils package. Copyright (C) 1995,
  2. 1996, 1997, 1998, 1999, 2000, 2005, 2008, Free Software Foundation, Inc.
  3. The GNU plotutils package is free software. You may redistribute it
  4. and/or modify it under the terms of the GNU General Public License as
  5. published by the Free Software foundation; either version 2, or (at your
  6. option) any later version.
  7. The GNU plotutils package is distributed in the hope that it will be
  8. useful, but WITHOUT ANY WARRANTY; without even the implied warranty of
  9. MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
  10. General Public License for more details.
  11. You should have received a copy of the GNU General Public License along
  12. with the GNU plotutils package; see the file COPYING. If not, write to
  13. the Free Software Foundation, Inc., 51 Franklin St., Fifth Floor,
  14. Boston, MA 02110-1301, USA. */
  15. #include "sys-defines.h"
  16. #include "extern.h"
  17. /* Computes the product of two PS-style transformation matrices
  18. (i.e. matrix representations of affine transformations). */
  19. void
  20. _matrix_product (const double m[6], const double n[6], double product[6])
  21. {
  22. double local_product[6];
  23. local_product[0] = m[0] * n[0] + m[1] * n[2];
  24. local_product[1] = m[0] * n[1] + m[1] * n[3];
  25. local_product[2] = m[2] * n[0] + m[3] * n[2];
  26. local_product[3] = m[2] * n[1] + m[3] * n[3];
  27. local_product[4] = m[4] * n[0] + m[5] * n[2] + n[4];
  28. local_product[5] = m[4] * n[1] + m[5] * n[3] + n[5];
  29. memcpy (product, local_product, 6 * sizeof (double));
  30. return;
  31. }
  32. /* Computes the inverse of a PS-style transformation matrix, which should
  33. be nonsingular for correct results. */
  34. void
  35. _matrix_inverse (const double m[6], double inverse[6])
  36. {
  37. double det = m[0] * m[3] - m[1] * m[2];
  38. if (det == 0.0)
  39. /* bogus */
  40. inverse[0] = inverse[1] = inverse[2] = inverse[3]
  41. = inverse[4] = inverse[5] = 0.0;
  42. else
  43. {
  44. double invdet = 1.0 / det;
  45. inverse[0] = invdet * m[3];
  46. inverse[1] = - invdet * m[1];
  47. inverse[2] = - invdet * m[2];
  48. inverse[3] = invdet * m[0];
  49. inverse[4] = invdet * (m[2] * m[5] - m[3] * m[4]);
  50. inverse[5] = invdet * (m[1] * m[4] - m[0] * m[5]);
  51. }
  52. }
  53. /* _matrix_norm computes the matrix norm (in the l^2 sense) of the linear
  54. transformation part of a PS-style transformation matrix. Actually we
  55. compute instead the geometric mean of the l^1 and l^infinity norms. By
  56. Hadamard's 3-line theorem, this geometric mean is an upper bound on the
  57. true l^2 norm.
  58. This function is called only to select appropriate line widths and font
  59. sizes. For the purposes of those functions, the above approximation
  60. should suffice. */
  61. double
  62. _matrix_norm (const double m[6])
  63. {
  64. double mt[4], pm[4];
  65. double norm1, norm2;
  66. double a[4];
  67. int i;
  68. mt[0] = m[0]; /* transpose of m */
  69. mt[1] = m[2];
  70. mt[2] = m[1];
  71. mt[3] = m[3];
  72. pm[0] = m[0] * mt[0] + m[1] * mt[2]; /* pm = m * mt */
  73. pm[1] = m[0] * mt[1] + m[1] * mt[3];
  74. pm[2] = m[2] * mt[0] + m[3] * mt[2];
  75. pm[3] = m[2] * mt[1] + m[3] * mt[3];
  76. for (i = 0; i < 4; i++)
  77. a[i] = fabs(pm[i]);
  78. /* compute l^1 and l^infinity norms of m * mt */
  79. norm1 = DMAX(a[0]+a[1], a[2]+a[3]);
  80. norm2 = DMAX(a[0]+a[2], a[1]+a[3]);
  81. /* l^2 norm of m is sqrt of l^2 norm of m * mt */
  82. return sqrt(sqrt(norm1 * norm2));
  83. }
  84. /* Compute the minimum and maximum singular values of a 2-by-2 matrix M.
  85. The singular values are the square roots of the eigenvalues of M times
  86. its transpose. */
  87. void
  88. _matrix_sing_vals (const double m[6], double *min_sing_val, double *max_sing_val)
  89. {
  90. double mt[4], pm[4];
  91. double trace, det, disc, sqrtdisc;
  92. double s1, s2;
  93. mt[0] = m[0]; /* transpose of m */
  94. mt[1] = m[2];
  95. mt[2] = m[1];
  96. mt[3] = m[3];
  97. pm[0] = m[0] * mt[0] + m[1] * mt[2]; /* pm = m * mt */
  98. pm[1] = m[0] * mt[1] + m[1] * mt[3];
  99. pm[2] = m[2] * mt[0] + m[3] * mt[2];
  100. pm[3] = m[2] * mt[1] + m[3] * mt[3];
  101. trace = pm[0] + pm[3];
  102. det = pm[0] * pm[3] - pm[1] * pm[2];
  103. /* s^2 + b s + c = 0, where b = -trace, c = det */
  104. disc = trace * trace - 4.0 * det;
  105. disc = DMAX(0.0, disc); /* paranoia */
  106. sqrtdisc = sqrt (disc);
  107. s1 = 0.5 * (trace - sqrtdisc);
  108. s2 = 0.5 * (trace + sqrtdisc);
  109. s1 = DMAX(0.0, s1); /* paranoia */
  110. s2 = DMAX(0.0, s2); /* paranoia */
  111. *min_sing_val = sqrt(s1);
  112. *max_sing_val = sqrt(s2);
  113. }