diff --git a/lib/node_modules/@stdlib/ndarray/splice/README.md b/lib/node_modules/@stdlib/ndarray/splice/README.md new file mode 100644 index 000000000000..c9c951a21010 --- /dev/null +++ b/lib/node_modules/@stdlib/ndarray/splice/README.md @@ -0,0 +1,264 @@ + + +# splice + +> Return an [`ndarray`][@stdlib/ndarray/ctor] where elements of an input [`ndarray`][@stdlib/ndarray/ctor] are replaced or removed along a specific dimension. + + + +
+ +
+ + + + + +
+ +## Usage + +```javascript +var splice = require( '@stdlib/ndarray/splice' ); +``` + +#### splice( x, slices\[, values\[, options]] ) + +Returns an [`ndarray`][@stdlib/ndarray/ctor] where elements of an input [`ndarray`][@stdlib/ndarray/ctor] are replaced or removed along a specific dimension. + +```javascript +var Slice = require( '@stdlib/slice/ctor' ); +var array = require( '@stdlib/ndarray/array' ); + +// Define input ndarray: +var x = array( [ [ 1.0, 2.0, 3.0 ], [ 4.0, 5.0, 6.0 ], [ 7.0, 8.0, 9.0 ] ] ); +// returns [ [ 1.0, 2.0, 3.0 ], [ 4.0, 5.0, 6.0 ], [ 7.0, 8.0, 9.0 ] ] + +// Define values ndarray: +var y = array( [ [ 20.0 ], [ 50.0 ], [ 80.0 ] ] ); +// returns [ [ 20.0 ], [ 50.0 ], [ 80.0 ] ] + +// Remove slice region from the input ndarray: +var o = splice( x, new Slice( 1, 2, 1 ) ); +// returns [ [ 1.0, 3.0 ], [ 4.0, 6.0 ], [ 7.0, 9.0 ] ] + +// Insert elements into the slice region of an input ndarray: +var o1 = splice( x, new Slice( 1, 2, 1 ), y ); +// returns [ [ 1.0, 20.0, 3.0 ], [ 4.0, 50.0, 6.0 ], [ 7.0, 80.0, 9.0 ] ] + +// Remove a slice region and insert elements into another slice region of an input ndarray: +var o2 = splice( x, new Slice( 2, null, 1 ), new Slice( 0, 1, 1 ), y ); +// returns [ [ 2.0, 20.0 ], [ 5.0, 50.0 ], [ 8.0, 80.0 ] ] +``` + +The function accepts the following arguments: + +- **x**: input [`ndarray`][@stdlib/ndarray/ctor]. +- **slices**: a [`Slice`][@stdlib/slice/ctor] instance, `null`, `undefined`, an integer, or an array of such values. If provided `null` or `undefined`, the argument is equivalent to `new Slice()` (i.e., the returned view should include all elements along a specified dimension). If provided a negative integer, the index at which to splice is determined by counting backward from the last element along that dimension (where `-1` refers to the last element). +- **values**: an [`ndarray`][@stdlib/ndarray/ctor] or an array of [`ndarrays`][@stdlib/ndarray/ctor] containing the elements to insert. The provided [`ndarrays`][@stdlib/ndarray/ctor] must be [broadcast compatible][@stdlib/ndarray/base/broadcast-shapes] with the shape of the input [`ndarray`][@stdlib/ndarray/ctor] except for the dimension along which to insert the values (_optional_). +- **options**: function options (_optional_). + +The function supports the following means for providing slice and values arguments: + +1. providing [`Slice`][@stdlib/slice/ctor] instances as separate arguments. +2. providing [`Slice`][@stdlib/slice/ctor] instances as an array of such values. +3. providing slice region integer indices as separate arguments. +4. providing slice region integer indices as an array of such values. +5. providing values [`ndarrays`][@stdlib/ndarray/ctor] as separate arguments. +6. providing values [`ndarrays`][@stdlib/ndarray/ctor] as an array of such values. + +The following example demonstrates each invocation style returning equivalent results. + +```javascript +var Slice = require( '@stdlib/slice/ctor' ); +var array = require( '@stdlib/ndarray/array' ); + +// Define input ndarray: +var x = array( [ [ 1.0, 2.0, 3.0 ], [ 4.0, 5.0, 6.0 ], [ 7.0, 8.0, 9.0 ] ] ); +// returns [ [ 1.0, 2.0, 3.0 ], [ 4.0, 5.0, 6.0 ], [ 7.0, 8.0, 9.0 ] ] + +// Define values ndarray: +var y = array( [ [ 20.0 ], [ 50.0 ], [ 80.0 ] ] ); +// returns [ [ 20.0 ], [ 50.0 ], [ 80.0 ] ] + +// 1. Slice instances and values as separate arguments: +var o = splice( x, new Slice( 0, 1, 1 ), new Slice( 2, null, 1 ) ); +// returns [ [ 2.0 ], [ 5.0 ], [ 8.0 ] ] + +// 2. Slice instances as an array of such values: +o = splice( x, [ new Slice( 0, 1, 1 ), new Slice( 2, null, 1 ) ] ); +// returns [ [ 2.0 ], [ 5.0 ], [ 8.0 ] ] + +// 3. Slice region integer indices as separate arguments: +o = splice( x, 0, 2 ); +// returns [ [ 2.0 ], [ 5.0 ], [ 8.0 ] ] + +// 4. Slice region integer indices as an array of such values: +o = splice( x, [ 0, 2 ] ); +// returns [ [ 2.0 ], [ 5.0 ], [ 8.0 ] ] + +// 5. Values ndarrays as separate arguments: +o = splice( x, new Slice( 0, 1, 1 ), y ); +// returns [ [ 20.0, 2.0, 3.0 ], [ 50.0, 5.0, 6.0 ], [ 80.0, 8.0, 9.0 ] ] + +// 6. Values ndarrays as an array of such values: +o = splice( x, new Slice( 0, 1, 1 ), new Slice( 1, 2, 1 ), [ y, y ] ); +// returns [ [ 20.0, 20.0, 3.0 ], [ 50.0, 50.0, 6.0 ], [ 80.0, 80.0, 9.0 ] ] +``` + +The function accepts the following `options`: + +- **dim**: dimension along which to perform the operation. If provided a negative integer, the dimension along which to perform the operation is determined by counting backward from the last dimension (where `-1` refers to the last dimension). Default: `-1`. + +By default, the function performs the operation on the last dimension. To perform the operation on any other dimension, specify the `dim` option. + +```javascript +var Slice = require( '@stdlib/slice/ctor' ); +var array = require( '@stdlib/ndarray/array' ); + +// Define input ndarray: +var x = array( [ [ 1.0, 2.0, 3.0 ], [ 4.0, 5.0, 6.0 ], [ 7.0, 8.0, 9.0 ] ] ); +// returns [ [ 1.0, 2.0, 3.0 ], [ 4.0, 5.0, 6.0 ], [ 7.0, 8.0, 9.0 ] ] + +// Define values ndarray: +var y = array( [ [ 20.0, 50.0, 80.0 ] ] ); +// returns [ [ 20.0, 50.0, 80.0 ] ] + +// Remove slice region from the input ndarray: +var o = splice( x, new Slice( 1, 2, 1 ), { + 'dim': -2 +}); +// returns [ [ 1.0, 2.0, 3.0 ], [ 7.0, 8.0, 9.0 ] ] + +// Insert elements into the slice region of an input ndarray: +var o1 = splice( x, new Slice( 1, 2, 1 ), y, { + 'dim': -2 +}); +// returns [ [ 1.0, 2.0, 3.0 ], [ 20.0, 50.0, 80.0 ], [ 7.0, 8.0, 9.0 ] ] + +// Remove a slice region and insert elements into another slice region of an input ndarray: +var o2 = splice( x, new Slice( 2, null, 1 ), new Slice( 0, 1, 1 ), [ y ], { + 'dim': -2 +}); +// returns [ [ 4.0, 5.0, 6.0 ], [ 20.0, 50.0, 80.0 ] ] +``` + +
+ + + + + +
+ +## Notes + +- If the function is provided more than one slice region as separate argument then the values argument must be provided as an array of [ndarrays][@stdlib/ndarray/ctor]. +- For splice operations which only remove slice regions from an input [ndarray][@stdlib/ndarray/ctor], the data type and [memory layout][@stdlib/ndarray/orders] of the output [ndarray][@stdlib/ndarray/ctor] is same as the input [ndarray][@stdlib/ndarray/ctor]. +- For splice operations which insert new elements into an input [ndarray][@stdlib/ndarray/ctor], the data type of the output [ndarray][@stdlib/ndarray/ctor] is determined by applying [type promotion rules][@stdlib/ndarray/promotion-rules] to the input [ndarray][@stdlib/ndarray/ctor] and the list of values. If provided [ndarrays][@stdlib/ndarray/ctor] having different [memory layouts][@stdlib/ndarray/orders], the output [ndarray][@stdlib/ndarray/ctor] has the [default order][@stdlib/ndarray/defaults]. +- For splice operations which insert new elements into an input [ndarray][@stdlib/ndarray/ctor], the list of values [ndarrays][@stdlib/ndarray/ctor] must have the same or fewer number of dimensions than the input [ndarray][@stdlib/ndarray/ctor]. + +
+ + + + + +
+ +## Examples + +```javascript +var uniform = require( '@stdlib/random/uniform' ); +var ndarray2array = require( '@stdlib/ndarray/to-array' ); +var Slice = require( '@stdlib/slice/ctor' ); +var splice = require( '@stdlib/ndarray/splice' ); + +// Define input ndarray: +var x = uniform( [ 3, 3, 3 ], -10, 10 ); +console.log( 'Input ndarray: ', ndarray2array( x ) ); + +// Define values ndarray: +var y = uniform( [ 1, 3, 3 ], 20, 40 ); +console.log( 'Values: ', ndarray2array( y ) ); + +// Remove slice region from the input ndarray: +var o = splice( x, new Slice( 1, 2, 1 ), { + 'dim': -3 +}); +console.log( ndarray2array( o ) ); + +// Insert elements into the slice region of an input ndarray: +var o1 = splice( x, new Slice( 1, 2, 1 ), y, { + 'dim': -3 +}); +console.log( ndarray2array( o1 ) ); + +// Remove a slice region and insert elements into another slice region of an input ndarray: +var o2 = splice( x, new Slice( 2, null, 1 ), new Slice( 0, 1, 1 ), [ y ], { + 'dim': -3 +}); +console.log( ndarray2array( o2 ) ); +``` + +
+ + + + + +
+ +
+ + + + + + + + + + + + + + diff --git a/lib/node_modules/@stdlib/ndarray/splice/benchmark/benchmark.2d.size.js b/lib/node_modules/@stdlib/ndarray/splice/benchmark/benchmark.2d.size.js new file mode 100644 index 000000000000..e2683838ca22 --- /dev/null +++ b/lib/node_modules/@stdlib/ndarray/splice/benchmark/benchmark.2d.size.js @@ -0,0 +1,131 @@ +/** +* @license Apache-2.0 +* +* Copyright (c) 2026 The Stdlib Authors. +* +* Licensed under the Apache License, Version 2.0 (the "License"); +* you may not use this file except in compliance with the License. +* You may obtain a copy of the License at +* +* http://www.apache.org/licenses/LICENSE-2.0 +* +* Unless required by applicable law or agreed to in writing, software +* distributed under the License is distributed on an "AS IS" BASIS, +* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. +* See the License for the specific language governing permissions and +* limitations under the License. +*/ + +'use strict'; + +// MODULES // + +var bench = require( '@stdlib/bench' ); +var isndarrayLike = require( '@stdlib/assert/is-ndarray-like' ); +var pow = require( '@stdlib/math/base/special/pow' ); +var floor = require( '@stdlib/math/base/special/floor' ); +var uniform = require( '@stdlib/random/uniform' ); +var format = require( '@stdlib/string/format' ); +var pkg = require( './../package.json' ).name; +var splice = require( './../lib' ); + + +// VARIABLES // + +var types = [ 'float64' ]; +var orders = [ 'row-major', 'column-major' ]; + + +// FUNCTIONS // + +/** +* Creates a benchmark function. +* +* @private +* @param {Array} shape - ndarray shape +* @param {string} type - ndarray data type +* @param {string} order - memory layout +* @returns {Function} benchmark function +*/ +function createBenchmark( shape, type, order ) { + var arrays; + var opts; + var x; + + opts = { + 'dtype': type, + 'order': order + }; + arrays = [ + uniform( [ shape[ 1 ], 1 ], 0.0, 100.0, opts ), + uniform( [ shape[ 1 ], 1 ], 0.0, 100.0, opts ), + uniform( [ shape[ 1 ], 1 ], 0.0, 100.0, opts ) + ]; + x = uniform( shape, 0.0, 100.0, opts ); + + return benchmark; + + /** + * Benchmark function. + * + * @private + * @param {Benchmark} b - benchmark instance + */ + function benchmark( b ) { + var out; + var i; + + b.tic(); + for ( i = 0; i < b.iterations; i++ ) { + out = splice( x, [ 1, 2, 6 ], [ arrays[ i%3 ], arrays[ (i+1)%3 ], arrays[ (i+2)%3 ] ] ); // eslint-disable-line max-len + if ( typeof out !== 'object' ) { + b.fail( 'should return an ndarray' ); + } + } + b.toc(); + if ( !isndarrayLike( out ) ) { + b.fail( 'should return an ndarray' ); + } + b.pass( 'benchmark finished' ); + b.end(); + } +} + + +// MAIN // + +/** +* Main execution sequence. +* +* @private +*/ +function main() { + var min; + var max; + var ord; + var sh; + var t1; + var N; + var f; + var i; + var j; + var k; + + min = 2; // 10^min + max = 6; // 10^max + + for ( k = 0; k < orders.length; k++ ) { + ord = orders[ k ]; + for ( j = 0; j < types.length; j++ ) { + t1 = types[ j ]; + for ( i = min; i <= max; i++ ) { + N = floor( pow( pow( 10, i ), 1.0/2.0 ) ); + sh = [ N, N ]; + f = createBenchmark( sh, t1, ord ); + bench( format( '%s::equidimensional:size=%d,dim=-1,order=%s,num_slices=3,removed=3,inserted=3', pkg, N*N, ord ), f ); + } + } + } +} + +main(); diff --git a/lib/node_modules/@stdlib/ndarray/splice/benchmark/benchmark.js b/lib/node_modules/@stdlib/ndarray/splice/benchmark/benchmark.js new file mode 100644 index 000000000000..45957793b627 --- /dev/null +++ b/lib/node_modules/@stdlib/ndarray/splice/benchmark/benchmark.js @@ -0,0 +1,192 @@ +/** +* @license Apache-2.0 +* +* Copyright (c) 2026 The Stdlib Authors. +* +* Licensed under the Apache License, Version 2.0 (the "License"); +* you may not use this file except in compliance with the License. +* You may obtain a copy of the License at +* +* http://www.apache.org/licenses/LICENSE-2.0 +* +* Unless required by applicable law or agreed to in writing, software +* distributed under the License is distributed on an "AS IS" BASIS, +* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. +* See the License for the specific language governing permissions and +* limitations under the License. +*/ + +'use strict'; + +// MODULES // + +var bench = require( '@stdlib/bench' ); +var isndarrayLike = require( '@stdlib/assert/is-ndarray-like' ); +var zeros = require( '@stdlib/ndarray/zeros' ); +var ones = require( '@stdlib/ndarray/ones' ); +var format = require( '@stdlib/string/format' ); +var pkg = require( './../package.json' ).name; +var splice = require( './../lib' ); + + +// MAIN // + +bench( format( '%s::1d,casting', pkg ), function benchmark( b ) { + var values; + var x; + var v; + var i; + + /* eslint-disable object-curly-newline, stdlib/line-closing-bracket-spacing */ + + values = [ + zeros( [ 1 ], { 'dtype': 'float32' } ), + zeros( [ 1 ], { 'dtype': 'int32' } ), + zeros( [ 1 ], { 'dtype': 'generic' } ) + ]; + x = ones( [ 96 ], { 'dtype': 'float64' } ); + + /* eslint-enable object-curly-newline, stdlib/line-closing-bracket-spacing */ + + b.tic(); + for ( i = 0; i < b.iterations; i++ ) { + v = splice( x, [ 0, 2, 4 ], values ); + if ( typeof v !== 'object' ) { + b.fail( 'should return an ndarray' ); + } + } + b.toc(); + if ( !isndarrayLike( v ) ) { + b.fail( 'should return an ndarray' ); + } + b.pass( 'benchmark finished' ); + b.end(); +}); + +bench( format( '%s::2d,casting', pkg ), function benchmark( b ) { + var values; + var x; + var v; + var i; + + /* eslint-disable object-curly-newline, stdlib/line-closing-bracket-spacing */ + + values = [ + zeros( [ 16, 1 ], { 'dtype': 'float32' } ), + zeros( [ 16, 1 ], { 'dtype': 'int32' } ), + zeros( [ 16, 1 ], { 'dtype': 'generic' } ) + ]; + x = ones( [ 16, 6 ], { 'dtype': 'float64' } ); + + /* eslint-enable object-curly-newline, stdlib/line-closing-bracket-spacing */ + + b.tic(); + for ( i = 0; i < b.iterations; i++ ) { + v = splice( x, [ 0, 2, 4 ], values ); + if ( typeof v !== 'object' ) { + b.fail( 'should return an ndarray' ); + } + } + b.toc(); + if ( !isndarrayLike( v ) ) { + b.fail( 'should return an ndarray' ); + } + b.pass( 'benchmark finished' ); + b.end(); +}); + +bench( format( '%s::3d,casting', pkg ), function benchmark( b ) { + var values; + var x; + var v; + var i; + + /* eslint-disable object-curly-newline, stdlib/line-closing-bracket-spacing */ + + values = [ + zeros( [ 4, 4, 1 ], { 'dtype': 'float32' } ), + zeros( [ 4, 4, 1 ], { 'dtype': 'int32' } ), + zeros( [ 4, 4, 1 ], { 'dtype': 'generic' } ) + ]; + x = ones( [ 4, 4, 6 ], { 'dtype': 'float64' } ); + + /* eslint-enable object-curly-newline, stdlib/line-closing-bracket-spacing */ + + b.tic(); + for ( i = 0; i < b.iterations; i++ ) { + v = splice( x, [ 0, 2, 4 ], values ); + if ( typeof v !== 'object' ) { + b.fail( 'should return an ndarray' ); + } + } + b.toc(); + if ( !isndarrayLike( v ) ) { + b.fail( 'should return an ndarray' ); + } + b.pass( 'benchmark finished' ); + b.end(); +}); + +bench( format( '%s::4d,casting', pkg ), function benchmark( b ) { + var values; + var x; + var v; + var i; + + /* eslint-disable object-curly-newline, stdlib/line-closing-bracket-spacing */ + + values = [ + zeros( [ 2, 2, 4, 1 ], { 'dtype': 'float32' } ), + zeros( [ 2, 2, 4, 1 ], { 'dtype': 'int32' } ), + zeros( [ 2, 2, 4, 1 ], { 'dtype': 'generic' } ) + ]; + x = ones( [ 2, 2, 4, 6 ], { 'dtype': 'float64' } ); + + /* eslint-enable object-curly-newline, stdlib/line-closing-bracket-spacing */ + + b.tic(); + for ( i = 0; i < b.iterations; i++ ) { + v = splice( x, [ 0, 2, 4 ], values ); + if ( typeof v !== 'object' ) { + b.fail( 'should return an ndarray' ); + } + } + b.toc(); + if ( !isndarrayLike( v ) ) { + b.fail( 'should return an ndarray' ); + } + b.pass( 'benchmark finished' ); + b.end(); +}); + +bench( format( '%s::5d,casting', pkg ), function benchmark( b ) { + var values; + var x; + var v; + var i; + + /* eslint-disable object-curly-newline, stdlib/line-closing-bracket-spacing */ + + values = [ + zeros( [ 2, 2, 2, 2, 1 ], { 'dtype': 'float32' } ), + zeros( [ 2, 2, 2, 2, 1 ], { 'dtype': 'int32' } ), + zeros( [ 2, 2, 2, 2, 1 ], { 'dtype': 'generic' } ) + ]; + x = ones( [ 2, 2, 2, 2, 6 ], { 'dtype': 'float64' } ); + + /* eslint-enable object-curly-newline, stdlib/line-closing-bracket-spacing */ + + b.tic(); + for ( i = 0; i < b.iterations; i++ ) { + v = splice( x, [ 0, 2, 4 ], values ); + if ( typeof v !== 'object' ) { + b.fail( 'should return an ndarray' ); + } + } + b.toc(); + if ( !isndarrayLike( v ) ) { + b.fail( 'should return an ndarray' ); + } + b.pass( 'benchmark finished' ); + b.end(); +}); diff --git a/lib/node_modules/@stdlib/ndarray/splice/docs/repl.txt b/lib/node_modules/@stdlib/ndarray/splice/docs/repl.txt new file mode 100644 index 000000000000..545c5ff8d8f9 --- /dev/null +++ b/lib/node_modules/@stdlib/ndarray/splice/docs/repl.txt @@ -0,0 +1,62 @@ + +{{alias}}( x, slices[, values[, options]] ) + Returns an ndarray where the elements of an input ndarray are replaced or + removed along a specific dimension. + + For splice operations which only remove slice regions from an input ndarray + the data type and memory layout of the output ndarray is same as the input + ndarray. + + For splice operations which insert new elements into an input ndarray + the data type of the output ndarray is determined by applying type promotion + rules to the input ndarray and the list of values. If provided ndarrays + having different memory layouts, the output ndarray has the default order. + + For splice operations which insert new elements into an input ndarray + the list of values ndarrays must have the same or fewer number of dimensions + than the input ndarray. + + Parameters + ---------- + x: ndarray + Input array. + + slices: Slice|integer|null|undefined|ArrayLikeObject + Slice object, `null`, `undefined`, an integer or an array of such + values. If provided `null` or `undefined`, the returned view includes + all elements along a specified dimension. If provided a negative integer + the index at which to splice is determined by counting backwards from + the last element along that dimension (where `-1` refers to the last + element). + + values: ndarray|ArrayLikeObject (optional) + An ndarray or an array of ndarrays containing the elements to insert. + The provided ndarrays must be broadcast-compatible with the shape of the + input ndarray except for the dimension along which to insert the values. + + options: Object (optional) + Options. + + options.dim: integer (optional) + Dimension along which to perform the operation. If provided a negative + integer the dimension along which to perform the operation is determined + by counting backward from the last dimension (where `-1` refers to the + last dimension). Default: -1. + + Returns + ------- + out: ndarray + Output array. + + Examples + -------- + > var x = {{alias:@stdlib/ndarray/array}}( [ [ 1.0, 2.0 ], [ 3.0, 4.0 ] ] ) + [ [ 1.0, 2.0 ], [ 3.0, 4.0 ] ] + > var y = {{alias:@stdlib/ndarray/array}}( [ [ 5.0 ], [ 6.0 ] ] ) + [ [ 5.0 ], [ 6.0 ] ] + > var s = new {{alias:@stdlib/slice/ctor}}( 1, 2, 1 ); + > var out = {{alias}}( x, s, y ) + [ [ 1.0, 5.0 ], [ 3.0, 6.0 ] ] + + See Also + -------- diff --git a/lib/node_modules/@stdlib/ndarray/splice/docs/types/index.d.ts b/lib/node_modules/@stdlib/ndarray/splice/docs/types/index.d.ts new file mode 100644 index 000000000000..9e169c657791 --- /dev/null +++ b/lib/node_modules/@stdlib/ndarray/splice/docs/types/index.d.ts @@ -0,0 +1,181 @@ +/* +* @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'; +import { Slice } from '@stdlib/types/slice'; + +/** +* Interface defining function options. +*/ +interface Options { + /** + * Dimension along which to perform the operation (default: -1). + */ + dim?: number; +} + +/** +* Slice argument. +*/ +type SliceArgument = Slice | number | null | undefined; + +/** +* Returns an ndarray where the elements of an input ndarray are replaced or removed along a specific dimension. +* +* @param x - input array +* @param s - slice arguments +* @param values - an ndarray or an array of ndarrays containing the elements to insert +* @param options - options +* @param options.dim - dimension along which to perform the operation +* @returns output ndarray +* +* @example +* var Slice = require( '@stdlib/slice/ctor' ); +* var array = require( '@stdlib/ndarray/array' ); +* +* var x = array( [ [ 1.0, 2.0 ], [ 3.0, 4.0 ], [ 5.0, 6.0 ] ] ); +* // returns [ [ 1.0, 2.0 ], [ 3.0, 4.0 ], [ 5.0, 6.0 ] ] +* +* var y = array( [ [ 10.0, 20.0 ] ] ); +* // returns [ [ 10.0, 20.0 ] ] +* +* var s = new Slice( 1, 2, 1 ); +* // returns +* +* var out = splice( x, s, y, { +* 'dim': -2 +* }); +* // returns [ [ 1.0, 2.0 ], [ 10.0, 20.0 ], [ 5.0, 6.0 ] ] +*/ +declare function splice( x: T, s: Array, values?: T, options?: Options ): T; + +/** +* Returns an ndarray where the elements of an input ndarray are replaced or removed along a specific dimension. +* +* @param x - input array +* @param s - slice arguments +* @param options - options +* @param options.dim - dimension along which to perform the operation +* @returns output ndarray +* +* @example +* var Slice = require( '@stdlib/slice/ctor' ); +* var array = require( '@stdlib/ndarray/array' ); +* +* var x = array( [ [ 1.0, 2.0 ], [ 3.0, 4.0 ], [ 5.0, 6.0 ] ] ); +* // returns [ [ 1.0, 2.0 ], [ 3.0, 4.0 ], [ 5.0, 6.0 ] ] +* +* var s = new Slice( 1, 2, 1 ); +* // returns +* +* var out = splice( x, [ s ], { +* 'dim': -2 +* }); +* // returns [ [ 1.0, 2.0 ], [ 5.0, 6.0 ] ] +*/ +declare function splice( x: T, s: Array, options?: Options ): T; + +/** +* Returns an ndarray where the elements of an input ndarray are replaced or removed along a specific dimension. +* +* @param x - input array +* @param args - slice and options arguments +* @returns output ndarray +* +* @example +* var Slice = require( '@stdlib/slice/ctor' ); +* var array = require( '@stdlib/ndarray/array' ); +* +* var x = array( [ [ 1.0, 2.0 ], [ 3.0, 4.0 ], [ 5.0, 6.0 ] ] ); +* // returns [ [ 1.0, 2.0 ], [ 3.0, 4.0 ], [ 5.0, 6.0 ] ] +* +* var s = new Slice( 1, 2, 1 ); +* // returns +* +* var out = splice( x, s, { +* 'dim': -2 +* }); +* // returns [ [ 1.0, 2.0 ], [ 5.0, 6.0 ] ] +*/ +declare function splice( x: T, ...args: Array ): T; + +/** +* Returns an ndarray where the elements of an input ndarray are replaced or removed along a specific dimension. +* +* @param x - input array +* @param s - slice arguments +* @param values - an ndarray or an array of ndarrays containing the elements to insert +* @param options - options +* @param options.dim - dimension along which to perform the operation +* @returns output ndarray +* +* @example +* var Slice = require( '@stdlib/slice/ctor' ); +* var array = require( '@stdlib/ndarray/array' ); +* +* var x = array( [ [ 1.0, 2.0 ], [ 3.0, 4.0 ], [ 5.0, 6.0 ] ] ); +* // returns [ [ 1.0, 2.0 ], [ 3.0, 4.0 ], [ 5.0, 6.0 ] ] +* +* var y = array( [ [ 10.0, 20.0 ] ] ); +* // returns [ [ 10.0, 20.0 ] ] +* +* var s = new Slice( 1, 2, 1 ); +* // returns +* +* var out = splice( x, [ s ], [ y ], { +* 'dim': -2 +* }); +* // returns [ [ 1.0, 2.0 ], [ 10.0, 20.0 ], [ 5.0, 6.0 ] ] +*/ +declare function splice( x: T, s: Array, values: ndarray | Array, options?: Options ): T; // FIXME: this can result in incorrect output type inference when type promotion occurs (e.g., input `float32` and values is a `float64`, the result is `float64`, not `float32`) + +/** +* Returns an ndarray where the elements of an input ndarray are replaced or removed along a specific dimension. +* +* @param x - input array +* @param args - slice, values and options arguments +* @returns output ndarray +* +* @example +* var Slice = require( '@stdlib/slice/ctor' ); +* var array = require( '@stdlib/ndarray/array' ); +* +* var x = array( [ [ 1.0, 2.0 ], [ 3.0, 4.0 ], [ 5.0, 6.0 ] ] ); +* // returns [ [ 1.0, 2.0 ], [ 3.0, 4.0 ], [ 5.0, 6.0 ] ] +* +* var y = array( [ [ 10.0, 20.0 ] ] ); +* // returns [ [ 10.0, 20.0 ] ] +* +* var s = new Slice( 1, 2, 1 ); +* // returns +* +* var out = splice( x, s, y, { +* 'dim': -2 +* }); +* // returns [ [ 1.0, 2.0 ], [ 10.0, 20.0 ], [ 5.0, 6.0 ] ] +*/ +declare function splice( x: T, ...args: Array | ndarray | Options> ): T; // FIXME: this can result in incorrect output type inference when type promotion occurs (e.g., input `float32` and values is a `float64`, the result is `float64`, not `float32`) + + +// EXPORTS // + +export = splice; diff --git a/lib/node_modules/@stdlib/ndarray/splice/docs/types/test.ts b/lib/node_modules/@stdlib/ndarray/splice/docs/types/test.ts new file mode 100644 index 000000000000..e9dc94373d68 --- /dev/null +++ b/lib/node_modules/@stdlib/ndarray/splice/docs/types/test.ts @@ -0,0 +1,207 @@ +/* +* @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. +*/ + +import empty = require( '@stdlib/ndarray/base/empty' ); +import Slice = require( '@stdlib/slice/ctor' ); +import splice = require( './index' ); + + +// TESTS // + +// The function returns an ndarray... +{ + const order = 'row-major'; + const sh = [ 2, 2 ]; + const s = new Slice( null ); + + splice( empty( 'float64', sh, order ), s ); // $ExpectType float64ndarray + splice( empty( 'float32', sh, order ), s ); // $ExpectType float32ndarray + splice( empty( 'complex128', sh, order ), s ); // $ExpectType complex128ndarray + splice( empty( 'complex64', sh, order ), s ); // $ExpectType complex64ndarray + splice( empty( 'int32', sh, order ), s ); // $ExpectType int32ndarray + splice( empty( 'int16', sh, order ), s ); // $ExpectType int16ndarray + splice( empty( 'int8', sh, order ), s ); // $ExpectType int8ndarray + splice( empty( 'uint32', sh, order ), s ); // $ExpectType uint32ndarray + splice( empty( 'uint16', sh, order ), s ); // $ExpectType uint16ndarray + splice( empty( 'uint8', sh, order ), s ); // $ExpectType uint8ndarray + splice( empty( 'uint8c', sh, order ), s ); // $ExpectType uint8cndarray + + splice( empty( 'float64', sh, order ), s, empty( 'float64', sh, order ) ); // $ExpectType float64ndarray + splice( empty( 'float32', sh, order ), s, empty( 'float32', sh, order ) ); // $ExpectType float32ndarray + splice( empty( 'complex128', sh, order ), s, empty( 'complex128', sh, order ) ); // $ExpectType complex128ndarray + splice( empty( 'complex64', sh, order ), s, empty( 'complex64', sh, order ) ); // $ExpectType complex64ndarray + splice( empty( 'int32', sh, order ), s, empty( 'int32', sh, order ) ); // $ExpectType int32ndarray + splice( empty( 'int16', sh, order ), s, empty( 'int16', sh, order ) ); // $ExpectType int16ndarray + splice( empty( 'int8', sh, order ), s, empty( 'int8', sh, order ) ); // $ExpectType int8ndarray + splice( empty( 'uint32', sh, order ), s, empty( 'uint32', sh, order ) ); // $ExpectType uint32ndarray + splice( empty( 'uint16', sh, order ), s, empty( 'uint16', sh, order ) ); // $ExpectType uint16ndarray + splice( empty( 'uint8', sh, order ), s, empty( 'uint8', sh, order ) ); // $ExpectType uint8ndarray + splice( empty( 'uint8c', sh, order ), s, empty( 'uint8c', sh, order ) ); // $ExpectType uint8cndarray + + splice( empty( 'float64', sh, order ), s, { 'dim': -2 } ); // $ExpectType float64ndarray + splice( empty( 'float32', sh, order ), s, { 'dim': -2 } ); // $ExpectType float32ndarray + splice( empty( 'complex128', sh, order ), s, { 'dim': -2 } ); // $ExpectType complex128ndarray + splice( empty( 'complex64', sh, order ), s, { 'dim': -2 } ); // $ExpectType complex64ndarray + splice( empty( 'int32', sh, order ), s, { 'dim': -2 } ); // $ExpectType int32ndarray + splice( empty( 'int16', sh, order ), s, { 'dim': -2 } ); // $ExpectType int16ndarray + splice( empty( 'int8', sh, order ), s, { 'dim': -2 } ); // $ExpectType int8ndarray + splice( empty( 'uint32', sh, order ), s, { 'dim': -2 } ); // $ExpectType uint32ndarray + splice( empty( 'uint16', sh, order ), s, { 'dim': -2 } ); // $ExpectType uint16ndarray + splice( empty( 'uint8', sh, order ), s, { 'dim': -2 } ); // $ExpectType uint8ndarray + splice( empty( 'uint8c', sh, order ), s, { 'dim': -2 } ); // $ExpectType uint8cndarray + + splice( empty( 'float64', sh, order ), s, empty( 'float64', sh, order ), { 'dim': -2 } ); // $ExpectType float64ndarray + splice( empty( 'float32', sh, order ), s, empty( 'float32', sh, order ), { 'dim': -2 } ); // $ExpectType float32ndarray + splice( empty( 'complex128', sh, order ), s, empty( 'complex128', sh, order ), { 'dim': -2 } ); // $ExpectType complex128ndarray + splice( empty( 'complex64', sh, order ), s, empty( 'complex64', sh, order ), { 'dim': -2 } ); // $ExpectType complex64ndarray + splice( empty( 'int32', sh, order ), s, empty( 'int32', sh, order ), { 'dim': -2 } ); // $ExpectType int32ndarray + splice( empty( 'int16', sh, order ), s, empty( 'int16', sh, order ), { 'dim': -2 } ); // $ExpectType int16ndarray + splice( empty( 'int8', sh, order ), s, empty( 'int8', sh, order ), { 'dim': -2 } ); // $ExpectType int8ndarray + splice( empty( 'uint32', sh, order ), s, empty( 'uint32', sh, order ), { 'dim': -2 } ); // $ExpectType uint32ndarray + splice( empty( 'uint16', sh, order ), s, empty( 'uint16', sh, order ), { 'dim': -2 } ); // $ExpectType uint16ndarray + splice( empty( 'uint8', sh, order ), s, empty( 'uint8', sh, order ), { 'dim': -2 } ); // $ExpectType uint8ndarray + splice( empty( 'uint8c', sh, order ), s, empty( 'uint8c', sh, order ), { 'dim': -2 } ); // $ExpectType uint8cndarray +} + +// The compiler throws an error if the function is provided a first argument which is not an ndarray... +{ + var y = empty( 'float64', [ 2, 2 ], 'row-major' ); + const s = new Slice( null ); + + splice( '10', s ); // $ExpectError + splice( 10, s ); // $ExpectError + splice( false, s ); // $ExpectError + splice( true, s ); // $ExpectError + splice( null, s ); // $ExpectError + splice( [], s ); // $ExpectError + splice( {}, s ); // $ExpectError + splice( ( x: number ): number => x, s ); // $ExpectError + + splice( '10', s, y ); // $ExpectError + splice( 10, s, y ); // $ExpectError + splice( false, s, y ); // $ExpectError + splice( true, s, y ); // $ExpectError + splice( null, s, y ); // $ExpectError + splice( [], s, y ); // $ExpectError + splice( {}, s, y ); // $ExpectError + splice( ( x: number ): number => x, s, y ); // $ExpectError + + splice( '10', s, {} ); // $ExpectError + splice( 10, s, {} ); // $ExpectError + splice( false, s, {} ); // $ExpectError + splice( true, s, {} ); // $ExpectError + splice( null, s, {} ); // $ExpectError + splice( [], s, {} ); // $ExpectError + splice( {}, s, {} ); // $ExpectError + splice( ( x: number ): number => x, s, {} ); // $ExpectError + + splice( '10', s, y, {} ); // $ExpectError + splice( 10, s, y, {} ); // $ExpectError + splice( false, s, y, {} ); // $ExpectError + splice( true, s, y, {} ); // $ExpectError + splice( null, s, y, {} ); // $ExpectError + splice( [], s, y, {} ); // $ExpectError + splice( {}, s, y, {} ); // $ExpectError + splice( ( x: number ): number => x, s, y, {} ); // $ExpectError +} + +// The compiler throws an error if the function is provided a second argument which is not a valid slice argument or an array of slice arguments... +{ + const x = empty( 'float64', [ 2, 2 ], 'row-major' ); + var y = empty( 'float64', [ 2, 2 ], 'row-major' ); + + splice( x, '5' ); // $ExpectError + splice( x, false ); // $ExpectError + splice( x, true ); // $ExpectError + splice( x, [ '5' ] ); // $ExpectError + splice( x, ( x: number ): number => x ); // $ExpectError + + splice( x, '5', {} ); // $ExpectError + splice( x, false, {} ); // $ExpectError + splice( x, true, {} ); // $ExpectError + splice( x, [ '5' ], {} ); // $ExpectError + splice( x, ( x: number ): number => x, {} ); // $ExpectError + + splice( x, '5', {} ); // $ExpectError + splice( x, false, {} ); // $ExpectError + splice( x, true, {} ); // $ExpectError + splice( x, [ '5' ], {} ); // $ExpectError + splice( x, ( x: number ): number => x, {} ); // $ExpectError + + splice( x, '5', y, {} ); // $ExpectError + splice( x, false, y, {} ); // $ExpectError + splice( x, true, y, {} ); // $ExpectError + splice( x, [ '5' ], y, {} ); // $ExpectError + splice( x, ( x: number ): number => x, y, {} ); // $ExpectError +} + +// The compiler throws an error if the function is provided a values argument which is not an ndarray or an array of ndarrays... +{ + const x = empty( 'float64', [ 2, 2 ], 'row-major' ); + const s = new Slice( null ); + + splice( x, s, '5' ); // $ExpectError + splice( x, s, true ); // $ExpectError + splice( x, s, false ); // $ExpectError + splice( x, s, [ '5' ] ); // $ExpectError + splice( x, s, ( x: number ): number => x ); // $ExpectError + + splice( x, s, '5', {} ); // $ExpectError + splice( x, s, true, {} ); // $ExpectError + splice( x, s, false, {} ); // $ExpectError + splice( x, s, [ '5' ], {} ); // $ExpectError + splice( x, s, ( x: number ): number => x, {} ); // $ExpectError +} + +// The compiler throws an error if the function is provided an options argument which is not an object... +{ + const x = empty( 'float64', [ 2, 2 ], 'row-major' ); + const y = empty( 'float64', [ 2, 2 ], 'row-major' ); + const s = new Slice( null ); + + splice( x, s, y, '5' ); // $ExpectError + splice( x, s, y, true ); // $ExpectError + splice( x, s, y, false ); // $ExpectError + splice( x, s, y, [ '5' ] ); // $ExpectError + splice( x, s, y, ( x: number ): number => x ); // $ExpectError + + splice( x, s, '5' ); // $ExpectError + splice( x, s, true ); // $ExpectError + splice( x, s, false ); // $ExpectError + splice( x, s, [ '5' ] ); // $ExpectError + splice( x, s, ( x: number ): number => x ); // $ExpectError +} + +// The compiler throws an error if the function is provided a `dim` option which is not a number... +{ + const x = empty( 'float64', [ 2, 2 ], 'row-major' ); + const y = empty( 'float64', [ 2, 2 ], 'row-major' ); + const s = new Slice( null ); + + splice( x, s, { 'dim': '5' } ); // $ExpectError + splice( x, s, { 'dim': null } ); // $ExpectError + splice( x, s, { 'dim': [ '5' ] } ); // $ExpectError + splice( x, s, { 'dim': {} } ); // $ExpectError + splice( x, s, { 'dim': ( x: number ): number => x } ); // $ExpectError + + splice( x, s, y, { 'dim': '5' } ); // $ExpectError + splice( x, s, y, { 'dim': null } ); // $ExpectError + splice( x, s, y, { 'dim': [ '5' ] } ); // $ExpectError + splice( x, s, y, { 'dim': {} } ); // $ExpectError + splice( x, s, y, { 'dim': ( x: number ): number => x } ); // $ExpectError +} diff --git a/lib/node_modules/@stdlib/ndarray/splice/examples/index.js b/lib/node_modules/@stdlib/ndarray/splice/examples/index.js new file mode 100644 index 000000000000..c4040396e87a --- /dev/null +++ b/lib/node_modules/@stdlib/ndarray/splice/examples/index.js @@ -0,0 +1,50 @@ +/** +* @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 Slice = require( '@stdlib/slice/ctor' ); +var splice = require( './../lib' ); + +// Define input ndarray: +var x = uniform( [ 3, 3, 3 ], -10, 10 ); +console.log( 'Input ndarray: ', ndarray2array( x ) ); + +// Define values ndarray: +var y = uniform( [ 1, 3, 3 ], 20, 40 ); +console.log( 'Values: ', ndarray2array( y ) ); + +// Remove slice region from the input ndarray: +var o = splice( x, new Slice( 1, 2, 1 ), { + 'dim': -3 +}); +console.log( ndarray2array( o ) ); + +// Insert elements into the slice region of an input ndarray: +var o1 = splice( x, new Slice( 1, 2, 1 ), y, { + 'dim': -3 +}); +console.log( ndarray2array( o1 ) ); + +// Remove a slice region and insert elements into another slice region of an input ndarray: +var o2 = splice( x, new Slice( 2, null, 1 ), new Slice( 0, 1, 1 ), [ y ], { + 'dim': -3 +}); +console.log( ndarray2array( o2 ) ); diff --git a/lib/node_modules/@stdlib/ndarray/splice/lib/index.js b/lib/node_modules/@stdlib/ndarray/splice/lib/index.js new file mode 100644 index 000000000000..41dc9ee124ff --- /dev/null +++ b/lib/node_modules/@stdlib/ndarray/splice/lib/index.js @@ -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. +*/ + +'use strict'; + +/** +* Return an ndarray where the elements of an input ndarray are replaced or removed along a specific dimension. +* +* @module @stdlib/ndarray/splice +* +* @example +* var Slice = require( '@stdlib/slice/ctor' ); +* var array = require( '@stdlib/ndarray/array' ); +* var splice = require( '@stdlib/ndarray/splice' ); +* +* var x = array( [ [ 1.0, 2.0 ], [ 3.0, 4.0 ], [ 5.0, 6.0 ] ] ); +* // returns [ [ 1.0, 2.0 ], [ 3.0, 4.0 ], [ 5.0, 6.0 ] ] +* +* var y = array( [ [ 10.0, 20.0 ] ] ); +* // returns [ [ 10.0, 20.0 ] ] +* +* var s = new Slice( 1, 2, 1 ); +* // returns +* +* var out = splice( x, s, y, { +* 'dim': -2 +* }); +* // returns [ [ 1.0, 2.0 ], [ 10.0, 20.0 ], [ 5.0, 6.0 ] ] +*/ + +// MODULES // + +var main = require( './main.js' ); + + +// EXPORTS // + +module.exports = main; diff --git a/lib/node_modules/@stdlib/ndarray/splice/lib/main.js b/lib/node_modules/@stdlib/ndarray/splice/lib/main.js new file mode 100644 index 000000000000..ea07bcdec1f6 --- /dev/null +++ b/lib/node_modules/@stdlib/ndarray/splice/lib/main.js @@ -0,0 +1,357 @@ +/** +* @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 max-lines-per-function, max-statements */ + +'use strict'; + +// MODULES // + +var isArray = require( '@stdlib/assert/is-array' ); +var isndarrayLike = require( '@stdlib/assert/is-ndarray-like' ); +var isInteger = require( '@stdlib/assert/is-integer' ); +var isPlainObject = require( '@stdlib/assert/is-plain-object' ); +var hasOwnProp = require( '@stdlib/assert/has-own-property' ); +var zeroTo = require( '@stdlib/array/zero-to' ); +var Slice = require( '@stdlib/slice/ctor' ); +var ndims = require( '@stdlib/ndarray/ndims' ); +var normalizeIndex = require( '@stdlib/ndarray/base/normalize-index' ); +var format = require( '@stdlib/string/format' ); +var concat = require( '@stdlib/ndarray/concat' ); +var args2multislice = require( '@stdlib/slice/base/args2multislice' ); +var slice = require( '@stdlib/ndarray/base/slice' ); +var getShape = require( '@stdlib/ndarray/shape' ); +var getDType = require( '@stdlib/ndarray/dtype' ); +var getOrder = require( '@stdlib/ndarray/order' ); +var nulls = require( '@stdlib/array/base/nulls' ); +var empty = require( '@stdlib/ndarray/empty' ); +var normalizeSlices = require( './normalizeslices.js' ); + + +// FUNCTIONS // + +/** +* Returns a function that compares slice indices by their `start` value. +* +* @private +* @param {Array} slices - an array of slice objects. +* @returns {Function} - a function that compares slice indices by their `start` value. +*/ +function sliceOrder( slices ) { + return fcn; + + /** + * Compares two slice indices based on their `start` value. + * + * @private + * @param {number} i - index of the first slice + * @param {number} j - index of the second slice + * @returns {number} sort order + */ + function fcn( i, j ) { + return slices[ i ].start - slices[ j ].start; + } +} + + +// MAIN // + +/** +* Returns an ndarray where the elements of an input ndarray are replaced or removed along a specific dimension. +* +* @param {ndarray} x - input array +* @param {...*} s - slice arguments +* @param {(ndarray|Array)} values - an ndarray or an array of ndarrays containing the elements to insert +* @param {Options} [options] - options +* @param {integer} [options.dim=-1] - dimension along which to perform the operation +* @throws {TypeError} first argument must be an ndarray +* @throws {TypeError} first argument must be an ndarray having one or more dimensions +* @throws {RangeError} dimension index exceeds the number of dimensions +* @throws {RangeError} slice exceeds array bounds +* @throws {TypeError} values argument must be an ndarray or an array of ndarrays +* @throws {RangeError} values argument must be an ndarray or an array of ndarrays having the same or fewer number of dimensions than the input ndarray +* @throws {RangeError} number of slices and values ndarray must be equal +* @throws {TypeError} options argument must be an object +* @throws {TypeError} must provide valid options +* @returns {ndarray} output ndarray +* +* @example +* var Slice = require( '@stdlib/slice/ctor' ); +* var array = require( '@stdlib/ndarray/array' ); +* +* var x = array( [ [ 1.0, 2.0 ], [ 3.0, 4.0 ], [ 5.0, 6.0 ] ] ); +* // returns [ [ 1.0, 2.0 ], [ 3.0, 4.0 ], [ 5.0, 6.0 ] ] +* +* var y = array( [ [ 10.0, 20.0 ] ] ); +* // returns [ [ 10.0, 20.0 ] ] +* +* var s = new Slice( 1, 2, 1 ); +* // returns +* +* var out = splice( x, s, y, { +* 'dim': -2 +* }); +* // returns [ [ 1.0, 2.0 ], [ 10.0, 20.0 ], [ 5.0, 6.0 ] ] +*/ +function splice( x ) { + var sortedSlices; + var hasValues; + var options; + var hasOpts; + var indices; + var prevEnd; + var slices; + var pieces; + var nargs; + var spdim; + var opts; + var args; + var sh; + var ms; + var v1; + var s1; + var v; + var N; + var i; + + nargs = arguments.length; + if ( !isndarrayLike( x ) ) { + throw new TypeError( format( 'invalid argument. First argument must be an ndarray. Value: `%s`.', x ) ); + } + + // Retrieve array meta data: + N = ndims( x ); + sh = getShape( x ); + + // 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 ) ); + } + + // Resolve function arguments: + hasOpts = false; + hasValues = false; + opts = { + 'dim': -1 + }; + if ( isPlainObject( arguments[ nargs - 1 ] ) ) { + hasOpts = true; + options = arguments[ nargs - 1 ]; + if ( nargs > 4 ) { + v = arguments[ nargs - 2 ]; + if ( isArray( v ) ) { // Case: splice( x, s1, s2, [ v1, v2 ], options ) + for ( i = 0; i < v.length; i++ ) { + if ( !isndarrayLike( v[ i ] ) ) { + throw new TypeError( format( 'invalid argument. Values argument must be an array of ndarrays. Value: `%s`.', v ) ); + } + if ( ndims( v[ i ] ) > N ) { + throw new RangeError( format( 'invalid argument. Values argument must be an ndarray or an array of ndarrays having the same or fewer number of dimensions than the input ndarray. Value: `%s`.', v[ i ] ) ); + } + } + hasValues = true; + slices = []; + for ( i = 1; i < arguments.length-2; i++ ) { + slices.push( arguments[ i ] ); + } + } else { // Case: splice( x, s1, s2, s3, options ) + slices = []; + for ( i = 1; i <= arguments.length - 2; i++ ) { + slices.push( arguments[ i ] ); + } + } + } else if ( nargs === 4 ) { + v = arguments[ nargs - 2 ]; + if ( isndarrayLike( v ) ) { // Case: splice( x, s1, v1, options ) or splice( x, [ s1 ], v1, options ) + if ( ndims( v ) > N ) { + throw new RangeError( format( 'invalid argument. Values argument must be an ndarray or an array of ndarrays having the same or fewer number of dimensions than the input ndarray. Value: `%s`.', v ) ); + } + v = [ v ]; + hasValues = true; + slices = arguments[ nargs - 3 ]; + if ( !isArray( slices ) ) { + slices = [ arguments[ nargs - 3 ] ]; + } + } else if ( isArray( v ) ) { // Case: splice( x, s1, [ v1 ], options ) or splice( x, [ s1 ], [ v1 ], options ) + for ( i = 0; i < v.length; i++ ) { + if ( !isndarrayLike( v[ i ] ) ) { + throw new TypeError( format( 'invalid argument. Values argument must be an array of ndarrays. Value: `%s`.', v ) ); + } + if ( ndims( v[ i ] ) > N ) { + throw new RangeError( format( 'invalid argument. Values argument must be an ndarray or an array of ndarrays having the same or fewer number of dimensions than the input ndarray. Value: `%s`.', v[ i ] ) ); + } + } + hasValues = true; + slices = arguments[ nargs - 3 ]; + if ( !isArray( slices ) ) { + slices = [ arguments[ nargs - 3 ] ]; + } + } else { // Case: splice( x, s1, s2, options ) + slices = []; + for ( i = 1; i <= arguments.length - 2; i++ ) { + slices.push( arguments[ i ] ); + } + } + } else if ( nargs <= 3 ) { // Case: splice( x, ???, options ) + slices = arguments[ nargs - 2 ]; + if ( !isArray( slices ) ) { + slices = [ slices ]; + } + } + } else { + hasOpts = false; + if ( nargs >= 3 ) { + v = arguments[ nargs - 1 ]; + if ( isndarrayLike( v ) ) { // Case: splice( x, s1, v1 ) or splice( x, [ s1 ], v1 ) + if ( ndims( v ) > N ) { + throw new RangeError( format( 'invalid argument. Values argument must be an ndarray or an array of ndarrays having the same or fewer number of dimensions than the input ndarray. Value: `%s`.', v ) ); + } + v = [ v ]; + hasValues = true; + if ( isArray( arguments[ 1 ] ) ) { + slices = arguments[ 1 ]; + } else { + slices = []; + for ( i = 1; i < arguments.length-1; i++ ) { + slices.push( arguments[ i ] ); + } + } + } else if ( isArray( v ) ) { // Case: splice( x, s1, s2, s3, [ v1, v2, v3 ] ) + for ( i = 0; i < v.length; i++ ) { + if ( !isndarrayLike( v[ i ] ) ) { + throw new TypeError( format( 'invalid argument. Values argument must be an array of ndarrays. Value: `%s`.', v ) ); + } + if ( ndims( v[ i ] ) > N ) { + throw new RangeError( format( 'invalid argument. Values argument must be an ndarray or an array of ndarrays having the same or fewer number of dimensions than the input ndarray. Value: `%s`.', v[ i ] ) ); + } + } + hasValues = true; + if ( isArray( arguments[ 1 ] ) ) { + slices = arguments[ 1 ]; + } else { + slices = []; + for ( i = 1; i < arguments.length-1; i++ ) { + slices.push( arguments[ i ] ); + } + } + } else { // Case: splice( x, s1, s2, s3 ) + slices = []; + for ( i = 1; i < arguments.length; i++ ) { + slices.push( arguments[ i ] ); + } + } + } else { // Case: splice( x, s1 ) or splice( x, [ s1 ] ) + slices = arguments[ nargs - 1 ]; + if ( !isArray( slices ) ) { + slices = [ arguments[ nargs - 1 ] ]; + } + } + } + + // Validate options: + if ( hasOpts ) { + if ( hasOwnProp( options, 'dim') ) { + if ( !isInteger( options.dim ) ) { + throw new TypeError( format( 'invalid option. `%s` option must be an integer. Option: `%d`.', 'dim', opts.dim ) ); + } + opts.dim = options.dim; + if ( opts.dim >= 0 ) { + opts.dim -= N; + if ( opts.dim >= 0 ) { + throw new RangeError( format( 'invalid argument. Dimension index exceeds the number of dimensions. Number of dimensions: %d. Value: `%d`.', N, options.dim ) ); + } + } + } + } + + // Normalize slices: + spdim = normalizeIndex( opts.dim, N-1 ); + if ( spdim === -1 ) { + throw new RangeError( format( 'invalid argument. Dimension index exceeds the number of dimensions. Number of dimensions: %d. Value: `%d`.', N, spdim ) ); + } + slices = normalizeSlices( x, slices, spdim ); + for ( i = 0; i < slices.length; i++ ) { + if ( slices[ i ].step !== 1 ) { + throw new RangeError( format( 'invalid argument. Slice arguments must have an index increment of `1` . Value: `%d`.', slices[ i ].step ) ); + } + } + + // Sort the normalized slices: + if ( slices.length > 1 ) { + indices = zeroTo( slices.length ); + indices.sort( sliceOrder( slices ) ); + sortedSlices = []; + for ( i = 0; i < indices.length; i++ ) { + sortedSlices.push( slices[ indices[ i ] ] ); + } + if ( hasValues ) { + v1 = []; + for ( i = 0; i < indices.length; i++ ) { + v1.push( v[ indices[ i ] ] ); + } + } + } else { + sortedSlices = slices; + if ( hasValues ) { + v1 = v; + } + } + + // Validate slice regions: + for ( i = 1; i < sortedSlices.length; i++ ) { + if ( + slices[ i ].start < slices[ i - 1 ].stop && + slices[ i ].stop > slices[ i - 1 ].start + ) { + throw new RangeError( 'invalid argument. Slice regions must not overlap.' ); + } + } + + pieces = []; + prevEnd = 0; + for ( i = 0; i < sortedSlices.length; i++ ) { + s1 = sortedSlices[ i ]; + if ( s1.start > prevEnd ) { + args = nulls( sh.length ); + args[ spdim ] = new Slice( prevEnd, s1.start, 1 ); + ms = args2multislice( args ); + pieces.push( slice( x, ms, true, false ) ); + } + if ( hasValues && v1[ i ] ) { + pieces.push( v1[ i ] ); + } + prevEnd = s1.stop; + } + if ( prevEnd < sh[ spdim ] ) { + args = nulls( sh.length ); + args[ spdim ] = new Slice( prevEnd, sh[ spdim ], 1 ); + ms = args2multislice( args ); + pieces.push( slice( x, ms, true, false ) ); + } + if ( pieces.length === 0 ) { + return empty( [], { + 'dtype': getDType( x ), + 'order': getOrder( x ) + }); + } + return concat( pieces, opts ); +} + + +// EXPORTS // + +module.exports = splice; diff --git a/lib/node_modules/@stdlib/ndarray/splice/lib/normalizeslices.js b/lib/node_modules/@stdlib/ndarray/splice/lib/normalizeslices.js new file mode 100644 index 000000000000..9149a8462e86 --- /dev/null +++ b/lib/node_modules/@stdlib/ndarray/splice/lib/normalizeslices.js @@ -0,0 +1,67 @@ +/** +* @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 args2multislice = require( '@stdlib/slice/base/args2multislice' ); +var normalizeMultiSlice = require( '@stdlib/slice/base/normalize-multi-slice' ); +var getShape = require( '@stdlib/ndarray/shape' ); +var filled = require( '@stdlib/array/filled' ); +var join = require( '@stdlib/array/base/join' ); +var format = require( '@stdlib/string/format' ); + + +// MAIN // + +/** +* Returns normalized slice regions for a list of slices along a specified dimension. +* +* @private +* @param {ndarray} x - input array +* @param {Array<(Slice|integer|null|void)>} sliceList - list of slices +* @param {integer} dim - dimension along which to normalize slices +* @throws {RangeError} slice exceeds array bounds +* @returns {Array} array of normalized slices +*/ +function normalizeSlices( x, sliceList, dim ) { + var sh; + var ms; + var S; + + sh = getShape( x ); + + // Create a "shape" for the express purpose of validating each of the slice arguments: + sh = filled( sh[ dim ], sliceList.length, 'generic' ); + + // Create an un-normalized multislice: + ms = args2multislice( sliceList ); + + // Normalize the multislice: + S = normalizeMultiSlice( ms, sh, true ); + if ( S.code ) { + throw new RangeError( format( 'invalid argument. Slice exceeds array bounds. Array shape: (%s).', join( sh, ',' ) ) ); + } + return S.data; +} + + +// EXPORTS // + +module.exports = normalizeSlices; diff --git a/lib/node_modules/@stdlib/ndarray/splice/package.json b/lib/node_modules/@stdlib/ndarray/splice/package.json new file mode 100644 index 000000000000..71ccb19186e0 --- /dev/null +++ b/lib/node_modules/@stdlib/ndarray/splice/package.json @@ -0,0 +1,64 @@ +{ + "name": "@stdlib/ndarray/splice", + "version": "0.0.0", + "description": "Return an ndarray where elements of an input ndarray are replaced or removed along a specific 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", + "data", + "structure", + "vector", + "ndarray", + "matrix", + "splice", + "replace", + "remove" + ] +} diff --git a/lib/node_modules/@stdlib/ndarray/splice/test/test.js b/lib/node_modules/@stdlib/ndarray/splice/test/test.js new file mode 100644 index 000000000000..7a85d4483770 --- /dev/null +++ b/lib/node_modules/@stdlib/ndarray/splice/test/test.js @@ -0,0 +1,1434 @@ +/** +* @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 object-curly-newline */ + +'use strict'; + +// MODULES // + +var tape = require( 'tape' ); +var isndarrayLike = require( '@stdlib/assert/is-ndarray-like' ); +var Slice = require( '@stdlib/slice/ctor' ); +var array = require( '@stdlib/ndarray/array' ); +var zeros = require( '@stdlib/ndarray/zeros' ); +var ndarray2array = require( '@stdlib/ndarray/to-array' ); +var splice = require( './../lib' ); + + +// TESTS // + +tape( 'main export is a function', function test( t ) { + t.ok( true, __filename ); + t.strictEqual( typeof splice, 'function', 'main export is a function' ); + t.end(); +}); + +tape( 'the function throws an error if provided a first argument which is not an ndarray', function test( t ) { + var values; + var i; + + values = [ + '5', + 5, + NaN, + true, + false, + null, + void 0, + [], + {}, + function noop() {} + ]; + + for ( i = 0; i < values.length; i++ ) { + t.throws( badValue( values[ i ] ), TypeError, 'throws an error when provided ' + values[ i ] ); + } + t.end(); + + function badValue( value ) { + return function badValue() { + splice( value, 0 ); + }; + } +}); + +tape( 'the function throws an error if provided a first argument which is not an ndarray (values)', function test( t ) { + var values; + var y; + var i; + + values = [ + '5', + 5, + NaN, + true, + false, + null, + void 0, + [], + {}, + function noop() {} + ]; + y = zeros( [ 1, 2 ] ); + + for ( i = 0; i < values.length; i++ ) { + t.throws( badValue( values[ i ] ), TypeError, 'throws an error when provided ' + values[ i ] ); + } + t.end(); + + function badValue( value ) { + return function badValue() { + splice( value, 0, y ); + }; + } +}); + +tape( 'the function throws an error if provided a first argument which is not an ndarray (options)', function test( t ) { + var values; + var i; + + values = [ + '5', + 5, + NaN, + true, + false, + null, + void 0, + [], + {}, + function noop() {} + ]; + + for ( i = 0; i < values.length; i++ ) { + t.throws( badValue( values[ i ] ), TypeError, 'throws an error when provided ' + values[ i ] ); + } + t.end(); + + function badValue( value ) { + return function badValue() { + splice( value, 0, {} ); + }; + } +}); + +tape( 'the function throws an error if provided a first argument which is not an ndarray (values, options)', function test( t ) { + var values; + var y; + var i; + + values = [ + '5', + 5, + NaN, + true, + false, + null, + void 0, + [], + {}, + function noop() {} + ]; + y = zeros( [ 1, 2 ] ); + + for ( i = 0; i < values.length; i++ ) { + t.throws( badValue( values[ i ] ), TypeError, 'throws an error when provided ' + values[ i ] ); + } + t.end(); + + function badValue( value ) { + return function badValue() { + splice( value, 0, y, {} ); + }; + } +}); + +tape( 'the function throws an error if provided a second argument which is not a valid slice argument', function test( t ) { + var values; + var x; + var i; + + values = [ + '5', + 3.14, + NaN, + true, + false, + [ '5' ], + {}, + function noop() {} + ]; + x = zeros( [ 2, 2 ] ); + + for ( i = 0; i < values.length; i++ ) { + t.throws( badValue( values[ i ] ), TypeError, 'throws an error when provided ' + values[ i ] ); + } + t.end(); + + function badValue( value ) { + return function badValue() { + splice( x, value ); + }; + } +}); + +tape( 'the function throws an error if provided a second argument which is not a valid slice argument (values)', function test( t ) { + var values; + var x; + var y; + var i; + + values = [ + '5', + 3.14, + NaN, + true, + false, + [ '5' ], + {}, + function noop() {} + ]; + x = zeros( [ 2, 2 ] ); + y = zeros( [ 1, 2 ] ); + + for ( i = 0; i < values.length; i++ ) { + t.throws( badValue( values[ i ] ), TypeError, 'throws an error when provided ' + values[ i ] ); + } + t.end(); + + function badValue( value ) { + return function badValue() { + splice( x, value, y ); + }; + } +}); + +tape( 'the function throws an error if provided a second argument which is not a valid slice argument (options)', function test( t ) { + var values; + var x; + var i; + + values = [ + '5', + 3.14, + NaN, + true, + false, + [ '5' ], + {}, + function noop() {} + ]; + x = zeros( [ 2, 2 ] ); + + for ( i = 0; i < values.length; i++ ) { + t.throws( badValue( values[ i ] ), TypeError, 'throws an error when provided ' + values[ i ] ); + } + t.end(); + + function badValue( value ) { + return function badValue() { + splice( x, value, {} ); + }; + } +}); + +tape( 'the function throws an error if provided a second argument which is not a valid slice argument (values, options)', function test( t ) { + var values; + var x; + var y; + var i; + + values = [ + '5', + 3.14, + NaN, + true, + false, + [ '5' ], + {}, + function noop() {} + ]; + x = zeros( [ 2, 2 ] ); + y = zeros( [ 1, 2 ] ); + + for ( i = 0; i < values.length; i++ ) { + t.throws( badValue( values[ i ] ), TypeError, 'throws an error when provided ' + values[ i ] ); + } + t.end(); + + function badValue( value ) { + return function badValue() { + splice( x, value, y, {} ); + }; + } +}); + +tape( 'the function throws an error if provided a values argument which is not an ndarray', function test( t ) { + var values; + var x; + var i; + + values = [ + '5', + 5, + NaN, + true, + false, + null, + void 0, + function noop() {} + ]; + x = zeros( [ 2, 2 ] ); + + for ( i = 0; i < values.length; i++ ) { + t.throws( badValue( values[ i ] ), Error, 'throws an error when provided ' + values[ i ] ); + } + t.end(); + + function badValue( value ) { + return function badValue() { + splice( x, 0, value ); + }; + } +}); + +tape( 'the function throws an error if provided a values argument which is not an ndarray (options)', function test( t ) { + var values; + var x; + var i; + + values = [ + '5', + 5, + NaN, + true, + false, + null, + void 0, + function noop() {} + ]; + x = zeros( [ 2, 2 ] ); + + for ( i = 0; i < values.length; i++ ) { + t.throws( badValue( values[ i ] ), Error, 'throws an error when provided ' + values[ i ] ); + } + t.end(); + + function badValue( value ) { + return function badValue() { + splice( x, 0, value, {} ); + }; + } +}); + +tape( 'the function throws an error if provided value ndarrays having dimensions greater than the input ndarray', function test( t ) { + var values; + var x; + var i; + + values = [ + zeros( [ 1, 2, 2 ] ), + zeros( [ 1, 1, 2, 2 ] ), + zeros( [ 1, 1, 1, 2, 2 ] ), + [ zeros( [ 1, 2, 2 ] ) ] + ]; + x = zeros( [ 2, 2 ] ); + + for ( i = 0; i < values.length; i++ ) { + t.throws( badValue( values[ i ] ), RangeError, 'throws an error when provided ' + values[ i ] ); + } + t.end(); + + function badValue( value ) { + return function badValue() { + splice( x, 0, value ); + }; + } +}); + +tape( 'the function throws an error if provided value ndarrays having dimensions greater than the input ndarray (options)', function test( t ) { + var values; + var x; + var i; + + values = [ + zeros( [ 1, 2, 2 ] ), + zeros( [ 1, 1, 2, 2 ] ), + zeros( [ 1, 1, 1, 2, 2 ] ), + [ zeros( [ 1, 2, 2 ] ) ] + ]; + x = zeros( [ 2, 2 ] ); + + for ( i = 0; i < values.length; i++ ) { + t.throws( badValue( values[ i ] ), RangeError, 'throws an error when provided ' + values[ i ] ); + } + t.end(); + + function badValue( value ) { + return function badValue() { + splice( x, 0, value, {} ); + }; + } +}); + +tape( 'the function throws an error if provided an options argument which is not an object', function test( t ) { + var values; + var x; + var i; + + values = [ + '5', + 5, + NaN, + true, + false, + null, + void 0, + function noop() {} + ]; + x = zeros( [ 2, 2 ] ); + + for ( i = 0; i < values.length; i++ ) { + t.throws( badValue( values[ i ] ), Error, 'throws an error when provided ' + values[ i ] ); + } + t.end(); + + function badValue( value ) { + return function badValue() { + splice( x, 0, value ); + }; + } +}); + +tape( 'the function throws an error if provided an options argument which is not an object (values)', function test( t ) { + var values; + var x; + var y; + var i; + + values = [ + '5', + 5, + NaN, + true, + false, + null, + void 0, + [], + function noop() {} + ]; + x = zeros( [ 2, 2 ] ); + y = zeros( [ 1, 2 ] ); + + for ( i = 0; i < values.length; i++ ) { + t.throws( badValue( values[ i ] ), Error, 'throws an error when provided ' + values[ i ] ); + } + t.end(); + + function badValue( value ) { + return function badValue() { + splice( x, 0, y, value ); + }; + } +}); + +tape( 'the function throws an error if provided a `dim` option which is not an integer', function test( t ) { + var values; + var x; + var i; + + values = [ + '5', + 5.5, + NaN, + true, + false, + null, + void 0, + {}, + [], + function noop() {} + ]; + x = zeros( [ 2, 2 ] ); + + for ( i = 0; i < values.length; i++ ) { + t.throws( badValue( values[ i ] ), Error, 'throws an error when provided ' + values[ i ] ); + } + t.end(); + + function badValue( value ) { + return function badValue() { + splice( x, 0, { + 'dim': value + }); + }; + } +}); + +tape( 'the function throws an error if provided a `dim` option which is not an integer (values)', function test( t ) { + var values; + var x; + var y; + var i; + + values = [ + '5', + 5.5, + NaN, + true, + false, + null, + void 0, + {}, + [], + function noop() {} + ]; + x = zeros( [ 2, 2 ] ); + y = zeros( [ 1, 2 ] ); + + for ( i = 0; i < values.length; i++ ) { + t.throws( badValue( values[ i ] ), Error, 'throws an error when provided ' + values[ i ] ); + } + t.end(); + + function badValue( value ) { + return function badValue() { + splice( x, 0, y, { + 'dim': value + }); + }; + } +}); + +tape( 'the function throws an error if provided a `dim` option which exceeds the number of dimensions of the input ndarray', function test( t ) { + var values; + var x; + var i; + + values = [ + { 'dim': 5 }, + { 'dim': -8 }, + { 'dim': 10 }, + { 'dim': -5 } + ]; + x = zeros( [ 2, 2 ] ); + + for ( i = 0; i < values.length; i++ ) { + t.throws( badValues( values[ i ] ), RangeError, 'throws an error when provided ' + values[ i ] ); + } + t.end(); + + function badValues( value ) { + return function badValues() { + splice( x, 0, value ); + }; + } +}); + +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() { + splice( x, 0 ); + }; + } +}); + +tape( 'the function throws an error if provided a zero-dimensional array (values)', function test( t ) { + var values; + var y; + var i; + + values = [ + zeros( [] ) + ]; + y = 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() { + splice( x, 0, y ); + }; + } +}); + +tape( 'the function throws an error when a slice exceeds array bounds', function test( t ) { + var values; + var slices; + var i; + + values = [ + zeros( [ 1 ] ), + zeros( [ 1, 1 ] ), + zeros( [ 1, 1, 1 ] ), + zeros( [ 1, 1, 1, 1 ] ) + ]; + + slices = [ + new Slice( 5, 20, 1 ), + 10, + new Slice( 7, 8, 1 ), + 20 + ]; + for ( i = 0; i < values.length; i++ ) { + t.throws( badValues( values[ i ], slices[ i ] ), RangeError, 'throws an error when provided ' + slices[ i ].toString() ); + } + t.end(); + + function badValues( x, s ) { + return function badValues() { + splice( x, s ); + }; + } +}); + +tape( 'the function throws an error when a slice exceeds array bounds (values)', function test( t ) { + var values; + var slices; + var y; + var i; + + values = [ + zeros( [ 1, 1 ] ), + zeros( [ 1, 1, 1 ] ), + zeros( [ 1, 1, 1, 1 ] ) + ]; + slices = [ + new Slice( 5, 20, 1 ), + 10, + new Slice( 7, 8, 1 ), + 20 + ]; + y = zeros( [ 5, 5 ] ); + + for ( i = 0; i < values.length; i++ ) { + t.throws( badValues( values[ i ], slices[ i ] ), RangeError, 'throws an error when provided ' + slices[ i ].toString() ); + } + t.end(); + + function badValues( x, s ) { + return function badValues() { + splice( x, s, y ); + }; + } +}); + +tape( 'the function throws an error when a slice exceeds array bounds (options)', function test( t ) { + var values; + var slices; + var i; + + values = [ + zeros( [ 1 ] ), + zeros( [ 1, 1 ] ), + zeros( [ 1, 1, 1 ] ), + zeros( [ 1, 1, 1, 1 ] ) + ]; + + slices = [ + new Slice( 5, 20, 1 ), + 10, + new Slice( 7, 8, 1 ), + 20 + ]; + for ( i = 0; i < values.length; i++ ) { + t.throws( badValues( values[ i ], slices[ i ] ), RangeError, 'throws an error when provided ' + slices[ i ].toString() ); + } + t.end(); + + function badValues( x, s ) { + return function badValues() { + splice( x, s, {} ); + }; + } +}); + +tape( 'the function throws an error when a slice exceeds array bounds (values, options)', function test( t ) { + var values; + var slices; + var y; + var i; + + values = [ + zeros( [ 1, 1 ] ), + zeros( [ 1, 1, 1 ] ), + zeros( [ 1, 1, 1, 1 ] ) + ]; + slices = [ + new Slice( 5, 20, 1 ), + 10, + new Slice( 7, 8, 1 ), + 20 + ]; + y = zeros( [ 5, 5 ] ); + + for ( i = 0; i < values.length; i++ ) { + t.throws( badValues( values[ i ], slices[ i ] ), RangeError, 'throws an error when provided ' + slices[ i ].toString() ); + } + t.end(); + + function badValues( x, s ) { + return function badValues() { + splice( x, s, y, {} ); + }; + } +}); + +tape( 'the function throws an error if provided a slice argument unsupported index increment', function test( t ) { + var values; + var x; + var i; + + values = [ + new Slice( 0, 1, 2 ), + new Slice( 1, 2, -1 ), + new Slice( 2, null, 3 ) + ]; + x = zeros( [ 2, 2, 2 ] ); + + for ( i = 0; i < values.length; i++ ) { + t.throws( badValues( values[ i ] ), RangeError, 'throws an error when provided ' + values[ i ].toString() ); + } + t.end(); + + function badValues( value ) { + return function badValues() { + splice( x, value ); + }; + } +}); + +tape( 'the function throws an error if provided a slice argument unsupported index increment (values)', function test( t ) { + var values; + var x; + var y; + var i; + + values = [ + new Slice( 0, 1, 2 ), + new Slice( 1, 2, -1 ), + new Slice( 2, null, 3 ) + ]; + x = zeros( [ 2, 2, 2 ] ); + y = zeros( [ 1, 2 ] ); + + for ( i = 0; i < values.length; i++ ) { + t.throws( badValues( values[ i ] ), RangeError, 'throws an error when provided ' + values[ i ].toString() ); + } + t.end(); + + function badValues( value ) { + return function badValues() { + splice( x, value, y ); + }; + } +}); + +tape( 'the function throws an error if provided a slice argument unsupported index increment (options)', function test( t ) { + var values; + var x; + var i; + + values = [ + new Slice( 0, 1, 2 ), + new Slice( 1, 2, -1 ), + new Slice( 2, null, 3 ) + ]; + x = zeros( [ 2, 2, 2 ] ); + + for ( i = 0; i < values.length; i++ ) { + t.throws( badValues( values[ i ] ), RangeError, 'throws an error when provided ' + values[ i ].toString() ); + } + t.end(); + + function badValues( value ) { + return function badValues() { + splice( x, value, {} ); + }; + } +}); + +tape( 'the function throws an error if provided a slice argument unsupported index increment (values, options)', function test( t ) { + var values; + var x; + var y; + var i; + + values = [ + new Slice( 0, 1, 2 ), + new Slice( 1, 2, -1 ), + new Slice( 2, null, 3 ) + ]; + x = zeros( [ 2, 2, 2 ] ); + y = zeros( [ 1, 2 ] ); + + for ( i = 0; i < values.length; i++ ) { + t.throws( badValues( values[ i ] ), RangeError, 'throws an error when provided ' + values[ i ].toString() ); + } + t.end(); + + function badValues( value ) { + return function badValues() { + splice( x, value, y, {} ); + }; + } +}); + +tape( 'the function returns an ndarray where elements of an input ndarray are replaced or removed along a specific dimension (1-dimensional)', function test( t ) { + var expected; + var out; + var x; + var y; + + x = array( [ 1.0, 2.0, 3.0, 4.0, 5.0 ] ); + + out = splice( x, new Slice( 1, 4, 1 ) ); + + expected = [ 1.0, 5.0 ]; + + t.strictEqual( isndarrayLike( out ), true, 'returns expected value' ); + t.deepEqual( ndarray2array( out ), expected, 'returns expected value' ); + + out = splice( x, new Slice( 1, 2, 1 ), new Slice( 3, 4, 1 ) ); + + expected = [ 1.0, 3.0, 5.0 ]; + + t.strictEqual( isndarrayLike( out ), true, 'returns expected value' ); + t.deepEqual( ndarray2array( out ), expected, 'returns expected value' ); + + y = zeros( [ 3 ] ); + + out = splice( x, new Slice( 1, 4, 1 ), y ); + + expected = [ 1.0, 0.0, 0.0, 0.0, 5.0 ]; + + t.strictEqual( isndarrayLike( out ), true, 'returns expected value' ); + t.deepEqual( ndarray2array( out ), expected, 'returns expected value' ); + + y = zeros( [ 1 ] ); + + out = splice( x, new Slice( 1, 2, 1 ), new Slice( 3, 4, 1 ), [ y, y ] ); + + expected = [ 1.0, 0.0, 3.0, 0.0, 5.0 ]; + + t.strictEqual( isndarrayLike( out ), true, 'returns expected value' ); + t.deepEqual( ndarray2array( out ), expected, 'returns expected value' ); + + t.end(); +}); + +tape( 'the function returns an ndarray where elements of an input ndarray are replaced or removed along a specific dimension (2-dimensional)', function test( t ) { + var expected; + var out; + var x; + var y; + + x = array( [ [ 1.0, 2.0, 3.0 ], [ 4.0, 5.0, 6.0 ], [ 7.0, 8.0, 9.0 ] ] ); + + out = splice( x, new Slice( 0, 1, 1 ) ); + + expected = [ [ 2.0, 3.0 ], [ 5.0, 6.0 ], [ 8.0, 9.0 ] ]; + + t.strictEqual( isndarrayLike( out ), true, 'returns expected value' ); + t.deepEqual( ndarray2array( out ), expected, 'returns expected value' ); + + out = splice( x, new Slice( 1, 2, 1 ) ); + + expected = [ [ 1.0, 3.0 ], [ 4.0, 6.0 ], [ 7.0, 9.0 ] ]; + + t.strictEqual( isndarrayLike( out ), true, 'returns expected value' ); + t.deepEqual( ndarray2array( out ), expected, 'returns expected value' ); + + out = splice( x, new Slice( 0, 1, 1 ), new Slice( 2, 3, 1 ) ); + + expected = [ [ 2.0 ], [ 5.0 ], [ 8.0 ] ]; + + t.strictEqual( isndarrayLike( out ), true, 'returns expected value' ); + t.deepEqual( ndarray2array( out ), expected, 'returns expected value' ); + + y = zeros( [ 3, 1 ] ); + + out = splice( x, new Slice( 0, 1, 1 ), y ); + + expected = [ [ 0.0, 2.0, 3.0 ], [ 0.0, 5.0, 6.0 ], [ 0.0, 8.0, 9.0 ] ]; + + t.strictEqual( isndarrayLike( out ), true, 'returns expected value' ); + t.deepEqual( ndarray2array( out ), expected, 'returns expected value' ); + + y = zeros( [ 3, 1 ] ); + + out = splice( x, new Slice( 1, 2, 1 ), y ); + + expected = [ [ 1.0, 0.0, 3.0 ], [ 4.0, 0.0, 6.0 ], [ 7.0, 0.0, 9.0 ] ]; + + y = zeros( [ 3, 1 ] ); + + out = splice( x, new Slice( 0, 1, 1 ), new Slice( 2, 3, 1 ), [ y, y ] ); + + expected = [ [ 0.0, 2.0, 0.0 ], [ 0.0, 5.0, 0.0 ], [ 0.0, 8.0, 0.0 ] ]; + + t.strictEqual( isndarrayLike( out ), true, 'returns expected value' ); + t.deepEqual( ndarray2array( out ), expected, 'returns expected value' ); + + out = splice( x, new Slice( 0, 1, 1 ), { + 'dim': -2 + }); + + expected = [ [ 4.0, 5.0, 6.0 ], [ 7.0, 8.0, 9.0 ] ]; + + t.strictEqual( isndarrayLike( out ), true, 'returns expected value' ); + t.deepEqual( ndarray2array( out ), expected, 'returns expected value' ); + + out = splice( x, new Slice( 1, 2, 1 ), { + 'dim': -2 + }); + + expected = [ [ 1.0, 2.0, 3.0 ], [ 7.0, 8.0, 9.0 ] ]; + + t.strictEqual( isndarrayLike( out ), true, 'returns expected value' ); + t.deepEqual( ndarray2array( out ), expected, 'returns expected value' ); + + out = splice( x, new Slice( 2, null, 1 ), { + 'dim': -2 + }); + + expected = [ [ 1.0, 2.0, 3.0 ], [ 4.0, 5.0, 6.0 ] ]; + + t.strictEqual( isndarrayLike( out ), true, 'returns expected value' ); + t.deepEqual( ndarray2array( out ), expected, 'returns expected value' ); + + out = splice( x, new Slice( 0, 1, 1 ), new Slice( 2, 3, 1 ), { + 'dim': -2 + }); + + expected = [ [ 4.0, 5.0, 6.0 ] ]; + + t.strictEqual( isndarrayLike( out ), true, 'returns expected value' ); + t.deepEqual( ndarray2array( out ), expected, 'returns expected value' ); + + y = zeros( [ 1, 3 ] ); + + out = splice( x, new Slice( 0, 1, 1 ), y, { + 'dim': -2 + }); + + expected = [ [ 0.0, 0.0, 0.0 ], [ 4.0, 5.0, 6.0 ], [ 7.0, 8.0, 9.0 ] ]; + + t.strictEqual( isndarrayLike( out ), true, 'returns expected value' ); + t.deepEqual( ndarray2array( out ), expected, 'returns expected value' ); + + y = zeros( [ 1, 3 ] ); + + out = splice( x, new Slice( 1, 2, 1 ), y, { + 'dim': -2 + }); + + expected = [ [ 1.0, 2.0, 3.0 ], [ 0.0, 0.0, 0.0 ], [ 7.0, 8.0, 9.0 ] ]; + + t.strictEqual( isndarrayLike( out ), true, 'returns expected value' ); + t.deepEqual( ndarray2array( out ), expected, 'returns expected value' ); + + y = zeros( [ 1, 3 ] ); + + out = splice( x, new Slice( 2, null, 1 ), y, { + 'dim': -2 + }); + + expected = [ [ 1.0, 2.0, 3.0 ], [ 4.0, 5.0, 6.0 ], [ 0.0, 0.0, 0.0 ] ]; + + t.strictEqual( isndarrayLike( out ), true, 'returns expected value' ); + t.deepEqual( ndarray2array( out ), expected, 'returns expected value' ); + + y = zeros( [ 1, 3 ] ); + + out = splice( x, new Slice( 0, 1, 1 ), new Slice( 2, null, 1 ), [ y, y ], { + 'dim': -2 + }); + + expected = [ [ 0.0, 0.0, 0.0 ], [ 4.0, 5.0, 6.0 ], [ 0.0, 0.0, 0.0 ] ]; + + t.strictEqual( isndarrayLike( out ), true, 'returns expected value' ); + t.deepEqual( ndarray2array( out ), expected, 'returns expected value' ); + + t.end(); +}); + +tape( 'the function returns an ndarray where elements of an input ndarray are replaced or removed along a specific dimension (3-dimensional)', function test( t ) { + var expected; + var out; + var x; + var y; + + x = array([ + [ + [ 1.0, 2.0, 3.0 ], + [ 4.0, 5.0, 6.0 ], + [ 7.0, 8.0, 9.0 ] + ], + [ + [ 10.0, 11.0, 12.0 ], + [ 13.0, 14.0, 15.0 ], + [ 16.0, 17.0, 18.0 ] + ], + [ + [ 19.0, 20.0, 21.0 ], + [ 22.0, 23.0, 24.0 ], + [ 25.0, 26.0, 27.0 ] + ] + ]); + + out = splice( x, new Slice( 0, 1, 1 ) ); + + expected = [ + [ + [ 2.0, 3.0 ], + [ 5.0, 6.0 ], + [ 8.0, 9.0 ] + ], + [ + [ 11.0, 12.0 ], + [ 14.0, 15.0 ], + [ 17.0, 18.0 ] + ], + [ + [ 20.0, 21.0 ], + [ 23.0, 24.0 ], + [ 26.0, 27.0 ] + ] + ]; + + t.strictEqual( isndarrayLike( out ), true, 'returns expected value' ); + t.deepEqual( ndarray2array( out ), expected, 'returns expected value' ); + + out = splice( x, new Slice( 1, 2, 1 ) ); + + expected = [ + [ + [ 1.0, 3.0 ], + [ 4.0, 6.0 ], + [ 7.0, 9.0 ] + ], + [ + [ 10.0, 12.0 ], + [ 13.0, 15.0 ], + [ 16.0, 18.0 ] + ], + [ + [ 19.0, 21.0 ], + [ 22.0, 24.0 ], + [ 25.0, 27.0 ] + ] + ]; + + t.strictEqual( isndarrayLike( out ), true, 'returns expected value' ); + t.deepEqual( ndarray2array( out ), expected, 'returns expected value' ); + + out = splice( x, new Slice( 0, 1, 1 ), new Slice( 2, 3, 1 ) ); + + expected = [ + [ + [ 2.0 ], + [ 5.0 ], + [ 8.0 ] + ], + [ + [ 11.0 ], + [ 14.0 ], + [ 17.0 ] + ], + [ + [ 20.0 ], + [ 23.0 ], + [ 26.0 ] + ] + ]; + + t.strictEqual( isndarrayLike( out ), true, 'returns expected value' ); + t.deepEqual( ndarray2array( out ), expected, 'returns expected value' ); + + y = zeros( [ 3, 3, 1 ] ); + + out = splice( x, new Slice( 0, 1, 1 ), y ); + + expected = [ + [ + [ 0.0, 2.0, 3.0 ], + [ 0.0, 5.0, 6.0 ], + [ 0.0, 8.0, 9.0 ] + ], + [ + [ 0.0, 11.0, 12.0 ], + [ 0.0, 14.0, 15.0 ], + [ 0.0, 17.0, 18.0 ] + ], + [ + [ 0.0, 20.0, 21.0 ], + [ 0.0, 23.0, 24.0 ], + [ 0.0, 26.0, 27.0 ] + ] + ]; + + t.strictEqual( isndarrayLike( out ), true, 'returns expected value' ); + t.deepEqual( ndarray2array( out ), expected, 'returns expected value' ); + + y = zeros( [ 3, 3, 1 ] ); + + out = splice( x, new Slice( 1, 2, 1 ), y ); + + expected = [ + [ + [ 1.0, 0.0, 3.0 ], + [ 4.0, 0.0, 6.0 ], + [ 7.0, 0.0, 9.0 ] + ], + [ + [ 10.0, 0.0, 12.0 ], + [ 13.0, 0.0, 15.0 ], + [ 16.0, 0.0, 18.0 ] + ], + [ + [ 19.0, 0.0, 21.0 ], + [ 22.0, 0.0, 24.0 ], + [ 25.0, 0.0, 27.0 ] + ] + ]; + + t.strictEqual( isndarrayLike( out ), true, 'returns expected value' ); + t.deepEqual( ndarray2array( out ), expected, 'returns expected value' ); + + y = zeros( [ 3, 3, 1 ] ); + + out = splice( x, new Slice( 0, 1, 1 ), new Slice( 2, 3, null ), [ y, y ] ); + + expected = [ + [ + [ 0.0, 2.0, 0.0 ], + [ 0.0, 5.0, 0.0 ], + [ 0.0, 8.0, 0.0 ] + ], + [ + [ 0.0, 11.0, 0.0 ], + [ 0.0, 14.0, 0.0 ], + [ 0.0, 17.0, 0.0 ] + ], + [ + [ 0.0, 20.0, 0.0 ], + [ 0.0, 23.0, 0.0 ], + [ 0.0, 26.0, 0.0 ] + ] + ]; + + t.strictEqual( isndarrayLike( out ), true, 'returns expected value' ); + t.deepEqual( ndarray2array( out ), expected, 'returns expected value' ); + + out = splice( x, new Slice( 0, 1, 1 ), { + 'dim': -2 + }); + + expected = [ + [ + [ 4.0, 5.0, 6.0 ], + [ 7.0, 8.0, 9.0 ] + ], + [ + [ 13.0, 14.0, 15.0 ], + [ 16.0, 17.0, 18.0 ] + ], + [ + [ 22.0, 23.0, 24.0 ], + [ 25.0, 26.0, 27.0 ] + ] + ]; + + t.strictEqual( isndarrayLike( out ), true, 'returns expected value' ); + t.deepEqual( ndarray2array( out ), expected, 'returns expected value' ); + + out = splice( x, new Slice( 1, 2, 1 ), { + 'dim': -2 + }); + + expected = [ + [ + [ 1.0, 2.0, 3.0 ], + [ 7.0, 8.0, 9.0 ] + ], + [ + [ 10.0, 11.0, 12.0 ], + [ 16.0, 17.0, 18.0 ] + ], + [ + [ 19.0, 20.0, 21.0 ], + [ 25.0, 26.0, 27.0 ] + ] + ]; + + t.strictEqual( isndarrayLike( out ), true, 'returns expected value' ); + t.deepEqual( ndarray2array( out ), expected, 'returns expected value' ); + + out = splice( x, new Slice( 0, 1, 1 ), new Slice( 2, 3, 1 ), { + 'dim': -2 + }); + + expected = [ + [ + [ 4.0, 5.0, 6.0 ] + ], + [ + [ 13.0, 14.0, 15.0 ] + ], + [ + [ 22.0, 23.0, 24.0 ] + ] + ]; + + t.strictEqual( isndarrayLike( out ), true, 'returns expected value' ); + t.deepEqual( ndarray2array( out ), expected, 'returns expected value' ); + + y = zeros( [ 3, 1, 3 ] ); + + out = splice( x, new Slice( 0, 1, 1 ), y, { + 'dim': -2 + }); + + expected = [ + [ + [ 0.0, 0.0, 0.0 ], + [ 4.0, 5.0, 6.0 ], + [ 7.0, 8.0, 9.0 ] + ], + [ + [ 0.0, 0.0, 0.0 ], + [ 13.0, 14.0, 15.0 ], + [ 16.0, 17.0, 18.0 ] + ], + [ + [ 0.0, 0.0, 0.0 ], + [ 22.0, 23.0, 24.0 ], + [ 25.0, 26.0, 27.0 ] + ] + ]; + + t.strictEqual( isndarrayLike( out ), true, 'returns expected value' ); + t.deepEqual( ndarray2array( out ), expected, 'returns expected value' ); + + y = zeros( [ 3, 1, 3 ] ); + + out = splice( x, new Slice( 1, 2, 1 ), y, { + 'dim': -2 + }); + + expected = [ + [ + [ 1.0, 2.0, 3.0 ], + [ 0.0, 0.0, 0.0 ], + [ 7.0, 8.0, 9.0 ] + ], + [ + [ 10.0, 11.0, 12.0 ], + [ 0.0, 0.0, 0.0 ], + [ 16.0, 17.0, 18.0 ] + ], + [ + [ 19.0, 20.0, 21.0 ], + [ 0.0, 0.0, 0.0 ], + [ 25.0, 26.0, 27.0 ] + ] + ]; + + t.strictEqual( isndarrayLike( out ), true, 'returns expected value' ); + t.deepEqual( ndarray2array( out ), expected, 'returns expected value' ); + + y = zeros( [ 3, 1, 3 ] ); + + out = splice( x, new Slice( 0, 1, 1 ), new Slice( 2, 3, 1 ), [ y, y ], { + 'dim': -2 + }); + + expected = [ + [ + [ 0.0, 0.0, 0.0 ], + [ 4.0, 5.0, 6.0 ], + [ 0.0, 0.0, 0.0 ] + ], + [ + [ 0.0, 0.0, 0.0 ], + [ 13.0, 14.0, 15 ], + [ 0.0, 0.0, 0.0 ] + ], + [ + [ 0.0, 0.0, 0.0 ], + [ 22.0, 23.0, 24.0 ], + [ 0.0, 0.0, 0.0 ] + ] + ]; + + t.strictEqual( isndarrayLike( out ), true, 'returns expected value' ); + t.deepEqual( ndarray2array( out ), expected, 'returns expected value' ); + + out = splice( x, new Slice( 0, 1, 1 ), { + 'dim': -3 + }); + + expected = [ + [ + [ 10.0, 11.0, 12.0 ], + [ 13.0, 14.0, 15.0 ], + [ 16.0, 17.0, 18.0 ] + ], + [ + [ 19.0, 20.0, 21.0 ], + [ 22.0, 23.0, 24.0 ], + [ 25.0, 26.0, 27.0 ] + ] + ]; + + t.strictEqual( isndarrayLike( out ), true, 'returns expected value' ); + t.deepEqual( ndarray2array( out ), expected, 'returns expected value' ); + + out = splice( x, new Slice( 1, 2, 1 ), { + 'dim': -3 + }); + + expected = [ + [ + [ 1.0, 2.0, 3.0 ], + [ 4.0, 5.0, 6.0 ], + [ 7.0, 8.0, 9.0 ] + ], + [ + [ 19.0, 20.0, 21.0 ], + [ 22.0, 23.0, 24.0 ], + [ 25.0, 26.0, 27.0 ] + ] + ]; + + t.strictEqual( isndarrayLike( out ), true, 'returns expected value' ); + t.deepEqual( ndarray2array( out ), expected, 'returns expected value' ); + + out = splice( x, new Slice( 0, 1, 1 ), new Slice( 2, 3, 1 ), { + 'dim': -3 + }); + + expected = [ + [ + [ 10.0, 11.0, 12.0 ], + [ 13.0, 14.0, 15.0 ], + [ 16.0, 17.0, 18.0 ] + ] + ]; + + t.strictEqual( isndarrayLike( out ), true, 'returns expected value' ); + t.deepEqual( ndarray2array( out ), expected, 'returns expected value' ); + + y = zeros( [ 1, 3, 3 ] ); + + out = splice( x, new Slice( 0, 1, 1 ), y, { + 'dim': -3 + }); + + expected = [ + [ + [ 0.0, 0.0, 0.0 ], + [ 0.0, 0.0, 0.0 ], + [ 0.0, 0.0, 0.0 ] + ], + [ + [ 10.0, 11.0, 12.0 ], + [ 13.0, 14.0, 15.0 ], + [ 16.0, 17.0, 18.0 ] + ], + [ + [ 19.0, 20.0, 21.0 ], + [ 22.0, 23.0, 24.0 ], + [ 25.0, 26.0, 27.0 ] + ] + ]; + + t.strictEqual( isndarrayLike( out ), true, 'returns expected value' ); + t.deepEqual( ndarray2array( out ), expected, 'returns expected value' ); + + y = zeros( [ 1, 3, 3 ] ); + + out = splice( x, new Slice( 1, 2, 1 ), y, { + 'dim': -3 + }); + + expected = [ + [ + [ 1.0, 2.0, 3.0 ], + [ 4.0, 5.0, 6.0 ], + [ 7.0, 8.0, 9.0 ] + ], + [ + [ 0.0, 0.0, 0.0 ], + [ 0.0, 0.0, 0.0 ], + [ 0.0, 0.0, 0.0 ] + ], + [ + [ 19.0, 20.0, 21.0 ], + [ 22.0, 23.0, 24.0 ], + [ 25.0, 26.0, 27.0 ] + ] + ]; + + t.strictEqual( isndarrayLike( out ), true, 'returns expected value' ); + t.deepEqual( ndarray2array( out ), expected, 'returns expected value' ); + + y = zeros( [ 1, 3, 3 ] ); + + out = splice( x, new Slice( 0, 1, 1 ), new Slice( 2, 3, 1 ), [ y, y ], { + 'dim': -3 + }); + + expected = [ + [ + [ 0.0, 0.0, 0.0 ], + [ 0.0, 0.0, 0.0 ], + [ 0.0, 0.0, 0.0 ] + ], + [ + [ 10.0, 11.0, 12.0 ], + [ 13.0, 14.0, 15.0 ], + [ 16.0, 17.0, 18.0 ] + ], + [ + [ 0.0, 0.0, 0.0 ], + [ 0.0, 0.0, 0.0 ], + [ 0.0, 0.0, 0.0 ] + ] + ]; + + t.strictEqual( isndarrayLike( out ), true, 'returns expected value' ); + t.deepEqual( ndarray2array( out ), expected, 'returns expected value' ); + + t.end(); +});