Skip to content

Commit 404a4ed

Browse files
gururaj1512kgryte
authored andcommitted
feat: add complex/float64/base/identity
Ref: #2261 --- 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: missing_dependencies - task: lint_c_examples status: missing_dependencies - task: lint_c_benchmarks status: missing_dependencies - 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 8f4f7b9 commit 404a4ed

File tree

25 files changed

+1964
-0
lines changed

25 files changed

+1964
-0
lines changed
Lines changed: 247 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,247 @@
1+
<!--
2+
3+
@license Apache-2.0
4+
5+
Copyright (c) 2021 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+
# cidentity
22+
23+
> Evaluate the [identity function][identity-function] of a double-precision [complex][@stdlib/complex/float64/ctor] floating-point number.
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(z) = z" alt="Identity function"> -->
30+
31+
```math
32+
f(z) = z
33+
```
34+
35+
<!-- <div class="equation" align="center" data-raw-text="f(z) = z" data-equation="eq:identity_function">
36+
<img src="https://cdn.jsdelivr.net/gh/stdlib-js/stdlib@3ac3ef7b03afae265d5c85a664dc22e0a373c0c2/lib/node_modules/@stdlib/complex/float64/base/identity/docs/img/equation_identity_function.svg" alt="Identity function">
37+
<br>
38+
</div> -->
39+
40+
<!-- </equation> -->
41+
42+
for all `z`.
43+
44+
</section>
45+
46+
<!-- /.intro -->
47+
48+
<section class="usage">
49+
50+
## Usage
51+
52+
```javascript
53+
var cidentity = require( '@stdlib/complex/float64/base/identity' );
54+
```
55+
56+
#### cidentity( z )
57+
58+
Evaluates the [identity function][identity-function] for a double-precision [complex][@stdlib/complex/float64/ctor] floating-point number.
59+
60+
```javascript
61+
var Complex128 = require( '@stdlib/complex/float64/ctor' );
62+
var real = require( '@stdlib/complex/float64/real' );
63+
var imag = require( '@stdlib/complex/float64/imag' );
64+
65+
var v = cidentity( new Complex128( -1.0, 2.0 ) );
66+
// returns <Complex128>
67+
68+
var re = real( v );
69+
// returns -1.0
70+
71+
var im = imag( v );
72+
// returns 2.0
73+
```
74+
75+
</section>
76+
77+
<!-- /.usage -->
78+
79+
<section class="examples">
80+
81+
## Examples
82+
83+
<!-- eslint-disable max-len -->
84+
85+
<!-- eslint no-undef: "error" -->
86+
87+
```javascript
88+
var discreteUniform = require( '@stdlib/random/base/discrete-uniform' );
89+
var Complex128 = require( '@stdlib/complex/float64/ctor' );
90+
var cidentity = require( '@stdlib/complex/float64/base/identity' );
91+
92+
var z;
93+
var i;
94+
for ( i = 0; i < 100; i++ ) {
95+
z = new Complex128( discreteUniform( -50, 50 ), discreteUniform( -50, 50 ) );
96+
console.log( 'identity(%s) = %s', z, cidentity( z ) );
97+
}
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/complex/float64/base/identity.h"
128+
```
129+
130+
#### stdlib_base_complex128_identity( z )
131+
132+
Evaluates the identity function for a double-precision complex floating-point number.
133+
134+
```c
135+
#include "stdlib/complex/float64/ctor.h"
136+
#include "stdlib/complex/float64/real.h"
137+
#include "stdlib/complex/float64/imag.h"
138+
139+
stdlib_complex128_t z = stdlib_complex128( 2.5, -1.5 );
140+
stdlib_complex128_t out = stdlib_base_complex128_identity( z );
141+
142+
double re = stdlib_complex128_real( out );
143+
// returns 2.5
144+
145+
double im = stdlib_complex128_imag( out );
146+
// returns -1.5
147+
```
148+
149+
The function accepts the following arguments:
150+
151+
- **z**: `[in] stdlib_complex128_t` input value.
152+
153+
```c
154+
stdlib_complex128_t stdlib_base_complex128_identity( const stdlib_complex128_t z );
155+
```
156+
157+
</section>
158+
159+
<!-- /.usage -->
160+
161+
<!-- C API usage notes. Make sure to keep an empty line after the `section` element and another before the `/section` close. -->
162+
163+
<section class="notes">
164+
165+
</section>
166+
167+
<!-- /.notes -->
168+
169+
<!-- C API usage examples. -->
170+
171+
<section class="examples">
172+
173+
### Examples
174+
175+
```c
176+
#include "stdlib/complex/float64/base/identity.h"
177+
#include "stdlib/complex/float64/ctor.h"
178+
#include "stdlib/complex/float64/reim.h"
179+
#include <stdio.h>
180+
181+
int main() {
182+
const stdlib_complex128_t x[] = {
183+
stdlib_complex128( 3.14, 1.5 ),
184+
stdlib_complex128( -3.14, -1.5 ),
185+
stdlib_complex128( 0.0, 0.0 ),
186+
stdlib_complex128( 0.0/0.0, 0.0/0.0 )
187+
};
188+
189+
stdlib_complex128_t v;
190+
stdlib_complex128_t y;
191+
double re1;
192+
double im1;
193+
double re2;
194+
double im2;
195+
int i;
196+
for ( i = 0; i < 4; i++ ) {
197+
v = x[ i ];
198+
y = stdlib_base_complex128_identity( v );
199+
stdlib_complex128_reim( v, &re1, &im1 );
200+
stdlib_complex128_reim( y, &re2, &im2 );
201+
printf( "cidentity(%lf + %lfi) = %lf + %lfi\n", re1, im1, re2, im2 );
202+
}
203+
}
204+
```
205+
206+
</section>
207+
208+
<!-- /.examples -->
209+
210+
</section>
211+
212+
<!-- /.c -->
213+
214+
<!-- Section for related `stdlib` packages. Do not manually edit this section, as it is automatically populated. -->
215+
216+
<section class="related">
217+
218+
* * *
219+
220+
## See Also
221+
222+
- <span class="package-name">[`@stdlib/math/base/special/cidentityf`][@stdlib/math/base/special/cidentityf]</span><span class="delimiter">: </span><span class="description">evaluate the identity function for a single-precision complex floating-point number.</span>
223+
- <span class="package-name">[`@stdlib/number/float64/base/identity`][@stdlib/number/float64/base/identity]</span><span class="delimiter">: </span><span class="description">evaluate the identity function for a double-precision floating-point number.</span>
224+
225+
</section>
226+
227+
<!-- /.related -->
228+
229+
<!-- Section for all links. Make sure to keep an empty line after the `section` element and another before the `/section` close. -->
230+
231+
<section class="links">
232+
233+
[identity-function]: https://en.wikipedia.org/wiki/Identity_function
234+
235+
[@stdlib/complex/float64/ctor]: https://github.com/stdlib-js/stdlib/tree/develop/lib/node_modules/%40stdlib/complex/float64/ctor
236+
237+
<!-- <related-links> -->
238+
239+
[@stdlib/math/base/special/cidentityf]: https://github.com/stdlib-js/stdlib/tree/develop/lib/node_modules/%40stdlib/math/base/special/cidentityf
240+
241+
[@stdlib/number/float64/base/identity]: https://github.com/stdlib-js/stdlib/tree/develop/lib/node_modules/%40stdlib/number/float64/base/identity
242+
243+
<!-- </related-links> -->
244+
245+
</section>
246+
247+
<!-- /.links -->
Lines changed: 58 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,58 @@
1+
/**
2+
* @license Apache-2.0
3+
*
4+
* Copyright (c) 2021 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/base/uniform' );
25+
var isnan = require( '@stdlib/math/base/assert/is-nan' );
26+
var Complex128 = require( '@stdlib/complex/float64/ctor' );
27+
var real = require( '@stdlib/complex/float64/real' );
28+
var imag = require( '@stdlib/complex/float64/imag' );
29+
var pkg = require( './../package.json' ).name;
30+
var cidentity = require( './../lib' );
31+
32+
33+
// MAIN //
34+
35+
bench( pkg, function benchmark( b ) {
36+
var values;
37+
var y;
38+
var i;
39+
40+
values = [
41+
new Complex128( uniform( -500.0, 500.0 ), uniform( -500.0, 500.0 ) ),
42+
new Complex128( uniform( -500.0, 500.0 ), uniform( -500.0, 500.0 ) )
43+
];
44+
45+
b.tic();
46+
for ( i = 0; i < b.iterations; i++ ) {
47+
y = cidentity( values[ i%values.length ] );
48+
if ( isnan( real( y ) ) ) {
49+
b.fail( 'should not return NaN' );
50+
}
51+
}
52+
b.toc();
53+
if ( isnan( imag( y ) ) ) {
54+
b.fail( 'should not return NaN' );
55+
}
56+
b.pass( 'benchmark finished' );
57+
b.end();
58+
});
Lines changed: 66 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,66 @@
1+
/**
2+
* @license Apache-2.0
3+
*
4+
* Copyright (c) 2023 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/base/uniform' );
26+
var isnan = require( '@stdlib/math/base/assert/is-nan' );
27+
var Complex128 = require( '@stdlib/complex/float64/ctor' );
28+
var real = require( '@stdlib/complex/float64/real' );
29+
var tryRequire = require( '@stdlib/utils/try-require' );
30+
var pkg = require( './../package.json' ).name;
31+
32+
33+
// VARIABLES //
34+
35+
var cidentity = tryRequire( resolve( __dirname, './../lib/native.js' ) );
36+
var opts = {
37+
'skip': ( cidentity instanceof Error )
38+
};
39+
40+
41+
// MAIN //
42+
43+
bench( pkg+'::native', opts, function benchmark( b ) {
44+
var values;
45+
var y;
46+
var i;
47+
48+
values = [
49+
new Complex128( uniform( -500.0, 500.0 ), uniform( -500.0, 500.0 ) ),
50+
new Complex128( uniform( -500.0, 500.0 ), uniform( -500.0, 500.0 ) )
51+
];
52+
53+
b.tic();
54+
for ( i = 0; i < b.iterations; i++ ) {
55+
y = cidentity( values[ i%values.length ] );
56+
if ( isnan( real( y ) ) ) {
57+
b.fail( 'should not return NaN' );
58+
}
59+
}
60+
b.toc();
61+
if ( isnan( real( y ) ) ) {
62+
b.fail( 'should not return NaN' );
63+
}
64+
b.pass( 'benchmark finished' );
65+
b.end();
66+
});

0 commit comments

Comments
 (0)