Skip to content

Commit a3d658e

Browse files
committed
Add Typescript definition
1 parent db5b868 commit a3d658e

2 files changed

Lines changed: 166 additions & 0 deletions

File tree

  • lib/node_modules/@stdlib/math/base/tools/continued-fraction/docs/types
Lines changed: 76 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,76 @@
1+
/*
2+
* @license Apache-2.0
3+
*
4+
* Copyright (c) 2021 The Stdlib Authors.
5+
*
6+
* Licensed under the Apache License, Version 2.0 (the "License");
7+
* you may not use this file except in compliance with the License.
8+
* You may obtain a copy of the License at
9+
*
10+
* http://www.apache.org/licenses/LICENSE-2.0
11+
*
12+
* Unless required by applicable law or agreed to in writing, software
13+
* distributed under the License is distributed on an "AS IS" BASIS,
14+
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
15+
* See the License for the specific language governing permissions and
16+
* limitations under the License.
17+
*/
18+
19+
// TypeScript Version: 2.0
20+
21+
/// <reference types="@stdlib/types"/>
22+
23+
/**
24+
* Interface defining function options.
25+
*/
26+
interface Options {
27+
/**
28+
* Maximum number of iterations (default: 1000).
29+
*/
30+
maxIter?: number;
31+
32+
/**
33+
* Further terms are only added as long as the next term is greater than current term times the tolerance (default: 2.22e-16).
34+
*/
35+
tolerance?: number;
36+
37+
/**
38+
* Boolean indicating whether to keep the leading b term (default: false).
39+
*/
40+
keep?: boolean;
41+
}
42+
43+
/**
44+
* Evaluates the continued fraction approximation for the supplied series generator using the modified Lentz algorithm.
45+
*
46+
* ## References
47+
*
48+
* - Lentz, William J. 1976. "Generating bessel functions in Mie scattering calculations using continued fractions." _Applied Optics_ 15 (3): 668–71. doi:[10.1364/AO.15.000668](https://doi.org/10.1364/AO.15.000668).
49+
*
50+
* @param generator - function returning terms of continued fraction expansion
51+
* @param options - function options
52+
* @param options.maxIter - maximum number of iterations (default: 1000)
53+
* @param options.tolerance - further terms are only added as long as the next term is greater than current term times the tolerance (default: 2.22e-16)
54+
* @param options.keep - whether to keep the leading b term (default: false)
55+
* @returns value of continued fraction
56+
*
57+
* @example
58+
* // Continued fraction for (e-1)^(-1):
59+
* var gen = generator();
60+
* var out = continuedFraction( gen );
61+
* // returns ~0.582
62+
*
63+
* function* generator() {
64+
* var i = 0;
65+
* while ( true ) {
66+
* i++;
67+
* yield [ i, i ];
68+
* }
69+
* }
70+
*/
71+
declare function continuedFraction( generator: Function, options?: Options ): number; // tslint:disable-line:max-line-length
72+
73+
74+
// EXPORTS //
75+
76+
export = continuedFraction;
Lines changed: 90 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,90 @@
1+
/*
2+
* @license Apache-2.0
3+
*
4+
* Copyright (c) 2021 The Stdlib Authors.
5+
*
6+
* Licensed under the Apache License, Version 2.0 (the "License");
7+
* you may not use this file except in compliance with the License.
8+
* You may obtain a copy of the License at
9+
*
10+
* http://www.apache.org/licenses/LICENSE-2.0
11+
*
12+
* Unless required by applicable law or agreed to in writing, software
13+
* distributed under the License is distributed on an "AS IS" BASIS,
14+
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
15+
* See the License for the specific language governing permissions and
16+
* limitations under the License.
17+
*/
18+
19+
import continuedFraction = require( './index' );
20+
21+
/**
22+
* Continued fraction for (e-1)^(-1).
23+
*/
24+
function* generator() {
25+
let i = 0;
26+
while ( true ) {
27+
i += 1;
28+
yield [ i, i ];
29+
}
30+
}
31+
32+
// TESTS //
33+
34+
// The function returns a number...
35+
{
36+
continuedFraction( generator ); // $ExpectType number
37+
const opts = {
38+
'keep': true,
39+
'maxIter': 500,
40+
'tolerance': 1e-6
41+
};
42+
continuedFraction( generator, opts ); // $ExpectType number
43+
}
44+
45+
// The compiler throws an error if the function is provided a first argument which is not a function...
46+
{
47+
continuedFraction( 'abc' ); // $ExpectError
48+
continuedFraction( 123 ); // $ExpectError
49+
continuedFraction( false ); // $ExpectError
50+
continuedFraction( true ); // $ExpectError
51+
continuedFraction( [] ); // $ExpectError
52+
continuedFraction( {} ); // $ExpectError
53+
}
54+
55+
// The compiler throws an error if the function is provided a `keep` option which is not a boolean...
56+
{
57+
continuedFraction( generator, { 'keep': 'abc' } ); // $ExpectError
58+
continuedFraction( generator, { 'keep': 123 } ); // $ExpectError
59+
continuedFraction( generator, { 'keep': null } ); // $ExpectError
60+
continuedFraction( generator, { 'keep': [] } ); // $ExpectError
61+
continuedFraction( generator, { 'keep': {} } ); // $ExpectError
62+
continuedFraction( generator, { 'keep': ( x: number ): number => x } ); // $ExpectError
63+
}
64+
65+
// The compiler throws an error if the function is provided a `maxIter` option which is not a number...
66+
{
67+
continuedFraction( generator, { 'maxIter': 'abc' } ); // $ExpectError
68+
continuedFraction( generator, { 'maxIter': true } ); // $ExpectError
69+
continuedFraction( generator, { 'maxIter': false } ); // $ExpectError
70+
continuedFraction( generator, { 'maxIter': null } ); // $ExpectError
71+
continuedFraction( generator, { 'maxIter': [] } ); // $ExpectError
72+
continuedFraction( generator, { 'maxIter': {} } ); // $ExpectError
73+
continuedFraction( generator, { 'maxIter': ( x: number ): number => x } ); // $ExpectError
74+
}
75+
76+
// The compiler throws an error if the function is provided a `tolerance` option which is not a number...
77+
{
78+
continuedFraction( generator, { 'tolerance': 'abc' } ); // $ExpectError
79+
continuedFraction( generator, { 'tolerance': true } ); // $ExpectError
80+
continuedFraction( generator, { 'tolerance': false } ); // $ExpectError
81+
continuedFraction( generator, { 'tolerance': null } ); // $ExpectError
82+
continuedFraction( generator, { 'tolerance': [] } ); // $ExpectError
83+
continuedFraction( generator, { 'tolerance': {} } ); // $ExpectError
84+
continuedFraction( generator, { 'tolerance': ( x: number ): number => x } ); // $ExpectError
85+
}
86+
87+
// The compiler throws an error if the function is provided insufficient arguments...
88+
{
89+
continuedFraction(); // $ExpectError
90+
}

0 commit comments

Comments
 (0)