Skip to content

Commit

Permalink
Auto-generated commit
Browse files Browse the repository at this point in the history
  • Loading branch information
stdlib-bot committed Mar 24, 2024
1 parent a813946 commit 6aadece
Show file tree
Hide file tree
Showing 18 changed files with 3,144 additions and 87 deletions.
6 changes: 5 additions & 1 deletion base/tools/evalpoly-compile-c/docs/types/index.d.ts
Original file line number Diff line number Diff line change
Expand Up @@ -18,6 +18,10 @@

// TypeScript Version: 4.1

/// <reference types="@stdlib/types"/>

import { Collection } from '@stdlib/types/array';

/**
* Interface describing function options.
*/
Expand All @@ -44,7 +48,7 @@ interface Options {
* var str = compile( [ 3.0, 2.0, 1.0 ] );
* // returns <string>
*/
declare function compile( c: Array<number>, options?: Options ): string;
declare function compile( c: Collection<number>, options?: Options ): string;


// EXPORTS //
Expand Down
6 changes: 5 additions & 1 deletion base/tools/evalpoly-compile/docs/types/index.d.ts
Original file line number Diff line number Diff line change
Expand Up @@ -18,6 +18,10 @@

// TypeScript Version: 4.1

/// <reference types="@stdlib/types"/>

import { Collection } from '@stdlib/types/array';

/**
* Interface describing function options.
*/
Expand All @@ -39,7 +43,7 @@ interface Options {
* var str = compile( [ 3.0, 2.0, 1.0 ] );
* // returns <string>
*/
declare function compile( c: Array<number>, options?: Options ): string;
declare function compile( c: Collection<number>, options?: Options ): string;


// EXPORTS //
Expand Down
8 changes: 6 additions & 2 deletions base/tools/evalrational-compile-c/docs/types/index.d.ts
Original file line number Diff line number Diff line change
Expand Up @@ -18,14 +18,18 @@

// TypeScript Version: 4.1

/// <reference types="@stdlib/types"/>

import { Collection } from '@stdlib/types/array';

/**
* Interface describing function options.
*/
interface Options {
/**
* Input value floating-point data type (e.g., `double` or `float`). Default: `'double'`.
*/
dtype?: string;
dtype?: 'double' | 'float';

/**
* Function name. Default: `'evalpoly'`.
Expand All @@ -48,7 +52,7 @@ interface Options {
* var str = compile( P, Q );
* // returns <string>
*/
declare function compile( P: Array<number>, Q: Array<number>, options?: Options ): string;
declare function compile( P: Collection<number>, Q: Collection<number>, options?: Options ): string;


// EXPORTS //
Expand Down
114 changes: 85 additions & 29 deletions base/tools/evalrational-compile/README.md
Original file line number Diff line number Diff line change
Expand Up @@ -36,9 +36,9 @@ limitations under the License.
var compile = require( '@stdlib/math/base/tools/evalrational-compile' );
```

#### compile( P, Q )
#### compile( P, Q\[, options] )

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

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

In the example above, the output `string` would correspond to the following module:
The function supports the following `options`:

- **dtype**: input argument floating-point data type (e.g., `float64` or `float32`). Default: `'float64'`.

In the example above, the output string would correspond to the following module:

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

Expand Down Expand Up @@ -84,12 +88,12 @@ function evalrational( x ) {
ax = x;
}
if ( ax <= 1.0 ) {
s1 = 3.0 + (x * (2.0 + (x * 1.0))); // eslint-disable-line max-len
s2 = -1.0 + (x * (-2.0 + (x * -3.0))); // eslint-disable-line max-len
s1 = 3.0 + (x * (2.0 + (x * 1.0)));
s2 = -1.0 + (x * (-2.0 + (x * -3.0)));
} else {
x = 1.0 / x;
s1 = 1.0 + (x * (2.0 + (x * 3.0))); // eslint-disable-line max-len
s2 = -3.0 + (x * (-2.0 + (x * -1.0))); // eslint-disable-line max-len
s1 = 1.0 + (x * (2.0 + (x * 3.0)));
s2 = -3.0 + (x * (-2.0 + (x * -1.0)));
}
return s1 / s2;
}
Expand All @@ -102,6 +106,75 @@ module.exports = evalrational;

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

By default, the function assumes double-precision floating-point arithmetic. To emulate single-precision floating-point arithmetic, set the `dtype` option to `'float32'`.

```javascript
var P = [ 3.0, 2.0, 1.0 ];
var Q = [ -1.0, -2.0, -3.0 ];

var str = compile( P, Q, {
'dtype': 'float32'
});
// returns <string>
```

In the previous example, the output string would correspond to the following module:

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

```javascript
'use strict';

// MODULES //

var float64ToFloat32 = require( '@stdlib/number/float64/base/to-float32' );


// MAIN //

/**
* 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
*
* @private
* @param {number} x - value at which to evaluate the rational function
* @returns {number} evaluated rational function
*/
function evalrational( x ) {
var ax;
var s1;
var s2;
if ( x === 0.0 ) {
return -3.0;
}
if ( x < 0.0 ) {
ax = -x;
} else {
ax = x;
}
if ( ax <= 1.0 ) {
s1 = float64ToFloat32(3.0 + float64ToFloat32(x * float64ToFloat32(2.0 + float64ToFloat32(x * 1.0)))); // eslint-disable-line max-len
s2 = float64ToFloat32(-1.0 + float64ToFloat32(x * float64ToFloat32(-2.0 + float64ToFloat32(x * -3.0)))); // eslint-disable-line max-len
} else {
x = float64ToFloat32( 1.0 / x );
s1 = float64ToFloat32(1.0 + float64ToFloat32(x * float64ToFloat32(2.0 + float64ToFloat32(x * 3.0)))); // eslint-disable-line max-len
s2 = float64ToFloat32(-3.0 + float64ToFloat32(x * float64ToFloat32(-2.0 + float64ToFloat32(x * -1.0)))); // eslint-disable-line max-len
}
return float64ToFloat32( s1 / s2 );
}


// EXPORTS //

module.exports = evalrational;
```

</section>

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

```javascript
var randu = require( '@stdlib/random/base/randu' );
var round = require( '@stdlib/math/base/special/round' );
var Float64Array = require( '@stdlib/array/float64' );
var discreteUniform = require( '@stdlib/random/array/discrete-uniform' );
var compile = require( '@stdlib/math/base/tools/evalrational-compile' );

var sign;
var str;
var P;
var Q;
var i;

// Create two arrays of random coefficients...
P = new Float64Array( 10 );
Q = new Float64Array( 10 );
for ( i = 0; i < P.length; i++ ) {
if ( randu() < 0.5 ) {
sign = -1.0;
} else {
sign = 1.0;
}
P[ i ] = sign * round( randu()*100.0 );
Q[ i ] = sign * round( randu()*100.0 );
}
// Create arrays of random coefficients:
var P = discreteUniform( 10, -100, 100 );
var Q = discreteUniform( 10, -100, 100 );

// Compile a module for evaluating a rational function:
str = compile( P, Q );
var str = compile( P, Q );
console.log( str );
```

Expand Down
17 changes: 16 additions & 1 deletion base/tools/evalrational-compile/docs/types/index.d.ts
Original file line number Diff line number Diff line change
Expand Up @@ -18,11 +18,26 @@

// TypeScript Version: 4.1

/// <reference types="@stdlib/types"/>

import { Collection } from '@stdlib/types/array';

/**
* Interface describing function options.
*/
interface Options {
/**
* Input value floating-point data type (e.g., `float64` or `float32`). Default: `'float64'`.
*/
dtype?: 'float64' | 'float32';
}

/**
* Compiles a module string which exports a function for evaluating a rational function.
*
* @param P - numerator polynomial coefficients sorted in ascending degree
* @param Q - denominator polynomial coefficients sorted in ascending degree
* @param options - function options
* @returns module string exporting a function for evaluating a rational function
*
* @example
Expand All @@ -32,7 +47,7 @@
* var str = compile( P, Q );
* // returns <string>
*/
declare function compile( P: Array<number>, Q: Array<number> ): string;
declare function compile( P: Collection<number>, Q: Collection<number>, options?: Options ): string;


// EXPORTS //
Expand Down
42 changes: 42 additions & 0 deletions base/tools/evalrational-compile/docs/types/test.ts
Original file line number Diff line number Diff line change
Expand Up @@ -24,33 +24,75 @@ import compile = require( './index' );
// The function returns a string...
{
compile( [ -6.0, -5.0 ], [ 3.0, 0.5 ] ); // $ExpectType string
compile( [ 3.0, 2.0, 1.0 ], { 'dtype': 'float32' } ); // $ExpectType string
}

// The compiler throws an error if the function is provided a first argument which is not an array of numbers...
{
const Q = [ 3.0, 0.5 ];

compile( true, Q ); // $ExpectError
compile( false, Q ); // $ExpectError
compile( 'abc', Q ); // $ExpectError
compile( 123, Q ); // $ExpectError
compile( {}, Q ); // $ExpectError
compile( ( x: number ): number => x, Q ); // $ExpectError

compile( true, Q, {} ); // $ExpectError
compile( false, Q, {} ); // $ExpectError
compile( 'abc', Q, {} ); // $ExpectError
compile( 123, Q, {} ); // $ExpectError
compile( {}, Q, {} ); // $ExpectError
compile( ( x: number ): number => x, Q, {} ); // $ExpectError
}

// The compiler throws an error if the function is provided a second argument which is not an array of numbers...
{
const P = [ -6.0, -5.0 ];

compile( P, true ); // $ExpectError
compile( P, false ); // $ExpectError
compile( P, 'abc' ); // $ExpectError
compile( P, 123 ); // $ExpectError
compile( P, {} ); // $ExpectError
compile( P, ( x: number ): number => x ); // $ExpectError

compile( P, true, {} ); // $ExpectError
compile( P, false, {} ); // $ExpectError
compile( P, 'abc', {} ); // $ExpectError
compile( P, 123, {} ); // $ExpectError
compile( P, {}, {} ); // $ExpectError
compile( P, ( x: number ): number => x, {} ); // $ExpectError
}

// The compiler throws an error if the function is provided a third argument which is not an object...
{
const P = [ -6.0, -5.0 ];
const Q = [ 3.0, 0.5 ];

compile( P, Q, true ); // $ExpectError
compile( P, Q, false ); // $ExpectError
compile( P, Q, 'abc' ); // $ExpectError
compile( P, Q, 123 ); // $ExpectError
compile( P, Q, ( x: number ): number => x ); // $ExpectError
}

// The compiler throws an error if the function is provided an invalid `dtype` option...
{
const P = [ -6.0, -5.0 ];
const Q = [ 3.0, 0.5 ];

compile( P, Q, { 'dtype': true } ); // $ExpectError
compile( P, Q, { 'dtype': false } ); // $ExpectError
compile( P, Q, { 'dtype': [] } ); // $ExpectError
compile( P, Q, { 'dtype': 123 } ); // $ExpectError
compile( P, Q, { 'dtype': ( x: number ): number => x } ); // $ExpectError
}

// The compiler throws an error if the function is provided an insufficient number of arguments...
{
const P = [ -6.0, -5.0 ];

compile(); // $ExpectError
compile( P ); // $ExpectError
}
27 changes: 5 additions & 22 deletions base/tools/evalrational-compile/examples/index.js
Original file line number Diff line number Diff line change
Expand Up @@ -19,9 +19,7 @@
'use strict';

var resolve = require( 'path' ).resolve;
var randu = require( '@stdlib/random/base/randu' );
var round = require( './../../../../base/special/round' );
var Float64Array = require( '@stdlib/array/float64' );
var discreteUniform = require( '@stdlib/random/array/discrete-uniform' );
var tryRequire = require( '@stdlib/utils/try-require' );

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

function main() {
var sign;
var str;
var P;
var Q;
var i;

// Create two arrays of random coefficients...
P = new Float64Array( 10 );
Q = new Float64Array( 10 );
for ( i = 0; i < P.length; i++ ) {
if ( randu() < 0.5 ) {
sign = -1.0;
} else {
sign = 1.0;
}
P[ i ] = sign * round( randu()*100.0 );
Q[ i ] = sign * round( randu()*100.0 );
}
// Create arrays of random coefficients:
var P = discreteUniform( 10, -100, 100 );
var Q = discreteUniform( 10, -100, 100 );

// Compile a module for evaluating a rational function:
str = compile( P, Q );
var str = compile( P, Q );
console.log( str );
}
Loading

0 comments on commit 6aadece

Please sign in to comment.