sundials_math.c 2.8 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100
  1. /*
  2. * -----------------------------------------------------------------
  3. * $Revision: 4272 $
  4. * $Date: 2014-12-02 11:19:41 -0800 (Tue, 02 Dec 2014) $
  5. * -----------------------------------------------------------------
  6. * Programmer(s): Scott D. Cohen, Alan C. Hindmarsh and
  7. * Aaron Collier @ LLNL
  8. * -----------------------------------------------------------------
  9. * LLNS Copyright Start
  10. * Copyright (c) 2014, Lawrence Livermore National Security
  11. * This work was performed under the auspices of the U.S. Department
  12. * of Energy by Lawrence Livermore National Laboratory in part under
  13. * Contract W-7405-Eng-48 and in part under Contract DE-AC52-07NA27344.
  14. * Produced at the Lawrence Livermore National Laboratory.
  15. * All rights reserved.
  16. * For details, see the LICENSE file.
  17. * LLNS Copyright End
  18. * -----------------------------------------------------------------
  19. * This is the implementation file for a simple C-language math
  20. * library.
  21. * -----------------------------------------------------------------
  22. */
  23. #include <stdio.h>
  24. #include <stdlib.h>
  25. #include <math.h>
  26. #include <sundials/sundials_math.h>
  27. #define ZERO RCONST(0.0)
  28. #define ONE RCONST(1.0)
  29. SUNDIALS_EXPORT realtype SUNRpowerI(realtype base, int exponent)
  30. {
  31. int i, expt;
  32. realtype prod;
  33. prod = ONE;
  34. expt = abs(exponent);
  35. for(i = 1; i <= expt; i++) prod *= base;
  36. if (exponent < 0) prod = ONE/prod;
  37. return(prod);
  38. }
  39. SUNDIALS_EXPORT realtype SUNRpowerR(realtype base, realtype exponent)
  40. {
  41. if (base <= ZERO) return(ZERO);
  42. #if defined(SUNDIALS_USE_GENERIC_MATH)
  43. return((realtype) pow((double) base, (double) exponent));
  44. #elif defined(SUNDIALS_DOUBLE_PRECISION)
  45. return(pow(base, exponent));
  46. #elif defined(SUNDIALS_SINGLE_PRECISION)
  47. return(powf(base, exponent));
  48. #elif defined(SUNDIALS_EXTENDED_PRECISION)
  49. return(powl(base, exponent));
  50. #endif
  51. }
  52. SUNDIALS_EXPORT realtype SUNRsqrt(realtype x)
  53. {
  54. if (x <= ZERO) return(ZERO);
  55. #if defined(SUNDIALS_USE_GENERIC_MATH)
  56. return((realtype) sqrt((double) x));
  57. #elif defined(SUNDIALS_DOUBLE_PRECISION)
  58. return(sqrt(x));
  59. #elif defined(SUNDIALS_SINGLE_PRECISION)
  60. return(sqrtf(x));
  61. #elif defined(SUNDIALS_EXTENDED_PRECISION)
  62. return(sqrtl(x));
  63. #endif
  64. }
  65. SUNDIALS_EXPORT realtype SUNRabs(realtype x)
  66. {
  67. #if defined(SUNDIALS_USE_GENERIC_MATH)
  68. return((realtype) fabs((double) x));
  69. #elif defined(SUNDIALS_DOUBLE_PRECISION)
  70. return(fabs(x));
  71. #elif defined(SUNDIALS_SINGLE_PRECISION)
  72. return(fabsf(x));
  73. #elif defined(SUNDIALS_EXTENDED_PRECISION)
  74. return(fabsl(x));
  75. #endif
  76. }
  77. SUNDIALS_EXPORT realtype SUNRexp(realtype x)
  78. {
  79. #if defined(SUNDIALS_USE_GENERIC_MATH)
  80. return((realtype) exp((double) x));
  81. #elif defined(SUNDIALS_DOUBLE_PRECISION)
  82. return(exp(x));
  83. #elif defined(SUNDIALS_SINGLE_PRECISION)
  84. return(expf(x));
  85. #elif defined(SUNDIALS_EXTENDED_PRECISION)
  86. return(expl(x));
  87. #endif
  88. }