From 53f99643472d1dabec613815c3ea8f81e529a38f Mon Sep 17 00:00:00 2001 From: GUNJ JOSHI Date: Wed, 20 Mar 2024 23:15:17 +0530 Subject: [PATCH 1/4] refactor: modified evalrational-compile-c --- .../tools/evalrational-compile-c/lib/main.js | 14 ++++++++-- .../tools/evalrational-compile-c/test/test.js | 28 +++++++++++++++++++ 2 files changed, 39 insertions(+), 3 deletions(-) diff --git a/lib/node_modules/@stdlib/math/base/tools/evalrational-compile-c/lib/main.js b/lib/node_modules/@stdlib/math/base/tools/evalrational-compile-c/lib/main.js index cec10b4fe8c..eca164837ec 100644 --- a/lib/node_modules/@stdlib/math/base/tools/evalrational-compile-c/lib/main.js +++ b/lib/node_modules/@stdlib/math/base/tools/evalrational-compile-c/lib/main.js @@ -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 // @@ -52,7 +54,13 @@ var NAN_TEMPLATE = readFile( join( dir, 'nan.c.txt' ), opts ); */ function value2string( x ) { var str = x.toString(); - if ( isInteger( x ) ) { + if ( x === PINF ) { + str = '1.0 / 0.0'; + } + else if ( x === NINF ) { + str = '-1.0 / 0.0'; + } + else if ( isInteger( x ) ) { str += '.0'; } str += '{{dtype_suffix}}'; @@ -224,7 +232,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 ); @@ -236,7 +244,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 ); diff --git a/lib/node_modules/@stdlib/math/base/tools/evalrational-compile-c/test/test.js b/lib/node_modules/@stdlib/math/base/tools/evalrational-compile-c/test/test.js index bb9dc850be2..9f1d254c59e 100644 --- a/lib/node_modules/@stdlib/math/base/tools/evalrational-compile-c/test/test.js +++ b/lib/node_modules/@stdlib/math/base/tools/evalrational-compile-c/test/test.js @@ -439,3 +439,31 @@ tape( 'the function returns a function string which evaluates a rational functio t.end(); }); + +tape( 'the function returns a function string which handles positive infinity correctly', opts, function test( t ) { + var expected; + var str; + var P; + var Q; + + expected = '1.0 / 0.0'; + P = [ 1.0, 2.0 ]; + Q = [ 0.0, 1.0 ]; + str = compile( P, Q ); + t.ok( str.includes( expected ), 'function string handles positive infinity correctly' ); + t.end(); +}); + +tape( 'the function returns a function string which handles negative infinity correctly', opts, function test( t ) { + var expected; + var str; + var P; + var Q; + + expected = '-1.0 / 0.0'; + P = [ -1.0, -2.0 ]; + Q = [ 0.0, 1.0 ]; + str = compile( P, Q ); + t.ok( str.includes( expected ), 'function string handles negative infinity correctly' ); + t.end(); +}); From 29dc6fa711d85135ae1071ca53d7733ce71884fa Mon Sep 17 00:00:00 2001 From: Athan Date: Wed, 20 Mar 2024 11:38:07 -0700 Subject: [PATCH 2/4] fix: include dtype suffix when serializing infinities Signed-off-by: Athan --- .../base/tools/evalrational-compile-c/lib/main.js | 11 ++++++----- 1 file changed, 6 insertions(+), 5 deletions(-) diff --git a/lib/node_modules/@stdlib/math/base/tools/evalrational-compile-c/lib/main.js b/lib/node_modules/@stdlib/math/base/tools/evalrational-compile-c/lib/main.js index eca164837ec..7f823e7d84b 100644 --- a/lib/node_modules/@stdlib/math/base/tools/evalrational-compile-c/lib/main.js +++ b/lib/node_modules/@stdlib/math/base/tools/evalrational-compile-c/lib/main.js @@ -53,14 +53,15 @@ 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 ) { - str = '1.0 / 0.0'; + return '1.0' + {{dtype_suffix}} + ' / 0.0' + {{dtype_suffix}}; } - else if ( x === NINF ) { - str = '-1.0 / 0.0'; + if ( x === NINF ) { + return '-1.0' + {{dtype_suffix}} + ' / 0.0' + {{dtype_suffix}}; } - else if ( isInteger( x ) ) { + str = x.toString(); + if ( isInteger( x ) ) { str += '.0'; } str += '{{dtype_suffix}}'; From 857a8eeacfe2c37f90c9221e37f49cad7f5afb18 Mon Sep 17 00:00:00 2001 From: Athan Date: Wed, 20 Mar 2024 12:25:28 -0700 Subject: [PATCH 3/4] fix: address syntax errors Signed-off-by: Athan --- .../math/base/tools/evalrational-compile-c/lib/main.js | 4 ++-- 1 file changed, 2 insertions(+), 2 deletions(-) diff --git a/lib/node_modules/@stdlib/math/base/tools/evalrational-compile-c/lib/main.js b/lib/node_modules/@stdlib/math/base/tools/evalrational-compile-c/lib/main.js index 7f823e7d84b..9cdd4486212 100644 --- a/lib/node_modules/@stdlib/math/base/tools/evalrational-compile-c/lib/main.js +++ b/lib/node_modules/@stdlib/math/base/tools/evalrational-compile-c/lib/main.js @@ -55,10 +55,10 @@ var NAN_TEMPLATE = readFile( join( dir, 'nan.c.txt' ), opts ); function value2string( x ) { var str; if ( x === PINF ) { - return '1.0' + {{dtype_suffix}} + ' / 0.0' + {{dtype_suffix}}; + return '1.0{{dtype_suffix}} / 0.0{{dtype_suffix}}'; } if ( x === NINF ) { - return '-1.0' + {{dtype_suffix}} + ' / 0.0' + {{dtype_suffix}}; + return '-1.0{{dtype_suffix}} / 0.0{{dtype_suffix}}'; } str = x.toString(); if ( isInteger( x ) ) { From 4148fc9aeec0e07747d0b264e7c3c13754933b4b Mon Sep 17 00:00:00 2001 From: Athan Reines Date: Wed, 20 Mar 2024 12:49:53 -0700 Subject: [PATCH 4/4] test: add test fixtures and update tests to address all function types --- .../coefficient_ratio_negative_infinity.c.txt | 9 + ...cient_ratio_negative_infinity_custom.c.txt | 9 + .../coefficient_ratio_positive_infinity.c.txt | 9 + ...cient_ratio_positive_infinity_custom.c.txt | 9 + .../test/fixtures/evalrational4.c.txt | 36 + .../test/fixtures/loop3.c.txt | 1057 +++++++++++++++++ .../tools/evalrational-compile-c/test/test.js | 139 ++- 7 files changed, 1249 insertions(+), 19 deletions(-) create mode 100644 lib/node_modules/@stdlib/math/base/tools/evalrational-compile-c/test/fixtures/coefficient_ratio_negative_infinity.c.txt create mode 100644 lib/node_modules/@stdlib/math/base/tools/evalrational-compile-c/test/fixtures/coefficient_ratio_negative_infinity_custom.c.txt create mode 100644 lib/node_modules/@stdlib/math/base/tools/evalrational-compile-c/test/fixtures/coefficient_ratio_positive_infinity.c.txt create mode 100644 lib/node_modules/@stdlib/math/base/tools/evalrational-compile-c/test/fixtures/coefficient_ratio_positive_infinity_custom.c.txt create mode 100644 lib/node_modules/@stdlib/math/base/tools/evalrational-compile-c/test/fixtures/evalrational4.c.txt create mode 100644 lib/node_modules/@stdlib/math/base/tools/evalrational-compile-c/test/fixtures/loop3.c.txt diff --git a/lib/node_modules/@stdlib/math/base/tools/evalrational-compile-c/test/fixtures/coefficient_ratio_negative_infinity.c.txt b/lib/node_modules/@stdlib/math/base/tools/evalrational-compile-c/test/fixtures/coefficient_ratio_negative_infinity.c.txt new file mode 100644 index 00000000000..0860b66d4dd --- /dev/null +++ b/lib/node_modules/@stdlib/math/base/tools/evalrational-compile-c/test/fixtures/coefficient_ratio_negative_infinity.c.txt @@ -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; +} diff --git a/lib/node_modules/@stdlib/math/base/tools/evalrational-compile-c/test/fixtures/coefficient_ratio_negative_infinity_custom.c.txt b/lib/node_modules/@stdlib/math/base/tools/evalrational-compile-c/test/fixtures/coefficient_ratio_negative_infinity_custom.c.txt new file mode 100644 index 00000000000..631da151171 --- /dev/null +++ b/lib/node_modules/@stdlib/math/base/tools/evalrational-compile-c/test/fixtures/coefficient_ratio_negative_infinity_custom.c.txt @@ -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; +} diff --git a/lib/node_modules/@stdlib/math/base/tools/evalrational-compile-c/test/fixtures/coefficient_ratio_positive_infinity.c.txt b/lib/node_modules/@stdlib/math/base/tools/evalrational-compile-c/test/fixtures/coefficient_ratio_positive_infinity.c.txt new file mode 100644 index 00000000000..aad954eb346 --- /dev/null +++ b/lib/node_modules/@stdlib/math/base/tools/evalrational-compile-c/test/fixtures/coefficient_ratio_positive_infinity.c.txt @@ -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; +} diff --git a/lib/node_modules/@stdlib/math/base/tools/evalrational-compile-c/test/fixtures/coefficient_ratio_positive_infinity_custom.c.txt b/lib/node_modules/@stdlib/math/base/tools/evalrational-compile-c/test/fixtures/coefficient_ratio_positive_infinity_custom.c.txt new file mode 100644 index 00000000000..6549a147b46 --- /dev/null +++ b/lib/node_modules/@stdlib/math/base/tools/evalrational-compile-c/test/fixtures/coefficient_ratio_positive_infinity_custom.c.txt @@ -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; +} diff --git a/lib/node_modules/@stdlib/math/base/tools/evalrational-compile-c/test/fixtures/evalrational4.c.txt b/lib/node_modules/@stdlib/math/base/tools/evalrational-compile-c/test/fixtures/evalrational4.c.txt new file mode 100644 index 00000000000..df0d513506f --- /dev/null +++ b/lib/node_modules/@stdlib/math/base/tools/evalrational-compile-c/test/fixtures/evalrational4.c.txt @@ -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; +} diff --git a/lib/node_modules/@stdlib/math/base/tools/evalrational-compile-c/test/fixtures/loop3.c.txt b/lib/node_modules/@stdlib/math/base/tools/evalrational-compile-c/test/fixtures/loop3.c.txt new file mode 100644 index 00000000000..fd2086693f0 --- /dev/null +++ b/lib/node_modules/@stdlib/math/base/tools/evalrational-compile-c/test/fixtures/loop3.c.txt @@ -0,0 +1,1057 @@ +static const double EVALRATIONAL_COEFFICIENTS_P = { + 1.0, + 1.0, + 2.0, + 3.0, + 4.0, + 5.0, + 6.0, + 7.0, + 8.0, + 9.0, + 10.0, + 11.0, + 12.0, + 13.0, + 14.0, + 15.0, + 16.0, + 17.0, + 18.0, + 19.0, + 20.0, + 21.0, + 22.0, + 23.0, + 24.0, + 25.0, + 26.0, + 27.0, + 28.0, + 29.0, + 30.0, + 31.0, + 32.0, + 33.0, + 34.0, + 35.0, + 36.0, + 37.0, + 38.0, + 39.0, + 40.0, + 41.0, + 42.0, + 43.0, + 44.0, + 45.0, + 46.0, + 47.0, + 48.0, + 49.0, + 50.0, + 51.0, + 52.0, + 53.0, + 54.0, + 55.0, + 56.0, + 57.0, + 58.0, + 59.0, + 60.0, + 61.0, + 62.0, + 63.0, + 64.0, + 65.0, + 66.0, + 67.0, + 68.0, + 69.0, + 70.0, + 71.0, + 72.0, + 73.0, + 74.0, + 75.0, + 76.0, + 77.0, + 78.0, + 79.0, + 80.0, + 81.0, + 82.0, + 83.0, + 84.0, + 85.0, + 86.0, + 87.0, + 88.0, + 89.0, + 90.0, + 91.0, + 92.0, + 93.0, + 94.0, + 95.0, + 96.0, + 97.0, + 98.0, + 99.0, + 100.0, + 101.0, + 102.0, + 103.0, + 104.0, + 105.0, + 106.0, + 107.0, + 108.0, + 109.0, + 110.0, + 111.0, + 112.0, + 113.0, + 114.0, + 115.0, + 116.0, + 117.0, + 118.0, + 119.0, + 120.0, + 121.0, + 122.0, + 123.0, + 124.0, + 125.0, + 126.0, + 127.0, + 128.0, + 129.0, + 130.0, + 131.0, + 132.0, + 133.0, + 134.0, + 135.0, + 136.0, + 137.0, + 138.0, + 139.0, + 140.0, + 141.0, + 142.0, + 143.0, + 144.0, + 145.0, + 146.0, + 147.0, + 148.0, + 149.0, + 150.0, + 151.0, + 152.0, + 153.0, + 154.0, + 155.0, + 156.0, + 157.0, + 158.0, + 159.0, + 160.0, + 161.0, + 162.0, + 163.0, + 164.0, + 165.0, + 166.0, + 167.0, + 168.0, + 169.0, + 170.0, + 171.0, + 172.0, + 173.0, + 174.0, + 175.0, + 176.0, + 177.0, + 178.0, + 179.0, + 180.0, + 181.0, + 182.0, + 183.0, + 184.0, + 185.0, + 186.0, + 187.0, + 188.0, + 189.0, + 190.0, + 191.0, + 192.0, + 193.0, + 194.0, + 195.0, + 196.0, + 197.0, + 198.0, + 199.0, + 200.0, + 201.0, + 202.0, + 203.0, + 204.0, + 205.0, + 206.0, + 207.0, + 208.0, + 209.0, + 210.0, + 211.0, + 212.0, + 213.0, + 214.0, + 215.0, + 216.0, + 217.0, + 218.0, + 219.0, + 220.0, + 221.0, + 222.0, + 223.0, + 224.0, + 225.0, + 226.0, + 227.0, + 228.0, + 229.0, + 230.0, + 231.0, + 232.0, + 233.0, + 234.0, + 235.0, + 236.0, + 237.0, + 238.0, + 239.0, + 240.0, + 241.0, + 242.0, + 243.0, + 244.0, + 245.0, + 246.0, + 247.0, + 248.0, + 249.0, + 250.0, + 251.0, + 252.0, + 253.0, + 254.0, + 255.0, + 256.0, + 257.0, + 258.0, + 259.0, + 260.0, + 261.0, + 262.0, + 263.0, + 264.0, + 265.0, + 266.0, + 267.0, + 268.0, + 269.0, + 270.0, + 271.0, + 272.0, + 273.0, + 274.0, + 275.0, + 276.0, + 277.0, + 278.0, + 279.0, + 280.0, + 281.0, + 282.0, + 283.0, + 284.0, + 285.0, + 286.0, + 287.0, + 288.0, + 289.0, + 290.0, + 291.0, + 292.0, + 293.0, + 294.0, + 295.0, + 296.0, + 297.0, + 298.0, + 299.0, + 300.0, + 301.0, + 302.0, + 303.0, + 304.0, + 305.0, + 306.0, + 307.0, + 308.0, + 309.0, + 310.0, + 311.0, + 312.0, + 313.0, + 314.0, + 315.0, + 316.0, + 317.0, + 318.0, + 319.0, + 320.0, + 321.0, + 322.0, + 323.0, + 324.0, + 325.0, + 326.0, + 327.0, + 328.0, + 329.0, + 330.0, + 331.0, + 332.0, + 333.0, + 334.0, + 335.0, + 336.0, + 337.0, + 338.0, + 339.0, + 340.0, + 341.0, + 342.0, + 343.0, + 344.0, + 345.0, + 346.0, + 347.0, + 348.0, + 349.0, + 350.0, + 351.0, + 352.0, + 353.0, + 354.0, + 355.0, + 356.0, + 357.0, + 358.0, + 359.0, + 360.0, + 361.0, + 362.0, + 363.0, + 364.0, + 365.0, + 366.0, + 367.0, + 368.0, + 369.0, + 370.0, + 371.0, + 372.0, + 373.0, + 374.0, + 375.0, + 376.0, + 377.0, + 378.0, + 379.0, + 380.0, + 381.0, + 382.0, + 383.0, + 384.0, + 385.0, + 386.0, + 387.0, + 388.0, + 389.0, + 390.0, + 391.0, + 392.0, + 393.0, + 394.0, + 395.0, + 396.0, + 397.0, + 398.0, + 399.0, + 400.0, + 401.0, + 402.0, + 403.0, + 404.0, + 405.0, + 406.0, + 407.0, + 408.0, + 409.0, + 410.0, + 411.0, + 412.0, + 413.0, + 414.0, + 415.0, + 416.0, + 417.0, + 418.0, + 419.0, + 420.0, + 421.0, + 422.0, + 423.0, + 424.0, + 425.0, + 426.0, + 427.0, + 428.0, + 429.0, + 430.0, + 431.0, + 432.0, + 433.0, + 434.0, + 435.0, + 436.0, + 437.0, + 438.0, + 439.0, + 440.0, + 441.0, + 442.0, + 443.0, + 444.0, + 445.0, + 446.0, + 447.0, + 448.0, + 449.0, + 450.0, + 451.0, + 452.0, + 453.0, + 454.0, + 455.0, + 456.0, + 457.0, + 458.0, + 459.0, + 460.0, + 461.0, + 462.0, + 463.0, + 464.0, + 465.0, + 466.0, + 467.0, + 468.0, + 469.0, + 470.0, + 471.0, + 472.0, + 473.0, + 474.0, + 475.0, + 476.0, + 477.0, + 478.0, + 479.0, + 480.0, + 481.0, + 482.0, + 483.0, + 484.0, + 485.0, + 486.0, + 487.0, + 488.0, + 489.0, + 490.0, + 491.0, + 492.0, + 493.0, + 494.0, + 495.0, + 496.0, + 497.0, + 498.0, + 499.0, + 500.0 +}; +static const double EVALRATIONAL_COEFFICIENTS_Q = { + 0.0, + 2.0, + 3.0, + 4.0, + 5.0, + 6.0, + 7.0, + 8.0, + 9.0, + 10.0, + 11.0, + 12.0, + 13.0, + 14.0, + 15.0, + 16.0, + 17.0, + 18.0, + 19.0, + 20.0, + 21.0, + 22.0, + 23.0, + 24.0, + 25.0, + 26.0, + 27.0, + 28.0, + 29.0, + 30.0, + 31.0, + 32.0, + 33.0, + 34.0, + 35.0, + 36.0, + 37.0, + 38.0, + 39.0, + 40.0, + 41.0, + 42.0, + 43.0, + 44.0, + 45.0, + 46.0, + 47.0, + 48.0, + 49.0, + 50.0, + 51.0, + 52.0, + 53.0, + 54.0, + 55.0, + 56.0, + 57.0, + 58.0, + 59.0, + 60.0, + 61.0, + 62.0, + 63.0, + 64.0, + 65.0, + 66.0, + 67.0, + 68.0, + 69.0, + 70.0, + 71.0, + 72.0, + 73.0, + 74.0, + 75.0, + 76.0, + 77.0, + 78.0, + 79.0, + 80.0, + 81.0, + 82.0, + 83.0, + 84.0, + 85.0, + 86.0, + 87.0, + 88.0, + 89.0, + 90.0, + 91.0, + 92.0, + 93.0, + 94.0, + 95.0, + 96.0, + 97.0, + 98.0, + 99.0, + 100.0, + 101.0, + 102.0, + 103.0, + 104.0, + 105.0, + 106.0, + 107.0, + 108.0, + 109.0, + 110.0, + 111.0, + 112.0, + 113.0, + 114.0, + 115.0, + 116.0, + 117.0, + 118.0, + 119.0, + 120.0, + 121.0, + 122.0, + 123.0, + 124.0, + 125.0, + 126.0, + 127.0, + 128.0, + 129.0, + 130.0, + 131.0, + 132.0, + 133.0, + 134.0, + 135.0, + 136.0, + 137.0, + 138.0, + 139.0, + 140.0, + 141.0, + 142.0, + 143.0, + 144.0, + 145.0, + 146.0, + 147.0, + 148.0, + 149.0, + 150.0, + 151.0, + 152.0, + 153.0, + 154.0, + 155.0, + 156.0, + 157.0, + 158.0, + 159.0, + 160.0, + 161.0, + 162.0, + 163.0, + 164.0, + 165.0, + 166.0, + 167.0, + 168.0, + 169.0, + 170.0, + 171.0, + 172.0, + 173.0, + 174.0, + 175.0, + 176.0, + 177.0, + 178.0, + 179.0, + 180.0, + 181.0, + 182.0, + 183.0, + 184.0, + 185.0, + 186.0, + 187.0, + 188.0, + 189.0, + 190.0, + 191.0, + 192.0, + 193.0, + 194.0, + 195.0, + 196.0, + 197.0, + 198.0, + 199.0, + 200.0, + 201.0, + 202.0, + 203.0, + 204.0, + 205.0, + 206.0, + 207.0, + 208.0, + 209.0, + 210.0, + 211.0, + 212.0, + 213.0, + 214.0, + 215.0, + 216.0, + 217.0, + 218.0, + 219.0, + 220.0, + 221.0, + 222.0, + 223.0, + 224.0, + 225.0, + 226.0, + 227.0, + 228.0, + 229.0, + 230.0, + 231.0, + 232.0, + 233.0, + 234.0, + 235.0, + 236.0, + 237.0, + 238.0, + 239.0, + 240.0, + 241.0, + 242.0, + 243.0, + 244.0, + 245.0, + 246.0, + 247.0, + 248.0, + 249.0, + 250.0, + 251.0, + 252.0, + 253.0, + 254.0, + 255.0, + 256.0, + 257.0, + 258.0, + 259.0, + 260.0, + 261.0, + 262.0, + 263.0, + 264.0, + 265.0, + 266.0, + 267.0, + 268.0, + 269.0, + 270.0, + 271.0, + 272.0, + 273.0, + 274.0, + 275.0, + 276.0, + 277.0, + 278.0, + 279.0, + 280.0, + 281.0, + 282.0, + 283.0, + 284.0, + 285.0, + 286.0, + 287.0, + 288.0, + 289.0, + 290.0, + 291.0, + 292.0, + 293.0, + 294.0, + 295.0, + 296.0, + 297.0, + 298.0, + 299.0, + 300.0, + 301.0, + 302.0, + 303.0, + 304.0, + 305.0, + 306.0, + 307.0, + 308.0, + 309.0, + 310.0, + 311.0, + 312.0, + 313.0, + 314.0, + 315.0, + 316.0, + 317.0, + 318.0, + 319.0, + 320.0, + 321.0, + 322.0, + 323.0, + 324.0, + 325.0, + 326.0, + 327.0, + 328.0, + 329.0, + 330.0, + 331.0, + 332.0, + 333.0, + 334.0, + 335.0, + 336.0, + 337.0, + 338.0, + 339.0, + 340.0, + 341.0, + 342.0, + 343.0, + 344.0, + 345.0, + 346.0, + 347.0, + 348.0, + 349.0, + 350.0, + 351.0, + 352.0, + 353.0, + 354.0, + 355.0, + 356.0, + 357.0, + 358.0, + 359.0, + 360.0, + 361.0, + 362.0, + 363.0, + 364.0, + 365.0, + 366.0, + 367.0, + 368.0, + 369.0, + 370.0, + 371.0, + 372.0, + 373.0, + 374.0, + 375.0, + 376.0, + 377.0, + 378.0, + 379.0, + 380.0, + 381.0, + 382.0, + 383.0, + 384.0, + 385.0, + 386.0, + 387.0, + 388.0, + 389.0, + 390.0, + 391.0, + 392.0, + 393.0, + 394.0, + 395.0, + 396.0, + 397.0, + 398.0, + 399.0, + 400.0, + 401.0, + 402.0, + 403.0, + 404.0, + 405.0, + 406.0, + 407.0, + 408.0, + 409.0, + 410.0, + 411.0, + 412.0, + 413.0, + 414.0, + 415.0, + 416.0, + 417.0, + 418.0, + 419.0, + 420.0, + 421.0, + 422.0, + 423.0, + 424.0, + 425.0, + 426.0, + 427.0, + 428.0, + 429.0, + 430.0, + 431.0, + 432.0, + 433.0, + 434.0, + 435.0, + 436.0, + 437.0, + 438.0, + 439.0, + 440.0, + 441.0, + 442.0, + 443.0, + 444.0, + 445.0, + 446.0, + 447.0, + 448.0, + 449.0, + 450.0, + 451.0, + 452.0, + 453.0, + 454.0, + 455.0, + 456.0, + 457.0, + 458.0, + 459.0, + 460.0, + 461.0, + 462.0, + 463.0, + 464.0, + 465.0, + 466.0, + 467.0, + 468.0, + 469.0, + 470.0, + 471.0, + 472.0, + 473.0, + 474.0, + 475.0, + 476.0, + 477.0, + 478.0, + 479.0, + 480.0, + 481.0, + 482.0, + 483.0, + 484.0, + 485.0, + 486.0, + 487.0, + 488.0, + 489.0, + 490.0, + 491.0, + 492.0, + 493.0, + 494.0, + 495.0, + 496.0, + 497.0, + 498.0, + 499.0, + 500.0, + 501.0 +}; + +/** +* 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 s1; + double s2; + double ax; + double ix; + int i; + + if ( x == 0.0 ) { + return 1.0 / 0.0; + } + if ( x < 0.0 ) { + ax = -x; + } else { + ax = x; + } + if ( ax <= 1.0 ) { + s1 = EVALRATIONAL_COEFFICIENTS_P[ 501 ]; + s2 = EVALRATIONAL_COEFFICIENTS_Q[ 501 ]; + for ( i = 501-1; i >= 0; i-- ) { + s1 *= x; + s2 *= x; + s1 += EVALRATIONAL_COEFFICIENTS_P[ i ]; + s2 += EVALRATIONAL_COEFFICIENTS_Q[ i ]; + } + } else { + ix = 1.0 / x; // use inverse to avoid overflow + s1 = EVALRATIONAL_COEFFICIENTS_P[ 0 ]; + s2 = EVALRATIONAL_COEFFICIENTS_Q[ 0 ]; + for ( i = 1; i <= 501; i++ ) { + s1 *= ix; + s2 *= ix; + s1 += EVALRATIONAL_COEFFICIENTS_P[ i ]; + s2 += EVALRATIONAL_COEFFICIENTS_Q[ i ]; + } + } + return s1 / s2; +} diff --git a/lib/node_modules/@stdlib/math/base/tools/evalrational-compile-c/test/test.js b/lib/node_modules/@stdlib/math/base/tools/evalrational-compile-c/test/test.js index 9f1d254c59e..97edbf2a33f 100644 --- a/lib/node_modules/@stdlib/math/base/tools/evalrational-compile-c/test/test.js +++ b/lib/node_modules/@stdlib/math/base/tools/evalrational-compile-c/test/test.js @@ -272,6 +272,88 @@ tape( 'if provided coefficient arrays each containing 1 coefficient, the functio t.end(); }); +tape( 'if provided coefficient arrays each containing 1 coefficient, the function returns a function string which always returns the ratio of those two coefficients (positive infinity)', opts, function test( t ) { + var expected; + var actual; + var fpath; + var fopts; + + fpath = join( __dirname, 'fixtures', 'coefficient_ratio_positive_infinity.c.txt' ); + fopts = { + 'encoding': 'utf8' + }; + expected = readFile( fpath, fopts ); + + actual = compile( [ 1.0 ], [ 0.0 ] ); + t.equal( actual, expected, 'returns expected value' ); + + t.end(); +}); + +tape( 'if provided coefficient arrays each containing 1 coefficient, the function returns a function string which always returns the ratio of those two coefficients (positive infinity; custom)', opts, function test( t ) { + var expected; + var options; + var actual; + var fpath; + var fopts; + + fpath = join( __dirname, 'fixtures', 'coefficient_ratio_positive_infinity_custom.c.txt' ); + fopts = { + 'encoding': 'utf8' + }; + expected = readFile( fpath, fopts ); + + options = { + 'dtype': 'float', + 'name': 'rational123' + }; + actual = compile( [ 1.0 ], [ 0.0 ], options ); + t.equal( actual, expected, 'returns expected value' ); + + t.end(); +}); + +tape( 'if provided coefficient arrays each containing 1 coefficient, the function returns a function string which always returns the ratio of those two coefficients (negative infinity)', opts, function test( t ) { + var expected; + var actual; + var fpath; + var fopts; + + fpath = join( __dirname, 'fixtures', 'coefficient_ratio_negative_infinity.c.txt' ); + fopts = { + 'encoding': 'utf8' + }; + expected = readFile( fpath, fopts ); + + actual = compile( [ -1.0 ], [ 0.0 ] ); + t.equal( actual, expected, 'returns expected value' ); + + t.end(); +}); + +tape( 'if provided coefficient arrays each containing 1 coefficient, the function returns a function string which always returns the ratio of those two coefficients (negative infinity; custom)', opts, function test( t ) { + var expected; + var options; + var actual; + var fpath; + var fopts; + + fpath = join( __dirname, 'fixtures', 'coefficient_ratio_negative_infinity_custom.c.txt' ); + fopts = { + 'encoding': 'utf8' + }; + expected = readFile( fpath, fopts ); + + options = { + 'dtype': 'float', + 'name': 'rational123' + }; + actual = compile( [ -1.0 ], [ 0.0 ], options ); + t.equal( actual, expected, 'returns expected value' ); + + t.end(); +}); + tape( 'the function returns a function string which evaluates a rational function', opts, function test( t ) { var expected; var actual; @@ -330,6 +412,24 @@ tape( 'the function returns a function string which evaluates a rational functio t.end(); }); +tape( 'the function returns a function string which evaluates a rational function', opts, function test( t ) { + var expected; + var actual; + var fpath; + var fopts; + + fpath = join( __dirname, 'fixtures', 'evalrational4.c.txt' ); + fopts = { + 'encoding': 'utf8' + }; + expected = readFile( fpath, fopts ); + + actual = compile( [ -1.0, 2.5, 3.14, -1.0 ], [ 0.0, -3.5, 2.2, 1.25 ] ); + t.equal( actual, expected, 'returns expected value' ); + + t.end(); +}); + tape( 'the function returns a function string which evaluates a rational function (custom)', opts, function test( t ) { var expected; var options; @@ -440,30 +540,31 @@ tape( 'the function returns a function string which evaluates a rational functio t.end(); }); -tape( 'the function returns a function string which handles positive infinity correctly', opts, function test( t ) { +tape( 'the function returns a function string which evaluates a rational function (large number of coefficients; integers and infinity)', opts, function test( t ) { var expected; - var str; + var actual; + var fpath; + var fopts; var P; var Q; + var i; - expected = '1.0 / 0.0'; - P = [ 1.0, 2.0 ]; - Q = [ 0.0, 1.0 ]; - str = compile( P, Q ); - t.ok( str.includes( expected ), 'function string handles positive infinity correctly' ); - t.end(); -}); + fpath = join( __dirname, 'fixtures', 'loop3.c.txt' ); + fopts = { + 'encoding': 'utf8' + }; + expected = readFile( fpath, fopts ); -tape( 'the function returns a function string which handles negative infinity correctly', opts, function test( t ) { - var expected; - var str; - var P; - var Q; + P = new Float64Array( 501 ); + Q = new Float64Array( 501 ); + for ( i = 0; i < P.length; i++ ) { + P[ i ] = i; + Q[ i ] = i + 1; + } + P[ 0 ] = 1.0; + Q[ 0 ] = 0.0; + actual = compile( P, Q ); + t.equal( actual, expected, 'returns expected value' ); - expected = '-1.0 / 0.0'; - P = [ -1.0, -2.0 ]; - Q = [ 0.0, 1.0 ]; - str = compile( P, Q ); - t.ok( str.includes( expected ), 'function string handles negative infinity correctly' ); t.end(); });