-
-
Notifications
You must be signed in to change notification settings - Fork 0
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
- Loading branch information
1 parent
9efe081
commit ccfe208
Showing
13 changed files
with
859 additions
and
5 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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 --> |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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(); |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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 | ||
-------- | ||
|
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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; |
Oops, something went wrong.