Skip to content

Commit 5debc39

Browse files
committed
feat: implement @stdlib/blas/ext/base/ndarray/drev
- Add ndarray wrapper for drev function - Implement comprehensive test suite with stride/offset tests - Add documentation (README, REPL, TypeScript types) - Add examples and performance benchmarks - Follow stdlib conventions for BLAS operations
1 parent 134e322 commit 5debc39

File tree

10 files changed

+756
-0
lines changed

10 files changed

+756
-0
lines changed
Lines changed: 112 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,112 @@
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+
# drev
22+
23+
> Reverse the elements of a one-dimensional double-precision floating-point ndarray in-place.
24+
25+
<section class="intro">
26+
27+
</section>
28+
29+
<!-- /.intro -->
30+
31+
<section class="usage">
32+
33+
## Usage
34+
35+
```javascript
36+
var drev = require( '@stdlib/blas/ext/base/ndarray/drev' );
37+
```
38+
39+
#### drev( arrays )
40+
41+
Reverses the elements of a one-dimensional double-precision floating-point ndarray in-place.
42+
43+
```javascript
44+
var Float64Array = require( '@stdlib/array/float64' );
45+
var ndarray = require( '@stdlib/ndarray/base/ctor' );
46+
47+
var xbuf = new Float64Array( [ 1.0, 2.0, 3.0 ] );
48+
var x = new ndarray( 'float64', xbuf, [ 3 ], [ 1 ], 0, 'row-major' );
49+
50+
drev( x );
51+
// x => <ndarray>[ 3.0, 2.0, 1.0 ]
52+
```
53+
54+
The function has the following parameters:
55+
56+
- **x**: input ndarray.
57+
58+
</section>
59+
60+
<!-- /.usage -->
61+
62+
<section class="notes">
63+
64+
## Notes
65+
66+
- The function **mutates** the input ndarray.
67+
68+
</section>
69+
70+
<!-- /.notes -->
71+
72+
<section class="examples">
73+
74+
## Examples
75+
76+
<!-- eslint no-undef: "error" -->
77+
78+
```javascript
79+
var discreteUniform = require( '@stdlib/random/array/discrete-uniform' );
80+
var ndarray = require( '@stdlib/ndarray/base/ctor' );
81+
var ndarray2array = require( '@stdlib/ndarray/to-array' );
82+
var drev = require( '@stdlib/blas/ext/base/ndarray/drev' );
83+
84+
var xbuf = discreteUniform( 10, -50, 50, {
85+
'dtype': 'float64'
86+
});
87+
var x = new ndarray( 'float64', xbuf, [ xbuf.length ], [ 1 ], 0, 'row-major' );
88+
console.log( ndarray2array( x ) );
89+
90+
drev( x );
91+
console.log( ndarray2array( x ) );
92+
```
93+
94+
</section>
95+
96+
<!-- /.examples -->
97+
98+
<!-- Section for related `stdlib` packages. Do not manually edit this section, as it is automatically populated. -->
99+
100+
<section class="related">
101+
102+
</section>
103+
104+
<!-- /.related -->
105+
106+
<!-- Section for all links. Make sure to keep an empty line after the `section` element and another before the `/section` close. -->
107+
108+
<section class="links">
109+
110+
</section>
111+
112+
<!-- /.links -->
Lines changed: 100 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,100 @@
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 uniform = require( '@stdlib/random/array/uniform' );
25+
var pow = require( '@stdlib/math/base/special/pow' );
26+
var ndarray = require( '@stdlib/ndarray/base/ctor' );
27+
var pkg = require( './../package.json' ).name;
28+
var drev = require( './../lib' );
29+
30+
31+
// VARIABLES //
32+
33+
var options = {
34+
'dtype': 'float64'
35+
};
36+
37+
38+
// FUNCTIONS //
39+
40+
/**
41+
* Creates a benchmark function.
42+
*
43+
* @private
44+
* @param {PositiveInteger} len - array length
45+
* @returns {Function} benchmark function
46+
*/
47+
function createBenchmark( len ) {
48+
var xbuf;
49+
var x;
50+
51+
xbuf = uniform( len, -10.0, 10.0, options );
52+
x = new ndarray( options.dtype, xbuf, [ len ], [ 1 ], 0, 'row-major' );
53+
54+
return benchmark;
55+
56+
function benchmark( b ) {
57+
var i;
58+
59+
b.tic();
60+
for ( i = 0; i < b.iterations; i++ ) {
61+
drev( x );
62+
if ( xbuf[ 0 ] !== xbuf[ 0 ] ) {
63+
b.fail( 'should not return NaN' );
64+
}
65+
}
66+
b.toc();
67+
if ( xbuf[ 0 ] !== xbuf[ 0 ] ) {
68+
b.fail( 'should not return NaN' );
69+
}
70+
b.pass( 'benchmark finished' );
71+
b.end();
72+
}
73+
}
74+
75+
76+
// MAIN //
77+
78+
/**
79+
* Main execution sequence.
80+
*
81+
* @private
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 + ':len=' + len, f );
97+
}
98+
}
99+
100+
main();
Lines changed: 34 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,34 @@
1+
2+
{{alias}}( x )
3+
Reverses the elements of a one-dimensional double-precision floating-point
4+
ndarray in-place.
5+
6+
The function mutates the input ndarray.
7+
8+
Parameters
9+
----------
10+
x: ndarray
11+
Input ndarray.
12+
13+
Returns
14+
-------
15+
out: ndarray
16+
Input ndarray.
17+
18+
Examples
19+
--------
20+
> var xbuf = new {{alias:@stdlib/array/float64}}( [ 1.0, 2.0, 3.0 ] );
21+
> var dt = 'float64';
22+
> var sh = [ xbuf.length ];
23+
> var sx = [ 1 ];
24+
> var ox = 0;
25+
> var ord = 'row-major';
26+
> var x = new {{alias:@stdlib/ndarray/ctor}}( dt, xbuf, sh, sx, ox, ord );
27+
> {{alias}}( x )
28+
<ndarray>
29+
> {{alias:@stdlib/ndarray/to-array}}( x )
30+
[ 3.0, 2.0, 1.0 ]
31+
32+
See Also
33+
--------
34+
Lines changed: 46 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,46 @@
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 { float64ndarray } from '@stdlib/types/ndarray';
24+
25+
/**
26+
* Reverses the elements of a one-dimensional double-precision floating-point ndarray in-place.
27+
*
28+
* @param x - input ndarray
29+
* @returns input ndarray
30+
*
31+
* @example
32+
* var Float64Array = require( '@stdlib/array/float64' );
33+
* var ndarray = require( '@stdlib/ndarray/base/ctor' );
34+
*
35+
* var xbuf = new Float64Array( [ 1.0, 2.0, 3.0 ] );
36+
* var x = new ndarray( 'float64', xbuf, [ 3 ], [ 1 ], 0, 'row-major' );
37+
*
38+
* drev( x );
39+
* // x => <ndarray>[ 3.0, 2.0, 1.0 ]
40+
*/
41+
declare function drev( x: float64ndarray ): float64ndarray;
42+
43+
44+
// EXPORTS //
45+
46+
export = drev;
Lines changed: 56 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,56 @@
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 drev = require( './index' );
20+
21+
22+
// TESTS //
23+
24+
// The function returns an ndarray...
25+
{
26+
const Float64Array = require( '@stdlib/array/float64' );
27+
const ndarray = require( '@stdlib/ndarray/base/ctor' );
28+
29+
const xbuf = new Float64Array( [ 1.0, 2.0, 3.0 ] );
30+
const x = new ndarray( 'float64', xbuf, [ 3 ], [ 1 ], 0, 'row-major' );
31+
32+
drev( x ); // $ExpectType float64ndarray
33+
}
34+
35+
// The compiler throws an error if the function is provided a first argument which is not an array-like object containing an ndarray...
36+
{
37+
drev( 123 ); // $ExpectError
38+
drev( true ); // $ExpectError
39+
drev( false ); // $ExpectError
40+
drev( null ); // $ExpectError
41+
drev( undefined ); // $ExpectError
42+
drev( {} ); // $ExpectError
43+
drev( [ 123 ] ); // $ExpectError
44+
}
45+
46+
// The compiler throws an error if the function is provided an unsupported number of arguments...
47+
{
48+
const Float64Array = require( '@stdlib/array/float64' );
49+
const ndarray = require( '@stdlib/ndarray/base/ctor' );
50+
51+
const xbuf = new Float64Array( [ 1.0, 2.0, 3.0 ] );
52+
const x = new ndarray( 'float64', xbuf, [ 3 ], [ 1 ], 0, 'row-major' );
53+
54+
drev(); // $ExpectError
55+
drev( x, 123 ); // $ExpectError
56+
}
Lines changed: 33 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,33 @@
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 discreteUniform = require( '@stdlib/random/array/discrete-uniform' );
22+
var ndarray = require( '@stdlib/ndarray/base/ctor' );
23+
var ndarray2array = require( '@stdlib/ndarray/to-array' );
24+
var drev = require( './../lib' );
25+
26+
var xbuf = discreteUniform( 10, -50, 50, {
27+
'dtype': 'float64'
28+
});
29+
var x = new ndarray( 'float64', xbuf, [ xbuf.length ], [ 1 ], 0, 'row-major' );
30+
console.log( ndarray2array( x ) );
31+
32+
drev( x );
33+
console.log( ndarray2array( x ) );

0 commit comments

Comments
 (0)