Skip to content

Commit 4fd384d

Browse files
vivekmaurya001stdlib-botgunjjoshianandkaranubc
authored
feat: add math/base/special/logitf
PR-URL: #3367 Ref: #649 Signed-off-by: Gunj Joshi <gunjjoshi8372@gmail.com> Co-authored-by: stdlib-bot <82920195+stdlib-bot@users.noreply.github.com> Co-authored-by: Gunj Joshi <gunjjoshi8372@gmail.com> Co-authored-by: Karan Anand <anandkarancompsci@gmail.com> Reviewed-by: Karan Anand <anandkarancompsci@gmail.com> Reviewed-by: Gunj Joshi <gunjjoshi8372@gmail.com> Reviewed-by: Philipp Burckhardt <pburckhardt@outlook.com>
1 parent 66a381d commit 4fd384d

File tree

30 files changed

+2355
-0
lines changed

30 files changed

+2355
-0
lines changed
Lines changed: 212 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,212 @@
1+
<!--
2+
3+
@license Apache-2.0
4+
5+
Copyright (c) 2025 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+
# logitf
22+
23+
> Compute the [logit][logit] function for a single-precision floating-point number.
24+
25+
<section class="intro">
26+
27+
The [logit][logit] function is defined as the logarithm of the odds `p / (1-p)`; i.e.,
28+
29+
<!-- <equation class="equation" label="eq:logitf_function" align="center" raw="\operatorname{logitf}(p)=\log \left({\frac {p}{1-p}}\right)" alt="Logitf function."> -->
30+
31+
```math
32+
\mathop{\mathrm{logitf}}(p)=\log \left({\frac {p}{1-p}}\right)
33+
```
34+
35+
<!-- </equation> -->
36+
37+
The [logit][logit] function is the inverse of the [standard logistic][standard-logistic] function, sometimes also called the sigmoid function.
38+
39+
</section>
40+
41+
<!-- /.intro -->
42+
43+
<section class="usage">
44+
45+
## Usage
46+
47+
```javascript
48+
var logitf = require( '@stdlib/math/base/special/logitf' );
49+
```
50+
51+
#### logitf( p )
52+
53+
Computes the [logit][logit] function for a single-precision floating-point number.
54+
55+
```javascript
56+
var v = logitf( 0.2 );
57+
// returns ~-1.386
58+
59+
v = logitf( 0.9 );
60+
// returns ~2.197
61+
```
62+
63+
If `p < 0` or `p > 1`, the function returns `NaN`.
64+
65+
```javascript
66+
var v = logitf( 1.3 );
67+
// returns NaN
68+
69+
v = logitf( -0.2 );
70+
// returns NaN
71+
```
72+
73+
</section>
74+
75+
<!-- /.usage -->
76+
77+
<section class="examples">
78+
79+
## Examples
80+
81+
<!-- eslint no-undef: "error" -->
82+
83+
```javascript
84+
var uniform = require( '@stdlib/random/array/uniform' );
85+
var logEachMap = require( '@stdlib/console/log-each-map' );
86+
var logitf = require( '@stdlib/math/base/special/logitf' );
87+
88+
var opts = {
89+
'dtype': 'float32'
90+
};
91+
var p = uniform( 100, 0.0, 1.0, opts );
92+
93+
logEachMap( 'logitf(%0.4f) = %0.4f', p, logitf );
94+
```
95+
96+
</section>
97+
98+
<!-- /.examples -->
99+
100+
<!-- C interface documentation. -->
101+
102+
* * *
103+
104+
<section class="c">
105+
106+
## C APIs
107+
108+
<!-- Section to include introductory text. Make sure to keep an empty line after the intro `section` element and another before the `/section` close. -->
109+
110+
<section class="intro">
111+
112+
</section>
113+
114+
<!-- /.intro -->
115+
116+
<!-- C usage documentation. -->
117+
118+
<section class="usage">
119+
120+
### Usage
121+
122+
```c
123+
#include "stdlib/math/base/special/logitf.h"
124+
```
125+
126+
#### stdlib_base_logitf( p )
127+
128+
Computes the [logit][logit] function for a single-precision floating-point number.
129+
130+
```c
131+
float out = stdlib_base_logitf( 0.2f );
132+
// returns ~-1.386f
133+
134+
out = stdlib_base_logitf( 0.9f );
135+
// returns ~2.197f
136+
```
137+
138+
The function accepts the following arguments:
139+
140+
- **p**: `[in] float` input value.
141+
142+
```c
143+
float stdlib_base_logitf( const float p );
144+
```
145+
146+
</section>
147+
148+
<!-- /.usage -->
149+
150+
<!-- C API usage notes. Make sure to keep an empty line after the `section` element and another before the `/section` close. -->
151+
152+
<section class="notes">
153+
154+
</section>
155+
156+
<!-- /.notes -->
157+
158+
<!-- C API usage examples. -->
159+
160+
<section class="examples">
161+
162+
### Examples
163+
164+
```c
165+
#include "stdlib/math/base/special/logitf.h"
166+
#include <stdlib.h>
167+
#include <stdio.h>
168+
169+
int main( void ) {
170+
float x;
171+
float v;
172+
int i;
173+
174+
for ( i = 0; i < 100; i++ ) {
175+
x = (float)rand() / (float)RAND_MAX;
176+
v = stdlib_base_logitf( x );
177+
printf( "logitf(%f) = %f\n", x, v );
178+
}
179+
}
180+
```
181+
182+
</section>
183+
184+
<!-- /.examples -->
185+
186+
</section>
187+
188+
<!-- /.c -->
189+
190+
<!-- Section for related `stdlib` packages. Do not manually edit this section, as it is automatically populated. -->
191+
192+
<section class="related">
193+
194+
</section>
195+
196+
<!-- /.related -->
197+
198+
<!-- Section for all links. Make sure to keep an empty line after the `section` element and another before the `/section` close. -->
199+
200+
<section class="links">
201+
202+
[logit]: https://en.wikipedia.org/wiki/Logit
203+
204+
[standard-logistic]: https://en.wikipedia.org/wiki/Logistic_function
205+
206+
<!-- <related-links> -->
207+
208+
<!-- </related-links> -->
209+
210+
</section>
211+
212+
<!-- /.links -->
Lines changed: 54 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,54 @@
1+
/**
2+
* @license Apache-2.0
3+
*
4+
* Copyright (c) 2025 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 uniform = require( '@stdlib/random/array/uniform' );
25+
var isnanf = require( '@stdlib/math/base/assert/is-nanf' );
26+
var pkg = require( './../package.json' ).name;
27+
var logitf = require( './../lib' );
28+
29+
30+
// MAIN //
31+
32+
bench( pkg, function benchmark( b ) {
33+
var x;
34+
var y;
35+
var i;
36+
37+
x = uniform( 100, 0.0, 1.0, {
38+
'dtype': 'float32'
39+
});
40+
41+
b.tic();
42+
for ( i = 0; i < b.iterations; i++ ) {
43+
y = logitf( x[ i%x.length ] );
44+
if ( isnanf( y ) ) {
45+
b.fail( 'should not return NaN' );
46+
}
47+
}
48+
b.toc();
49+
if ( isnanf( y ) ) {
50+
b.fail( 'should not return NaN' );
51+
}
52+
b.pass( 'benchmark finished' );
53+
b.end();
54+
});
Lines changed: 63 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,63 @@
1+
/**
2+
* @license Apache-2.0
3+
*
4+
* Copyright (c) 2025 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 uniform = require( '@stdlib/random/array/uniform' );
26+
var isnanf = require( '@stdlib/math/base/assert/is-nanf' );
27+
var tryRequire = require( '@stdlib/utils/try-require' );
28+
var pkg = require( './../package.json' ).name;
29+
30+
31+
// VARIABLES //
32+
33+
var logitf = tryRequire( resolve( __dirname, './../lib/native.js' ) );
34+
var opts = {
35+
'skip': ( logitf 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+
x = uniform( 100, 0.0, 1.0, {
47+
'dtype': 'float32'
48+
});
49+
50+
b.tic();
51+
for ( i = 0; i < b.iterations; i++ ) {
52+
y = logitf( x[ i%x.length ] );
53+
if ( isnanf( y ) ) {
54+
b.fail( 'should not return NaN' );
55+
}
56+
}
57+
b.toc();
58+
if ( isnanf( y ) ) {
59+
b.fail( 'should not return NaN' );
60+
}
61+
b.pass( 'benchmark finished' );
62+
b.end();
63+
});

0 commit comments

Comments
 (0)