@stdlib/math-base-special-exp 0.0.6 → 0.2.0
This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
- package/LICENSE +3 -262
- package/NOTICE +1 -1
- package/README.md +122 -24
- package/SECURITY.md +5 -0
- package/dist/index.d.ts +3 -0
- package/dist/index.js +11 -0
- package/dist/index.js.map +7 -0
- package/docs/types/index.d.ts +1 -1
- package/include/stdlib/math/base/special/exp.h +38 -0
- package/include.gypi +53 -0
- package/lib/expmulti.js +31 -1
- package/lib/index.js +2 -2
- package/lib/{exp.js → main.js} +33 -5
- package/lib/native.js +58 -0
- package/lib/polyval_p.js +1 -2
- package/manifest.json +84 -0
- package/package.json +27 -15
- package/src/addon.c +24 -0
- package/src/main.c +253 -0
- package/docs/repl.txt +0 -28
- package/docs/types/test.ts +0 -44
package/src/main.c
ADDED
|
@@ -0,0 +1,253 @@
|
|
|
1
|
+
/**
|
|
2
|
+
* @license Apache-2.0
|
|
3
|
+
*
|
|
4
|
+
* Copyright (c) 2022 The Stdlib Authors.
|
|
5
|
+
*
|
|
6
|
+
* Licensed under the Apache License, Version 2.0 (the "License");
|
|
7
|
+
* you may not use this file except in compliance with the License.
|
|
8
|
+
* You may obtain a copy of the License at
|
|
9
|
+
*
|
|
10
|
+
* http://www.apache.org/licenses/LICENSE-2.0
|
|
11
|
+
*
|
|
12
|
+
* Unless required by applicable law or agreed to in writing, software
|
|
13
|
+
* distributed under the License is distributed on an "AS IS" BASIS,
|
|
14
|
+
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
|
|
15
|
+
* See the License for the specific language governing permissions and
|
|
16
|
+
* limitations under the License.
|
|
17
|
+
*
|
|
18
|
+
*
|
|
19
|
+
* ## Notice
|
|
20
|
+
*
|
|
21
|
+
* * The following copyrights, licenses, and long comment were part of the original implementation available as part of [Go]{@link https://github.com/golang/go/blob/cb07765045aed5104a3df31507564ac99e6ddce8/src/math/exp.go}, which in turn was based on an implementation available as part of [FreeBSD]{@link https://svnweb.freebsd.org/base/release/9.3.0/lib/msun/src/e_exp.c}. The implementation follows the original, but has been modified according to project conventions.
|
|
22
|
+
*
|
|
23
|
+
* ```text
|
|
24
|
+
* Copyright (c) 2009 The Go Authors. All rights reserved.
|
|
25
|
+
*
|
|
26
|
+
* Redistribution and use in source and binary forms, with or without
|
|
27
|
+
* modification, are permitted provided that the following conditions are
|
|
28
|
+
* met:
|
|
29
|
+
*
|
|
30
|
+
* * Redistributions of source code must retain the above copyright
|
|
31
|
+
* notice, this list of conditions and the following disclaimer.
|
|
32
|
+
* * Redistributions in binary form must reproduce the above
|
|
33
|
+
* copyright notice, this list of conditions and the following disclaimer
|
|
34
|
+
* in the documentation and/or other materials provided with the
|
|
35
|
+
* distribution.
|
|
36
|
+
* * Neither the name of Google Inc. nor the names of its
|
|
37
|
+
* contributors may be used to endorse or promote products derived from
|
|
38
|
+
* this software without specific prior written permission.
|
|
39
|
+
*
|
|
40
|
+
* THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS
|
|
41
|
+
* "AS IS" AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT
|
|
42
|
+
* LIMITED TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR
|
|
43
|
+
* A PARTICULAR PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT
|
|
44
|
+
* OWNER OR CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL,
|
|
45
|
+
* SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT
|
|
46
|
+
* LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE,
|
|
47
|
+
* DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY
|
|
48
|
+
* THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT
|
|
49
|
+
* (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE
|
|
50
|
+
* OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
|
|
51
|
+
* ```
|
|
52
|
+
*
|
|
53
|
+
* ```text
|
|
54
|
+
* Copyright (C) 2004 by Sun Microsystems, Inc. All rights reserved.
|
|
55
|
+
*
|
|
56
|
+
* Developed at SunPro, a Sun Microsystems, Inc. business.
|
|
57
|
+
* Permission to use, copy, modify, and distribute this
|
|
58
|
+
* software is freely granted, provided that this notice
|
|
59
|
+
* is preserved.
|
|
60
|
+
* ```
|
|
61
|
+
*/
|
|
62
|
+
|
|
63
|
+
#include "stdlib/math/base/special/exp.h"
|
|
64
|
+
#include "stdlib/math/base/assert/is_nan.h"
|
|
65
|
+
#include "stdlib/math/base/special/trunc.h"
|
|
66
|
+
#include "stdlib/constants/float64/ninf.h"
|
|
67
|
+
#include "stdlib/constants/float64/pinf.h"
|
|
68
|
+
#include "stdlib/math/base/special/ldexp.h"
|
|
69
|
+
#include <stdint.h>
|
|
70
|
+
|
|
71
|
+
static const double LN2_HI = 6.93147180369123816490e-01;
|
|
72
|
+
static const double LN2_LO = 1.90821492927058770002e-10;
|
|
73
|
+
static const double LOG2_E = 1.44269504088896338700e+00;
|
|
74
|
+
static const double EXP_OVERFLOW = 7.09782712893383973096e+02;
|
|
75
|
+
static const double EXP_UNDERFLOW = -7.45133219101941108420e+02;
|
|
76
|
+
static const double NEARZERO = 1.0 / (1 << 28); // 2^-28
|
|
77
|
+
static const double NEG_NEARZERO = -NEARZERO;
|
|
78
|
+
|
|
79
|
+
/* Begin auto-generated functions. The following functions are auto-generated. Do not edit directly. */
|
|
80
|
+
|
|
81
|
+
// BEGIN: polyval_p
|
|
82
|
+
|
|
83
|
+
/**
|
|
84
|
+
* Evaluates a polynomial.
|
|
85
|
+
*
|
|
86
|
+
* ## Notes
|
|
87
|
+
*
|
|
88
|
+
* - The implementation uses [Horner's rule][horners-method] for efficient computation.
|
|
89
|
+
*
|
|
90
|
+
* [horners-method]: https://en.wikipedia.org/wiki/Horner%27s_method
|
|
91
|
+
*
|
|
92
|
+
* @param x value at which to evaluate the polynomial
|
|
93
|
+
* @return evaluated polynomial
|
|
94
|
+
*/
|
|
95
|
+
static double polyval_p( const double x ) {
|
|
96
|
+
return 0.16666666666666602 + (x * (-0.0027777777777015593 + (x * (0.00006613756321437934 + (x * (-0.0000016533902205465252 + (x * 4.1381367970572385e-8)))))));
|
|
97
|
+
}
|
|
98
|
+
|
|
99
|
+
// END: polyval_p
|
|
100
|
+
|
|
101
|
+
/* End auto-generated functions. */
|
|
102
|
+
|
|
103
|
+
/**
|
|
104
|
+
* Computes \\(e^{r} 2^k\\) where \\(r = \mathrm{hi} - \mathrm{lo}\\) and \\(|r| \leq \ln(2)/2\\).
|
|
105
|
+
*
|
|
106
|
+
* @param hi upper bound
|
|
107
|
+
* @param lo lower bound
|
|
108
|
+
* @param k power of 2
|
|
109
|
+
* @return function value
|
|
110
|
+
*/
|
|
111
|
+
static double expmulti( const double hi, const double lo, const int32_t k ) {
|
|
112
|
+
double r;
|
|
113
|
+
double t;
|
|
114
|
+
double c;
|
|
115
|
+
double y;
|
|
116
|
+
|
|
117
|
+
r = hi - lo;
|
|
118
|
+
t = r * r;
|
|
119
|
+
c = r - (t * polyval_p( t ));
|
|
120
|
+
y = 1.0 - ( lo - ( ( r * c ) / ( 2.0 - c ) ) - hi);
|
|
121
|
+
|
|
122
|
+
return stdlib_base_ldexp( y, k );
|
|
123
|
+
}
|
|
124
|
+
|
|
125
|
+
/**
|
|
126
|
+
* Evaluates the natural exponential function.
|
|
127
|
+
*
|
|
128
|
+
* ## Method
|
|
129
|
+
*
|
|
130
|
+
* 1. We reduce \\( x \\) to an \\( r \\) so that \\( |r| \leq 0.5 \cdot \ln(2) \approx 0.34658 \\). Given \\( x \\), we find an \\( r \\) and integer \\( k \\) such that
|
|
131
|
+
*
|
|
132
|
+
* ```tex
|
|
133
|
+
* \begin{align*}
|
|
134
|
+
* x &= k \cdot \ln(2) + r \\
|
|
135
|
+
* |r| &\leq 0.5 \cdot \ln(2)
|
|
136
|
+
* \end{align*}
|
|
137
|
+
* ```
|
|
138
|
+
*
|
|
139
|
+
* <!-- <note> -->
|
|
140
|
+
*
|
|
141
|
+
* \\( r \\) can be represented as \\( r = \mathrm{hi} - \mathrm{lo} \\) for better accuracy.
|
|
142
|
+
*
|
|
143
|
+
* <!-- </note> -->
|
|
144
|
+
*
|
|
145
|
+
* 2. We approximate of \\( e^{r} \\) by a special rational function on the interval \\(\[0,0.34658]\\):
|
|
146
|
+
*
|
|
147
|
+
* ```tex
|
|
148
|
+
* \begin{align*}
|
|
149
|
+
* R\left(r^2\right) &= r \cdot \frac{ e^{r}+1 }{ e^{r}-1 } \\
|
|
150
|
+
* &= 2 + \frac{r^2}{6} - \frac{r^4}{360} + \ldots
|
|
151
|
+
* \end{align*}
|
|
152
|
+
* ```
|
|
153
|
+
*
|
|
154
|
+
* We use a special Remes algorithm on \\(\[0,0.34658]\\) to generate a polynomial of degree \\(5\\) to approximate \\(R\\). The maximum error of this polynomial approximation is bounded by \\(2^{-59}\\). In other words,
|
|
155
|
+
*
|
|
156
|
+
* ```tex
|
|
157
|
+
* R(z) \sim 2 + P_1 z + P_2 z^2 + P_3 z^3 + P_4 z^4 + P_5 z^5
|
|
158
|
+
* ```
|
|
159
|
+
*
|
|
160
|
+
* where \\( z = r^2 \\) and
|
|
161
|
+
*
|
|
162
|
+
* ```tex
|
|
163
|
+
* \left| 2 + P_1 z + \ldots + P_5 z^5 - R(z) \right| \leq 2^{-59}
|
|
164
|
+
* ```
|
|
165
|
+
*
|
|
166
|
+
* <!-- <note> -->
|
|
167
|
+
*
|
|
168
|
+
* The values of \\( P_1 \\) to \\( P_5 \\) are listed in the source code.
|
|
169
|
+
*
|
|
170
|
+
* <!-- </note> -->
|
|
171
|
+
*
|
|
172
|
+
* The computation of \\( e^{r} \\) thus becomes
|
|
173
|
+
*
|
|
174
|
+
* ```tex
|
|
175
|
+
* \begin{align*}
|
|
176
|
+
* e^{r} &= 1 + \frac{2r}{R-r} \\
|
|
177
|
+
* &= 1 + r + \frac{r \cdot R_1(r)}{2 - R_1(r)}\ \text{for better accuracy}
|
|
178
|
+
* \end{align*}
|
|
179
|
+
* ```
|
|
180
|
+
*
|
|
181
|
+
* where
|
|
182
|
+
*
|
|
183
|
+
* ```tex
|
|
184
|
+
* R_1(r) = r - P_1\ r^2 + P_2\ r^4 + \ldots + P_5\ r^{10}
|
|
185
|
+
* ```
|
|
186
|
+
*
|
|
187
|
+
* 3. We scale back to obtain \\( e^{x} \\). From step 1, we have
|
|
188
|
+
*
|
|
189
|
+
* ```tex
|
|
190
|
+
* e^{x} = 2^k e^{r}
|
|
191
|
+
* ```
|
|
192
|
+
*
|
|
193
|
+
*
|
|
194
|
+
* ## Special Cases
|
|
195
|
+
*
|
|
196
|
+
* ```tex
|
|
197
|
+
* \begin{align*}
|
|
198
|
+
* e^\infty &= \infty \\
|
|
199
|
+
* e^{-\infty} &= 0 \\
|
|
200
|
+
* e^{\mathrm{NaN}} &= \mathrm{NaN} \\
|
|
201
|
+
* e^0 &= 1\ \mathrm{is\ exact\ for\ finite\ argument\ only}
|
|
202
|
+
* \end{align*}
|
|
203
|
+
* ```
|
|
204
|
+
*
|
|
205
|
+
* ## Notes
|
|
206
|
+
*
|
|
207
|
+
* - According to an error analysis, the error is always less than \\(1\\) ulp (unit in the last place).
|
|
208
|
+
*
|
|
209
|
+
* - For an IEEE double,
|
|
210
|
+
*
|
|
211
|
+
* - if \\(x > 7.09782712893383973096\mbox{e+}02\\), then \\(e^{x}\\) overflows
|
|
212
|
+
* - if \\(x < -7.45133219101941108420\mbox{e+}02\\), then \\(e^{x}\\) underflows
|
|
213
|
+
*
|
|
214
|
+
* - The hexadecimal values included in the source code are the intended ones for the used constants. Decimal values may be used, provided that the compiler will convert from decimal to binary accurately enough to produce the intended hexadecimal values.
|
|
215
|
+
*
|
|
216
|
+
* @param x input value
|
|
217
|
+
* @return output value
|
|
218
|
+
*
|
|
219
|
+
* @example
|
|
220
|
+
* double out = stdlib_base_exp( 0.0 );
|
|
221
|
+
* // returns 1.0
|
|
222
|
+
*/
|
|
223
|
+
double stdlib_base_exp( const double x ) {
|
|
224
|
+
double hi;
|
|
225
|
+
double lo;
|
|
226
|
+
int32_t k;
|
|
227
|
+
|
|
228
|
+
if ( stdlib_base_is_nan( x ) || x == STDLIB_CONSTANT_FLOAT64_PINF ) {
|
|
229
|
+
return x;
|
|
230
|
+
}
|
|
231
|
+
if ( x == STDLIB_CONSTANT_FLOAT64_NINF ) {
|
|
232
|
+
return 0.0;
|
|
233
|
+
}
|
|
234
|
+
if ( x > EXP_OVERFLOW ) {
|
|
235
|
+
return STDLIB_CONSTANT_FLOAT64_PINF;
|
|
236
|
+
}
|
|
237
|
+
if ( x < EXP_UNDERFLOW ) {
|
|
238
|
+
return 0.0;
|
|
239
|
+
}
|
|
240
|
+
if ( x > NEG_NEARZERO && x < NEARZERO ) {
|
|
241
|
+
return 1.0 + x;
|
|
242
|
+
}
|
|
243
|
+
// Reduce and compute `r = hi - lo` for extra precision...
|
|
244
|
+
if ( x < 0.0 ) {
|
|
245
|
+
k = stdlib_base_trunc( ( LOG2_E * x ) - 0.5 );
|
|
246
|
+
} else {
|
|
247
|
+
k = stdlib_base_trunc( ( LOG2_E * x ) + 0.5 );
|
|
248
|
+
}
|
|
249
|
+
hi = x - ( k * LN2_HI );
|
|
250
|
+
lo = k * LN2_LO;
|
|
251
|
+
|
|
252
|
+
return expmulti( hi, lo, k );
|
|
253
|
+
}
|
package/docs/repl.txt
DELETED
|
@@ -1,28 +0,0 @@
|
|
|
1
|
-
|
|
2
|
-
{{alias}}( x )
|
|
3
|
-
Evaluates the natural exponential function.
|
|
4
|
-
|
|
5
|
-
Parameters
|
|
6
|
-
----------
|
|
7
|
-
x: number
|
|
8
|
-
Input value.
|
|
9
|
-
|
|
10
|
-
Returns
|
|
11
|
-
-------
|
|
12
|
-
y: number
|
|
13
|
-
Function value.
|
|
14
|
-
|
|
15
|
-
Examples
|
|
16
|
-
--------
|
|
17
|
-
> var y = {{alias}}( 4.0 )
|
|
18
|
-
~54.5982
|
|
19
|
-
> y = {{alias}}( -9.0 )
|
|
20
|
-
~1.234e-4
|
|
21
|
-
> y = {{alias}}( 0.0 )
|
|
22
|
-
1.0
|
|
23
|
-
> y = {{alias}}( NaN )
|
|
24
|
-
NaN
|
|
25
|
-
|
|
26
|
-
See Also
|
|
27
|
-
--------
|
|
28
|
-
|
package/docs/types/test.ts
DELETED
|
@@ -1,44 +0,0 @@
|
|
|
1
|
-
/*
|
|
2
|
-
* @license Apache-2.0
|
|
3
|
-
*
|
|
4
|
-
* Copyright (c) 2019 The Stdlib Authors.
|
|
5
|
-
*
|
|
6
|
-
* Licensed under the Apache License, Version 2.0 (the "License");
|
|
7
|
-
* you may not use this file except in compliance with the License.
|
|
8
|
-
* You may obtain a copy of the License at
|
|
9
|
-
*
|
|
10
|
-
* http://www.apache.org/licenses/LICENSE-2.0
|
|
11
|
-
*
|
|
12
|
-
* Unless required by applicable law or agreed to in writing, software
|
|
13
|
-
* distributed under the License is distributed on an "AS IS" BASIS,
|
|
14
|
-
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
|
|
15
|
-
* See the License for the specific language governing permissions and
|
|
16
|
-
* limitations under the License.
|
|
17
|
-
*/
|
|
18
|
-
|
|
19
|
-
import exp = require( './index' );
|
|
20
|
-
|
|
21
|
-
|
|
22
|
-
// TESTS //
|
|
23
|
-
|
|
24
|
-
// The function returns a number...
|
|
25
|
-
{
|
|
26
|
-
exp( 0.5 ); // $ExpectType number
|
|
27
|
-
}
|
|
28
|
-
|
|
29
|
-
// The function does not compile if provided a value other than a number...
|
|
30
|
-
{
|
|
31
|
-
exp( true ); // $ExpectError
|
|
32
|
-
exp( false ); // $ExpectError
|
|
33
|
-
exp( null ); // $ExpectError
|
|
34
|
-
exp( undefined ); // $ExpectError
|
|
35
|
-
exp( '5' ); // $ExpectError
|
|
36
|
-
exp( [] ); // $ExpectError
|
|
37
|
-
exp( {} ); // $ExpectError
|
|
38
|
-
exp( ( x: number ): number => x ); // $ExpectError
|
|
39
|
-
}
|
|
40
|
-
|
|
41
|
-
// The function does not compile if provided insufficient arguments...
|
|
42
|
-
{
|
|
43
|
-
exp(); // $ExpectError
|
|
44
|
-
}
|