diff --git a/lib/node_modules/@stdlib/ndarray/base/maybe-remove-singleton-dimensions/README.md b/lib/node_modules/@stdlib/ndarray/base/maybe-remove-singleton-dimensions/README.md new file mode 100644 index 000000000000..1dfc0fb3b2b5 --- /dev/null +++ b/lib/node_modules/@stdlib/ndarray/base/maybe-remove-singleton-dimensions/README.md @@ -0,0 +1,131 @@ + + +# maybeRemoveSingletonDimensions + +> Remove singleton dimensions from an input ndarray if and only if it contains singleton dimensions. + + + +
+ +
+ + + + + +
+ +## Usage + + + +```javascript +var maybeRemoveSingletonDimensions = require( '@stdlib/ndarray/base/maybe-remove-singleton-dimensions' ); +``` + +#### maybeRemoveSingletonDimensions( x, writable ) + +Removes singleton dimensions from an input ndarray if and only if it contains singleton dimensions. + + + +```javascript +var array = require( '@stdlib/ndarray/array' ); + +// Create a 1x2x2 ndarray: +var x = array( [ [ [ 1, 2 ], [ 3, 4 ] ] ] ); +// returns [ [ [ 1, 2 ], [ 3, 4 ] ] ] + +// Remove singleton dimensions: +var y = maybeRemoveSingletonDimensions( x, false ); +// returns [ [ 1, 2 ], [ 3, 4 ] ] +``` + +The function accepts the following arguments: + +- **x**: input ndarray. +- **writable**: boolean indicating whether a returned ndarray should be writable. + +
+ + + + + +
+ +## Notes + +- The `writable` parameter **only** applies when the function returns a new ndarray instance (i.e., when the input ndarray contains singleton dimensions). When the input ndarray does not contain singleton dimensions, the function returns the input ndarray unchanged, and the `writable` parameter is ignored. +- The function **only** returns a new ndarray instance if the input ndarray contains singleton dimensions. Otherwise, the input ndarray is returned unchanged. + +
+ + + + + +
+ +## Examples + + + +```javascript +var uniform = require( '@stdlib/random/uniform' ); +var ndarray2array = require( '@stdlib/ndarray/to-array' ); +var maybeRemoveSingletonDimensions = require( '@stdlib/ndarray/base/maybe-remove-singleton-dimensions' ); + +var x = uniform( [ 1, 1, 3, 3, 3 ], -10.0, 10.0 ); +console.log( ndarray2array( x ) ); + +var y = maybeRemoveSingletonDimensions( x, false ); +console.log( ndarray2array( y ) ); +``` + +
+ + + + + +
+ +
+ + + + + + + + + + + + + + diff --git a/lib/node_modules/@stdlib/ndarray/base/maybe-remove-singleton-dimensions/benchmark/benchmark.js b/lib/node_modules/@stdlib/ndarray/base/maybe-remove-singleton-dimensions/benchmark/benchmark.js new file mode 100644 index 000000000000..c8aa774f2a1e --- /dev/null +++ b/lib/node_modules/@stdlib/ndarray/base/maybe-remove-singleton-dimensions/benchmark/benchmark.js @@ -0,0 +1,197 @@ +/** +* @license Apache-2.0 +* +* Copyright (c) 2026 The Stdlib Authors. +* +* Licensed under the Apache License, Version 2.0 (the "License"); +* you may not use this file except in compliance with the License. +* You may obtain a copy of the License at +* +* http://www.apache.org/licenses/LICENSE-2.0 +* +* Unless required by applicable law or agreed to in writing, software +* distributed under the License is distributed on an "AS IS" BASIS, +* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. +* See the License for the specific language governing permissions and +* limitations under the License. +*/ + +'use strict'; + +// MODULES // + +var bench = require( '@stdlib/bench' ); +var Float64Array = require( '@stdlib/array/float64' ); +var ndarrayBase = require( '@stdlib/ndarray/base/ctor' ); +var ndarray = require( '@stdlib/ndarray/ctor' ); +var isndarrayLike = require( '@stdlib/assert/is-ndarray-like' ); +var format = require( '@stdlib/string/format' ); +var pkg = require( './../package.json' ).name; +var maybeRemoveSingletonDimensions = require( './../lib' ); + + +// MAIN // + +bench( format( '%s::base_ndarray,2d', pkg ), function benchmark( b ) { + var strides; + var values; + var buffer; + var offset; + var dtype; + var shape; + var order; + var out; + var i; + + dtype = 'float64'; + buffer = new Float64Array( 2 ); + shape = [ 1, 2 ]; + strides = [ 2, 1 ]; + offset = 0; + order = 'row-major'; + + values = [ + ndarrayBase( dtype, buffer, shape, strides, offset, order ), + ndarrayBase( dtype, buffer, shape, strides, offset, order ), + ndarrayBase( dtype, buffer, shape, strides, offset, order ), + ndarrayBase( dtype, buffer, shape, strides, offset, order ), + ndarrayBase( dtype, buffer, shape, strides, offset, order ) + ]; + + b.tic(); + for ( i = 0; i < b.iterations; i++ ) { + out = maybeRemoveSingletonDimensions( values[ i%values.length ], false ); + if ( typeof out !== 'object' ) { + b.fail( 'should return an object' ); + } + } + b.toc(); + if ( !isndarrayLike( out ) ) { + b.fail( 'should return an ndarray' ); + } + b.pass( 'benchmark finished' ); + b.end(); +}); + +bench( format( '%s::base_ndarray,2d,no_singleton_dimensions', pkg ), function benchmark( b ) { + var strides; + var values; + var buffer; + var offset; + var dtype; + var shape; + var order; + var out; + var i; + + dtype = 'float64'; + buffer = new Float64Array( 4 ); + shape = [ 2, 2 ]; + strides = [ 2, 1 ]; + offset = 0; + order = 'row-major'; + + values = [ + ndarrayBase( dtype, buffer, shape, strides, offset, order ), + ndarrayBase( dtype, buffer, shape, strides, offset, order ), + ndarrayBase( dtype, buffer, shape, strides, offset, order ), + ndarrayBase( dtype, buffer, shape, strides, offset, order ), + ndarrayBase( dtype, buffer, shape, strides, offset, order ) + ]; + + b.tic(); + for ( i = 0; i < b.iterations; i++ ) { + out = maybeRemoveSingletonDimensions( values[ i%values.length ], false ); + if ( typeof out !== 'object' ) { + b.fail( 'should return an object' ); + } + } + b.toc(); + if ( !isndarrayLike( out ) ) { + b.fail( 'should return an ndarray' ); + } + b.pass( 'benchmark finished' ); + b.end(); +}); + +bench( format( '%s::ndarray,2d', pkg ), function benchmark( b ) { + var strides; + var values; + var buffer; + var offset; + var dtype; + var shape; + var order; + var out; + var i; + + dtype = 'float64'; + buffer = new Float64Array( 2 ); + shape = [ 1, 2 ]; + strides = [ 2, 1 ]; + offset = 0; + order = 'row-major'; + + values = [ + ndarray( dtype, buffer, shape, strides, offset, order ), + ndarray( dtype, buffer, shape, strides, offset, order ), + ndarray( dtype, buffer, shape, strides, offset, order ), + ndarray( dtype, buffer, shape, strides, offset, order ), + ndarray( dtype, buffer, shape, strides, offset, order ) + ]; + + b.tic(); + for ( i = 0; i < b.iterations; i++ ) { + out = maybeRemoveSingletonDimensions( values[ i%values.length ], false ); + if ( typeof out !== 'object' ) { + b.fail( 'should return an object' ); + } + } + b.toc(); + if ( !isndarrayLike( out ) ) { + b.fail( 'should return an ndarray' ); + } + b.pass( 'benchmark finished' ); + b.end(); +}); + +bench( format( '%s::ndarray,2d,no_singleton_dimensions', pkg ), function benchmark( b ) { + var strides; + var values; + var buffer; + var offset; + var dtype; + var shape; + var order; + var out; + var i; + + dtype = 'float64'; + buffer = new Float64Array( 4 ); + shape = [ 2, 2 ]; + strides = [ 2, 1 ]; + offset = 0; + order = 'row-major'; + + values = [ + ndarray( dtype, buffer, shape, strides, offset, order ), + ndarray( dtype, buffer, shape, strides, offset, order ), + ndarray( dtype, buffer, shape, strides, offset, order ), + ndarray( dtype, buffer, shape, strides, offset, order ), + ndarray( dtype, buffer, shape, strides, offset, order ) + ]; + + b.tic(); + for ( i = 0; i < b.iterations; i++ ) { + out = maybeRemoveSingletonDimensions( values[ i%values.length ], false ); + if ( typeof out !== 'object' ) { + b.fail( 'should return an object' ); + } + } + b.toc(); + if ( !isndarrayLike( out ) ) { + b.fail( 'should return an ndarray' ); + } + b.pass( 'benchmark finished' ); + b.end(); +}); diff --git a/lib/node_modules/@stdlib/ndarray/base/maybe-remove-singleton-dimensions/docs/repl.txt b/lib/node_modules/@stdlib/ndarray/base/maybe-remove-singleton-dimensions/docs/repl.txt new file mode 100644 index 000000000000..47b0a990751b --- /dev/null +++ b/lib/node_modules/@stdlib/ndarray/base/maybe-remove-singleton-dimensions/docs/repl.txt @@ -0,0 +1,27 @@ + +{{alias}}( x, writable ) + Removes singleton dimensions from an input ndarray if and only if it + contains singleton dimensions. + + Parameters + ---------- + x: ndarray + Input array. + + writable: boolean + Boolean indicating whether a returned array should be writable. + + Returns + ------- + out: ndarray + Squeezed ndarray view or input ndarray. + + Examples + -------- + > var x = {{alias:@stdlib/ndarray/array}}( [ [ 1, 2 ], [ 3, 4 ] ], { 'ndmin': 5 } ) + [ [ [ [ [ 1, 2 ], [ 3, 4 ] ] ] ] ] + > var y = {{alias}}( x, false ) + [ [ 1, 2 ], [ 3, 4 ] ] + + See Also + -------- diff --git a/lib/node_modules/@stdlib/ndarray/base/maybe-remove-singleton-dimensions/docs/types/index.d.ts b/lib/node_modules/@stdlib/ndarray/base/maybe-remove-singleton-dimensions/docs/types/index.d.ts new file mode 100644 index 000000000000..c0720f1f7d0c --- /dev/null +++ b/lib/node_modules/@stdlib/ndarray/base/maybe-remove-singleton-dimensions/docs/types/index.d.ts @@ -0,0 +1,53 @@ +/* +* @license Apache-2.0 +* +* Copyright (c) 2026 The Stdlib Authors. +* +* Licensed under the Apache License, Version 2.0 (the "License"); +* you may not use this file except in compliance with the License. +* You may obtain a copy of the License at +* +* http://www.apache.org/licenses/LICENSE-2.0 +* +* Unless required by applicable law or agreed to in writing, software +* distributed under the License is distributed on an "AS IS" BASIS, +* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. +* See the License for the specific language governing permissions and +* limitations under the License. +*/ + +// TypeScript Version: 4.1 + +/// + +import { ndarray } from '@stdlib/types/ndarray'; + +/** +* Removes singleton dimensions from an input ndarray if and only if it contains singleton dimensions. +* +* ## Notes +* +* - If the input ndarray does not contain any singleton dimensions, the function returns the input ndarray unchanged. +* - If the input ndarray contains singleton dimensions, the function returns a new ndarray instance without those dimensions. +* +* @param x - input array +* @param writable - boolean indicating whether a returned array should be writable +* @returns squeezed ndarray view or input ndarray +* +* @example +* var array = require( '@stdlib/ndarray/array' ); +* +* var x = array( [ [ 1, 2 ], [ 3, 4 ] ], { +* 'ndmin': 5 +* }); +* // returns [ [ [ [ [ 1, 2 ], [ 3, 4 ] ] ] ] ] +* +* var y = maybeRemoveSingletonDimensions( x, false ); +* // returns [ [ 1, 2 ], [ 3, 4 ] ] +*/ +declare function maybeRemoveSingletonDimensions( x: T, writable: boolean ): T; + + +// EXPORTS // + +export = maybeRemoveSingletonDimensions; diff --git a/lib/node_modules/@stdlib/ndarray/base/maybe-remove-singleton-dimensions/docs/types/test.ts b/lib/node_modules/@stdlib/ndarray/base/maybe-remove-singleton-dimensions/docs/types/test.ts new file mode 100644 index 000000000000..6786b502392c --- /dev/null +++ b/lib/node_modules/@stdlib/ndarray/base/maybe-remove-singleton-dimensions/docs/types/test.ts @@ -0,0 +1,71 @@ +/* +* @license Apache-2.0 +* +* Copyright (c) 2026 The Stdlib Authors. +* +* Licensed under the Apache License, Version 2.0 (the "License"); +* you may not use this file except in compliance with the License. +* You may obtain a copy of the License at +* +* http://www.apache.org/licenses/LICENSE-2.0 +* +* Unless required by applicable law or agreed to in writing, software +* distributed under the License is distributed on an "AS IS" BASIS, +* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. +* See the License for the specific language governing permissions and +* limitations under the License. +*/ + +/* eslint-disable space-in-parens */ + +import zeros = require( '@stdlib/ndarray/zeros' ); +import maybeRemoveSingletonDimensions = require( './index' ); + + +// TESTS // + +// The function returns an ndarray... +{ + const x = zeros( [ 2, 2 ], { + 'dtype': 'float64' + }); + + maybeRemoveSingletonDimensions( x, false ); // $ExpectType float64ndarray +} + +// The compiler throws an error if the function is not provided a first argument which is an ndarray... +{ + maybeRemoveSingletonDimensions( '5', false ); // $ExpectError + maybeRemoveSingletonDimensions( 5, false ); // $ExpectError + maybeRemoveSingletonDimensions( true, false ); // $ExpectError + maybeRemoveSingletonDimensions( false, false ); // $ExpectError + maybeRemoveSingletonDimensions( null, false ); // $ExpectError + maybeRemoveSingletonDimensions( {}, false ); // $ExpectError + maybeRemoveSingletonDimensions( [ '5' ], false ); // $ExpectError + maybeRemoveSingletonDimensions( ( x: number ): number => x, false ); // $ExpectError +} + +// The compiler throws an error if the function is not provided a second argument which is a boolean... +{ + const x = zeros( [ 2, 2 ], { + 'dtype': 'float64' + }); + + maybeRemoveSingletonDimensions( x, '5' ); // $ExpectError + maybeRemoveSingletonDimensions( x, 5 ); // $ExpectError + maybeRemoveSingletonDimensions( x, null ); // $ExpectError + maybeRemoveSingletonDimensions( x, {} ); // $ExpectError + maybeRemoveSingletonDimensions( x, [ '5' ] ); // $ExpectError + maybeRemoveSingletonDimensions( x, ( x: number ): number => x ); // $ExpectError +} + +// The compiler throws an error if the function is provided an unsupported number of arguments... +{ + const x = zeros( [ 2, 2 ], { + 'dtype': 'float64' + }); + + maybeRemoveSingletonDimensions(); // $ExpectError + maybeRemoveSingletonDimensions( x ); // $ExpectError + maybeRemoveSingletonDimensions( x, false, {} ); // $ExpectError +} diff --git a/lib/node_modules/@stdlib/ndarray/base/maybe-remove-singleton-dimensions/examples/index.js b/lib/node_modules/@stdlib/ndarray/base/maybe-remove-singleton-dimensions/examples/index.js new file mode 100644 index 000000000000..47dd775b690d --- /dev/null +++ b/lib/node_modules/@stdlib/ndarray/base/maybe-remove-singleton-dimensions/examples/index.js @@ -0,0 +1,29 @@ +/** +* @license Apache-2.0 +* +* Copyright (c) 2026 The Stdlib Authors. +* +* Licensed under the Apache License, Version 2.0 (the "License"); +* you may not use this file except in compliance with the License. +* You may obtain a copy of the License at +* +* http://www.apache.org/licenses/LICENSE-2.0 +* +* Unless required by applicable law or agreed to in writing, software +* distributed under the License is distributed on an "AS IS" BASIS, +* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. +* See the License for the specific language governing permissions and +* limitations under the License. +*/ + +'use strict'; + +var uniform = require( '@stdlib/random/uniform' ); +var ndarray2array = require( '@stdlib/ndarray/to-array' ); +var maybeRemoveSingletonDimensions = require( './../lib' ); + +var x = uniform( [ 1, 1, 3, 3, 3 ], -10.0, 10.0 ); +console.log( ndarray2array( x ) ); + +var y = maybeRemoveSingletonDimensions( x, false ); +console.log( ndarray2array( y ) ); diff --git a/lib/node_modules/@stdlib/ndarray/base/maybe-remove-singleton-dimensions/lib/index.js b/lib/node_modules/@stdlib/ndarray/base/maybe-remove-singleton-dimensions/lib/index.js new file mode 100644 index 000000000000..9da1492aa02f --- /dev/null +++ b/lib/node_modules/@stdlib/ndarray/base/maybe-remove-singleton-dimensions/lib/index.js @@ -0,0 +1,46 @@ +/** +* @license Apache-2.0 +* +* Copyright (c) 2026 The Stdlib Authors. +* +* Licensed under the Apache License, Version 2.0 (the "License"); +* you may not use this file except in compliance with the License. +* You may obtain a copy of the License at +* +* http://www.apache.org/licenses/LICENSE-2.0 +* +* Unless required by applicable law or agreed to in writing, software +* distributed under the License is distributed on an "AS IS" BASIS, +* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. +* See the License for the specific language governing permissions and +* limitations under the License. +*/ + +'use strict'; + +/** +* Remove singleton dimensions from an input ndarray if and only if it contains singleton dimensions. +* +* @module @stdlib/ndarray/base/maybe-remove-singleton-dimensions +* +* @example +* var array = require( '@stdlib/ndarray/array' ); +* var maybeRemoveSingletonDimensions = require( '@stdlib/ndarray/base/maybe-remove-singleton-dimensions' ); +* +* var x = array( [ [ 1, 2 ], [ 3, 4 ] ], { +* 'ndmin': 5 +* }); +* // returns [ [ [ [ [ 1, 2 ], [ 3, 4 ] ] ] ] ] +* +* var y = maybeRemoveSingletonDimensions( x, false ); +* // returns [ [ 1, 2 ], [ 3, 4 ] ] +*/ + +// MODULES // + +var main = require( './main.js' ); + + +// EXPORTS // + +module.exports = main; diff --git a/lib/node_modules/@stdlib/ndarray/base/maybe-remove-singleton-dimensions/lib/main.js b/lib/node_modules/@stdlib/ndarray/base/maybe-remove-singleton-dimensions/lib/main.js new file mode 100644 index 000000000000..75683adaca14 --- /dev/null +++ b/lib/node_modules/@stdlib/ndarray/base/maybe-remove-singleton-dimensions/lib/main.js @@ -0,0 +1,93 @@ +/** +* @license Apache-2.0 +* +* Copyright (c) 2026 The Stdlib Authors. +* +* Licensed under the Apache License, Version 2.0 (the "License"); +* you may not use this file except in compliance with the License. +* You may obtain a copy of the License at +* +* http://www.apache.org/licenses/LICENSE-2.0 +* +* Unless required by applicable law or agreed to in writing, software +* distributed under the License is distributed on an "AS IS" BASIS, +* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. +* See the License for the specific language governing permissions and +* limitations under the License. +*/ + +'use strict'; + +// MODULES // + +var getDType = require( '@stdlib/ndarray/base/dtype' ); +var getShape = require( '@stdlib/ndarray/base/shape' ); +var getStrides = require( '@stdlib/ndarray/base/strides' ); +var getOffset = require( '@stdlib/ndarray/base/offset' ); +var getOrder = require( '@stdlib/ndarray/base/order' ); +var getData = require( '@stdlib/ndarray/base/data-buffer' ); + + +// MAIN // + +/** +* Removes singleton dimensions from an input ndarray if and only if it contains singleton dimensions. +* +* ## Notes +* +* - If the input ndarray does not contain any singleton dimensions, the function returns the input ndarray unchanged. +* - If the input ndarray contains singleton dimensions, the function returns a new ndarray instance without those dimensions. +* +* @param {ndarray} x - input array +* @param {boolean} writable - boolean indicating whether a returned array should be writable +* @returns {ndarray} squeezed ndarray view or input ndarray +* +* @example +* var array = require( '@stdlib/ndarray/array' ); +* +* var x = array( [ [ 1, 2 ], [ 3, 4 ] ], { +* 'ndmin': 5 +* }); +* // returns [ [ [ [ [ 1, 2 ], [ 3, 4 ] ] ] ] ] +* +* var y = maybeRemoveSingletonDimensions( x, false ); +* // returns [ [ 1, 2 ], [ 3, 4 ] ] +*/ +function maybeRemoveSingletonDimensions( x, writable ) { + var strides; + var shape; + var sh; + var st; + var N; + var i; + + sh = getShape( x, false ); + st = getStrides( x, false ); + N = sh.length; + + strides = []; + shape = []; + + // Check for singleton dimensions... + for ( i = 0; i < N; i++ ) { + if ( sh[ i ] !== 1 ) { + shape.push( sh[ i ] ); + strides.push( st[ i ] ); + } + } + + // If we filtered out at least one singleton dimension, return a new view... + if ( shape.length < N ) { + return new x.constructor( getDType( x ), getData( x ), shape, strides, getOffset( x ), getOrder( x ), { // eslint-disable-line max-len + 'readonly': !writable + }); + } + + // If no singleton dimensions were found, return the input array... + return x; +} + + +// EXPORTS // + +module.exports = maybeRemoveSingletonDimensions; diff --git a/lib/node_modules/@stdlib/ndarray/base/maybe-remove-singleton-dimensions/package.json b/lib/node_modules/@stdlib/ndarray/base/maybe-remove-singleton-dimensions/package.json new file mode 100644 index 000000000000..466e58306d66 --- /dev/null +++ b/lib/node_modules/@stdlib/ndarray/base/maybe-remove-singleton-dimensions/package.json @@ -0,0 +1,68 @@ +{ + "name": "@stdlib/ndarray/base/maybe-remove-singleton-dimensions", + "version": "0.0.0", + "description": "Remove singleton dimensions from an input ndarray if and only if it contains singleton dimensions.", + "license": "Apache-2.0", + "author": { + "name": "The Stdlib Authors", + "url": "https://github.com/stdlib-js/stdlib/graphs/contributors" + }, + "contributors": [ + { + "name": "The Stdlib Authors", + "url": "https://github.com/stdlib-js/stdlib/graphs/contributors" + } + ], + "main": "./lib", + "directories": { + "benchmark": "./benchmark", + "doc": "./docs", + "example": "./examples", + "lib": "./lib", + "test": "./test" + }, + "types": "./docs/types", + "scripts": {}, + "homepage": "https://github.com/stdlib-js/stdlib", + "repository": { + "type": "git", + "url": "git://github.com/stdlib-js/stdlib.git" + }, + "bugs": { + "url": "https://github.com/stdlib-js/stdlib/issues" + }, + "dependencies": {}, + "devDependencies": {}, + "engines": { + "node": ">=0.10.0", + "npm": ">2.7.0" + }, + "os": [ + "aix", + "darwin", + "freebsd", + "linux", + "macos", + "openbsd", + "sunos", + "win32", + "windows" + ], + "keywords": [ + "stdlib", + "stdtypes", + "types", + "base", + "ndarray", + "squeeze", + "reshape", + "singleton", + "multidimensional", + "array", + "utilities", + "utility", + "utils", + "util" + ], + "__stdlib__": {} +} diff --git a/lib/node_modules/@stdlib/ndarray/base/maybe-remove-singleton-dimensions/test/test.js b/lib/node_modules/@stdlib/ndarray/base/maybe-remove-singleton-dimensions/test/test.js new file mode 100644 index 000000000000..b23ccbadd2e8 --- /dev/null +++ b/lib/node_modules/@stdlib/ndarray/base/maybe-remove-singleton-dimensions/test/test.js @@ -0,0 +1,169 @@ +/** +* @license Apache-2.0 +* +* Copyright (c) 2026 The Stdlib Authors. +* +* Licensed under the Apache License, Version 2.0 (the "License"); +* you may not use this file except in compliance with the License. +* You may obtain a copy of the License at +* +* http://www.apache.org/licenses/LICENSE-2.0 +* +* Unless required by applicable law or agreed to in writing, software +* distributed under the License is distributed on an "AS IS" BASIS, +* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. +* See the License for the specific language governing permissions and +* limitations under the License. +*/ + +'use strict'; + +// MODULES // + +var tape = require( 'tape' ); +var array = require( '@stdlib/ndarray/array' ); +var getShape = require( '@stdlib/ndarray/shape' ); +var getData = require( '@stdlib/ndarray/data-buffer' ); +var ndarray = require( '@stdlib/ndarray/base/ctor' ); +var ndarray2array = require( '@stdlib/ndarray/to-array' ); +var isReadOnly = require( '@stdlib/ndarray/base/assert/is-read-only' ); +var maybeRemoveSingletonDimensions = require( './../lib' ); + + +// TESTS // + +tape( 'main export is a function', function test( t ) { + t.ok( true, __filename ); + t.strictEqual( typeof maybeRemoveSingletonDimensions, 'function', 'main export is a function' ); + t.end(); +}); + +tape( 'if a provided array does not contain singleton dimensions, the function returns the input array', function test( t ) { + var x; + var y; + + x = array( [ [ 1, 2 ], [ 3, 4 ] ] ); + + y = maybeRemoveSingletonDimensions( x, false ); + + t.strictEqual( y, x, 'returns expected value' ); + + x = array( [ [ 1, 2 ], [ 3, 4 ] ] ); + + y = maybeRemoveSingletonDimensions( x, true ); + + t.strictEqual( y, x, 'returns expected value' ); + + t.end(); +}); + +tape( 'if a provided array does not contain singleton dimensions, the function returns the input array (base)', function test( t ) { + var x; + var y; + + x = ndarray( 'generic', [ 1, 2, 3, 4 ], [ 2, 2 ], [ 2, 1 ], 0, 'row-major' ); + + y = maybeRemoveSingletonDimensions( x, false ); + + t.strictEqual( y, x, 'returns expected value' ); + + t.end(); +}); + +tape( 'if a provided array does not contain singleton dimensions, the function returns the input array (base, 0D)', function test( t ) { + var x; + var y; + + x = ndarray( 'generic', [ 1 ], [], [ 0 ], 0, 'row-major' ); + + y = maybeRemoveSingletonDimensions( x, false ); + + t.strictEqual( y, x, 'returns expected value' ); + + t.end(); +}); + +tape( 'if a provided array does not contain singleton dimensions, the function returns the input array (base, empty)', function test( t ) { + var x; + var y; + + x = ndarray( 'generic', [ 1, 2, 3, 4 ], [ 2, 0, 2 ], [ 0, 2, 1 ], 0, 'row-major' ); + + y = maybeRemoveSingletonDimensions( x, false ); + + t.strictEqual( y, x, 'returns expected value' ); + + t.end(); +}); + +tape( 'the function removes singleton dimensions if present (leading)', function test( t ) { + var expected; + var x; + var y; + + x = array( [ [ 1, 2 ], [ 3, 4 ] ], { + 'ndmin': 5 + }); + expected = [ [ 1, 2 ], [ 3, 4 ] ]; + + y = maybeRemoveSingletonDimensions( x, false ); + + t.notEqual( y, x, 'returns expected value' ); + t.deepEqual( getShape( y ), [ 2, 2 ], 'returns expected value' ); + t.strictEqual( getData( y ), getData( x ), 'returns expected value' ); + t.deepEqual( ndarray2array( y ), expected, 'returns expected value' ); + t.strictEqual( isReadOnly( y ), true, 'returns expected value' ); + + y = maybeRemoveSingletonDimensions( x, true ); + + t.notEqual( y, x, 'returns expected value' ); + t.deepEqual( getShape( y ), [ 2, 2 ], 'returns expected value' ); + t.strictEqual( getData( y ), getData( x ), 'returns expected value' ); + t.deepEqual( ndarray2array( y ), expected, 'returns expected value' ); + t.strictEqual( isReadOnly( y ), false, 'returns expected value' ); + + t.end(); +}); + +tape( 'the function removes singleton dimensions if present (trailing)', function test( t ) { + var expected; + var x; + var y; + + x = array( [ 1, 2, 3, 4 ], { + 'shape': [ 2, 1, 2, 1, 1, 1 ] + }); + expected = [ [ 1, 2 ], [ 3, 4 ] ]; + + y = maybeRemoveSingletonDimensions( x, false ); + + t.notEqual( y, x, 'returns expected value' ); + t.deepEqual( getShape( y ), [ 2, 2 ], 'returns expected value' ); + t.strictEqual( getData( y ), getData( x ), 'returns expected value' ); + t.deepEqual( ndarray2array( y ), expected, 'returns expected value' ); + t.strictEqual( isReadOnly( y ), true, 'returns expected value' ); + + y = maybeRemoveSingletonDimensions( x, true ); + + t.notEqual( y, x, 'returns expected value' ); + t.deepEqual( getShape( y ), [ 2, 2 ], 'returns expected value' ); + t.strictEqual( getData( y ), getData( x ), 'returns expected value' ); + t.deepEqual( ndarray2array( y ), expected, 'returns expected value' ); + t.strictEqual( isReadOnly( y ), false, 'returns expected value' ); + + t.end(); +}); + +tape( 'the function removes singleton dimensions if present (base)', function test( t ) { + var x; + var y; + + x = ndarray( 'generic', [ 1, 2, 3, 4 ], [ 1, 1, 2, 1, 2 ], [ 4, 4, 2, 2, 1 ], 0, 'row-major' ); + y = maybeRemoveSingletonDimensions( x, false ); + + t.notEqual( y, x, 'returns expected value' ); + t.deepEqual( getShape( y ), [ 2, 2 ], 'returns expected value' ); + t.strictEqual( getData( y ), getData( x ), 'returns expected value' ); + + t.end(); +});