Skip to content

Commit

Permalink
refactor: add support for ratios evaluating as infinity in `math/base…
Browse files Browse the repository at this point in the history
…/tools/evalrational-compile-c`

PR-URL: 	#1970
Ref: #1834 (comment)
Co-authored-by: Athan Reines <kgryte@gmail.com>
Reviewed-by: Athan Reines <kgryte@gmail.com> 
Signed-off-by: Athan Reines <kgryte@gmail.com>
  • Loading branch information
gunjjoshi and kgryte authored Mar 20, 2024
1 parent 26a903f commit f36b80d
Show file tree
Hide file tree
Showing 8 changed files with 1,270 additions and 3 deletions.
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 ) {
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

1 comment on commit f36b80d

@stdlib-bot
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Coverage Report

Package Statements Branches Functions Lines
math/base/tools/evalrational-compile-c $\color{green}300/300$
$\color{green}+100.00\%$
$\color{green}38/38$
$\color{green}+100.00\%$
$\color{green}5/5$
$\color{green}+100.00\%$
$\color{green}300/300$
$\color{green}+100.00\%$

The above coverage report was generated for the changes in this push.

Please sign in to comment.