-
-
Notifications
You must be signed in to change notification settings - Fork 907
feat: add math/base/special/bernoullif
#3037
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
Changes from 16 commits
b796c03
c54b6bc
5489bb6
1824084
d6e8e6b
7c37cc0
aca6ec3
2dcd971
46c3d20
e5b1927
2634808
90cb9e1
0ba3ac6
9822a83
a7e3e92
da01fa4
c9c3de0
da8ec49
12053ac
e197e41
dc31e82
File filter
Filter by extension
Conversations
Jump to
Diff view
Diff view
There are no files selected for viewing
Original file line number | Diff line number | Diff line change | ||||||||||||||||||||||||||||||||||||
---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|
@@ -0,0 +1,232 @@ | ||||||||||||||||||||||||||||||||||||||
<!-- | ||||||||||||||||||||||||||||||||||||||
|
||||||||||||||||||||||||||||||||||||||
@license Apache-2.0 | ||||||||||||||||||||||||||||||||||||||
|
||||||||||||||||||||||||||||||||||||||
Copyright (c) 2024 The Stdlib Authors. | ||||||||||||||||||||||||||||||||||||||
|
||||||||||||||||||||||||||||||||||||||
Licensed under the Apache License, Version 2.0 (the "License"); | ||||||||||||||||||||||||||||||||||||||
you may not use this file except in compliance with the License. | ||||||||||||||||||||||||||||||||||||||
You may obtain a copy of the License at | ||||||||||||||||||||||||||||||||||||||
|
||||||||||||||||||||||||||||||||||||||
http://www.apache.org/licenses/LICENSE-2.0 | ||||||||||||||||||||||||||||||||||||||
|
||||||||||||||||||||||||||||||||||||||
Unless required by applicable law or agreed to in writing, software | ||||||||||||||||||||||||||||||||||||||
distributed under the License is distributed on an "AS IS" BASIS, | ||||||||||||||||||||||||||||||||||||||
WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. | ||||||||||||||||||||||||||||||||||||||
See the License for the specific language governing permissions and | ||||||||||||||||||||||||||||||||||||||
limitations under the License. | ||||||||||||||||||||||||||||||||||||||
|
||||||||||||||||||||||||||||||||||||||
--> | ||||||||||||||||||||||||||||||||||||||
|
||||||||||||||||||||||||||||||||||||||
# bernoullif | ||||||||||||||||||||||||||||||||||||||
|
||||||||||||||||||||||||||||||||||||||
> Compute the nth [Bernoulli number][bernoulli-number] as a single-precision floating-point number. | ||||||||||||||||||||||||||||||||||||||
|
||||||||||||||||||||||||||||||||||||||
<section class="intro"> | ||||||||||||||||||||||||||||||||||||||
|
||||||||||||||||||||||||||||||||||||||
<!-- /.intro --> | ||||||||||||||||||||||||||||||||||||||
|
||||||||||||||||||||||||||||||||||||||
<section class="usage"> | ||||||||||||||||||||||||||||||||||||||
|
||||||||||||||||||||||||||||||||||||||
## Usage | ||||||||||||||||||||||||||||||||||||||
|
||||||||||||||||||||||||||||||||||||||
```javascript | ||||||||||||||||||||||||||||||||||||||
var bernoullif = require( '@stdlib/math/base/special/bernoullif' ); | ||||||||||||||||||||||||||||||||||||||
``` | ||||||||||||||||||||||||||||||||||||||
|
||||||||||||||||||||||||||||||||||||||
#### bernoullif( n ) | ||||||||||||||||||||||||||||||||||||||
|
||||||||||||||||||||||||||||||||||||||
Compute the nth [Bernoulli number][bernoulli-number] as a single-precision floating-point number. | ||||||||||||||||||||||||||||||||||||||
|
||||||||||||||||||||||||||||||||||||||
```javascript | ||||||||||||||||||||||||||||||||||||||
var v = bernoullif( 0 ); | ||||||||||||||||||||||||||||||||||||||
// returns 1.0 | ||||||||||||||||||||||||||||||||||||||
|
||||||||||||||||||||||||||||||||||||||
v = bernoullif( 1 ); | ||||||||||||||||||||||||||||||||||||||
// returns 0.5 | ||||||||||||||||||||||||||||||||||||||
|
||||||||||||||||||||||||||||||||||||||
v = bernoullif( 2 ); | ||||||||||||||||||||||||||||||||||||||
// returns ~0.167 | ||||||||||||||||||||||||||||||||||||||
|
||||||||||||||||||||||||||||||||||||||
v = bernoullif( 3 ); | ||||||||||||||||||||||||||||||||||||||
// returns 0.0 | ||||||||||||||||||||||||||||||||||||||
|
||||||||||||||||||||||||||||||||||||||
v = bernoullif( 4 ); | ||||||||||||||||||||||||||||||||||||||
// returns ~-0.033 | ||||||||||||||||||||||||||||||||||||||
|
||||||||||||||||||||||||||||||||||||||
v = bernoullif( 5 ); | ||||||||||||||||||||||||||||||||||||||
// returns 0.0 | ||||||||||||||||||||||||||||||||||||||
|
||||||||||||||||||||||||||||||||||||||
v = bernoullif( 20 ); | ||||||||||||||||||||||||||||||||||||||
// returns ~-529.124 | ||||||||||||||||||||||||||||||||||||||
``` | ||||||||||||||||||||||||||||||||||||||
|
||||||||||||||||||||||||||||||||||||||
For even integers `n >= 66`, the function alternates between returning positive and negative infinity, as larger [Bernoulli numbers][bernoulli-number] cannot be safely represented in [single-precision floating-point format][ieee754] | ||||||||||||||||||||||||||||||||||||||
|
||||||||||||||||||||||||||||||||||||||
```javascript | ||||||||||||||||||||||||||||||||||||||
var v = bernoullif( 66 ); | ||||||||||||||||||||||||||||||||||||||
// returns Infinity | ||||||||||||||||||||||||||||||||||||||
|
||||||||||||||||||||||||||||||||||||||
v = bernoullif( 68 ); | ||||||||||||||||||||||||||||||||||||||
// returns -Infinity | ||||||||||||||||||||||||||||||||||||||
|
||||||||||||||||||||||||||||||||||||||
v = bernoullif( 70 ); | ||||||||||||||||||||||||||||||||||||||
// returns Infinity | ||||||||||||||||||||||||||||||||||||||
``` | ||||||||||||||||||||||||||||||||||||||
|
||||||||||||||||||||||||||||||||||||||
If not provided a nonnegative integer value, the function returns `NaN`. | ||||||||||||||||||||||||||||||||||||||
|
||||||||||||||||||||||||||||||||||||||
```javascript | ||||||||||||||||||||||||||||||||||||||
var v = bernoullif( 3.14 ); | ||||||||||||||||||||||||||||||||||||||
// returns NaN | ||||||||||||||||||||||||||||||||||||||
|
||||||||||||||||||||||||||||||||||||||
v = bernoullif( -1 ); | ||||||||||||||||||||||||||||||||||||||
// returns NaN | ||||||||||||||||||||||||||||||||||||||
``` | ||||||||||||||||||||||||||||||||||||||
|
||||||||||||||||||||||||||||||||||||||
If provided `NaN`, the function returns `NaN`. | ||||||||||||||||||||||||||||||||||||||
|
||||||||||||||||||||||||||||||||||||||
```javascript | ||||||||||||||||||||||||||||||||||||||
var v = bernoullif( NaN ); | ||||||||||||||||||||||||||||||||||||||
// returns NaN | ||||||||||||||||||||||||||||||||||||||
``` | ||||||||||||||||||||||||||||||||||||||
|
||||||||||||||||||||||||||||||||||||||
</section> | ||||||||||||||||||||||||||||||||||||||
|
||||||||||||||||||||||||||||||||||||||
<!-- /.usage --> | ||||||||||||||||||||||||||||||||||||||
|
||||||||||||||||||||||||||||||||||||||
<section class="notes"> | ||||||||||||||||||||||||||||||||||||||
|
||||||||||||||||||||||||||||||||||||||
</section> | ||||||||||||||||||||||||||||||||||||||
|
||||||||||||||||||||||||||||||||||||||
<!-- /.notes --> | ||||||||||||||||||||||||||||||||||||||
|
||||||||||||||||||||||||||||||||||||||
<section class="examples"> | ||||||||||||||||||||||||||||||||||||||
|
||||||||||||||||||||||||||||||||||||||
## Examples | ||||||||||||||||||||||||||||||||||||||
|
||||||||||||||||||||||||||||||||||||||
<!-- eslint no-undef: "error" --> | ||||||||||||||||||||||||||||||||||||||
|
||||||||||||||||||||||||||||||||||||||
```javascript | ||||||||||||||||||||||||||||||||||||||
var bernoullif = require( '@stdlib/math/base/special/bernoullif' ); | ||||||||||||||||||||||||||||||||||||||
|
||||||||||||||||||||||||||||||||||||||
var v; | ||||||||||||||||||||||||||||||||||||||
var i; | ||||||||||||||||||||||||||||||||||||||
|
||||||||||||||||||||||||||||||||||||||
for ( i = 0; i < 70; i++ ) { | ||||||||||||||||||||||||||||||||||||||
v = bernoullif( i ); | ||||||||||||||||||||||||||||||||||||||
console.log( v ); | ||||||||||||||||||||||||||||||||||||||
} | ||||||||||||||||||||||||||||||||||||||
|
var bernoullif = require( '@stdlib/math/base/special/bernoullif' ); | |
var v; | |
var i; | |
for ( i = 0; i < 70; i++ ) { | |
v = bernoullif( i ); | |
console.log( v ); | |
} | |
var logEachMap = require( '@stdlib/console/log-each-map' ); | |
var discreteUniform = require( '@stdlib/random/array/discrete-uniform' ); | |
var bernoullif = require( './../lib' ); | |
var x = discreteUniform( 100, 0, 70, { | |
'dtype': 'int32' | |
}); | |
logEachMap( 'bernoullif(%d) = %0.4f', x, bernoullif ); |
Planeshifter marked this conversation as resolved.
Outdated
Show resolved
Hide resolved
Outdated
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
// returns 1.0 | |
// returns 1.0f |
Outdated
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
// returns 0.5 | |
// returns 0.5f |
gururaj1512 marked this conversation as resolved.
Show resolved
Hide resolved
|
Original file line number | Diff line number | Diff line change | ||||
---|---|---|---|---|---|---|
@@ -0,0 +1,52 @@ | ||||||
/** | ||||||
* @license Apache-2.0 | ||||||
* | ||||||
* Copyright (c) 2024 The Stdlib Authors. | ||||||
* | ||||||
* Licensed under the Apache License, Version 2.0 (the "License"); | ||||||
* you may not use this file except in compliance with the License. | ||||||
* You may obtain a copy of the License at | ||||||
* | ||||||
* http://www.apache.org/licenses/LICENSE-2.0 | ||||||
* | ||||||
* Unless required by applicable law or agreed to in writing, software | ||||||
* distributed under the License is distributed on an "AS IS" BASIS, | ||||||
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. | ||||||
* See the License for the specific language governing permissions and | ||||||
* limitations under the License. | ||||||
*/ | ||||||
|
||||||
'use strict'; | ||||||
|
||||||
// MODULES // | ||||||
|
||||||
var bench = require( '@stdlib/bench' ); | ||||||
var randu = require( '@stdlib/random/array/discrete-uniform' ); | ||||||
|
var randu = require( '@stdlib/random/array/discrete-uniform' ); | |
var discreteUniform = require( '@stdlib/random/array/discrete-uniform' ); |
For being consistent across other packages.
Outdated
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
x = randu( 100, 0, 500 ); | |
x = discreteUniform( 100, 0, 100 ); |
100 is fine here for the max value.
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,61 @@ | ||
/** | ||
* @license Apache-2.0 | ||
* | ||
* Copyright (c) 2024 The Stdlib Authors. | ||
* | ||
* Licensed under the Apache License, Version 2.0 (the "License"); | ||
* you may not use this file except in compliance with the License. | ||
* You may obtain a copy of the License at | ||
* | ||
* http://www.apache.org/licenses/LICENSE-2.0 | ||
* | ||
* Unless required by applicable law or agreed to in writing, software | ||
* distributed under the License is distributed on an "AS IS" BASIS, | ||
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. | ||
* See the License for the specific language governing permissions and | ||
* limitations under the License. | ||
*/ | ||
|
||
'use strict'; | ||
|
||
// MODULES // | ||
|
||
var resolve = require( 'path' ).resolve; | ||
var bench = require( '@stdlib/bench' ); | ||
var randu = require( '@stdlib/random/array/discrete-uniform' ); | ||
var isnanf = require( '@stdlib/math/base/assert/is-nanf' ); | ||
var tryRequire = require( '@stdlib/utils/try-require' ); | ||
var pkg = require( './../package.json' ).name; | ||
|
||
|
||
// VARIABLES // | ||
|
||
var bernoullif = tryRequire( resolve( __dirname, './../lib/native.js' ) ); | ||
var opts = { | ||
'skip': ( bernoullif instanceof Error ) | ||
}; | ||
|
||
|
||
// MAIN // | ||
|
||
bench( pkg+'::native', opts, function benchmark( b ) { | ||
var x; | ||
var y; | ||
var i; | ||
|
||
x = randu( 100, 0, 500 ); | ||
|
||
b.tic(); | ||
for ( i = 0; i < b.iterations; i++ ) { | ||
y = bernoullif( x[ i % x.length ] ); | ||
if ( isnanf( y ) ) { | ||
b.fail( 'should not return NaN' ); | ||
} | ||
} | ||
b.toc(); | ||
if ( isnanf( y ) ) { | ||
b.fail( 'should not return NaN' ); | ||
} | ||
b.pass( 'benchmark finished' ); | ||
b.end(); | ||
}); |
Uh oh!
There was an error while loading. Please reload this page.