Skip to content

Commit 13fcc1d

Browse files
feat: add assign and strided methods to math/base/special/croundf
PR-URL: #16039 Ref: stdlib-js/metr-issue-tracker#1155 Reviewed-by: Philipp Burckhardt <pburckhardt@outlook.com>
1 parent fa49d31 commit 13fcc1d

20 files changed

Lines changed: 1194 additions & 129 deletions

File tree

‎lib/node_modules/@stdlib/math/base/special/croundf/README.md‎

Lines changed: 56 additions & 9 deletions
Original file line numberDiff line numberDiff line change
@@ -69,6 +69,55 @@ v = croundf( new Complex64( NaN, NaN ) );
6969
// returns <Complex64>[ NaN, NaN ]
7070
```
7171

72+
#### croundf.assign( re, im, out, strideOut, offsetOut )
73+
74+
Rounds each component of a single-precision complex floating-point number to the nearest integer and assigns results to a provided output array.
75+
76+
```javascript
77+
var Float32Array = require( '@stdlib/array/float32' );
78+
79+
var out = new Float32Array( 2 );
80+
var v = croundf.assign( -1.5, 2.5, out, 1, 0 );
81+
// returns <Float32Array>[ -1.0, 3.0 ]
82+
83+
var bool = ( out === v );
84+
// returns true
85+
```
86+
87+
The function supports the following parameters:
88+
89+
- **re**: real component.
90+
- **im**: imaginary component.
91+
- **out**: output array.
92+
- **strideOut**: stride length for `out`.
93+
- **offsetOut**: starting index for `out`.
94+
95+
#### croundf.strided( z, sz, oz, out, so, oo )
96+
97+
Rounds each component of a single-precision complex floating-point number stored in a real-valued strided array view to the nearest integer and assigns results to a provided strided output array.
98+
99+
```javascript
100+
var Float32Array = require( '@stdlib/array/float32' );
101+
102+
var z = new Float32Array( [ -1.5, 2.5 ] );
103+
var out = new Float32Array( 2 );
104+
105+
var v = croundf.strided( z, 1, 0, out, 1, 0 );
106+
// returns <Float32Array>[ -1.0, 3.0 ]
107+
108+
var bool = ( out === v );
109+
// returns true
110+
```
111+
112+
The function supports the following parameters:
113+
114+
- **z**: complex number strided array view.
115+
- **sz**: stride length for `z`.
116+
- **oz**: starting index for `z`.
117+
- **out**: output array.
118+
- **so**: stride length for `out`.
119+
- **oo**: starting index for `out`.
120+
72121
</section>
73122

74123
<!-- /.usage -->
@@ -80,18 +129,16 @@ v = croundf( new Complex64( NaN, NaN ) );
80129
<!-- eslint no-undef: "error" -->
81130

82131
```javascript
83-
var uniform = require( '@stdlib/random/base/uniform' ).factory;
84-
var Complex64 = require( '@stdlib/complex/float32/ctor' );
132+
var Complex64Array = require( '@stdlib/array/complex64' );
133+
var uniform = require( '@stdlib/random/array/uniform' );
134+
var logEachMap = require( '@stdlib/console/log-each-map' );
85135
var croundf = require( '@stdlib/math/base/special/croundf' );
86136

87-
var rand = uniform( -50.0, 50.0 );
137+
// Create an array of random numbers:
138+
var arr = new Complex64Array( uniform( 200, -50.0, 50.0 ) );
88139

89-
var z;
90-
var i;
91-
for ( i = 0; i < 100; i++ ) {
92-
z = new Complex64( rand(), rand() );
93-
console.log( 'croundf(%s) = %s', z, croundf( z ) );
94-
}
140+
// Round each component of each complex number to the nearest integer:
141+
logEachMap( 'croundf(%s) = %s', arr, croundf );
95142
```
96143

97144
</section>
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) 2026 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/array/uniform' );
25+
var isnanf = require( '@stdlib/math/base/assert/is-nanf' );
26+
var Float32Array = require( '@stdlib/array/float32' );
27+
var format = require( '@stdlib/string/format' );
28+
var pkg = require( './../package.json' ).name;
29+
var assign = require( './../lib' ).assign;
30+
31+
32+
// VARIABLES //
33+
34+
var options = {
35+
'dtype': 'float32'
36+
};
37+
38+
39+
// MAIN //
40+
41+
bench( format( '%s:assign', pkg ), function benchmark( b ) {
42+
var out;
43+
var re;
44+
var im;
45+
var N;
46+
var i;
47+
var j;
48+
49+
N = 100;
50+
re = uniform( N, -500.0, 500.0, options );
51+
im = uniform( N, -500.0, 500.0, options );
52+
53+
out = new Float32Array( 2 );
54+
55+
b.tic();
56+
for ( i = 0; i < b.iterations; i++ ) {
57+
j = i % N;
58+
out = assign( re[ j ], im[ j ], out, 1, 0 );
59+
if ( typeof out !== 'object' ) {
60+
b.fail( 'should return an object' );
61+
}
62+
}
63+
b.toc();
64+
if ( isnanf( out[ 0 ] ) || isnanf( out[ 1 ] ) ) {
65+
b.fail( 'should not return NaN' );
66+
}
67+
b.pass( 'benchmark finished' );
68+
b.end();
69+
});

‎lib/node_modules/@stdlib/math/base/special/croundf/benchmark/benchmark.js‎

Lines changed: 19 additions & 13 deletions
Original file line numberDiff line numberDiff line change
@@ -21,37 +21,43 @@
2121
// MODULES //
2222

2323
var bench = require( '@stdlib/bench' );
24-
var uniform = require( '@stdlib/random/base/uniform' );
24+
var uniform = require( '@stdlib/random/array/uniform' );
2525
var isnanf = require( '@stdlib/math/base/assert/is-nanf' );
26-
var Complex64 = require( '@stdlib/complex/float32/ctor' );
27-
var real = require( '@stdlib/complex/float32/real' );
28-
var imag = require( '@stdlib/complex/float32/imag' );
26+
var Complex64Array = require( '@stdlib/array/complex64' );
27+
var realf = require( '@stdlib/complex/float32/real' );
28+
var imagf = require( '@stdlib/complex/float32/imag' );
2929
var pkg = require( './../package.json' ).name;
3030
var croundf = require( './../lib' );
3131

3232

33+
// VARIABLES //
34+
35+
var options = {
36+
'dtype': 'float32'
37+
};
38+
39+
3340
// MAIN //
3441

3542
bench( pkg, function benchmark( b ) {
36-
var values;
43+
var zbuf;
44+
var z;
3745
var y;
3846
var i;
3947

40-
values = [
41-
new Complex64( uniform( -500.0, 500.0 ), uniform( -500.0, 500.0 ) ),
42-
new Complex64( uniform( -500.0, 500.0 ), uniform( -500.0, 500.0 ) )
43-
];
48+
zbuf = uniform( 200, -500.0, 500.0, options );
49+
z = new Complex64Array( zbuf.buffer );
4450

4551
b.tic();
4652
for ( i = 0; i < b.iterations; i++ ) {
47-
y = croundf( values[ i%values.length ] );
48-
if ( isnanf( real( y ) ) ) {
53+
y = croundf( z.get( i%100 ) );
54+
if ( isnanf( realf( y ) ) ) {
4955
b.fail( 'should not return NaN' );
5056
}
5157
}
5258
b.toc();
53-
if ( isnanf( imag( y ) ) ) {
54-
b.fail( 'should not return not NaN' );
59+
if ( isnanf( imagf( y ) ) ) {
60+
b.fail( 'should not return NaN' );
5561
}
5662
b.pass( 'benchmark finished' );
5763
b.end();

‎lib/node_modules/@stdlib/math/base/special/croundf/benchmark/benchmark.native.js‎

Lines changed: 14 additions & 11 deletions
Original file line numberDiff line numberDiff line change
@@ -22,10 +22,11 @@
2222

2323
var resolve = require( 'path' ).resolve;
2424
var bench = require( '@stdlib/bench' );
25-
var uniform = require( '@stdlib/random/base/uniform' );
25+
var uniform = require( '@stdlib/random/array/uniform' );
2626
var isnanf = require( '@stdlib/math/base/assert/is-nanf' );
27-
var Complex64 = require( '@stdlib/complex/float32/ctor' );
28-
var real = require( '@stdlib/complex/float32/real' );
27+
var Complex64Array = require( '@stdlib/array/complex64' );
28+
var realf = require( '@stdlib/complex/float32/real' );
29+
var imagf = require( '@stdlib/complex/float32/imag' );
2930
var tryRequire = require( '@stdlib/utils/try-require' );
3031
var format = require( '@stdlib/string/format' );
3132
var pkg = require( './../package.json' ).name;
@@ -37,29 +38,31 @@ var croundf = tryRequire( resolve( __dirname, './../lib/native.js' ) );
3738
var opts = {
3839
'skip': ( croundf instanceof Error )
3940
};
41+
var options = {
42+
'dtype': 'float32'
43+
};
4044

4145

4246
// MAIN //
4347

4448
bench( format( '%s::native', pkg ), opts, function benchmark( b ) {
45-
var values;
49+
var zbuf;
50+
var z;
4651
var y;
4752
var i;
4853

49-
values = [
50-
new Complex64( uniform( -500.0, 500.0 ), uniform( -500.0, 500.0 ) ),
51-
new Complex64( uniform( -500.0, 500.0 ), uniform( -500.0, 500.0 ) )
52-
];
54+
zbuf = uniform( 200, -500.0, 500.0, options );
55+
z = new Complex64Array( zbuf.buffer );
5356

5457
b.tic();
5558
for ( i = 0; i < b.iterations; i++ ) {
56-
y = croundf( values[ i%values.length ] );
57-
if ( isnanf( real( y ) ) ) {
59+
y = croundf( z.get( i%100 ) );
60+
if ( isnanf( realf( y ) ) ) {
5861
b.fail( 'should not return NaN' );
5962
}
6063
}
6164
b.toc();
62-
if ( isnanf( real( y ) ) ) {
65+
if ( isnanf( imagf( y ) ) ) {
6366
b.fail( 'should not return NaN' );
6467
}
6568
b.pass( 'benchmark finished' );
Lines changed: 67 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,67 @@
1+
/**
2+
* @license Apache-2.0
3+
*
4+
* Copyright (c) 2026 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/array/uniform' );
25+
var isnanf = require( '@stdlib/math/base/assert/is-nanf' );
26+
var Float32Array = require( '@stdlib/array/float32' );
27+
var format = require( '@stdlib/string/format' );
28+
var pkg = require( './../package.json' ).name;
29+
var strided = require( './../lib' ).strided;
30+
31+
32+
// VARIABLES //
33+
34+
var options = {
35+
'dtype': 'float32'
36+
};
37+
38+
39+
// MAIN //
40+
41+
bench( format( '%s:strided', pkg ), function benchmark( b ) {
42+
var out;
43+
var z;
44+
var N;
45+
var i;
46+
var j;
47+
48+
N = 50;
49+
z = uniform( N*2, -500.0, 500.0, options );
50+
51+
out = new Float32Array( 2 );
52+
53+
b.tic();
54+
for ( i = 0; i < b.iterations; i++ ) {
55+
j = ( i % N ) * 2;
56+
out = strided( z, 1, j, out, 1, 0 );
57+
if ( typeof out !== 'object' ) {
58+
b.fail( 'should return an object' );
59+
}
60+
}
61+
b.toc();
62+
if ( isnanf( out[ 0 ] ) || isnanf( out[ 1 ] ) ) {
63+
b.fail( 'should not return NaN' );
64+
}
65+
b.pass( 'benchmark finished' );
66+
b.end();
67+
});

‎lib/node_modules/@stdlib/math/base/special/croundf/benchmark/c/benchmark.c‎

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -20,9 +20,10 @@
2020
#include <stdio.h>
2121
#include <math.h>
2222
#include <complex.h>
23+
#include <time.h>
2324
#include <sys/time.h>
2425

25-
#define NAME "roundf"
26+
#define NAME "croundf"
2627
#define ITERATIONS 1000000
2728
#define REPEATS 3
2829

0 commit comments

Comments
 (0)