Skip to content

Commit

Permalink
Allow arbitrary step
Browse files Browse the repository at this point in the history
  • Loading branch information
paulmasson committed Mar 27, 2024
1 parent 36a5d0e commit 37061d4
Show file tree
Hide file tree
Showing 3 changed files with 9 additions and 7 deletions.
6 changes: 3 additions & 3 deletions build/math.js
Original file line number Diff line number Diff line change
Expand Up @@ -5237,21 +5237,21 @@ function discreteIntegral( values, step ) {
}


function summation( f, [a,b] ) {
function summation( f, [a,b,step=1] ) {

if ( isComplex( f(a) ) ) {

var s = complex(0);

for ( var i = a ; i <= b ; i++ ) s = add( s, f(i) );
for ( var i = a ; i <= b ; i += step ) s = add( s, f(i) );

return s;

} else {

var s = 0;

for ( var i = a ; i <= b ; i++ ) s += f(i);
for ( var i = a ; i <= b ; i += step ) s += f(i);

return s;

Expand Down
4 changes: 3 additions & 1 deletion docs/calculus.html
Original file line number Diff line number Diff line change
Expand Up @@ -45,7 +45,9 @@ <h1>Integration</h1>

<p><b>discreteIntegral( values, step )</b> &mdash; numerical integral over discrete real values separated by step using Euler-Maclaurin summation</p>

<p><b>summation( <i>f</i>, [<i>a</i>,<i>b</i>] )</b> &mdash; discrete summation of real or complex function values from <i>a</i> to <i>b</i> inclusive in integer steps</p>
<p><b>summation( <i>f</i>, [<i>a</i>,<i>b</i>] )</b> &mdash; discrete summation of real or complex function values from <i>a</i> to <i>b</i> inclusive by unit step</p>

<p><b>summation( <i>f</i>, [<i>a</i>,<i>b</i>,step] )</b> &mdash; discrete summation of real or complex function values from <i>a</i> to <i>b</i> inclusive by arbitrary step</p>

<br id="interpolation"/>
<h1>Interpolation</h1>
Expand Down
6 changes: 3 additions & 3 deletions src/calculus/integration.js
Original file line number Diff line number Diff line change
Expand Up @@ -241,21 +241,21 @@ function discreteIntegral( values, step ) {
}


function summation( f, [a,b] ) {
function summation( f, [a,b,step=1] ) {

if ( isComplex( f(a) ) ) {

var s = complex(0);

for ( var i = a ; i <= b ; i++ ) s = add( s, f(i) );
for ( var i = a ; i <= b ; i += step ) s = add( s, f(i) );

return s;

} else {

var s = 0;

for ( var i = a ; i <= b ; i++ ) s += f(i);
for ( var i = a ; i <= b ; i += step ) s += f(i);

return s;

Expand Down

0 comments on commit 37061d4

Please sign in to comment.