gsl_fft__factorize.c 3.6 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177
  1. /* fft/factorize.c
  2. *
  3. * Copyright (C) 1996, 1997, 1998, 1999, 2000, 2007 Brian Gough
  4. *
  5. * This program is free software; you can redistribute it and/or modify
  6. * it under the terms of the GNU General Public License as published by
  7. * the Free Software Foundation; either version 3 of the License, or (at
  8. * your option) any later version.
  9. *
  10. * This program is distributed in the hope that it will be useful, but
  11. * WITHOUT ANY WARRANTY; without even the implied warranty of
  12. * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
  13. * General Public License for more details.
  14. *
  15. * You should have received a copy of the GNU General Public License
  16. * along with this program; if not, write to the Free Software
  17. * Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301, USA.
  18. */
  19. #include "gsl__config.h"
  20. #include "gsl_errno.h"
  21. #include "gsl_fft_complex.h"
  22. #include "gsl_fft__factorize.h"
  23. static int
  24. fft_complex_factorize (const size_t n,
  25. size_t *nf,
  26. size_t factors[])
  27. {
  28. const size_t complex_subtransforms[] =
  29. {7, 6, 5, 4, 3, 2, 0};
  30. /* other factors can be added here if their transform modules are
  31. implemented. The end of the list is marked by 0. */
  32. int status = fft_factorize (n, complex_subtransforms, nf, factors);
  33. return status;
  34. }
  35. static int
  36. fft_halfcomplex_factorize (const size_t n,
  37. size_t *nf,
  38. size_t factors[])
  39. {
  40. const size_t halfcomplex_subtransforms[] =
  41. {5, 4, 3, 2, 0};
  42. int status = fft_factorize (n, halfcomplex_subtransforms, nf, factors);
  43. return status;
  44. }
  45. static int
  46. fft_real_factorize (const size_t n,
  47. size_t *nf,
  48. size_t factors[])
  49. {
  50. const size_t real_subtransforms[] =
  51. {5, 4, 3, 2, 0};
  52. int status = fft_factorize (n, real_subtransforms, nf, factors);
  53. return status;
  54. }
  55. static int
  56. fft_factorize (const size_t n,
  57. const size_t implemented_subtransforms[],
  58. size_t *n_factors,
  59. size_t factors[])
  60. {
  61. size_t nf = 0;
  62. size_t ntest = n;
  63. size_t factor;
  64. size_t i = 0;
  65. if (n == 0)
  66. {
  67. GSL_ERROR ("length n must be positive integer", GSL_EDOM);
  68. }
  69. if (n == 1)
  70. {
  71. factors[0] = 1;
  72. *n_factors = 1;
  73. return 0;
  74. }
  75. /* deal with the implemented factors first */
  76. while (implemented_subtransforms[i] && ntest != 1)
  77. {
  78. factor = implemented_subtransforms[i];
  79. while ((ntest % factor) == 0)
  80. {
  81. ntest = ntest / factor;
  82. factors[nf] = factor;
  83. nf++;
  84. }
  85. i++;
  86. }
  87. /* deal with any other even prime factors (there is only one) */
  88. factor = 2;
  89. while ((ntest % factor) == 0 && (ntest != 1))
  90. {
  91. ntest = ntest / factor;
  92. factors[nf] = factor;
  93. nf++;
  94. }
  95. /* deal with any other odd prime factors */
  96. factor = 3;
  97. while (ntest != 1)
  98. {
  99. while ((ntest % factor) != 0)
  100. {
  101. factor += 2;
  102. }
  103. ntest = ntest / factor;
  104. factors[nf] = factor;
  105. nf++;
  106. }
  107. /* check that the factorization is correct */
  108. {
  109. size_t product = 1;
  110. for (i = 0; i < nf; i++)
  111. {
  112. product *= factors[i];
  113. }
  114. if (product != n)
  115. {
  116. GSL_ERROR ("factorization failed", GSL_ESANITY);
  117. }
  118. }
  119. *n_factors = nf;
  120. return 0;
  121. }
  122. static int
  123. fft_binary_logn (const size_t n)
  124. {
  125. size_t ntest ;
  126. size_t binary_logn = 0 ;
  127. size_t k = 1;
  128. while (k < n)
  129. {
  130. k *= 2;
  131. binary_logn++;
  132. }
  133. ntest = (1 << binary_logn) ;
  134. if (n != ntest )
  135. {
  136. return -1 ; /* n is not a power of 2 */
  137. }
  138. return binary_logn;
  139. }