/** * @license Apache-2.0 * * Copyright (c) 2018 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-evalrational-compile' ); var compileC = require( '@stdlib/math-base-tools-evalrational-compile-c' ); // VARIABLES // // Polynomial coefficients ordered in ascending degree... // Coefficients for erfinv on [0, 0.5]: var P1 = [ -5.08781949658280665617e-4, -8.36874819741736770379e-3, 3.34806625409744615033e-2, -1.26926147662974029034e-2, -3.65637971411762664006e-2, 2.19878681111168899165e-2, 8.22687874676915743155e-3, -5.38772965071242932965e-3, 0.0, 0.0 ]; var Q1 = [ 1.0, -9.70005043303290640362e-1, -1.56574558234175846809, 1.56221558398423026363, 6.62328840472002992063e-1, -7.1228902341542847553e-1, -5.27396382340099713954e-2, 7.95283687341571680018e-2, -2.33393759374190016776e-3, 8.86216390456424707504e-4 ]; // Coefficients for erfinv for 0.5 > 1-x >= 0: var P2 = [ -2.02433508355938759655e-1, 1.05264680699391713268e-1, 8.37050328343119927838, 1.76447298408374015486e1, -1.88510648058714251895e1, -4.46382324441786960818e1, 1.7445385985570866523e1, 2.11294655448340526258e1, -3.67192254707729348546 ]; var Q2 = [ 1.0, 6.24264124854247537712, 3.9713437953343869095, -2.86608180499800029974e1, -2.01432634680485188801e1, 4.85609213108739935468e1, 1.08268667355460159008e1, -2.26436933413139721736e1, 1.72114765761200282724 ]; // Coefficients for erfinv for sqrt( -log(1-x) ): var P3 = [ -1.31102781679951906451e-1, -1.63794047193317060787e-1, 1.17030156341995252019e-1, 3.87079738972604337464e-1, 3.37785538912035898924e-1, 1.42869534408157156766e-1, 2.90157910005329060432e-2, 2.14558995388805277169e-3, -6.79465575181126350155e-7, 2.85225331782217055858e-8, -6.81149956853776992068e-10 ]; var Q3 = [ 1.0, 3.46625407242567245975, 5.38168345707006855425, 4.77846592945843778382, 2.59301921623620271374, 8.48854343457902036425e-1, 1.52264338295331783612e-1, 1.105924229346489121e-2, 0.0, 0.0, 0.0 ]; var P4 = [ -3.50353787183177984712e-2, -2.22426529213447927281e-3, 1.85573306514231072324e-2, 9.50804701325919603619e-3, 1.87123492819559223345e-3, 1.57544617424960554631e-4, 4.60469890584317994083e-6, -2.30404776911882601748e-10, 2.66339227425782031962e-12 ]; var Q4 = [ 1.0, 1.3653349817554063097, 7.62059164553623404043e-1, 2.20091105764131249824e-1, 3.41589143670947727934e-2, 2.63861676657015992959e-3, 7.64675292302794483503e-5, 0.0, 0.0 ]; var P5 = [ -1.67431005076633737133e-2, -1.12951438745580278863e-3, 1.05628862152492910091e-3, 2.09386317487588078668e-4, 1.49624783758342370182e-5, 4.49696789927706453732e-7, 4.62596163522878599135e-9, -2.81128735628831791805e-14, 9.9055709973310326855e-17 ]; var Q5 = [ 1.0, 5.91429344886417493481e-1, 1.38151865749083321638e-1, 1.60746087093676504695e-2, 9.64011807005165528527e-4, 2.75335474764726041141e-5, 2.82243172016108031869e-7, 0.0, 0.0 ]; // 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', 'rational_p1q1.js' ); str = header + compile( P1, Q1 ); writeFileSync( fpath, str, opts ); fpath = resolve( __dirname, '..', 'lib', 'rational_p2q2.js' ); str = header + compile( P2, Q2 ); writeFileSync( fpath, str, opts ); fpath = resolve( __dirname, '..', 'lib', 'rational_p3q3.js' ); str = header + compile( P3, Q3 ); writeFileSync( fpath, str, opts ); fpath = resolve( __dirname, '..', 'lib', 'rational_p4q4.js' ); str = header + compile( P4, Q4 ); writeFileSync( fpath, str, opts ); fpath = resolve( __dirname, '..', 'lib', 'rational_p5q5.js' ); str = header + compile( P5, Q5 ); writeFileSync( fpath, str, opts ); copts = { 'dtype': 'double', 'name': '' }; fpath = resolve( __dirname, '..', 'src', 'main.c' ); file = readFileSync( fpath, opts ); copts.name = 'rational_p1q1'; str = compileC( P1, Q1, copts ); file = insert( file, copts.name, str ); copts.name = 'rational_p2q2'; str = compileC( P2, Q2, copts ); file = insert( file, copts.name, str ); copts.name = 'rational_p3q3'; str = compileC( P3, Q3, copts ); file = insert( file, copts.name, str ); copts.name = 'rational_p4q4'; str = compileC( P4, Q4, copts ); file = insert( file, copts.name, str ); copts.name = 'rational_p5q5'; str = compileC( P5, Q5, copts ); file = insert( file, copts.name, str ); writeFileSync( fpath, file, opts ); } main();