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 Jan 21, 2024
1 parent 7cdd56a commit d8b6612
Show file tree
Hide file tree
Showing 13 changed files with 794 additions and 5 deletions.
148 changes: 148 additions & 0 deletions base/at3d/README.md
Original file line number Diff line number Diff line change
@@ -0,0 +1,148 @@
<!--
@license Apache-2.0
Copyright (c) 2024 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.
-->

# at3d

> Return an element from a three-dimensional nested array.
<!-- Section to include introductory text. Make sure to keep an empty line after the intro `section` element and another before the `/section` close. -->

<section class="intro">

</section>

<!-- /.intro -->

<!-- Package usage documentation. -->

<section class="usage">

## Usage

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

#### at3d( x, i0, i1, i2 )

Return an element from a three-dimensional nested array.

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

var out = at3d( x, 0, 0, 1 );
// returns 2

out = at3d( x, 0, 1, 0 );
// returns 3
```

The function accepts the following arguments:

- **x**: three-dimensional nested input array.
- **i0**: first dimension index.
- **i1**: second dimension index.
- **i2**: third dimension index.

</section>

<!-- /.usage -->

<!-- Package usage notes. Make sure to keep an empty line after the `section` element and another before the `/section` close. -->

<section class="notes">

## Notes

- Negative indices are resolved relative to the last element along the respective dimension, with the last element corresponding to `-1`.
- If provided out-of-bounds indices, the function always returns `undefined`.

</section>

<!-- /.notes -->

<!-- Package usage examples. -->

<section class="examples">

## Examples

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

```javascript
var papply = require( '@stdlib/utils/papply' );
var discreteUniform = require( '@stdlib/random/base/discrete-uniform' ).factory;
var filled3dBy = require( '@stdlib/array/base/filled3d-by' );
var ternary3d = require( '@stdlib/array/base/ternary3d' );
var zeros3d = require( '@stdlib/array/base/zeros3d' );
var at3d = require( '@stdlib/array/base/at3d' );

var shape = [ 3, 3, 3 ];

// Define a nested input array:
var x = filled3dBy( shape, discreteUniform( -100, 100 ) );
console.log( x );

// Define arrays containing random index values:
var i0 = filled3dBy( shape, discreteUniform( -shape[0], shape[0]-1 ) );
console.log( i0 );

var i1 = filled3dBy( shape, discreteUniform( -shape[1], shape[1]-1 ) );
console.log( i1 );

var i2 = filled3dBy( shape, discreteUniform( -shape[2], shape[2]-1 ) );
console.log( i2 );

// Define an output array:
var out = zeros3d( shape );
console.log( out );

// Fill the output array with randomly selected values from the input array:
ternary3d( [ i0, i1, i2, out ], shape, papply( at3d, x ) );
console.log( out );
```

</section>

<!-- /.examples -->

<!-- Section to include cited references. If references are included, add a horizontal rule *before* the section. Make sure to keep an empty line after the `section` element and another before the `/section` close. -->

<section class="references">

</section>

<!-- /.references -->

<!-- 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">

</section>

<!-- /.links -->
55 changes: 55 additions & 0 deletions base/at3d/benchmark/benchmark.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,55 @@
/**
* @license Apache-2.0
*
* Copyright (c) 2024 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 uniform = require( '@stdlib/random/base/uniform' ).factory;
var filled3dBy = require( './../../../base/filled3d-by' );
var isnan = require( '@stdlib/math/base/assert/is-nan' );
var pkg = require( './../package.json' ).name;
var at3d = require( './../lib' );


// MAIN //

bench( pkg, function benchmark( b ) {
var x;
var v;
var i;
var j;

x = filled3dBy( [ 10, 10, 10 ], uniform( 0.0, 10.0 ) );

b.tic();
for ( i = 0; i < b.iterations; i++ ) {
j = ( i%20 ) - 10;
v = at3d( x, j, j, j );
if ( v !== v ) {
b.fail( 'should not return NaN' );
}
}
b.toc();
if ( isnan( v ) ) {
b.fail( 'should not return NaN' );
}
b.pass( 'benchmark finished' );
b.end();
});
39 changes: 39 additions & 0 deletions base/at3d/docs/repl.txt
Original file line number Diff line number Diff line change
@@ -0,0 +1,39 @@

{{alias}}( x, i0, i1, i2 )
Returns an element from a three-dimensional nested array.

Negative indices are resolved relative to the last element along the
respective dimension, with the last element corresponding to `-1`.

If provided out-of-bounds indices, the function always returns `undefined`.

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

i0: integer
First dimension index.

i1: integer
Second dimension index.

i2: integer
Third dimension index.

Returns
-------
out: any
Element value.

Examples
--------
> var x = [ [ [ 1, 2 ], [ 3, 4 ] ] ];
> {{alias}}( x, 0, 0, 1 )
2
> {{alias}}( x, 0, 1, 0 )
3

See Also
--------

51 changes: 51 additions & 0 deletions base/at3d/docs/types/index.d.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,51 @@
/*
* @license Apache-2.0
*
* Copyright (c) 2024 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 } from '@stdlib/types/array';

/**
* Returns an element from a three-dimensional nested array.
*
* @param x - input array
* @param i0 - first dimension index
* @param i1 - second dimension index
* @param i2 - third dimension index
* @returns nested array element
*
* @example
* var x = [ [ [ 1, 2 ], [ 3, 4 ] ] ];
*
* var v = at3d( x, 0, 0, 1 );
* // returns 2
*
* v = at3d( x, 0, 1, 0 );
* // returns 3
*
* v = at3d( x, -1, -2, -2 );
* // returns 1
*/
declare function at3d<T = unknown>( x: Array3D<T>, i0: number, i1: number, i2: number ): T | void;


// EXPORTS //

export = at3d;
94 changes: 94 additions & 0 deletions base/at3d/docs/types/test.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,94 @@
/*
* @license Apache-2.0
*
* Copyright (c) 2024 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.
*/

import at3d = require( './index' );


// TESTS //

// The function returns an array element...
{
const x = [ [ [ 1, 2 ], [ 3, 4 ] ] ];

at3d( x, 0, 0, 0 ); // $ExpectType number | void
}

// The compiler throws an error if the function is provided a first argument which is not a nested array...
{
at3d( 'abc', 0, 0, 0 ); // $ExpectError
at3d( 5, 0, 0, 0 ); // $ExpectError
at3d( true, 0, 0, 0 ); // $ExpectError
at3d( false, 0, 0, 0 ); // $ExpectError
at3d( null, 0, 0, 0 ); // $ExpectError
at3d( void 0, 0, 0, 0 ); // $ExpectError
at3d( {}, 0, 0, 0 ); // $ExpectError
at3d( ( x: number ): number => x, 0, 0, 0 ); // $ExpectError
}

// The compiler throws an error if the function is provided a second argument which is not a number...
{
const x = [ [ [ 1, 2 ], [ 3, 4 ] ] ];

at3d( x, 'abc', 0, 0 ); // $ExpectError
at3d( x, true, 0, 0 ); // $ExpectError
at3d( x, false, 0, 0 ); // $ExpectError
at3d( x, null, 0, 0 ); // $ExpectError
at3d( x, void 0, 0, 0 ); // $ExpectError
at3d( x, [ '1' ], 0, 0 ); // $ExpectError
at3d( x, {}, 0, 0 ); // $ExpectError
at3d( x, ( x: number ): number => x, 0, 0 ); // $ExpectError
}

// The compiler throws an error if the function is provided a third argument which is not a number...
{
const x = [ [ [ 1, 2 ], [ 3, 4 ] ] ];

at3d( x, 0, 'abc', 0 ); // $ExpectError
at3d( x, 0, true, 0 ); // $ExpectError
at3d( x, 0, false, 0 ); // $ExpectError
at3d( x, 0, null, 0 ); // $ExpectError
at3d( x, 0, void 0, 0 ); // $ExpectError
at3d( x, 0, [ '1' ], 0 ); // $ExpectError
at3d( x, 0, {}, 0 ); // $ExpectError
at3d( x, 0, ( x: number ): number => x, 0 ); // $ExpectError
}

// The compiler throws an error if the function is provided a fourth argument which is not a number...
{
const x = [ [ [ 1, 2 ], [ 3, 4 ] ] ];

at3d( x, 0, 0, 'abc' ); // $ExpectError
at3d( x, 0, 0, true ); // $ExpectError
at3d( x, 0, 0, false ); // $ExpectError
at3d( x, 0, 0, null ); // $ExpectError
at3d( x, 0, 0, void 0 ); // $ExpectError
at3d( x, 0, 0, [ '1' ] ); // $ExpectError
at3d( x, 0, 0, {} ); // $ExpectError
at3d( x, 0, 0, ( x: number ): number => x ); // $ExpectError
}

// The compiler throws an error if the function is provided an unsupported number of arguments...
{
const x = [ [ [ 1, 2 ], [ 3, 4 ] ] ];

at3d(); // $ExpectError
at3d( x ); // $ExpectError
at3d( x, 0 ); // $ExpectError
at3d( x, 0, 0 ); // $ExpectError
at3d( x, 0, 0, 0, 0 ); // $ExpectError
}
Loading

0 comments on commit d8b6612

Please sign in to comment.