Skip to content

Commit

Permalink
Auto-generated commit
Browse files Browse the repository at this point in the history
  • Loading branch information
stdlib-bot committed Nov 7, 2023
1 parent 9efe081 commit ccfe208
Show file tree
Hide file tree
Showing 13 changed files with 859 additions and 5 deletions.
9 changes: 9 additions & 0 deletions base/lib/index.js
Original file line number Diff line number Diff line change
Expand Up @@ -648,6 +648,15 @@ setReadOnly( ns, 'setter', require( './../../base/setter' ) );
*/
setReadOnly( ns, 'strided2array2d', require( './../../base/strided2array2d' ) );

/**
* @name strided2array3d
* @memberof ns
* @readonly
* @type {Function}
* @see {@link module:@stdlib/array/base/strided2array3d}
*/
setReadOnly( ns, 'strided2array3d', require( './../../base/strided2array3d' ) );

/**
* @name take
* @memberof ns
Expand Down
118 changes: 118 additions & 0 deletions base/strided2array3d/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.
-->

# strided2array3d

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

</section>

<!-- /.intro -->

<section class="usage">

## Usage

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

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

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

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

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

arr = strided2array3d( x, [ 1, 3, 2 ], [ 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 strided2array3d = require( '@stdlib/array/base/strided2array3d' );

var shape = [ 3, 3, 3 ];

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

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

y = strided2array3d( 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 -->
123 changes: 123 additions & 0 deletions base/strided2array3d/benchmark/benchmark.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,123 @@
/**
* @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( './../../../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 strided2array3d = 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 i;

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

i2 = i % shape[ 0 ];
i1 = i % shape[ 1 ];
i0 = i % shape[ 2 ];
if ( isnan( out[ 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/3.0 ) );
sh = [ 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 base/strided2array3d/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 three-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, 2, 2 ], [ 4, 2, 1 ], 0 )
[ [ [ 1.0, 2.0 ], [ 3.0, 4.0 ] ] ]

See Also
--------

56 changes: 56 additions & 0 deletions base/strided2array3d/docs/types/index.d.ts
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 { Array3D, ArrayLike } from '@stdlib/types/array';
import { Shape3D, Strides3D } from '@stdlib/types/ndarray';

/**
* Converts a strided array to a three-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 three-dimensional nested array
*
* @example
* var x = [ 1, 2, 3, 4, 5, 6 ];
*
* var arr = strided2array3d( x, [ 1, 3, 2 ], [ 4, 2, 1 ], 0 );
* // returns [ [ [ 1, 2 ], [ 3, 4 ], [ 5, 6 ] ] ]
*
* @example
* var x = [ 1, 2, 3, 4, 5, 6 ];
*
* var arr = strided2array3d( x, [ 1, 3, 2 ], [ 1, 1, 3 ], 0 );
* // returns [ [ [ 1, 4 ], [ 2, 5 ], [ 3, 6 ] ] ]
*/
declare function strided2array3d<T = unknown>( x: ArrayLike<T>, shape: Shape3D, strides: Strides3D, offset: number ): Array3D<T>;


// EXPORTS //

export = strided2array3d;
Loading

0 comments on commit ccfe208

Please sign in to comment.