diff --git a/lib/node_modules/@stdlib/ndarray/base/remove-singleton-dimensions/README.md b/lib/node_modules/@stdlib/ndarray/base/remove-singleton-dimensions/README.md index f5de94717b5d..fdb967f088ef 100644 --- a/lib/node_modules/@stdlib/ndarray/base/remove-singleton-dimensions/README.md +++ b/lib/node_modules/@stdlib/ndarray/base/remove-singleton-dimensions/README.md @@ -40,7 +40,7 @@ limitations under the License. var removeSingletonDimensions = require( '@stdlib/ndarray/base/remove-singleton-dimensions' ); ``` -#### removeSingletonDimensions( x ) +#### removeSingletonDimensions( x, writable ) Returns an ndarray without singleton dimensions (i.e., dimensions whose size is equal to `1`). @@ -49,16 +49,18 @@ var array = require( '@stdlib/ndarray/array' ); // Create a 1x2x2 ndarray: var x = array( [ [ [ 1, 2 ], [ 3, 4 ] ] ] ); -// returns +// returns [ [ [ 1, 2 ], [ 3, 4 ] ] ] // Remove singleton dimensions: -var y = removeSingletonDimensions( x ); -// returns - -var sh = y.shape; -// returns [ 2, 2 ] +var y = removeSingletonDimensions( 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. + @@ -69,8 +71,7 @@ var sh = y.shape; ## Notes -- If a provided ndarray does not have any singleton dimensions, the function returns the provided ndarray unchanged. -- If a provided ndarray does have singleton dimensions, the function returns a new ndarray view. +- The function always returns a new view of the input ndarray. @@ -85,33 +86,15 @@ var sh = y.shape; ```javascript -var array = require( '@stdlib/ndarray/array' ); -var numel = require( '@stdlib/ndarray/base/numel' ); -var ind2sub = require( '@stdlib/ndarray/ind2sub' ); +var uniform = require( '@stdlib/random/uniform' ); +var ndarray2array = require( '@stdlib/ndarray/to-array' ); var removeSingletonDimensions = require( '@stdlib/ndarray/base/remove-singleton-dimensions' ); -// Create a 5-dimensional array: -var x = array( [ [ 1, 2 ], [ 3, 4 ] ], { - 'ndmin': 5 -}); -// returns - -// Remove singleton dimensions: -var y = removeSingletonDimensions( x ); -// returns - -// Retrieve the shape: -var sh = y.shape; -// returns [ 2, 2 ] - -// Retrieve the number of elements: -var N = numel( sh ); +var x = uniform( [ 1, 1, 3, 3, 3 ], -10.0, 10.0 ); +console.log( ndarray2array( x ) ); -// Loop through the array elements... -var i; -for ( i = 0; i < N; i++ ) { - console.log( 'Y[%s] = %d', ind2sub( sh, i ).join( ', ' ), y.iget( i ) ); -} +var y = removeSingletonDimensions( x, false ); +console.log( ndarray2array( y ) ); ``` diff --git a/lib/node_modules/@stdlib/ndarray/base/remove-singleton-dimensions/benchmark/benchmark.js b/lib/node_modules/@stdlib/ndarray/base/remove-singleton-dimensions/benchmark/benchmark.js index cfca24fd32d1..60f6e0d03a73 100644 --- a/lib/node_modules/@stdlib/ndarray/base/remove-singleton-dimensions/benchmark/benchmark.js +++ b/lib/node_modules/@stdlib/ndarray/base/remove-singleton-dimensions/benchmark/benchmark.js @@ -25,13 +25,14 @@ 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 removeSingletonDimensions = require( './../lib' ); // MAIN // -bench( pkg+'::base_ndarray,2d', function benchmark( b ) { +bench( format( '%s::base_ndarray,2d', pkg ), function benchmark( b ) { var strides; var values; var buffer; @@ -59,7 +60,7 @@ bench( pkg+'::base_ndarray,2d', function benchmark( b ) { b.tic(); for ( i = 0; i < b.iterations; i++ ) { - out = removeSingletonDimensions( values[ i%values.length ] ); + out = removeSingletonDimensions( values[ i%values.length ], false ); if ( typeof out !== 'object' ) { b.fail( 'should return an object' ); } @@ -72,7 +73,7 @@ bench( pkg+'::base_ndarray,2d', function benchmark( b ) { b.end(); }); -bench( pkg+'::base_ndarray,2d,no_singleton_dimensions', function benchmark( b ) { +bench( format( '%s::base_ndarray,2d,no_singleton_dimensions', pkg ), function benchmark( b ) { var strides; var values; var buffer; @@ -100,7 +101,7 @@ bench( pkg+'::base_ndarray,2d,no_singleton_dimensions', function benchmark( b ) b.tic(); for ( i = 0; i < b.iterations; i++ ) { - out = removeSingletonDimensions( values[ i%values.length ] ); + out = removeSingletonDimensions( values[ i%values.length ], false ); if ( typeof out !== 'object' ) { b.fail( 'should return an object' ); } @@ -113,7 +114,7 @@ bench( pkg+'::base_ndarray,2d,no_singleton_dimensions', function benchmark( b ) b.end(); }); -bench( pkg+'::ndarray,2d', function benchmark( b ) { +bench( format( '%s::ndarray,2d', pkg ), function benchmark( b ) { var strides; var values; var buffer; @@ -141,7 +142,7 @@ bench( pkg+'::ndarray,2d', function benchmark( b ) { b.tic(); for ( i = 0; i < b.iterations; i++ ) { - out = removeSingletonDimensions( values[ i%values.length ] ); + out = removeSingletonDimensions( values[ i%values.length ], false ); if ( typeof out !== 'object' ) { b.fail( 'should return an object' ); } @@ -154,7 +155,7 @@ bench( pkg+'::ndarray,2d', function benchmark( b ) { b.end(); }); -bench( pkg+'::ndarray,2d,no_singleton_dimensions', function benchmark( b ) { +bench( format( '%s::ndarray,2d,no_singleton_dimensions', pkg ), function benchmark( b ) { var strides; var values; var buffer; @@ -182,7 +183,7 @@ bench( pkg+'::ndarray,2d,no_singleton_dimensions', function benchmark( b ) { b.tic(); for ( i = 0; i < b.iterations; i++ ) { - out = removeSingletonDimensions( values[ i%values.length ] ); + out = removeSingletonDimensions( values[ i%values.length ], false ); if ( typeof out !== 'object' ) { b.fail( 'should return an object' ); } diff --git a/lib/node_modules/@stdlib/ndarray/base/remove-singleton-dimensions/benchmark/benchmark.ndims.js b/lib/node_modules/@stdlib/ndarray/base/remove-singleton-dimensions/benchmark/benchmark.ndims.js index 3573c7055d0e..98f0753e7177 100644 --- a/lib/node_modules/@stdlib/ndarray/base/remove-singleton-dimensions/benchmark/benchmark.ndims.js +++ b/lib/node_modules/@stdlib/ndarray/base/remove-singleton-dimensions/benchmark/benchmark.ndims.js @@ -23,6 +23,7 @@ var bench = require( '@stdlib/bench' ); var array = require( '@stdlib/ndarray/array' ); var isndarrayLike = require( '@stdlib/assert/is-ndarray-like' ); +var format = require( '@stdlib/string/format' ); var pkg = require( './../package.json' ).name; var removeSingletonDimensions = require( './../lib' ); @@ -54,7 +55,7 @@ function createBenchmark( ndims ) { b.tic(); for ( i = 0; i < b.iterations; i++ ) { - out = removeSingletonDimensions( x ); + out = removeSingletonDimensions( x, false ); if ( typeof out !== 'object' ) { b.fail( 'should return an object' ); } @@ -87,7 +88,7 @@ function main() { for ( i = min; i <= max; i++ ) { f = createBenchmark( i ); - bench( pkg+'::ndarray,2d:singleton_dimensions='+i, f ); + bench( format( '%s::ndarray,2d:singleton_dimensions=%d', pkg, i ), f ); } } diff --git a/lib/node_modules/@stdlib/ndarray/base/remove-singleton-dimensions/docs/repl.txt b/lib/node_modules/@stdlib/ndarray/base/remove-singleton-dimensions/docs/repl.txt index b98834b89d8a..e4e369eacf0f 100644 --- a/lib/node_modules/@stdlib/ndarray/base/remove-singleton-dimensions/docs/repl.txt +++ b/lib/node_modules/@stdlib/ndarray/base/remove-singleton-dimensions/docs/repl.txt @@ -1,18 +1,17 @@ -{{alias}}( x ) +{{alias}}( x, writable ) Returns an array without singleton dimensions. - If a provided ndarray does not have any singleton dimensions, the function - returns the provided ndarray unchanged. - - If a provided ndarray does have singleton dimensions, the function returns a - new ndarray view. + The function always returns a new view of the input ndarray. Parameters ---------- x: ndarray Input array. + writable: boolean + Boolean indicating whether a returned array should be writable. + Returns ------- out: ndarray @@ -21,21 +20,9 @@ Examples -------- > var x = {{alias:@stdlib/ndarray/array}}( [ [ 1, 2 ], [ 3, 4 ] ], { 'ndmin': 5 } ) - - > var sh = x.shape - [ 1, 1, 1, 2, 2 ] - > var y = {{alias}}( x ) - - > sh = y.shape - [ 2, 2 ] - > var v = y.get( 0, 0 ) - 1 - > v = y.get( 0, 1 ) - 2 - > v = y.get( 1, 0 ) - 3 - > v = y.get( 1, 1 ) - 4 + [ [ [ [ [1, 2 ], [ 3, 4 ] ] ] ] ] + > var y = {{alias}}( x, false ) + [ [ 1, 2 ], [ 3, 4 ] ] See Also -------- diff --git a/lib/node_modules/@stdlib/ndarray/base/remove-singleton-dimensions/docs/types/index.d.ts b/lib/node_modules/@stdlib/ndarray/base/remove-singleton-dimensions/docs/types/index.d.ts index edca210957f5..23b5ae3b0774 100644 --- a/lib/node_modules/@stdlib/ndarray/base/remove-singleton-dimensions/docs/types/index.d.ts +++ b/lib/node_modules/@stdlib/ndarray/base/remove-singleton-dimensions/docs/types/index.d.ts @@ -27,10 +27,10 @@ import { ndarray } from '@stdlib/types/ndarray'; * * ## Notes * -* - If a provided ndarray does not have any singleton dimensions, the function returns the provided ndarray unchanged. -* - If a provided ndarray does have singleton dimensions, the function returns a new ndarray view. +* - The function always returns a new view of the input ndarray. * * @param x - input array +* @param writable - boolean indicating whether a returned array should be writable * @returns squeezed array * * @example @@ -39,30 +39,12 @@ import { ndarray } from '@stdlib/types/ndarray'; * var x = array( [ [ 1, 2 ], [ 3, 4 ] ], { * 'ndmin': 5 * }); -* // returns +* // returns [ [ [ [ [ 1, 2 ], [ 3, 4 ] ] ] ] ] * -* var shx = x.shape; -* // returns [ 1, 1, 1, 2, 2 ] -* -* var y = removeSingletonDimensions( x ); -* // returns -* -* var shy = y.shape; -* // returns [ 2, 2 ] -* -* var v = y.get( 0, 0 ); -* // returns 1 -* -* v = y.get( 0, 1 ); -* // returns 2 -* -* v = y.get( 1, 0 ); -* // returns 3 -* -* v = y.get( 1, 1 ); -* // returns 4 +* var y = removeSingletonDimensions( x, false ); +* // returns [ [ 1, 2 ], [ 3, 4 ] ] */ -declare function removeSingletonDimensions( x: ndarray ): ndarray; +declare function removeSingletonDimensions( x: ndarray, writable: boolean ): ndarray; // EXPORTS // diff --git a/lib/node_modules/@stdlib/ndarray/base/remove-singleton-dimensions/docs/types/test.ts b/lib/node_modules/@stdlib/ndarray/base/remove-singleton-dimensions/docs/types/test.ts index 9369e480e0b5..2c4ce0a1218b 100644 --- a/lib/node_modules/@stdlib/ndarray/base/remove-singleton-dimensions/docs/types/test.ts +++ b/lib/node_modules/@stdlib/ndarray/base/remove-singleton-dimensions/docs/types/test.ts @@ -26,19 +26,31 @@ import removeSingletonDimensions = require( './index' ); { const x = array( [ [ 1, 2 ], [ 3, 4 ] ] ); - removeSingletonDimensions( x ); // $ExpectType ndarray + removeSingletonDimensions( x, false ); // $ExpectType ndarray } // The compiler throws an error if the function is not provided a first argument which is an ndarray... { - removeSingletonDimensions( '5' ); // $ExpectError - removeSingletonDimensions( 5 ); // $ExpectError - removeSingletonDimensions( true ); // $ExpectError - removeSingletonDimensions( false ); // $ExpectError - removeSingletonDimensions( null ); // $ExpectError - removeSingletonDimensions( {} ); // $ExpectError - removeSingletonDimensions( [ '5' ] ); // $ExpectError - removeSingletonDimensions( ( x: number ): number => x ); // $ExpectError + removeSingletonDimensions( '5', false ); // $ExpectError + removeSingletonDimensions( 5, false ); // $ExpectError + removeSingletonDimensions( true, false ); // $ExpectError + removeSingletonDimensions( false, false ); // $ExpectError + removeSingletonDimensions( null, false ); // $ExpectError + removeSingletonDimensions( {}, false ); // $ExpectError + removeSingletonDimensions( [ '5' ], false ); // $ExpectError + removeSingletonDimensions( ( 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 = array( [ [ 1, 2 ], [ 3, 4 ] ] ); + + removeSingletonDimensions( x, '5' ); // $ExpectError + removeSingletonDimensions( x, 5 ); // $ExpectError + removeSingletonDimensions( x, null ); // $ExpectError + removeSingletonDimensions( x, {} ); // $ExpectError + removeSingletonDimensions( x, [ '5' ] ); // $ExpectError + removeSingletonDimensions( x, ( x: number ): number => x ); // $ExpectError } // The compiler throws an error if the function is provided an unsupported number of arguments... @@ -46,5 +58,6 @@ import removeSingletonDimensions = require( './index' ); const x = array( [ [ 1, 2 ], [ 3, 4 ] ] ); removeSingletonDimensions(); // $ExpectError - removeSingletonDimensions( x, [ 1, 2, 3 ], [ 2, 3 ] ); // $ExpectError + removeSingletonDimensions( x ); // $ExpectError + removeSingletonDimensions( x, false, {} ); // $ExpectError } diff --git a/lib/node_modules/@stdlib/ndarray/base/remove-singleton-dimensions/examples/index.js b/lib/node_modules/@stdlib/ndarray/base/remove-singleton-dimensions/examples/index.js index b9bf8d542b31..21b43da0097f 100644 --- a/lib/node_modules/@stdlib/ndarray/base/remove-singleton-dimensions/examples/index.js +++ b/lib/node_modules/@stdlib/ndarray/base/remove-singleton-dimensions/examples/index.js @@ -18,30 +18,12 @@ 'use strict'; -var array = require( '@stdlib/ndarray/array' ); -var numel = require( '@stdlib/ndarray/base/numel' ); -var ind2sub = require( '@stdlib/ndarray/ind2sub' ); +var uniform = require( '@stdlib/random/uniform' ); +var ndarray2array = require( '@stdlib/ndarray/to-array' ); var removeSingletonDimensions = require( './../lib' ); -// Create a 5-dimensional array: -var x = array( [ [ 1, 2 ], [ 3, 4 ] ], { - 'ndmin': 5 -}); -// returns +var x = uniform( [ 1, 1, 3, 3, 3 ], -10.0, 10.0 ); +console.log( ndarray2array( x ) ); -// Remove singleton dimensions: -var y = removeSingletonDimensions( x ); -// returns - -// Retrieve the shape: -var sh = y.shape; -// returns [ 2, 2 ] - -// Retrieve the number of elements: -var N = numel( sh ); - -// Loop through the array elements... -var i; -for ( i = 0; i < N; i++ ) { - console.log( 'Y[%s] = %d', ind2sub( sh, i ).join( ', ' ), y.iget( i ) ); -} +var y = removeSingletonDimensions( x, false ); +console.log( ndarray2array( y ) ); diff --git a/lib/node_modules/@stdlib/ndarray/base/remove-singleton-dimensions/lib/index.js b/lib/node_modules/@stdlib/ndarray/base/remove-singleton-dimensions/lib/index.js index 31b5ba30a4a0..4010399b87d3 100644 --- a/lib/node_modules/@stdlib/ndarray/base/remove-singleton-dimensions/lib/index.js +++ b/lib/node_modules/@stdlib/ndarray/base/remove-singleton-dimensions/lib/index.js @@ -30,28 +30,10 @@ * var x = array( [ [ 1, 2 ], [ 3, 4 ] ], { * 'ndmin': 5 * }); -* // returns +* // returns [ [ [ [ [ 1, 2 ], [ 3, 4 ] ] ] ] ] * -* var shx = x.shape; -* // returns [ 1, 1, 1, 2, 2 ] -* -* var y = removeSingletonDimensions( x ); -* // returns -* -* var shy = y.shape; -* // returns [ 2, 2 ] -* -* var v = y.get( 0, 0 ); -* // returns 1 -* -* v = y.get( 0, 1 ); -* // returns 2 -* -* v = y.get( 1, 0 ); -* // returns 3 -* -* v = y.get( 1, 1 ); -* // returns 4 +* var y = removeSingletonDimensions( x, false ); +* // returns [ [ 1, 2 ], [ 3, 4 ] ] */ // MODULES // diff --git a/lib/node_modules/@stdlib/ndarray/base/remove-singleton-dimensions/lib/main.js b/lib/node_modules/@stdlib/ndarray/base/remove-singleton-dimensions/lib/main.js index e3ab756fd771..5bfb5fd3752f 100644 --- a/lib/node_modules/@stdlib/ndarray/base/remove-singleton-dimensions/lib/main.js +++ b/lib/node_modules/@stdlib/ndarray/base/remove-singleton-dimensions/lib/main.js @@ -20,7 +20,6 @@ // MODULES // -var isReadOnly = require( '@stdlib/ndarray/base/assert/is-read-only' ); var getDType = require( '@stdlib/ndarray/base/dtype' ); var getShape = require( '@stdlib/ndarray/base/shape' ); var getStrides = require( '@stdlib/ndarray/base/strides' ); @@ -36,10 +35,10 @@ var getData = require( '@stdlib/ndarray/base/data-buffer' ); * * ## Notes * -* - If a provided ndarray does not have any singleton dimensions, the function returns the provided ndarray unchanged. -* - If a provided ndarray does have singleton dimensions, the function returns a new ndarray view. +* - The function always returns a new view of the input ndarray. * * @param {ndarray} x - input array +* @param {boolean} writable - boolean indicating whether a returned array should be writable * @returns {ndarray} squeezed array * * @example @@ -48,30 +47,12 @@ var getData = require( '@stdlib/ndarray/base/data-buffer' ); * var x = array( [ [ 1, 2 ], [ 3, 4 ] ], { * 'ndmin': 5 * }); -* // returns +* // returns [ [ [ [ [ 1, 2 ], [ 3, 4 ] ] ] ] ] * -* var shx = x.shape; -* // returns [ 1, 1, 1, 2, 2 ] -* -* var y = removeSingletonDimensions( x ); -* // returns -* -* var shy = y.shape; -* // returns [ 2, 2 ] -* -* var v = y.get( 0, 0 ); -* // returns 1 -* -* v = y.get( 0, 1 ); -* // returns 2 -* -* v = y.get( 1, 0 ); -* // returns 3 -* -* v = y.get( 1, 1 ); -* // returns 4 +* var y = removeSingletonDimensions( x, false ); +* // returns [ [ 1, 2 ], [ 3, 4 ] ] */ -function removeSingletonDimensions( x ) { +function removeSingletonDimensions( x, writable ) { var strides; var shape; var sh; @@ -95,15 +76,12 @@ function removeSingletonDimensions( x ) { } if ( shape.length === N ) { // We did not find any singleton dimensions... - return x; - } - if ( isReadOnly( x ) ) { - // If provided a read-only view, the returned array should also be read-only... - return new x.constructor( getDType( x ), getData( x ), shape, strides, getOffset( x ), getOrder( x ), { // eslint-disable-line max-len - 'readonly': true - }); + shape = sh; + strides = st; } - return new x.constructor( getDType( x ), getData( x ), shape, strides, getOffset( x ), getOrder( x ) ); // eslint-disable-line max-len + return new x.constructor( getDType( x ), getData( x ), shape, strides, getOffset( x ), getOrder( x ), { // eslint-disable-line max-len + 'readonly': !writable + }); } diff --git a/lib/node_modules/@stdlib/ndarray/base/remove-singleton-dimensions/test/test.js b/lib/node_modules/@stdlib/ndarray/base/remove-singleton-dimensions/test/test.js index c4694217f175..9db2198c2109 100644 --- a/lib/node_modules/@stdlib/ndarray/base/remove-singleton-dimensions/test/test.js +++ b/lib/node_modules/@stdlib/ndarray/base/remove-singleton-dimensions/test/test.js @@ -22,6 +22,8 @@ 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 isReadOnly = require( '@stdlib/ndarray/base/assert/is-read-only' ); var removeSingletonDimensions = require( './../lib' ); @@ -35,66 +37,67 @@ tape( 'main export is a function', function test( t ) { t.end(); }); -tape( 'if a provided array does not contain singleton dimensions, the function returns the provided array unchanged', function test( t ) { - var sh; +tape( 'if a provided array does not contain singleton dimensions, the function returns a new view of the input array', function test( t ) { var x; var y; x = array( [ [ 1, 2 ], [ 3, 4 ] ] ); - sh = x.shape; - y = removeSingletonDimensions( x ); + y = removeSingletonDimensions( x, false ); - t.strictEqual( y, x, 'returns expected value' ); - t.deepEqual( y.shape, sh, 'returns expected value' ); + t.notEqual( y, x, 'returns expected value' ); + t.deepEqual( getShape( y ), getShape( x ), 'returns expected value' ); + t.strictEqual( isReadOnly( y ), true, 'returns expected value' ); + + x = array( [ [ 1, 2 ], [ 3, 4 ] ] ); + + y = removeSingletonDimensions( x, true ); + + t.notEqual( y, x, 'returns expected value' ); + t.deepEqual( getShape( y ), getShape( x ), 'returns expected value' ); + t.strictEqual( isReadOnly( y ), false, 'returns expected value' ); t.end(); }); -tape( 'if a provided array does not contain singleton dimensions, the function returns the provided array unchanged (base)', function test( t ) { - var sh; +tape( 'if a provided array does not contain singleton dimensions, the function returns a new view of 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' ); - sh = x.shape; - y = removeSingletonDimensions( x ); + y = removeSingletonDimensions( x, false ); - t.strictEqual( y, x, 'returns expected value' ); - t.deepEqual( y.shape, sh, 'returns expected value' ); + t.notEqual( y, x, 'returns expected value' ); + t.deepEqual( getShape( y ), getShape( x ), 'returns expected value' ); t.end(); }); -tape( 'if a provided array does not contain singleton dimensions, the function returns the provided array unchanged (0D)', function test( t ) { - var sh; +tape( 'if a provided array does not contain singleton dimensions, the function returns a new view of the input array (base, 0D)', function test( t ) { var x; var y; x = ndarray( 'generic', [ 1 ], [], [ 0 ], 0, 'row-major' ); - sh = x.shape; - y = removeSingletonDimensions( x ); + y = removeSingletonDimensions( x, false ); - t.strictEqual( y, x, 'returns expected value' ); - t.deepEqual( y.shape, sh, 'returns expected value' ); + t.notEqual( y, x, 'returns expected value' ); + t.deepEqual( getShape( y ), getShape( x ), 'returns expected value' ); t.end(); }); -tape( 'if a provided array does not contain singleton dimensions, the function returns the provided array unchanged (empty)', function test( t ) { - var sh; +tape( 'if a provided array does not contain singleton dimensions, the function returns a new view of 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' ); - sh = x.shape; - y = removeSingletonDimensions( x ); + y = removeSingletonDimensions( x, false ); - t.strictEqual( y, x, 'returns expected value' ); - t.deepEqual( y.shape, sh, 'returns expected value' ); + t.notEqual( y, x, 'returns expected value' ); + t.deepEqual( getShape( y ), getShape( x ), 'returns expected value' ); t.end(); }); @@ -107,95 +110,58 @@ tape( 'the function removes singleton dimensions (leading)', function test( t ) 'ndmin': 5 }); - y = removeSingletonDimensions( x ); + y = removeSingletonDimensions( x, false ); t.notEqual( y, x, 'returns expected value' ); - t.deepEqual( y.shape, [ 2, 2 ], 'returns expected value' ); - t.strictEqual( y.data, x.data, 'returns expected value' ); - - t.end(); -}); - -tape( 'the function removes singleton dimensions (trailing)', function test( t ) { - var x; - var y; - - x = array( [ 1, 2, 3, 4 ], { - 'shape': [ 2, 1, 2, 1, 1, 1 ] - }); - - y = removeSingletonDimensions( x ); - - t.notEqual( y, x, 'returns expected value' ); - t.deepEqual( y.shape, [ 2, 2 ], 'returns expected value' ); - t.strictEqual( y.data, x.data, 'returns expected value' ); - - t.end(); -}); - -tape( 'the function removes singleton dimensions (base)', function test( t ) { - var x; - var y; + t.deepEqual( getShape( y ), [ 2, 2 ], 'returns expected value' ); + t.strictEqual( getData( y ), getData( x ), 'returns expected value' ); + t.strictEqual( isReadOnly( y ), true, 'returns expected value' ); - x = ndarray( 'generic', [ 1, 2, 3, 4 ], [ 1, 1, 2, 1, 2 ], [ 4, 4, 2, 2, 1 ], 0, 'row-major' ); - y = removeSingletonDimensions( x ); + y = removeSingletonDimensions( x, true ); t.notEqual( y, x, 'returns expected value' ); - t.deepEqual( y.shape, [ 2, 2 ], 'returns expected value' ); - t.strictEqual( y.data, x.data, 'returns expected value' ); + t.deepEqual( getShape( y ), [ 2, 2 ], 'returns expected value' ); + t.strictEqual( getData( y ), getData( x ), 'returns expected value' ); + t.strictEqual( isReadOnly( y ), false, 'returns expected value' ); t.end(); }); -tape( 'if provided a read-only array, the function returns a read-only array', function test( t ) { +tape( 'the function removes singleton dimensions (trailing)', function test( t ) { var x; var y; x = array( [ 1, 2, 3, 4 ], { - 'shape': [ 2, 1, 2, 1, 1, 1 ], - 'readonly': true + 'shape': [ 2, 1, 2, 1, 1, 1 ] }); - y = removeSingletonDimensions( x ); + y = removeSingletonDimensions( x, false ); t.notEqual( y, x, 'returns expected value' ); - t.deepEqual( y.shape, [ 2, 2 ], 'returns expected value' ); - t.strictEqual( y.data, x.data, 'returns expected value' ); + t.deepEqual( getShape( y ), [ 2, 2 ], 'returns expected value' ); + t.strictEqual( getData( y ), getData( x ), 'returns expected value' ); t.strictEqual( isReadOnly( y ), true, 'returns expected value' ); - t.end(); -}); - -tape( 'if provided a writable array, the function returns a writable array', function test( t ) { - var x; - var y; - - x = array( [ 1, 2, 3, 4 ], { - 'shape': [ 2, 1, 2, 1, 1, 1 ], - 'readonly': false - }); - - y = removeSingletonDimensions( x ); + y = removeSingletonDimensions( x, true ); t.notEqual( y, x, 'returns expected value' ); - t.deepEqual( y.shape, [ 2, 2 ], 'returns expected value' ); - t.strictEqual( y.data, x.data, 'returns expected value' ); + t.deepEqual( getShape( y ), [ 2, 2 ], 'returns expected value' ); + t.strictEqual( getData( y ), getData( x ), 'returns expected value' ); t.strictEqual( isReadOnly( y ), false, 'returns expected value' ); t.end(); }); -tape( 'if provided a writable array, the function returns a writable array (base)', function test( t ) { +tape( 'the function removes singleton dimensions (base)', function test( t ) { var x; var y; - x = ndarray( 'generic', [ 1, 2, 3, 4 ], [ 2, 1, 2 ], [ 2, 2, 1 ], 0, 'row-major' ); - y = removeSingletonDimensions( x ); + x = ndarray( 'generic', [ 1, 2, 3, 4 ], [ 1, 1, 2, 1, 2 ], [ 4, 4, 2, 2, 1 ], 0, 'row-major' ); + y = removeSingletonDimensions( x, false ); t.notEqual( y, x, 'returns expected value' ); t.deepEqual( y.shape, [ 2, 2 ], 'returns expected value' ); - t.strictEqual( y.data, x.data, 'returns expected value' ); - t.strictEqual( isReadOnly( y ), false, 'returns expected value' ); + t.strictEqual( getData( y ), getData( x ), 'returns expected value' ); t.end(); });