Skip to content

Commit df2c439

Browse files
committed
feat: add array/base/at3d
1 parent 6d5a54c commit df2c439

File tree

10 files changed

+780
-0
lines changed

10 files changed

+780
-0
lines changed
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,148 @@
1+
<!--
2+
3+
@license Apache-2.0
4+
5+
Copyright (c) 2024 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+
# at3d
22+
23+
> Return an element from a three-dimensional nested array.
24+
25+
<!-- Section to include introductory text. Make sure to keep an empty line after the intro `section` element and another before the `/section` close. -->
26+
27+
<section class="intro">
28+
29+
</section>
30+
31+
<!-- /.intro -->
32+
33+
<!-- Package usage documentation. -->
34+
35+
<section class="usage">
36+
37+
## Usage
38+
39+
```javascript
40+
var at3d = require( '@stdlib/array/base/at3d' );
41+
```
42+
43+
#### at3d( x, i0, i1, i2 )
44+
45+
Return an element from a three-dimensional nested array.
46+
47+
```javascript
48+
var x = [ [ [ 1, 2 ], [ 3, 4 ] ] ];
49+
50+
var out = at3d( x, 0, 0, 1 );
51+
// returns 2
52+
53+
out = at3d( x, 0, 1, 0 );
54+
// returns 3
55+
```
56+
57+
The function accepts the following arguments:
58+
59+
- **x**: three-dimensional nested input array.
60+
- **i0**: first dimension index.
61+
- **i1**: second dimension index.
62+
- **i2**: third dimension index.
63+
64+
</section>
65+
66+
<!-- /.usage -->
67+
68+
<!-- Package usage notes. Make sure to keep an empty line after the `section` element and another before the `/section` close. -->
69+
70+
<section class="notes">
71+
72+
## Notes
73+
74+
- Negative indices are resolved relative to the last element along the respective dimension, with the last element corresponding to `-1`.
75+
- If provided out-of-bounds indices, the function always returns `undefined`.
76+
77+
</section>
78+
79+
<!-- /.notes -->
80+
81+
<!-- Package usage examples. -->
82+
83+
<section class="examples">
84+
85+
## Examples
86+
87+
<!-- eslint no-undef: "error" -->
88+
89+
```javascript
90+
var papply = require( '@stdlib/utils/papply' );
91+
var discreteUniform = require( '@stdlib/random/base/discrete-uniform' ).factory;
92+
var filled3dBy = require( '@stdlib/array/base/filled3d-by' );
93+
var ternary3d = require( '@stdlib/array/base/ternary3d' );
94+
var zeros3d = require( '@stdlib/array/base/zeros3d' );
95+
var at3d = require( '@stdlib/array/base/at3d' );
96+
97+
var shape = [ 3, 3, 3 ];
98+
99+
// Define a nested input array:
100+
var x = filled3dBy( shape, discreteUniform( -100, 100 ) );
101+
console.log( x );
102+
103+
// Define arrays containing random index values:
104+
var i0 = filled3dBy( shape, discreteUniform( -shape[0], shape[0]-1 ) );
105+
console.log( i0 );
106+
107+
var i1 = filled3dBy( shape, discreteUniform( -shape[1], shape[1]-1 ) );
108+
console.log( i1 );
109+
110+
var i2 = filled3dBy( shape, discreteUniform( -shape[2], shape[2]-1 ) );
111+
console.log( i2 );
112+
113+
// Define an output array:
114+
var out = zeros3d( shape );
115+
console.log( out );
116+
117+
// Fill the output array with randomly selected values from the input array:
118+
ternary3d( [ i0, i1, i2, out ], shape, papply( at3d, x ) );
119+
console.log( out );
120+
```
121+
122+
</section>
123+
124+
<!-- /.examples -->
125+
126+
<!-- 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. -->
127+
128+
<section class="references">
129+
130+
</section>
131+
132+
<!-- /.references -->
133+
134+
<!-- Section for related `stdlib` packages. Do not manually edit this section, as it is automatically populated. -->
135+
136+
<section class="related">
137+
138+
</section>
139+
140+
<!-- /.related -->
141+
142+
<!-- Section for all links. Make sure to keep an empty line after the `section` element and another before the `/section` close. -->
143+
144+
<section class="links">
145+
146+
</section>
147+
148+
<!-- /.links -->
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,55 @@
1+
/**
2+
* @license Apache-2.0
3+
*
4+
* Copyright (c) 2024 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 uniform = require( '@stdlib/random/base/uniform' ).factory;
25+
var filled3dBy = require( '@stdlib/array/base/filled3d-by' );
26+
var isnan = require( '@stdlib/math/base/assert/is-nan' );
27+
var pkg = require( './../package.json' ).name;
28+
var at3d = require( './../lib' );
29+
30+
31+
// MAIN //
32+
33+
bench( pkg, function benchmark( b ) {
34+
var x;
35+
var v;
36+
var i;
37+
var j;
38+
39+
x = filled3dBy( [ 10, 10, 10 ], uniform( 0.0, 10.0 ) );
40+
41+
b.tic();
42+
for ( i = 0; i < b.iterations; i++ ) {
43+
j = ( i%20 ) - 10;
44+
v = at3d( x, j, j, j );
45+
if ( v !== v ) {
46+
b.fail( 'should not return NaN' );
47+
}
48+
}
49+
b.toc();
50+
if ( isnan( v ) ) {
51+
b.fail( 'should not return NaN' );
52+
}
53+
b.pass( 'benchmark finished' );
54+
b.end();
55+
});
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,39 @@
1+
2+
{{alias}}( x, i0, i1, i2 )
3+
Returns an element from a three-dimensional nested array.
4+
5+
Negative indices are resolved relative to the last element along the
6+
respective dimension, with the last element corresponding to `-1`.
7+
8+
If provided out-of-bounds indices, the function always returns `undefined`.
9+
10+
Parameters
11+
----------
12+
x: ArrayLikeObject
13+
Input nested array.
14+
15+
i0: integer
16+
First dimension index.
17+
18+
i1: integer
19+
Second dimension index.
20+
21+
i2: integer
22+
Third dimension index.
23+
24+
Returns
25+
-------
26+
out: any
27+
Element value.
28+
29+
Examples
30+
--------
31+
> var x = [ [ [ 1, 2 ], [ 3, 4 ] ] ];
32+
> {{alias}}( x, 0, 0, 1 )
33+
2
34+
> {{alias}}( x, 0, 1, 0 )
35+
3
36+
37+
See Also
38+
--------
39+
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,51 @@
1+
/*
2+
* @license Apache-2.0
3+
*
4+
* Copyright (c) 2024 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 { Array3D } from '@stdlib/types/array';
24+
25+
/**
26+
* Returns an element from a three-dimensional nested array.
27+
*
28+
* @param x - input array
29+
* @param i0 - first dimension index
30+
* @param i1 - second dimension index
31+
* @param i2 - third dimension index
32+
* @returns nested array element
33+
*
34+
* @example
35+
* var x = [ [ [ 1, 2 ], [ 3, 4 ] ] ];
36+
*
37+
* var v = at3d( x, 0, 0, 1 );
38+
* // returns 2
39+
*
40+
* v = at3d( x, 0, 1, 0 );
41+
* // returns 3
42+
*
43+
* v = at3d( x, -1, -2, -2 );
44+
* // returns 1
45+
*/
46+
declare function at3d<T = unknown>( x: Array3D<T>, i0: number, i1: number, i2: number ): T | void;
47+
48+
49+
// EXPORTS //
50+
51+
export = at3d;
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,94 @@
1+
/*
2+
* @license Apache-2.0
3+
*
4+
* Copyright (c) 2024 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+
import at3d = require( './index' );
20+
21+
22+
// TESTS //
23+
24+
// The function returns an array element...
25+
{
26+
const x = [ [ [ 1, 2 ], [ 3, 4 ] ] ];
27+
28+
at3d( x, 0, 0, 0 ); // $ExpectType number | void
29+
}
30+
31+
// The compiler throws an error if the function is provided a first argument which is not a nested array...
32+
{
33+
at3d( 'abc', 0, 0, 0 ); // $ExpectError
34+
at3d( 5, 0, 0, 0 ); // $ExpectError
35+
at3d( true, 0, 0, 0 ); // $ExpectError
36+
at3d( false, 0, 0, 0 ); // $ExpectError
37+
at3d( null, 0, 0, 0 ); // $ExpectError
38+
at3d( void 0, 0, 0, 0 ); // $ExpectError
39+
at3d( {}, 0, 0, 0 ); // $ExpectError
40+
at3d( ( x: number ): number => x, 0, 0, 0 ); // $ExpectError
41+
}
42+
43+
// The compiler throws an error if the function is provided a second argument which is not a number...
44+
{
45+
const x = [ [ [ 1, 2 ], [ 3, 4 ] ] ];
46+
47+
at3d( x, 'abc', 0, 0 ); // $ExpectError
48+
at3d( x, true, 0, 0 ); // $ExpectError
49+
at3d( x, false, 0, 0 ); // $ExpectError
50+
at3d( x, null, 0, 0 ); // $ExpectError
51+
at3d( x, void 0, 0, 0 ); // $ExpectError
52+
at3d( x, [ '1' ], 0, 0 ); // $ExpectError
53+
at3d( x, {}, 0, 0 ); // $ExpectError
54+
at3d( x, ( x: number ): number => x, 0, 0 ); // $ExpectError
55+
}
56+
57+
// The compiler throws an error if the function is provided a third argument which is not a number...
58+
{
59+
const x = [ [ [ 1, 2 ], [ 3, 4 ] ] ];
60+
61+
at3d( x, 0, 'abc', 0 ); // $ExpectError
62+
at3d( x, 0, true, 0 ); // $ExpectError
63+
at3d( x, 0, false, 0 ); // $ExpectError
64+
at3d( x, 0, null, 0 ); // $ExpectError
65+
at3d( x, 0, void 0, 0 ); // $ExpectError
66+
at3d( x, 0, [ '1' ], 0 ); // $ExpectError
67+
at3d( x, 0, {}, 0 ); // $ExpectError
68+
at3d( x, 0, ( x: number ): number => x, 0 ); // $ExpectError
69+
}
70+
71+
// The compiler throws an error if the function is provided a fourth argument which is not a number...
72+
{
73+
const x = [ [ [ 1, 2 ], [ 3, 4 ] ] ];
74+
75+
at3d( x, 0, 0, 'abc' ); // $ExpectError
76+
at3d( x, 0, 0, true ); // $ExpectError
77+
at3d( x, 0, 0, false ); // $ExpectError
78+
at3d( x, 0, 0, null ); // $ExpectError
79+
at3d( x, 0, 0, void 0 ); // $ExpectError
80+
at3d( x, 0, 0, [ '1' ] ); // $ExpectError
81+
at3d( x, 0, 0, {} ); // $ExpectError
82+
at3d( x, 0, 0, ( x: number ): number => x ); // $ExpectError
83+
}
84+
85+
// The compiler throws an error if the function is provided an unsupported number of arguments...
86+
{
87+
const x = [ [ [ 1, 2 ], [ 3, 4 ] ] ];
88+
89+
at3d(); // $ExpectError
90+
at3d( x ); // $ExpectError
91+
at3d( x, 0 ); // $ExpectError
92+
at3d( x, 0, 0 ); // $ExpectError
93+
at3d( x, 0, 0, 0, 0 ); // $ExpectError
94+
}

0 commit comments

Comments
 (0)