From 7840656eebc24d42b65433072d6dab65be9fe18e Mon Sep 17 00:00:00 2001 From: headlessNode Date: Sat, 2 May 2026 00:30:04 +0500 Subject: [PATCH 01/10] 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 --- --- .../@stdlib/ndarray/last/README.md | 173 +++++ .../ndarray/last/benchmark/benchmark.js | 231 +++++++ .../@stdlib/ndarray/last/docs/repl.txt | 46 ++ .../ndarray/last/docs/types/index.d.ts | 64 ++ .../@stdlib/ndarray/last/docs/types/test.ts | 93 +++ .../@stdlib/ndarray/last/examples/index.js | 42 ++ .../@stdlib/ndarray/last/lib/defaults.json | 3 + .../@stdlib/ndarray/last/lib/index.js | 54 ++ .../@stdlib/ndarray/last/lib/main.js | 120 ++++ .../@stdlib/ndarray/last/lib/validate.js | 79 +++ .../@stdlib/ndarray/last/package.json | 66 ++ .../@stdlib/ndarray/last/test/test.js | 591 ++++++++++++++++++ 12 files changed, 1562 insertions(+) create mode 100644 lib/node_modules/@stdlib/ndarray/last/README.md create mode 100644 lib/node_modules/@stdlib/ndarray/last/benchmark/benchmark.js create mode 100644 lib/node_modules/@stdlib/ndarray/last/docs/repl.txt create mode 100644 lib/node_modules/@stdlib/ndarray/last/docs/types/index.d.ts create mode 100644 lib/node_modules/@stdlib/ndarray/last/docs/types/test.ts create mode 100644 lib/node_modules/@stdlib/ndarray/last/examples/index.js create mode 100644 lib/node_modules/@stdlib/ndarray/last/lib/defaults.json create mode 100644 lib/node_modules/@stdlib/ndarray/last/lib/index.js create mode 100644 lib/node_modules/@stdlib/ndarray/last/lib/main.js create mode 100644 lib/node_modules/@stdlib/ndarray/last/lib/validate.js create mode 100644 lib/node_modules/@stdlib/ndarray/last/package.json create mode 100644 lib/node_modules/@stdlib/ndarray/last/test/test.js diff --git a/lib/node_modules/@stdlib/ndarray/last/README.md b/lib/node_modules/@stdlib/ndarray/last/README.md new file mode 100644 index 000000000000..92fe35b6675f --- /dev/null +++ b/lib/node_modules/@stdlib/ndarray/last/README.md @@ -0,0 +1,173 @@ + + +# last + +> Return a read-only view of the last element (or subarray) along one or more [`ndarray`][@stdlib/ndarray/ctor] dimensions. + + + +
+ +
+ + + + + +
+ +## Usage + +```javascript +var last = require( '@stdlib/ndarray/last' ); +``` + +#### last( x\[, options] ) + +Returns a read-only view of the last element (or subarray) along one or more [`ndarray`][@stdlib/ndarray/ctor] dimensions. + +```javascript +var array = require( '@stdlib/ndarray/array' ); + +var x = array( [ [ 1.0, 2.0 ], [ 3.0, 4.0 ] ] ); +// returns [ [ 1.0, 2.0 ], [ 3.0, 4.0 ] ] + +var v = last( x ); +// returns [ 4.0 ] +``` + +The function accepts the following arguments: + +- **x**: input [`ndarray`][@stdlib/ndarray/ctor]. +- **options**: function options. + +The function accepts the following `options`: + +- **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]. + +To resolve the last element (or subarray) along one or more specific dimensions, provide a `dims` option: + +```javascript +var array = require( '@stdlib/ndarray/array' ); + +var x = array( [ [ 1.0, 2.0 ], [ 3.0, 4.0 ] ] ); +// returns [ [ 1.0, 2.0 ], [ 3.0, 4.0 ] ] + +// Last column: +var v = last( x, { + 'dims': [ -1 ] +}); +// returns [ 2.0, 4.0 ] + +// Last row: +v = last( x, { + 'dims': [ -2 ] +}); +// returns [ 3.0, 4.0 ] +``` + +
+ + + + + +
+ +## Notes + +- 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]). +- 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]. +- If provided an empty `dims` array, the function returns a read-only view of the input [`ndarray`][@stdlib/ndarray/ctor]. +- If provided a zero-dimensional input [`ndarray`][@stdlib/ndarray/ctor], the function returns a read-only view of the input [`ndarray`][@stdlib/ndarray/ctor]. + +
+ + + + + +
+ +## Examples + + + +```javascript +var uniform = require( '@stdlib/random/uniform' ); +var ndarray2array = require( '@stdlib/ndarray/to-array' ); +var last = require( '@stdlib/ndarray/last' ); + +var x = uniform( [ 3, 3, 3 ], -10.0, 10.0 ); +console.log( ndarray2array( x ) ); + +// Last scalar element: +var v = last( x ); +console.log( v.get() ); + +// Last "row" along the innermost dimension: +v = last( x, { + 'dims': [ -1 ] +}); +console.log( ndarray2array( v ) ); + +// Last "matrix" along the outermost dimension: +v = last( x, { + 'dims': [ 0 ] +}); +console.log( ndarray2array( v ) ); +``` + +
+ + + + + +
+ +
+ + + + + + + + + + + + + + diff --git a/lib/node_modules/@stdlib/ndarray/last/benchmark/benchmark.js b/lib/node_modules/@stdlib/ndarray/last/benchmark/benchmark.js new file mode 100644 index 000000000000..17da5abb8631 --- /dev/null +++ b/lib/node_modules/@stdlib/ndarray/last/benchmark/benchmark.js @@ -0,0 +1,231 @@ +/** +* @license Apache-2.0 +* +* Copyright (c) 2026 The Stdlib Authors. +* +* Licensed under the Apache License, Version 2.0 (the "License"); +* you may not use this file except in compliance with the License. +* You may obtain a copy of the License at +* +* http://www.apache.org/licenses/LICENSE-2.0 +* +* Unless required by applicable law or agreed to in writing, software +* distributed under the License is distributed on an "AS IS" BASIS, +* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. +* See the License for the specific language governing permissions and +* limitations under the License. +*/ + +'use strict'; + +// MODULES // + +var bench = require( '@stdlib/bench' ); +var isndarrayLike = require( '@stdlib/assert/is-ndarray-like' ); +var empty = require( '@stdlib/ndarray/empty' ); +var format = require( '@stdlib/string/format' ); +var pkg = require( './../package.json' ).name; +var last = require( './../lib' ); + + +// MAIN // + +bench( format( '%s:ndims=0', pkg ), function benchmark( b ) { + var values; + var v; + var i; + + /* eslint-disable object-curly-newline, stdlib/line-closing-bracket-spacing */ + + values = [ + empty( [], { 'dtype': 'float64' } ), + empty( [], { 'dtype': 'float32' } ), + empty( [], { 'dtype': 'int32' } ), + empty( [], { 'dtype': 'complex128' } ), + empty( [], { 'dtype': 'generic' } ) + ]; + + /* eslint-enable object-curly-newline, stdlib/line-closing-bracket-spacing */ + + b.tic(); + for ( i = 0; i < b.iterations; i++ ) { + v = last( values[ i%values.length ] ); + if ( typeof v !== 'object' ) { + b.fail( 'should return an ndarray' ); + } + } + b.toc(); + if ( !isndarrayLike( v ) ) { + b.fail( 'should return an ndarray' ); + } + b.pass( 'benchmark finished' ); + b.end(); +}); + +bench( format( '%s:ndims=1', pkg ), function benchmark( b ) { + var values; + var v; + var i; + + /* eslint-disable object-curly-newline, stdlib/line-closing-bracket-spacing */ + + values = [ + empty( [ 4 ], { 'dtype': 'float64' } ), + empty( [ 4 ], { 'dtype': 'float32' } ), + empty( [ 4 ], { 'dtype': 'int32' } ), + empty( [ 4 ], { 'dtype': 'complex128' } ), + empty( [ 4 ], { 'dtype': 'generic' } ) + ]; + + /* eslint-enable object-curly-newline, stdlib/line-closing-bracket-spacing */ + + b.tic(); + for ( i = 0; i < b.iterations; i++ ) { + v = last( values[ i%values.length ] ); + if ( typeof v !== 'object' ) { + b.fail( 'should return an ndarray' ); + } + } + b.toc(); + if ( !isndarrayLike( v ) ) { + b.fail( 'should return an ndarray' ); + } + b.pass( 'benchmark finished' ); + b.end(); +}); + +bench( format( '%s:ndims=2', pkg ), function benchmark( b ) { + var values; + var v; + var i; + + /* eslint-disable object-curly-newline, stdlib/line-closing-bracket-spacing */ + + values = [ + empty( [ 2, 2 ], { 'dtype': 'float64' } ), + empty( [ 2, 2 ], { 'dtype': 'float32' } ), + empty( [ 2, 2 ], { 'dtype': 'int32' } ), + empty( [ 2, 2 ], { 'dtype': 'complex128' } ), + empty( [ 2, 2 ], { 'dtype': 'generic' } ) + ]; + + /* eslint-enable object-curly-newline, stdlib/line-closing-bracket-spacing */ + + b.tic(); + for ( i = 0; i < b.iterations; i++ ) { + v = last( values[ i%values.length ], { + 'dims': [ -1 ] + }); + if ( typeof v !== 'object' ) { + b.fail( 'should return an ndarray' ); + } + } + b.toc(); + if ( !isndarrayLike( v ) ) { + b.fail( 'should return an ndarray' ); + } + b.pass( 'benchmark finished' ); + b.end(); +}); + +bench( format( '%s:ndims=3', pkg ), function benchmark( b ) { + var values; + var v; + var i; + + /* eslint-disable object-curly-newline, stdlib/line-closing-bracket-spacing */ + + values = [ + empty( [ 2, 2, 2 ], { 'dtype': 'float64' } ), + empty( [ 2, 2, 2 ], { 'dtype': 'float32' } ), + empty( [ 2, 2, 2 ], { 'dtype': 'int32' } ), + empty( [ 2, 2, 2 ], { 'dtype': 'complex128' } ), + empty( [ 2, 2, 2 ], { 'dtype': 'generic' } ) + ]; + + /* eslint-enable object-curly-newline, stdlib/line-closing-bracket-spacing */ + + b.tic(); + for ( i = 0; i < b.iterations; i++ ) { + v = last( values[ i%values.length ], { + 'dims': [ -1 ] + }); + if ( typeof v !== 'object' ) { + b.fail( 'should return an ndarray' ); + } + } + b.toc(); + if ( !isndarrayLike( v ) ) { + b.fail( 'should return an ndarray' ); + } + b.pass( 'benchmark finished' ); + b.end(); +}); + +bench( format( '%s:ndims=4', pkg ), function benchmark( b ) { + var values; + var v; + var i; + + /* eslint-disable object-curly-newline, stdlib/line-closing-bracket-spacing */ + + values = [ + empty( [ 2, 2, 2, 2 ], { 'dtype': 'float64' } ), + empty( [ 2, 2, 2, 2 ], { 'dtype': 'float32' } ), + empty( [ 2, 2, 2, 2 ], { 'dtype': 'int32' } ), + empty( [ 2, 2, 2, 2 ], { 'dtype': 'complex128' } ), + empty( [ 2, 2, 2, 2 ], { 'dtype': 'generic' } ) + ]; + + /* eslint-enable object-curly-newline, stdlib/line-closing-bracket-spacing */ + + b.tic(); + for ( i = 0; i < b.iterations; i++ ) { + v = last( values[ i%values.length ], { + 'dims': [ -1 ] + }); + if ( typeof v !== 'object' ) { + b.fail( 'should return an ndarray' ); + } + } + b.toc(); + if ( !isndarrayLike( v ) ) { + b.fail( 'should return an ndarray' ); + } + b.pass( 'benchmark finished' ); + b.end(); +}); + +bench( format( '%s:ndims=5', pkg ), function benchmark( b ) { + var values; + var v; + var i; + + /* eslint-disable object-curly-newline, stdlib/line-closing-bracket-spacing */ + + values = [ + empty( [ 2, 2, 2, 2, 2 ], { 'dtype': 'float64' } ), + empty( [ 2, 2, 2, 2, 2 ], { 'dtype': 'float32' } ), + empty( [ 2, 2, 2, 2, 2 ], { 'dtype': 'int32' } ), + empty( [ 2, 2, 2, 2, 2 ], { 'dtype': 'complex128' } ), + empty( [ 2, 2, 2, 2, 2 ], { 'dtype': 'generic' } ) + ]; + + /* eslint-enable object-curly-newline, stdlib/line-closing-bracket-spacing */ + + b.tic(); + for ( i = 0; i < b.iterations; i++ ) { + v = last( values[ i%values.length ], { + 'dims': [ -1 ] + }); + if ( typeof v !== 'object' ) { + b.fail( 'should return an ndarray' ); + } + } + b.toc(); + if ( !isndarrayLike( v ) ) { + b.fail( 'should return an ndarray' ); + } + b.pass( 'benchmark finished' ); + b.end(); +}); diff --git a/lib/node_modules/@stdlib/ndarray/last/docs/repl.txt b/lib/node_modules/@stdlib/ndarray/last/docs/repl.txt new file mode 100644 index 000000000000..50dd3b6b87d1 --- /dev/null +++ b/lib/node_modules/@stdlib/ndarray/last/docs/repl.txt @@ -0,0 +1,46 @@ + +{{alias}}( x[, options] ) + Returns a read-only view of the last element (or subarray) along one or + more ndarray dimensions. + + By default, the function performs the operation over all dimensions and + thus returns the last element of the input ndarray as a zero-dimensional + ndarray. + + If provided an empty `dims` array, the function returns a read-only view + of the input ndarray. + + If provided a zero-dimensional input ndarray, the function returns a read- + only view of the input ndarray. + + Parameters + ---------- + x: ndarray + Input ndarray. + + options: Object (optional) + Function options. + + options.dims: Array (optional) + List of dimensions over which to perform the operation. + + Returns + ------- + out: ndarray + Output ndarray. The returned ndarray is a read-only view of the input + ndarray. + + Examples + -------- + > var x = {{alias:@stdlib/ndarray/array}}( [ [ 1.0, 2.0 ], [ 3.0, 4.0 ] ] ) + [ [ 1.0, 2.0 ], [ 3.0, 4.0 ] ] + > var v = {{alias}}( x ) + [ 4.0 ] + > v = {{alias}}( x, { 'dims': [ -1 ] } ) + [ 2.0, 4.0 ] + > v = {{alias}}( x, { 'dims': [ -2 ] } ) + [ 3.0, 4.0 ] + + See Also + -------- + diff --git a/lib/node_modules/@stdlib/ndarray/last/docs/types/index.d.ts b/lib/node_modules/@stdlib/ndarray/last/docs/types/index.d.ts new file mode 100644 index 000000000000..841ec26d9f11 --- /dev/null +++ b/lib/node_modules/@stdlib/ndarray/last/docs/types/index.d.ts @@ -0,0 +1,64 @@ +/* +* @license Apache-2.0 +* +* Copyright (c) 2026 The Stdlib Authors. +* +* Licensed under the Apache License, Version 2.0 (the "License"); +* you may not use this file except in compliance with the License. +* You may obtain a copy of the License at +* +* http://www.apache.org/licenses/LICENSE-2.0 +* +* Unless required by applicable law or agreed to in writing, software +* distributed under the License is distributed on an "AS IS" BASIS, +* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. +* See the License for the specific language governing permissions and +* limitations under the License. +*/ + +// TypeScript Version: 4.1 + +/// + +import { ArrayLike } from '@stdlib/types/array'; +import { ndarray } from '@stdlib/types/ndarray'; + +/** +* Interface defining function options. +*/ +interface Options { + /** + * List of dimensions over which to perform the operation. + */ + dims?: ArrayLike; +} + +/** +* Returns a read-only view of the last element (or subarray) along one or more ndarray dimensions. +* +* ## Notes +* +* - By default, the function performs the operation over all dimensions and thus returns the last element of the input ndarray as a zero-dimensional ndarray. +* - If provided an empty `dims` array, the function returns a read-only view of the input ndarray. +* - If provided a zero-dimensional input ndarray, the function returns a read-only view of the input ndarray. +* +* @param x - input array +* @param options - function options +* @param options.dims - list of dimensions over which to perform the operation +* @returns output array +* +* @example +* var array = require( '@stdlib/ndarray/array' ); +* +* var x = array( [ [ 1.0, 2.0 ], [ 3.0, 4.0 ] ] ); +* // returns [ [ 1.0, 2.0 ], [ 3.0, 4.0 ] ] +* +* var v = last( x ); +* // returns [ 4.0 ] +*/ +declare function last( x: ndarray, options?: Options ): ndarray; + + +// EXPORTS // + +export = last; diff --git a/lib/node_modules/@stdlib/ndarray/last/docs/types/test.ts b/lib/node_modules/@stdlib/ndarray/last/docs/types/test.ts new file mode 100644 index 000000000000..f07ce8374675 --- /dev/null +++ b/lib/node_modules/@stdlib/ndarray/last/docs/types/test.ts @@ -0,0 +1,93 @@ +/* +* @license Apache-2.0 +* +* Copyright (c) 2026 The Stdlib Authors. +* +* Licensed under the Apache License, Version 2.0 (the "License"); +* you may not use this file except in compliance with the License. +* You may obtain a copy of the License at +* +* http://www.apache.org/licenses/LICENSE-2.0 +* +* Unless required by applicable law or agreed to in writing, software +* distributed under the License is distributed on an "AS IS" BASIS, +* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. +* See the License for the specific language governing permissions and +* limitations under the License. +*/ + +/* eslint-disable space-in-parens */ + +import empty = require( '@stdlib/ndarray/base/empty' ); +import last = require( './index' ); + + +// TESTS // + +// The function returns an ndarray... +{ + const order = 'row-major'; + const sh = [ 2, 2 ]; + + last( empty( 'float64', sh, order ) ); // $ExpectType ndarray + last( empty( 'float64', sh, order ), {} ); // $ExpectType ndarray + last( empty( 'float64', sh, order ), { 'dims': [ -1 ] } ); // $ExpectType ndarray + last( empty( 'complex64', sh, order ), { 'dims': [ 0 ] } ); // $ExpectType ndarray + last( empty( 'int32', sh, order ), { 'dims': [ 0, 1 ] } ); // $ExpectType ndarray +} + +// The compiler throws an error if the function is provided a first argument which is not an ndarray... +{ + last( '10' ); // $ExpectError + last( 10 ); // $ExpectError + last( false ); // $ExpectError + last( true ); // $ExpectError + last( null ); // $ExpectError + last( [] ); // $ExpectError + last( {} ); // $ExpectError + last( ( x: number ): number => x ); // $ExpectError + + last( '10', {} ); // $ExpectError + last( 10, {} ); // $ExpectError + last( false, {} ); // $ExpectError + last( true, {} ); // $ExpectError + last( null, {} ); // $ExpectError + last( [], {} ); // $ExpectError + last( {}, {} ); // $ExpectError + last( ( x: number ): number => x, {} ); // $ExpectError +} + +// The compiler throws an error if the function is provided a second argument which is not an object... +{ + const x = empty( 'float64', [ 2, 2 ], 'row-major' ); + + last( x, '10' ); // $ExpectError + last( x, 10 ); // $ExpectError + last( x, false ); // $ExpectError + last( x, true ); // $ExpectError + last( x, null ); // $ExpectError + last( x, [] ); // $ExpectError + last( x, ( x: number ): number => x ); // $ExpectError +} + +// The compiler throws an error if the function is provided a `dims` option which is not an array-like object of numbers... +{ + const x = empty( 'float64', [ 2, 2 ], 'row-major' ); + + last( x, { 'dims': '10' } ); // $ExpectError + last( x, { 'dims': 10 } ); // $ExpectError + last( x, { 'dims': false } ); // $ExpectError + last( x, { 'dims': true } ); // $ExpectError + last( x, { 'dims': null } ); // $ExpectError + last( x, { 'dims': [ '1' ] } ); // $ExpectError + last( x, { 'dims': {} } ); // $ExpectError + last( x, { 'dims': ( x: number ): number => x } ); // $ExpectError +} + +// The compiler throws an error if the function is provided an unsupported number of arguments... +{ + const x = empty( 'float64', [ 2, 2 ], 'row-major' ); + + last(); // $ExpectError + last( x, {}, {} ); // $ExpectError +} diff --git a/lib/node_modules/@stdlib/ndarray/last/examples/index.js b/lib/node_modules/@stdlib/ndarray/last/examples/index.js new file mode 100644 index 000000000000..a76e58054404 --- /dev/null +++ b/lib/node_modules/@stdlib/ndarray/last/examples/index.js @@ -0,0 +1,42 @@ +/** +* @license Apache-2.0 +* +* Copyright (c) 2026 The Stdlib Authors. +* +* Licensed under the Apache License, Version 2.0 (the "License"); +* you may not use this file except in compliance with the License. +* You may obtain a copy of the License at +* +* http://www.apache.org/licenses/LICENSE-2.0 +* +* Unless required by applicable law or agreed to in writing, software +* distributed under the License is distributed on an "AS IS" BASIS, +* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. +* See the License for the specific language governing permissions and +* limitations under the License. +*/ + +'use strict'; + +var uniform = require( '@stdlib/random/uniform' ); +var ndarray2array = require( '@stdlib/ndarray/to-array' ); +var last = require( './../lib' ); + +var x = uniform( [ 3, 3, 3 ], -10.0, 10.0 ); +console.log( ndarray2array( x ) ); + +// Last scalar element: +var v = last( x ); +console.log( v.get() ); + +// Last "row" along the innermost dimension: +v = last( x, { + 'dims': [ -1 ] +}); +console.log( ndarray2array( v ) ); + +// Last "matrix" along the outermost dimension: +v = last( x, { + 'dims': [ 0 ] +}); +console.log( ndarray2array( v ) ); diff --git a/lib/node_modules/@stdlib/ndarray/last/lib/defaults.json b/lib/node_modules/@stdlib/ndarray/last/lib/defaults.json new file mode 100644 index 000000000000..d8cc9c72ad33 --- /dev/null +++ b/lib/node_modules/@stdlib/ndarray/last/lib/defaults.json @@ -0,0 +1,3 @@ +{ + "dims": null +} diff --git a/lib/node_modules/@stdlib/ndarray/last/lib/index.js b/lib/node_modules/@stdlib/ndarray/last/lib/index.js new file mode 100644 index 000000000000..e0e989b15b5c --- /dev/null +++ b/lib/node_modules/@stdlib/ndarray/last/lib/index.js @@ -0,0 +1,54 @@ +/** +* @license Apache-2.0 +* +* Copyright (c) 2026 The Stdlib Authors. +* +* Licensed under the Apache License, Version 2.0 (the "License"); +* you may not use this file except in compliance with the License. +* You may obtain a copy of the License at +* +* http://www.apache.org/licenses/LICENSE-2.0 +* +* Unless required by applicable law or agreed to in writing, software +* distributed under the License is distributed on an "AS IS" BASIS, +* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. +* See the License for the specific language governing permissions and +* limitations under the License. +*/ + +'use strict'; + +/** +* Return a read-only view of the last element (or subarray) along one or more ndarray dimensions. +* +* @module @stdlib/ndarray/last +* +* @example +* var array = require( '@stdlib/ndarray/array' ); +* var last = require( '@stdlib/ndarray/last' ); +* +* var x = array( [ [ 1.0, 2.0 ], [ 3.0, 4.0 ] ] ); +* // returns [ [ 1.0, 2.0 ], [ 3.0, 4.0 ] ] +* +* var v = last( x ); +* // returns [ 4.0 ] +* +* v = last( x, { +* 'dims': [ -1 ] +* }); +* // returns [ 2.0, 4.0 ] +* +* v = last( x, { +* 'dims': [ -2 ] +* }); +* // returns [ 3.0, 4.0 ] +*/ + +// MODULES // + +var main = require( './main.js' ); + + +// EXPORTS // + +module.exports = main; diff --git a/lib/node_modules/@stdlib/ndarray/last/lib/main.js b/lib/node_modules/@stdlib/ndarray/last/lib/main.js new file mode 100644 index 000000000000..03c769ca906c --- /dev/null +++ b/lib/node_modules/@stdlib/ndarray/last/lib/main.js @@ -0,0 +1,120 @@ +/** +* @license Apache-2.0 +* +* Copyright (c) 2026 The Stdlib Authors. +* +* Licensed under the Apache License, Version 2.0 (the "License"); +* you may not use this file except in compliance with the License. +* You may obtain a copy of the License at +* +* http://www.apache.org/licenses/LICENSE-2.0 +* +* Unless required by applicable law or agreed to in writing, software +* distributed under the License is distributed on an "AS IS" BASIS, +* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. +* See the License for the specific language governing permissions and +* limitations under the License. +*/ + +'use strict'; + +// MODULES // + +var isndarrayLike = require( '@stdlib/assert/is-ndarray-like' ); +var args2multislice = require( '@stdlib/slice/base/args2multislice' ); +var base = require( '@stdlib/ndarray/base/slice' ); +var getShape = require( '@stdlib/ndarray/shape' ); +var zeroTo = require( '@stdlib/array/base/zero-to' ); +var objectAssign = require( '@stdlib/object/assign' ); +var format = require( '@stdlib/string/format' ); +var defaults = require( './defaults.json' ); +var validate = require( './validate.js' ); + + +// MAIN // + +/** +* Returns a read-only view of the last element (or subarray) along one or more ndarray dimensions. +* +* ## Notes +* +* - By default, the function performs the operation over all dimensions and thus returns the last element of the input ndarray as a zero-dimensional ndarray. +* - If provided an empty `dims` array, the function returns a read-only view of the input ndarray. +* - If provided a zero-dimensional input ndarray, the function returns a read-only view of the input ndarray. +* +* @param {ndarray} x - input ndarray +* @param {Options} [options] - function options +* @param {IntegerArray} [options.dims] - list of dimensions over which to perform the operation +* @throws {TypeError} first argument must be an ndarray-like object +* @throws {TypeError} options argument must be an object +* @throws {TypeError} `dims` option must be an array of integers +* @throws {RangeError} `dims` option contains an out-of-bounds dimension index +* @throws {Error} `dims` option contains duplicate indices +* @returns {ndarray} ndarray view +* +* @example +* var array = require( '@stdlib/ndarray/array' ); +* +* var x = array( [ [ 1.0, 2.0 ], [ 3.0, 4.0 ] ] ); +* // returns [ [ 1.0, 2.0 ], [ 3.0, 4.0 ] ] +* +* var v = last( x ); +* // returns [ 4.0 ] +* +* v = last( x, { +* 'dims': [ -1 ] +* }); +* // returns [ 2.0, 4.0 ] +* +* v = last( x, { +* 'dims': [ -2 ] +* }); +* // returns [ 3.0, 4.0 ] +*/ +function last( x, options ) { + var args; + var opts; + var dims; + var err; + var sh; + var N; + var i; + + if ( !isndarrayLike( x ) ) { + throw new TypeError( format( 'invalid argument. First argument must be an ndarray-like object. Value: `%s`.', x ) ); + } + sh = getShape( x ); + N = sh.length; + + opts = objectAssign( {}, defaults ); + if ( arguments.length > 1 ) { + err = validate( opts, N, options ); + if ( err ) { + throw err; + } + } + // Handle the 0-D case by returning a view of the input ndarray: + if ( N === 0 ) { + return base( x, args2multislice( [] ), true, false ); + } + // When a list of dimensions is not provided, perform the operation across all dimensions... + if ( opts.dims === null ) { + dims = zeroTo( N ); + } else { + dims = opts.dims; + } + // Build a list of slice arguments such that each dimension in `dims` resolves to its last index and all other dimensions are kept in full: + args = []; + for ( i = 0; i < N; i++ ) { + args.push( null ); + } + for ( i = 0; i < dims.length; i++ ) { + args[ dims[ i ] ] = sh[ dims[ i ] ] - 1; + } + return base( x, args2multislice( args ), true, false ); +} + + +// EXPORTS // + +module.exports = last; diff --git a/lib/node_modules/@stdlib/ndarray/last/lib/validate.js b/lib/node_modules/@stdlib/ndarray/last/lib/validate.js new file mode 100644 index 000000000000..1fb9e9e2162b --- /dev/null +++ b/lib/node_modules/@stdlib/ndarray/last/lib/validate.js @@ -0,0 +1,79 @@ +/** +* @license Apache-2.0 +* +* Copyright (c) 2026 The Stdlib Authors. +* +* Licensed under the Apache License, Version 2.0 (the "License"); +* you may not use this file except in compliance with the License. +* You may obtain a copy of the License at +* +* http://www.apache.org/licenses/LICENSE-2.0 +* +* Unless required by applicable law or agreed to in writing, software +* distributed under the License is distributed on an "AS IS" BASIS, +* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. +* See the License for the specific language governing permissions and +* limitations under the License. +*/ + +'use strict'; + +// MODULES // + +var isObject = require( '@stdlib/assert/is-plain-object' ); +var hasOwnProp = require( '@stdlib/assert/has-own-property' ); +var isIntegerArray = require( '@stdlib/assert/is-integer-array' ).primitives; +var isEmptyCollection = require( '@stdlib/assert/is-empty-collection' ); +var normalizeIndices = require( '@stdlib/ndarray/base/to-unique-normalized-indices' ); +var join = require( '@stdlib/array/base/join' ); +var format = require( '@stdlib/string/format' ); + + +// MAIN // + +/** +* Validates function options. +* +* @private +* @param {Object} opts - destination object +* @param {NonNegativeInteger} ndims - number of input ndarray dimensions +* @param {Options} options - function options +* @param {IntegerArray} [options.dims] - list of dimensions over which to perform the operation +* @returns {(Error|null)} null or an error object +* +* @example +* var opts = {}; +* var options = { +* 'dims': [ 0 ] +* }; +* var err = validate( opts, 2, options ); +* if ( err ) { +* throw err; +* } +*/ +function validate( opts, ndims, options ) { + var tmp; + if ( !isObject( options ) ) { + return new TypeError( format( 'invalid argument. Options argument must be an object. Value: `%s`.', options ) ); + } + if ( hasOwnProp( options, 'dims' ) ) { + opts.dims = options.dims; + if ( !isIntegerArray( opts.dims ) && !isEmptyCollection( opts.dims ) ) { + return new TypeError( format( 'invalid option. `%s` option must be an array of integers. Option: `%s`.', 'dims', opts.dims ) ); + } + tmp = normalizeIndices( opts.dims, ndims-1 ); + if ( tmp === null ) { + return new RangeError( format( 'invalid option. `%s` option contains an out-of-bounds dimension index. Option: [%s].', 'dims', join( opts.dims, ',' ) ) ); + } + if ( tmp.length !== opts.dims.length ) { + return new Error( format( 'invalid option. `%s` option contains duplicate indices. Option: [%s].', 'dims', join( opts.dims, ',' ) ) ); + } + opts.dims = tmp; + } + return null; +} + + +// EXPORTS // + +module.exports = validate; diff --git a/lib/node_modules/@stdlib/ndarray/last/package.json b/lib/node_modules/@stdlib/ndarray/last/package.json new file mode 100644 index 000000000000..8da5700838b6 --- /dev/null +++ b/lib/node_modules/@stdlib/ndarray/last/package.json @@ -0,0 +1,66 @@ +{ + "name": "@stdlib/ndarray/last", + "version": "0.0.0", + "description": "Return a read-only view of the last element (or subarray) along one or more ndarray dimensions.", + "license": "Apache-2.0", + "author": { + "name": "The Stdlib Authors", + "url": "https://github.com/stdlib-js/stdlib/graphs/contributors" + }, + "contributors": [ + { + "name": "The Stdlib Authors", + "url": "https://github.com/stdlib-js/stdlib/graphs/contributors" + } + ], + "main": "./lib", + "directories": { + "benchmark": "./benchmark", + "doc": "./docs", + "example": "./examples", + "lib": "./lib", + "test": "./test" + }, + "types": "./docs/types", + "scripts": {}, + "homepage": "https://github.com/stdlib-js/stdlib", + "repository": { + "type": "git", + "url": "git://github.com/stdlib-js/stdlib.git" + }, + "bugs": { + "url": "https://github.com/stdlib-js/stdlib/issues" + }, + "dependencies": {}, + "devDependencies": {}, + "engines": { + "node": ">=0.10.0", + "npm": ">2.7.0" + }, + "os": [ + "aix", + "darwin", + "freebsd", + "linux", + "macos", + "openbsd", + "sunos", + "win32", + "windows" + ], + "keywords": [ + "stdlib", + "stdtypes", + "types", + "data", + "structure", + "ndarray", + "multidimensional", + "array", + "last", + "tail", + "slice", + "view", + "subarray" + ] +} diff --git a/lib/node_modules/@stdlib/ndarray/last/test/test.js b/lib/node_modules/@stdlib/ndarray/last/test/test.js new file mode 100644 index 000000000000..69b0f18af56b --- /dev/null +++ b/lib/node_modules/@stdlib/ndarray/last/test/test.js @@ -0,0 +1,591 @@ +/** +* @license Apache-2.0 +* +* Copyright (c) 2026 The Stdlib Authors. +* +* Licensed under the Apache License, Version 2.0 (the "License"); +* you may not use this file except in compliance with the License. +* You may obtain a copy of the License at +* +* http://www.apache.org/licenses/LICENSE-2.0 +* +* Unless required by applicable law or agreed to in writing, software +* distributed under the License is distributed on an "AS IS" BASIS, +* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. +* See the License for the specific language governing permissions and +* limitations under the License. +*/ + +'use strict'; + +// MODULES // + +var tape = require( 'tape' ); +var isndarrayLike = require( '@stdlib/assert/is-ndarray-like' ); +var isReadOnly = require( '@stdlib/ndarray/base/assert/is-read-only' ); +var isEqualDataType = require( '@stdlib/ndarray/base/assert/is-equal-data-type' ); +var zeroTo = require( '@stdlib/array/zero-to' ); +var scalar2ndarray = require( '@stdlib/ndarray/from-scalar' ); +var getShape = require( '@stdlib/ndarray/shape' ); +var getData = require( '@stdlib/ndarray/data-buffer' ); +var getDType = require( '@stdlib/ndarray/dtype' ); +var ndarray2array = require( '@stdlib/ndarray/to-array' ); +var ndarray = require( '@stdlib/ndarray/ctor' ); +var last = require( './../lib' ); + + +// TESTS // + +tape( 'main export is a function', function test( t ) { + t.ok( true, __filename ); + t.strictEqual( typeof last, 'function', 'main export is a function' ); + t.end(); +}); + +tape( 'the function throws an error if provided a first argument which is not an ndarray-like object', function test( t ) { + var values; + var i; + + values = [ + '5', + 5, + NaN, + true, + false, + null, + void 0, + [], + {}, + function noop() {} + ]; + + for ( i = 0; i < values.length; i++ ) { + t.throws( badValue( values[ i ] ), TypeError, 'throws an error when provided ' + values[ i ] ); + } + t.end(); + + function badValue( value ) { + return function badValue() { + last( value ); + }; + } +}); + +tape( 'the function throws an error if provided a first argument which is not an ndarray-like object (options)', function test( t ) { + var values; + var i; + + values = [ + '5', + 5, + NaN, + true, + false, + null, + void 0, + [], + {}, + function noop() {} + ]; + + for ( i = 0; i < values.length; i++ ) { + t.throws( badValue( values[ i ] ), TypeError, 'throws an error when provided ' + values[ i ] ); + } + t.end(); + + function badValue( value ) { + return function badValue() { + last( value, {} ); + }; + } +}); + +tape( 'the function throws an error if provided an options argument which is not an object', function test( t ) { + var values; + var x; + var i; + + x = new ndarray( 'float64', zeroTo( 4, 'float64' ), [ 2, 2 ], [ 2, 1 ], 0, 'row-major' ); + + values = [ + '5', + 5, + NaN, + true, + false, + null, + void 0, + [], + function noop() {} + ]; + + for ( i = 0; i < values.length; i++ ) { + t.throws( badValue( values[ i ] ), TypeError, 'throws an error when provided ' + values[ i ] ); + } + t.end(); + + function badValue( value ) { + return function badValue() { + last( x, value ); + }; + } +}); + +tape( 'the function throws an error if provided a `dims` option which is not an array of integers', function test( t ) { + var values; + var x; + var i; + + x = new ndarray( 'float64', zeroTo( 4, 'float64' ), [ 2, 2 ], [ 2, 1 ], 0, 'row-major' ); + + values = [ + '5', + 5, + NaN, + true, + false, + null, + [ '1' ], + [ 3.14 ], + {}, + function noop() {} + ]; + + for ( i = 0; i < values.length; i++ ) { + t.throws( badValue( values[ i ] ), TypeError, 'throws an error when provided ' + values[ i ] ); + } + t.end(); + + function badValue( value ) { + return function badValue() { + last( x, { + 'dims': value + }); + }; + } +}); + +tape( 'the function throws an error if provided a `dims` option which contains an out-of-bounds dimension index', function test( t ) { + var values; + var x; + var i; + + x = new ndarray( 'float64', zeroTo( 4, 'float64' ), [ 2, 2 ], [ 2, 1 ], 0, 'row-major' ); + + values = [ + [ 2 ], + [ -3 ], + [ 0, 5 ], + [ -10, 0 ] + ]; + + for ( i = 0; i < values.length; i++ ) { + t.throws( badValue( values[ i ] ), RangeError, 'throws an error when provided ' + values[ i ] ); + } + t.end(); + + function badValue( value ) { + return function badValue() { + last( x, { + 'dims': value + }); + }; + } +}); + +tape( 'the function throws an error if provided a `dims` option which contains duplicate indices', function test( t ) { + var values; + var x; + var i; + + x = new ndarray( 'float64', zeroTo( 6, 'float64' ), [ 2, 3 ], [ 3, 1 ], 0, 'row-major' ); + + values = [ + [ 0, 0 ], + [ -1, -1 ], + [ 0, 1, 0 ] + ]; + + for ( i = 0; i < values.length; i++ ) { + t.throws( badValue( values[ i ] ), Error, 'throws an error when provided ' + values[ i ] ); + } + t.end(); + + function badValue( value ) { + return function badValue() { + last( x, { + 'dims': value + }); + }; + } +}); + +tape( 'the function throws an error if provided a `dims` option which contains negative-and-positive indices aliasing the same dimension', function test( t ) { + var values; + var x; + var i; + + x = new ndarray( 'float64', zeroTo( 6, 'float64' ), [ 2, 3 ], [ 3, 1 ], 0, 'row-major' ); + + values = [ + [ 1, -1 ], + [ 0, 1, -2 ], + [ -2, 0 ] + ]; + + for ( i = 0; i < values.length; i++ ) { + t.throws( badValue( values[ i ] ), Error, 'throws an error when provided ' + values[ i ] ); + } + t.end(); + + function badValue( value ) { + return function badValue() { + last( x, { + 'dims': value + }); + }; + } +}); + +tape( 'the function returns a read-only view of a zero-dimensional input ndarray', function test( t ) { + var actual; + var x; + + x = scalar2ndarray( 3.14, { + 'dtype': 'float64', + 'order': 'row-major' + }); + + actual = last( x ); + + t.strictEqual( isndarrayLike( actual ), true, 'returns expected value' ); + t.strictEqual( isReadOnly( actual ), true, 'returns expected value' ); + t.deepEqual( getShape( actual ), [], 'returns expected value' ); + t.strictEqual( actual.get(), 3.14, 'returns expected value' ); + t.strictEqual( isEqualDataType( getDType( actual ), getDType( x ) ), true, 'returns expected value' ); + t.strictEqual( getData( actual ), getData( x ), 'returns expected value' ); + + t.end(); +}); + +tape( 'the function returns the last element of a one-dimensional ndarray', function test( t ) { + var actual; + var x; + + x = new ndarray( 'float64', zeroTo( 4, 'float64' ), [ 4 ], [ 1 ], 0, 'row-major' ); + + actual = last( x ); + + t.strictEqual( isndarrayLike( actual ), true, 'returns expected value' ); + t.strictEqual( isReadOnly( actual ), true, 'returns expected value' ); + t.deepEqual( getShape( actual ), [], 'returns expected value' ); + t.strictEqual( actual.get(), 3, 'returns expected value' ); + t.strictEqual( isEqualDataType( getDType( actual ), getDType( x ) ), true, 'returns expected value' ); + + t.end(); +}); + +tape( 'the function returns the last element of a multi-dimensional ndarray (default dims)', function test( t ) { + var actual; + var x; + + x = new ndarray( 'float64', zeroTo( 6, 'float64' ), [ 2, 3 ], [ 3, 1 ], 0, 'row-major' ); + + actual = last( x ); + + t.strictEqual( isndarrayLike( actual ), true, 'returns expected value' ); + t.strictEqual( isReadOnly( actual ), true, 'returns expected value' ); + t.deepEqual( getShape( actual ), [], 'returns expected value' ); + t.strictEqual( actual.get(), 5, 'returns expected value' ); + t.strictEqual( isEqualDataType( getDType( actual ), getDType( x ) ), true, 'returns expected value' ); + + t.end(); +}); + +tape( 'the function returns the last element along a single dimension (negative index)', function test( t ) { + var expected; + var actual; + var x; + + x = new ndarray( 'float64', zeroTo( 6, 'float64' ), [ 2, 3 ], [ 3, 1 ], 0, 'row-major' ); + + // Last column: + actual = last( x, { + 'dims': [ -1 ] + }); + expected = [ 2, 5 ]; + + t.strictEqual( isReadOnly( actual ), true, 'returns expected value' ); + t.deepEqual( getShape( actual ), [ 2 ], 'returns expected value' ); + t.deepEqual( ndarray2array( actual ), expected, 'returns expected value' ); + + // Last row: + actual = last( x, { + 'dims': [ -2 ] + }); + expected = [ 3, 4, 5 ]; + + t.strictEqual( isReadOnly( actual ), true, 'returns expected value' ); + t.deepEqual( getShape( actual ), [ 3 ], 'returns expected value' ); + t.deepEqual( ndarray2array( actual ), expected, 'returns expected value' ); + + t.end(); +}); + +tape( 'the function returns the last element along a single dimension (positive index)', function test( t ) { + var expected; + var actual; + var x; + + x = new ndarray( 'float64', zeroTo( 6, 'float64' ), [ 2, 3 ], [ 3, 1 ], 0, 'row-major' ); + + // Last row (dim 0): + actual = last( x, { + 'dims': [ 0 ] + }); + expected = [ 3, 4, 5 ]; + + t.strictEqual( isReadOnly( actual ), true, 'returns expected value' ); + t.deepEqual( getShape( actual ), [ 3 ], 'returns expected value' ); + t.deepEqual( ndarray2array( actual ), expected, 'returns expected value' ); + t.strictEqual( isEqualDataType( getDType( actual ), getDType( x ) ), true, 'returns expected value' ); + + // Last column (dim 1): + actual = last( x, { + 'dims': [ 1 ] + }); + expected = [ 2, 5 ]; + + t.strictEqual( isReadOnly( actual ), true, 'returns expected value' ); + t.deepEqual( getShape( actual ), [ 2 ], 'returns expected value' ); + t.deepEqual( ndarray2array( actual ), expected, 'returns expected value' ); + t.strictEqual( isEqualDataType( getDType( actual ), getDType( x ) ), true, 'returns expected value' ); + + t.end(); +}); + +tape( 'the function supports a three-dimensional ndarray', function test( t ) { + var expected; + var actual; + var x; + + x = new ndarray( 'float64', zeroTo( 24, 'float64' ), [ 2, 3, 4 ], [ 12, 4, 1 ], 0, 'row-major' ); + + // Default: last scalar: + actual = last( x ); + t.strictEqual( isReadOnly( actual ), true, 'returns expected value' ); + t.deepEqual( getShape( actual ), [], 'returns expected value' ); + t.strictEqual( actual.get(), 23, 'returns expected value' ); + t.strictEqual( isEqualDataType( getDType( actual ), getDType( x ) ), true, 'returns expected value' ); + + // Take last along innermost dim: + actual = last( x, { + 'dims': [ -1 ] + }); + expected = [ + [ 3, 7, 11 ], + [ 15, 19, 23 ] + ]; + t.strictEqual( isReadOnly( actual ), true, 'returns expected value' ); + t.deepEqual( getShape( actual ), [ 2, 3 ], 'returns expected value' ); + t.deepEqual( ndarray2array( actual ), expected, 'returns expected value' ); + t.strictEqual( isEqualDataType( getDType( actual ), getDType( x ) ), true, 'returns expected value' ); + + // Take last along outermost dim: + actual = last( x, { + 'dims': [ 0 ] + }); + expected = [ + [ 12, 13, 14, 15 ], + [ 16, 17, 18, 19 ], + [ 20, 21, 22, 23 ] + ]; + t.strictEqual( isReadOnly( actual ), true, 'returns expected value' ); + t.deepEqual( getShape( actual ), [ 3, 4 ], 'returns expected value' ); + t.deepEqual( ndarray2array( actual ), expected, 'returns expected value' ); + + // Take last along middle dim: + actual = last( x, { + 'dims': [ 1 ] + }); + expected = [ + [ 8, 9, 10, 11 ], + [ 20, 21, 22, 23 ] + ]; + t.strictEqual( isReadOnly( actual ), true, 'returns expected value' ); + t.deepEqual( getShape( actual ), [ 2, 4 ], 'returns expected value' ); + t.deepEqual( ndarray2array( actual ), expected, 'returns expected value' ); + + t.end(); +}); + +tape( 'the function supports specifying multiple, but not all, dimensions', function test( t ) { + var expected; + var actual; + var x; + + x = new ndarray( 'float64', zeroTo( 24, 'float64' ), [ 2, 3, 4 ], [ 12, 4, 1 ], 0, 'row-major' ); + + // Take last along dims 0 and 2 (keep dim 1): + actual = last( x, { + 'dims': [ 0, -1 ] + }); + expected = [ 15, 19, 23 ]; + + t.strictEqual( isReadOnly( actual ), true, 'returns expected value' ); + t.deepEqual( getShape( actual ), [ 3 ], 'returns expected value' ); + t.deepEqual( ndarray2array( actual ), expected, 'returns expected value' ); + t.strictEqual( isEqualDataType( getDType( actual ), getDType( x ) ), true, 'returns expected value' ); + + t.end(); +}); + +tape( 'the function returns the last element when `dims` includes every dimension', function test( t ) { + var actual; + var x; + + x = new ndarray( 'float64', zeroTo( 6, 'float64' ), [ 2, 3 ], [ 3, 1 ], 0, 'row-major' ); + + actual = last( x, { + 'dims': [ -2, -1 ] + }); + + t.strictEqual( isReadOnly( actual ), true, 'returns expected value' ); + t.deepEqual( getShape( actual ), [], 'returns expected value' ); + t.strictEqual( actual.get(), 5, 'returns expected value' ); + + t.end(); +}); + +tape( 'the function returns a view of the input ndarray when `dims` is empty', function test( t ) { + var expected; + var actual; + var x; + + x = new ndarray( 'float64', zeroTo( 6, 'float64' ), [ 2, 3 ], [ 3, 1 ], 0, 'row-major' ); + + actual = last( x, { + 'dims': [] + }); + expected = [ + [ 0, 1, 2 ], + [ 3, 4, 5 ] + ]; + + t.strictEqual( isReadOnly( actual ), true, 'returns expected value' ); + t.deepEqual( getShape( actual ), [ 2, 3 ], 'returns expected value' ); + t.deepEqual( ndarray2array( actual ), expected, 'returns expected value' ); + t.strictEqual( getData( actual ), getData( x ), 'returns expected value' ); + + t.end(); +}); + +tape( 'the function returns a view sharing the input ndarray data buffer', function test( t ) { + var actual; + var x; + + x = new ndarray( 'float64', zeroTo( 6, 'float64' ), [ 2, 3 ], [ 3, 1 ], 0, 'row-major' ); + + actual = last( x, { + 'dims': [ -1 ] + }); + + t.strictEqual( getData( actual ), getData( x ), 'returns expected value' ); + + t.end(); +}); + +tape( 'the function supports input ndarrays having a non-zero offset', function test( t ) { + var expected; + var actual; + var x; + + // Underlying buffer has 8 elements; the ndarray "starts" at index 2: + x = new ndarray( 'float64', zeroTo( 8, 'float64' ), [ 2, 3 ], [ 3, 1 ], 2, 'row-major' ); + + // Default: last element of the offset view: + actual = last( x ); + t.strictEqual( isReadOnly( actual ), true, 'returns expected value' ); + t.deepEqual( getShape( actual ), [], 'returns expected value' ); + t.strictEqual( actual.get(), 7, 'returns expected value' ); + + // Last column of the offset view: + actual = last( x, { + 'dims': [ -1 ] + }); + expected = [ 4, 7 ]; + t.strictEqual( isReadOnly( actual ), true, 'returns expected value' ); + t.deepEqual( getShape( actual ), [ 2 ], 'returns expected value' ); + t.deepEqual( ndarray2array( actual ), expected, 'returns expected value' ); + + // Last row of the offset view: + actual = last( x, { + 'dims': [ -2 ] + }); + expected = [ 5, 6, 7 ]; + t.strictEqual( isReadOnly( actual ), true, 'returns expected value' ); + t.deepEqual( getShape( actual ), [ 3 ], 'returns expected value' ); + t.deepEqual( ndarray2array( actual ), expected, 'returns expected value' ); + + t.end(); +}); + +tape( 'the function supports input ndarrays having non-contiguous strides', function test( t ) { + var expected; + var actual; + var x; + + // Underlying buffer has 12 elements; the ndarray accesses every other element via stride 2 along the innermost dim: + x = new ndarray( 'float64', zeroTo( 12, 'float64' ), [ 2, 3 ], [ 6, 2 ], 0, 'row-major' ); + + // Logical view: [ [ 0, 2, 4 ], [ 6, 8, 10 ] ] + actual = last( x ); + t.strictEqual( isReadOnly( actual ), true, 'returns expected value' ); + t.deepEqual( getShape( actual ), [], 'returns expected value' ); + t.strictEqual( actual.get(), 10, 'returns expected value' ); + + actual = last( x, { + 'dims': [ -1 ] + }); + expected = [ 4, 10 ]; + t.strictEqual( isReadOnly( actual ), true, 'returns expected value' ); + t.deepEqual( ndarray2array( actual ), expected, 'returns expected value' ); + + actual = last( x, { + 'dims': [ -2 ] + }); + expected = [ 6, 8, 10 ]; + t.strictEqual( isReadOnly( actual ), true, 'returns expected value' ); + t.deepEqual( ndarray2array( actual ), expected, 'returns expected value' ); + + t.end(); +}); + +tape( 'the function supports column-major input ndarrays', function test( t ) { + var expected; + var actual; + var x; + + // In column-major with strides [ 1, 2 ], x = [ [ 0, 2, 4 ], [ 1, 3, 5 ] ]: + x = new ndarray( 'float64', zeroTo( 6, 'float64' ), [ 2, 3 ], [ 1, 2 ], 0, 'column-major' ); + + actual = last( x, { + 'dims': [ -1 ] + }); + expected = [ 4, 5 ]; + + t.strictEqual( isReadOnly( actual ), true, 'returns expected value' ); + t.deepEqual( getShape( actual ), [ 2 ], 'returns expected value' ); + t.deepEqual( ndarray2array( actual ), expected, 'returns expected value' ); + + actual = last( x, { + 'dims': [ -2 ] + }); + expected = [ 1, 3, 5 ]; + + t.strictEqual( isReadOnly( actual ), true, 'returns expected value' ); + t.deepEqual( getShape( actual ), [ 3 ], 'returns expected value' ); + t.deepEqual( ndarray2array( actual ), expected, 'returns expected value' ); + + t.end(); +}); From 4ced05abd96ec4aa4ae50a0a1a031cd0cb869702 Mon Sep 17 00:00:00 2001 From: headlessNode Date: Sun, 3 May 2026 01:41:58 +0500 Subject: [PATCH 02/10] fix: apply suggestions from code review --- .../@stdlib/ndarray/last/README.md | 1 - .../@stdlib/ndarray/last/docs/repl.txt | 11 +- .../ndarray/last/docs/types/index.d.ts | 15 +- .../@stdlib/ndarray/last/docs/types/test.ts | 19 ++- .../@stdlib/ndarray/last/lib/main.js | 4 +- .../@stdlib/ndarray/last/test/test.js | 145 ++++++++++-------- 6 files changed, 103 insertions(+), 92 deletions(-) diff --git a/lib/node_modules/@stdlib/ndarray/last/README.md b/lib/node_modules/@stdlib/ndarray/last/README.md index 92fe35b6675f..e9f377011ac6 100644 --- a/lib/node_modules/@stdlib/ndarray/last/README.md +++ b/lib/node_modules/@stdlib/ndarray/last/README.md @@ -97,7 +97,6 @@ v = last( x, { - 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]). - 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]. - If provided an empty `dims` array, the function returns a read-only view of the input [`ndarray`][@stdlib/ndarray/ctor]. -- If provided a zero-dimensional input [`ndarray`][@stdlib/ndarray/ctor], the function returns a read-only view of the input [`ndarray`][@stdlib/ndarray/ctor]. diff --git a/lib/node_modules/@stdlib/ndarray/last/docs/repl.txt b/lib/node_modules/@stdlib/ndarray/last/docs/repl.txt index 50dd3b6b87d1..7225da1a67e6 100644 --- a/lib/node_modules/@stdlib/ndarray/last/docs/repl.txt +++ b/lib/node_modules/@stdlib/ndarray/last/docs/repl.txt @@ -3,16 +3,9 @@ Returns a read-only view of the last element (or subarray) along one or more ndarray dimensions. - By default, the function performs the operation over all dimensions and - thus returns the last element of the input ndarray as a zero-dimensional - ndarray. - If provided an empty `dims` array, the function returns a read-only view of the input ndarray. - If provided a zero-dimensional input ndarray, the function returns a read- - only view of the input ndarray. - Parameters ---------- x: ndarray @@ -22,7 +15,9 @@ Function options. options.dims: Array (optional) - List of dimensions over which to perform the operation. + List of dimensions over which to perform the operation. If not provided, + the function performs the operation over all dimensions and thus returns + the last element of the input ndarray as a zero-dimensional ndarray. Returns ------- diff --git a/lib/node_modules/@stdlib/ndarray/last/docs/types/index.d.ts b/lib/node_modules/@stdlib/ndarray/last/docs/types/index.d.ts index 841ec26d9f11..e116cbcc2f7b 100644 --- a/lib/node_modules/@stdlib/ndarray/last/docs/types/index.d.ts +++ b/lib/node_modules/@stdlib/ndarray/last/docs/types/index.d.ts @@ -40,12 +40,11 @@ interface Options { * * - By default, the function performs the operation over all dimensions and thus returns the last element of the input ndarray as a zero-dimensional ndarray. * - If provided an empty `dims` array, the function returns a read-only view of the input ndarray. -* - If provided a zero-dimensional input ndarray, the function returns a read-only view of the input ndarray. * -* @param x - input array +* @param x - input ndarray * @param options - function options * @param options.dims - list of dimensions over which to perform the operation -* @returns output array +* @returns output ndarray * * @example * var array = require( '@stdlib/ndarray/array' ); @@ -55,6 +54,16 @@ interface Options { * * var v = last( x ); * // returns [ 4.0 ] +* +* v = last( x, { +* 'dims': [ -1 ] +* }); +* // returns [ 2.0, 4.0 ] +* +* v = last( x, { +* 'dims': [ -2 ] +* }); +* // returns [ 3.0, 4.0 ] */ declare function last( x: ndarray, options?: Options ): ndarray; diff --git a/lib/node_modules/@stdlib/ndarray/last/docs/types/test.ts b/lib/node_modules/@stdlib/ndarray/last/docs/types/test.ts index f07ce8374675..451e2972726a 100644 --- a/lib/node_modules/@stdlib/ndarray/last/docs/types/test.ts +++ b/lib/node_modules/@stdlib/ndarray/last/docs/types/test.ts @@ -18,7 +18,7 @@ /* eslint-disable space-in-parens */ -import empty = require( '@stdlib/ndarray/base/empty' ); +import empty = require( '@stdlib/ndarray/empty' ); import last = require( './index' ); @@ -26,14 +26,13 @@ import last = require( './index' ); // The function returns an ndarray... { - const order = 'row-major'; const sh = [ 2, 2 ]; - last( empty( 'float64', sh, order ) ); // $ExpectType ndarray - last( empty( 'float64', sh, order ), {} ); // $ExpectType ndarray - last( empty( 'float64', sh, order ), { 'dims': [ -1 ] } ); // $ExpectType ndarray - last( empty( 'complex64', sh, order ), { 'dims': [ 0 ] } ); // $ExpectType ndarray - last( empty( 'int32', sh, order ), { 'dims': [ 0, 1 ] } ); // $ExpectType ndarray + last( empty( sh, { 'dtype': 'float64' } ) ); // $ExpectType ndarray + last( empty( sh, { 'dtype': 'float64' } ), {} ); // $ExpectType ndarray + last( empty( sh, { 'dtype': 'float64' } ), { 'dims': [ -1 ] } ); // $ExpectType ndarray + last( empty( sh, { 'dtype': 'complex64' } ), { 'dims': [ 0 ] } ); // $ExpectType ndarray + last( empty( sh, { 'dtype': 'int32' } ), { 'dims': [ 0, 1 ] } ); // $ExpectType ndarray } // The compiler throws an error if the function is provided a first argument which is not an ndarray... @@ -59,7 +58,7 @@ import last = require( './index' ); // The compiler throws an error if the function is provided a second argument which is not an object... { - const x = empty( 'float64', [ 2, 2 ], 'row-major' ); + const x = empty( [ 2, 2 ], { 'dtype': 'float64' } ); last( x, '10' ); // $ExpectError last( x, 10 ); // $ExpectError @@ -72,7 +71,7 @@ import last = require( './index' ); // The compiler throws an error if the function is provided a `dims` option which is not an array-like object of numbers... { - const x = empty( 'float64', [ 2, 2 ], 'row-major' ); + const x = empty( [ 2, 2 ], { 'dtype': 'float64' } ); last( x, { 'dims': '10' } ); // $ExpectError last( x, { 'dims': 10 } ); // $ExpectError @@ -86,7 +85,7 @@ import last = require( './index' ); // The compiler throws an error if the function is provided an unsupported number of arguments... { - const x = empty( 'float64', [ 2, 2 ], 'row-major' ); + const x = empty( [ 2, 2 ], { 'dtype': 'float64' } ); last(); // $ExpectError last( x, {}, {} ); // $ExpectError diff --git a/lib/node_modules/@stdlib/ndarray/last/lib/main.js b/lib/node_modules/@stdlib/ndarray/last/lib/main.js index 03c769ca906c..cda0d2d3a3e2 100644 --- a/lib/node_modules/@stdlib/ndarray/last/lib/main.js +++ b/lib/node_modules/@stdlib/ndarray/last/lib/main.js @@ -40,7 +40,6 @@ var validate = require( './validate.js' ); * * - By default, the function performs the operation over all dimensions and thus returns the last element of the input ndarray as a zero-dimensional ndarray. * - If provided an empty `dims` array, the function returns a read-only view of the input ndarray. -* - If provided a zero-dimensional input ndarray, the function returns a read-only view of the input ndarray. * * @param {ndarray} x - input ndarray * @param {Options} [options] - function options @@ -86,6 +85,7 @@ function last( x, options ) { sh = getShape( x ); N = sh.length; + // Resolve options: opts = objectAssign( {}, defaults ); if ( arguments.length > 1 ) { err = validate( opts, N, options ); @@ -93,11 +93,9 @@ function last( x, options ) { throw err; } } - // Handle the 0-D case by returning a view of the input ndarray: if ( N === 0 ) { return base( x, args2multislice( [] ), true, false ); } - // When a list of dimensions is not provided, perform the operation across all dimensions... if ( opts.dims === null ) { dims = zeroTo( N ); } else { diff --git a/lib/node_modules/@stdlib/ndarray/last/test/test.js b/lib/node_modules/@stdlib/ndarray/last/test/test.js index 69b0f18af56b..cd53525b87ca 100644 --- a/lib/node_modules/@stdlib/ndarray/last/test/test.js +++ b/lib/node_modules/@stdlib/ndarray/last/test/test.js @@ -21,6 +21,10 @@ // MODULES // var tape = require( 'tape' ); +var Complex64Array = require( '@stdlib/array/complex64' ); +var Complex64 = require( '@stdlib/complex/float32/ctor' ); +var realf = require( '@stdlib/complex/float32/real' ); +var imagf = require( '@stdlib/complex/float32/imag' ); var isndarrayLike = require( '@stdlib/assert/is-ndarray-like' ); var isReadOnly = require( '@stdlib/ndarray/base/assert/is-read-only' ); var isEqualDataType = require( '@stdlib/ndarray/base/assert/is-equal-data-type' ); @@ -71,35 +75,6 @@ tape( 'the function throws an error if provided a first argument which is not an } }); -tape( 'the function throws an error if provided a first argument which is not an ndarray-like object (options)', function test( t ) { - var values; - var i; - - values = [ - '5', - 5, - NaN, - true, - false, - null, - void 0, - [], - {}, - function noop() {} - ]; - - for ( i = 0; i < values.length; i++ ) { - t.throws( badValue( values[ i ] ), TypeError, 'throws an error when provided ' + values[ i ] ); - } - t.end(); - - function badValue( value ) { - return function badValue() { - last( value, {} ); - }; - } -}); - tape( 'the function throws an error if provided an options argument which is not an object', function test( t ) { var values; var x; @@ -133,6 +108,7 @@ tape( 'the function throws an error if provided an options argument which is not tape( 'the function throws an error if provided a `dims` option which is not an array of integers', function test( t ) { var values; + var opts; var x; var i; @@ -158,15 +134,17 @@ tape( 'the function throws an error if provided a `dims` option which is not an function badValue( value ) { return function badValue() { - last( x, { + opts = { 'dims': value - }); + }; + last( x, opts ); }; } }); tape( 'the function throws an error if provided a `dims` option which contains an out-of-bounds dimension index', function test( t ) { var values; + var opts; var x; var i; @@ -186,15 +164,17 @@ tape( 'the function throws an error if provided a `dims` option which contains a function badValue( value ) { return function badValue() { - last( x, { + opts = { 'dims': value - }); + }; + last( x, opts ); }; } }); -tape( 'the function throws an error if provided a `dims` option which contains duplicate indices', function test( t ) { +tape( 'the function throws an error if provided a `dims` option which contains duplicate indices after normalization', function test( t ) { var values; + var opts; var x; var i; @@ -203,7 +183,10 @@ tape( 'the function throws an error if provided a `dims` option which contains d values = [ [ 0, 0 ], [ -1, -1 ], - [ 0, 1, 0 ] + [ 0, 1, 0 ], + [ 1, -1 ], + [ 0, 1, -2 ], + [ -2, 0 ] ]; for ( i = 0; i < values.length; i++ ) { @@ -213,36 +196,42 @@ tape( 'the function throws an error if provided a `dims` option which contains d function badValue( value ) { return function badValue() { - last( x, { + opts = { 'dims': value - }); + }; + last( x, opts ); }; } }); -tape( 'the function throws an error if provided a `dims` option which contains negative-and-positive indices aliasing the same dimension', function test( t ) { - var values; - var x; - var i; +tape( 'the function throws an error if invoked without any arguments', function test( t ) { + t.throws( foo, TypeError, 'throws an error' ); + t.end(); - x = new ndarray( 'float64', zeroTo( 6, 'float64' ), [ 2, 3 ], [ 3, 1 ], 0, 'row-major' ); + function foo() { + last(); + } +}); - values = [ - [ 1, -1 ], - [ 0, 1, -2 ], - [ -2, 0 ] - ]; +tape( 'the function throws an error if provided a `dims` option for a zero-dimensional input ndarray', function test( t ) { + var opts; + var x; - for ( i = 0; i < values.length; i++ ) { - t.throws( badValue( values[ i ] ), Error, 'throws an error when provided ' + values[ i ] ); - } + x = scalar2ndarray( 3.14, { + 'dtype': 'float64', + 'order': 'row-major' + }); + + t.throws( badValue( [ 0 ] ), RangeError, 'throws an error when provided [0]' ); + t.throws( badValue( [ -1 ] ), RangeError, 'throws an error when provided [-1]' ); t.end(); function badValue( value ) { return function badValue() { - last( x, { + opts = { 'dims': value - }); + }; + last( x, opts ); }; } }); @@ -480,21 +469,6 @@ tape( 'the function returns a view of the input ndarray when `dims` is empty', f t.end(); }); -tape( 'the function returns a view sharing the input ndarray data buffer', function test( t ) { - var actual; - var x; - - x = new ndarray( 'float64', zeroTo( 6, 'float64' ), [ 2, 3 ], [ 3, 1 ], 0, 'row-major' ); - - actual = last( x, { - 'dims': [ -1 ] - }); - - t.strictEqual( getData( actual ), getData( x ), 'returns expected value' ); - - t.end(); -}); - tape( 'the function supports input ndarrays having a non-zero offset', function test( t ) { var expected; var actual; @@ -550,6 +524,7 @@ tape( 'the function supports input ndarrays having non-contiguous strides', func expected = [ 4, 10 ]; t.strictEqual( isReadOnly( actual ), true, 'returns expected value' ); t.deepEqual( ndarray2array( actual ), expected, 'returns expected value' ); + t.strictEqual( getData( actual ), getData( x ), 'returns expected value' ); actual = last( x, { 'dims': [ -2 ] @@ -589,3 +564,39 @@ tape( 'the function supports column-major input ndarrays', function test( t ) { t.end(); }); + +tape( 'the function supports complex-valued input ndarrays', function test( t ) { + var actual; + var v; + var x; + + x = new ndarray( 'complex64', new Complex64Array( [ 1.0, 2.0, 3.0, 4.0, 5.0, 6.0, 7.0, 8.0 ] ), [ 2, 2 ], [ 2, 1 ], 0, 'row-major' ); + + // Default: last scalar element (i.e., complex64 value `7+8i`): + actual = last( x ); + t.strictEqual( isReadOnly( actual ), true, 'returns expected value' ); + t.deepEqual( getShape( actual ), [], 'returns expected value' ); + t.strictEqual( isEqualDataType( getDType( actual ), getDType( x ) ), true, 'returns expected value' ); + + v = actual.get(); + t.strictEqual( v instanceof Complex64, true, 'returns expected value' ); + t.strictEqual( realf( v ), 7.0, 'returns expected value' ); + t.strictEqual( imagf( v ), 8.0, 'returns expected value' ); + + // Last column: + actual = last( x, { + 'dims': [ -1 ] + }); + t.strictEqual( isReadOnly( actual ), true, 'returns expected value' ); + t.deepEqual( getShape( actual ), [ 2 ], 'returns expected value' ); + t.strictEqual( isEqualDataType( getDType( actual ), getDType( x ) ), true, 'returns expected value' ); + + v = actual.get( 0 ); + t.strictEqual( realf( v ), 3.0, 'returns expected value' ); + t.strictEqual( imagf( v ), 4.0, 'returns expected value' ); + v = actual.get( 1 ); + t.strictEqual( realf( v ), 7.0, 'returns expected value' ); + t.strictEqual( imagf( v ), 8.0, 'returns expected value' ); + + t.end(); +}); From 450f74e6c2b7d3b2b140c846711f8a3750a8dba7 Mon Sep 17 00:00:00 2001 From: headlessNode Date: Sun, 3 May 2026 14:35:46 +0500 Subject: [PATCH 03/10] refactor: apply suggestions from code review --- lib/node_modules/@stdlib/ndarray/last/test/test.js | 4 ++-- 1 file changed, 2 insertions(+), 2 deletions(-) diff --git a/lib/node_modules/@stdlib/ndarray/last/test/test.js b/lib/node_modules/@stdlib/ndarray/last/test/test.js index cd53525b87ca..c6e90bcbbbc0 100644 --- a/lib/node_modules/@stdlib/ndarray/last/test/test.js +++ b/lib/node_modules/@stdlib/ndarray/last/test/test.js @@ -429,7 +429,7 @@ tape( 'the function supports specifying multiple, but not all, dimensions', func t.end(); }); -tape( 'the function returns the last element when `dims` includes every dimension', function test( t ) { +tape( 'the function returns the last element when `dims` includes all dimensions', function test( t ) { var actual; var x; @@ -446,7 +446,7 @@ tape( 'the function returns the last element when `dims` includes every dimensio t.end(); }); -tape( 'the function returns a view of the input ndarray when `dims` is empty', function test( t ) { +tape( 'the function returns a view of the input ndarray when `dims` includes no dimensions', function test( t ) { var expected; var actual; var x; From 1c62b4bca7d19de28da7795ff264952a90c55295 Mon Sep 17 00:00:00 2001 From: Athan Date: Sun, 3 May 2026 02:40:51 -0700 Subject: [PATCH 04/10] Apply suggestions from code review Co-authored-by: Athan Signed-off-by: Athan --- lib/node_modules/@stdlib/ndarray/last/docs/types/index.d.ts | 6 +++--- 1 file changed, 3 insertions(+), 3 deletions(-) diff --git a/lib/node_modules/@stdlib/ndarray/last/docs/types/index.d.ts b/lib/node_modules/@stdlib/ndarray/last/docs/types/index.d.ts index e116cbcc2f7b..2ce7f2021bbf 100644 --- a/lib/node_modules/@stdlib/ndarray/last/docs/types/index.d.ts +++ b/lib/node_modules/@stdlib/ndarray/last/docs/types/index.d.ts @@ -20,7 +20,7 @@ /// -import { ArrayLike } from '@stdlib/types/array'; +import { Collection } from '@stdlib/types/array'; import { ndarray } from '@stdlib/types/ndarray'; /** @@ -30,7 +30,7 @@ interface Options { /** * List of dimensions over which to perform the operation. */ - dims?: ArrayLike; + dims?: Collection; } /** @@ -65,7 +65,7 @@ interface Options { * }); * // returns [ 3.0, 4.0 ] */ -declare function last( x: ndarray, options?: Options ): ndarray; +declare function last( x: T, options?: Options ): T; // EXPORTS // From 643c10387344d340a0ec152a82091d4f5ee0ff75 Mon Sep 17 00:00:00 2001 From: Athan Date: Sun, 3 May 2026 02:41:57 -0700 Subject: [PATCH 05/10] Apply suggestions from code review Co-authored-by: Athan Signed-off-by: Athan --- .../@stdlib/ndarray/last/docs/types/test.ts | 10 +++++----- 1 file changed, 5 insertions(+), 5 deletions(-) diff --git a/lib/node_modules/@stdlib/ndarray/last/docs/types/test.ts b/lib/node_modules/@stdlib/ndarray/last/docs/types/test.ts index 451e2972726a..e3639bd5f0b0 100644 --- a/lib/node_modules/@stdlib/ndarray/last/docs/types/test.ts +++ b/lib/node_modules/@stdlib/ndarray/last/docs/types/test.ts @@ -28,11 +28,11 @@ import last = require( './index' ); { const sh = [ 2, 2 ]; - last( empty( sh, { 'dtype': 'float64' } ) ); // $ExpectType ndarray - last( empty( sh, { 'dtype': 'float64' } ), {} ); // $ExpectType ndarray - last( empty( sh, { 'dtype': 'float64' } ), { 'dims': [ -1 ] } ); // $ExpectType ndarray - last( empty( sh, { 'dtype': 'complex64' } ), { 'dims': [ 0 ] } ); // $ExpectType ndarray - last( empty( sh, { 'dtype': 'int32' } ), { 'dims': [ 0, 1 ] } ); // $ExpectType ndarray + last( empty( sh, { 'dtype': 'float64' } ) ); // $ExpectType float64ndarray + last( empty( sh, { 'dtype': 'float64' } ), {} ); // $ExpectType float64ndarray + last( empty( sh, { 'dtype': 'float64' } ), { 'dims': [ -1 ] } ); // $ExpectType float64ndarray + last( empty( sh, { 'dtype': 'complex64' } ), { 'dims': [ 0 ] } ); // $ExpectType complex64ndarray + last( empty( sh, { 'dtype': 'int32' } ), { 'dims': [ 0, 1 ] } ); // $ExpectType int32ndarray } // The compiler throws an error if the function is provided a first argument which is not an ndarray... From 167e8bb03f0041a011b3474632002b4833dcf7eb Mon Sep 17 00:00:00 2001 From: Athan Date: Sun, 3 May 2026 02:49:23 -0700 Subject: [PATCH 06/10] Apply suggestions from code review Co-authored-by: Athan Signed-off-by: Athan --- lib/node_modules/@stdlib/ndarray/last/README.md | 4 ++-- lib/node_modules/@stdlib/ndarray/last/examples/index.js | 4 ++-- 2 files changed, 4 insertions(+), 4 deletions(-) diff --git a/lib/node_modules/@stdlib/ndarray/last/README.md b/lib/node_modules/@stdlib/ndarray/last/README.md index e9f377011ac6..a0723ffc1b76 100644 --- a/lib/node_modules/@stdlib/ndarray/last/README.md +++ b/lib/node_modules/@stdlib/ndarray/last/README.md @@ -122,13 +122,13 @@ console.log( ndarray2array( x ) ); var v = last( x ); console.log( v.get() ); -// Last "row" along the innermost dimension: +// Last column along the innermost dimension: v = last( x, { 'dims': [ -1 ] }); console.log( ndarray2array( v ) ); -// Last "matrix" along the outermost dimension: +// Last matrix along the outermost dimension: v = last( x, { 'dims': [ 0 ] }); diff --git a/lib/node_modules/@stdlib/ndarray/last/examples/index.js b/lib/node_modules/@stdlib/ndarray/last/examples/index.js index a76e58054404..2a92cd712001 100644 --- a/lib/node_modules/@stdlib/ndarray/last/examples/index.js +++ b/lib/node_modules/@stdlib/ndarray/last/examples/index.js @@ -29,13 +29,13 @@ console.log( ndarray2array( x ) ); var v = last( x ); console.log( v.get() ); -// Last "row" along the innermost dimension: +// Last column along the innermost dimension: v = last( x, { 'dims': [ -1 ] }); console.log( ndarray2array( v ) ); -// Last "matrix" along the outermost dimension: +// Last matrix along the outermost dimension: v = last( x, { 'dims': [ 0 ] }); From c914032d3e285d6023e2eebcfbabbb52b0f022b8 Mon Sep 17 00:00:00 2001 From: Athan Date: Sun, 3 May 2026 02:55:06 -0700 Subject: [PATCH 07/10] Apply suggestions from code review Co-authored-by: Athan Signed-off-by: Athan --- lib/node_modules/@stdlib/ndarray/last/README.md | 2 +- lib/node_modules/@stdlib/ndarray/last/examples/index.js | 2 +- 2 files changed, 2 insertions(+), 2 deletions(-) diff --git a/lib/node_modules/@stdlib/ndarray/last/README.md b/lib/node_modules/@stdlib/ndarray/last/README.md index a0723ffc1b76..602433423902 100644 --- a/lib/node_modules/@stdlib/ndarray/last/README.md +++ b/lib/node_modules/@stdlib/ndarray/last/README.md @@ -122,7 +122,7 @@ console.log( ndarray2array( x ) ); var v = last( x ); console.log( v.get() ); -// Last column along the innermost dimension: +// Last columns along the innermost dimension: v = last( x, { 'dims': [ -1 ] }); diff --git a/lib/node_modules/@stdlib/ndarray/last/examples/index.js b/lib/node_modules/@stdlib/ndarray/last/examples/index.js index 2a92cd712001..e07814d6e34e 100644 --- a/lib/node_modules/@stdlib/ndarray/last/examples/index.js +++ b/lib/node_modules/@stdlib/ndarray/last/examples/index.js @@ -29,7 +29,7 @@ console.log( ndarray2array( x ) ); var v = last( x ); console.log( v.get() ); -// Last column along the innermost dimension: +// Last columns along the innermost dimension: v = last( x, { 'dims': [ -1 ] }); From ac8d781c84dea85226fa1421444c681c2101c011 Mon Sep 17 00:00:00 2001 From: Athan Date: Sun, 3 May 2026 02:56:22 -0700 Subject: [PATCH 08/10] Apply suggestions from code review Co-authored-by: Athan Signed-off-by: Athan --- lib/node_modules/@stdlib/ndarray/last/lib/main.js | 6 ++---- 1 file changed, 2 insertions(+), 4 deletions(-) diff --git a/lib/node_modules/@stdlib/ndarray/last/lib/main.js b/lib/node_modules/@stdlib/ndarray/last/lib/main.js index cda0d2d3a3e2..ab46f661ff68 100644 --- a/lib/node_modules/@stdlib/ndarray/last/lib/main.js +++ b/lib/node_modules/@stdlib/ndarray/last/lib/main.js @@ -25,6 +25,7 @@ var args2multislice = require( '@stdlib/slice/base/args2multislice' ); var base = require( '@stdlib/ndarray/base/slice' ); var getShape = require( '@stdlib/ndarray/shape' ); var zeroTo = require( '@stdlib/array/base/zero-to' ); +var nulls = require( '@stdlib/array/base/nulls' ); var objectAssign = require( '@stdlib/object/assign' ); var format = require( '@stdlib/string/format' ); var defaults = require( './defaults.json' ); @@ -102,10 +103,7 @@ function last( x, options ) { dims = opts.dims; } // Build a list of slice arguments such that each dimension in `dims` resolves to its last index and all other dimensions are kept in full: - args = []; - for ( i = 0; i < N; i++ ) { - args.push( null ); - } + args = nulls( N ); for ( i = 0; i < dims.length; i++ ) { args[ dims[ i ] ] = sh[ dims[ i ] ] - 1; } From 2af701784797f7f6fe4440c3270eb32b8931be2e Mon Sep 17 00:00:00 2001 From: headlessNode Date: Sun, 3 May 2026 16:23:58 +0500 Subject: [PATCH 09/10] fix: apply suggestions from code review --- lib/node_modules/@stdlib/ndarray/last/README.md | 3 +-- lib/node_modules/@stdlib/ndarray/last/docs/repl.txt | 8 +++++--- 2 files changed, 6 insertions(+), 5 deletions(-) diff --git a/lib/node_modules/@stdlib/ndarray/last/README.md b/lib/node_modules/@stdlib/ndarray/last/README.md index 602433423902..e4082d293759 100644 --- a/lib/node_modules/@stdlib/ndarray/last/README.md +++ b/lib/node_modules/@stdlib/ndarray/last/README.md @@ -61,7 +61,7 @@ The function accepts the following arguments: The function accepts the following `options`: -- **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]. +- **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. To resolve the last element (or subarray) along one or more specific dimensions, provide a `dims` option: @@ -95,7 +95,6 @@ v = last( x, { ## Notes - 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]). -- 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]. - If provided an empty `dims` array, the function returns a read-only view of the input [`ndarray`][@stdlib/ndarray/ctor]. diff --git a/lib/node_modules/@stdlib/ndarray/last/docs/repl.txt b/lib/node_modules/@stdlib/ndarray/last/docs/repl.txt index 7225da1a67e6..5310eed5ffea 100644 --- a/lib/node_modules/@stdlib/ndarray/last/docs/repl.txt +++ b/lib/node_modules/@stdlib/ndarray/last/docs/repl.txt @@ -15,9 +15,11 @@ Function options. options.dims: Array (optional) - List of dimensions over which to perform the operation. If not provided, - the function performs the operation over all dimensions and thus returns - the last element of the input ndarray as a zero-dimensional ndarray. + 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. Returns ------- From 1f43df7ad23f6b5df27e672fcee168a1f9fa1d93 Mon Sep 17 00:00:00 2001 From: Athan Date: Sun, 3 May 2026 04:36:03 -0700 Subject: [PATCH 10/10] Apply suggestions from code review Co-authored-by: Athan Signed-off-by: Athan --- lib/node_modules/@stdlib/ndarray/last/docs/repl.txt | 3 +-- 1 file changed, 1 insertion(+), 2 deletions(-) diff --git a/lib/node_modules/@stdlib/ndarray/last/docs/repl.txt b/lib/node_modules/@stdlib/ndarray/last/docs/repl.txt index 5310eed5ffea..31d91776d9aa 100644 --- a/lib/node_modules/@stdlib/ndarray/last/docs/repl.txt +++ b/lib/node_modules/@stdlib/ndarray/last/docs/repl.txt @@ -24,8 +24,7 @@ Returns ------- out: ndarray - Output ndarray. The returned ndarray is a read-only view of the input - ndarray. + Output ndarray. Examples --------