/** * @license Apache-2.0 * * Copyright (c) 2022 The Stdlib Authors. * * Licensed under the Apache License, Version 2.0 (the "License"); * you may not use this file except in compliance with the License. * You may obtain a copy of the License at * * http://www.apache.org/licenses/LICENSE-2.0 * * Unless required by applicable law or agreed to in writing, software * distributed under the License is distributed on an "AS IS" BASIS, * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. * See the License for the specific language governing permissions and * limitations under the License. */ /* * This script compiles modules for evaluating polynomial functions. If any polynomial coefficients change, this script should be rerun to update the compiled files. */ 'use strict'; // MODULES // var resolve = require( 'path' ).resolve; var readFileSync = require( '@stdlib/fs-read-file' ).sync; var writeFileSync = require( '@stdlib/fs-write-file' ).sync; var currentYear = require( '@stdlib/time-current-year' ); var substringBefore = require( '@stdlib/string-substring-before' ); var substringAfter = require( '@stdlib/string-substring-after' ); var format = require( '@stdlib/string-format' ); var licenseHeader = require( '@stdlib/_tools-licenses-header' ); var compile = require( '@stdlib/math-base-tools-evalpoly-compile' ); var compileC = require( '@stdlib/math-base-tools-evalpoly-compile-c' ); // VARIABLES // // Polynomial coefficients ordered in ascending degree... // Coefficients for approximation to erf on [0, 0.84375) var PP = [ -3.25042107247001499370e-1, // 0xBFD4CD7D, 0x691CB913 -2.84817495755985104766e-2, // 0xBF9D2A51, 0xDBD7194F -5.77027029648944159157e-3, // 0xBF77A291, 0x236668E4 -2.37630166566501626084e-5 // 0xBEF8EAD6, 0x120016AC ]; var QQ = [ 3.97917223959155352819e-1, // 0x3FD97779, 0xCDDADC09 6.50222499887672944485e-2, // 0x3FB0A54C, 0x5536CEBA 5.08130628187576562776e-3, // 0x3F74D022, 0xC4D36B0F 1.32494738004321644526e-4, // 0x3F215DC9, 0x221C1A10 -3.96022827877536812320e-6 // 0xBED09C43, 0x42A26120 ]; // Coefficients for approximation to erf on [0.84375, 1.25) var PA = [ 4.14856118683748331666e-1, // 0x3FDA8D00, 0xAD92B34D -3.72207876035701323847e-1, // 0xBFD7D240, 0xFBB8C3F1 3.18346619901161753674e-1, // 0x3FD45FCA, 0x805120E4 -1.10894694282396677476e-1, // 0xBFBC6398, 0x3D3E28EC 3.54783043256182359371e-2, // 0x3FA22A36, 0x599795EB -2.16637559486879084300e-3 // 0xBF61BF38, 0x0A96073F ]; var QA = [ 1.06420880400844228286e-1, // 0x3FBB3E66, 0x18EEE323 5.40397917702171048937e-1, // 0x3FE14AF0, 0x92EB6F33 7.18286544141962662868e-2, // 0x3FB2635C, 0xD99FE9A7 1.26171219808761642112e-1, // 0x3FC02660, 0xE763351F 1.36370839120290507362e-2, // 0x3F8BEDC2, 0x6B51DD1C 1.19844998467991074170e-2 // 0x3F888B54, 0x5735151D ]; // Coefficients for approximation to erfc on [1.25, 1/0.35) var RA = [ -6.93858572707181764372e-1, // 0xBFE63416, 0xE4BA7360 -1.05586262253232909814e1, // 0xC0251E04, 0x41B0E726 -6.23753324503260060396e1, // 0xC04F300A, 0xE4CBA38D -1.62396669462573470355e2, // 0xC0644CB1, 0x84282266 -1.84605092906711035994e2, // 0xC067135C, 0xEBCCABB2 -8.12874355063065934246e1, // 0xC0545265, 0x57E4D2F2 -9.81432934416914548592 // 0xC023A0EF, 0xC69AC25C ]; var SA = [ 1.96512716674392571292e1, // 0x4033A6B9, 0xBD707687 1.37657754143519042600e2, // 0x4061350C, 0x526AE721 4.34565877475229228821e2, // 0x407B290D, 0xD58A1A71 6.45387271733267880336e2, // 0x40842B19, 0x21EC2868 4.29008140027567833386e2, // 0x407AD021, 0x57700314 1.08635005541779435134e2, // 0x405B28A3, 0xEE48AE2C 6.57024977031928170135, // 0x401A47EF, 0x8E484A93 -6.04244152148580987438e-2 // 0xBFAEEFF2, 0xEE749A62 ]; // Coefficients for approximation to erfc on [1/0.35, 28] var RB = [ -7.99283237680523006574e-1, // 0xBFE993BA, 0x70C285DE -1.77579549177547519889e1, // 0xC031C209, 0x555F995A -1.60636384855821916062e2, // 0xC064145D, 0x43C5ED98 -6.37566443368389627722e2, // 0xC083EC88, 0x1375F228 -1.02509513161107724954e3, // 0xC0900461, 0x6A2E5992 -4.83519191608651397019e2 // 0xC07E384E, 0x9BDC383F ]; var SB = [ 3.03380607434824582924e1, // 0x403E568B, 0x261D5190 3.25792512996573918826e2, // 0x40745CAE, 0x221B9F0A 1.53672958608443695994e3, // 0x409802EB, 0x189D5118 3.19985821950859553908e3, // 0x40A8FFB7, 0x688C246A 2.55305040643316442583e3, // 0x40A3F219, 0xCEDF3BE6 4.74528541206955367215e2, // 0x407DA874, 0xE79FE763 -2.24409524465858183362e1 // 0xC03670E2, 0x42712D62 ]; // Header to add to output files: var header = licenseHeader( 'Apache-2.0', 'js', { 'year': currentYear(), 'copyright': 'The Stdlib Authors' }); header += '\n/* This is a generated file. Do not edit directly. */\n'; // FUNCTIONS // /** * Inserts a compiled function into file content. * * @private * @param {string} text - source content * @param {string} id - function identifier * @param {string} str - function string * @returns {string} updated content */ function insert( text, id, str ) { var before; var after; var begin; var end; begin = '// BEGIN: '+id; end = '// END: '+id; before = substringBefore( text, begin ); after = substringAfter( text, end ); return format( '%s// BEGIN: %s\n\n%s\n%s%s', before, id, str, end, after ); } // MAIN // /** * Main execution sequence. * * @private */ function main() { var fpath; var copts; var opts; var file; var str; opts = { 'encoding': 'utf8' }; fpath = resolve( __dirname, '..', 'lib', 'polyval_pp.js' ); str = header + compile( PP ); writeFileSync( fpath, str, opts ); fpath = resolve( __dirname, '..', 'lib', 'polyval_qq.js' ); str = header + compile( QQ ); writeFileSync( fpath, str, opts ); fpath = resolve( __dirname, '..', 'lib', 'polyval_pa.js' ); str = header + compile( PA ); writeFileSync( fpath, str, opts ); fpath = resolve( __dirname, '..', 'lib', 'polyval_qa.js' ); str = header + compile( QA ); writeFileSync( fpath, str, opts ); fpath = resolve( __dirname, '..', 'lib', 'polyval_ra.js' ); str = header + compile( RA ); writeFileSync( fpath, str, opts ); fpath = resolve( __dirname, '..', 'lib', 'polyval_sa.js' ); str = header + compile( SA ); writeFileSync( fpath, str, opts ); fpath = resolve( __dirname, '..', 'lib', 'polyval_rb.js' ); str = header + compile( RB ); writeFileSync( fpath, str, opts ); fpath = resolve( __dirname, '..', 'lib', 'polyval_sb.js' ); str = header + compile( SB ); writeFileSync( fpath, str, opts ); copts = { 'dtype': 'double', 'name': '' }; fpath = resolve( __dirname, '..', 'src', 'main.c' ); file = readFileSync( fpath, opts ); copts.name = 'polyval_pa'; str = compileC( PA, copts ); file = insert( file, copts.name, str ); copts.name = 'polyval_pp'; str = compileC( PP, copts ); file = insert( file, copts.name, str ); copts.name = 'polyval_qa'; str = compileC(QA, copts ); file = insert( file, copts.name, str ); copts.name = 'polyval_qq'; str = compileC( QQ, copts ); file = insert( file, copts.name, str ); copts.name = 'polyval_ra'; str = compileC( RA, copts ); file = insert( file, copts.name, str ); copts.name = 'polyval_rb'; str = compileC( RB, copts ); file = insert( file, copts.name, str ); copts.name = 'polyval_sa'; str = compileC( SA, copts ); file = insert( file, copts.name, str ); copts.name = 'polyval_sb'; str = compileC( SB, copts ); file = insert( file, copts.name, str ); writeFileSync( fpath, file, opts ); } main();