Skip to content

Commit

Permalink
feat: add array/base/strided2array5d
Browse files Browse the repository at this point in the history
  • Loading branch information
kgryte committed Nov 7, 2023
1 parent bbf1876 commit 3a7f770
Show file tree
Hide file tree
Showing 10 changed files with 955 additions and 0 deletions.
118 changes: 118 additions & 0 deletions lib/node_modules/@stdlib/array/base/strided2array5d/README.md
Original file line number Diff line number Diff line change
@@ -0,0 +1,118 @@
<!--
@license Apache-2.0
Copyright (c) 2023 The Stdlib Authors.
Licensed under the Apache License, Version 2.0 (the "License");
you may not use this file except in compliance with the License.
You may obtain a copy of the License at
http://www.apache.org/licenses/LICENSE-2.0
Unless required by applicable law or agreed to in writing, software
distributed under the License is distributed on an "AS IS" BASIS,
WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
See the License for the specific language governing permissions and
limitations under the License.
-->

# strided2array5d

> Convert a strided array to a five-dimensional nested array.
<section class="intro">

</section>

<!-- /.intro -->

<section class="usage">

## Usage

```javascript
var strided2array5d = require( '@stdlib/array/base/strided2array5d' );
```

#### strided2array5d( x, shape, strides, offset )

Converts a strided array to a five-dimensional nested array.

```javascript
var x = [ 1, 2, 3, 4, 5, 6 ];

var arr = strided2array5d( x, [ 1, 1, 1, 3, 2 ], [ 6, 6, 6, 2, 1 ], 0 );
// returns [ [ [ [ [ 1, 2 ], [ 3, 4 ], [ 5, 6 ] ] ] ] ]

arr = strided2array5d( x, [ 1, 1, 1, 3, 2 ], [ 1, 1, 1, 1, 3 ], 0 );
// returns [ [ [ [ [ 1, 4 ], [ 2, 5 ], [ 3, 6 ] ] ] ] ]
```

The function accepts the following arguments:

- **x**: input array.
- **shape**: array shape.
- **strides**: dimension strides.
- **offset**: index of the first indexed value in the input array.

</section>

<!-- /.usage -->

<section class="notes">

## Notes

- 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.

</section>

<!-- /.notes -->

<section class="examples">

## Examples

<!-- eslint no-undef: "error" -->

```javascript
var zeroTo = require( '@stdlib/array/base/zero-to' );
var numel = require( '@stdlib/ndarray/base/numel' );
var shape2strides = require( '@stdlib/ndarray/base/shape2strides' );
var strided2array5d = require( '@stdlib/array/base/strided2array5d' );

var shape = [ 1, 1, 3, 3, 3 ];

var x = zeroTo( numel( shape ) );
console.log( x );

var y = strided2array5d( x, shape, shape2strides( shape, 'row-major' ), 0 );
console.log( y );

y = strided2array5d( x, shape, shape2strides( shape, 'column-major' ), 0 );
console.log( y );
```

</section>

<!-- /.examples -->

<!-- Section for related `stdlib` packages. Do not manually edit this section, as it is automatically populated. -->

<section class="related">

</section>

<!-- /.related -->

<!-- Section for all links. Make sure to keep an empty line after the `section` element and another before the `/section` close. -->

<section class="links">

[@stdlib/ndarray/base/assert/is-buffer-length-compatible]: https://github.com/stdlib-js/stdlib

</section>

<!-- /.links -->
Original file line number Diff line number Diff line change
@@ -0,0 +1,129 @@
/**
* @license Apache-2.0
*
* Copyright (c) 2023 The Stdlib Authors.
*
* Licensed under the Apache License, Version 2.0 (the "License");
* you may not use this file except in compliance with the License.
* You may obtain a copy of the License at
*
* http://www.apache.org/licenses/LICENSE-2.0
*
* Unless required by applicable law or agreed to in writing, software
* distributed under the License is distributed on an "AS IS" BASIS,
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
* See the License for the specific language governing permissions and
* limitations under the License.
*/

'use strict';

// MODULES //

var bench = require( '@stdlib/bench' );
var isnan = require( '@stdlib/math/base/assert/is-nan' );
var pow = require( '@stdlib/math/base/special/pow' );
var floor = require( '@stdlib/math/base/special/floor' );
var zeroTo = require( '@stdlib/array/base/zero-to' );
var numel = require( '@stdlib/ndarray/base/numel' );
var orders = require( '@stdlib/ndarray/orders' );
var shape2strides = require( '@stdlib/ndarray/base/shape2strides' );
var pkg = require( './../package.json' ).name;
var strided2array5d = require( './../lib' );


// VARIABLES //

var ORDERS = orders();


// FUNCTIONS //

/**
* Creates a benchmark function.
*
* @private
* @param {PositiveIntegerArray} shape - array shape
* @param {string} order - memory layout order
* @returns {Function} benchmark function
*/
function createBenchmark( shape, order ) {
var strides = shape2strides( shape, order );
var x = zeroTo( numel( shape ) );
return benchmark;

/**
* Benchmark function.
*
* @private
* @param {Benchmark} b - benchmark instance
*/
function benchmark( b ) {
var out;
var i0;
var i1;
var i2;
var i3;
var i4;
var i;

b.tic();
for ( i = 0; i < b.iterations; i++ ) {
out = strided2array5d( x, shape, strides, 0 );
i4 = i % shape[ 0 ];
i3 = i % shape[ 1 ];
i2 = i % shape[ 2 ];
i1 = i % shape[ 3 ];
i0 = i % shape[ 4 ];
if ( isnan( out[ i4 ][ i3 ][ i2 ][ i1 ][ i0 ] ) ) {
b.fail( 'should not return NaN' );
}
}
b.toc();

i4 = i % shape[ 0 ];
i3 = i % shape[ 1 ];
i2 = i % shape[ 2 ];
i1 = i % shape[ 3 ];
i0 = i % shape[ 4 ];
if ( isnan( out[ i4 ][ i3 ][ i2 ][ i1 ][ i0 ] ) ) {
b.fail( 'should not return NaN' );
}
b.pass( 'benchmark finished' );
b.end();
}
}


// MAIN //

/**
* Main execution sequence.
*
* @private
*/
function main() {
var min;
var max;
var ord;
var sh;
var N;
var f;
var i;
var j;

min = 1; // 10^min
max = 6; // 10^max

for ( j = 0; j < ORDERS.length; j++ ) {
for ( i = min; i <= max; i++ ) {
N = floor( pow( pow( 10, i ), 1.0/5.0 ) );
sh = [ N, N, N, N, N ];
ord = ORDERS[ j ];
f = createBenchmark( sh, ord );
bench( pkg+'::equidimensional:size='+numel( sh )+',order='+ord, f );
}
}
}

main();
30 changes: 30 additions & 0 deletions lib/node_modules/@stdlib/array/base/strided2array5d/docs/repl.txt
Original file line number Diff line number Diff line change
@@ -0,0 +1,30 @@

{{alias}}( x, shape, strides, offset )
Converts a strided array to a five-dimensional nested array.

The function assumes that the input array is compatible with the specified
array shape, dimension strides, and index offset.

Parameters
----------
x: ArrayLikeObject
Input array.

shape: Array<integer>
Array shape.

strides: Array<integer>
Dimension strides.

offset: integer
Index of the first indexed value in the input array.

Examples
--------
> var x = [ 1.0, 2.0, 3.0, 4.0 ];
> var y = {{alias}}( x, [ 1, 1, 1, 2, 2 ], [ 4, 4, 4, 2, 1 ], 0 )
[ [ [ [ [ 1.0, 2.0 ], [ 3.0, 4.0 ] ] ] ] ]

See Also
--------

Original file line number Diff line number Diff line change
@@ -0,0 +1,56 @@
/*
* @license Apache-2.0
*
* Copyright (c) 2023 The Stdlib Authors.
*
* Licensed under the Apache License, Version 2.0 (the "License");
* you may not use this file except in compliance with the License.
* You may obtain a copy of the License at
*
* http://www.apache.org/licenses/LICENSE-2.0
*
* Unless required by applicable law or agreed to in writing, software
* distributed under the License is distributed on an "AS IS" BASIS,
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
* See the License for the specific language governing permissions and
* limitations under the License.
*/

// TypeScript Version: 4.1

/// <reference types="@stdlib/types"/>

import { Array5D, ArrayLike } from '@stdlib/types/array';
import { Shape5D, Strides5D } from '@stdlib/types/ndarray';

/**
* Converts a strided array to a five-dimensional nested array.
*
* ## Notes
*
* - The function assumes that the input array is compatible with the specified array shape, dimension strides, and index offset.
*
* @param x - input array
* @param shape - array shape
* @param strides - dimension strides
* @param offset - index of the first indexed value in the input array
* @returns five-dimensional nested array
*
* @example
* var x = [ 1, 2, 3, 4, 5, 6 ];
*
* var arr = strided2array5d( x, [ 1, 1, 1, 3, 2 ], [ 6, 6, 6, 2, 1 ], 0 );
* // returns [ [ [ [ [ 1, 2 ], [ 3, 4 ], [ 5, 6 ] ] ] ] ]
*
* @example
* var x = [ 1, 2, 3, 4, 5, 6 ];
*
* var arr = strided2array5d( x, [ 1, 1, 1, 3, 2 ], [ 1, 1, 1, 1, 3 ], 0 );
* // returns [ [ [ [ [ 1, 4 ], [ 2, 5 ], [ 3, 6 ] ] ] ] ]
*/
declare function strided2array5d<T = unknown>( x: ArrayLike<T>, shape: Shape5D, strides: Strides5D, offset: number ): Array5D<T>;


// EXPORTS //

export = strided2array5d;
Loading

0 comments on commit 3a7f770

Please sign in to comment.