Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

refactor: add support for ratios evaluating as infinity in math/base/tools/evalrational-compile-c #1970

Merged
merged 4 commits into from
Mar 20, 2024
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
Original file line number Diff line number Diff line change
Expand Up @@ -25,6 +25,8 @@ var readFile = require( '@stdlib/fs/read-file' ).sync;
var replace = require( '@stdlib/string/replace' );
var isInteger = require( '@stdlib/assert/is-integer' ).isPrimitive;
var uppercase = require( '@stdlib/string/base/uppercase' );
var PINF = require( '@stdlib/constants/float64/pinf' );
var NINF = require( '@stdlib/constants/float64/ninf' );


// VARIABLES //
Expand All @@ -51,7 +53,14 @@ var NAN_TEMPLATE = readFile( join( dir, 'nan.c.txt' ), opts );
* @returns {string} serialized value
*/
function value2string( x ) {
var str = x.toString();
var str;
if ( x === PINF ) {
kgryte marked this conversation as resolved.
Show resolved Hide resolved
return '1.0{{dtype_suffix}} / 0.0{{dtype_suffix}}';
}
if ( x === NINF ) {
return '-1.0{{dtype_suffix}} / 0.0{{dtype_suffix}}';
}
str = x.toString();
if ( isInteger( x ) ) {
str += '.0';
}
Expand Down Expand Up @@ -224,7 +233,7 @@ function compile( P, Q, options ) {
if ( n > 500 ) {
str = replace( LOOP_TEMPLATE, '{{P}}', array2list( P ) );
str = replace( str, '{{Q}}', array2list( Q ) );
str = replace( str, '{{ratio}}', value2string( P[0]/Q[0] ) );
str = replace( str, '{{ratio}}', value2string( P[0] / Q[0] ) );
str = replace( str, '{{num_coefficients}}', n.toString() );
str = replace( str, '{{dtype}}', opts.dtype );
str = replace( str, '{{dtype_suffix}}', opts.suffix );
Expand All @@ -236,7 +245,7 @@ function compile( P, Q, options ) {
str = replace( str, '{{Q_ASCENDING}}', hornerAscending( Q ) );
str = replace( str, '{{P_DESCENDING}}', hornerDescending( P ) );
str = replace( str, '{{Q_DESCENDING}}', hornerDescending( Q ) );
str = replace( str, '{{ratio}}', value2string( P[0]/Q[0] ) );
str = replace( str, '{{ratio}}', value2string( P[0] / Q[0] ) );
str = replace( str, '{{dtype}}', opts.dtype );
str = replace( str, '{{dtype_suffix}}', opts.suffix );
return replace( str, '{{fname}}', opts.name );
Expand Down
Original file line number Diff line number Diff line change
@@ -0,0 +1,9 @@
/**
* Evaluates a rational function.
*
* @param x value at which to evaluate the rational function
* @return evaluated rational function
*/
static double evalrational() {
return -1.0 / 0.0;
}
Original file line number Diff line number Diff line change
@@ -0,0 +1,9 @@
/**
* Evaluates a rational function.
*
* @param x value at which to evaluate the rational function
* @return evaluated rational function
*/
static float rational123() {
return -1.0f / 0.0f;
}
Original file line number Diff line number Diff line change
@@ -0,0 +1,9 @@
/**
* Evaluates a rational function.
*
* @param x value at which to evaluate the rational function
* @return evaluated rational function
*/
static double evalrational() {
return 1.0 / 0.0;
}
Original file line number Diff line number Diff line change
@@ -0,0 +1,9 @@
/**
* Evaluates a rational function.
*
* @param x value at which to evaluate the rational function
* @return evaluated rational function
*/
static float rational123() {
return 1.0f / 0.0f;
}
Original file line number Diff line number Diff line change
@@ -0,0 +1,36 @@
/**
* Evaluates a rational function (i.e., the ratio of two polynomials described by the coefficients stored in \\(P\\) and \\(Q\\)).
*
* ## Notes
*
* - Coefficients should be sorted in ascending degree.
* - The implementation uses [Horner's rule][horners-method] for efficient computation.
*
* [horners-method]: https://en.wikipedia.org/wiki/Horner%27s_method
*
* @param x value at which to evaluate the rational function
* @return evaluated rational function
*/
static double evalrational( const double x ) {
double ax;
double ix;
double s1;
double s2;
if ( x == 0.0 ) {
return -1.0 / 0.0;
}
if ( x < 0.0 ) {
ax = -x;
} else {
ax = x;
}
if ( ax <= 1.0 ) {
s1 = -1.0 + (x * (2.5 + (x * (3.14 + (x * -1.0)))));
s2 = 0.0 + (x * (-3.5 + (x * (2.2 + (x * 1.25)))));
} else {
ix = 1.0 / x;
s1 = -1.0 + (ix * (3.14 + (ix * (2.5 + (ix * -1.0)))));
s2 = 1.25 + (ix * (2.2 + (ix * (-3.5 + (ix * 0.0)))));
}
return s1 / s2;
}
Loading
Loading