Xcos solvers : Sundials Dormand-Price 4(5) implementation
[scilab.git] / scilab / modules / scicos / src / scicos_sundials / src / sundials / sundials_math.c
1 /*
2  * -----------------------------------------------------------------
3  * $Revision: 1.1 $
4  * $Date: 2006/07/05 15:32:38 $
5  * -----------------------------------------------------------------
6  * Programmer(s): Scott D. Cohen, Alan C. Hindmarsh and
7  *                Aaron Collier @ LLNL
8  * -----------------------------------------------------------------
9  * Copyright (c) 2002, The Regents of the University of California.
10  * Produced at the Lawrence Livermore National Laboratory.
11  * All rights reserved.
12  * For details, see the LICENSE file.
13  * -----------------------------------------------------------------
14  * This is the implementation file for a simple C-language math
15  * library.
16  * -----------------------------------------------------------------
17  */
18
19 #include <stdio.h>
20 #include <stdlib.h>
21 #include <math.h>
22
23 #include <sundials/sundials_math.h>
24
25 #define ZERO RCONST(0.0)
26 #define ONE  RCONST(1.0)
27
28 realtype RPowerI(realtype base, int exponent)
29 {
30   int i, expt;
31   realtype prod;
32
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
40 realtype RPowerR(realtype base, realtype exponent)
41 {
42   if (base <= ZERO) return(ZERO);
43
44 #if defined(SUNDIALS_USE_GENERIC_MATH)
45   return((realtype) pow((double) base, (double) exponent));
46 #elif defined(SUNDIALS_DOUBLE_PRECISION)
47   return(pow(base, exponent));
48 #elif defined(SUNDIALS_SINGLE_PRECISION)
49   return(powf(base, exponent));
50 #elif defined(SUNDIALS_EXTENDED_PRECISION)
51   return(powl(base, exponent));
52 #endif
53 }
54
55 realtype RSqrt(realtype x)
56 {
57   if (x <= ZERO) return(ZERO);
58
59 #if defined(SUNDIALS_USE_GENERIC_MATH)
60   return((realtype) sqrt((double) x));
61 #elif defined(SUNDIALS_DOUBLE_PRECISION)
62   return(sqrt(x));
63 #elif defined(SUNDIALS_SINGLE_PRECISION)
64   return(sqrtf(x));
65 #elif defined(SUNDIALS_EXTENDED_PRECISION)
66   return(sqrtl(x));
67 #endif
68 }
69
70 realtype RAbs(realtype x)
71 {
72 #if defined(SUNDIALS_USE_GENERIC_MATH)
73   return((realtype) fabs((double) x));
74 #elif defined(SUNDIALS_DOUBLE_PRECISION)
75   return(fabs(x));
76 #elif defined(SUNDIALS_SINGLE_PRECISION)
77   return(fabsf(x));
78 #elif defined(SUNDIALS_EXTENDED_PRECISION)
79   return(fabsl(x));
80 #endif
81 }
82
83 realtype RExp(realtype x)
84 {
85 #if defined(SUNDIALS_USE_GENERIC_MATH)
86   return((realtype) exp((double) x));
87 #elif defined(SUNDIALS_DOUBLE_PRECISION)
88   return(exp(x));
89 #elif defined(SUNDIALS_SINGLE_PRECISION)
90   return(expf(x));
91 #elif defined(SUNDIALS_EXTENDED_PRECISION)
92   return(expl(x));
93 #endif
94 }
95
96 realtype FLOOR(realtype x)
97 {
98 #if defined(SUNDIALS_USE_GENERIC_MATH)
99   return(floor((double) x));
100 #elif defined(SUNDIALS_DOUBLE_PRECISION)
101   return((floor(x));
102 #elif defined(SUNDIALS_SINGLE_PRECISION)
103   return(floor(x));
104 #elif defined(SUNDIALS_EXTENDED_PRECISION)
105   return(floor(x));
106 #endif
107 }
108