diff --git a/lib/node_modules/@stdlib/ndarray/first/README.md b/lib/node_modules/@stdlib/ndarray/first/README.md new file mode 100644 index 000000000000..5d08feb37f48 --- /dev/null +++ b/lib/node_modules/@stdlib/ndarray/first/README.md @@ -0,0 +1,171 @@ + + +# first + +> Return a read-only view of the first element (or subarray) along one or more [`ndarray`][@stdlib/ndarray/ctor] dimensions. + + + +
+ +
+ + + + + +
+ +## Usage + +```javascript +var first = require( '@stdlib/ndarray/first' ); +``` + +#### first( x\[, options] ) + +Returns a read-only view of the first 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 = first( x ); +// returns [ 1.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. 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 first element (or subarray) along one or more specified 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 ] ] + +// First column: +var v = first( x, { + 'dims': [ -1 ] +}); +// returns [ 1.0, 3.0 ] + +// First row: +v = first( x, { + 'dims': [ -2 ] +}); +// returns [ 1.0, 2.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]). +- If provided an empty `dims` array, 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 first = require( '@stdlib/ndarray/first' ); + +var x = uniform( [ 3, 3, 3 ], -10.0, 10.0 ); +console.log( ndarray2array( x ) ); + +// First scalar element: +var v = first( x ); +console.log( v.get() ); + +// First columns along the innermost dimension: +v = first( x, { + 'dims': [ -1 ] +}); +console.log( ndarray2array( v ) ); + +// First matrix along the outermost dimension: +v = first( x, { + 'dims': [ 0 ] +}); +console.log( ndarray2array( v ) ); +``` + +
+ + + + + +
+ +
+ + + + + + + + + + + + + + diff --git a/lib/node_modules/@stdlib/ndarray/first/benchmark/benchmark.js b/lib/node_modules/@stdlib/ndarray/first/benchmark/benchmark.js new file mode 100644 index 000000000000..f736624ec12b --- /dev/null +++ b/lib/node_modules/@stdlib/ndarray/first/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 first = 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 = first( 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 = first( 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 = first( 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 = first( 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 = first( 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 = first( 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/first/docs/repl.txt b/lib/node_modules/@stdlib/ndarray/first/docs/repl.txt new file mode 100644 index 000000000000..7126a7418c7b --- /dev/null +++ b/lib/node_modules/@stdlib/ndarray/first/docs/repl.txt @@ -0,0 +1,42 @@ + +{{alias}}( x[, options] ) + Returns a read-only view of the first element (or subarray) along one or + more ndarray dimensions. + + If provided an empty `dims` array, 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. 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 + ------- + out: ndarray + Output 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 ) + [ 1.0 ] + > v = {{alias}}( x, { 'dims': [ -1 ] } ) + [ 1.0, 3.0 ] + > v = {{alias}}( x, { 'dims': [ -2 ] } ) + [ 1.0, 2.0 ] + + See Also + -------- + diff --git a/lib/node_modules/@stdlib/ndarray/first/docs/types/index.d.ts b/lib/node_modules/@stdlib/ndarray/first/docs/types/index.d.ts new file mode 100644 index 000000000000..328ba6fd2a2d --- /dev/null +++ b/lib/node_modules/@stdlib/ndarray/first/docs/types/index.d.ts @@ -0,0 +1,73 @@ +/* +* @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 { Collection } 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?: Collection; +} + +/** +* Returns a read-only view of the first element (or subarray) along one or more ndarray dimensions. +* +* ## Notes +* +* - By default, the function performs the operation over all dimensions and thus returns the first 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. +* +* @param x - input ndarray +* @param options - function options +* @param options.dims - list of dimensions over which to perform the operation +* @returns output ndarray +* +* @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 = first( x ); +* // returns [ 1.0 ] +* +* v = first( x, { +* 'dims': [ -1 ] +* }); +* // returns [ 1.0, 3.0 ] +* +* v = first( x, { +* 'dims': [ -2 ] +* }); +* // returns [ 1.0, 2.0 ] +*/ +declare function first( x: T, options?: Options ): T; + + +// EXPORTS // + +export = first; diff --git a/lib/node_modules/@stdlib/ndarray/first/docs/types/test.ts b/lib/node_modules/@stdlib/ndarray/first/docs/types/test.ts new file mode 100644 index 000000000000..c1324e5d3be9 --- /dev/null +++ b/lib/node_modules/@stdlib/ndarray/first/docs/types/test.ts @@ -0,0 +1,92 @@ +/* +* @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/empty' ); +import first = require( './index' ); + + +// TESTS // + +// The function returns an ndarray... +{ + const sh = [ 2, 2 ]; + + first( empty( sh, { 'dtype': 'float64' } ) ); // $ExpectType float64ndarray + first( empty( sh, { 'dtype': 'float64' } ), {} ); // $ExpectType float64ndarray + first( empty( sh, { 'dtype': 'float64' } ), { 'dims': [ -1 ] } ); // $ExpectType float64ndarray + first( empty( sh, { 'dtype': 'complex64' } ), { 'dims': [ 0 ] } ); // $ExpectType complex64ndarray + first( 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... +{ + first( '10' ); // $ExpectError + first( 10 ); // $ExpectError + first( false ); // $ExpectError + first( true ); // $ExpectError + first( null ); // $ExpectError + first( [] ); // $ExpectError + first( {} ); // $ExpectError + first( ( x: number ): number => x ); // $ExpectError + + first( '10', {} ); // $ExpectError + first( 10, {} ); // $ExpectError + first( false, {} ); // $ExpectError + first( true, {} ); // $ExpectError + first( null, {} ); // $ExpectError + first( [], {} ); // $ExpectError + first( {}, {} ); // $ExpectError + first( ( 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( [ 2, 2 ], { 'dtype': 'float64' } ); + + first( x, '10' ); // $ExpectError + first( x, 10 ); // $ExpectError + first( x, false ); // $ExpectError + first( x, true ); // $ExpectError + first( x, null ); // $ExpectError + first( x, [] ); // $ExpectError + first( 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( [ 2, 2 ], { 'dtype': 'float64' } ); + + first( x, { 'dims': '10' } ); // $ExpectError + first( x, { 'dims': 10 } ); // $ExpectError + first( x, { 'dims': false } ); // $ExpectError + first( x, { 'dims': true } ); // $ExpectError + first( x, { 'dims': null } ); // $ExpectError + first( x, { 'dims': [ '1' ] } ); // $ExpectError + first( x, { 'dims': {} } ); // $ExpectError + first( 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( [ 2, 2 ], { 'dtype': 'float64' } ); + + first(); // $ExpectError + first( x, {}, {} ); // $ExpectError +} diff --git a/lib/node_modules/@stdlib/ndarray/first/examples/index.js b/lib/node_modules/@stdlib/ndarray/first/examples/index.js new file mode 100644 index 000000000000..a979928df9c0 --- /dev/null +++ b/lib/node_modules/@stdlib/ndarray/first/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 first = require( './../lib' ); + +var x = uniform( [ 3, 3, 3 ], -10.0, 10.0 ); +console.log( ndarray2array( x ) ); + +// First scalar element: +var v = first( x ); +console.log( v.get() ); + +// First columns along the innermost dimension: +v = first( x, { + 'dims': [ -1 ] +}); +console.log( ndarray2array( v ) ); + +// First matrix along the outermost dimension: +v = first( x, { + 'dims': [ 0 ] +}); +console.log( ndarray2array( v ) ); diff --git a/lib/node_modules/@stdlib/ndarray/first/lib/defaults.json b/lib/node_modules/@stdlib/ndarray/first/lib/defaults.json new file mode 100644 index 000000000000..d8cc9c72ad33 --- /dev/null +++ b/lib/node_modules/@stdlib/ndarray/first/lib/defaults.json @@ -0,0 +1,3 @@ +{ + "dims": null +} diff --git a/lib/node_modules/@stdlib/ndarray/first/lib/index.js b/lib/node_modules/@stdlib/ndarray/first/lib/index.js new file mode 100644 index 000000000000..25d56f14b97a --- /dev/null +++ b/lib/node_modules/@stdlib/ndarray/first/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 first element (or subarray) along one or more ndarray dimensions. +* +* @module @stdlib/ndarray/first +* +* @example +* var array = require( '@stdlib/ndarray/array' ); +* var first = require( '@stdlib/ndarray/first' ); +* +* var x = array( [ [ 1.0, 2.0 ], [ 3.0, 4.0 ] ] ); +* // returns [ [ 1.0, 2.0 ], [ 3.0, 4.0 ] ] +* +* var v = first( x ); +* // returns [ 1.0 ] +* +* v = first( x, { +* 'dims': [ -1 ] +* }); +* // returns [ 1.0, 3.0 ] +* +* v = first( x, { +* 'dims': [ -2 ] +* }); +* // returns [ 1.0, 2.0 ] +*/ + +// MODULES // + +var main = require( './main.js' ); + + +// EXPORTS // + +module.exports = main; diff --git a/lib/node_modules/@stdlib/ndarray/first/lib/main.js b/lib/node_modules/@stdlib/ndarray/first/lib/main.js new file mode 100644 index 000000000000..555756968e61 --- /dev/null +++ b/lib/node_modules/@stdlib/ndarray/first/lib/main.js @@ -0,0 +1,116 @@ +/** +* @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 nulls = require( '@stdlib/array/base/nulls' ); +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 first element (or subarray) along one or more ndarray dimensions. +* +* ## Notes +* +* - By default, the function performs the operation over all dimensions and thus returns the first 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. +* +* @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 = first( x ); +* // returns [ 1.0 ] +* +* v = first( x, { +* 'dims': [ -1 ] +* }); +* // returns [ 1.0, 3.0 ] +* +* v = first( x, { +* 'dims': [ -2 ] +* }); +* // returns [ 1.0, 2.0 ] +*/ +function first( 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; + + // Resolve options: + opts = objectAssign( {}, defaults ); + if ( arguments.length > 1 ) { + err = validate( opts, N, options ); + if ( err ) { + throw err; + } + } + if ( N === 0 ) { + return base( x, args2multislice( [] ), true, false ); + } + 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 first index and all other dimensions are kept in full: + args = nulls( N ); + for ( i = 0; i < dims.length; i++ ) { + args[ dims[ i ] ] = 0; + } + return base( x, args2multislice( args ), true, false ); +} + + +// EXPORTS // + +module.exports = first; diff --git a/lib/node_modules/@stdlib/ndarray/first/lib/validate.js b/lib/node_modules/@stdlib/ndarray/first/lib/validate.js new file mode 100644 index 000000000000..1fb9e9e2162b --- /dev/null +++ b/lib/node_modules/@stdlib/ndarray/first/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/first/package.json b/lib/node_modules/@stdlib/ndarray/first/package.json new file mode 100644 index 000000000000..ef839867f4c6 --- /dev/null +++ b/lib/node_modules/@stdlib/ndarray/first/package.json @@ -0,0 +1,66 @@ +{ + "name": "@stdlib/ndarray/first", + "version": "0.0.0", + "description": "Return a read-only view of the first 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", + "first", + "head", + "slice", + "view", + "subarray" + ] +} diff --git a/lib/node_modules/@stdlib/ndarray/first/test/test.js b/lib/node_modules/@stdlib/ndarray/first/test/test.js new file mode 100644 index 000000000000..16e864175461 --- /dev/null +++ b/lib/node_modules/@stdlib/ndarray/first/test/test.js @@ -0,0 +1,602 @@ +/** +* @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 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' ); +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 first = require( './../lib' ); + + +// TESTS // + +tape( 'main export is a function', function test( t ) { + t.ok( true, __filename ); + t.strictEqual( typeof first, '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() { + first( 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() { + first( 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 opts; + 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() { + opts = { + 'dims': value + }; + first( 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; + + 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() { + opts = { + 'dims': value + }; + first( x, opts ); + }; + } +}); + +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; + + x = new ndarray( 'float64', zeroTo( 6, 'float64' ), [ 2, 3 ], [ 3, 1 ], 0, 'row-major' ); + + values = [ + [ 0, 0 ], + [ -1, -1 ], + [ 0, 1, 0 ], + [ 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() { + opts = { + 'dims': value + }; + first( x, opts ); + }; + } +}); + +tape( 'the function throws an error if invoked without any arguments', function test( t ) { + t.throws( foo, TypeError, 'throws an error' ); + t.end(); + + function foo() { + first(); + } +}); + +tape( 'the function throws an error if provided a `dims` option for a zero-dimensional input ndarray', function test( t ) { + var opts; + var x; + + 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() { + opts = { + 'dims': value + }; + first( x, opts ); + }; + } +}); + +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 = first( 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 first 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 = first( 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(), 0, 'returns expected value' ); + t.strictEqual( isEqualDataType( getDType( actual ), getDType( x ) ), true, 'returns expected value' ); + + t.end(); +}); + +tape( 'the function returns the first 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 = first( 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(), 0, 'returns expected value' ); + t.strictEqual( isEqualDataType( getDType( actual ), getDType( x ) ), true, 'returns expected value' ); + + t.end(); +}); + +tape( 'the function returns the first 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' ); + + // First column: + actual = first( x, { + 'dims': [ -1 ] + }); + expected = [ 0, 3 ]; + + t.strictEqual( isReadOnly( actual ), true, 'returns expected value' ); + t.deepEqual( getShape( actual ), [ 2 ], 'returns expected value' ); + t.deepEqual( ndarray2array( actual ), expected, 'returns expected value' ); + + // First row: + actual = first( x, { + 'dims': [ -2 ] + }); + expected = [ 0, 1, 2 ]; + + 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 first 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' ); + + // First row (dim 0): + actual = first( x, { + 'dims': [ 0 ] + }); + expected = [ 0, 1, 2 ]; + + 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' ); + + // First column (dim 1): + actual = first( x, { + 'dims': [ 1 ] + }); + expected = [ 0, 3 ]; + + 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: first scalar: + actual = first( x ); + t.strictEqual( isReadOnly( actual ), true, 'returns expected value' ); + t.deepEqual( getShape( actual ), [], 'returns expected value' ); + t.strictEqual( actual.get(), 0, 'returns expected value' ); + t.strictEqual( isEqualDataType( getDType( actual ), getDType( x ) ), true, 'returns expected value' ); + + // Take first along innermost dim: + actual = first( x, { + 'dims': [ -1 ] + }); + expected = [ + [ 0, 4, 8 ], + [ 12, 16, 20 ] + ]; + 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 first along outermost dim: + actual = first( x, { + 'dims': [ 0 ] + }); + expected = [ + [ 0, 1, 2, 3 ], + [ 4, 5, 6, 7 ], + [ 8, 9, 10, 11 ] + ]; + 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 first along middle dim: + actual = first( x, { + 'dims': [ 1 ] + }); + expected = [ + [ 0, 1, 2, 3 ], + [ 12, 13, 14, 15 ] + ]; + 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 first along dims 0 and 2 (keep dim 1): + actual = first( x, { + 'dims': [ 0, -1 ] + }); + expected = [ 0, 4, 8 ]; + + 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 first element when `dims` includes all dimensions', function test( t ) { + var actual; + var x; + + x = new ndarray( 'float64', zeroTo( 6, 'float64' ), [ 2, 3 ], [ 3, 1 ], 0, 'row-major' ); + + actual = first( x, { + 'dims': [ -2, -1 ] + }); + + t.strictEqual( isReadOnly( actual ), true, 'returns expected value' ); + t.deepEqual( getShape( actual ), [], 'returns expected value' ); + t.strictEqual( actual.get(), 0, 'returns expected value' ); + + t.end(); +}); + +tape( 'the function returns a view of the input ndarray when `dims` includes no dimensions', function test( t ) { + var expected; + var actual; + var x; + + x = new ndarray( 'float64', zeroTo( 6, 'float64' ), [ 2, 3 ], [ 3, 1 ], 0, 'row-major' ); + + actual = first( 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 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: first element of the offset view: + actual = first( x ); + t.strictEqual( isReadOnly( actual ), true, 'returns expected value' ); + t.deepEqual( getShape( actual ), [], 'returns expected value' ); + t.strictEqual( actual.get(), 2, 'returns expected value' ); + + // First column of the offset view: + actual = first( 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' ); + + // First row of the offset view: + actual = first( x, { + 'dims': [ -2 ] + }); + expected = [ 2, 3, 4 ]; + 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 = first( x ); + t.strictEqual( isReadOnly( actual ), true, 'returns expected value' ); + t.deepEqual( getShape( actual ), [], 'returns expected value' ); + t.strictEqual( actual.get(), 0, 'returns expected value' ); + + actual = first( x, { + 'dims': [ -1 ] + }); + expected = [ 0, 6 ]; + 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 = first( x, { + 'dims': [ -2 ] + }); + expected = [ 0, 2, 4 ]; + 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 = first( x, { + 'dims': [ -1 ] + }); + expected = [ 0, 1 ]; + + 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 = first( x, { + 'dims': [ -2 ] + }); + expected = [ 0, 2, 4 ]; + + 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 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: first scalar element (i.e., complex64 value `1+2i`): + actual = first( 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 ), 1.0, 'returns expected value' ); + t.strictEqual( imagf( v ), 2.0, 'returns expected value' ); + + // First column: + actual = first( 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 ), 1.0, 'returns expected value' ); + t.strictEqual( imagf( v ), 2.0, 'returns expected value' ); + v = actual.get( 1 ); + t.strictEqual( realf( v ), 5.0, 'returns expected value' ); + t.strictEqual( imagf( v ), 6.0, 'returns expected value' ); + + t.end(); +});