Skip to content

Commit 9e8bb89

Browse files
headlessNodekgryte
andauthored
feat: add ndarray/last
PR-URL: #11881 Co-authored-by: Athan Reines <kgryte@gmail.com> Reviewed-by: Athan Reines <kgryte@gmail.com> Closes: stdlib-js/metr-issue-tracker#264
1 parent e8183a7 commit 9e8bb89

12 files changed

Lines changed: 1571 additions & 0 deletions

File tree

Lines changed: 171 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,171 @@
1+
<!--
2+
3+
@license Apache-2.0
4+
5+
Copyright (c) 2026 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+
# last
22+
23+
> Return a read-only view of the last element (or subarray) along one or more [`ndarray`][@stdlib/ndarray/ctor] dimensions.
24+
25+
<!-- Section to include introductory text. Make sure to keep an empty line after the intro section element. -->
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 last = require( '@stdlib/ndarray/last' );
41+
```
42+
43+
#### last( x\[, options] )
44+
45+
Returns a read-only view of the last element (or subarray) along one or more [`ndarray`][@stdlib/ndarray/ctor] dimensions.
46+
47+
```javascript
48+
var array = require( '@stdlib/ndarray/array' );
49+
50+
var x = array( [ [ 1.0, 2.0 ], [ 3.0, 4.0 ] ] );
51+
// returns <ndarray>[ [ 1.0, 2.0 ], [ 3.0, 4.0 ] ]
52+
53+
var v = last( x );
54+
// returns <ndarray>[ 4.0 ]
55+
```
56+
57+
The function accepts the following arguments:
58+
59+
- **x**: input [`ndarray`][@stdlib/ndarray/ctor].
60+
- **options**: function options.
61+
62+
The function accepts the following `options`:
63+
64+
- **dims**: list of dimensions over which to perform the operation. If a dimension index is provided as an integer less than zero, the dimension index is resolved relative to the last dimension, with the last dimension corresponding to the value `-1`. By default, the function performs the operation over all dimensions.
65+
66+
To resolve the last element (or subarray) along one or more specific dimensions, provide a `dims` option:
67+
68+
```javascript
69+
var array = require( '@stdlib/ndarray/array' );
70+
71+
var x = array( [ [ 1.0, 2.0 ], [ 3.0, 4.0 ] ] );
72+
// returns <ndarray>[ [ 1.0, 2.0 ], [ 3.0, 4.0 ] ]
73+
74+
// Last column:
75+
var v = last( x, {
76+
'dims': [ -1 ]
77+
});
78+
// returns <ndarray>[ 2.0, 4.0 ]
79+
80+
// Last row:
81+
v = last( x, {
82+
'dims': [ -2 ]
83+
});
84+
// returns <ndarray>[ 3.0, 4.0 ]
85+
```
86+
87+
</section>
88+
89+
<!-- /.usage -->
90+
91+
<!-- Package usage notes. Make sure to keep an empty line after the `section` element and another before the `/section` close. -->
92+
93+
<section class="notes">
94+
95+
## Notes
96+
97+
- The function always returns a **read-only** view. To convert a returned view to a writable [`ndarray`][@stdlib/ndarray/ctor], copy the contents to a new [`ndarray`][@stdlib/ndarray/ctor] (e.g., via [`@stdlib/ndarray/copy`][@stdlib/ndarray/copy]).
98+
- If provided an empty `dims` array, the function returns a read-only view of the input [`ndarray`][@stdlib/ndarray/ctor].
99+
100+
</section>
101+
102+
<!-- /.notes -->
103+
104+
<!-- Package usage examples. -->
105+
106+
<section class="examples">
107+
108+
## Examples
109+
110+
<!-- eslint no-undef: "error" -->
111+
112+
```javascript
113+
var uniform = require( '@stdlib/random/uniform' );
114+
var ndarray2array = require( '@stdlib/ndarray/to-array' );
115+
var last = require( '@stdlib/ndarray/last' );
116+
117+
var x = uniform( [ 3, 3, 3 ], -10.0, 10.0 );
118+
console.log( ndarray2array( x ) );
119+
120+
// Last scalar element:
121+
var v = last( x );
122+
console.log( v.get() );
123+
124+
// Last columns along the innermost dimension:
125+
v = last( x, {
126+
'dims': [ -1 ]
127+
});
128+
console.log( ndarray2array( v ) );
129+
130+
// Last matrix along the outermost dimension:
131+
v = last( x, {
132+
'dims': [ 0 ]
133+
});
134+
console.log( ndarray2array( v ) );
135+
```
136+
137+
</section>
138+
139+
<!-- /.examples -->
140+
141+
<!-- 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. -->
142+
143+
<section class="references">
144+
145+
</section>
146+
147+
<!-- /.references -->
148+
149+
<!-- Section for related `stdlib` packages. Do not manually edit this section, as it is automatically populated. -->
150+
151+
<section class="related">
152+
153+
</section>
154+
155+
<!-- /.related -->
156+
157+
<!-- Section for all links. Make sure to keep an empty line after the `section` element and another before the `/section` close. -->
158+
159+
<section class="links">
160+
161+
[@stdlib/ndarray/ctor]: https://github.com/stdlib-js/stdlib/tree/develop/lib/node_modules/%40stdlib/ndarray/ctor
162+
163+
[@stdlib/ndarray/copy]: https://github.com/stdlib-js/stdlib/tree/develop/lib/node_modules/%40stdlib/ndarray/copy
164+
165+
<!-- <related-links> -->
166+
167+
<!-- </related-links> -->
168+
169+
</section>
170+
171+
<!-- /.links -->

0 commit comments

Comments
 (0)