Skip to content

Commit 7840656

Browse files
committed
feat: add ndarray/last
--- type: pre_commit_static_analysis_report description: Results of running static analysis checks when committing changes. report: - task: lint_filenames status: passed - task: lint_editorconfig status: passed - task: lint_markdown status: passed - task: lint_package_json status: passed - task: lint_repl_help status: passed - task: lint_javascript_src status: passed - task: lint_javascript_cli status: na - task: lint_javascript_examples status: passed - task: lint_javascript_tests status: passed - task: lint_javascript_benchmarks status: passed - task: lint_python status: na - task: lint_r status: na - task: lint_c_src status: na - task: lint_c_examples status: na - task: lint_c_benchmarks status: na - task: lint_c_tests_fixtures status: na - task: lint_shell status: na - task: lint_typescript_declarations status: passed - task: lint_typescript_tests status: passed - task: lint_license_headers status: passed ---
1 parent 642af21 commit 7840656

12 files changed

Lines changed: 1562 additions & 0 deletions

File tree

Lines changed: 173 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,173 @@
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. By default, the function performs the operation over all dimensions and thus returns the last element of the input [`ndarray`][@stdlib/ndarray/ctor] as a zero-dimensional [`ndarray`][@stdlib/ndarray/ctor].
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+
- By default, the function performs the operation over all dimensions and thus returns the last element of the input [`ndarray`][@stdlib/ndarray/ctor] as a zero-dimensional [`ndarray`][@stdlib/ndarray/ctor].
99+
- If provided an empty `dims` array, the function returns a read-only view of the input [`ndarray`][@stdlib/ndarray/ctor].
100+
- If provided a zero-dimensional input [`ndarray`][@stdlib/ndarray/ctor], the function returns a read-only view of the input [`ndarray`][@stdlib/ndarray/ctor].
101+
102+
</section>
103+
104+
<!-- /.notes -->
105+
106+
<!-- Package usage examples. -->
107+
108+
<section class="examples">
109+
110+
## Examples
111+
112+
<!-- eslint no-undef: "error" -->
113+
114+
```javascript
115+
var uniform = require( '@stdlib/random/uniform' );
116+
var ndarray2array = require( '@stdlib/ndarray/to-array' );
117+
var last = require( '@stdlib/ndarray/last' );
118+
119+
var x = uniform( [ 3, 3, 3 ], -10.0, 10.0 );
120+
console.log( ndarray2array( x ) );
121+
122+
// Last scalar element:
123+
var v = last( x );
124+
console.log( v.get() );
125+
126+
// Last "row" along the innermost dimension:
127+
v = last( x, {
128+
'dims': [ -1 ]
129+
});
130+
console.log( ndarray2array( v ) );
131+
132+
// Last "matrix" along the outermost dimension:
133+
v = last( x, {
134+
'dims': [ 0 ]
135+
});
136+
console.log( ndarray2array( v ) );
137+
```
138+
139+
</section>
140+
141+
<!-- /.examples -->
142+
143+
<!-- 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. -->
144+
145+
<section class="references">
146+
147+
</section>
148+
149+
<!-- /.references -->
150+
151+
<!-- Section for related `stdlib` packages. Do not manually edit this section, as it is automatically populated. -->
152+
153+
<section class="related">
154+
155+
</section>
156+
157+
<!-- /.related -->
158+
159+
<!-- Section for all links. Make sure to keep an empty line after the `section` element and another before the `/section` close. -->
160+
161+
<section class="links">
162+
163+
[@stdlib/ndarray/ctor]: https://github.com/stdlib-js/stdlib/tree/develop/lib/node_modules/%40stdlib/ndarray/ctor
164+
165+
[@stdlib/ndarray/copy]: https://github.com/stdlib-js/stdlib/tree/develop/lib/node_modules/%40stdlib/ndarray/copy
166+
167+
<!-- <related-links> -->
168+
169+
<!-- </related-links> -->
170+
171+
</section>
172+
173+
<!-- /.links -->

0 commit comments

Comments
 (0)