Skip to content

Commit 2cd9388

Browse files
committed
Add Typescript definition
1 parent a3d658e commit 2cd9388

2 files changed

Lines changed: 165 additions & 0 deletions

File tree

Lines changed: 72 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,72 @@
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 terms to be added (default: 1000000).
29+
*/
30+
maxTerms?: 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+
* Initial value of the resulting sum (default: 0).
39+
*/
40+
initialValue?: number;
41+
}
42+
43+
/**
44+
* Sum the elements of the series given by the supplied function.
45+
*
46+
* @param generator - series function
47+
* @param options - function options
48+
* @param options.maxTerms - maximum number of terms to be added (default: 1000000)
49+
* @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)
50+
* @param options.initialValue - initial value of the resulting sum (default: 0)
51+
* @returns sum of all series terms
52+
*
53+
* @example
54+
* var pow = require( `@stdlib/math/base/special/pow` );
55+
* var gen = geometricSeriesGenerator( 0.9 );
56+
* var out = sumSeries( gen );
57+
* // returns 10.0
58+
*
59+
* function* geometricSeriesGenerator( x ) {
60+
* var exponent = 0;
61+
* while ( true ) {
62+
* yield pow( x, exponent );
63+
* exponent += 1;
64+
* }
65+
* }
66+
*/
67+
declare function sumSeries( generator: Function, options?: Options ): number;
68+
69+
70+
// EXPORTS //
71+
72+
export = sumSeries;
Lines changed: 93 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,93 @@
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 pow = require( '@stdlib/math/base/special/pow' );
20+
import sumSeries = require( './index' );
21+
22+
/**
23+
* Geometric series.
24+
*/
25+
function* generator( x: number ) {
26+
let exponent = 0;
27+
while ( true ) {
28+
yield pow( x, exponent );
29+
exponent += 1;
30+
}
31+
}
32+
33+
34+
// TESTS //
35+
36+
// The function returns a number...
37+
{
38+
sumSeries( generator ); // $ExpectType number
39+
const opts = {
40+
'initialValue': 1,
41+
'maxTerms': 500,
42+
'tolerance': 1e-6
43+
};
44+
sumSeries( generator, opts ); // $ExpectType number
45+
}
46+
47+
// The compiler throws an error if the function is provided a first argument which is not a function...
48+
{
49+
sumSeries( 'abc' ); // $ExpectError
50+
sumSeries( 123 ); // $ExpectError
51+
sumSeries( false ); // $ExpectError
52+
sumSeries( true ); // $ExpectError
53+
sumSeries( [] ); // $ExpectError
54+
sumSeries( {} ); // $ExpectError
55+
}
56+
57+
// The compiler throws an error if the function is provided a `initialValue` option which is not a number...
58+
{
59+
sumSeries( generator, { 'initialValue': 'abc' } ); // $ExpectError
60+
sumSeries( generator, { 'initialValue': true } ); // $ExpectError
61+
sumSeries( generator, { 'initialValue': false } ); // $ExpectError
62+
sumSeries( generator, { 'initialValue': null } ); // $ExpectError
63+
sumSeries( generator, { 'initialValue': [] } ); // $ExpectError
64+
sumSeries( generator, { 'initialValue': {} } ); // $ExpectError
65+
sumSeries( generator, { 'initialValue': ( x: number ): number => x } ); // $ExpectError
66+
}
67+
68+
// The compiler throws an error if the function is provided a `maxTerms` option which is not a number...
69+
{
70+
sumSeries( generator, { 'maxTerms': 'abc' } ); // $ExpectError
71+
sumSeries( generator, { 'maxTerms': true } ); // $ExpectError
72+
sumSeries( generator, { 'maxTerms': false } ); // $ExpectError
73+
sumSeries( generator, { 'maxTerms': null } ); // $ExpectError
74+
sumSeries( generator, { 'maxTerms': [] } ); // $ExpectError
75+
sumSeries( generator, { 'maxTerms': {} } ); // $ExpectError
76+
sumSeries( generator, { 'maxTerms': ( x: number ): number => x } ); // $ExpectError
77+
}
78+
79+
// The compiler throws an error if the function is provided a `tolerance` option which is not a number...
80+
{
81+
sumSeries( generator, { 'tolerance': 'abc' } ); // $ExpectError
82+
sumSeries( generator, { 'tolerance': true } ); // $ExpectError
83+
sumSeries( generator, { 'tolerance': false } ); // $ExpectError
84+
sumSeries( generator, { 'tolerance': null } ); // $ExpectError
85+
sumSeries( generator, { 'tolerance': [] } ); // $ExpectError
86+
sumSeries( generator, { 'tolerance': {} } ); // $ExpectError
87+
sumSeries( generator, { 'tolerance': ( x: number ): number => x } ); // $ExpectError
88+
}
89+
90+
// The compiler throws an error if the function is provided insufficient arguments...
91+
{
92+
sumSeries(); // $ExpectError
93+
}

0 commit comments

Comments
 (0)