Skip to content

Commit f36b80d

Browse files
gunjjoshikgryte
andauthored
refactor: add support for ratios evaluating as infinity in math/base/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>
1 parent 26a903f commit f36b80d

File tree

8 files changed

+1270
-3
lines changed

8 files changed

+1270
-3
lines changed

lib/node_modules/@stdlib/math/base/tools/evalrational-compile-c/lib/main.js

Lines changed: 12 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -25,6 +25,8 @@ var readFile = require( '@stdlib/fs/read-file' ).sync;
2525
var replace = require( '@stdlib/string/replace' );
2626
var isInteger = require( '@stdlib/assert/is-integer' ).isPrimitive;
2727
var uppercase = require( '@stdlib/string/base/uppercase' );
28+
var PINF = require( '@stdlib/constants/float64/pinf' );
29+
var NINF = require( '@stdlib/constants/float64/ninf' );
2830

2931

3032
// VARIABLES //
@@ -51,7 +53,14 @@ var NAN_TEMPLATE = readFile( join( dir, 'nan.c.txt' ), opts );
5153
* @returns {string} serialized value
5254
*/
5355
function value2string( x ) {
54-
var str = x.toString();
56+
var str;
57+
if ( x === PINF ) {
58+
return '1.0{{dtype_suffix}} / 0.0{{dtype_suffix}}';
59+
}
60+
if ( x === NINF ) {
61+
return '-1.0{{dtype_suffix}} / 0.0{{dtype_suffix}}';
62+
}
63+
str = x.toString();
5564
if ( isInteger( x ) ) {
5665
str += '.0';
5766
}
@@ -224,7 +233,7 @@ function compile( P, Q, options ) {
224233
if ( n > 500 ) {
225234
str = replace( LOOP_TEMPLATE, '{{P}}', array2list( P ) );
226235
str = replace( str, '{{Q}}', array2list( Q ) );
227-
str = replace( str, '{{ratio}}', value2string( P[0]/Q[0] ) );
236+
str = replace( str, '{{ratio}}', value2string( P[0] / Q[0] ) );
228237
str = replace( str, '{{num_coefficients}}', n.toString() );
229238
str = replace( str, '{{dtype}}', opts.dtype );
230239
str = replace( str, '{{dtype_suffix}}', opts.suffix );
@@ -236,7 +245,7 @@ function compile( P, Q, options ) {
236245
str = replace( str, '{{Q_ASCENDING}}', hornerAscending( Q ) );
237246
str = replace( str, '{{P_DESCENDING}}', hornerDescending( P ) );
238247
str = replace( str, '{{Q_DESCENDING}}', hornerDescending( Q ) );
239-
str = replace( str, '{{ratio}}', value2string( P[0]/Q[0] ) );
248+
str = replace( str, '{{ratio}}', value2string( P[0] / Q[0] ) );
240249
str = replace( str, '{{dtype}}', opts.dtype );
241250
str = replace( str, '{{dtype_suffix}}', opts.suffix );
242251
return replace( str, '{{fname}}', opts.name );
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,9 @@
1+
/**
2+
* Evaluates a rational function.
3+
*
4+
* @param x value at which to evaluate the rational function
5+
* @return evaluated rational function
6+
*/
7+
static double evalrational() {
8+
return -1.0 / 0.0;
9+
}
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,9 @@
1+
/**
2+
* Evaluates a rational function.
3+
*
4+
* @param x value at which to evaluate the rational function
5+
* @return evaluated rational function
6+
*/
7+
static float rational123() {
8+
return -1.0f / 0.0f;
9+
}
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,9 @@
1+
/**
2+
* Evaluates a rational function.
3+
*
4+
* @param x value at which to evaluate the rational function
5+
* @return evaluated rational function
6+
*/
7+
static double evalrational() {
8+
return 1.0 / 0.0;
9+
}
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,9 @@
1+
/**
2+
* Evaluates a rational function.
3+
*
4+
* @param x value at which to evaluate the rational function
5+
* @return evaluated rational function
6+
*/
7+
static float rational123() {
8+
return 1.0f / 0.0f;
9+
}
Lines changed: 36 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,36 @@
1+
/**
2+
* Evaluates a rational function (i.e., the ratio of two polynomials described by the coefficients stored in \\(P\\) and \\(Q\\)).
3+
*
4+
* ## Notes
5+
*
6+
* - Coefficients should be sorted in ascending degree.
7+
* - The implementation uses [Horner's rule][horners-method] for efficient computation.
8+
*
9+
* [horners-method]: https://en.wikipedia.org/wiki/Horner%27s_method
10+
*
11+
* @param x value at which to evaluate the rational function
12+
* @return evaluated rational function
13+
*/
14+
static double evalrational( const double x ) {
15+
double ax;
16+
double ix;
17+
double s1;
18+
double s2;
19+
if ( x == 0.0 ) {
20+
return -1.0 / 0.0;
21+
}
22+
if ( x < 0.0 ) {
23+
ax = -x;
24+
} else {
25+
ax = x;
26+
}
27+
if ( ax <= 1.0 ) {
28+
s1 = -1.0 + (x * (2.5 + (x * (3.14 + (x * -1.0)))));
29+
s2 = 0.0 + (x * (-3.5 + (x * (2.2 + (x * 1.25)))));
30+
} else {
31+
ix = 1.0 / x;
32+
s1 = -1.0 + (ix * (3.14 + (ix * (2.5 + (ix * -1.0)))));
33+
s2 = 1.25 + (ix * (2.2 + (ix * (-3.5 + (ix * 0.0)))));
34+
}
35+
return s1 / s2;
36+
}

0 commit comments

Comments
 (0)