stdlib
stdlib copied to clipboard
feat: add C implementation for `@stdlib/math/base/special/erfcx`
Description
What is the purpose of this pull request?
This pull request:
- adds C implementation for
@stdlib/math/base/special/erfcx
Related Issues
Does this pull request have any related issues?
This pull request:
- resolves #1949
Questions
Any questions for reviewers of this pull request?
No.
Other
Any other information relevant to this pull request? This may include screenshots, references, and/or implementation notes.
No.
Checklist
Please ensure the following tasks are completed before submitting this pull request.
- [x] Read, understood, and followed the contributing guidelines.
@stdlib-js/reviewers
const fs = require('fs');
const convertJstoC = () => {
const string = fs.readFileSync("./erfcx_y100.js", "utf8");
const regex = /function/g;
const arg = /\(\s*([^)]+?)\s*\)/g; // regex to get args inside ()
const functionSplits = string.split("*/\n"); // split string using */ of the comment
const replaced = functionSplits.map((func) => {
const args = func.match(arg);
func = func.replaceAll("* @param {number} t -","* @param t ").replaceAll(" {number}","").replaceAll("* @private\n","").replaceAll("@returns ", "@return ").replaceAll(regex,"static double"); // convert all jsDoc types to C-Deoxygen as well as convert functions to statis double
if(args) {
func = func.replaceAll(args[0], "( const double t )"); // convert arg into const double t ( we used arg[0] as arg[1] is mathematical part of p functions as they also match the regex
}
return func; // return updated
});
console.log(replaced) // for debug purpose
return replaced.join("*/\n"); // join replaced string to get full string
}
const news = convertJstoC();
fs.writeFileSync('erfcx_chebyshev.c', news); // save it to another file from where we can copy it to main codebase
created this script to convert all p0 to p100 functions from JavaScript to C as well as update doc to follow C-Deoxygen format
this code still had JavaScript elements such as var
, "use-strict"
etc as this script was purely to convert functions from JavaScript to C
and rest of the code was written manually
I have not added this script on pr as this didnt seem important hence added it in comment as an insight
screenshot for output
edit: Updated Script to match stdlib conventions as well as updated screenshot output to show visual output
updated all missed @\returns to @\return
now this pr is ready for review
@kgryte updated all the suggested changes, as well as updated script in the comment and now it should follow the convention:
also updated screenshot for a visual output
@kgryte any particular reason why we are not using scripts/evalPoly.js
here?
any particular reason why we are not using
scripts/evaly_poly.js
here?
Hi @Pranavchiku, I can't seem to find scripts folder in erfcx directory ???
any particular reason why we are not using scripts/evalPoly.js here?
No. This was likely an oversight during the initial implementation. I did a quick check and it looks like Horner's method works with the Chebyshev polynomials in the source files. There was an open comment on the commit by @Planeshifter that was never resolved regarding this: https://github.com/stdlib-js/stdlib/commit/9c1a9d6a78b2342c387aaccc905b9b6bee1b86d7#r78996426.
Accordingly, we should be able to refactor the implementation to use our polynomial function generation machinery, including via the use of scripts/evalpoly.js
.
@USERSATOSHI Is this something you want to take on or should we get this merged and we should refactor on our side?
Okay, thanks for confirming :)
sure if there is a reference/ guide that I can follow
I will be able to take this on
@USERSATOSHI I'd consult other packages in math/base/special
which have a scripts directory and contain a script for evalpoly
. That script is more or less the same for every relevant package needing to generate polynomial functions. The main bit of manual labor will be copying over the coefficients into arrays, rather than inlined as they are now in the source files.
thanks for info, this can be implemented, but I had a doubt
writing the evalpoly and using it will lead to general of 101 js files
each file for each range ( like from [0.01, 0.02) )
is this needed? cause it would be 1 file per polynomial.
or we can do evalpoly to use the current single erfcx_y100.js and compile & insert that into main.c
what's your review on this
@USERSATOSHI I think it is better to skip the script refactoring and just leave as is. We can always revisit in the future if, and when, we need to update polynomial coefficients.
okay, then we can revisit this in future
@gunjjoshi You should now have permissions to take over this PR and directly edit/commit. LMK if not.
Made some changes, should be good to go!