Skip to content

Commit 9efe081

Browse files
committed
Auto-generated commit
1 parent 4da1515 commit 9efe081

File tree

13 files changed

+799
-5
lines changed

13 files changed

+799
-5
lines changed

base/lib/index.js

Lines changed: 9 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -639,6 +639,15 @@ setReadOnly( ns, 'quinary2d', require( './../../base/quinary2d' ) );
639639
*/
640640
setReadOnly( ns, 'setter', require( './../../base/setter' ) );
641641

642+
/**
643+
* @name strided2array2d
644+
* @memberof ns
645+
* @readonly
646+
* @type {Function}
647+
* @see {@link module:@stdlib/array/base/strided2array2d}
648+
*/
649+
setReadOnly( ns, 'strided2array2d', require( './../../base/strided2array2d' ) );
650+
642651
/**
643652
* @name take
644653
* @memberof ns

base/strided2array2d/README.md

Lines changed: 118 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,118 @@
1+
<!--
2+
3+
@license Apache-2.0
4+
5+
Copyright (c) 2023 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+
# strided2array2d
22+
23+
> Convert a strided array to a two-dimensional nested array.
24+
25+
<section class="intro">
26+
27+
</section>
28+
29+
<!-- /.intro -->
30+
31+
<section class="usage">
32+
33+
## Usage
34+
35+
```javascript
36+
var strided2array2d = require( '@stdlib/array/base/strided2array2d' );
37+
```
38+
39+
#### strided2array2d( x, shape, strides, offset )
40+
41+
Converts a strided array to a two-dimensional nested array.
42+
43+
```javascript
44+
var x = [ 1, 2, 3, 4, 5, 6 ];
45+
46+
var arr = strided2array2d( x, [ 3, 2 ], [ 2, 1 ], 0 );
47+
// returns [ [ 1, 2 ], [ 3, 4 ], [ 5, 6 ] ]
48+
49+
arr = strided2array2d( x, [ 3, 2 ], [ 1, 3 ], 0 );
50+
// returns [ [ 1, 4 ], [ 2, 5 ], [ 3, 6 ] ]
51+
```
52+
53+
The function accepts the following arguments:
54+
55+
- **x**: input array.
56+
- **shape**: array shape.
57+
- **strides**: dimension strides.
58+
- **offset**: index of the first indexed value in the input array.
59+
60+
</section>
61+
62+
<!-- /.usage -->
63+
64+
<section class="notes">
65+
66+
## Notes
67+
68+
- The function assumes that the input array is [compatible][@stdlib/ndarray/base/assert/is-buffer-length-compatible] with the specified array shape, dimension strides, and index offset.
69+
70+
</section>
71+
72+
<!-- /.notes -->
73+
74+
<section class="examples">
75+
76+
## Examples
77+
78+
<!-- eslint no-undef: "error" -->
79+
80+
```javascript
81+
var zeroTo = require( '@stdlib/array/base/zero-to' );
82+
var numel = require( '@stdlib/ndarray/base/numel' );
83+
var shape2strides = require( '@stdlib/ndarray/base/shape2strides' );
84+
var strided2array2d = require( '@stdlib/array/base/strided2array2d' );
85+
86+
var shape = [ 3, 3 ];
87+
88+
var x = zeroTo( numel( shape ) );
89+
console.log( x );
90+
91+
var y = strided2array2d( x, shape, shape2strides( shape, 'row-major' ), 0 );
92+
console.log( y );
93+
94+
y = strided2array2d( x, shape, shape2strides( shape, 'column-major' ), 0 );
95+
console.log( y );
96+
```
97+
98+
</section>
99+
100+
<!-- /.examples -->
101+
102+
<!-- Section for related `stdlib` packages. Do not manually edit this section, as it is automatically populated. -->
103+
104+
<section class="related">
105+
106+
</section>
107+
108+
<!-- /.related -->
109+
110+
<!-- Section for all links. Make sure to keep an empty line after the `section` element and another before the `/section` close. -->
111+
112+
<section class="links">
113+
114+
[@stdlib/ndarray/base/assert/is-buffer-length-compatible]: https://github.com/stdlib-js/stdlib
115+
116+
</section>
117+
118+
<!-- /.links -->
Lines changed: 120 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,120 @@
1+
/**
2+
* @license Apache-2.0
3+
*
4+
* Copyright (c) 2023 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 isnan = require( '@stdlib/math/base/assert/is-nan' );
25+
var pow = require( '@stdlib/math/base/special/pow' );
26+
var floor = require( '@stdlib/math/base/special/floor' );
27+
var zeroTo = require( './../../../base/zero-to' );
28+
var numel = require( '@stdlib/ndarray/base/numel' );
29+
var orders = require( '@stdlib/ndarray/orders' );
30+
var shape2strides = require( '@stdlib/ndarray/base/shape2strides' );
31+
var pkg = require( './../package.json' ).name;
32+
var strided2array2d = require( './../lib' );
33+
34+
35+
// VARIABLES //
36+
37+
var ORDERS = orders();
38+
39+
40+
// FUNCTIONS //
41+
42+
/**
43+
* Creates a benchmark function.
44+
*
45+
* @private
46+
* @param {PositiveIntegerArray} shape - array shape
47+
* @param {string} order - memory layout order
48+
* @returns {Function} benchmark function
49+
*/
50+
function createBenchmark( shape, order ) {
51+
var strides = shape2strides( shape, order );
52+
var x = zeroTo( numel( shape ) );
53+
return benchmark;
54+
55+
/**
56+
* Benchmark function.
57+
*
58+
* @private
59+
* @param {Benchmark} b - benchmark instance
60+
*/
61+
function benchmark( b ) {
62+
var out;
63+
var i0;
64+
var i1;
65+
var i;
66+
67+
b.tic();
68+
for ( i = 0; i < b.iterations; i++ ) {
69+
out = strided2array2d( x, shape, strides, 0 );
70+
i1 = i % shape[ 0 ];
71+
i0 = i % shape[ 1 ];
72+
if ( isnan( out[ i1 ][ i0 ] ) ) {
73+
b.fail( 'should not return NaN' );
74+
}
75+
}
76+
b.toc();
77+
78+
i1 = i % shape[ 0 ];
79+
i0 = i % shape[ 1 ];
80+
if ( isnan( out[ i1 ][ i0 ] ) ) {
81+
b.fail( 'should not return NaN' );
82+
}
83+
b.pass( 'benchmark finished' );
84+
b.end();
85+
}
86+
}
87+
88+
89+
// MAIN //
90+
91+
/**
92+
* Main execution sequence.
93+
*
94+
* @private
95+
*/
96+
function main() {
97+
var min;
98+
var max;
99+
var ord;
100+
var sh;
101+
var N;
102+
var f;
103+
var i;
104+
var j;
105+
106+
min = 1; // 10^min
107+
max = 6; // 10^max
108+
109+
for ( j = 0; j < ORDERS.length; j++ ) {
110+
for ( i = min; i <= max; i++ ) {
111+
N = floor( pow( pow( 10, i ), 1.0/2.0 ) );
112+
sh = [ N, N ];
113+
ord = ORDERS[ j ];
114+
f = createBenchmark( sh, ord );
115+
bench( pkg+'::square_matrix:size='+numel( sh )+',order='+ord, f );
116+
}
117+
}
118+
}
119+
120+
main();

base/strided2array2d/docs/repl.txt

Lines changed: 30 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,30 @@
1+
2+
{{alias}}( x, shape, strides, offset )
3+
Converts a strided array to a two-dimensional nested array.
4+
5+
The function assumes that the input array is compatible with the specified
6+
array shape, dimension strides, and index offset.
7+
8+
Parameters
9+
----------
10+
x: ArrayLikeObject
11+
Input array.
12+
13+
shape: Array<integer>
14+
Array shape.
15+
16+
strides: Array<integer>
17+
Dimension strides.
18+
19+
offset: integer
20+
Index of the first indexed value in the input array.
21+
22+
Examples
23+
--------
24+
> var x = [ 1.0, 2.0, 3.0, 4.0 ];
25+
> var y = {{alias}}( x, [ 2, 2 ], [ 2, 1 ], 0 )
26+
[ [ 1.0, 2.0 ], [ 3.0, 4.0 ] ]
27+
28+
See Also
29+
--------
30+
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) 2023 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 { Array2D, ArrayLike } from '@stdlib/types/array';
24+
import { Shape2D, Strides2D } from '@stdlib/types/ndarray';
25+
26+
/**
27+
* Converts a strided array to a two-dimensional nested array.
28+
*
29+
* ## Notes
30+
*
31+
* - The function assumes that the input array is compatible with the specified array shape, dimension strides, and index offset.
32+
*
33+
* @param x - input array
34+
* @param shape - array shape
35+
* @param strides - dimension strides
36+
* @param offset - index of the first indexed value in the input array
37+
* @returns two-dimensional nested array
38+
*
39+
* @example
40+
* var x = [ 1, 2, 3, 4, 5, 6 ];
41+
*
42+
* var arr = strided2array2d( x, [ 3, 2 ], [ 2, 1 ], 0 );
43+
* // returns [ [ 1, 2 ], [ 3, 4 ], [ 5, 6 ] ]
44+
*
45+
* @example
46+
* var x = [ 1, 2, 3, 4, 5, 6 ];
47+
*
48+
* var arr = strided2array2d( x, [ 3, 2 ], [ 1, 3 ], 0 );
49+
* // returns [ [ 1, 4 ], [ 2, 5 ], [ 3, 6 ] ]
50+
*/
51+
declare function strided2array2d<T = unknown>( x: ArrayLike<T>, shape: Shape2D, strides: Strides2D, offset: number ): Array2D<T>;
52+
53+
54+
// EXPORTS //
55+
56+
export = strided2array2d;

0 commit comments

Comments
 (0)