Skip to content

Commit 8877111

Browse files
committed
feat: add number/uint32/base/identity
--- 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 2cffc9d commit 8877111

File tree

25 files changed

+1730
-0
lines changed

25 files changed

+1730
-0
lines changed
Lines changed: 214 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,214 @@
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+
# Identity Function
22+
23+
> Evaluate the [identity function][identity-function] of an unsigned 32-bit integer.
24+
25+
<section class="intro">
26+
27+
The [identity-function][identity-function] is defined as
28+
29+
<!-- <equation class="equation" label="eq:identity_function" align="center" raw="f(x) = x" alt="Identity function"> -->
30+
31+
```math
32+
f(x) = x
33+
```
34+
35+
<!-- <div class="equation" align="center" data-raw-text="f(x) = x" data-equation="eq:identity_function">
36+
<img src="https://cdn.jsdelivr.net/gh/stdlib-js/stdlib@ad7afa5d7ec1b1596f8a4828153d8c2e87a90161/lib/node_modules/@stdlib/number/uint32/base/identity/docs/img/equation_identity_function.svg" alt="Identity function">
37+
<br>
38+
</div> -->
39+
40+
<!-- </equation> -->
41+
42+
for all `x`.
43+
44+
</section>
45+
46+
<!-- /.intro -->
47+
48+
<section class="usage">
49+
50+
## Usage
51+
52+
```javascript
53+
var identity = require( '@stdlib/number/uint32/base/identity' );
54+
```
55+
56+
#### identity( x )
57+
58+
Evaluates the [identity function][identity-function] for an unsigned 32-bit integer.
59+
60+
```javascript
61+
var v = identity( 1 );
62+
// returns 1
63+
64+
v = identity( 2 );
65+
// returns 2
66+
67+
v = identity( 0 );
68+
// returns 0
69+
70+
v = identity( 4294967295 );
71+
// returns 4294967295
72+
```
73+
74+
</section>
75+
76+
<!-- /.usage -->
77+
78+
<section class="examples">
79+
80+
## Examples
81+
82+
<!-- eslint no-undef: "error" -->
83+
84+
```javascript
85+
var discreteUniform = require( '@stdlib/random/array/discrete-uniform' );
86+
var identity = require( '@stdlib/number/uint32/base/identity' );
87+
88+
var opts = {
89+
'dtype': 'uint32'
90+
};
91+
92+
var x = discreteUniform( 100, 0, 50, opts );
93+
94+
var v;
95+
var i;
96+
for ( i = 0; i < x.length; i++ ) {
97+
v = identity( x[ i ] );
98+
console.log( 'identity(%d) = %d', x[ i ], v );
99+
}
100+
```
101+
102+
</section>
103+
104+
<!-- /.examples -->
105+
106+
<!-- C interface documentation. -->
107+
108+
* * *
109+
110+
<section class="c">
111+
112+
## C APIs
113+
114+
<!-- Section to include introductory text. Make sure to keep an empty line after the intro `section` element and another before the `/section` close. -->
115+
116+
<section class="intro">
117+
118+
</section>
119+
120+
<!-- /.intro -->
121+
122+
<!-- C usage documentation. -->
123+
124+
<section class="usage">
125+
126+
### Usage
127+
128+
```c
129+
#include "stdlib/number/uint32/base/identity.h"
130+
```
131+
132+
#### stdlib_base_uint32_identity( x )
133+
134+
Evaluates the identity function for an unsigned 32-bit integer.
135+
136+
```c
137+
#include <stdint.h>
138+
139+
uint32_t y = stdlib_base_uint32_identity( 2 );
140+
// returns 2
141+
```
142+
143+
The function accepts the following arguments:
144+
145+
- **x**: `[in] uint32_t` input value.
146+
147+
```c
148+
uint32_t stdlib_base_uint32_identity( const uint32_t x );
149+
```
150+
151+
</section>
152+
153+
<!-- /.usage -->
154+
155+
<!-- C API usage notes. Make sure to keep an empty line after the `section` element and another before the `/section` close. -->
156+
157+
<section class="notes">
158+
159+
</section>
160+
161+
<!-- /.notes -->
162+
163+
<!-- C API usage examples. -->
164+
165+
<section class="examples">
166+
167+
### Examples
168+
169+
```c
170+
#include "stdlib/number/uint32/base/identity.h"
171+
#include <stdint.h>
172+
#include <stdio.h>
173+
174+
int main( void ) {
175+
const uint32_t x[] = { 3, 5, 10, 12 };
176+
177+
uint32_t y;
178+
int i;
179+
for ( i = 0; i < 4; i++ ) {
180+
y = stdlib_base_uint32_identity( x[ i ] );
181+
printf( "f(%u) = %u\n", x[ i ], y );
182+
}
183+
}
184+
```
185+
186+
</section>
187+
188+
<!-- /.examples -->
189+
190+
</section>
191+
192+
<!-- /.c -->
193+
194+
<!-- Section for related `stdlib` packages. Do not manually edit this section, as it is automatically populated. -->
195+
196+
<section class="related">
197+
198+
</section>
199+
200+
<!-- /.related -->
201+
202+
<!-- Section for all links. Make sure to keep an empty line after the `section` element and another before the `/section` close. -->
203+
204+
<section class="links">
205+
206+
[identity-function]: https://en.wikipedia.org/wiki/Identity_function
207+
208+
<!-- <related-links> -->
209+
210+
<!-- </related-links> -->
211+
212+
</section>
213+
214+
<!-- /.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 discreteUniform = require( '@stdlib/random/array/discrete-uniform' );
25+
var isnan = require( '@stdlib/math/base/assert/is-nan' );
26+
var pkg = require( './../package.json' ).name;
27+
var identity = 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 = discreteUniform( 100, 0, 50, {
38+
'dtype': 'uint32'
39+
});
40+
41+
b.tic();
42+
for ( i = 0; i < b.iterations; i++ ) {
43+
y = identity( x[ i%x.length ] );
44+
if ( isnan( y ) ) {
45+
b.fail( 'should not return NaN' );
46+
}
47+
}
48+
b.toc();
49+
if ( isnan( 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 discreteUniform = require( '@stdlib/random/array/discrete-uniform' );
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 identity = tryRequire( resolve( __dirname, './../lib/native.js' ) );
34+
var opts = {
35+
'skip': ( identity instanceof Error )
36+
};
37+
38+
39+
// MAIN //
40+
41+
bench( pkg, opts, function benchmark( b ) {
42+
var x;
43+
var y;
44+
var i;
45+
46+
x = discreteUniform( 100, 0, 50, {
47+
'dtype': 'uint32'
48+
});
49+
50+
b.tic();
51+
for ( i = 0; i < b.iterations; i++ ) {
52+
y = identity( x[ i%x.length ] );
53+
if ( isnan( y ) ) {
54+
b.fail( 'should not return NaN' );
55+
}
56+
}
57+
b.toc();
58+
if ( isnan( y ) ) {
59+
b.fail( 'should not return NaN' );
60+
}
61+
b.pass( 'benchmark finished' );
62+
b.end();
63+
});

0 commit comments

Comments
 (0)