Skip to content

Commit 03b9166

Browse files
committed
feat: implement ndarray/srev package
1 parent e7292ba commit 03b9166

10 files changed

Lines changed: 669 additions & 0 deletions

File tree

Lines changed: 102 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,102 @@
1+
<!--
2+
3+
@license Apache-2.0
4+
5+
Copyright (c) 2025 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 the elements of a one-dimensional single-precision floating-point ndarray.
24+
25+
<section class="intro">
26+
27+
</section>
28+
29+
<!-- /.intro -->
30+
31+
<section class="usage">
32+
33+
## Usage
34+
35+
```javascript
36+
var srev = require( '@stdlib/blas/ext/base/ndarray/srev' );
37+
```
38+
39+
#### srev( arrays )
40+
41+
Reverses the elements of a one-dimensional single-precision floating-point ndarray.
42+
43+
```javascript
44+
var Float32Array = require( '@stdlib/array/float32' );
45+
var ndarray = require( '@stdlib/ndarray/base/ctor' );
46+
47+
var xbuf = new Float32Array( [ 1.0, 2.0, 3.0 ] );
48+
var x = new ndarray( 'float32', xbuf, [ 3 ], [ 1 ], 0, 'row-major' );
49+
50+
srev( [ x ] );
51+
// xbuf => <Float32Array>[ 3.0, 2.0, 1.0 ]
52+
```
53+
54+
The function accepts the following arguments:
55+
56+
- **arrays**: array-like object containing one one-dimensional `Float32` ndarray.
57+
58+
</section>
59+
60+
<!-- /.usage -->
61+
62+
<section class="examples">
63+
64+
## Examples
65+
66+
```javascript
67+
var Float32Array = require( '@stdlib/array/float32' );
68+
var ndarray = require( '@stdlib/ndarray/base/ctor' );
69+
var srev = require( '@stdlib/blas/ext/base/ndarray/srev' );
70+
71+
var xbuf, x, i;
72+
73+
xbuf = new Float32Array( [ 1.0, 2.0, 3.0, 4.0, 5.0, 6.0 ] );
74+
x = new ndarray( 'float32', xbuf, [ 6 ], [ -1 ], 5, 'row-major' );
75+
76+
srev( [ x ] );
77+
78+
console.log( 'x:' );
79+
for ( i = 0; i < 6; i++ ) {
80+
console.log( x.get( i ) );
81+
}
82+
```
83+
84+
</section>
85+
86+
<!-- /.examples -->
87+
88+
<!-- Section for related `stdlib` packages. Do not manually edit this section. -->
89+
90+
<section class="related">
91+
92+
</section>
93+
94+
<!-- /.related -->
95+
96+
<!-- Section for all links. Make sure to keep this section sorted. -->
97+
98+
<section class="links">
99+
100+
</section>
101+
102+
<!-- /.links -->
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) 2025 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 Float32Array = require( '@stdlib/array/float32' );
25+
var ndarray = require( '@stdlib/ndarray/base/ctor' );
26+
var uniform = require( '@stdlib/random/base/uniform' );
27+
var isnan = require( '@stdlib/math/base/assert/is-nan' );
28+
var pkg = require( './../package.json' ).name;
29+
var srev = require( './../lib' );
30+
31+
32+
// MAIN //
33+
34+
bench( pkg, function benchmark( b ) {
35+
var len;
36+
var buf;
37+
var x;
38+
var i;
39+
40+
len = 100;
41+
buf = new Float32Array( len );
42+
for ( i = 0; i < len; i++ ) {
43+
buf[ i ] = uniform( -10.0, 10.0 );
44+
}
45+
x = new ndarray( 'float32', buf, [ len ], [ 1 ], 0, 'row-major' );
46+
47+
b.tic();
48+
for ( i = 0; i < b.iterations; i++ ) {
49+
srev( [ x ] );
50+
if ( isnan( x.get( i%len ) ) ) {
51+
b.fail( 'should not return NaN' );
52+
}
53+
}
54+
b.toc();
55+
if ( isnan( x.get( i%len ) ) ) {
56+
b.fail( 'should not return NaN' );
57+
}
58+
b.pass( 'benchmark finished' );
59+
b.end();
60+
});
Lines changed: 29 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,29 @@
1+
2+
// Create a Float32Array:
3+
var Float32Array = require( '@stdlib/array/float32' );
4+
var xbuf = new Float32Array( [ 1.0, 2.0, 3.0, 4.0, 5.0, 6.0 ] );
5+
6+
// Define a strided array:
7+
var ndarray = require( '@stdlib/ndarray/base/ctor' );
8+
var x = new ndarray( 'float32', xbuf, [ 6 ], [ -1 ], 5, 'row-major' );
9+
10+
// Import the function:
11+
var srev = require( '@stdlib/blas/ext/base/ndarray/srev' );
12+
13+
// Reverse elements:
14+
srev( [ x ] );
15+
16+
// Check the underlying buffer:
17+
xbuf
18+
// => <Float32Array>[ 1.0, 2.0, 3.0, 4.0, 5.0, 6.0 ]
19+
20+
// Check the array contents:
21+
x.data
22+
// => <Float32Array>[ 1.0, 2.0, 3.0, 4.0, 5.0, 6.0 ]
23+
24+
// Access elements via the ndarray instance:
25+
x.get( 0 )
26+
// => 1.0
27+
28+
x.get( 5 )
29+
// => 6.0
Lines changed: 45 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,45 @@
1+
/*
2+
* @license Apache-2.0
3+
*
4+
* Copyright (c) 2025 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+
// TypeScript Version: 4.1
20+
21+
/// <reference types="@stdlib/types"/>
22+
23+
import { ndarray } from '@stdlib/types/ndarray';
24+
25+
/**
26+
* Reverses the elements of a one-dimensional single-precision floating-point ndarray.
27+
*
28+
* @param arrays - array-like object containing one input ndarray
29+
*
30+
* @example
31+
* var Float32Array = require( '@stdlib/array/float32' );
32+
* var ndarray = require( '@stdlib/ndarray/base/ctor' );
33+
*
34+
* var xbuf = new Float32Array( [ 1.0, 2.0, 3.0 ] );
35+
* var x = new ndarray( 'float32', xbuf, [ 3 ], [ 1 ], 0, 'row-major' );
36+
*
37+
* srev( [ x ] );
38+
* // xbuf => <Float32Array>[ 3.0, 2.0, 1.0 ]
39+
*/
40+
declare function srev( arrays: ArrayLike<ndarray> ): void;
41+
42+
43+
// EXPORTS //
44+
45+
export = srev;
Lines changed: 49 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,49 @@
1+
/*
2+
* @license Apache-2.0
3+
*
4+
* Copyright (c) 2025 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+
import Float32Array = require( '@stdlib/array/float32' );
20+
import ndarray = require( '@stdlib/ndarray/base/ctor' );
21+
import srev = require( './index' );
22+
23+
24+
// TESTS //
25+
26+
// The function returns void...
27+
{
28+
const xbuf = new Float32Array( 10 );
29+
const x = new ndarray( 'float32', xbuf, [ 10 ], [ 1 ], 0, 'row-major' );
30+
31+
srev( [ x ] ); // $ExpectType void
32+
}
33+
34+
// The compiler throws an error if the function is provided a first argument which is not an array-like object containing ndarrays...
35+
{
36+
srev( 'abc' ); // $ExpectError
37+
srev( 3.14 ); // $ExpectError
38+
srev( true ); // $ExpectError
39+
srev( false ); // $ExpectError
40+
srev( null ); // $ExpectError
41+
srev( undefined ); // $ExpectError
42+
srev( [ '1' ] ); // $ExpectError
43+
srev( {} ); // $ExpectError
44+
}
45+
46+
// The compiler throws an error if the function is provided an insufficient number of arguments...
47+
{
48+
srev(); // $ExpectError
49+
}
Lines changed: 34 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,34 @@
1+
/**
2+
* @license Apache-2.0
3+
*
4+
* Copyright (c) 2025 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+
var Float32Array = require( '@stdlib/array/float32' );
22+
var ndarray = require( '@stdlib/ndarray/base/ctor' );
23+
var srev = require( './../lib' );
24+
25+
var xbuf = new Float32Array( [ 1.0, 2.0, 3.0, 4.0, 5.0, 6.0 ] );
26+
var x = new ndarray( 'float32', xbuf, [ 6 ], [ -1 ], 5, 'row-major' );
27+
28+
console.log( 'Original:' );
29+
console.log( x.data );
30+
31+
srev( [ x ] );
32+
33+
console.log( 'Reversed:' );
34+
console.log( x.data );
Lines changed: 45 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,45 @@
1+
/**
2+
* @license Apache-2.0
3+
*
4+
* Copyright (c) 2025 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+
/**
22+
* Reverse the elements of a one-dimensional single-precision floating-point ndarray.
23+
*
24+
* @module @stdlib/blas/ext/base/ndarray/srev
25+
*
26+
* @example
27+
* var Float32Array = require( '@stdlib/array/float32' );
28+
* var ndarray = require( '@stdlib/ndarray/base/ctor' );
29+
* var srev = require( '@stdlib/blas/ext/base/ndarray/srev' );
30+
*
31+
* var xbuf = new Float32Array( [ 1.0, 2.0, 3.0 ] );
32+
* var x = new ndarray( 'float32', xbuf, [ 3 ], [ 1 ], 0, 'row-major' );
33+
*
34+
* srev( [ x ] );
35+
* // xbuf => <Float32Array>[ 3.0, 2.0, 1.0 ]
36+
*/
37+
38+
// MODULES //
39+
40+
var main = require( './main.js' );
41+
42+
43+
// EXPORTS //
44+
45+
module.exports = main;

0 commit comments

Comments
 (0)