Skip to content

Commit ce37093

Browse files
committed
feat: add array/base/at2d
1 parent 2e0791d commit ce37093

File tree

10 files changed

+740
-0
lines changed

10 files changed

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

0 commit comments

Comments
 (0)