Skip to content

feat: add math/base/special/gammasgnf #3365

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

Merged
merged 19 commits into from
May 16, 2025
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
Show all changes
19 commits
Select commit Hold shift + click to select a range
ffafd6c
feat(math): add math/base/special/gammasgnf
vivekmaurya001 Dec 7, 2024
34d42d2
Merge branch 'gammasgnf' of https://github.com/vivekmaurya001/stdlib …
vivekmaurya001 Dec 7, 2024
0c79f38
chore: update copyright years
stdlib-bot Dec 7, 2024
c975792
Merge branch 'stdlib-js:develop' into gammasgnf
vivekmaurya001 Dec 14, 2024
3ef55ba
feat(add gammasgnf): add math/base/special/gammasgnf
vivekmaurya001 Dec 14, 2024
1019f44
Merge branch 'gammasgnf' of https://github.com/vivekmaurya001/stdlib …
vivekmaurya001 Dec 14, 2024
16dc7a8
Merge branch 'stdlib-js:develop' into gammasgnf
vivekmaurya001 Jan 18, 2025
cb42022
add math/base/special/gammasgnf
vivekmaurya001 Jan 18, 2025
d1c8ae6
chore: update copyright years
stdlib-bot Jan 18, 2025
fa86fcd
add math/base/special/gammasgnf
vivekmaurya001 Jan 18, 2025
87a4039
Merge branch 'gammasgnf' of https://github.com/vivekmaurya001/stdlib …
vivekmaurya001 Jan 18, 2025
f9c5cc9
add math/base/special/gammasgnf
vivekmaurya001 Jan 18, 2025
efefd9b
Update lib/node_modules/@stdlib/math/base/special/gammasgnf/src/addon.c
gunjjoshi Jan 20, 2025
f9cfa42
Merge branch 'stdlib-js:develop' into gammasgnf
vivekmaurya001 Mar 8, 2025
ce4380d
feat: add math/base/special/gammasgnf
vivekmaurya001 Mar 8, 2025
fb11d85
feat: add math/base/special/gammasgnf
vivekmaurya001 Mar 8, 2025
8f8f7ae
Merge remote-tracking branch 'upstream/develop' into gammasgnf
stdlib-bot May 15, 2025
216b78f
fix: code review
anandkaranubc May 15, 2025
1ee9932
Apply suggestions from code review
kgryte May 16, 2025
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
217 changes: 217 additions & 0 deletions lib/node_modules/@stdlib/math/base/special/gammasgnf/README.md
Original file line number Diff line number Diff line change
@@ -0,0 +1,217 @@
<!--

@license Apache-2.0

Copyright (c) 2025 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.

-->

# gammasgnf

> Sign of the [gamma function][@stdlib/math/base/special/gamma] for a single-precision floating-point number.

<section class="intro">

The sign of the [gamma-function][@stdlib/math/base/special/gamma] is defined as

<!-- <equation class="equation" label="eq:gamma_sign_function" align="center" raw="\operatorname{gammasgn} ( x ) = \begin{cases} 1 & \textrm{if}\ \Gamma > 0 \\ -1 & \textrm{if}\ \Gamma < 0 \\ 0 & \textrm{otherwise}\ \end{cases}" alt="Sign of the gamma function"> -->

```math
\mathop{\mathrm{gammasgn}} ( x ) = \begin{cases} 1 & \textrm{if}\ \Gamma > 0 \\ -1 & \textrm{if}\ \Gamma < 0 \\ 0 & \textrm{otherwise}\ \end{cases}
```

<!-- </equation> -->

The [gamma function][@stdlib/math/base/special/gamma] can be computed as the product of `gammasgn(x)` and `exp(gammaln(x))`.

</section>

<!-- /.intro -->

<section class="usage">

## Usage

```javascript
var gammasgnf = require( '@stdlib/math/base/special/gammasgnf' );
```

#### gammasgnf( x )

Computes the sign of the [gamma function][@stdlib/math/base/special/gamma] for a single-precision floating-point number.

```javascript
var v = gammasgnf( 1.0 );
// returns 1.0

v = gammasgnf( -2.5 );
// returns -1.0

v = gammasgnf( 0.0 );
// returns 0.0

v = gammasgnf( NaN );
// returns NaN
```

</section>

<!-- /.usage -->

<section class="notes">

## Notes

- The [gamma function][@stdlib/math/base/special/gamma] is not defined for negative integer values (i.e., `gamma(x) === NaN` when `x` is a negative integer). The [natural logarithm of the gamma function][@stdlib/math/base/special/gammaln] is defined for negative integer values (i.e., `gammaln(x) === Infinity` when `x` is a negative integer). Accordingly, in order for the equality `gamma(x) === gammasgn(x) * exp(gammaln(x))` to hold (i.e., return `NaN`), `gammasgn` needs to either return `NaN` or `0`. By convention, this function returns `0`.

</section>

<!-- /. notes -->

<section class="examples">

## Examples

<!-- eslint no-undef: "error" -->

```javascript
var logEachMap = require( '@stdlib/console/log-each-map' );
var uniform = require( '@stdlib/random/array/uniform' );
var gammasgnf = require( '@stdlib/math/base/special/gammasgnf' );

var x = uniform( 100, -10.0, 10.0, {
'dtype': 'float32'
});

logEachMap( 'x: %0.4f, f(x): %0.4f', x, gammasgnf );
```

</section>

<!-- /.examples -->

<!-- C interface documentation. -->

* * *

<section class="c">

## C APIs

<!-- Section to include introductory text. Make sure to keep an empty line after the intro `section` element and another before the `/section` close. -->

<section class="intro">

</section>

<!-- /.intro -->

<!-- C usage documentation. -->

<section class="usage">

### Usage

```c
#include "stdlib/math/base/special/gammasgnf.h"
```

#### stdlib_base_gammasgnf( x )

Computes the sign of the [gamma function][@stdlib/math/base/special/gamma] for a single-precision floating-point number.

```c
float out = stdlib_base_gammasgnf( 1.0f );
// returns 1.0f

out = stdlib_base_gammasgnf( -2.5f );
// returns -1.0f
```

The function accepts the following arguments:

- **x**: `[in] float` input value.

```c
float stdlib_base_gammasgnf( const float x );
```

</section>

<!-- /.usage -->

<!-- C API usage notes. Make sure to keep an empty line after the `section` element and another before the `/section` close. -->

<section class="notes">

</section>

<!-- /.notes -->

<!-- C API usage examples. -->

<section class="examples">

### Examples

```c
#include "stdlib/math/base/special/gammasgnf.h"
#include <stdlib.h>
#include <stdio.h>

int main( void ) {
float x;
float v;
int i;

for ( i = 0; i < 100; i++ ) {
x = ( (float)rand() / (float)RAND_MAX ) * 100.0f;
v = stdlib_base_gammasgnf( x );
printf( "gammasgnf(%f) = %f\n", x, v );
}
}
```

</section>

<!-- /.examples -->

</section>

<!-- /.c -->

<!-- Section for related `stdlib` packages. Do not manually edit this section, as it is automatically populated. -->

<section class="related">

</section>

<!-- /.related -->

<!-- Section for all links. Make sure to keep an empty line after the `section` element and another before the `/section` close. -->

<section class="links">

[@stdlib/math/base/special/gamma]: https://github.com/stdlib-js/stdlib/tree/develop/lib/node_modules/%40stdlib/math/base/special/gamma

[@stdlib/math/base/special/gammaln]: https://github.com/stdlib-js/stdlib/tree/develop/lib/node_modules/%40stdlib/math/base/special/gammaln

<!-- <related-links> -->

<!-- </related-links> -->

</section>

<!-- /.links -->
Original file line number Diff line number Diff line change
@@ -0,0 +1,54 @@
/**
* @license Apache-2.0
*
* Copyright (c) 2025 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 uniform = require( '@stdlib/random/array/uniform' );
var isnanf = require( '@stdlib/math/base/assert/is-nanf' );
var pkg = require( './../package.json' ).name;
var gammasgnf = require( './../lib' );


// MAIN //

bench( pkg, function benchmark( b ) {
var x;
var y;
var i;

x = uniform( 100, -10.0, 10.0, {
'dtype': 'float32'
});

b.tic();
for ( i = 0; i < b.iterations; i++ ) {
y = gammasgnf( 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();
});
Original file line number Diff line number Diff line change
@@ -0,0 +1,63 @@
/**
* @license Apache-2.0
*
* Copyright (c) 2025 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 uniform = require( '@stdlib/random/array/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 gammasgnf = tryRequire( resolve( __dirname, './../lib/native.js' ) );
var opts = {
'skip': ( gammasgnf instanceof Error )
};


// MAIN //

bench( pkg+'::native', opts, function benchmark( b ) {
var x;
var y;
var i;

x = uniform( 100, -10.0, 10.0, {
'dtype': 'float32'
});

b.tic();
for ( i = 0; i < b.iterations; i++ ) {
y = gammasgnf( 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();
});
Loading