Skip to content

Commit 8965325

Browse files
committed
feat: refactor and native addons for complex/float64/conj
--- 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: na - task: lint_package_json status: na - task: lint_repl_help status: na - task: lint_javascript_src status: passed - task: lint_javascript_cli status: na - task: lint_javascript_examples status: na - 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: na - task: lint_c_benchmarks status: na - task: lint_c_tests_fixtures status: na - task: lint_shell status: na - task: lint_typescript_declarations status: na - task: lint_typescript_tests status: na - task: lint_license_headers status: passed ---
1 parent c584077 commit 8965325

File tree

8 files changed

+481
-16
lines changed

8 files changed

+481
-16
lines changed

lib/node_modules/@stdlib/complex/float64/conj/benchmark/benchmark.js

Lines changed: 11 additions & 10 deletions
Original file line numberDiff line numberDiff line change
@@ -21,9 +21,9 @@
2121
// MODULES //
2222

2323
var bench = require( '@stdlib/bench' );
24-
var isComplex128 = require( '@stdlib/assert/is-complex128' );
2524
var Complex128 = require( '@stdlib/complex/float64/ctor' );
26-
var randu = require( '@stdlib/random/base/randu' );
25+
var isComplex128 = require( '@stdlib/assert/is-complex128' );
26+
var uniform = require( '@stdlib/random/base/uniform' );
2727
var pkg = require( './../package.json' ).name;
2828
var conj = require( './../lib' );
2929

@@ -32,25 +32,26 @@ var conj = require( './../lib' );
3232

3333
bench( pkg, function benchmark( b ) {
3434
var values;
35-
var v;
35+
var z;
36+
var y;
3637
var i;
3738

3839
values = [
39-
new Complex128( randu(), randu() ),
40-
new Complex128( randu(), randu() ),
41-
new Complex128( randu(), randu() )
40+
new Complex128( uniform( -500.0, 500.0 ), uniform( -500.0, 500.0 ) ),
41+
new Complex128( uniform( -500.0, 500.0 ), uniform( -500.0, 500.0 ) )
4242
];
4343

4444
b.tic();
4545
for ( i = 0; i < b.iterations; i++ ) {
46-
v = conj( values[ i%values.length ] );
47-
if ( typeof v !== 'object' ) {
46+
z = values[ i%values.length ];
47+
y = conj( z );
48+
if ( typeof y !== 'object' ) {
4849
b.fail( 'should return an object' );
4950
}
5051
}
5152
b.toc();
52-
if ( !isComplex128( v ) ) {
53-
b.fail( 'should return an object' );
53+
if ( !isComplex128( y ) ) {
54+
b.fail( 'should return a Complex128' );
5455
}
5556
b.pass( 'benchmark finished' );
5657
b.end();
Lines changed: 69 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,69 @@
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 imag = require( '@stdlib/complex/float64/imag' );
30+
var tryRequire = require( '@stdlib/utils/try-require' );
31+
var pkg = require( './../package.json' ).name;
32+
33+
34+
// VARIABLES //
35+
36+
var conj = tryRequire( resolve( __dirname, './../lib/native.js' ) );
37+
var opts = {
38+
'skip': ( conj instanceof Error )
39+
};
40+
41+
42+
// MAIN //
43+
44+
bench( pkg+'::native', opts, function benchmark( b ) {
45+
var values;
46+
var out;
47+
var z;
48+
var i;
49+
50+
values = [
51+
new Complex128( uniform( -500.0, 500.0 ), uniform( -500.0, 500.0 ) ),
52+
new Complex128( uniform( -500.0, 500.0 ), uniform( -500.0, 500.0 ) )
53+
];
54+
55+
b.tic();
56+
for ( i = 0; i < b.iterations; i++ ) {
57+
z = values[ i%values.length ];
58+
out = conj( z );
59+
if ( typeof out !== 'object' ) {
60+
b.fail( 'should return an object' );
61+
}
62+
}
63+
b.toc();
64+
if ( isnan( real( out ) ) || isnan( imag( out ) ) ) {
65+
b.fail( 'should not return NaN' );
66+
}
67+
b.pass( 'benchmark finished' );
68+
b.end();
69+
});

lib/node_modules/@stdlib/complex/float64/conj/lib/main.js

Lines changed: 7 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -18,6 +18,12 @@
1818

1919
'use strict';
2020

21+
// MODULES //
22+
23+
var real = require( '@stdlib/complex/float64/real' );
24+
var imag = require( '@stdlib/complex/float64/imag' );
25+
var Complex128 = require( '@stdlib/complex/float64/ctor' );
26+
2127
/**
2228
* Returns the complex conjugate of a double-precision complex floating-point number.
2329
*
@@ -41,7 +47,7 @@
4147
* // returns -3.0
4248
*/
4349
function conj( z ) {
44-
return new z.constructor( z.re, -z.im );
50+
return new Complex128( real( z ), -imag( z ) );
4551
}
4652

4753

Lines changed: 60 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,60 @@
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 Complex128 = require( '@stdlib/complex/float64/ctor' );
24+
var addon = require( './../src/addon.node' );
25+
26+
27+
// MAIN //
28+
29+
/**
30+
* Returns the complex conjugate of a double-precision complex floating-point number.
31+
*
32+
* @private
33+
* @param {Complex128} z - complex number
34+
* @returns {Complex128} complex conjugate
35+
*
36+
* @example
37+
* var Complex128 = require( '@stdlib/complex/float64/ctor' );
38+
* var real = require( '@stdlib/complex/float64/real' );
39+
* var imag = require( '@stdlib/complex/float64/imag' );
40+
*
41+
* var z = new Complex128( 5.0, 3.0 );
42+
*
43+
* var v = conj( z );
44+
* // returns <Complex128>
45+
*
46+
* var re = real( v );
47+
* // returns 5.0
48+
*
49+
* var im = imag( v );
50+
* // returns -3.0
51+
*/
52+
function conj( z ) {
53+
var v = addon( z );
54+
return new Complex128( v.re, v.im );
55+
}
56+
57+
58+
// EXPORTS //
59+
60+
module.exports = conj;
Lines changed: 70 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,70 @@
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+
# VARIABLES #
20+
21+
ifndef VERBOSE
22+
QUIET := @
23+
else
24+
QUIET :=
25+
endif
26+
27+
# Determine the OS ([1][1], [2][2]).
28+
#
29+
# [1]: https://en.wikipedia.org/wiki/Uname#Examples
30+
# [2]: http://stackoverflow.com/a/27776822/2225624
31+
OS ?= $(shell uname)
32+
ifneq (, $(findstring MINGW,$(OS)))
33+
OS := WINNT
34+
else
35+
ifneq (, $(findstring MSYS,$(OS)))
36+
OS := WINNT
37+
else
38+
ifneq (, $(findstring CYGWIN,$(OS)))
39+
OS := WINNT
40+
else
41+
ifneq (, $(findstring Windows_NT,$(OS)))
42+
OS := WINNT
43+
endif
44+
endif
45+
endif
46+
endif
47+
48+
49+
# RULES #
50+
51+
#/
52+
# Removes generated files for building an add-on.
53+
#
54+
# @example
55+
# make clean-addon
56+
#/
57+
clean-addon:
58+
$(QUIET) -rm -f *.o *.node
59+
60+
.PHONY: clean-addon
61+
62+
#/
63+
# Removes generated files.
64+
#
65+
# @example
66+
# make clean
67+
#/
68+
clean: clean-addon
69+
70+
.PHONY: clean
Lines changed: 22 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,22 @@
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+
#include "stdlib/complex/float64/conj.h"
20+
#include "stdlib/math/base/napi/unary.h"
21+
22+
STDLIB_MATH_BASE_NAPI_MODULE_Z_Z( stdlib_complex128_neg )

0 commit comments

Comments
 (0)