Skip to content

Commit c553280

Browse files
committed
feat: add ndarray/at
1 parent 8f7bed2 commit c553280

File tree

10 files changed

+1186
-0
lines changed

10 files changed

+1186
-0
lines changed
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,179 @@
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+
# at
22+
23+
> Return an [`ndarray`][@stdlib/ndarray/ctor] element.
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 at = require( '@stdlib/ndarray/at' );
41+
```
42+
43+
#### at( x\[, ...indices] )
44+
45+
Returns an [`ndarray`][@stdlib/ndarray/ctor] element.
46+
47+
```javascript
48+
var array = require( '@stdlib/ndarray/array' );
49+
50+
var x = array( [ [ 1, 2 ], [ 3, 4 ] ] );
51+
// returns <ndarray>
52+
53+
var v = at( x, 0, 0 );
54+
// returns 1
55+
56+
v = at( x, 0, 1 );
57+
// returns 2
58+
59+
v = at( x, 1, 0 );
60+
// returns 3
61+
62+
v = at( x, 1, 1 );
63+
// returns 4
64+
```
65+
66+
The function accepts the following arguments:
67+
68+
- **x**: input [`ndarray`][@stdlib/ndarray/ctor].
69+
- **indices**: index arguments. The number of index arguments **must** equal the number of dimensions.
70+
71+
</section>
72+
73+
<!-- /.usage -->
74+
75+
<!-- Package usage notes. Make sure to keep an empty line after the `section` element and another before the `/section` close. -->
76+
77+
<section class="notes">
78+
79+
## Notes
80+
81+
- If provided out-of-bounds indices, the function always returns `undefined`.
82+
83+
```javascript
84+
var array = require( '@stdlib/ndarray/array' );
85+
86+
var x = array( [ [ 1, 2 ], [ 3, 4 ] ] );
87+
// returns <ndarray>
88+
89+
var v = at( x, 10, 20 );
90+
// returns undefined
91+
```
92+
93+
- Negative indices are resolved relative to the last element along the respective dimension, with the last element corresponding to `-1`.
94+
95+
```javascript
96+
var array = require( '@stdlib/ndarray/array' );
97+
98+
var x = array( [ [ 1, 2 ], [ 3, 4 ] ] );
99+
// returns <ndarray>
100+
101+
var v = at( x, -1, -1 );
102+
// returns 4
103+
104+
v = at( x, -2, -2 );
105+
// returns 1
106+
```
107+
108+
</section>
109+
110+
<!-- /.notes -->
111+
112+
<!-- Package usage examples. -->
113+
114+
<section class="examples">
115+
116+
## Examples
117+
118+
<!-- eslint no-undef: "error" -->
119+
120+
<!-- eslint-disable new-cap -->
121+
122+
```javascript
123+
var cartesianProduct = require( '@stdlib/array/cartesian-product' );
124+
var zeroTo = require( '@stdlib/array/zero-to' );
125+
var discreteUniform = require( '@stdlib/random/array/discrete-uniform' );
126+
var array = require( '@stdlib/ndarray/array' );
127+
var at = require( '@stdlib/ndarray/at' );
128+
129+
// Define a two-dimensional array:
130+
var shape = [ 5, 5 ];
131+
var x = array( discreteUniform( 25, -100, 100 ), {
132+
'shape': shape
133+
});
134+
135+
// Define lists of dimension indices:
136+
var i0 = zeroTo( shape[ 0 ], 'generic' );
137+
var i1 = zeroTo( shape[ 1 ], 'generic' );
138+
139+
// Create a list of index pairs:
140+
var indices = cartesianProduct( i0, i1 );
141+
142+
// Print array contents...
143+
var idx;
144+
var i;
145+
for ( i = 0; i < x.length; i++ ) {
146+
idx = indices[ i ];
147+
console.log( 'x[%d,%d] = %d', idx[ 0 ], idx[ 1 ], at( x, idx[ 0 ], idx[ 1 ] ) );
148+
}
149+
```
150+
151+
</section>
152+
153+
<!-- /.examples -->
154+
155+
<!-- 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. -->
156+
157+
<section class="references">
158+
159+
</section>
160+
161+
<!-- /.references -->
162+
163+
<!-- Section for related `stdlib` packages. Do not manually edit this section, as it is automatically populated. -->
164+
165+
<section class="related">
166+
167+
</section>
168+
169+
<!-- /.related -->
170+
171+
<!-- Section for all links. Make sure to keep an empty line after the `section` element and another before the `/section` close. -->
172+
173+
<section class="links">
174+
175+
[@stdlib/ndarray/ctor]: https://github.com/stdlib-js/stdlib/tree/develop/lib/node_modules/%40stdlib/ndarray/ctor
176+
177+
</section>
178+
179+
<!-- /.links -->
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,56 @@
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 array = require( '@stdlib/ndarray/array' );
28+
var pkg = require( './../package.json' ).name;
29+
var at = require( './../lib' );
30+
31+
32+
// MAIN //
33+
34+
bench( pkg, function benchmark( b ) {
35+
var x;
36+
var v;
37+
var i;
38+
var j;
39+
40+
x = array( filled2dBy( [ 10, 10 ], uniform( 0.0, 10.0 ) ) );
41+
42+
b.tic();
43+
for ( i = 0; i < b.iterations; i++ ) {
44+
j = ( i%20 ) - 10;
45+
v = at( x, j, j );
46+
if ( v !== v ) {
47+
b.fail( 'should not return NaN' );
48+
}
49+
}
50+
b.toc();
51+
if ( isnan( v ) ) {
52+
b.fail( 'should not return NaN' );
53+
}
54+
b.pass( 'benchmark finished' );
55+
b.end();
56+
});
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,34 @@
1+
2+
{{alias}}( x[, ...indices] )
3+
Returns an ndarray element.
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: ndarray
13+
Input ndarray.
14+
15+
indices: ...integer (optional)
16+
Index arguments. The number of index arguments must equal the number of
17+
dimensions.
18+
19+
Returns
20+
-------
21+
out: any
22+
Element value.
23+
24+
Examples
25+
--------
26+
> var x = {{alias:@stdlib/ndarray/array}}( [ [ 1, 2 ], [ 3, 4 ] ] );
27+
> {{alias}}( x, 0, 1 )
28+
2
29+
> {{alias}}( x, 1, 0 )
30+
3
31+
32+
See Also
33+
--------
34+

0 commit comments

Comments
 (0)