gsl_sum__work_utrunc.c 1.4 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859606162636465666768
  1. #include "gsl__config.h"
  2. #include "gsl_math.h"
  3. #include "gsl_errno.h"
  4. #include "gsl_sum.h"
  5. gsl_sum_levin_utrunc_workspace *
  6. gsl_sum_levin_utrunc_alloc (size_t n)
  7. {
  8. gsl_sum_levin_utrunc_workspace * w;
  9. if (n == 0)
  10. {
  11. GSL_ERROR_VAL ("length n must be positive integer", GSL_EDOM, 0);
  12. }
  13. w = (gsl_sum_levin_utrunc_workspace *) malloc(sizeof(gsl_sum_levin_utrunc_workspace));
  14. if (w == NULL)
  15. {
  16. GSL_ERROR_VAL ("failed to allocate struct", GSL_ENOMEM, 0);
  17. }
  18. w->q_num = (double *) malloc (n * sizeof (double));
  19. if (w->q_num == NULL)
  20. {
  21. free(w) ; /* error in constructor, prevent memory leak */
  22. GSL_ERROR_VAL ("failed to allocate space for q_num", GSL_ENOMEM, 0);
  23. }
  24. w->q_den = (double *) malloc (n * sizeof (double));
  25. if (w->q_den == NULL)
  26. {
  27. free (w->q_num);
  28. free (w) ; /* error in constructor, prevent memory leak */
  29. GSL_ERROR_VAL ("failed to allocate space for q_den", GSL_ENOMEM, 0);
  30. }
  31. w->dsum = (double *) malloc (n * sizeof (double));
  32. if (w->dsum == NULL)
  33. {
  34. free (w->q_den);
  35. free (w->q_num);
  36. free (w) ; /* error in constructor, prevent memory leak */
  37. GSL_ERROR_VAL ("failed to allocate space for dsum", GSL_ENOMEM, 0);
  38. }
  39. w->size = n;
  40. w->terms_used = 0;
  41. w->sum_plain = 0;
  42. return w;
  43. }
  44. void
  45. gsl_sum_levin_utrunc_free (gsl_sum_levin_utrunc_workspace * w)
  46. {
  47. free (w->dsum);
  48. free (w->q_den);
  49. free (w->q_num);
  50. free (w);
  51. }