complex.h 2.5 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104
  1. #ifndef _complex_h_
  2. #define _complex_h_
  3. /* complex.h
  4. *
  5. * Copyright (C) 1992-2011,2017 Paul Boersma
  6. *
  7. * This code is free software; you can redistribute it and/or modify
  8. * it under the terms of the GNU General Public License as published by
  9. * the Free Software Foundation; either version 2 of the License, or (at
  10. * your option) any later version.
  11. *
  12. * This code is distributed in the hope that it will be useful, but
  13. * WITHOUT ANY WARRANTY; without even the implied warranty of
  14. * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.
  15. * See the GNU General Public License for more details.
  16. *
  17. * You should have received a copy of the GNU General Public License
  18. * along with this work. If not, see <http://www.gnu.org/licenses/>.
  19. */
  20. #include <math.h>
  21. struct dcomplex { double re, im; };
  22. inline static dcomplex dcomplex_add (dcomplex a, dcomplex b) {
  23. dcomplex result;
  24. result.re = a.re + b.re;
  25. result.im = a.im + b.im;
  26. return result;
  27. }
  28. inline static dcomplex dcomplex_sub (dcomplex a, dcomplex b) {
  29. dcomplex result;
  30. result.re = a.re - b.re;
  31. result.im = a.im - b.im;
  32. return result;
  33. }
  34. inline static dcomplex dcomplex_mul (dcomplex a, dcomplex b) {
  35. dcomplex result;
  36. result.re = a.re * b.re - a.im * b.im;
  37. result.im = a.im * b.re + a.re * b.im;
  38. return result;
  39. }
  40. inline static dcomplex dcomplex_conjugate (dcomplex z) {
  41. dcomplex result;
  42. result.re = z.re;
  43. result.im = - z.im;
  44. return result;
  45. }
  46. inline static dcomplex dcomplex_div (dcomplex a, dcomplex b) {
  47. dcomplex result;
  48. double r, den;
  49. if (fabs (b.re) >= fabs (b.im)) {
  50. r = b.im / b.re;
  51. den = b.re + r * b.im;
  52. result.re = (a.re + r * a.im) / den;
  53. result.im = (a.im - r * a.re) / den;
  54. } else {
  55. r = b.re / b.im;
  56. den = b.im + r * b.re;
  57. result.re = (a.re * r + a.im) / den;
  58. result.im = (a.im * r - a.re) / den;
  59. }
  60. return result;
  61. }
  62. inline static double dcomplex_abs (dcomplex z) {
  63. double x, y, temp;
  64. x = fabs (z.re);
  65. y = fabs (z.im);
  66. if (x == 0.0) return y;
  67. if (y == 0.0) return x;
  68. if (x > y) {
  69. temp = y / x;
  70. return x * sqrt (1.0 + temp * temp);
  71. } else {
  72. temp = x / y;
  73. return y * sqrt (1.0 + temp * temp);
  74. }
  75. }
  76. inline static dcomplex dcomplex_rmul (double x, dcomplex a) {
  77. dcomplex result;
  78. result.re = x * a.re;
  79. result.im = x * a.im;
  80. return result;
  81. }
  82. inline static dcomplex dcomplex_exp (dcomplex z) {
  83. dcomplex result;
  84. double size = exp (z.re);
  85. result.re = size * cos (z.im);
  86. result.im = size * sin (z.im);
  87. return result;
  88. }
  89. dcomplex dcomplex_sqrt (dcomplex z);
  90. /* End of file complex.h */
  91. #endif