Skip to content

Navigation Menu

Sign in
Appearance settings

Search code, repositories, users, issues, pull requests...

Provide feedback

We read every piece of feedback, and take your input very seriously.

Saved searches

Use saved searches to filter your results more quickly

Sign up
Appearance settings

Commit a61bc08

Browse files
feat: add math/base/special/sincf
PR-URL: #7741 Ref: #649 Reviewed-by: Philipp Burckhardt <pburckhardt@outlook.com>
1 parent c8567dc commit a61bc08

35 files changed

+2166
-0
lines changed
Lines changed: 201 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,201 @@
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+
# sincf
22+
23+
> Compute the [cardinal sine][sinc] of a single-precision floating-point number (in radians).
24+
25+
<section class="intro">
26+
27+
The normalized [cardinal sine][sinc] function is defined as
28+
29+
<!-- <equation class="equation" label="eq:sinc_function" align="center" raw="\operatorname{sinc}(x) := \begin{cases} \frac {\sin(\pi x)}{\pi x} & \textrm{if}\ x \neq 0 \\ 1 & \textrm{if}\ x = 0 \end{cases}" alt="Sinc function"> -->
30+
31+
```math
32+
\mathop{\mathrm{sinc}}(x) := \begin{cases} \frac {\sin(\pi x)}{\pi x} & \textrm{if}\ x \neq 0 \\ 1 & \textrm{if}\ x = 0 \end{cases}
33+
```
34+
35+
<!-- </equation> -->
36+
37+
for any real number `x`.
38+
39+
</section>
40+
41+
<!-- /.intro -->
42+
43+
<section class="usage">
44+
45+
## Usage
46+
47+
```javascript
48+
var sincf = require( '@stdlib/math/base/special/sincf' );
49+
```
50+
51+
#### sincf( x )
52+
53+
Computes the normalized [cardinal sine][sinc] of a single-precision floating-point number (in radians).
54+
55+
```javascript
56+
var v = sincf( 0.5 );
57+
// returns ~0.637
58+
59+
v = sincf( -1.2 );
60+
// returns ~-0.156
61+
62+
v = sincf( 0.0 );
63+
// returns 1.0
64+
65+
v = sincf( NaN );
66+
// returns NaN
67+
```
68+
69+
</section>
70+
71+
<!-- /.usage -->
72+
73+
<section class="examples">
74+
75+
## Examples
76+
77+
<!-- eslint no-undef: "error" -->
78+
79+
```javascript
80+
var uniform = require( '@stdlib/random/array/uniform' );
81+
var logEachMap = require( '@stdlib/console/log-each-map' );
82+
var sincf = require( '@stdlib/math/base/special/sincf' );
83+
84+
var opts = {
85+
'dtype': 'float32'
86+
};
87+
var x = uniform( 100, -5.0, 5.0, opts );
88+
89+
logEachMap( 'sincf( %0.4f ) = %0.4f', x, sincf );
90+
```
91+
92+
</section>
93+
94+
<!-- /.examples -->
95+
96+
<!-- C interface documentation. -->
97+
98+
* * *
99+
100+
<section class="c">
101+
102+
## C APIs
103+
104+
<!-- Section to include introductory text. Make sure to keep an empty line after the intro `section` element and another before the `/section` close. -->
105+
106+
<section class="intro">
107+
108+
</section>
109+
110+
<!-- /.intro -->
111+
112+
<!-- C usage documentation. -->
113+
114+
<section class="usage">
115+
116+
### Usage
117+
118+
```c
119+
#include "stdlib/math/base/special/sincf.h"
120+
```
121+
122+
#### stdlib_base_sincf( x )
123+
124+
Computes the normalized [cardinal sine][sinc] of a single-precision floating-point number (in radians).
125+
126+
```c
127+
float y = stdlib_base_sincf( 0.5f );
128+
// returns ~0.637f
129+
```
130+
131+
The function accepts the following arguments:
132+
133+
- **x**: `[in] float` input value.
134+
135+
```c
136+
float stdlib_base_sincf( const float x );
137+
```
138+
139+
</section>
140+
141+
<!-- /.usage -->
142+
143+
<!-- C API usage notes. Make sure to keep an empty line after the `section` element and another before the `/section` close. -->
144+
145+
<section class="notes">
146+
147+
</section>
148+
149+
<!-- /.notes -->
150+
151+
<!-- C API usage examples. -->
152+
153+
<section class="examples">
154+
155+
### Examples
156+
157+
```c
158+
#include "stdlib/math/base/special/sincf.h"
159+
#include <stdio.h>
160+
161+
int main( void ) {
162+
const float x[] = { 0.0f, 0.523f, 0.785f, 1.047f, 3.14f };
163+
164+
float y;
165+
int i;
166+
for ( i = 0; i < 5; i++ ) {
167+
y = stdlib_base_sincf( x[ i ] );
168+
printf( "sincf(%f) = %f\n", x[ i ], y );
169+
}
170+
}
171+
```
172+
173+
</section>
174+
175+
<!-- /.examples -->
176+
177+
</section>
178+
179+
<!-- /.c -->
180+
181+
<!-- Section for related `stdlib` packages. Do not manually edit this section, as it is automatically populated. -->
182+
183+
<section class="related">
184+
185+
</section>
186+
187+
<!-- /.related -->
188+
189+
<!-- Section for all links. Make sure to keep an empty line after the `section` element and another before the `/section` close. -->
190+
191+
<section class="links">
192+
193+
[sinc]: https://en.wikipedia.org/wiki/Sinc_function
194+
195+
<!-- <related-links> -->
196+
197+
<!-- </related-links> -->
198+
199+
</section>
200+
201+
<!-- /.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 sincf = 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, -10.0, 10.0, {
38+
'dtype': 'float32'
39+
});
40+
41+
b.tic();
42+
for ( i = 0; i < b.iterations; i++ ) {
43+
y = sincf( 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 sincf = tryRequire( resolve( __dirname, './../lib/native.js' ) );
34+
var opts = {
35+
'skip': ( sincf 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, -10.0, 10.0, {
47+
'dtype': 'float32'
48+
});
49+
50+
b.tic();
51+
for ( i = 0; i < b.iterations; i++ ) {
52+
y = sincf( 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)

AltStyle によって変換されたページ (->オリジナル) /