Skip to content

Commit 02da8f1

Browse files
steff456kgryte
andauthored
feat: add C implementation to math/base/special/cidentity
PR-URL: stdlib-js#976 Co-authored-by: Athan Reines <kgryte@gmail.com> Reviewed-by: Athan Reines <kgryte@gmail.com> Private-ref: stdlib-js/todo#1454
1 parent 7fc8042 commit 02da8f1

File tree

16 files changed

+722
-69
lines changed

16 files changed

+722
-69
lines changed

lib/node_modules/@stdlib/math/base/special/cidentity/README.md

Lines changed: 33 additions & 13 deletions
Original file line numberDiff line numberDiff line change
@@ -18,7 +18,7 @@ limitations under the License.
1818
1919
-->
2020

21-
# Identity Function
21+
# cidentity
2222

2323
> Evaluate the [identity function][identity-function] of a double-precision [complex][@stdlib/complex/float64] floating-point number.
2424
@@ -132,18 +132,26 @@ for ( i = 0; i < 100; i++ ) {
132132
Evaluates the identity function for a double-precision complex floating-point number.
133133

134134
```c
135-
#include <complex.h>
135+
#include "stdlib/complex/float64.h"
136+
#include "stdlib/complex/real.h"
137+
#include "stdlib/complex/imag.h"
136138

137-
double complex y = stdlib_base_cidentity( 2.0+2.0*I );
138-
// returns 2.0+2.0*I
139+
stdlib_complex128_t z = stdlib_complex128( 2.5, -1.5 );
140+
stdlib_complex128_t out = stdlib_base_cidentity( z );
141+
142+
double re = stdlib_real( out );
143+
// returns 2.5
144+
145+
double im = stdlib_imag( out );
146+
// returns -1.5
139147
```
140148

141149
The function accepts the following arguments:
142150

143-
- **z**: `[in] double complex` input value.
151+
- **z**: `[in] stdlib_complex128_t` input value.
144152

145153
```c
146-
double complex stdlib_base_cidentity( const double complex z );
154+
stdlib_complex128_t stdlib_base_cidentity( const stdlib_complex128_t z );
147155
```
148156
149157
</section>
@@ -166,19 +174,31 @@ double complex stdlib_base_cidentity( const double complex z );
166174
167175
```c
168176
#include "stdlib/math/base/special/cidentity.h"
177+
#include "stdlib/complex/float64.h"
178+
#include "stdlib/complex/reim.h"
169179
#include <stdio.h>
170-
#include <complex.h>
171-
172-
int main( void ) {
173-
const double complex x[] = { 3.14+1.0*I, -3.14-1.0*I, 0.0+0.0*I, 0.0/0.0+0.0/0.0*I };
174180
175-
double complex v;
176-
double complex y;
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;
177195
int i;
178196
for ( i = 0; i < 4; i++ ) {
179197
v = x[ i ];
180198
y = stdlib_base_cidentity( v );
181-
printf( "f(%lf + %lf) = %lf + %lf\n", creal( v ), cimag( v ), creal( y ), cimag( y ) );
199+
stdlib_reim( v, &re1, &im1 );
200+
stdlib_reim( y, &re2, &im2 );
201+
printf( "cidentity(%lf + %lfi) = %lf + %lfi\n", re1, im1, re2, im2 );
182202
}
183203
}
184204
```
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' );
28+
var real = require( '@stdlib/complex/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+
});

lib/node_modules/@stdlib/math/base/special/cidentity/benchmark/c/Makefile renamed to lib/node_modules/@stdlib/math/base/special/cidentity/benchmark/c/native/Makefile

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,7 +1,7 @@
11
#/
22
# @license Apache-2.0
33
#
4-
# Copyright (c) 2021 The Stdlib Authors.
4+
# Copyright (c) 2023 The Stdlib Authors.
55
#
66
# Licensed under the Apache License, Version 2.0 (the "License");
77
# you may not use this file except in compliance with the License.

lib/node_modules/@stdlib/math/base/special/cidentity/benchmark/c/benchmark.c renamed to lib/node_modules/@stdlib/math/base/special/cidentity/benchmark/c/native/benchmark.c

Lines changed: 12 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -1,7 +1,7 @@
11
/**
22
* @license Apache-2.0
33
*
4-
* Copyright (c) 2021 The Stdlib Authors.
4+
* Copyright (c) 2023 The Stdlib Authors.
55
*
66
* Licensed under the Apache License, Version 2.0 (the "License");
77
* you may not use this file except in compliance with the License.
@@ -20,7 +20,8 @@
2020
* Benchmark `cidentity`.
2121
*/
2222
#include "stdlib/math/base/special/cidentity.h"
23-
#include <complex.h>
23+
#include "stdlib/complex/float64.h"
24+
#include "stdlib/complex/reim.h"
2425
#include <stdlib.h>
2526
#include <stdio.h>
2627
#include <math.h>
@@ -94,25 +95,29 @@ double rand_double() {
9495
* @return elapsed time in seconds
9596
*/
9697
double benchmark() {
97-
double complex x;
98-
double complex y;
9998
double elapsed;
99+
double re;
100+
double im;
100101
double t;
101102
double v;
102103
int i;
103104

105+
stdlib_complex128_t x;
106+
stdlib_complex128_t y;
107+
104108
t = tic();
105109
for ( i = 0; i < ITERATIONS; i++ ) {
106110
v = ( 1000.0*rand_double() ) - 500.0;
107-
x = v + v*I;
111+
x = stdlib_complex128( v, v );
108112
y = stdlib_base_cidentity( x );
109-
if ( creal( y ) != v ) {
113+
stdlib_reim( y, &re, &im );
114+
if ( re != re ) {
110115
printf( "unexpected result\n" );
111116
break;
112117
}
113118
}
114119
elapsed = tic() - t;
115-
if ( cimag( y ) != v ) {
120+
if ( im != im ) {
116121
printf( "unexpected result\n" );
117122
}
118123
return elapsed;
Lines changed: 170 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,170 @@
1+
# @license Apache-2.0
2+
#
3+
# Copyright (c) 2023 The Stdlib Authors.
4+
#
5+
# Licensed under the Apache License, Version 2.0 (the "License");
6+
# you may not use this file except in compliance with the License.
7+
# You may obtain a copy of the License at
8+
#
9+
# http://www.apache.org/licenses/LICENSE-2.0
10+
#
11+
# Unless required by applicable law or agreed to in writing, software
12+
# distributed under the License is distributed on an "AS IS" BASIS,
13+
# WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
14+
# See the License for the specific language governing permissions and
15+
# limitations under the License.
16+
17+
# A `.gyp` file for building a Node.js native add-on.
18+
#
19+
# [1]: https://gyp.gsrc.io/docs/InputFormatReference.md
20+
# [2]: https://gyp.gsrc.io/docs/UserDocumentation.md
21+
{
22+
# List of files to include in this file:
23+
'includes': [
24+
'./include.gypi',
25+
],
26+
27+
# Define variables to be used throughout the configuration for all targets:
28+
'variables': {
29+
# Target name should match the add-on export name:
30+
'addon_target_name%': 'addon',
31+
32+
# Set variables based on the host OS:
33+
'conditions': [
34+
[
35+
'OS=="win"',
36+
{
37+
# Define the object file suffix:
38+
'obj': 'obj',
39+
},
40+
{
41+
# Define the object file suffix:
42+
'obj': 'o',
43+
}
44+
], # end condition (OS=="win")
45+
], # end conditions
46+
}, # end variables
47+
48+
# Define compile targets:
49+
'targets': [
50+
51+
# Target to generate an add-on:
52+
{
53+
# The target name should match the add-on export name:
54+
'target_name': '<(addon_target_name)',
55+
56+
# Define dependencies:
57+
'dependencies': [],
58+
59+
# Define directories which contain relevant include headers:
60+
'include_dirs': [
61+
# Local include directory:
62+
'<@(include_dirs)',
63+
],
64+
65+
# List of source files:
66+
'sources': [
67+
'<@(src_files)',
68+
],
69+
70+
# Settings which should be applied when a target's object files are used as linker input:
71+
'link_settings': {
72+
# Define libraries:
73+
'libraries': [
74+
'<@(libraries)',
75+
],
76+
77+
# Define library directories:
78+
'library_dirs': [
79+
'<@(library_dirs)',
80+
],
81+
},
82+
83+
# C/C++ compiler flags:
84+
'cflags': [
85+
# Enable commonly used warning options:
86+
'-Wall',
87+
88+
# Aggressive optimization:
89+
'-O3',
90+
],
91+
92+
# C specific compiler flags:
93+
'cflags_c': [
94+
# Specify the C standard to which a program is expected to conform:
95+
'-std=c99',
96+
],
97+
98+
# C++ specific compiler flags:
99+
'cflags_cpp': [
100+
# Specify the C++ standard to which a program is expected to conform:
101+
'-std=c++11',
102+
],
103+
104+
# Linker flags:
105+
'ldflags': [],
106+
107+
# Apply conditions based on the host OS:
108+
'conditions': [
109+
[
110+
'OS=="mac"',
111+
{
112+
# Linker flags:
113+
'ldflags': [
114+
'-undefined dynamic_lookup',
115+
'-Wl,-no-pie',
116+
'-Wl,-search_paths_first',
117+
],
118+
},
119+
], # end condition (OS=="mac")
120+
[
121+
'OS!="win"',
122+
{
123+
# C/C++ flags:
124+
'cflags': [
125+
# Generate platform-independent code:
126+
'-fPIC',
127+
],
128+
},
129+
], # end condition (OS!="win")
130+
], # end conditions
131+
}, # end target <(addon_target_name)
132+
133+
# Target to copy a generated add-on to a standard location:
134+
{
135+
'target_name': 'copy_addon',
136+
137+
# Declare that the output of this target is not linked:
138+
'type': 'none',
139+
140+
# Define dependencies:
141+
'dependencies': [
142+
# Require that the add-on be generated before building this target:
143+
'<(addon_target_name)',
144+
],
145+
146+
# Define a list of actions:
147+
'actions': [
148+
{
149+
'action_name': 'copy_addon',
150+
'message': 'Copying addon...',
151+
152+
# Explicitly list the inputs in the command-line invocation below:
153+
'inputs': [],
154+
155+
# Declare the expected outputs:
156+
'outputs': [
157+
'<(addon_output_dir)/<(addon_target_name).node',
158+
],
159+
160+
# Define the command-line invocation:
161+
'action': [
162+
'cp',
163+
'<(PRODUCT_DIR)/<(addon_target_name).node',
164+
'<(addon_output_dir)/<(addon_target_name).node',
165+
],
166+
},
167+
], # end actions
168+
}, # end target copy_addon
169+
], # end targets
170+
}

lib/node_modules/@stdlib/math/base/special/cidentity/examples/c/Makefile

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,7 +1,7 @@
11
#/
22
# @license Apache-2.0
33
#
4-
# Copyright (c) 2021 The Stdlib Authors.
4+
# Copyright (c) 2023 The Stdlib Authors.
55
#
66
# Licensed under the Apache License, Version 2.0 (the "License");
77
# you may not use this file except in compliance with the License.

0 commit comments

Comments
 (0)