From baf2515f24f7f89c80afab561046bd85f3c59f27 Mon Sep 17 00:00:00 2001 From: headlessNode Date: 2025年9月21日 19:07:50 +0500 Subject: [PATCH 1/7] feat: add ndarray/base/shift --- 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/base/shift/README.md | 153 ++++++++++ .../ndarray/base/shift/benchmark/benchmark.js | 190 +++++++++++++ .../@stdlib/ndarray/base/shift/docs/repl.txt | 41 +++ .../ndarray/base/shift/docs/types/index.d.ts | 174 ++++++++++++ .../ndarray/base/shift/docs/types/test.ts | 95 +++++++ .../ndarray/base/shift/examples/index.js | 40 +++ .../@stdlib/ndarray/base/shift/lib/index.js | 59 ++++ .../@stdlib/ndarray/base/shift/lib/main.js | 92 ++++++ .../@stdlib/ndarray/base/shift/package.json | 67 +++++ .../@stdlib/ndarray/base/shift/test/test.js | 261 ++++++++++++++++++ 10 files changed, 1172 insertions(+) create mode 100644 lib/node_modules/@stdlib/ndarray/base/shift/README.md create mode 100644 lib/node_modules/@stdlib/ndarray/base/shift/benchmark/benchmark.js create mode 100644 lib/node_modules/@stdlib/ndarray/base/shift/docs/repl.txt create mode 100644 lib/node_modules/@stdlib/ndarray/base/shift/docs/types/index.d.ts create mode 100644 lib/node_modules/@stdlib/ndarray/base/shift/docs/types/test.ts create mode 100644 lib/node_modules/@stdlib/ndarray/base/shift/examples/index.js create mode 100644 lib/node_modules/@stdlib/ndarray/base/shift/lib/index.js create mode 100644 lib/node_modules/@stdlib/ndarray/base/shift/lib/main.js create mode 100644 lib/node_modules/@stdlib/ndarray/base/shift/package.json create mode 100644 lib/node_modules/@stdlib/ndarray/base/shift/test/test.js diff --git a/lib/node_modules/@stdlib/ndarray/base/shift/README.md b/lib/node_modules/@stdlib/ndarray/base/shift/README.md new file mode 100644 index 000000000000..de176f895682 --- /dev/null +++ b/lib/node_modules/@stdlib/ndarray/base/shift/README.md @@ -0,0 +1,153 @@ + + +# shift + +> Return an array containing a truncated view of an input ndarray and a complementary view of the first element(s) along a specified dimension. + + + +
+ +
+ + + + + +
+ +## Usage + +```javascript +var shift = require( '@stdlib/ndarray/base/shift' ); +``` + +#### shift( x, dim, strict, writable ) + +Returns an array containing a truncated view of an input ndarray and a complementary view of the first element(s) along a specified dimension. + +```javascript +var ndarray = require( '@stdlib/ndarray/ctor' ); +var ndarray2array = require( '@stdlib/ndarray/to-array' ); + +var buffer = [ 1.0, 2.0, 3.0, 4.0, 5.0, 6.0 ]; +var shape = [ 3, 2 ]; +var strides = [ 2, 1 ]; +var offset = 0; + +var x = ndarray( 'generic', buffer, shape, strides, offset, 'row-major' ); +// returns + +var sh = x.shape; +// returns [ 3, 2 ] + +var arr = ndarray2array( x ); +// returns [ [ 1.0, 2.0 ], [ 3.0, 4.0 ], [ 5.0, 6.0 ] ] + +var y = shift( x, 0, false, false ); +// returns [ , ] + +arr = ndarray2array( y[ 0 ] ); +// returns [ [ 3.0, 4.0 ], [ 5.0, 6.0 ] ] + +arr = ndarray2array( y[ 1 ] ); +// returns [ [ 1.0, 2.0 ] ] +``` + +The function accepts the following arguments: + +- **x**: input ndarray. +- **dim**: dimension along which to perform the operation. If provided an integer less than zero, the dimension index is resolved relative to the last dimension, with the last dimension corresponding to the value `-1`. +- **strict**: boolean indicating whether to enforce strict bounds checking. +- **writable**: boolean indicating whether a returned ndarray should be writable. + +
+ + + + + +
+ +## Notes + +- The `writable` parameter **only** applies to ndarray constructors supporting **read-only** instances. + +
+ + + + + +
+ +## Examples + + + +```javascript +var array = require( '@stdlib/ndarray/array' ); +var ndarray2array = require( '@stdlib/ndarray/to-array' ); +var zeroTo = require( '@stdlib/array/base/zero-to' ); +var shift = require( '@stdlib/ndarray/base/shift' ); + +// Create a linear ndarray buffer: +var buf = zeroTo( 27 ); + +// Create an ndarray which is a stack of three 3x3 matrices: +var x = array( buf, { + 'shape': [ 3, 3, 3 ] +}); + +var y = shift( x, 1, false, false ); +// returns [ , ] + +console.log( ndarray2array( y[ 0 ] ) ); +console.log( ndarray2array( y[ 1 ] ) ); +``` + +
+ + + + + +
+ +
+ + + + + +
+ +
+ + + + + +
+ +
+ + diff --git a/lib/node_modules/@stdlib/ndarray/base/shift/benchmark/benchmark.js b/lib/node_modules/@stdlib/ndarray/base/shift/benchmark/benchmark.js new file mode 100644 index 000000000000..d049b8438b17 --- /dev/null +++ b/lib/node_modules/@stdlib/ndarray/base/shift/benchmark/benchmark.js @@ -0,0 +1,190 @@ +/** +* @license Apache-2.0 +* +* Copyright (c) 2025 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 pkg = require( './../package.json' ).name; +var shift = require( './../lib' ); + + +// MAIN // + +bench( pkg+'::1d', function benchmark( b ) { + var values; + var v; + var i; + + /* eslint-disable object-curly-newline, stdlib/line-closing-bracket-spacing */ + + values = [ + empty( [ 2 ], { 'dtype': 'float64' } ), + empty( [ 2 ], { 'dtype': 'float32' } ), + empty( [ 2 ], { 'dtype': 'int32' } ), + empty( [ 2 ], { 'dtype': 'complex128' } ), + empty( [ 2 ], { 'dtype': 'generic' } ) + ]; + + /* eslint-enable object-curly-newline, stdlib/line-closing-bracket-spacing */ + + b.tic(); + for ( i = 0; i < b.iterations; i++ ) { + v = shift( values[ i%values.length ], 0, false, false ); + if ( typeof v !== 'object' ) { + b.fail( 'should return an array of ndarrays' ); + } + } + b.toc(); + if ( !isndarrayLike( v[0] ) || !isndarrayLike( v[1] ) ) { + b.fail( 'should return an array of ndarrays' ); + } + b.pass( 'benchmark finished' ); + b.end(); +}); + +bench( pkg+'::2d', function benchmark( b ) { + var values; + var v; + var i; + + /* eslint-disable object-curly-newline */ + + 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 */ + + b.tic(); + for ( i = 0; i < b.iterations; i++ ) { + v = shift( values[ i%values.length ], 0, false, false ); + if ( typeof v !== 'object' ) { + b.fail( 'should return an array of ndarrays' ); + } + } + b.toc(); + if ( !isndarrayLike( v[0] ) || !isndarrayLike( v[1] ) ) { + b.fail( 'should return an array of ndarrays' ); + } + b.pass( 'benchmark finished' ); + b.end(); +}); + +bench( pkg+'::3d', function benchmark( b ) { + var values; + var v; + var i; + + /* eslint-disable object-curly-newline */ + + 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 */ + + b.tic(); + for ( i = 0; i < b.iterations; i++ ) { + v = shift( values[ i%values.length ], 0, false, false ); + if ( typeof v !== 'object' ) { + b.fail( 'should return an array of ndarrays' ); + } + } + b.toc(); + if ( !isndarrayLike( v[0] ) || !isndarrayLike( v[1] ) ) { + b.fail( 'should return an array of ndarrays' ); + } + b.pass( 'benchmark finished' ); + b.end(); +}); + +bench( pkg+'::4d', function benchmark( b ) { + var values; + var v; + var i; + + /* eslint-disable object-curly-newline */ + + 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 */ + + b.tic(); + for ( i = 0; i < b.iterations; i++ ) { + v = shift( values[ i%values.length ], 0, false, false ); + if ( typeof v !== 'object' ) { + b.fail( 'should return an array of ndarrays' ); + } + } + b.toc(); + if ( !isndarrayLike( v[0] ) || !isndarrayLike( v[1] ) ) { + b.fail( 'should return an array of ndarrays' ); + } + b.pass( 'benchmark finished' ); + b.end(); +}); + +bench( pkg+'::5d', function benchmark( b ) { + var values; + var v; + var i; + + /* eslint-disable object-curly-newline */ + + 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 */ + + b.tic(); + for ( i = 0; i < b.iterations; i++ ) { + v = shift( values[ i%values.length ], 0, false, false ); + if ( typeof v !== 'object' ) { + b.fail( 'should return an array of ndarrays' ); + } + } + b.toc(); + if ( !isndarrayLike( v[0] ) || !isndarrayLike( v[1] ) ) { + b.fail( 'should return an array of ndarrays' ); + } + b.pass( 'benchmark finished' ); + b.end(); +}); diff --git a/lib/node_modules/@stdlib/ndarray/base/shift/docs/repl.txt b/lib/node_modules/@stdlib/ndarray/base/shift/docs/repl.txt new file mode 100644 index 000000000000..a7db648f8116 --- /dev/null +++ b/lib/node_modules/@stdlib/ndarray/base/shift/docs/repl.txt @@ -0,0 +1,41 @@ + +{{alias}}( x, dim, strict, writable ) + Returns an array containing a truncated view of an input ndarray and a + complementary view of the first element(s) along a specified dimension. + + Parameters + ---------- + x: ndarray + Input array. + + dim: integer + Dimension along which to perform the operation. If provided an integer + less than zero, the dimension index is resolved relative to the last + dimension, with the last dimension corresponding to the value `-1`. + + strict: boolean + Boolean indicating whether to enforce strict bounds checking. + + writable: boolean + Boolean indicating whether a returned ndarray should be writable. This + parameter only applies to ndarray constructors which support read-only + instances. + + Returns + ------- + out: array + Output array. + + Examples + -------- +> var x = {{alias:@stdlib/ndarray/array}}( [ [ 1, 2 ], [ 3, 4 ] ] ) + +> var y = {{alias}}( x, 1, false, false ) + [ , ] +> {{alias:@stdlib/ndarray/to-array}}( y[0] ) + [ [ 2 ], [ 4 ] ] +> {{alias:@stdlib/ndarray/to-array}}( y[1] ) + [ [ 1 ], [ 3 ] ] + + See Also + -------- diff --git a/lib/node_modules/@stdlib/ndarray/base/shift/docs/types/index.d.ts b/lib/node_modules/@stdlib/ndarray/base/shift/docs/types/index.d.ts new file mode 100644 index 000000000000..ac6ac1ce6008 --- /dev/null +++ b/lib/node_modules/@stdlib/ndarray/base/shift/docs/types/index.d.ts @@ -0,0 +1,174 @@ +/* +* @license Apache-2.0 +* +* Copyright (c) 2025 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 { typedndarray, genericndarray } from '@stdlib/types/ndarray'; +import { ComplexLike } from '@stdlib/types/complex'; +import { ArrayLike } from '@stdlib/types/array'; + + +/** +* Returns an array containing a truncated view of an input ndarray and a complementary view of the first element(s) along a specified dimension. +* +* @param x - input array +* @param dim - dimension along which to perform the operation +* @param strict - boolean indicating whether to enforce strict bounds checking +* @param writable - boolean indicating whether returned arrays should be writable +* @returns a list of ndarrays +* +* @example +* var Float64Array = require( '@stdlib/array/float64' ); +* var ndarray = require( '@stdlib/ndarray/ctor' ); +* var ndarray2array = require( '@stdlib/ndarray/to-array' ); +* +* var buffer = new Float64Array( [ 1.0, 2.0, 3.0, 4.0, 5.0, 6.0 ] ); +* var shape = [ 3, 2 ]; +* var strides = [ 2, 1 ]; +* var offset = 0; +* +* var x = ndarray( 'float64', buffer, shape, strides, offset, 'row-major' ); +* // returns +* +* var arr = ndarray2array( x ); +* // returns [ [ 1.0, 2.0 ], [ 3.0, 4.0 ], [ 5.0, 6.0 ] ] +* +* var y = shift( x, 0, false, false ); +* // returns [ , ] +* +* arr = ndarray2array( y[ 0 ] ); +* // returns [ [ 3.0, 4.0 ], [ 5.0, 6.0 ] ] +* +* arr = ndarray2array( y[ 1 ] ); +* // returns [ [ 1.0, 2.0 ] ] +*/ +declare function shift = typedndarray>( x: T, dim: number, strict: boolean, writable: boolean ): ArrayLike; + +/** +* Returns an array containing a truncated view of an input ndarray and a complementary view of the first element(s) along a specified dimension. +* +* @param x - input array +* @param dim - dimension along which to perform the operation +* @param strict - boolean indicating whether to enforce strict bounds checking +* @param writable - boolean indicating whether returned arrays should be writable +* @returns a list of ndarrays +* +* @example +* var Complex64Array = require( '@stdlib/array/complex64' ); +* var ndarray = require( '@stdlib/ndarray/ctor' ); +* var ndarray2array = require( '@stdlib/ndarray/to-array' ); +* +* var buffer = new Complex64Array( [ 1.0, 2.0, 3.0, 4.0, 5.0, 6.0, 7.0, 8.0 ] ); +* var shape = [ 2, 2 ]; +* var strides = [ 2, 1 ]; +* var offset = 0; +* +* var x = ndarray( 'complex64', buffer, shape, strides, offset, 'row-major' ); +* // returns +* +* var arr = ndarray2array( x ); +* // returns [ [ 1.0, 2.0, 3.0, 4.0 ], [ 5.0, 6.0, 7.0, 8.0 ] ] +* +* var y = shift( x, 0, false, false ); +* // returns [ , ] +* +* arr = ndarray2array( y[ 0 ] ); +* // returns [ [ 2.0, 3.0 ], [ 7.0, 8.0 ] ] +* +* arr = ndarray2array( y[ 1 ] ); +* // returns [ [ 1.0, 2.0 ], [ 5.0, 6.0 ] ] +*/ +declare function shift = typedndarray>( x: U, dim: number, strict: boolean, writable: boolean ): ArrayLike; + +/** +* Returns an array containing a truncated view of an input ndarray and a complementary view of the first element(s) along a specified dimension. +* +* @param x - input array +* @param dim - dimension along which to perform the operation +* @param strict - boolean indicating whether to enforce strict bounds checking +* @param writable - boolean indicating whether returned arrays should be writable +* @returns a list of ndarrays +* +* @example +* var BooleanArray = require( '@stdlib/array/bool' ); +* var ndarray = require( '@stdlib/ndarray/ctor' ); +* var ndarray2array = require( '@stdlib/ndarray/to-array' ); +* +* var buffer = new BooleanArray( [ true, false, true, false, true, false ] ); +* var shape = [ 3, 2 ]; +* var strides = [ 2, 1 ]; +* var offset = 0; +* +* var x = ndarray( 'uint8c', buffer, shape, strides, offset, 'row-major' ); +* // returns +* +* var arr = ndarray2array( x ); +* // returns [ [ true, false ], [ true, false ], [ true, false ] ] +* +* var y = shift( x, 0, false, false ); +* // returns [ , ] +* +* arr = ndarray2array( y[ 0 ] ); +* // returns [ [ true, false ], [ true, false ] ] +* +* arr = ndarray2array( y[ 1 ] ); +* // returns [ [ true, false ] ] +*/ +declare function shift = typedndarray>( x: T, dim: number, strict: boolean, writable: boolean ): ArrayLike; + +/** +* Returns an array containing a truncated view of an input ndarray and a complementary view of the first element(s) along a specified dimension. +* +* @param x - input array +* @param dim - dimension along which to perform the operation +* @param strict - boolean indicating whether to enforce strict bounds checking +* @param writable - boolean indicating whether returned arrays should be writable +* @returns a list of ndarrays +* +* @example +* var ndarray = require( '@stdlib/ndarray/ctor' ); +* var ndarray2array = require( '@stdlib/ndarray/to-array' ); +* +* var buffer = [ 1.0, 2.0, 3.0, 4.0, 5.0, 6.0 ]; +* var shape = [ 3, 2 ]; +* var strides = [ 2, 1 ]; +* var offset = 0; +* +* var x = ndarray( 'generic', buffer, shape, strides, offset, 'row-major' ); +* // returns +* +* var arr = ndarray2array( x ); +* // returns [ [ 1.0, 2.0 ], [ 3.0, 4.0 ], [ 5.0, 6.0 ] ] +* +* var y = shift( x, 0, false, false ); +* // returns [ , ] +* +* arr = ndarray2array( y[ 0 ] ); +* // returns [ [ 3.0, 4.0 ], [ 5.0, 6.0 ] ] +* +* arr = ndarray2array( y[ 1 ] ); +* // returns [ [ 1.0, 2.0 ] ] +*/ +declare function shift = genericndarray>( x: U, dim: number, strict: boolean, writable: boolean ): ArrayLike; + + +// EXPORTS // + +export = shift; diff --git a/lib/node_modules/@stdlib/ndarray/base/shift/docs/types/test.ts b/lib/node_modules/@stdlib/ndarray/base/shift/docs/types/test.ts new file mode 100644 index 000000000000..d7b5f437818b --- /dev/null +++ b/lib/node_modules/@stdlib/ndarray/base/shift/docs/types/test.ts @@ -0,0 +1,95 @@ +/* +* @license Apache-2.0 +* +* Copyright (c) 2025 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. +*/ + +import empty = require( '@stdlib/ndarray/base/empty' ); +import shift = require( './index' ); + + +// TESTS // + +// The function returns an ndarray... +{ + const order = 'row-major'; + const sh = [ 2, 2 ]; + + shift( empty( 'float64', sh, order ), 1, false, false ); // $ExpectType ArrayLike + shift( empty( 'complex64', sh, order ), 1, false, false ); // $ExpectType ArrayLike + shift( empty( 'uint8c', sh, order ), 1, false, false ); // $ExpectType ArrayLike +} + +// The compiler throws an error if the function is provided a first argument which is not an ndarray... +{ + shift( '10', 1, false, false ); // $ExpectError + shift( 10, 1, false, false ); // $ExpectError + shift( false, 1, false, false ); // $ExpectError + shift( true, 1, false, false ); // $ExpectError + shift( null, 1, false, false ); // $ExpectError + shift( [], 1, false, false ); // $ExpectError + shift( {}, 1, false, false ); // $ExpectError + shift( ( x: number ): number => x, 1, false, false ); // $ExpectError +} + +// The compiler throws an error if the function is provided a second argument which is not an integer... +{ + const x = empty( 'float64', [ 2, 2 ], 'row-major' ); + + shift( x, '5', false, false ); // $ExpectError + shift( x, false, false, false ); // $ExpectError + shift( x, true, false, false ); // $ExpectError + shift( x, null, false, false ); // $ExpectError + shift( x, undefined, false, false ); // $ExpectError + shift( x, [ '5' ], false, false ); // $ExpectError + shift( x, {}, false, false ); // $ExpectError + shift( x, ( x: number ): number => x, false, false ); // $ExpectError +} + +// The compiler throws an error if the function is provided a third argument which is not a boolean... +{ + const x = empty( 'float64', [ 2, 2 ], 'row-major' ); + + shift( x, 1, '5', false ); // $ExpectError + shift( x, 1, 5, false ); // $ExpectError + shift( x, 1, null, false ); // $ExpectError + shift( x, 1, undefined, false ); // $ExpectError + shift( x, 1, [ '5' ], false ); // $ExpectError + shift( x, 1, {}, false ); // $ExpectError + shift( x, 1, ( x: number ): number => x, false ); // $ExpectError +} + +// The compiler throws an error if the function is provided a fourth argument which is not a boolean... +{ + const x = empty( 'float64', [ 2, 2 ], 'row-major' ); + + shift( x, 1, false, '5' ); // $ExpectError + shift( x, 1, false, 5 ); // $ExpectError + shift( x, 1, false, null ); // $ExpectError + shift( x, 1, false, undefined ); // $ExpectError + shift( x, 1, false, [ '5' ] ); // $ExpectError + shift( x, 1, false, {} ); // $ExpectError + shift( x, 1, false, ( 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' ); + + shift( x ); // $ExpectError + shift( x, 1 ); // $ExpectError + shift( x, 1, false ); // $ExpectError + shift( x, 1, false, false, {} ); // $ExpectError +} diff --git a/lib/node_modules/@stdlib/ndarray/base/shift/examples/index.js b/lib/node_modules/@stdlib/ndarray/base/shift/examples/index.js new file mode 100644 index 000000000000..eb75e18edd69 --- /dev/null +++ b/lib/node_modules/@stdlib/ndarray/base/shift/examples/index.js @@ -0,0 +1,40 @@ +/** +* @license Apache-2.0 +* +* Copyright (c) 2025 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 array = require( '@stdlib/ndarray/array' ); +var ndarray2array = require( '@stdlib/ndarray/to-array' ); +var zeroTo = require( '@stdlib/array/base/zero-to' ); +var shift = require( './../lib' ); + +// Create a linear ndarray buffer: +var buf = zeroTo( 27 ); + +// Create an ndarray: +var x = array( buf, { + 'shape': [ 3, 3, 3 ] +}); + +console.log( ndarray2array( x ) ); + +var y = shift( x, 1, false, false ); + +console.log( ndarray2array( y[ 0 ] ) ); + +console.log( ndarray2array( y[ 1 ] ) ); diff --git a/lib/node_modules/@stdlib/ndarray/base/shift/lib/index.js b/lib/node_modules/@stdlib/ndarray/base/shift/lib/index.js new file mode 100644 index 000000000000..93ff01c01e44 --- /dev/null +++ b/lib/node_modules/@stdlib/ndarray/base/shift/lib/index.js @@ -0,0 +1,59 @@ +/** +* @license Apache-2.0 +* +* Copyright (c) 2025 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 an array containing a truncated view of an input ndarray and a complementary view of the first element(s) along a specified dimension. +* +* @module @stdlib/ndarray/base/shift +* +* @example +* var ndarray = require( '@stdlib/ndarray/ctor' ); +* var ndarray2array = require( '@stdlib/ndarray/to-array' ); +* var shift = require( '@stdlib/ndarray/base/shift' ); +* +* var buffer = [ 1.0, 2.0, 3.0, 4.0, 5.0, 6.0 ]; +* var shape = [ 3, 2 ]; +* var strides = [ 2, 1 ]; +* var offset = 0; +* +* var x = ndarray( 'generic', buffer, shape, strides, offset, 'row-major' ); +* // returns +* +* var sh = x.shape; +* // returns [ 3, 2 ] +* +* var arr = ndarray2array( x ); +* // returns [ [ 1.0, 2.0 ], [ 3.0, 4.0 ], [ 5.0, 6.0 ] ] +* +* var y = shift( x, 0, false, false ); +* +* arr = ndarray2array( y[ 0 ] ); +* +* arr = ndarray2array( y[ 1 ] ); +*/ + +// MODULES // + +var main = require( './main.js' ); + + +// EXPORTS // + +module.exports = main; diff --git a/lib/node_modules/@stdlib/ndarray/base/shift/lib/main.js b/lib/node_modules/@stdlib/ndarray/base/shift/lib/main.js new file mode 100644 index 000000000000..d51f6eddfe40 --- /dev/null +++ b/lib/node_modules/@stdlib/ndarray/base/shift/lib/main.js @@ -0,0 +1,92 @@ +/** +* @license Apache-2.0 +* +* Copyright (c) 2025 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 ndims = require( '@stdlib/ndarray/base/ndims' ); +var normalizeIndex = require( '@stdlib/ndarray/base/normalize-index' ); +var getShape = require( '@stdlib/ndarray/base/shape' ); +var sliceFrom = require( '@stdlib/ndarray/base/slice-from' ); +var sliceTo = require( '@stdlib/ndarray/base/slice-to' ); + + +// MAIN // + +/** +* Returns an array containing a truncated view of an input ndarray and a complementary view of the first element(s) along a specified dimension. +* +* @param {ndarray} x - input array +* @param {integer} dim - dimension along which to perform the operation +* @param {boolean} strict - boolean indicating whether to enforce strict bounds checking +* @param {boolean} writable - boolean indicating whether returned arrays should be writable +* @returns {Array} a list of ndarrays +* +* @example +* var ndarray = require( '@stdlib/ndarray/ctor' ); +* var ndarray2array = require( '@stdlib/ndarray/to-array' ); +* +* var buffer = [ 1.0, 2.0, 3.0, 4.0, 5.0, 6.0 ]; +* var shape = [ 3, 2 ]; +* var strides = [ 2, 1 ]; +* var offset = 0; +* +* var x = ndarray( 'generic', buffer, shape, strides, offset, 'row-major' ); +* // returns +* +* var arr = ndarray2array( x ); +* // returns [ [ 1.0, 2.0 ], [ 3.0, 4.0 ], [ 5.0, 6.0 ] ] +* +* var y = shift( x, 0, false, false ); +* // returns [ , ] +* +* arr = ndarray2array( y[ 0 ] ); +* // returns [ [ 3, 4 ], [ 5, 6 ] ] +* +* arr = ndarray2array( y[ 1 ] ); +* // returns [ [ 1, 2 ] ] +*/ +function shift( x, dim, strict, writable ) { + var v0; + var v1; + var sh; + var N; + var s; + var i; + + // Retrieve array meta data: + sh = getShape( x ); + N = ndims( x ); + + // Resolve view slice: + s = []; + for ( i = 0; i < N; i++ ) { + s[ i ] = null; + } + dim = normalizeIndex( dim, N -1 ); + s[ dim ] = sh[ dim ] - ( sh[ dim ] - 1 ); + v0 = sliceFrom( x, s, strict, writable ); + v1 = sliceTo( x, s, strict, writable ); + return [ v0, v1 ]; +} + + +// EXPORTS // + +module.exports = shift; diff --git a/lib/node_modules/@stdlib/ndarray/base/shift/package.json b/lib/node_modules/@stdlib/ndarray/base/shift/package.json new file mode 100644 index 000000000000..fe3dbd83b1cc --- /dev/null +++ b/lib/node_modules/@stdlib/ndarray/base/shift/package.json @@ -0,0 +1,67 @@ +{ + "name": "@stdlib/ndarray/base/shift", + "version": "0.0.0", + "description": "Return an array containing a truncated view of an input ndarray and a complementary view of the first element(s) along a specified dimension.", + "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", + "base", + "data", + "structure", + "vector", + "ndarray", + "matrix", + "slice", + "shift", + "view", + "remove", + "truncate" + ] +} diff --git a/lib/node_modules/@stdlib/ndarray/base/shift/test/test.js b/lib/node_modules/@stdlib/ndarray/base/shift/test/test.js new file mode 100644 index 000000000000..00f0ed421e8b --- /dev/null +++ b/lib/node_modules/@stdlib/ndarray/base/shift/test/test.js @@ -0,0 +1,261 @@ +/** +* @license Apache-2.0 +* +* Copyright (c) 2025 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 zeroTo = require( '@stdlib/array/base/zero-to' ); +var typedarray = require( '@stdlib/array/typed' ); +var ndarray2array = require( '@stdlib/ndarray/to-array' ); +var ctor = require( '@stdlib/ndarray/ctor' ); +var shift = require( './../lib' ); + + +// TESTS // + +tape( 'main export is a function', function test( t ) { + t.ok( true, __filename ); + t.strictEqual( typeof shift, 'function', 'main export is a function' ); + t.end(); +}); + +tape( 'the function returns an array containing ndarrays (ndims=1, strict, readonly)', function test( t ) { + var actual; + var buf; + var ord; + var sh; + var st; + var o; + var x; + + buf = typedarray( zeroTo( 6 ), 'float64' ); + sh = [ 6 ]; + st = [ 1 ]; + o = 0; + ord = 'row-major'; + + x = new ctor( 'float64', buf, sh, st, o, ord ); + + actual = shift( x, 0, true, false ); + + t.strictEqual( isndarrayLike( actual[0] ), true, 'returns expected value' ); + t.strictEqual( isndarrayLike( actual[1] ), true, 'returns expected value' ); + t.strictEqual( isReadOnly( actual[0] ), true, 'returns expected value' ); + t.strictEqual( isReadOnly( actual[1] ), true, 'returns expected value' ); + t.strictEqual( actual[0].ndims, 1, 'returns expected value' ); + t.strictEqual( actual[1].ndims, 1, 'returns expected value' ); + t.strictEqual( actual[0].length, 5, 'returns expected value' ); + t.strictEqual( actual[1].length, 1, 'returns expected value' ); + t.deepEqual( ndarray2array( actual[0] ), [ 1, 2, 3, 4, 5 ], 'returns expected value' ); + t.deepEqual( ndarray2array( actual[1] ), [ 0 ], 'returns expected value' ); + + t.end(); +}); + +tape( 'the function returns an array containing ndarrays (ndims=1, strict, writable)', function test( t ) { + var actual; + var buf; + var ord; + var sh; + var st; + var o; + var x; + + buf = typedarray( zeroTo( 6 ), 'float64' ); + sh = [ 6 ]; + st = [ 1 ]; + o = 0; + ord = 'row-major'; + + x = new ctor( 'float64', buf, sh, st, o, ord ); + + actual = shift( x, 0, true, true ); + + t.strictEqual( isndarrayLike( actual[0] ), true, 'returns expected value' ); + t.strictEqual( isndarrayLike( actual[1] ), true, 'returns expected value' ); + t.strictEqual( isReadOnly( actual[0] ), false, 'returns expected value' ); + t.strictEqual( isReadOnly( actual[1] ), false, 'returns expected value' ); + t.strictEqual( actual[0].ndims, 1, 'returns expected value' ); + t.strictEqual( actual[1].ndims, 1, 'returns expected value' ); + t.strictEqual( actual[0].length, 5, 'returns expected value' ); + t.strictEqual( actual[1].length, 1, 'returns expected value' ); + t.deepEqual( ndarray2array( actual[0] ), [ 1, 2, 3, 4, 5 ], 'returns expected value' ); + t.deepEqual( ndarray2array( actual[1] ), [ 0 ], 'returns expected value' ); + + t.end(); +}); + +tape( 'the function returns an array containing ndarrays (ndims=2, strict, readonly)', function test( t ) { + var actual; + var buf; + var ord; + var sh; + var st; + var o; + var x; + + buf = typedarray( zeroTo( 8 ), 'float64' ); + sh = [ 2, 4 ]; + st = [ 4, 1 ]; + o = 0; + ord = 'row-major'; + + x = new ctor( 'float64', buf, sh, st, o, ord ); + + actual = shift( x, 0, true, false ); + + t.strictEqual( isndarrayLike( actual[0] ), true, 'returns expected value' ); + t.strictEqual( isndarrayLike( actual[1] ), true, 'returns expected value' ); + t.strictEqual( isReadOnly( actual[0] ), true, 'returns expected value' ); + t.strictEqual( isReadOnly( actual[1] ), true, 'returns expected value' ); + t.strictEqual( actual[0].ndims, 2, 'returns expected value' ); + t.strictEqual( actual[1].ndims, 2, 'returns expected value' ); + t.strictEqual( actual[0].length, 4, 'returns expected value' ); + t.strictEqual( actual[1].length, 4, 'returns expected value' ); + t.deepEqual( ndarray2array( actual[0] ), [ [ 4, 5, 6, 7 ] ], 'returns expected value' ); + t.deepEqual( ndarray2array( actual[1] ), [ [ 0, 1, 2, 3 ] ], 'returns expected value' ); + + t.end(); +}); + +tape( 'the function returns an array containing ndarrays (ndims=2, strict, writable)', function test( t ) { + var actual; + var buf; + var ord; + var sh; + var st; + var o; + var x; + + buf = typedarray( zeroTo( 8 ), 'float64' ); + sh = [ 2, 4 ]; + st = [ 4, 1 ]; + o = 0; + ord = 'row-major'; + + x = new ctor( 'float64', buf, sh, st, o, ord ); + + actual = shift( x, 0, true, true ); + + t.strictEqual( isndarrayLike( actual[0] ), true, 'returns expected value' ); + t.strictEqual( isndarrayLike( actual[1] ), true, 'returns expected value' ); + t.strictEqual( isReadOnly( actual[0] ), false, 'returns expected value' ); + t.strictEqual( isReadOnly( actual[1] ), false, 'returns expected value' ); + t.strictEqual( actual[0].ndims, 2, 'returns expected value' ); + t.strictEqual( actual[1].ndims, 2, 'returns expected value' ); + t.strictEqual( actual[0].length, 4, 'returns expected value' ); + t.strictEqual( actual[1].length, 4, 'returns expected value' ); + t.deepEqual( ndarray2array( actual[0] ), [ [ 4, 5, 6, 7 ] ], 'returns expected value' ); + t.deepEqual( ndarray2array( actual[1] ), [ [ 0, 1, 2, 3 ] ], 'returns expected value' ); + + t.end(); +}); + +tape( 'the function returns an array containing ndarrays (ndims=3, strict, readonly)', function test( t ) { + var actual; + var buf; + var ord; + var sh; + var st; + var o; + var x; + + buf = typedarray( zeroTo( 8 ), 'float64' ); + sh = [ 2, 2, 2 ]; + st = [ 4, 2, 1 ]; + o = 0; + ord = 'row-major'; + + /* + * [ + * [ + * [ 0, 1 ], + * [ 2, 3 ] + * ], + * [ + * [ 4, 5 ], + * [ 6, 7 ] + * ] + *]; + */ + x = new ctor( 'float64', buf, sh, st, o, ord ); + + actual = shift( x, 1, true, false ); + + t.strictEqual( isndarrayLike( actual[0] ), true, 'returns expected value' ); + t.strictEqual( isndarrayLike( actual[1] ), true, 'returns expected value' ); + t.strictEqual( isReadOnly( actual[0] ), true, 'returns expected value' ); + t.strictEqual( isReadOnly( actual[1] ), true, 'returns expected value' ); + t.strictEqual( actual[0].ndims, 3, 'returns expected value' ); + t.strictEqual( actual[1].ndims, 3, 'returns expected value' ); + t.strictEqual( actual[0].length, 4, 'returns expected value' ); + t.strictEqual( actual[1].length, 4, 'returns expected value' ); + t.deepEqual( ndarray2array( actual[0] ), [ [ [ 2, 3 ] ], [ [ 6, 7 ] ] ], 'returns expected value' ); + t.deepEqual( ndarray2array( actual[1] ), [ [ [ 0, 1 ] ], [ [ 4, 5 ] ] ], 'returns expected value' ); + + t.end(); +}); + +tape( 'the function returns an array containing ndarrays (ndims=3, strict, writable)', function test( t ) { + var actual; + var buf; + var ord; + var sh; + var st; + var o; + var x; + + buf = typedarray( zeroTo( 8 ), 'float64' ); + sh = [ 2, 2, 2 ]; + st = [ 4, 2, 1 ]; + o = 0; + ord = 'row-major'; + + /* + * [ + * [ + * [ 0, 1 ], + * [ 2, 3 ] + * ], + * [ + * [ 4, 5 ], + * [ 6, 7 ] + * ] + *]; + */ + x = new ctor( 'float64', buf, sh, st, o, ord ); + + actual = shift( x, 1, true, true ); + + t.strictEqual( isndarrayLike( actual[0] ), true, 'returns expected value' ); + t.strictEqual( isndarrayLike( actual[1] ), true, 'returns expected value' ); + t.strictEqual( isReadOnly( actual[0] ), false, 'returns expected value' ); + t.strictEqual( isReadOnly( actual[1] ), false, 'returns expected value' ); + t.strictEqual( actual[0].ndims, 3, 'returns expected value' ); + t.strictEqual( actual[1].ndims, 3, 'returns expected value' ); + t.strictEqual( actual[0].length, 4, 'returns expected value' ); + t.strictEqual( actual[1].length, 4, 'returns expected value' ); + t.deepEqual( ndarray2array( actual[0] ), [ [ [ 2, 3 ] ], [ [ 6, 7 ] ] ], 'returns expected value' ); + t.deepEqual( ndarray2array( actual[1] ), [ [ [ 0, 1 ] ], [ [ 4, 5 ] ] ], 'returns expected value' ); + + t.end(); +}); From f0ec0b4f0c042aef840d2c82968042119392cd43 Mon Sep 17 00:00:00 2001 From: Muhammad Haris <101793258+headlessnode@users.noreply.github.com> Date: 2025年9月21日 19:13:21 +0500 Subject: [PATCH 2/7] docs: apply suggestions from code review Signed-off-by: Muhammad Haris <101793258+headlessnode@users.noreply.github.com> --- lib/node_modules/@stdlib/ndarray/base/shift/lib/index.js | 6 +++--- 1 file changed, 3 insertions(+), 3 deletions(-) diff --git a/lib/node_modules/@stdlib/ndarray/base/shift/lib/index.js b/lib/node_modules/@stdlib/ndarray/base/shift/lib/index.js index 93ff01c01e44..8392fda66b9b 100644 --- a/lib/node_modules/@stdlib/ndarray/base/shift/lib/index.js +++ b/lib/node_modules/@stdlib/ndarray/base/shift/lib/index.js @@ -36,17 +36,17 @@ * var x = ndarray( 'generic', buffer, shape, strides, offset, 'row-major' ); * // returns * -* var sh = x.shape; -* // returns [ 3, 2 ] -* * var arr = ndarray2array( x ); * // returns [ [ 1.0, 2.0 ], [ 3.0, 4.0 ], [ 5.0, 6.0 ] ] * * var y = shift( x, 0, false, false ); +* // returns [ , ] * * arr = ndarray2array( y[ 0 ] ); +* // returns [ [ 3, 4 ], [ 5, 6 ] ] * * arr = ndarray2array( y[ 1 ] ); +* // returns [ [ 1, 2 ] ] */ // MODULES // From 0d4ea355a2f426b177e5df59dc64639a2fbc0a76 Mon Sep 17 00:00:00 2001 From: headlessNode Date: 2025年9月24日 21:53:59 +0500 Subject: [PATCH 3/7] refactor: apply suggestions from code review --- 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: na - task: lint_javascript_tests status: na - 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/base/shift/README.md | 11 +- .../ndarray/base/shift/benchmark/benchmark.js | 66 +++++----- .../@stdlib/ndarray/base/shift/docs/repl.txt | 16 ++- .../ndarray/base/shift/docs/types/index.d.ts | 118 +----------------- .../ndarray/base/shift/docs/types/test.ts | 70 +++++------ .../@stdlib/ndarray/base/shift/lib/index.js | 4 +- .../@stdlib/ndarray/base/shift/lib/main.js | 45 ++++--- .../@stdlib/ndarray/base/shift/package.json | 2 +- 8 files changed, 108 insertions(+), 224 deletions(-) diff --git a/lib/node_modules/@stdlib/ndarray/base/shift/README.md b/lib/node_modules/@stdlib/ndarray/base/shift/README.md index de176f895682..905bf9c05d73 100644 --- a/lib/node_modules/@stdlib/ndarray/base/shift/README.md +++ b/lib/node_modules/@stdlib/ndarray/base/shift/README.md @@ -20,7 +20,7 @@ limitations under the License. # shift -> Return an array containing a truncated view of an input ndarray and a complementary view of the first element(s) along a specified dimension. +> Return an array containing a truncated view of an input ndarray and a view of the first element(s) along a specified dimension. @@ -40,9 +40,9 @@ limitations under the License. var shift = require( '@stdlib/ndarray/base/shift' ); ``` -#### shift( x, dim, strict, writable ) +#### shift( x, dim, writable ) -Returns an array containing a truncated view of an input ndarray and a complementary view of the first element(s) along a specified dimension. +Returns an array containing a truncated view of an input ndarray and a view of the first element(s) along a specified dimension. ```javascript var ndarray = require( '@stdlib/ndarray/ctor' ); @@ -62,7 +62,7 @@ var sh = x.shape; var arr = ndarray2array( x ); // returns [ [ 1.0, 2.0 ], [ 3.0, 4.0 ], [ 5.0, 6.0 ] ] -var y = shift( x, 0, false, false ); +var y = shift( x, 0, false ); // returns [ , ] arr = ndarray2array( y[ 0 ] ); @@ -76,7 +76,6 @@ The function accepts the following arguments: - **x**: input ndarray. - **dim**: dimension along which to perform the operation. If provided an integer less than zero, the dimension index is resolved relative to the last dimension, with the last dimension corresponding to the value `-1`. -- **strict**: boolean indicating whether to enforce strict bounds checking. - **writable**: boolean indicating whether a returned ndarray should be writable. @@ -117,7 +116,7 @@ var x = array( buf, { 'shape': [ 3, 3, 3 ] }); -var y = shift( x, 1, false, false ); +var y = shift( x, 1, false ); // returns [ , ] console.log( ndarray2array( y[ 0 ] ) ); diff --git a/lib/node_modules/@stdlib/ndarray/base/shift/benchmark/benchmark.js b/lib/node_modules/@stdlib/ndarray/base/shift/benchmark/benchmark.js index d049b8438b17..485840f3d061 100644 --- a/lib/node_modules/@stdlib/ndarray/base/shift/benchmark/benchmark.js +++ b/lib/node_modules/@stdlib/ndarray/base/shift/benchmark/benchmark.js @@ -48,7 +48,7 @@ bench( pkg+'::1d', function benchmark( b ) { b.tic(); for ( i = 0; i < b.iterations; i++ ) { - v = shift( values[ i%values.length ], 0, false, false ); + v = shift( values[ i%values.length ], 0, false ); if ( typeof v !== 'object' ) { b.fail( 'should return an array of ndarrays' ); } @@ -66,21 +66,21 @@ bench( pkg+'::2d', function benchmark( b ) { var v; var i; - /* eslint-disable object-curly-newline */ + /* 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' }) + 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 */ + /* eslint-enable object-curly-newline, stdlib/line-closing-bracket-spacing */ b.tic(); for ( i = 0; i < b.iterations; i++ ) { - v = shift( values[ i%values.length ], 0, false, false ); + v = shift( values[ i%values.length ], 0, false ); if ( typeof v !== 'object' ) { b.fail( 'should return an array of ndarrays' ); } @@ -98,21 +98,21 @@ bench( pkg+'::3d', function benchmark( b ) { var v; var i; - /* eslint-disable object-curly-newline */ + /* 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' }) + 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 */ + /* eslint-enable object-curly-newline, stdlib/line-closing-bracket-spacing */ b.tic(); for ( i = 0; i < b.iterations; i++ ) { - v = shift( values[ i%values.length ], 0, false, false ); + v = shift( values[ i%values.length ], 0, false ); if ( typeof v !== 'object' ) { b.fail( 'should return an array of ndarrays' ); } @@ -130,21 +130,21 @@ bench( pkg+'::4d', function benchmark( b ) { var v; var i; - /* eslint-disable object-curly-newline */ + /* 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' }) + 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 */ + /* eslint-enable object-curly-newline, stdlib/line-closing-bracket-spacing */ b.tic(); for ( i = 0; i < b.iterations; i++ ) { - v = shift( values[ i%values.length ], 0, false, false ); + v = shift( values[ i%values.length ], 0, false ); if ( typeof v !== 'object' ) { b.fail( 'should return an array of ndarrays' ); } @@ -162,21 +162,21 @@ bench( pkg+'::5d', function benchmark( b ) { var v; var i; - /* eslint-disable object-curly-newline */ + /* 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' }) + 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 */ + /* eslint-enable object-curly-newline, stdlib/line-closing-bracket-spacing */ b.tic(); for ( i = 0; i < b.iterations; i++ ) { - v = shift( values[ i%values.length ], 0, false, false ); + v = shift( values[ i%values.length ], 0, false ); if ( typeof v !== 'object' ) { b.fail( 'should return an array of ndarrays' ); } diff --git a/lib/node_modules/@stdlib/ndarray/base/shift/docs/repl.txt b/lib/node_modules/@stdlib/ndarray/base/shift/docs/repl.txt index a7db648f8116..d448ecb630c2 100644 --- a/lib/node_modules/@stdlib/ndarray/base/shift/docs/repl.txt +++ b/lib/node_modules/@stdlib/ndarray/base/shift/docs/repl.txt @@ -1,7 +1,7 @@ -{{alias}}( x, dim, strict, writable ) - Returns an array containing a truncated view of an input ndarray and a - complementary view of the first element(s) along a specified dimension. +{{alias}}( x, dim, writable ) + Returns an array containing a truncated view of an input ndarray and a view + of the first element(s) along a specified dimension. Parameters ---------- @@ -13,9 +13,6 @@ less than zero, the dimension index is resolved relative to the last dimension, with the last dimension corresponding to the value `-1`. - strict: boolean - Boolean indicating whether to enforce strict bounds checking. - writable: boolean Boolean indicating whether a returned ndarray should be writable. This parameter only applies to ndarray constructors which support read-only @@ -23,14 +20,15 @@ Returns ------- - out: array - Output array. + out: Array + An array containing a truncated view of an input ndarray and a view of + the first element(s) along a specified dimension. Examples -------- > var x = {{alias:@stdlib/ndarray/array}}( [ [ 1, 2 ], [ 3, 4 ] ] ) -> var y = {{alias}}( x, 1, false, false ) +> var y = {{alias}}( x, 1, false ) [ , ] > {{alias:@stdlib/ndarray/to-array}}( y[0] ) [ [ 2 ], [ 4 ] ] diff --git a/lib/node_modules/@stdlib/ndarray/base/shift/docs/types/index.d.ts b/lib/node_modules/@stdlib/ndarray/base/shift/docs/types/index.d.ts index ac6ac1ce6008..b20d0542c1c8 100644 --- a/lib/node_modules/@stdlib/ndarray/base/shift/docs/types/index.d.ts +++ b/lib/node_modules/@stdlib/ndarray/base/shift/docs/types/index.d.ts @@ -20,17 +20,14 @@ /// -import { typedndarray, genericndarray } from '@stdlib/types/ndarray'; -import { ComplexLike } from '@stdlib/types/complex'; -import { ArrayLike } from '@stdlib/types/array'; +import { ndarray } from '@stdlib/types/ndarray'; /** -* Returns an array containing a truncated view of an input ndarray and a complementary view of the first element(s) along a specified dimension. +* Returns an array containing a truncated view of an input ndarray and a view of the first element(s) along a specified dimension. * * @param x - input array * @param dim - dimension along which to perform the operation -* @param strict - boolean indicating whether to enforce strict bounds checking * @param writable - boolean indicating whether returned arrays should be writable * @returns a list of ndarrays * @@ -50,7 +47,7 @@ import { ArrayLike } from '@stdlib/types/array'; * var arr = ndarray2array( x ); * // returns [ [ 1.0, 2.0 ], [ 3.0, 4.0 ], [ 5.0, 6.0 ] ] * -* var y = shift( x, 0, false, false ); +* var y = shift( x, 0, false ); * // returns [ , ] * * arr = ndarray2array( y[ 0 ] ); @@ -59,114 +56,7 @@ import { ArrayLike } from '@stdlib/types/array'; * arr = ndarray2array( y[ 1 ] ); * // returns [ [ 1.0, 2.0 ] ] */ -declare function shift = typedndarray>( x: T, dim: number, strict: boolean, writable: boolean ): ArrayLike; - -/** -* Returns an array containing a truncated view of an input ndarray and a complementary view of the first element(s) along a specified dimension. -* -* @param x - input array -* @param dim - dimension along which to perform the operation -* @param strict - boolean indicating whether to enforce strict bounds checking -* @param writable - boolean indicating whether returned arrays should be writable -* @returns a list of ndarrays -* -* @example -* var Complex64Array = require( '@stdlib/array/complex64' ); -* var ndarray = require( '@stdlib/ndarray/ctor' ); -* var ndarray2array = require( '@stdlib/ndarray/to-array' ); -* -* var buffer = new Complex64Array( [ 1.0, 2.0, 3.0, 4.0, 5.0, 6.0, 7.0, 8.0 ] ); -* var shape = [ 2, 2 ]; -* var strides = [ 2, 1 ]; -* var offset = 0; -* -* var x = ndarray( 'complex64', buffer, shape, strides, offset, 'row-major' ); -* // returns -* -* var arr = ndarray2array( x ); -* // returns [ [ 1.0, 2.0, 3.0, 4.0 ], [ 5.0, 6.0, 7.0, 8.0 ] ] -* -* var y = shift( x, 0, false, false ); -* // returns [ , ] -* -* arr = ndarray2array( y[ 0 ] ); -* // returns [ [ 2.0, 3.0 ], [ 7.0, 8.0 ] ] -* -* arr = ndarray2array( y[ 1 ] ); -* // returns [ [ 1.0, 2.0 ], [ 5.0, 6.0 ] ] -*/ -declare function shift = typedndarray>( x: U, dim: number, strict: boolean, writable: boolean ): ArrayLike; - -/** -* Returns an array containing a truncated view of an input ndarray and a complementary view of the first element(s) along a specified dimension. -* -* @param x - input array -* @param dim - dimension along which to perform the operation -* @param strict - boolean indicating whether to enforce strict bounds checking -* @param writable - boolean indicating whether returned arrays should be writable -* @returns a list of ndarrays -* -* @example -* var BooleanArray = require( '@stdlib/array/bool' ); -* var ndarray = require( '@stdlib/ndarray/ctor' ); -* var ndarray2array = require( '@stdlib/ndarray/to-array' ); -* -* var buffer = new BooleanArray( [ true, false, true, false, true, false ] ); -* var shape = [ 3, 2 ]; -* var strides = [ 2, 1 ]; -* var offset = 0; -* -* var x = ndarray( 'uint8c', buffer, shape, strides, offset, 'row-major' ); -* // returns -* -* var arr = ndarray2array( x ); -* // returns [ [ true, false ], [ true, false ], [ true, false ] ] -* -* var y = shift( x, 0, false, false ); -* // returns [ , ] -* -* arr = ndarray2array( y[ 0 ] ); -* // returns [ [ true, false ], [ true, false ] ] -* -* arr = ndarray2array( y[ 1 ] ); -* // returns [ [ true, false ] ] -*/ -declare function shift = typedndarray>( x: T, dim: number, strict: boolean, writable: boolean ): ArrayLike; - -/** -* Returns an array containing a truncated view of an input ndarray and a complementary view of the first element(s) along a specified dimension. -* -* @param x - input array -* @param dim - dimension along which to perform the operation -* @param strict - boolean indicating whether to enforce strict bounds checking -* @param writable - boolean indicating whether returned arrays should be writable -* @returns a list of ndarrays -* -* @example -* var ndarray = require( '@stdlib/ndarray/ctor' ); -* var ndarray2array = require( '@stdlib/ndarray/to-array' ); -* -* var buffer = [ 1.0, 2.0, 3.0, 4.0, 5.0, 6.0 ]; -* var shape = [ 3, 2 ]; -* var strides = [ 2, 1 ]; -* var offset = 0; -* -* var x = ndarray( 'generic', buffer, shape, strides, offset, 'row-major' ); -* // returns -* -* var arr = ndarray2array( x ); -* // returns [ [ 1.0, 2.0 ], [ 3.0, 4.0 ], [ 5.0, 6.0 ] ] -* -* var y = shift( x, 0, false, false ); -* // returns [ , ] -* -* arr = ndarray2array( y[ 0 ] ); -* // returns [ [ 3.0, 4.0 ], [ 5.0, 6.0 ] ] -* -* arr = ndarray2array( y[ 1 ] ); -* // returns [ [ 1.0, 2.0 ] ] -*/ -declare function shift = genericndarray>( x: U, dim: number, strict: boolean, writable: boolean ): ArrayLike; +declare function shift( x: T, dim: number, writable: boolean ): [ T, T ]; // EXPORTS // diff --git a/lib/node_modules/@stdlib/ndarray/base/shift/docs/types/test.ts b/lib/node_modules/@stdlib/ndarray/base/shift/docs/types/test.ts index d7b5f437818b..504b9a89c303 100644 --- a/lib/node_modules/@stdlib/ndarray/base/shift/docs/types/test.ts +++ b/lib/node_modules/@stdlib/ndarray/base/shift/docs/types/test.ts @@ -22,66 +22,53 @@ import shift = require( './index' ); // TESTS // -// The function returns an ndarray... +// The function returns an array of ndarrays... { const order = 'row-major'; const sh = [ 2, 2 ]; - shift( empty( 'float64', sh, order ), 1, false, false ); // $ExpectType ArrayLike - shift( empty( 'complex64', sh, order ), 1, false, false ); // $ExpectType ArrayLike - shift( empty( 'uint8c', sh, order ), 1, false, false ); // $ExpectType ArrayLike + shift( empty( 'float64', sh, order ), 1, false ); // $ExpectType [float64ndarray, float64ndarray] + shift( empty( 'complex64', sh, order ), 1, false ); // $ExpectType [complex64ndarray, complex64ndarray] + shift( empty( 'uint8', sh, order ), 1, false ); // $ExpectType [uint8ndarray, uint8ndarray] } // The compiler throws an error if the function is provided a first argument which is not an ndarray... { - shift( '10', 1, false, false ); // $ExpectError - shift( 10, 1, false, false ); // $ExpectError - shift( false, 1, false, false ); // $ExpectError - shift( true, 1, false, false ); // $ExpectError - shift( null, 1, false, false ); // $ExpectError - shift( [], 1, false, false ); // $ExpectError - shift( {}, 1, false, false ); // $ExpectError - shift( ( x: number ): number => x, 1, false, false ); // $ExpectError + shift( '10', 1, false ); // $ExpectError + shift( 10, 1, false ); // $ExpectError + shift( false, 1, false ); // $ExpectError + shift( true, 1, false ); // $ExpectError + shift( null, 1, false ); // $ExpectError + shift( [], 1, false ); // $ExpectError + shift( {}, 1, false ); // $ExpectError + shift( ( x: number ): number => x, 1, false ); // $ExpectError } // The compiler throws an error if the function is provided a second argument which is not an integer... { const x = empty( 'float64', [ 2, 2 ], 'row-major' ); - shift( x, '5', false, false ); // $ExpectError - shift( x, false, false, false ); // $ExpectError - shift( x, true, false, false ); // $ExpectError - shift( x, null, false, false ); // $ExpectError - shift( x, undefined, false, false ); // $ExpectError - shift( x, [ '5' ], false, false ); // $ExpectError - shift( x, {}, false, false ); // $ExpectError - shift( x, ( x: number ): number => x, false, false ); // $ExpectError + shift( x, '5', false ); // $ExpectError + shift( x, false, false ); // $ExpectError + shift( x, true, false ); // $ExpectError + shift( x, null, false ); // $ExpectError + shift( x, undefined, false ); // $ExpectError + shift( x, [ '5' ], false ); // $ExpectError + shift( x, {}, false ); // $ExpectError + shift( x, ( x: number ): number => x, false ); // $ExpectError } // The compiler throws an error if the function is provided a third argument which is not a boolean... { const x = empty( 'float64', [ 2, 2 ], 'row-major' ); - shift( x, 1, '5', false ); // $ExpectError - shift( x, 1, 5, false ); // $ExpectError - shift( x, 1, null, false ); // $ExpectError - shift( x, 1, undefined, false ); // $ExpectError - shift( x, 1, [ '5' ], false ); // $ExpectError - shift( x, 1, {}, false ); // $ExpectError - shift( x, 1, ( x: number ): number => x, false ); // $ExpectError -} - -// The compiler throws an error if the function is provided a fourth argument which is not a boolean... -{ - const x = empty( 'float64', [ 2, 2 ], 'row-major' ); - - shift( x, 1, false, '5' ); // $ExpectError - shift( x, 1, false, 5 ); // $ExpectError - shift( x, 1, false, null ); // $ExpectError - shift( x, 1, false, undefined ); // $ExpectError - shift( x, 1, false, [ '5' ] ); // $ExpectError - shift( x, 1, false, {} ); // $ExpectError - shift( x, 1, false, ( x: number ): number => x ); // $ExpectError + shift( x, 1, '5' ); // $ExpectError + shift( x, 1, 5 ); // $ExpectError + shift( x, 1, null ); // $ExpectError + shift( x, 1, undefined ); // $ExpectError + shift( x, 1, [ '5' ] ); // $ExpectError + shift( x, 1, {} ); // $ExpectError + shift( x, 1, ( x: number ): number => x ); // $ExpectError } // The compiler throws an error if the function is provided an unsupported number of arguments... @@ -90,6 +77,5 @@ import shift = require( './index' ); shift( x ); // $ExpectError shift( x, 1 ); // $ExpectError - shift( x, 1, false ); // $ExpectError - shift( x, 1, false, false, {} ); // $ExpectError + shift( x, 1, false, {} ); // $ExpectError } diff --git a/lib/node_modules/@stdlib/ndarray/base/shift/lib/index.js b/lib/node_modules/@stdlib/ndarray/base/shift/lib/index.js index 8392fda66b9b..b5bb150adc95 100644 --- a/lib/node_modules/@stdlib/ndarray/base/shift/lib/index.js +++ b/lib/node_modules/@stdlib/ndarray/base/shift/lib/index.js @@ -19,7 +19,7 @@ 'use strict'; /** -* Return an array containing a truncated view of an input ndarray and a complementary view of the first element(s) along a specified dimension. +* Return an array containing a truncated view of an input ndarray and a view of the first element(s) along a specified dimension. * * @module @stdlib/ndarray/base/shift * @@ -39,7 +39,7 @@ * var arr = ndarray2array( x ); * // returns [ [ 1.0, 2.0 ], [ 3.0, 4.0 ], [ 5.0, 6.0 ] ] * -* var y = shift( x, 0, false, false ); +* var y = shift( x, 0, false ); * // returns [ , ] * * arr = ndarray2array( y[ 0 ] ); diff --git a/lib/node_modules/@stdlib/ndarray/base/shift/lib/main.js b/lib/node_modules/@stdlib/ndarray/base/shift/lib/main.js index d51f6eddfe40..95373be48740 100644 --- a/lib/node_modules/@stdlib/ndarray/base/shift/lib/main.js +++ b/lib/node_modules/@stdlib/ndarray/base/shift/lib/main.js @@ -20,23 +20,25 @@ // MODULES // -var ndims = require( '@stdlib/ndarray/base/ndims' ); var normalizeIndex = require( '@stdlib/ndarray/base/normalize-index' ); var getShape = require( '@stdlib/ndarray/base/shape' ); var sliceFrom = require( '@stdlib/ndarray/base/slice-from' ); var sliceTo = require( '@stdlib/ndarray/base/slice-to' ); +var nulls = require( '@stdlib/array/base/nulls' ); +var format = require( '@stdlib/string/format' ); // MAIN // /** -* Returns an array containing a truncated view of an input ndarray and a complementary view of the first element(s) along a specified dimension. +* Returns an array containing a truncated view of an input ndarray and a view of the first element(s) along a specified dimension. * * @param {ndarray} x - input array * @param {integer} dim - dimension along which to perform the operation -* @param {boolean} strict - boolean indicating whether to enforce strict bounds checking * @param {boolean} writable - boolean indicating whether returned arrays should be writable -* @returns {Array} a list of ndarrays +* @throws {TypeError} first argument must be an ndarray having one or more dimensions +* @throws {RangeError} dimension index exceeds the number of dimensions +* @returns {Array} a list of ndarrays * * @example * var ndarray = require( '@stdlib/ndarray/ctor' ); @@ -53,36 +55,45 @@ var sliceTo = require( '@stdlib/ndarray/base/slice-to' ); * var arr = ndarray2array( x ); * // returns [ [ 1.0, 2.0 ], [ 3.0, 4.0 ], [ 5.0, 6.0 ] ] * -* var y = shift( x, 0, false, false ); +* var y = shift( x, 0, false ); * // returns [ , ] * * arr = ndarray2array( y[ 0 ] ); -* // returns [ [ 3, 4 ], [ 5, 6 ] ] +* // returns [ [ 3.0, 4.0 ], [ 5.0, 6.0 ] ] * * arr = ndarray2array( y[ 1 ] ); -* // returns [ [ 1, 2 ] ] +* // returns [ [ 1.0, 2.0 ] ] */ -function shift( x, dim, strict, writable ) { +function shift( x, dim, writable ) { var v0; var v1; var sh; var N; var s; - var i; // Retrieve array meta data: sh = getShape( x ); - N = ndims( x ); + N = sh.length; - // Resolve view slice: - s = []; - for ( i = 0; i < N; i++ ) { - s[ i ] = null; + // Check whether we were provided a zero-dimensional array... + if ( N === 0 ) { + throw new TypeError( format( 'invalid argument. First argument must be an ndarray having one or more dimensions. Number of dimensions: %d.', N ) ); } - dim = normalizeIndex( dim, N -1 ); + // Normalize the dimension index: + dim = normalizeIndex( dim, N-1 ); + if ( dim === -1 ) { + throw new RangeError( format( 'invalid argument. Dimension index exceeds the number of dimensions. Number of dimensions: %d. Value: `%d`.', N, dim ) ); + } + // Define a list of slice arguments: + s = nulls( N ); s[ dim ] = sh[ dim ] - ( sh[ dim ] - 1 ); - v0 = sliceFrom( x, s, strict, writable ); - v1 = sliceTo( x, s, strict, writable ); + + // Create a truncated view: + v0 = sliceFrom( x, s, true, writable ); + + // Create a view of the first element(s): + v1 = sliceTo( x, s, true, writable ); + return [ v0, v1 ]; } diff --git a/lib/node_modules/@stdlib/ndarray/base/shift/package.json b/lib/node_modules/@stdlib/ndarray/base/shift/package.json index fe3dbd83b1cc..fac244fcf27f 100644 --- a/lib/node_modules/@stdlib/ndarray/base/shift/package.json +++ b/lib/node_modules/@stdlib/ndarray/base/shift/package.json @@ -1,7 +1,7 @@ { "name": "@stdlib/ndarray/base/shift", "version": "0.0.0", - "description": "Return an array containing a truncated view of an input ndarray and a complementary view of the first element(s) along a specified dimension.", + "description": "Return an array containing a truncated view of an input ndarray and a view of the first element(s) along a specified dimension.", "license": "Apache-2.0", "author": { "name": "The Stdlib Authors", From bf4686f6e93757d055889d00cacd1a9c35237d5c Mon Sep 17 00:00:00 2001 From: headlessNode Date: 2025年9月24日 22:04:36 +0500 Subject: [PATCH 4/7] refactor: update tests --- 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: na - task: lint_package_json status: na - task: lint_repl_help status: na - task: lint_javascript_src status: na - task: lint_javascript_cli status: na - task: lint_javascript_examples status: na - task: lint_javascript_tests status: passed - task: lint_javascript_benchmarks status: na - 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: na - task: lint_typescript_tests status: na - task: lint_license_headers status: passed --- --- .../@stdlib/ndarray/base/shift/test/test.js | 24 +++++++++---------- 1 file changed, 12 insertions(+), 12 deletions(-) diff --git a/lib/node_modules/@stdlib/ndarray/base/shift/test/test.js b/lib/node_modules/@stdlib/ndarray/base/shift/test/test.js index 00f0ed421e8b..5ed72522cd3c 100644 --- a/lib/node_modules/@stdlib/ndarray/base/shift/test/test.js +++ b/lib/node_modules/@stdlib/ndarray/base/shift/test/test.js @@ -38,7 +38,7 @@ tape( 'main export is a function', function test( t ) { t.end(); }); -tape( 'the function returns an array containing ndarrays (ndims=1, strict, readonly)', function test( t ) { +tape( 'the function returns an array containing ndarrays (ndims=1, readonly)', function test( t ) { var actual; var buf; var ord; @@ -55,7 +55,7 @@ tape( 'the function returns an array containing ndarrays (ndims=1, strict, reado x = new ctor( 'float64', buf, sh, st, o, ord ); - actual = shift( x, 0, true, false ); + actual = shift( x, 0, false ); t.strictEqual( isndarrayLike( actual[0] ), true, 'returns expected value' ); t.strictEqual( isndarrayLike( actual[1] ), true, 'returns expected value' ); @@ -71,7 +71,7 @@ tape( 'the function returns an array containing ndarrays (ndims=1, strict, reado t.end(); }); -tape( 'the function returns an array containing ndarrays (ndims=1, strict, writable)', function test( t ) { +tape( 'the function returns an array containing ndarrays (ndims=1, writable)', function test( t ) { var actual; var buf; var ord; @@ -88,7 +88,7 @@ tape( 'the function returns an array containing ndarrays (ndims=1, strict, writa x = new ctor( 'float64', buf, sh, st, o, ord ); - actual = shift( x, 0, true, true ); + actual = shift( x, 0, true ); t.strictEqual( isndarrayLike( actual[0] ), true, 'returns expected value' ); t.strictEqual( isndarrayLike( actual[1] ), true, 'returns expected value' ); @@ -104,7 +104,7 @@ tape( 'the function returns an array containing ndarrays (ndims=1, strict, writa t.end(); }); -tape( 'the function returns an array containing ndarrays (ndims=2, strict, readonly)', function test( t ) { +tape( 'the function returns an array containing ndarrays (ndims=2, readonly)', function test( t ) { var actual; var buf; var ord; @@ -121,7 +121,7 @@ tape( 'the function returns an array containing ndarrays (ndims=2, strict, reado x = new ctor( 'float64', buf, sh, st, o, ord ); - actual = shift( x, 0, true, false ); + actual = shift( x, 0, false ); t.strictEqual( isndarrayLike( actual[0] ), true, 'returns expected value' ); t.strictEqual( isndarrayLike( actual[1] ), true, 'returns expected value' ); @@ -137,7 +137,7 @@ tape( 'the function returns an array containing ndarrays (ndims=2, strict, reado t.end(); }); -tape( 'the function returns an array containing ndarrays (ndims=2, strict, writable)', function test( t ) { +tape( 'the function returns an array containing ndarrays (ndims=2, writable)', function test( t ) { var actual; var buf; var ord; @@ -154,7 +154,7 @@ tape( 'the function returns an array containing ndarrays (ndims=2, strict, writa x = new ctor( 'float64', buf, sh, st, o, ord ); - actual = shift( x, 0, true, true ); + actual = shift( x, 0, true ); t.strictEqual( isndarrayLike( actual[0] ), true, 'returns expected value' ); t.strictEqual( isndarrayLike( actual[1] ), true, 'returns expected value' ); @@ -170,7 +170,7 @@ tape( 'the function returns an array containing ndarrays (ndims=2, strict, writa t.end(); }); -tape( 'the function returns an array containing ndarrays (ndims=3, strict, readonly)', function test( t ) { +tape( 'the function returns an array containing ndarrays (ndims=3, readonly)', function test( t ) { var actual; var buf; var ord; @@ -199,7 +199,7 @@ tape( 'the function returns an array containing ndarrays (ndims=3, strict, reado */ x = new ctor( 'float64', buf, sh, st, o, ord ); - actual = shift( x, 1, true, false ); + actual = shift( x, 1, false ); t.strictEqual( isndarrayLike( actual[0] ), true, 'returns expected value' ); t.strictEqual( isndarrayLike( actual[1] ), true, 'returns expected value' ); @@ -215,7 +215,7 @@ tape( 'the function returns an array containing ndarrays (ndims=3, strict, reado t.end(); }); -tape( 'the function returns an array containing ndarrays (ndims=3, strict, writable)', function test( t ) { +tape( 'the function returns an array containing ndarrays (ndims=3, writable)', function test( t ) { var actual; var buf; var ord; @@ -244,7 +244,7 @@ tape( 'the function returns an array containing ndarrays (ndims=3, strict, writa */ x = new ctor( 'float64', buf, sh, st, o, ord ); - actual = shift( x, 1, true, true ); + actual = shift( x, 1, true ); t.strictEqual( isndarrayLike( actual[0] ), true, 'returns expected value' ); t.strictEqual( isndarrayLike( actual[1] ), true, 'returns expected value' ); From cdf1ab58ccd1c59df3a69f30802ef6e2953fdda4 Mon Sep 17 00:00:00 2001 From: Athan Date: 2025年9月25日 00:19:17 -0700 Subject: [PATCH 5/7] chore: clean-up --- 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: na - task: lint_repl_help status: na - 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: na - 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: na - task: lint_typescript_tests status: na - task: lint_license_headers status: passed --- --- .../@stdlib/ndarray/base/shift/README.md | 4 +- .../ndarray/base/shift/examples/index.js | 5 +- .../@stdlib/ndarray/base/shift/lib/index.js | 4 +- .../@stdlib/ndarray/base/shift/lib/main.js | 8 +- .../@stdlib/ndarray/base/shift/test/test.js | 165 ++++++++++++++---- 5 files changed, 141 insertions(+), 45 deletions(-) diff --git a/lib/node_modules/@stdlib/ndarray/base/shift/README.md b/lib/node_modules/@stdlib/ndarray/base/shift/README.md index 905bf9c05d73..c1180483163e 100644 --- a/lib/node_modules/@stdlib/ndarray/base/shift/README.md +++ b/lib/node_modules/@stdlib/ndarray/base/shift/README.md @@ -46,6 +46,7 @@ Returns an array containing a truncated view of an input ndarray and a view of t ```javascript var ndarray = require( '@stdlib/ndarray/ctor' ); +var getShape = require( '@stdlib/ndarray/shape' ); var ndarray2array = require( '@stdlib/ndarray/to-array' ); var buffer = [ 1.0, 2.0, 3.0, 4.0, 5.0, 6.0 ]; @@ -56,7 +57,7 @@ var offset = 0; var x = ndarray( 'generic', buffer, shape, strides, offset, 'row-major' ); // returns -var sh = x.shape; +var sh = getShape( x ); // returns [ 3, 2 ] var arr = ndarray2array( x ); @@ -116,6 +117,7 @@ var x = array( buf, { 'shape': [ 3, 3, 3 ] }); +// Remove the first row from each matrix: var y = shift( x, 1, false ); // returns [ , ] diff --git a/lib/node_modules/@stdlib/ndarray/base/shift/examples/index.js b/lib/node_modules/@stdlib/ndarray/base/shift/examples/index.js index eb75e18edd69..79402d072ba4 100644 --- a/lib/node_modules/@stdlib/ndarray/base/shift/examples/index.js +++ b/lib/node_modules/@stdlib/ndarray/base/shift/examples/index.js @@ -30,11 +30,10 @@ var buf = zeroTo( 27 ); var x = array( buf, { 'shape': [ 3, 3, 3 ] }); - console.log( ndarray2array( x ) ); -var y = shift( x, 1, false, false ); +// Remove the first row from each matrix: +var y = shift( x, 1, false ); console.log( ndarray2array( y[ 0 ] ) ); - console.log( ndarray2array( y[ 1 ] ) ); diff --git a/lib/node_modules/@stdlib/ndarray/base/shift/lib/index.js b/lib/node_modules/@stdlib/ndarray/base/shift/lib/index.js index b5bb150adc95..e3b0749a781e 100644 --- a/lib/node_modules/@stdlib/ndarray/base/shift/lib/index.js +++ b/lib/node_modules/@stdlib/ndarray/base/shift/lib/index.js @@ -43,10 +43,10 @@ * // returns [ , ] * * arr = ndarray2array( y[ 0 ] ); -* // returns [ [ 3, 4 ], [ 5, 6 ] ] +* // returns [ [ 3.0, 4.0 ], [ 5.0, 6.0 ] ] * * arr = ndarray2array( y[ 1 ] ); -* // returns [ [ 1, 2 ] ] +* // returns [ [ 1.0, 2.0 ] ] */ // MODULES // diff --git a/lib/node_modules/@stdlib/ndarray/base/shift/lib/main.js b/lib/node_modules/@stdlib/ndarray/base/shift/lib/main.js index 95373be48740..ec75aa9c56a9 100644 --- a/lib/node_modules/@stdlib/ndarray/base/shift/lib/main.js +++ b/lib/node_modules/@stdlib/ndarray/base/shift/lib/main.js @@ -21,7 +21,7 @@ // MODULES // var normalizeIndex = require( '@stdlib/ndarray/base/normalize-index' ); -var getShape = require( '@stdlib/ndarray/base/shape' ); +var ndims = require( '@stdlib/ndarray/base/ndims' ); var sliceFrom = require( '@stdlib/ndarray/base/slice-from' ); var sliceTo = require( '@stdlib/ndarray/base/slice-to' ); var nulls = require( '@stdlib/array/base/nulls' ); @@ -67,13 +67,11 @@ var format = require( '@stdlib/string/format' ); function shift( x, dim, writable ) { var v0; var v1; - var sh; var N; var s; // Retrieve array meta data: - sh = getShape( x ); - N = sh.length; + N = ndims( x ); // Check whether we were provided a zero-dimensional array... if ( N === 0 ) { @@ -86,7 +84,7 @@ function shift( x, dim, writable ) { } // Define a list of slice arguments: s = nulls( N ); - s[ dim ] = sh[ dim ] - ( sh[ dim ] - 1 ); + s[ dim ] = 1; // Create a truncated view: v0 = sliceFrom( x, s, true, writable ); diff --git a/lib/node_modules/@stdlib/ndarray/base/shift/test/test.js b/lib/node_modules/@stdlib/ndarray/base/shift/test/test.js index 5ed72522cd3c..7ddd903fb75b 100644 --- a/lib/node_modules/@stdlib/ndarray/base/shift/test/test.js +++ b/lib/node_modules/@stdlib/ndarray/base/shift/test/test.js @@ -24,7 +24,8 @@ var tape = require( 'tape' ); var isndarrayLike = require( '@stdlib/assert/is-ndarray-like' ); var isReadOnly = require( '@stdlib/ndarray/base/assert/is-read-only' ); var zeroTo = require( '@stdlib/array/base/zero-to' ); -var typedarray = require( '@stdlib/array/typed' ); +var zeros = require( '@stdlib/ndarray/zeros' ); +var getShape = require( '@stdlib/ndarray/shape' ); var ndarray2array = require( '@stdlib/ndarray/to-array' ); var ctor = require( '@stdlib/ndarray/ctor' ); var shift = require( './../lib' ); @@ -38,6 +39,48 @@ tape( 'main export is a function', function test( t ) { t.end(); }); +tape( 'the function throws an error if provided a zero-dimensional array', function test( t ) { + var values; + var i; + + values = [ + zeros( [] ) + ]; + for ( i = 0; i < values.length; i++ ) { + t.throws( badValue( values[ i ] ), TypeError, 'throws an error' ); + } + t.end(); + + function badValue( x ) { + return function badValue() { + shift( x, 0, false ); + }; + } +}); + +tape( 'the function throws an error if the dimension index exceeds the number of dimensions', function test( t ) { + var values; + var i; + + values = [ + zeros( [ 1 ] ), + zeros( [ 1, 1 ] ), + zeros( [ 1, 1, 1 ] ), + zeros( [ 1, 1, 1, 1 ] ) + ]; + for ( i = 0; i < values.length; i++ ) { + t.throws( badValue( values[ i ], 10 ), RangeError, 'throws an error when provided ' + values[ i ].shape.join( 'x' ) ); + t.throws( badValue( values[ i ], -10 ), RangeError, 'throws an error when provided ' + values[ i ].shape.join( 'x' ) ); + } + t.end(); + + function badValue( x, dim ) { + return function badValue() { + shift( x, dim, false ); + }; + } +}); + tape( 'the function returns an array containing ndarrays (ndims=1, readonly)', function test( t ) { var actual; var buf; @@ -47,7 +90,7 @@ tape( 'the function returns an array containing ndarrays (ndims=1, readonly)', f var o; var x; - buf = typedarray( zeroTo( 6 ), 'float64' ); + buf = zeroTo( 6, 'float64' ); sh = [ 6 ]; st = [ 1 ]; o = 0; @@ -61,10 +104,8 @@ tape( 'the function returns an array containing ndarrays (ndims=1, readonly)', f t.strictEqual( isndarrayLike( actual[1] ), true, 'returns expected value' ); t.strictEqual( isReadOnly( actual[0] ), true, 'returns expected value' ); t.strictEqual( isReadOnly( actual[1] ), true, 'returns expected value' ); - t.strictEqual( actual[0].ndims, 1, 'returns expected value' ); - t.strictEqual( actual[1].ndims, 1, 'returns expected value' ); - t.strictEqual( actual[0].length, 5, 'returns expected value' ); - t.strictEqual( actual[1].length, 1, 'returns expected value' ); + t.deepEqual( getShape( actual[0] ), [ 5 ], 'returns expected value' ); + t.deepEqual( getShape( actual[1] ), [ 1 ], 'returns expected value' ); t.deepEqual( ndarray2array( actual[0] ), [ 1, 2, 3, 4, 5 ], 'returns expected value' ); t.deepEqual( ndarray2array( actual[1] ), [ 0 ], 'returns expected value' ); @@ -80,7 +121,7 @@ tape( 'the function returns an array containing ndarrays (ndims=1, writable)', f var o; var x; - buf = typedarray( zeroTo( 6 ), 'float64' ); + buf = zeroTo( 6, 'float64' ); sh = [ 6 ]; st = [ 1 ]; o = 0; @@ -88,16 +129,14 @@ tape( 'the function returns an array containing ndarrays (ndims=1, writable)', f x = new ctor( 'float64', buf, sh, st, o, ord ); - actual = shift( x, 0, true ); + actual = shift( x, -1, true ); t.strictEqual( isndarrayLike( actual[0] ), true, 'returns expected value' ); t.strictEqual( isndarrayLike( actual[1] ), true, 'returns expected value' ); t.strictEqual( isReadOnly( actual[0] ), false, 'returns expected value' ); t.strictEqual( isReadOnly( actual[1] ), false, 'returns expected value' ); - t.strictEqual( actual[0].ndims, 1, 'returns expected value' ); - t.strictEqual( actual[1].ndims, 1, 'returns expected value' ); - t.strictEqual( actual[0].length, 5, 'returns expected value' ); - t.strictEqual( actual[1].length, 1, 'returns expected value' ); + t.deepEqual( getShape( actual[0] ), [ 5 ], 'returns expected value' ); + t.deepEqual( getShape( actual[1] ), [ 1 ], 'returns expected value' ); t.deepEqual( ndarray2array( actual[0] ), [ 1, 2, 3, 4, 5 ], 'returns expected value' ); t.deepEqual( ndarray2array( actual[1] ), [ 0 ], 'returns expected value' ); @@ -113,7 +152,7 @@ tape( 'the function returns an array containing ndarrays (ndims=2, readonly)', f var o; var x; - buf = typedarray( zeroTo( 8 ), 'float64' ); + buf = zeroTo( 8, 'float64' ); sh = [ 2, 4 ]; st = [ 4, 1 ]; o = 0; @@ -127,13 +166,22 @@ tape( 'the function returns an array containing ndarrays (ndims=2, readonly)', f t.strictEqual( isndarrayLike( actual[1] ), true, 'returns expected value' ); t.strictEqual( isReadOnly( actual[0] ), true, 'returns expected value' ); t.strictEqual( isReadOnly( actual[1] ), true, 'returns expected value' ); - t.strictEqual( actual[0].ndims, 2, 'returns expected value' ); - t.strictEqual( actual[1].ndims, 2, 'returns expected value' ); - t.strictEqual( actual[0].length, 4, 'returns expected value' ); - t.strictEqual( actual[1].length, 4, 'returns expected value' ); + t.deepEqual( getShape( actual[0] ), [ 1, 4 ], 'returns expected value' ); + t.deepEqual( getShape( actual[1] ), [ 1, 4 ], 'returns expected value' ); t.deepEqual( ndarray2array( actual[0] ), [ [ 4, 5, 6, 7 ] ], 'returns expected value' ); t.deepEqual( ndarray2array( actual[1] ), [ [ 0, 1, 2, 3 ] ], 'returns expected value' ); + actual = shift( x, 1, false ); + + t.strictEqual( isndarrayLike( actual[0] ), true, 'returns expected value' ); + t.strictEqual( isndarrayLike( actual[1] ), true, 'returns expected value' ); + t.strictEqual( isReadOnly( actual[0] ), true, 'returns expected value' ); + t.strictEqual( isReadOnly( actual[1] ), true, 'returns expected value' ); + t.deepEqual( getShape( actual[0] ), [ 2, 3 ], 'returns expected value' ); + t.deepEqual( getShape( actual[1] ), [ 2, 1 ], 'returns expected value' ); + t.deepEqual( ndarray2array( actual[0] ), [ [ 1, 2, 3 ], [ 5, 6, 7 ] ], 'returns expected value' ); + t.deepEqual( ndarray2array( actual[1] ), [ [ 0 ], [ 4 ] ], 'returns expected value' ); + t.end(); }); @@ -146,7 +194,7 @@ tape( 'the function returns an array containing ndarrays (ndims=2, writable)', f var o; var x; - buf = typedarray( zeroTo( 8 ), 'float64' ); + buf = zeroTo( 8, 'float64' ); sh = [ 2, 4 ]; st = [ 4, 1 ]; o = 0; @@ -154,19 +202,28 @@ tape( 'the function returns an array containing ndarrays (ndims=2, writable)', f x = new ctor( 'float64', buf, sh, st, o, ord ); - actual = shift( x, 0, true ); + actual = shift( x, -2, true ); t.strictEqual( isndarrayLike( actual[0] ), true, 'returns expected value' ); t.strictEqual( isndarrayLike( actual[1] ), true, 'returns expected value' ); t.strictEqual( isReadOnly( actual[0] ), false, 'returns expected value' ); t.strictEqual( isReadOnly( actual[1] ), false, 'returns expected value' ); - t.strictEqual( actual[0].ndims, 2, 'returns expected value' ); - t.strictEqual( actual[1].ndims, 2, 'returns expected value' ); - t.strictEqual( actual[0].length, 4, 'returns expected value' ); - t.strictEqual( actual[1].length, 4, 'returns expected value' ); + t.deepEqual( getShape( actual[0] ), [ 1, 4 ], 'returns expected value' ); + t.deepEqual( getShape( actual[1] ), [ 1, 4 ], 'returns expected value' ); t.deepEqual( ndarray2array( actual[0] ), [ [ 4, 5, 6, 7 ] ], 'returns expected value' ); t.deepEqual( ndarray2array( actual[1] ), [ [ 0, 1, 2, 3 ] ], 'returns expected value' ); + actual = shift( x, -1, true ); + + t.strictEqual( isndarrayLike( actual[0] ), true, 'returns expected value' ); + t.strictEqual( isndarrayLike( actual[1] ), true, 'returns expected value' ); + t.strictEqual( isReadOnly( actual[0] ), false, 'returns expected value' ); + t.strictEqual( isReadOnly( actual[1] ), false, 'returns expected value' ); + t.deepEqual( getShape( actual[0] ), [ 2, 3 ], 'returns expected value' ); + t.deepEqual( getShape( actual[1] ), [ 2, 1 ], 'returns expected value' ); + t.deepEqual( ndarray2array( actual[0] ), [ [ 1, 2, 3 ], [ 5, 6, 7 ] ], 'returns expected value' ); + t.deepEqual( ndarray2array( actual[1] ), [ [ 0 ], [ 4 ] ], 'returns expected value' ); + t.end(); }); @@ -179,7 +236,7 @@ tape( 'the function returns an array containing ndarrays (ndims=3, readonly)', f var o; var x; - buf = typedarray( zeroTo( 8 ), 'float64' ); + buf = zeroTo( 8, 'float64' ); sh = [ 2, 2, 2 ]; st = [ 4, 2, 1 ]; o = 0; @@ -199,19 +256,39 @@ tape( 'the function returns an array containing ndarrays (ndims=3, readonly)', f */ x = new ctor( 'float64', buf, sh, st, o, ord ); + actual = shift( x, 0, false ); + + t.strictEqual( isndarrayLike( actual[0] ), true, 'returns expected value' ); + t.strictEqual( isndarrayLike( actual[1] ), true, 'returns expected value' ); + t.strictEqual( isReadOnly( actual[0] ), true, 'returns expected value' ); + t.strictEqual( isReadOnly( actual[1] ), true, 'returns expected value' ); + t.deepEqual( getShape( actual[0] ), [ 1, 2, 2 ], 'returns expected value' ); + t.deepEqual( getShape( actual[1] ), [ 1, 2, 2 ], 'returns expected value' ); + t.deepEqual( ndarray2array( actual[0] ), [ [ [ 4, 5 ], [ 6, 7 ] ] ], 'returns expected value' ); + t.deepEqual( ndarray2array( actual[1] ), [ [ [ 0, 1 ], [ 2, 3 ] ] ], 'returns expected value' ); + actual = shift( x, 1, false ); t.strictEqual( isndarrayLike( actual[0] ), true, 'returns expected value' ); t.strictEqual( isndarrayLike( actual[1] ), true, 'returns expected value' ); t.strictEqual( isReadOnly( actual[0] ), true, 'returns expected value' ); t.strictEqual( isReadOnly( actual[1] ), true, 'returns expected value' ); - t.strictEqual( actual[0].ndims, 3, 'returns expected value' ); - t.strictEqual( actual[1].ndims, 3, 'returns expected value' ); - t.strictEqual( actual[0].length, 4, 'returns expected value' ); - t.strictEqual( actual[1].length, 4, 'returns expected value' ); + t.deepEqual( getShape( actual[0] ), [ 2, 1, 2 ], 'returns expected value' ); + t.deepEqual( getShape( actual[1] ), [ 2, 1, 2 ], 'returns expected value' ); t.deepEqual( ndarray2array( actual[0] ), [ [ [ 2, 3 ] ], [ [ 6, 7 ] ] ], 'returns expected value' ); t.deepEqual( ndarray2array( actual[1] ), [ [ [ 0, 1 ] ], [ [ 4, 5 ] ] ], 'returns expected value' ); + actual = shift( x, 2, false ); + + t.strictEqual( isndarrayLike( actual[0] ), true, 'returns expected value' ); + t.strictEqual( isndarrayLike( actual[1] ), true, 'returns expected value' ); + t.strictEqual( isReadOnly( actual[0] ), true, 'returns expected value' ); + t.strictEqual( isReadOnly( actual[1] ), true, 'returns expected value' ); + t.deepEqual( getShape( actual[0] ), [ 2, 2, 1 ], 'returns expected value' ); + t.deepEqual( getShape( actual[1] ), [ 2, 2, 1 ], 'returns expected value' ); + t.deepEqual( ndarray2array( actual[0] ), [ [ [ 1 ], [ 3 ] ], [ [ 5 ], [ 7 ] ] ], 'returns expected value' ); + t.deepEqual( ndarray2array( actual[1] ), [ [ [ 0 ], [ 2 ] ], [ [ 4 ], [ 6 ] ] ], 'returns expected value' ); + t.end(); }); @@ -224,7 +301,7 @@ tape( 'the function returns an array containing ndarrays (ndims=3, writable)', f var o; var x; - buf = typedarray( zeroTo( 8 ), 'float64' ); + buf = zeroTo( 8, 'float64' ); sh = [ 2, 2, 2 ]; st = [ 4, 2, 1 ]; o = 0; @@ -244,18 +321,38 @@ tape( 'the function returns an array containing ndarrays (ndims=3, writable)', f */ x = new ctor( 'float64', buf, sh, st, o, ord ); - actual = shift( x, 1, true ); + actual = shift( x, -3, true ); t.strictEqual( isndarrayLike( actual[0] ), true, 'returns expected value' ); t.strictEqual( isndarrayLike( actual[1] ), true, 'returns expected value' ); t.strictEqual( isReadOnly( actual[0] ), false, 'returns expected value' ); t.strictEqual( isReadOnly( actual[1] ), false, 'returns expected value' ); - t.strictEqual( actual[0].ndims, 3, 'returns expected value' ); - t.strictEqual( actual[1].ndims, 3, 'returns expected value' ); - t.strictEqual( actual[0].length, 4, 'returns expected value' ); - t.strictEqual( actual[1].length, 4, 'returns expected value' ); + t.deepEqual( getShape( actual[0] ), [ 1, 2, 2 ], 'returns expected value' ); + t.deepEqual( getShape( actual[1] ), [ 1, 2, 2 ], 'returns expected value' ); + t.deepEqual( ndarray2array( actual[0] ), [ [ [ 4, 5 ], [ 6, 7 ] ] ], 'returns expected value' ); + t.deepEqual( ndarray2array( actual[1] ), [ [ [ 0, 1 ], [ 2, 3 ] ] ], 'returns expected value' ); + + actual = shift( x, -2, true ); + + t.strictEqual( isndarrayLike( actual[0] ), true, 'returns expected value' ); + t.strictEqual( isndarrayLike( actual[1] ), true, 'returns expected value' ); + t.strictEqual( isReadOnly( actual[0] ), false, 'returns expected value' ); + t.strictEqual( isReadOnly( actual[1] ), false, 'returns expected value' ); + t.deepEqual( getShape( actual[0] ), [ 2, 1, 2 ], 'returns expected value' ); + t.deepEqual( getShape( actual[1] ), [ 2, 1, 2 ], 'returns expected value' ); t.deepEqual( ndarray2array( actual[0] ), [ [ [ 2, 3 ] ], [ [ 6, 7 ] ] ], 'returns expected value' ); t.deepEqual( ndarray2array( actual[1] ), [ [ [ 0, 1 ] ], [ [ 4, 5 ] ] ], 'returns expected value' ); + actual = shift( x, -1, true ); + + t.strictEqual( isndarrayLike( actual[0] ), true, 'returns expected value' ); + t.strictEqual( isndarrayLike( actual[1] ), true, 'returns expected value' ); + t.strictEqual( isReadOnly( actual[0] ), false, 'returns expected value' ); + t.strictEqual( isReadOnly( actual[1] ), false, 'returns expected value' ); + t.deepEqual( getShape( actual[0] ), [ 2, 2, 1 ], 'returns expected value' ); + t.deepEqual( getShape( actual[1] ), [ 2, 2, 1 ], 'returns expected value' ); + t.deepEqual( ndarray2array( actual[0] ), [ [ [ 1 ], [ 3 ] ], [ [ 5 ], [ 7 ] ] ], 'returns expected value' ); + t.deepEqual( ndarray2array( actual[1] ), [ [ [ 0 ], [ 2 ] ], [ [ 4 ], [ 6 ] ] ], 'returns expected value' ); + t.end(); }); From dbfbbe67ea1f9b0d71a3f33e3b4fb3569062134d Mon Sep 17 00:00:00 2001 From: Athan Date: 2025年9月25日 00:26:38 -0700 Subject: [PATCH 6/7] fix: handle edge case when provided an empty array --- 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: na - task: lint_package_json status: na - task: lint_repl_help status: na - task: lint_javascript_src status: passed - task: lint_javascript_cli status: na - task: lint_javascript_examples status: na - task: lint_javascript_tests status: passed - task: lint_javascript_benchmarks status: na - 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: na - task: lint_typescript_tests status: na - task: lint_license_headers status: passed --- --- .../@stdlib/ndarray/base/shift/lib/main.js | 4 +- .../@stdlib/ndarray/base/shift/test/test.js | 67 +++++++++++++++++++ 2 files changed, 69 insertions(+), 2 deletions(-) diff --git a/lib/node_modules/@stdlib/ndarray/base/shift/lib/main.js b/lib/node_modules/@stdlib/ndarray/base/shift/lib/main.js index ec75aa9c56a9..637a15a63626 100644 --- a/lib/node_modules/@stdlib/ndarray/base/shift/lib/main.js +++ b/lib/node_modules/@stdlib/ndarray/base/shift/lib/main.js @@ -87,10 +87,10 @@ function shift( x, dim, writable ) { s[ dim ] = 1; // Create a truncated view: - v0 = sliceFrom( x, s, true, writable ); + v0 = sliceFrom( x, s, false, writable ); // Create a view of the first element(s): - v1 = sliceTo( x, s, true, writable ); + v1 = sliceTo( x, s, false, writable ); return [ v0, v1 ]; } diff --git a/lib/node_modules/@stdlib/ndarray/base/shift/test/test.js b/lib/node_modules/@stdlib/ndarray/base/shift/test/test.js index 7ddd903fb75b..4b9c5afc16cf 100644 --- a/lib/node_modules/@stdlib/ndarray/base/shift/test/test.js +++ b/lib/node_modules/@stdlib/ndarray/base/shift/test/test.js @@ -356,3 +356,70 @@ tape( 'the function returns an array containing ndarrays (ndims=3, writable)', f t.end(); }); + +tape( 'the function returns empty views if provided an empty array (ndims=2)', function test( t ) { + var actual; + var buf; + var ord; + var sh; + var st; + var o; + var x; + + buf = zeroTo( 8, 'float64' ); + st = [ 4, 1 ]; + o = 0; + ord = 'row-major'; + + sh = [ 2, 0 ]; + x = new ctor( 'float64', buf, sh, st, o, ord ); + + actual = shift( x, 0, false ); + + t.strictEqual( isndarrayLike( actual[0] ), true, 'returns expected value' ); + t.strictEqual( isndarrayLike( actual[1] ), true, 'returns expected value' ); + t.strictEqual( isReadOnly( actual[0] ), true, 'returns expected value' ); + t.strictEqual( isReadOnly( actual[1] ), true, 'returns expected value' ); + t.deepEqual( getShape( actual[0] ), [ 1, 0 ], 'returns expected value' ); + t.deepEqual( getShape( actual[1] ), [ 1, 0 ], 'returns expected value' ); + t.deepEqual( ndarray2array( actual[0] ), [], 'returns expected value' ); + t.deepEqual( ndarray2array( actual[1] ), [], 'returns expected value' ); + + actual = shift( x, 1, false ); + + t.strictEqual( isndarrayLike( actual[0] ), true, 'returns expected value' ); + t.strictEqual( isndarrayLike( actual[1] ), true, 'returns expected value' ); + t.strictEqual( isReadOnly( actual[0] ), true, 'returns expected value' ); + t.strictEqual( isReadOnly( actual[1] ), true, 'returns expected value' ); + t.deepEqual( getShape( actual[0] ), [ 2, 0 ], 'returns expected value' ); + t.deepEqual( getShape( actual[1] ), [ 2, 0 ], 'returns expected value' ); + t.deepEqual( ndarray2array( actual[0] ), [], 'returns expected value' ); + t.deepEqual( ndarray2array( actual[1] ), [], 'returns expected value' ); + + sh = [ 0, 4 ]; + x = new ctor( 'float64', buf, sh, st, o, ord ); + + actual = shift( x, 0, false ); + + t.strictEqual( isndarrayLike( actual[0] ), true, 'returns expected value' ); + t.strictEqual( isndarrayLike( actual[1] ), true, 'returns expected value' ); + t.strictEqual( isReadOnly( actual[0] ), true, 'returns expected value' ); + t.strictEqual( isReadOnly( actual[1] ), true, 'returns expected value' ); + t.deepEqual( getShape( actual[0] ), [ 0, 4 ], 'returns expected value' ); + t.deepEqual( getShape( actual[1] ), [ 0, 4 ], 'returns expected value' ); + t.deepEqual( ndarray2array( actual[0] ), [], 'returns expected value' ); + t.deepEqual( ndarray2array( actual[1] ), [], 'returns expected value' ); + + actual = shift( x, 1, false ); + + t.strictEqual( isndarrayLike( actual[0] ), true, 'returns expected value' ); + t.strictEqual( isndarrayLike( actual[1] ), true, 'returns expected value' ); + t.strictEqual( isReadOnly( actual[0] ), true, 'returns expected value' ); + t.strictEqual( isReadOnly( actual[1] ), true, 'returns expected value' ); + t.deepEqual( getShape( actual[0] ), [ 0, 3 ], 'returns expected value' ); + t.deepEqual( getShape( actual[1] ), [ 0, 1 ], 'returns expected value' ); + t.deepEqual( ndarray2array( actual[0] ), [], 'returns expected value' ); + t.deepEqual( ndarray2array( actual[1] ), [], 'returns expected value' ); + + t.end(); +}); From caeb483abc74b9e666b4d5cd9a7177a8d20672b6 Mon Sep 17 00:00:00 2001 From: Athan Date: 2025年9月25日 00:30:36 -0700 Subject: [PATCH 7/7] style: rename variable --- 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: na - task: lint_package_json status: na - task: lint_repl_help status: na - task: lint_javascript_src status: na - task: lint_javascript_cli status: na - task: lint_javascript_examples status: na - task: lint_javascript_tests status: passed - task: lint_javascript_benchmarks status: na - 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: na - task: lint_typescript_tests status: na - task: lint_license_headers status: passed --- --- .../@stdlib/ndarray/base/shift/test/test.js | 18 +++++++++--------- 1 file changed, 9 insertions(+), 9 deletions(-) diff --git a/lib/node_modules/@stdlib/ndarray/base/shift/test/test.js b/lib/node_modules/@stdlib/ndarray/base/shift/test/test.js index 4b9c5afc16cf..87eef35829a0 100644 --- a/lib/node_modules/@stdlib/ndarray/base/shift/test/test.js +++ b/lib/node_modules/@stdlib/ndarray/base/shift/test/test.js @@ -27,7 +27,7 @@ var zeroTo = require( '@stdlib/array/base/zero-to' ); var zeros = require( '@stdlib/ndarray/zeros' ); var getShape = require( '@stdlib/ndarray/shape' ); var ndarray2array = require( '@stdlib/ndarray/to-array' ); -var ctor = require( '@stdlib/ndarray/ctor' ); +var ndarray = require( '@stdlib/ndarray/ctor' ); var shift = require( './../lib' ); @@ -96,7 +96,7 @@ tape( 'the function returns an array containing ndarrays (ndims=1, readonly)', f o = 0; ord = 'row-major'; - x = new ctor( 'float64', buf, sh, st, o, ord ); + x = new ndarray( 'float64', buf, sh, st, o, ord ); actual = shift( x, 0, false ); @@ -127,7 +127,7 @@ tape( 'the function returns an array containing ndarrays (ndims=1, writable)', f o = 0; ord = 'row-major'; - x = new ctor( 'float64', buf, sh, st, o, ord ); + x = new ndarray( 'float64', buf, sh, st, o, ord ); actual = shift( x, -1, true ); @@ -158,7 +158,7 @@ tape( 'the function returns an array containing ndarrays (ndims=2, readonly)', f o = 0; ord = 'row-major'; - x = new ctor( 'float64', buf, sh, st, o, ord ); + x = new ndarray( 'float64', buf, sh, st, o, ord ); actual = shift( x, 0, false ); @@ -200,7 +200,7 @@ tape( 'the function returns an array containing ndarrays (ndims=2, writable)', f o = 0; ord = 'row-major'; - x = new ctor( 'float64', buf, sh, st, o, ord ); + x = new ndarray( 'float64', buf, sh, st, o, ord ); actual = shift( x, -2, true ); @@ -254,7 +254,7 @@ tape( 'the function returns an array containing ndarrays (ndims=3, readonly)', f * ] *]; */ - x = new ctor( 'float64', buf, sh, st, o, ord ); + x = new ndarray( 'float64', buf, sh, st, o, ord ); actual = shift( x, 0, false ); @@ -319,7 +319,7 @@ tape( 'the function returns an array containing ndarrays (ndims=3, writable)', f * ] *]; */ - x = new ctor( 'float64', buf, sh, st, o, ord ); + x = new ndarray( 'float64', buf, sh, st, o, ord ); actual = shift( x, -3, true ); @@ -372,7 +372,7 @@ tape( 'the function returns empty views if provided an empty array (ndims=2)', f ord = 'row-major'; sh = [ 2, 0 ]; - x = new ctor( 'float64', buf, sh, st, o, ord ); + x = new ndarray( 'float64', buf, sh, st, o, ord ); actual = shift( x, 0, false ); @@ -397,7 +397,7 @@ tape( 'the function returns empty views if provided an empty array (ndims=2)', f t.deepEqual( ndarray2array( actual[1] ), [], 'returns expected value' ); sh = [ 0, 4 ]; - x = new ctor( 'float64', buf, sh, st, o, ord ); + x = new ndarray( 'float64', buf, sh, st, o, ord ); actual = shift( x, 0, false );

AltStyle によって変換されたページ (->オリジナル) /