Skip to content

Commit ffafd6c

Browse files
feat(math): add math/base/special/gammasgnf
1 parent a0ba090 commit ffafd6c

File tree

32 files changed

+2254
-0
lines changed

32 files changed

+2254
-0
lines changed
Lines changed: 222 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,222 @@
1+
<!--
2+
3+
@license Apache-2.0
4+
5+
Copyright (c) 2022 The Stdlib Authors.
6+
7+
Licensed under the Apache License, Version 2.0 (the "License");
8+
you may not use this file except in compliance with the License.
9+
You may obtain a copy of the License at
10+
11+
http://www.apache.org/licenses/LICENSE-2.0
12+
13+
Unless required by applicable law or agreed to in writing, software
14+
distributed under the License is distributed on an "AS IS" BASIS,
15+
WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
16+
See the License for the specific language governing permissions and
17+
limitations under the License.
18+
19+
-->
20+
21+
# gammasgnf
22+
23+
> Sign of the [gamma function][@stdlib/math/base/special/gamma] for a single precision-floating point number.
24+
25+
<section class="intro">
26+
27+
The sign of the [gamma-function][@stdlib/math/base/special/gamma] is defined as
28+
29+
<!-- <equation class="equation" label="eq:gamma_sign_function" align="center" raw="\operatorname{gammasgnf} ( x ) = \begin{cases} 1 & \textrm{if}\ \Gamma > 1 \\ -1 & \textrm{if}\ \Gamma < 1 \\ 0 & \textrm{otherwise}\ \end{cases}" alt="Sign of the gamma function"> -->
30+
31+
```math
32+
\mathop{\mathrm{gammasgnf}} ( x ) = \begin{cases} 1 & \textrm{if}\ \Gamma > 1 \\ -1 & \textrm{if}\ \Gamma < 1 \\ 0 & \textrm{otherwise}\ \end{cases}
33+
```
34+
35+
<!-- <div class="equation" align="center" data-raw-text="\operatorname{gammasgnf} ( x ) = \begin{cases} 1 &amp; \textrm{if}\ \Gamma &gt; 1 \\ -1 &amp; \textrm{if}\ \Gamma &lt; 1 \\ 0 &amp; \textrm{otherwise}\ \end{cases}" data-equation="eq:gamma_sign_function">
36+
<img src="https://cdn.jsdelivr.net/gh/stdlib-js/stdlib@50b141156b147529227e2eb4247eda81c781dec9/lib/node_modules/@stdlib/math/base/special/gammasgnf/docs/img/equation_gamma_sign_function.svg" alt="Sign of the gamma function">
37+
<br>
38+
</div> -->
39+
40+
<!-- </equation> -->
41+
42+
The [gamma function][@stdlib/math/base/special/gamma] can be computed as the product of `gammasgnf(x)` and `exp(gammaln(x))`.
43+
44+
</section>
45+
46+
<!-- /.intro -->
47+
48+
<section class="usage">
49+
50+
## Usage
51+
52+
```javascript
53+
var gammasgnf = require( '@stdlib/math/base/special/gammasgnf' );
54+
```
55+
56+
#### gammasgnf( x )
57+
58+
Returns the sign of the [gamma function][@stdlib/math/base/special/gamma].
59+
60+
```javascript
61+
var v = gammasgnf( 1.0 );
62+
// returns 1.0
63+
64+
v = gammasgnf( -2.5 );
65+
// returns -1.0
66+
67+
v = gammasgnf( 0.0 );
68+
// returns 0.0
69+
70+
v = gammasgnf( NaN );
71+
// returns NaN
72+
```
73+
74+
</section>
75+
76+
<!-- /.usage -->
77+
78+
<section class="notes">
79+
80+
## Notes
81+
82+
- 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) === gammasgnf(x) * exp(gammaln(x))` to hold (i.e., return `NaN`), `gammasgnf` needs to either return `NaN` or `0`. By convention, this function returns `0`.
83+
84+
</section>
85+
86+
<!-- /. notes -->
87+
88+
<section class="examples">
89+
90+
## Examples
91+
92+
<!-- eslint no-undef: "error" -->
93+
94+
```javascript
95+
var linspace = require( '@stdlib/array/base/linspace' );
96+
var gammasgnf = require( '@stdlib/math/base/special/gammasgnf' );
97+
98+
var x = linspace( -10.0, 10.0, 100 );
99+
100+
var i;
101+
for ( i = 0; i < x.length; i++ ) {
102+
console.log( 'x: %d, f(x): %f', x[ i ], gammasgnf( x[ i ] ) );
103+
}
104+
```
105+
106+
</section>
107+
108+
<!-- /.examples -->
109+
110+
<!-- C interface documentation. -->
111+
112+
* * *
113+
114+
<section class="c">
115+
116+
## C APIs
117+
118+
<!-- Section to include introductory text. Make sure to keep an empty line after the intro `section` element and another before the `/section` close. -->
119+
120+
<section class="intro">
121+
122+
</section>
123+
124+
<!-- /.intro -->
125+
126+
<!-- C usage documentation. -->
127+
128+
<section class="usage">
129+
130+
### Usage
131+
132+
```c
133+
#include "stdlib/math/base/special/gammasgnf.h"
134+
```
135+
136+
#### stdlib_base_gammasgnf( x )
137+
138+
Returns the sign of the [gamma-function][@stdlib/math/base/special/gamma].
139+
140+
```c
141+
float out = stdlib_base_gammasgnf( 1.0 );
142+
// returns 1.0
143+
144+
out = stdlib_base_gammasgnf( -2.5 );
145+
// returns -1.0
146+
```
147+
148+
The function accepts the following arguments:
149+
150+
- **x**: `[in] float` input value.
151+
152+
```c
153+
float stdlib_base_gammasgnf( const float x );
154+
```
155+
156+
</section>
157+
158+
<!-- /.usage -->
159+
160+
<!-- C API usage notes. Make sure to keep an empty line after the `section` element and another before the `/section` close. -->
161+
162+
<section class="notes">
163+
164+
</section>
165+
166+
<!-- /.notes -->
167+
168+
<!-- C API usage examples. -->
169+
170+
<section class="examples">
171+
172+
### Examples
173+
174+
```c
175+
#include "stdlib/math/base/special/gammasgnf.h"
176+
#include <stdlib.h>
177+
#include <stdio.h>
178+
179+
int main( void ) {
180+
float x;
181+
float v;
182+
int i;
183+
184+
for ( i = 0; i < 100; i++ ) {
185+
x = ( (float)rand() / (float)RAND_MAX ) * 100.0f;
186+
v = stdlib_base_gammasgnf( x );
187+
printf( "gammasgnf%f = %f\n", x, v );
188+
}
189+
}
190+
```
191+
192+
</section>
193+
194+
<!-- /.examples -->
195+
196+
</section>
197+
198+
<!-- /.c -->
199+
200+
<!-- Section for related `stdlib` packages. Do not manually edit this section, as it is automatically populated. -->
201+
202+
<section class="related">
203+
204+
</section>
205+
206+
<!-- /.related -->
207+
208+
<!-- Section for all links. Make sure to keep an empty line after the `section` element and another before the `/section` close. -->
209+
210+
<section class="links">
211+
212+
[@stdlib/math/base/special/gamma]: https://github.com/stdlib-js/stdlib/tree/develop/lib/node_modules/%40stdlib/math/base/special/gamma
213+
214+
[@stdlib/math/base/special/gammaln]: https://github.com/stdlib-js/stdlib/tree/develop/lib/node_modules/%40stdlib/math/base/special/gammaln
215+
216+
<!-- <related-links> -->
217+
218+
<!-- </related-links> -->
219+
220+
</section>
221+
222+
<!-- /.links -->
Lines changed: 51 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,51 @@
1+
/**
2+
* @license Apache-2.0
3+
*
4+
* Copyright (c) 2022 The Stdlib Authors.
5+
*
6+
* Licensed under the Apache License, Version 2.0 (the "License");
7+
* you may not use this file except in compliance with the License.
8+
* You may obtain a copy of the License at
9+
*
10+
* http://www.apache.org/licenses/LICENSE-2.0
11+
*
12+
* Unless required by applicable law or agreed to in writing, software
13+
* distributed under the License is distributed on an "AS IS" BASIS,
14+
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
15+
* See the License for the specific language governing permissions and
16+
* limitations under the License.
17+
*/
18+
19+
'use strict';
20+
21+
// MODULES //
22+
23+
var bench = require( '@stdlib/bench' );
24+
var randu = require( '@stdlib/random/base/randu' );
25+
var isnan = require( '@stdlib/math/base/assert/is-nan' );
26+
var pkg = require( './../package.json' ).name;
27+
var gammasgnf = require( './../lib' );
28+
29+
30+
// MAIN //
31+
32+
bench( pkg, function benchmark( b ) {
33+
var x;
34+
var y;
35+
var i;
36+
37+
b.tic();
38+
for ( i = 0; i < b.iterations; i++ ) {
39+
x = ( randu()*171.0 ) - 0.0;
40+
y = gammasgnf( x );
41+
if ( isnan( y ) ) {
42+
b.fail( 'should not return NaN' );
43+
}
44+
}
45+
b.toc();
46+
if ( isnan( y ) ) {
47+
b.fail( 'should not return NaN' );
48+
}
49+
b.pass( 'benchmark finished' );
50+
b.end();
51+
});
Lines changed: 60 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,60 @@
1+
/**
2+
* @license Apache-2.0
3+
*
4+
* Copyright (c) 2024 The Stdlib Authors.
5+
*
6+
* Licensed under the Apache License, Version 2.0 (the "License");
7+
* you may not use this file except in compliance with the License.
8+
* You may obtain a copy of the License at
9+
*
10+
* http://www.apache.org/licenses/LICENSE-2.0
11+
*
12+
* Unless required by applicable law or agreed to in writing, software
13+
* distributed under the License is distributed on an "AS IS" BASIS,
14+
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
15+
* See the License for the specific language governing permissions and
16+
* limitations under the License.
17+
*/
18+
19+
'use strict';
20+
21+
// MODULES //
22+
23+
var resolve = require( 'path' ).resolve;
24+
var bench = require( '@stdlib/bench' );
25+
var randu = require( '@stdlib/random/base/randu' );
26+
var isnan = require( '@stdlib/math/base/assert/is-nan' );
27+
var tryRequire = require( '@stdlib/utils/try-require' );
28+
var pkg = require( './../package.json' ).name;
29+
30+
31+
// VARIABLES //
32+
33+
var gammasgnf = tryRequire( resolve( __dirname, './../lib/native.js' ) );
34+
var opts = {
35+
'skip': ( gammasgnf instanceof Error )
36+
};
37+
38+
39+
// MAIN //
40+
41+
bench( pkg+'::native', opts, function benchmark( b ) {
42+
var x;
43+
var y;
44+
var i;
45+
46+
b.tic();
47+
for ( i = 0; i < b.iterations; i++ ) {
48+
x = ( randu() * 171.0 ) - 0.0;
49+
y = gammasgnf( x );
50+
if ( isnan( y ) ) {
51+
b.fail( 'should not return NaN' );
52+
}
53+
}
54+
b.toc();
55+
if ( isnan( y ) ) {
56+
b.fail( 'should not return NaN' );
57+
}
58+
b.pass( 'benchmark finished' );
59+
b.end();
60+
});

0 commit comments

Comments
 (0)