Skip to content

Commit 75a21c0

Browse files
feat: add C & JS implementation of math/base/special/digammaf
--- type: pre_commit_static_analysis_report description: Results of running static analysis checks when committing changes. report: - task: lint_filenames status: passed - task: lint_editorconfig status: passed - task: lint_markdown status: passed - task: lint_package_json status: passed - task: lint_repl_help status: passed - task: lint_javascript_src status: passed - task: lint_javascript_cli status: na - task: lint_javascript_examples status: passed - task: lint_javascript_tests status: passed - task: lint_javascript_benchmarks status: passed - task: lint_python status: na - task: lint_r status: na - task: lint_c_src status: passed - task: lint_c_examples status: passed - task: lint_c_benchmarks status: passed - task: lint_c_tests_fixtures status: na - task: lint_shell status: na - task: lint_typescript_declarations status: passed - task: lint_typescript_tests status: passed - task: lint_license_headers status: passed ---
1 parent 61e0c07 commit 75a21c0

33 files changed

Lines changed: 2732 additions & 0 deletions
Lines changed: 241 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,241 @@
1+
<!--
2+
3+
@license Apache-2.0
4+
5+
Copyright (c) 2026 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+
# digammaf
22+
23+
> [Digamma][digamma-function] function (single-precision).
24+
25+
<section class="intro">
26+
27+
The [digamma function][digamma-function] `ψ` is the logarithmic derivative of the [gamma function][gamma-function], i.e.
28+
29+
<!-- <equation class="equation" label="eq:digamma_function" align="center" raw="\psi(x) =\frac{d}{dx} \ln{\Gamma(x)}= \frac{\Gamma\,'(x)}{\Gamma(x)}" alt="Digamma function"> -->
30+
31+
```math
32+
\psi(x) =\frac{d}{dx} \ln{\Gamma(x)}= \frac{\Gamma\,'(x)}{\Gamma(x)}
33+
```
34+
35+
<!-- <div class="equation" align="center" data-raw-text="\psi(x) =\frac{d}{dx} \ln{\Gamma(x)}= \frac{\Gamma\,&#39;(x)}{\Gamma(x)}" data-equation="eq:digamma_function">
36+
<img src="https://cdn.jsdelivr.net/gh/stdlib-js/stdlib@bb29798906e119fcb2af99e94b60407a270c9b32/lib/node_modules/@stdlib/math/base/special/digammaf/docs/img/equation_digamma_function.svg" alt="Digamma function">
37+
<br>
38+
</div> -->
39+
40+
<!-- </equation> -->
41+
42+
</section>
43+
44+
<!-- /.intro -->
45+
46+
<section class="usage">
47+
48+
## Usage
49+
50+
```javascript
51+
var digammaf = require( '@stdlib/math/base/special/digammaf' );
52+
```
53+
54+
#### digammaf( x )
55+
56+
Evaluates the [digamma function][digamma-function] for a single-precision floating-point number.
57+
58+
```javascript
59+
var v = digammaf( -2.5 );
60+
// returns ~1.103
61+
62+
v = digammaf( 1.0 );
63+
// returns ~-0.577
64+
65+
v = digammaf( 10.0 );
66+
// returns ~2.252
67+
```
68+
69+
If `x` is `0` or a negative `integer`, the function returns `NaN`.
70+
71+
```javascript
72+
var v = digammaf( 0.0 );
73+
// returns NaN
74+
75+
v = digammaf( -1.0 );
76+
// returns NaN
77+
78+
v = digammaf( -2.0 );
79+
// returns NaN
80+
```
81+
82+
If provided `NaN`, the function returns `NaN`.
83+
84+
```javascript
85+
var v = digammaf( NaN );
86+
// returns NaN
87+
```
88+
89+
</section>
90+
91+
<!-- /.usage -->
92+
93+
<section class="examples">
94+
95+
## Examples
96+
97+
<!-- eslint no-undef: "error" -->
98+
99+
```javascript
100+
var uniform = require( '@stdlib/random/array/uniform' );
101+
var logEachMap = require( '@stdlib/console/log-each-map' );
102+
var digammaf = require( '@stdlib/math/base/special/digammaf' );
103+
104+
var opts = {
105+
'dtype': 'float32'
106+
};
107+
var x = uniform( 100, -5.0, 5.0, opts );
108+
109+
logEachMap( 'x: %0.4f, f(x): %0.4f', x, digammaf );
110+
```
111+
112+
</section>
113+
114+
<!-- /.examples -->
115+
116+
<!-- C interface documentation. -->
117+
118+
* * *
119+
120+
<section class="c">
121+
122+
## C APIs
123+
124+
<!-- Section to include introductory text. Make sure to keep an empty line after the intro `section` element and another before the `/section` close. -->
125+
126+
<section class="intro">
127+
128+
</section>
129+
130+
<!-- /.intro -->
131+
132+
<!-- C usage documentation. -->
133+
134+
<section class="usage">
135+
136+
### Usage
137+
138+
```c
139+
#include "stdlib/math/base/special/digammaf.h"
140+
```
141+
142+
#### stdlib_base_digammaf( x )
143+
144+
Evaluates the [digamma function][digamma-function] for a single-precision floating-point number.
145+
146+
```c
147+
float out = stdlib_base_digammaf( 1.0f );
148+
// returns ~-0.577
149+
150+
out = stdlib_base_digammaf( 2.0f );
151+
// returns ~0.423
152+
```
153+
154+
The function accepts the following arguments:
155+
156+
- **x**: `[in] float` input value.
157+
158+
```c
159+
float stdlib_base_digammaf( const float x );
160+
```
161+
162+
</section>
163+
164+
<!-- /.usage -->
165+
166+
<!-- C API usage notes. Make sure to keep an empty line after the `section` element and another before the `/section` close. -->
167+
168+
<section class="notes">
169+
170+
</section>
171+
172+
<!-- /.notes -->
173+
174+
<!-- C API usage examples. -->
175+
176+
<section class="examples">
177+
178+
### Examples
179+
180+
```c
181+
#include "stdlib/math/base/special/digammaf.h"
182+
#include <stdlib.h>
183+
#include <stdio.h>
184+
185+
int main( void ) {
186+
const float x[] = { 1.0f, 2.0f, 3.0f, 4.0f, 5.0f };
187+
188+
float y;
189+
int i;
190+
for ( i = 0; i < 5; i++ ) {
191+
y = stdlib_base_digammaf( x[ i ] );
192+
printf( "digamma(%f) = %f\n", x[ i ], y );
193+
}
194+
}
195+
```
196+
197+
</section>
198+
199+
<!-- /.examples -->
200+
201+
</section>
202+
203+
<!-- /.c -->
204+
205+
<!-- Section for related `stdlib` packages. Do not manually edit this section, as it is automatically populated. -->
206+
207+
<section class="related">
208+
209+
* * *
210+
211+
## See Also
212+
213+
- <span class="package-name">[`@stdlib/math/base/special/digamma`][@stdlib/math/base/special/digamma]</span><span class="delimiter">: </span><span class="description">digamma function.</span>
214+
- <span class="package-name">[`@stdlib/math/base/special/gamma`][@stdlib/math/base/special/gamma]</span><span class="delimiter">: </span><span class="description">gamma function.</span>
215+
- <span class="package-name">[`@stdlib/math/base/special/trigamma`][@stdlib/math/base/special/trigamma]</span><span class="delimiter">: </span><span class="description">trigamma function.</span>
216+
217+
</section>
218+
219+
<!-- /.related -->
220+
221+
<!-- Section for all links. Make sure to keep an empty line after the `section` element and another before the `/section` close. -->
222+
223+
<section class="links">
224+
225+
[digamma-function]: https://en.wikipedia.org/wiki/Digamma_function
226+
227+
[gamma-function]: https://en.wikipedia.org/wiki/Gamma_function
228+
229+
<!-- <related-links> -->
230+
231+
[@stdlib/math/base/special/digamma]: https://github.com/stdlib-js/stdlib/tree/develop/lib/node_modules/%40stdlib/math/base/special/digamma
232+
233+
[@stdlib/math/base/special/gamma]: https://github.com/stdlib-js/stdlib/tree/develop/lib/node_modules/%40stdlib/math/base/special/gamma
234+
235+
[@stdlib/math/base/special/trigamma]: https://github.com/stdlib-js/stdlib/tree/develop/lib/node_modules/%40stdlib/math/base/special/trigamma
236+
237+
<!-- </related-links> -->
238+
239+
</section>
240+
241+
<!-- /.links -->
Lines changed: 52 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,52 @@
1+
/**
2+
* @license Apache-2.0
3+
*
4+
* Copyright (c) 2026 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 isnanf = require( '@stdlib/math/base/assert/is-nanf' );
26+
var EPS = require( '@stdlib/constants/float32/eps' );
27+
var pkg = require( './../package.json' ).name;
28+
var digammaf = require( './../lib' );
29+
30+
31+
// MAIN //
32+
33+
bench(pkg, function benchmark(b) {
34+
var x;
35+
var y;
36+
var i;
37+
38+
b.tic();
39+
for (i = 0; i < b.iterations; i++) {
40+
x = (randu() * 1000.0) + EPS;
41+
y = digammaf(x);
42+
if (isnanf(y)) {
43+
b.fail('should not return NaN');
44+
}
45+
}
46+
b.toc();
47+
if (isnanf(y)) {
48+
b.fail('should not return NaN');
49+
}
50+
b.pass('benchmark finished');
51+
b.end();
52+
});
Lines changed: 61 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,61 @@
1+
/**
2+
* @license Apache-2.0
3+
*
4+
* Copyright (c) 2026 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 isnanf = require( '@stdlib/math/base/assert/is-nanf' );
27+
var EPS = require( '@stdlib/constants/float32/eps' );
28+
var tryRequire = require( '@stdlib/utils/try-require' );
29+
var pkg = require( './../package.json' ).name;
30+
31+
32+
// VARIABLES //
33+
34+
var digammaf = tryRequire(resolve(__dirname, './../lib/native.js'));
35+
var opts = {
36+
'skip': (digammaf instanceof Error)
37+
};
38+
39+
40+
// MAIN //
41+
42+
bench(pkg + '::native', opts, function benchmark(b) {
43+
var x;
44+
var y;
45+
var i;
46+
47+
b.tic();
48+
for (i = 0; i < b.iterations; i++) {
49+
x = (randu() * 1000.0) + EPS;
50+
y = digammaf(x);
51+
if (isnanf(y)) {
52+
b.fail('should not return NaN');
53+
}
54+
}
55+
b.toc();
56+
if (isnanf(y)) {
57+
b.fail('should not return NaN');
58+
}
59+
b.pass('benchmark finished');
60+
b.end();
61+
});

0 commit comments

Comments
 (0)