Skip to content

Commit

Permalink
Generalized Bernoulli polynomials
Browse files Browse the repository at this point in the history
  • Loading branch information
paulmasson committed Sep 10, 2023
1 parent c3d1af2 commit 931ee02
Show file tree
Hide file tree
Showing 3 changed files with 54 additions and 16 deletions.
33 changes: 26 additions & 7 deletions build/math.js
Original file line number Diff line number Diff line change
Expand Up @@ -4414,19 +4414,38 @@ function dirichletEta( x ) { return mul( zeta(x), sub( 1, pow( 2, sub(1,x) ) ) )

function bernoulli( n, x ) {

if ( !Number.isInteger(n) ) throw Error( 'Noninteger index for Bernoulli number' );
if ( arguments.length === 2 ) {

if ( n < 0 ) throw Error( 'Unsupported index for Bernoulli number' );
if ( isZero(n) ) return isComplex(n) || isComplex(x) ? complex(1) : 1;

if ( arguments.length > 1 && !isZero(x) ) return mul( -n, hurwitzZeta(1-n,x) );
// avoid Hurwitz zeta parameter poles
if ( isNegativeIntegerOrZero(x) ) return complexAverage( x => bernoulli(n,x), x );

if ( n === 0 ) return 1;
return mul( neg(n), hurwitzZeta( sub(1,n), x ) );

}

if ( Number.isInteger(n) && n >= 0 ) {

if ( n === 0 ) return 1;

if ( n === 1 ) return -.5;
if ( n === 1 ) return -.5;

if ( n & 1 ) return 0;

var m = n/2;
if ( m <= bernoulli2nN.length )
return arbitrary( div( bernoulli2nN[m], bernoulli2nD[m] ) );

return -n * zeta(1-n);

}

if ( n & 1 ) return 0;
if ( isPositiveIntegerOrZero(n) ) return complex( bernoulli( n.re ) );

return -n * zeta(1-n);
// generalized Bernoulli number
console.log( 'Returning generalized Bernoulli number' );
return bernoulli( n, 0 );

}

Expand Down
4 changes: 2 additions & 2 deletions docs/functions.html
Original file line number Diff line number Diff line change
Expand Up @@ -335,9 +335,9 @@ <h1>Zeta Functions</h1>

<p><a href="functions/dirichletEta.html"><b>dirichletEta( <i>x</i> )</b></a> &mdash; Dirichlet eta function of a real or complex number</p>

<p><b>bernoulli( <i>n</i> )</b> &mdash; Bernoulli number for index <i>n</i></p>
<p><b>bernoulli( <i>n</i> )</b> &mdash; Bernoulli number for index <i>n</i>. Returns a generalized value for fractional, negative or complex indices.</p>

<p><b>bernoulli( <i>n</i>, <i>x</i> )</b> &mdash; Bernoulli polynomial for index <i>n</i> of a real or complex number</p>
<p><b>bernoulli( <i>n</i>, <i>x</i> )</b> &mdash; Bernoulli polynomial for real or complex index <i>n</i> of a real or complex number</p>

<p><b>harmonic( <i>n</i> )</b> &mdash; harmonic number for index <i>n</i></p>

Expand Down
33 changes: 26 additions & 7 deletions src/functions/zeta.js
Original file line number Diff line number Diff line change
Expand Up @@ -79,19 +79,38 @@ function dirichletEta( x ) { return mul( zeta(x), sub( 1, pow( 2, sub(1,x) ) ) )

function bernoulli( n, x ) {

if ( !Number.isInteger(n) ) throw Error( 'Noninteger index for Bernoulli number' );
if ( arguments.length === 2 ) {

if ( n < 0 ) throw Error( 'Unsupported index for Bernoulli number' );
if ( isZero(n) ) return isComplex(n) || isComplex(x) ? complex(1) : 1;

if ( arguments.length > 1 && !isZero(x) ) return mul( -n, hurwitzZeta(1-n,x) );
// avoid Hurwitz zeta parameter poles
if ( isNegativeIntegerOrZero(x) ) return complexAverage( x => bernoulli(n,x), x );

if ( n === 0 ) return 1;
return mul( neg(n), hurwitzZeta( sub(1,n), x ) );

if ( n === 1 ) return -.5;
}

if ( Number.isInteger(n) && n >= 0 ) {

if ( n === 0 ) return 1;

if ( n === 1 ) return -.5;

if ( n & 1 ) return 0;

var m = n/2;
if ( m <= bernoulli2nN.length )
return arbitrary( div( bernoulli2nN[m], bernoulli2nD[m] ) );

return -n * zeta(1-n);

}

if ( n & 1 ) return 0;
if ( isPositiveIntegerOrZero(n) ) return complex( bernoulli( n.re ) );

return -n * zeta(1-n);
// generalized Bernoulli number
console.log( 'Returning generalized Bernoulli number' );
return bernoulli( n, 0 );

}

Expand Down

0 comments on commit 931ee02

Please sign in to comment.