Skip to content

Commit c6c2a16

Browse files
committed
feat: add support for single-precision floating-point arithmetic emulation
1 parent 1cfdb61 commit c6c2a16

File tree

15 files changed

+3128
-83
lines changed

15 files changed

+3128
-83
lines changed

lib/node_modules/@stdlib/math/base/tools/evalrational-compile/README.md

Lines changed: 85 additions & 29 deletions
Original file line numberDiff line numberDiff line change
@@ -36,9 +36,9 @@ limitations under the License.
3636
var compile = require( '@stdlib/math/base/tools/evalrational-compile' );
3737
```
3838

39-
#### compile( P, Q )
39+
#### compile( P, Q\[, options] )
4040

41-
Compiles a module `string` containing an exported function which evaluates a [rational function][@stdlib/math/base/tools/evalrational] having coefficients `P` and `Q`.
41+
Compiles a module string containing an exported function which evaluates a [rational function][@stdlib/math/base/tools/evalrational] having coefficients `P` and `Q`.
4242

4343
```javascript
4444
var P = [ 3.0, 2.0, 1.0 ];
@@ -48,7 +48,11 @@ var str = compile( P, Q );
4848
// returns <string>
4949
```
5050

51-
In the example above, the output `string` would correspond to the following module:
51+
The function supports the following `options`:
52+
53+
- **dtype**: input argument floating-point data type (e.g., `float64` or `float32`). Default: `'float64'`.
54+
55+
In the example above, the output string would correspond to the following module:
5256

5357
<!-- eslint-disable no-unused-expressions -->
5458

@@ -84,12 +88,12 @@ function evalrational( x ) {
8488
ax = x;
8589
}
8690
if ( ax <= 1.0 ) {
87-
s1 = 3.0 + (x * (2.0 + (x * 1.0))); // eslint-disable-line max-len
88-
s2 = -1.0 + (x * (-2.0 + (x * -3.0))); // eslint-disable-line max-len
91+
s1 = 3.0 + (x * (2.0 + (x * 1.0)));
92+
s2 = -1.0 + (x * (-2.0 + (x * -3.0)));
8993
} else {
9094
x = 1.0 / x;
91-
s1 = 1.0 + (x * (2.0 + (x * 3.0))); // eslint-disable-line max-len
92-
s2 = -3.0 + (x * (-2.0 + (x * -1.0))); // eslint-disable-line max-len
95+
s1 = 1.0 + (x * (2.0 + (x * 3.0)));
96+
s2 = -3.0 + (x * (-2.0 + (x * -1.0)));
9397
}
9498
return s1 / s2;
9599
}
@@ -102,6 +106,75 @@ module.exports = evalrational;
102106

103107
The coefficients should be ordered in **ascending** degree, thus matching summation notation.
104108

109+
By default, the function assumes double-precision floating-point arithmetic. To emulate single-precision floating-point arithmetic, set the `dtype` option to `'float32'`.
110+
111+
```javascript
112+
var P = [ 3.0, 2.0, 1.0 ];
113+
var Q = [ -1.0, -2.0, -3.0 ];
114+
115+
var str = compile( P, Q, {
116+
'dtype': 'float32'
117+
});
118+
// returns <string>
119+
```
120+
121+
In the previous example, the output string would correspond to the following module:
122+
123+
<!-- eslint-disable no-unused-expressions -->
124+
125+
```javascript
126+
'use strict';
127+
128+
// MODULES //
129+
130+
var float64ToFloat32 = require( '@stdlib/number/float64/base/to-float32' );
131+
132+
133+
// MAIN //
134+
135+
/**
136+
* Evaluates a rational function (i.e., the ratio of two polynomials described by the coefficients stored in \\(P\\) and \\(Q\\)).
137+
*
138+
* ## Notes
139+
*
140+
* - Coefficients should be sorted in ascending degree.
141+
* - The implementation uses [Horner's rule][horners-method] for efficient computation.
142+
*
143+
* [horners-method]: https://en.wikipedia.org/wiki/Horner%27s_method
144+
*
145+
* @private
146+
* @param {number} x - value at which to evaluate the rational function
147+
* @returns {number} evaluated rational function
148+
*/
149+
function evalrational( x ) {
150+
var ax;
151+
var s1;
152+
var s2;
153+
if ( x === 0.0 ) {
154+
return -3.0;
155+
}
156+
if ( x < 0.0 ) {
157+
ax = -x;
158+
} else {
159+
ax = x;
160+
}
161+
if ( ax <= 1.0 ) {
162+
s1 = float64ToFloat32(3.0 + float64ToFloat32(x * float64ToFloat32(2.0 + float64ToFloat32(x * 1.0)))); // eslint-disable-line max-len
163+
s2 = float64ToFloat32(-1.0 + float64ToFloat32(x * float64ToFloat32(-2.0 + float64ToFloat32(x * -3.0)))); // eslint-disable-line max-len
164+
} else {
165+
x = float64ToFloat32( 1.0 / x );
166+
s1 = float64ToFloat32(1.0 + float64ToFloat32(x * float64ToFloat32(2.0 + float64ToFloat32(x * 3.0)))); // eslint-disable-line max-len
167+
s2 = float64ToFloat32(-3.0 + float64ToFloat32(x * float64ToFloat32(-2.0 + float64ToFloat32(x * -1.0)))); // eslint-disable-line max-len
168+
}
169+
return float64ToFloat32( s1 / s2 );
170+
}
171+
172+
173+
// EXPORTS //
174+
175+
module.exports = evalrational;
176+
```
177+
105178
</section>
106179

107180
<!-- /.usage -->
@@ -123,32 +196,15 @@ The coefficients should be ordered in **ascending** degree, thus matching summat
123196
<!-- eslint no-undef: "error" -->
124197

125198
```javascript
126-
var randu = require( '@stdlib/random/base/randu' );
127-
var round = require( '@stdlib/math/base/special/round' );
128-
var Float64Array = require( '@stdlib/array/float64' );
199+
var discreteUniform = require( '@stdlib/random/array/discrete-uniform' );
129200
var compile = require( '@stdlib/math/base/tools/evalrational-compile' );
130201

131-
var sign;
132-
var str;
133-
var P;
134-
var Q;
135-
var i;
136-
137-
// Create two arrays of random coefficients...
138-
P = new Float64Array( 10 );
139-
Q = new Float64Array( 10 );
140-
for ( i = 0; i < P.length; i++ ) {
141-
if ( randu() < 0.5 ) {
142-
sign = -1.0;
143-
} else {
144-
sign = 1.0;
145-
}
146-
P[ i ] = sign * round( randu()*100.0 );
147-
Q[ i ] = sign * round( randu()*100.0 );
148-
}
202+
// Create arrays of random coefficients:
203+
var P = discreteUniform( 10, -100, 100 );
204+
var Q = discreteUniform( 10, -100, 100 );
149205

150206
// Compile a module for evaluating a rational function:
151-
str = compile( P, Q );
207+
var str = compile( P, Q );
152208
console.log( str );
153209
```
154210

lib/node_modules/@stdlib/math/base/tools/evalrational-compile/docs/types/index.d.ts

Lines changed: 16 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -18,11 +18,26 @@
1818

1919
// TypeScript Version: 4.1
2020

21+
/// <reference types="@stdlib/types"/>
22+
23+
import { Collection } from '@stdlib/types/array';
24+
25+
/**
26+
* Interface describing function options.
27+
*/
28+
interface Options {
29+
/**
30+
* Input value floating-point data type (e.g., `float64` or `float32`). Default: `'float64'`.
31+
*/
32+
dtype?: 'float64' | 'float32';
33+
}
34+
2135
/**
2236
* Compiles a module string which exports a function for evaluating a rational function.
2337
*
2438
* @param P - numerator polynomial coefficients sorted in ascending degree
2539
* @param Q - denominator polynomial coefficients sorted in ascending degree
40+
* @param options - function options
2641
* @returns module string exporting a function for evaluating a rational function
2742
*
2843
* @example
@@ -32,7 +47,7 @@
3247
* var str = compile( P, Q );
3348
* // returns <string>
3449
*/
35-
declare function compile( P: Array<number>, Q: Array<number> ): string;
50+
declare function compile( P: Collection<number>, Q: Collection<number>, options?: Options ): string;
3651

3752

3853
// EXPORTS //

lib/node_modules/@stdlib/math/base/tools/evalrational-compile/docs/types/test.ts

Lines changed: 42 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -24,33 +24,75 @@ import compile = require( './index' );
2424
// The function returns a string...
2525
{
2626
compile( [ -6.0, -5.0 ], [ 3.0, 0.5 ] ); // $ExpectType string
27+
compile( [ 3.0, 2.0, 1.0 ], { 'dtype': 'float32' } ); // $ExpectType string
2728
}
2829

2930
// The compiler throws an error if the function is provided a first argument which is not an array of numbers...
3031
{
3132
const Q = [ 3.0, 0.5 ];
33+
3234
compile( true, Q ); // $ExpectError
3335
compile( false, Q ); // $ExpectError
3436
compile( 'abc', Q ); // $ExpectError
3537
compile( 123, Q ); // $ExpectError
3638
compile( {}, Q ); // $ExpectError
3739
compile( ( x: number ): number => x, Q ); // $ExpectError
40+
41+
compile( true, Q, {} ); // $ExpectError
42+
compile( false, Q, {} ); // $ExpectError
43+
compile( 'abc', Q, {} ); // $ExpectError
44+
compile( 123, Q, {} ); // $ExpectError
45+
compile( {}, Q, {} ); // $ExpectError
46+
compile( ( x: number ): number => x, Q, {} ); // $ExpectError
3847
}
3948

4049
// The compiler throws an error if the function is provided a second argument which is not an array of numbers...
4150
{
4251
const P = [ -6.0, -5.0 ];
52+
4353
compile( P, true ); // $ExpectError
4454
compile( P, false ); // $ExpectError
4555
compile( P, 'abc' ); // $ExpectError
4656
compile( P, 123 ); // $ExpectError
4757
compile( P, {} ); // $ExpectError
4858
compile( P, ( x: number ): number => x ); // $ExpectError
59+
60+
compile( P, true, {} ); // $ExpectError
61+
compile( P, false, {} ); // $ExpectError
62+
compile( P, 'abc', {} ); // $ExpectError
63+
compile( P, 123, {} ); // $ExpectError
64+
compile( P, {}, {} ); // $ExpectError
65+
compile( P, ( x: number ): number => x, {} ); // $ExpectError
66+
}
67+
68+
// The compiler throws an error if the function is provided a third argument which is not an object...
69+
{
70+
const P = [ -6.0, -5.0 ];
71+
const Q = [ 3.0, 0.5 ];
72+
73+
compile( P, Q, true ); // $ExpectError
74+
compile( P, Q, false ); // $ExpectError
75+
compile( P, Q, 'abc' ); // $ExpectError
76+
compile( P, Q, 123 ); // $ExpectError
77+
compile( P, Q, ( x: number ): number => x ); // $ExpectError
78+
}
79+
80+
// The compiler throws an error if the function is provided an invalid `dtype` option...
81+
{
82+
const P = [ -6.0, -5.0 ];
83+
const Q = [ 3.0, 0.5 ];
84+
85+
compile( P, Q, { 'dtype': true } ); // $ExpectError
86+
compile( P, Q, { 'dtype': false } ); // $ExpectError
87+
compile( P, Q, { 'dtype': [] } ); // $ExpectError
88+
compile( P, Q, { 'dtype': 123 } ); // $ExpectError
89+
compile( P, Q, { 'dtype': ( x: number ): number => x } ); // $ExpectError
4990
}
5091

5192
// The compiler throws an error if the function is provided an insufficient number of arguments...
5293
{
5394
const P = [ -6.0, -5.0 ];
95+
5496
compile(); // $ExpectError
5597
compile( P ); // $ExpectError
5698
}

lib/node_modules/@stdlib/math/base/tools/evalrational-compile/examples/index.js

Lines changed: 5 additions & 22 deletions
Original file line numberDiff line numberDiff line change
@@ -19,9 +19,7 @@
1919
'use strict';
2020

2121
var resolve = require( 'path' ).resolve;
22-
var randu = require( '@stdlib/random/base/randu' );
23-
var round = require( '@stdlib/math/base/special/round' );
24-
var Float64Array = require( '@stdlib/array/float64' );
22+
var discreteUniform = require( '@stdlib/random/array/discrete-uniform' );
2523
var tryRequire = require( '@stdlib/utils/try-require' );
2624

2725
var compile = tryRequire( resolve( __dirname, '..', 'lib' ) );
@@ -32,26 +30,11 @@ if ( compile instanceof Error ) {
3230
}
3331

3432
function main() {
35-
var sign;
36-
var str;
37-
var P;
38-
var Q;
39-
var i;
40-
41-
// Create two arrays of random coefficients...
42-
P = new Float64Array( 10 );
43-
Q = new Float64Array( 10 );
44-
for ( i = 0; i < P.length; i++ ) {
45-
if ( randu() < 0.5 ) {
46-
sign = -1.0;
47-
} else {
48-
sign = 1.0;
49-
}
50-
P[ i ] = sign * round( randu()*100.0 );
51-
Q[ i ] = sign * round( randu()*100.0 );
52-
}
33+
// Create arrays of random coefficients:
34+
var P = discreteUniform( 10, -100, 100 );
35+
var Q = discreteUniform( 10, -100, 100 );
5336

5437
// Compile a module for evaluating a rational function:
55-
str = compile( P, Q );
38+
var str = compile( P, Q );
5639
console.log( str );
5740
}

0 commit comments

Comments
 (0)