Skip to content

Commit d4383ec

Browse files
authored
feat: add number/uint16/base/identity
PR-URL: #7846 Reviewed-by: Athan Reines <[email protected]>
1 parent b3414ca commit d4383ec

File tree

25 files changed

+1726
-0
lines changed

25 files changed

+1726
-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+
# Identity Function
22+
23+
> Evaluate the [identity function][identity-function] of an unsigned 16-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/uint16/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/uint16/base/identity' );
54+
```
55+
56+
#### identity( x )
57+
58+
Evaluates the [identity function][identity-function] for an unsigned 16-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( 65535 );
71+
// returns 65535
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 logEachMap = require( '@stdlib/console/log-each-map' );
87+
var identity = require( '@stdlib/number/uint16/base/identity' );
88+
89+
var opts = {
90+
'dtype': 'uint16'
91+
};
92+
93+
// Create an array of random values:
94+
var x = discreteUniform( 100, 0, 50, opts );
95+
96+
// Perform element-wise operation:
97+
logEachMap( 'identity(%d) = %d', x, identity );
98+
```
99+
100+
</section>
101+
102+
<!-- /.examples -->
103+
104+
<!-- C interface documentation. -->
105+
106+
* * *
107+
108+
<section class="c">
109+
110+
## C APIs
111+
112+
<!-- Section to include introductory text. Make sure to keep an empty line after the intro `section` element and another before the `/section` close. -->
113+
114+
<section class="intro">
115+
116+
</section>
117+
118+
<!-- /.intro -->
119+
120+
<!-- C usage documentation. -->
121+
122+
<section class="usage">
123+
124+
### Usage
125+
126+
```c
127+
#include "stdlib/number/uint16/base/identity.h"
128+
```
129+
130+
#### stdlib_base_uint16_identity( x )
131+
132+
Evaluates the identity function for an unsigned 16-bit integer.
133+
134+
```c
135+
#include <stdint.h>
136+
137+
uint16_t y = stdlib_base_uint16_identity( 2 );
138+
// returns 2
139+
```
140+
141+
The function accepts the following arguments:
142+
143+
- **x**: `[in] uint16_t` input value.
144+
145+
```c
146+
uint16_t stdlib_base_uint16_identity( const uint16_t x );
147+
```
148+
149+
</section>
150+
151+
<!-- /.usage -->
152+
153+
<!-- C API usage notes. Make sure to keep an empty line after the `section` element and another before the `/section` close. -->
154+
155+
<section class="notes">
156+
157+
</section>
158+
159+
<!-- /.notes -->
160+
161+
<!-- C API usage examples. -->
162+
163+
<section class="examples">
164+
165+
### Examples
166+
167+
```c
168+
#include "stdlib/number/uint16/base/identity.h"
169+
#include <stdint.h>
170+
#include <stdio.h>
171+
172+
int main( void ) {
173+
const uint16_t x[] = { 3, 5, 10, 12 };
174+
175+
uint16_t y;
176+
int i;
177+
for ( i = 0; i < 4; i++ ) {
178+
y = stdlib_base_uint16_identity( x[ i ] );
179+
printf( "f(%u) = %u\n", x[ i ], y );
180+
}
181+
}
182+
```
183+
184+
</section>
185+
186+
<!-- /.examples -->
187+
188+
</section>
189+
190+
<!-- /.c -->
191+
192+
<!-- Section for related `stdlib` packages. Do not manually edit this section, as it is automatically populated. -->
193+
194+
<section class="related">
195+
196+
</section>
197+
198+
<!-- /.related -->
199+
200+
<!-- Section for all links. Make sure to keep an empty line after the `section` element and another before the `/section` close. -->
201+
202+
<section class="links">
203+
204+
[identity-function]: https://en.wikipedia.org/wiki/Identity_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 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': 'uint16'
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+'::native', opts, function benchmark( b ) {
42+
var x;
43+
var y;
44+
var i;
45+
46+
x = discreteUniform( 100, 0, 50, {
47+
'dtype': 'uint16'
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)