1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67
|
{{alias}}( generator[, options] )
Sum the elements of the series given by the supplied function.
Parameters
----------
generator: Function
Series function.
options: Object (optional)
Options.
options.maxTerms: integer (optional)
Maximum number of terms to be added. Default: `1000000`.
options.tolerance: number (optional)
Further terms are only added as long as the next term is greater than
the current term times the tolerance. Default: `2.22e-16`.
options.initialValue: number (optional)
Initial value of the resulting sum. Default: `0`.
Returns
-------
out: number
Sum of series terms.
Examples
--------
// Using an ES6 generator function:
> function* geometricSeriesGenerator( x ) {
... var exponent = 0;
... while ( true ) {
... yield Math.pow( x, exponent );
... exponent += 1;
... }
... };
> var gen = geometricSeriesGenerator( 0.9 );
> var out = {{alias}}( gen )
10
// Using a closure:
> function geometricSeriesClosure( x ) {
... var exponent = -1;
... return function() {
... exponent += 1;
... return Math.pow( x, exponent );
... };
... };
> gen = geometricSeriesClosure( 0.9 );
> out = {{alias}}( gen )
10
// Setting an initial value for the sum:
> out = {{alias}}( geometricSeriesGenerator( 0.5 ), { 'initialValue': 1 } )
3
// Changing the maximum number of terms to be summed:
> out = {{alias}}( geometricSeriesGenerator( 0.5 ), { 'maxTerms': 10 } )
~1.998 // Infinite sum is 2
// Adjusting the used tolerance:
> out = {{alias}}( geometricSeriesGenerator( 0.5 ), { 'tolerance': 1e-3 } )
~1.998
See Also
--------
|