Skip to content

Commit 57944a0

Browse files
committed
Add strided interface to reverse a strided array in-place
1 parent a9b5794 commit 57944a0

33 files changed

+3762
-0
lines changed
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,172 @@
1+
<!--
2+
3+
@license Apache-2.0
4+
5+
Copyright (c) 2020 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+
# srev
22+
23+
> Reverse a single-precision floating-point strided array in-place.
24+
25+
<section class="usage">
26+
27+
## Usage
28+
29+
```javascript
30+
var srev = require( '@stdlib/blas/ext/base/srev' );
31+
```
32+
33+
#### srev( N, x, stride )
34+
35+
Reverses a single-precision floating-point strided array `x` in-place.
36+
37+
```javascript
38+
var Float32Array = require( '@stdlib/array/float32' );
39+
40+
var x = new Float32Array( [ -2.0, 1.0, 3.0, -5.0, 4.0, 0.0, -1.0, -3.0 ] );
41+
42+
srev( x.length, x, 1 );
43+
// x => <Float32Array>[ -3.0, -1.0, 0.0, 4.0, -5.0, 3.0, 1.0, -2.0 ]
44+
```
45+
46+
The function has the following parameters:
47+
48+
- **N**: number of indexed elements.
49+
- **x**: input [`Float32Array`][@stdlib/array/float32].
50+
- **stride**: index increment.
51+
52+
The `N` and `stride` parameters determine which elements in `x` are accessed at runtime. For example, to reverse every other element
53+
54+
```javascript
55+
var Float32Array = require( '@stdlib/array/float32' );
56+
var floor = require( '@stdlib/math/base/special/floor' );
57+
58+
var x = new Float32Array( [ -2.0, 1.0, 3.0, -5.0, 4.0, 0.0, -1.0, -3.0 ] );
59+
var N = floor( x.length / 2 );
60+
61+
srev( N, x, 2 );
62+
// x => <Float32Array>[ -1.0, 1.0, 4.0, -5.0, 3.0, 0.0, -2.0, -3.0 ]
63+
```
64+
65+
Note that indexing is relative to the first index. To introduce an offset, use [`typed array`][mdn-typed-array] views.
66+
67+
```javascript
68+
var Float32Array = require( '@stdlib/array/float32' );
69+
var floor = require( '@stdlib/math/base/special/floor' );
70+
71+
// Initial array...
72+
var x0 = new Float32Array( [ 1.0, -2.0, 3.0, -4.0, 5.0, -6.0 ] );
73+
74+
// Create an offset view...
75+
var x1 = new Float32Array( x0.buffer, x0.BYTES_PER_ELEMENT*1 ); // start at 2nd element
76+
var N = floor( x0.length/2 );
77+
78+
// Reverse every other element...
79+
srev( N, x1, 2 );
80+
// x0 => <Float32Array>[ 1.0, -6.0, 3.0, -4.0, 5.0, -2.0 ]
81+
```
82+
83+
#### srev.ndarray( N, x, stride, offset )
84+
85+
Reverses a single-precision floating-point strided array `x` in-place using alternative indexing semantics.
86+
87+
```javascript
88+
var Float32Array = require( '@stdlib/array/float32' );
89+
90+
var x = new Float32Array( [ -2.0, 1.0, 3.0, -5.0, 4.0, 0.0, -1.0, -3.0 ] );
91+
92+
srev.ndarray( x.length, x, 1, 0 );
93+
// x => <Float32Array>[ -3.0, -1.0, 0.0, 4.0, -5.0, 3.0, 1.0, -2.0 ]
94+
```
95+
96+
The function has the following additional parameters:
97+
98+
- **offset**: starting index.
99+
100+
While [`typed array`][mdn-typed-array] views mandate a view offset based on the underlying `buffer`, the `offset` parameter supports indexing semantics based on a starting index. For example, to access only the last three elements of `x`
101+
102+
```javascript
103+
var Float32Array = require( '@stdlib/array/float32' );
104+
105+
var x = new Float32Array( [ 1.0, -2.0, 3.0, -4.0, 5.0, -6.0 ] );
106+
107+
srev.ndarray( 3, x, 1, x.length-3 );
108+
// x => <Float32Array>[ 1.0, -2.0, 3.0, -6.0, 5.0, -4.0 ]
109+
```
110+
111+
</section>
112+
113+
<!-- /.usage -->
114+
115+
<section class="notes">
116+
117+
## Notes
118+
119+
- If `N <= 0`, both functions return `x` unchanged.
120+
- Where possible, one should "reverse" a strided array by negating its stride, which is an `O(1)` operation, in contrast to performing an in-place reversal, which is `O(N)`. However, in certain circumstances, this is not tenable, particularly when interfacing with libraries which assume and/or expect a specific memory layout (e.g., strided array elements arranged in memory in ascending order). In general, when working with strided arrays, only perform an in-place reversal when strictly necessary.
121+
122+
</section>
123+
124+
<!-- /.notes -->
125+
126+
<section class="examples">
127+
128+
## Examples
129+
130+
<!-- eslint no-undef: "error" -->
131+
132+
```javascript
133+
var round = require( '@stdlib/math/base/special/round' );
134+
var randu = require( '@stdlib/random/base/randu' );
135+
var Float32Array = require( '@stdlib/array/float32' );
136+
var srev = require( '@stdlib/blas/ext/base/srev' );
137+
138+
var rand;
139+
var sign;
140+
var x;
141+
var i;
142+
143+
x = new Float32Array( 10 );
144+
for ( i = 0; i < x.length; i++ ) {
145+
rand = round( randu()*100.0 );
146+
sign = randu();
147+
if ( sign < 0.5 ) {
148+
sign = -1.0;
149+
} else {
150+
sign = 1.0;
151+
}
152+
x[ i ] = sign * rand;
153+
}
154+
console.log( x );
155+
156+
srev( x.length, 5.0, x, 1 );
157+
console.log( x );
158+
```
159+
160+
</section>
161+
162+
<!-- /.examples -->
163+
164+
<section class="links">
165+
166+
[@stdlib/array/float32]: https://github.com/stdlib-js/stdlib
167+
168+
[mdn-typed-array]: https://developer.mozilla.org/en-US/docs/Web/JavaScript/Reference/Global_Objects/TypedArray
169+
170+
</section>
171+
172+
<!-- /.links -->
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,91 @@
1+
/**
2+
* @license Apache-2.0
3+
*
4+
* Copyright (c) 2020 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 randu = require( '@stdlib/random/base/randu' );
25+
var isnanf = require( '@stdlib/math/base/assert/is-nanf' );
26+
var pow = require( '@stdlib/math/base/special/pow' );
27+
var Float32Array = require( '@stdlib/array/float32' );
28+
var pkg = require( './../package.json' ).name;
29+
var srev = require( './../lib/srev.js' );
30+
31+
32+
// FUNCTIONS //
33+
34+
/**
35+
* Create a benchmark function.
36+
*
37+
* @private
38+
* @param {PositiveInteger} len - array length
39+
* @returns {Function} benchmark function
40+
*/
41+
function createBenchmark( len ) {
42+
var x;
43+
var i;
44+
45+
x = new Float32Array( len );
46+
for ( i = 0; i < len; i++ ) {
47+
x[ i ] = ( randu()*20.0 ) - 10.0;
48+
}
49+
return benchmark;
50+
51+
function benchmark( b ) {
52+
var y;
53+
var i;
54+
55+
b.tic();
56+
for ( i = 0; i < b.iterations; i++ ) {
57+
y = srev( x.length, x, 1 );
58+
if ( isnanf( y[ i%x.length ] ) ) {
59+
b.fail( 'should not return NaN' );
60+
}
61+
}
62+
b.toc();
63+
if ( isnanf( y[ i%x.length ] ) ) {
64+
b.fail( 'should not return NaN' );
65+
}
66+
b.pass( 'benchmark finished' );
67+
b.end();
68+
}
69+
}
70+
71+
72+
// MAIN //
73+
74+
function main() {
75+
var len;
76+
var min;
77+
var max;
78+
var f;
79+
var i;
80+
81+
min = 1; // 10^min
82+
max = 6; // 10^max
83+
84+
for ( i = min; i <= max; i++ ) {
85+
len = pow( 10, i );
86+
f = createBenchmark( len );
87+
bench( pkg+':len='+len, f );
88+
}
89+
}
90+
91+
main();
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,100 @@
1+
/**
2+
* @license Apache-2.0
3+
*
4+
* Copyright (c) 2020 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 randu = require( '@stdlib/random/base/randu' );
26+
var isnanf = require( '@stdlib/math/base/assert/is-nanf' );
27+
var pow = require( '@stdlib/math/base/special/pow' );
28+
var Float32Array = require( '@stdlib/array/float32' );
29+
var tryRequire = require( '@stdlib/utils/try-require' );
30+
var pkg = require( './../package.json' ).name;
31+
32+
33+
// VARIABLES //
34+
35+
var srev = tryRequire( resolve( __dirname, './../lib/native.js' ) );
36+
var opts = {
37+
'skip': ( srev instanceof Error )
38+
};
39+
40+
41+
// FUNCTIONS //
42+
43+
/**
44+
* Create a benchmark function.
45+
*
46+
* @private
47+
* @param {PositiveInteger} len - array length
48+
* @returns {Function} benchmark function
49+
*/
50+
function createBenchmark( len ) {
51+
var x;
52+
var i;
53+
54+
x = new Float32Array( len );
55+
for ( i = 0; i < len; i++ ) {
56+
x[ i ] = ( randu()*20.0 ) - 10.0;
57+
}
58+
return benchmark;
59+
60+
function benchmark( b ) {
61+
var y;
62+
var i;
63+
64+
b.tic();
65+
for ( i = 0; i < b.iterations; i++ ) {
66+
y = srev( x.length, x, 1 );
67+
if ( isnanf( y[ i%x.length ] ) ) {
68+
b.fail( 'should not return NaN' );
69+
}
70+
}
71+
b.toc();
72+
if ( isnanf( y[ i%x.length ] ) ) {
73+
b.fail( 'should not return NaN' );
74+
}
75+
b.pass( 'benchmark finished' );
76+
b.end();
77+
}
78+
}
79+
80+
81+
// MAIN //
82+
83+
function main() {
84+
var len;
85+
var min;
86+
var max;
87+
var f;
88+
var i;
89+
90+
min = 1; // 10^min
91+
max = 6; // 10^max
92+
93+
for ( i = min; i <= max; i++ ) {
94+
len = pow( 10, i );
95+
f = createBenchmark( len );
96+
bench( pkg+'::native:len='+len, opts, f );
97+
}
98+
}
99+
100+
main();

0 commit comments

Comments
 (0)