Skip to content

Commit 6001e62

Browse files
refactor: replace evalpoly with compiled polyval functions
PR-URL: stdlib-js#6368 Reviewed-by: Athan Reines <[email protected]>
1 parent b72f445 commit 6001e62

File tree

4 files changed

+182
-24
lines changed

4 files changed

+182
-24
lines changed
Lines changed: 86 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,86 @@
1+
/**
2+
* @license Apache-2.0
3+
*
4+
* Copyright (c) 2025 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+
/*
20+
* This script compiles modules for evaluating polynomial functions. If any polynomial coefficients change, this script should be rerun to update the compiled files.
21+
*/
22+
'use strict';
23+
24+
// MODULES //
25+
26+
var resolve = require( 'path' ).resolve;
27+
var writeFileSync = require( '@stdlib/fs/write-file' ).sync;
28+
var currentYear = require( '@stdlib/time/current-year' );
29+
var licenseHeader = require( '@stdlib/_tools/licenses/header' );
30+
var compile = require( '@stdlib/math/base/tools/evalpoly-compile' );
31+
32+
33+
// VARIABLES //
34+
35+
// Polynomial coefficients ordered in ascending degree...
36+
var SIN_COEF = [
37+
-1.66666666666666307295e-1, // 0xbfc5555555555548
38+
8.33333333332211858878e-3, // 0x3f8111111110f7d0
39+
-1.98412698295895385996e-4, // 0xbf2a01a019bfdf03
40+
2.75573136213857245213e-6, // 0x3ec71de3567d48a1
41+
-2.50507477628578072866e-8, // 0xbe5ae5e5a9291f5d
42+
1.58962301576546568060e-10 // 0x3de5d8fd1fd19ccd
43+
];
44+
var COS_COEF = [
45+
4.16666666666665929218e-2, // 0x3fa555555555554b
46+
-1.38888888888730564116e-3, // 0xbf56c16c16c14f91
47+
2.48015872888517045348e-5, // 0x3efa01a019c844f5
48+
-2.75573141792967388112e-7, // 0xbe927e4f7eac4bc6
49+
2.08757008419747316778e-9, // 0x3e21ee9d7b4e3f05
50+
-1.13585365213876817300e-11 // 0xbda8fa49a0861a9b
51+
];
52+
53+
// Header to add to output files:
54+
var header = licenseHeader( 'Apache-2.0', 'js', {
55+
'year': currentYear(),
56+
'copyright': 'The Stdlib Authors'
57+
});
58+
header += '\n/* This is a generated file. Do not edit directly. */\n';
59+
60+
61+
// MAIN //
62+
63+
/**
64+
* Main execution sequence.
65+
*
66+
* @private
67+
*/
68+
function main() {
69+
var fpath;
70+
var opts;
71+
var str;
72+
73+
opts = {
74+
'encoding': 'utf8'
75+
};
76+
77+
fpath = resolve( __dirname, 'polyval_sin.js' );
78+
str = header + compile( SIN_COEF );
79+
writeFileSync( fpath, str, opts );
80+
81+
fpath = resolve( __dirname, 'polyval_cos.js' );
82+
str = header + compile( COS_COEF );
83+
writeFileSync( fpath, str, opts );
84+
}
85+
86+
main();

lib/node_modules/@stdlib/math/base/special/cos/scripts/cephes/index.js

Lines changed: 2 additions & 24 deletions
Original file line numberDiff line numberDiff line change
@@ -34,11 +34,12 @@
3434

3535
// MODULES //
3636

37-
var evalpoly = require( '@stdlib/math/base/tools/evalpoly' ).factory; // TODO: replace with compiled polyval functions
3837
var floor = require( '@stdlib/math/base/special/floor' );
3938
var ldexp = require( '@stdlib/math/base/special/ldexp' );
4039
var isnan = require( '@stdlib/math/base/assert/is-nan' );
4140
var isinfinite = require( '@stdlib/math/base/assert/is-infinite' );
41+
var polyvalSIN = require( './polyval_sin.js' );
42+
var polyvalCOS = require( './polyval_cos.js' );
4243

4344

4445
// VARIABLES //
@@ -47,29 +48,6 @@ var DP1 = 7.85398125648498535156e-1; // 0x3fe921fb40000000, Pi/4 split into thre
4748
var DP2 = 3.77489470793079817668e-8; // 0x3e64442d00000000,
4849
var DP3 = 2.69515142907905952645e-15; // 0x3ce8469898cc5170,
4950
var PIO4 = 7.85398163397448309616E-1; // 4/pi
50-
var SIN_COEF = [
51-
-1.66666666666666307295e-1, // 0xbfc5555555555548
52-
8.33333333332211858878e-3, // 0x3f8111111110f7d0
53-
-1.98412698295895385996e-4, // 0xbf2a01a019bfdf03
54-
2.75573136213857245213e-6, // 0x3ec71de3567d48a1
55-
-2.50507477628578072866e-8, // 0xbe5ae5e5a9291f5d
56-
1.58962301576546568060e-10 // 0x3de5d8fd1fd19ccd
57-
];
58-
var COS_COEF = [
59-
4.16666666666665929218e-2, // 0x3fa555555555554b
60-
-1.38888888888730564116e-3, // 0xbf56c16c16c14f91
61-
2.48015872888517045348e-5, // 0x3efa01a019c844f5
62-
-2.75573141792967388112e-7, // 0xbe927e4f7eac4bc6
63-
2.08757008419747316778e-9, // 0x3e21ee9d7b4e3f05
64-
-1.13585365213876817300e-11 // 0xbda8fa49a0861a9b
65-
];
66-
67-
68-
// FUNCTIONS //
69-
70-
// Compile functions to evaluate polynomial functions based on the above coefficients...
71-
var polyvalSIN = evalpoly( SIN_COEF );
72-
var polyvalCOS = evalpoly( COS_COEF );
7351

7452

7553
// MAIN //
Lines changed: 47 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,47 @@
1+
/**
2+
* @license Apache-2.0
3+
*
4+
* Copyright (c) 2025 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+
/* This is a generated file. Do not edit directly. */
20+
'use strict';
21+
22+
// MAIN //
23+
24+
/**
25+
* Evaluates a polynomial.
26+
*
27+
* ## Notes
28+
*
29+
* - The implementation uses [Horner's rule][horners-method] for efficient computation.
30+
*
31+
* [horners-method]: https://en.wikipedia.org/wiki/Horner%27s_method
32+
*
33+
* @private
34+
* @param {number} x - value at which to evaluate the polynomial
35+
* @returns {number} evaluated polynomial
36+
*/
37+
function evalpoly( x ) {
38+
if ( x === 0.0 ) {
39+
return 0.041666666666666595;
40+
}
41+
return 0.041666666666666595 + (x * (-0.0013888888888873056 + (x * (0.000024801587288851704 + (x * (-2.755731417929674e-7 + (x * (2.087570084197473e-9 + (x * -1.1358536521387682e-11))))))))); // eslint-disable-line max-len
42+
}
43+
44+
45+
// EXPORTS //
46+
47+
module.exports = evalpoly;
Lines changed: 47 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,47 @@
1+
/**
2+
* @license Apache-2.0
3+
*
4+
* Copyright (c) 2025 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+
/* This is a generated file. Do not edit directly. */
20+
'use strict';
21+
22+
// MAIN //
23+
24+
/**
25+
* Evaluates a polynomial.
26+
*
27+
* ## Notes
28+
*
29+
* - The implementation uses [Horner's rule][horners-method] for efficient computation.
30+
*
31+
* [horners-method]: https://en.wikipedia.org/wiki/Horner%27s_method
32+
*
33+
* @private
34+
* @param {number} x - value at which to evaluate the polynomial
35+
* @returns {number} evaluated polynomial
36+
*/
37+
function evalpoly( x ) {
38+
if ( x === 0.0 ) {
39+
return -0.1666666666666663;
40+
}
41+
return -0.1666666666666663 + (x * (0.008333333333322118 + (x * (-0.0001984126982958954 + (x * (0.0000027557313621385722 + (x * (-2.5050747762857807e-8 + (x * 1.5896230157654656e-10))))))))); // eslint-disable-line max-len
42+
}
43+
44+
45+
// EXPORTS //
46+
47+
module.exports = evalpoly;

0 commit comments

Comments
 (0)