Skip to content

Commit 9e9d892

Browse files
committed
Add support for providing an output array
1 parent 441c865 commit 9e9d892

File tree

7 files changed

+211
-8
lines changed

7 files changed

+211
-8
lines changed

lib/node_modules/@stdlib/math/base/complex/round/README.md

+13-1
Original file line numberDiff line numberDiff line change
@@ -10,7 +10,7 @@
1010
var cround = require( '@stdlib/math/base/complex/round' );
1111
```
1212

13-
#### cround( re, im )
13+
#### cround( \[out,] re, im )
1414

1515
Rounds a `complex` number comprised of a **real** component `re` and an **imaginary** component `im` to the nearest integer.
1616

@@ -28,6 +28,18 @@ v = cround( NaN, NaN );
2828
// returns [ NaN, NaN ]
2929
```
3030

31+
By default, the function returns real and imaginary components as a two-element `array`. To avoid unnecessary memory allocation, the function supports providing an output (destination) object.
32+
33+
```javascript
34+
var out = new Float64Array( 2 );
35+
36+
var v = cround( out, -4.2, 5.5 );
37+
// returns <Float64Array>[ -4.0, 6.0 ]
38+
39+
var bool = ( v === out );
40+
// returns true
41+
```
42+
3143
</section>
3244

3345
<!-- /.usage -->

lib/node_modules/@stdlib/math/base/complex/round/benchmark/benchmark.js

+52
Original file line numberDiff line numberDiff line change
@@ -35,6 +35,32 @@ bench( pkg, function benchmark( b ) {
3535
b.end();
3636
});
3737

38+
bench( pkg+'::memory_reuse', function benchmark( b ) {
39+
var out;
40+
var re;
41+
var im;
42+
var y;
43+
var i;
44+
45+
out = new Array( 2 );
46+
47+
b.tic();
48+
for ( i = 0; i < b.iterations; i++ ) {
49+
re = ( randu()*1000.0 ) - 500.0;
50+
im = ( randu()*1000.0 ) - 500.0;
51+
y = cround( out, re, im );
52+
if ( y.length === 0 ) {
53+
b.fail( 'should not be empty' );
54+
}
55+
}
56+
b.toc();
57+
if ( isnan( y ) ) {
58+
b.fail( 'should not return NaN' );
59+
}
60+
b.pass( 'benchmark finished' );
61+
b.end();
62+
});
63+
3864
bench( pkg+'::built-in', function benchmark( b ) {
3965
var re;
4066
var im;
@@ -57,3 +83,29 @@ bench( pkg+'::built-in', function benchmark( b ) {
5783
b.pass( 'benchmark finished' );
5884
b.end();
5985
});
86+
87+
bench( pkg+'::built-in,memory_reuse', function benchmark( b ) {
88+
var re;
89+
var im;
90+
var y;
91+
var i;
92+
93+
y = new Array( 2 );
94+
95+
b.tic();
96+
for ( i = 0; i < b.iterations; i++ ) {
97+
re = ( randu()*1000.0 ) - 500.0;
98+
im = ( randu()*1000.0 ) - 500.0;
99+
y[ 0 ] = round( re );
100+
y[ 1 ] = round( im );
101+
if ( y.length === 0 ) {
102+
b.fail( 'should not be empty' );
103+
}
104+
}
105+
b.toc();
106+
if ( isnan( y ) ) {
107+
b.fail( 'should not return NaN' );
108+
}
109+
b.pass( 'benchmark finished' );
110+
b.end();
111+
});
Original file line numberDiff line numberDiff line change
@@ -1,9 +1,12 @@
11

2-
{{alias}}( re, im )
2+
{{alias}}( [out,] re, im )
33
Rounds a complex number to the nearest integer.
44

55
Parameters
66
----------
7+
out: Array|TypedArray|Object (optional)
8+
Output array.
9+
710
re: number
811
Real component.
912

@@ -12,14 +15,21 @@
1215

1316
Returns
1417
-------
15-
out: Array<number>
18+
out: Array|TypedArray|Object
1619
Rounded components.
1720

1821
Examples
1922
--------
2023
> var out = {{alias}}( 5.5, 3.3 )
2124
[ 6.0, 3.0 ]
2225

26+
// Provide an output array:
27+
> out = new Float64Array( 2 );
28+
> var v = {{alias}}( out, 5.5, 3.3 )
29+
<Float64Array>[ 6.0, 3.0 ]
30+
> var bool = ( v === out )
31+
true
32+
2333
See Also
2434
--------
2535

lib/node_modules/@stdlib/math/base/complex/round/lib/cround.js

+16-3
Original file line numberDiff line numberDiff line change
@@ -10,15 +10,26 @@ var round = require( '@stdlib/math/base/special/round' );
1010
/**
1111
* Rounds a complex number to the nearest integer.
1212
*
13+
* @private
14+
* @param {(Array|TypedArray|Object)} out - output array
1315
* @param {number} re - real component
1416
* @param {number} im - imaginary component
15-
* @returns {Array<number>} rounded components
17+
* @returns {(Array|TypedArray|Object)} rounded components
1618
*
1719
* @example
1820
* var v = cround( -4.2, 5.5 );
1921
* // returns [ -4.0, 6.0 ]
2022
*
2123
* @example
24+
* var out = new Array( 2 );
25+
*
26+
* var v = cround( out, -4.2, 5.5 );
27+
* // returns [ -4.0, 6.0 ]
28+
*
29+
* var bool = ( v === out );
30+
* // returns true
31+
*
32+
* @example
2233
* var v = cround( 9.99999, 0.1 );
2334
* // returns [ 10.0, 0.0 ]
2435
*
@@ -30,8 +41,10 @@ var round = require( '@stdlib/math/base/special/round' );
3041
* var v = cround( NaN, NaN );
3142
* // returns [ NaN, NaN ]
3243
*/
33-
function cround( re, im ) {
34-
return [ round( re ), round( im ) ];
44+
function cround( out, re, im ) {
45+
out[ 0 ] = round( re );
46+
out[ 1 ] = round( im );
47+
return out;
3548
} // end FUNCTION cround()
3649

3750

lib/node_modules/@stdlib/math/base/complex/round/lib/index.js

+12-1
Original file line numberDiff line numberDiff line change
@@ -19,11 +19,22 @@
1919
*
2020
* v = cround( NaN, NaN );
2121
* // returns [ NaN, NaN ]
22+
*
23+
* @example
24+
* var cround = require( '@stdlib/math/base/complex/round' );
25+
*
26+
* var out = new Array( 2 );
27+
*
28+
* var v = cround( out, -4.2, 5.5 );
29+
* // returns [ -5.0, 6.0 ]
30+
*
31+
* var bool = ( v === out );
32+
* // returns true
2233
*/
2334

2435
// MODULES //
2536

26-
var cround = require( './cround.js' );
37+
var cround = require( './main.js' );
2738

2839

2940
// EXPORTS //
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,53 @@
1+
'use strict';
2+
3+
// MODULES //
4+
5+
var round = require( './cround.js' );
6+
7+
8+
// MAIN //
9+
10+
/**
11+
* Rounds a complex number to the nearest integer.
12+
*
13+
* @param {(Array|TypedArray|Object)} [out] - output array
14+
* @param {number} re - real component
15+
* @param {number} im - imaginary component
16+
* @returns {(Array|TypedArray|Object)} rounded components
17+
*
18+
* @example
19+
* var v = cround( -4.2, 5.5 );
20+
* // returns [ -4.0, 6.0 ]
21+
*
22+
* @example
23+
* var out = new Array( 2 );
24+
*
25+
* var v = cround( out, -4.2, 5.5 );
26+
* // returns [ -4.0, 6.0 ]
27+
*
28+
* var bool = ( v === out );
29+
* // returns true
30+
*
31+
* @example
32+
* var v = cround( 9.99999, 0.1 );
33+
* // returns [ 10.0, 0.0 ]
34+
*
35+
* @example
36+
* var v = cround( 0.0, 0.0 );
37+
* // returns [ 0.0, 0.0 ]
38+
*
39+
* @example
40+
* var v = cround( NaN, NaN );
41+
* // returns [ NaN, NaN ]
42+
*/
43+
function cround( out, re, im ) {
44+
if ( arguments.length === 2 ) {
45+
return round( new Array( 2 ), out, re );
46+
}
47+
return round( out, re, im );
48+
} // end FUNCTION cround()
49+
50+
51+
// EXPORTS //
52+
53+
module.exports = cround;

lib/node_modules/@stdlib/math/base/complex/round/test/test.js

+53-1
Original file line numberDiff line numberDiff line change
@@ -8,6 +8,7 @@ var NINF = require( '@stdlib/math/constants/float64-ninf' );
88
var isnan = require( '@stdlib/math/base/assert/is-nan' );
99
var isNegativeZero = require( '@stdlib/math/base/assert/is-negative-zero' );
1010
var isPositiveZero = require( '@stdlib/math/base/assert/is-positive-zero' );
11+
var Float64Array = require( '@stdlib/types/array/float64' ); // eslint-disable-line no-redeclare
1112
var cround = require( './../lib' );
1213

1314

@@ -19,7 +20,7 @@ tape( 'main export is a function', function test( t ) {
1920
t.end();
2021
});
2122

22-
tape( 'the function rounds real and imaginary components to the nearest integer.', function test( t ) {
23+
tape( 'the function rounds real and imaginary components to the nearest integer', function test( t ) {
2324
var expected;
2425
var actual;
2526

@@ -38,6 +39,57 @@ tape( 'the function rounds real and imaginary components to the nearest integer.
3839
t.end();
3940
});
4041

42+
tape( 'the function rounds real and imaginary components to the nearest integer (output array)', function test( t ) {
43+
var expected;
44+
var actual;
45+
var out;
46+
47+
out = new Array( 2 );
48+
actual = cround( out, -4.2, 5.5 );
49+
50+
expected = [ -4.0, 6.0 ];
51+
52+
t.deepEqual( actual, expected, 'returns expected value' );
53+
t.strictEqual( actual, out, 'returns output value' );
54+
55+
t.end();
56+
});
57+
58+
tape( 'the function rounds real and imaginary components to the nearest integer (output typed array)', function test( t ) {
59+
var expected;
60+
var actual;
61+
var out;
62+
63+
out = new Float64Array( 2 );
64+
actual = cround( out, 9.99999, 0.1 );
65+
66+
expected = new Float64Array( [ 10.0, 0.0 ] );
67+
68+
t.deepEqual( actual, expected, 'returns expected value' );
69+
t.strictEqual( actual, out, 'returns output value' );
70+
71+
t.end();
72+
});
73+
74+
tape( 'the function rounds real and imaginary components to the nearest integer (output object)', function test( t ) {
75+
var expected;
76+
var actual;
77+
var out;
78+
79+
out = {};
80+
actual = cround( out, 0.0, 0.0 );
81+
82+
expected = {
83+
'0': 0.0,
84+
'1': 0.0
85+
};
86+
87+
t.deepEqual( actual, expected, 'returns expected value' );
88+
t.strictEqual( actual, out, 'returns output value' );
89+
90+
t.end();
91+
});
92+
4193
tape( 'the function returns a `NaN` if provided a `NaN`', function test( t ) {
4294
var val = cround( NaN, NaN );
4395
t.strictEqual( isnan( val[ 0 ] ), true, 'returns expected value' );

0 commit comments

Comments
 (0)