Skip to content

Commit f476848

Browse files
committed
feat: add array/base/at4d
1 parent bce88f2 commit f476848

File tree

10 files changed

+807
-0
lines changed

10 files changed

+807
-0
lines changed
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,152 @@
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+
# at4d
22+
23+
> Return an element from a four-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 at4d = require( '@stdlib/array/base/at4d' );
41+
```
42+
43+
#### at4d( x, i0, i1, i2, i3 )
44+
45+
Return an element from a four-dimensional nested array.
46+
47+
```javascript
48+
var x = [ [ [ [ 1, 2 ], [ 3, 4 ] ] ] ];
49+
50+
var out = at4d( x, 0, 0, 0, 1 );
51+
// returns 2
52+
53+
out = at4d( x, 0, 0, 1, 0 );
54+
// returns 3
55+
```
56+
57+
The function accepts the following arguments:
58+
59+
- **x**: four-dimensional nested input array.
60+
- **i0**: first dimension index.
61+
- **i1**: second dimension index.
62+
- **i2**: third dimension index.
63+
- **i3**: fourth dimension index.
64+
65+
</section>
66+
67+
<!-- /.usage -->
68+
69+
<!-- Package usage notes. Make sure to keep an empty line after the `section` element and another before the `/section` close. -->
70+
71+
<section class="notes">
72+
73+
## Notes
74+
75+
- Negative indices are resolved relative to the last element along the respective dimension, with the last element corresponding to `-1`.
76+
- If provided out-of-bounds indices, the function always returns `undefined`.
77+
78+
</section>
79+
80+
<!-- /.notes -->
81+
82+
<!-- Package usage examples. -->
83+
84+
<section class="examples">
85+
86+
## Examples
87+
88+
<!-- eslint no-undef: "error" -->
89+
90+
```javascript
91+
var papply = require( '@stdlib/utils/papply' );
92+
var discreteUniform = require( '@stdlib/random/base/discrete-uniform' ).factory;
93+
var filled4dBy = require( '@stdlib/array/base/filled4d-by' );
94+
var quaternary4d = require( '@stdlib/array/base/quaternary4d' );
95+
var zeros4d = require( '@stdlib/array/base/zeros4d' );
96+
var at4d = require( '@stdlib/array/base/at4d' );
97+
98+
var shape = [ 2, 2, 4, 4 ];
99+
100+
// Define a nested input array:
101+
var x = filled4dBy( shape, discreteUniform( -100, 100 ) );
102+
console.log( x );
103+
104+
// Define arrays containing random index values:
105+
var i0 = filled4dBy( shape, discreteUniform( -shape[0], shape[0]-1 ) );
106+
console.log( i0 );
107+
108+
var i1 = filled4dBy( shape, discreteUniform( -shape[1], shape[1]-1 ) );
109+
console.log( i1 );
110+
111+
var i2 = filled4dBy( shape, discreteUniform( -shape[2], shape[2]-1 ) );
112+
console.log( i2 );
113+
114+
var i3 = filled4dBy( shape, discreteUniform( -shape[3], shape[3]-1 ) );
115+
console.log( i3 );
116+
117+
// Define an output array:
118+
var out = zeros4d( shape );
119+
console.log( out );
120+
121+
// Fill the output array with randomly selected values from the input array:
122+
quaternary4d( [ i0, i1, i2, i3, out ], shape, papply( at4d, x ) );
123+
console.log( out );
124+
```
125+
126+
</section>
127+
128+
<!-- /.examples -->
129+
130+
<!-- 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. -->
131+
132+
<section class="references">
133+
134+
</section>
135+
136+
<!-- /.references -->
137+
138+
<!-- Section for related `stdlib` packages. Do not manually edit this section, as it is automatically populated. -->
139+
140+
<section class="related">
141+
142+
</section>
143+
144+
<!-- /.related -->
145+
146+
<!-- Section for all links. Make sure to keep an empty line after the `section` element and another before the `/section` close. -->
147+
148+
<section class="links">
149+
150+
</section>
151+
152+
<!-- /.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 filled4dBy = require( '@stdlib/array/base/filled4d-by' );
26+
var isnan = require( '@stdlib/math/base/assert/is-nan' );
27+
var pkg = require( './../package.json' ).name;
28+
var at4d = 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 = filled4dBy( [ 10, 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 = at4d( x, j, 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,42 @@
1+
2+
{{alias}}( x, i0, i1, i2, i3 )
3+
Returns an element from a four-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+
i3: integer
25+
Fourth dimension index.
26+
27+
Returns
28+
-------
29+
out: any
30+
Element value.
31+
32+
Examples
33+
--------
34+
> var x = [ [ [ [ 1, 2 ], [ 3, 4 ] ] ] ];
35+
> {{alias}}( x, 0, 0, 0, 1 )
36+
2
37+
> {{alias}}( x, 0, 0, 1, 0 )
38+
3
39+
40+
See Also
41+
--------
42+
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,52 @@
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 { Array4D } from '@stdlib/types/array';
24+
25+
/**
26+
* Returns an element from a four-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+
* @param i3 - fourth dimension index
33+
* @returns nested array element
34+
*
35+
* @example
36+
* var x = [ [ [ [ 1, 2 ], [ 3, 4 ] ] ] ];
37+
*
38+
* var v = at4d( x, 0, 0, 0, 1 );
39+
* // returns 2
40+
*
41+
* v = at4d( x, 0, 0, 1, 0 );
42+
* // returns 3
43+
*
44+
* v = at4d( x, -1, -1, -2, -2 );
45+
* // returns 1
46+
*/
47+
declare function at4d<T = unknown>( x: Array4D<T>, i0: number, i1: number, i2: number, i3: number ): T | void;
48+
49+
50+
// EXPORTS //
51+
52+
export = at4d;

0 commit comments

Comments
 (0)