diff --git a/lib/node_modules/@stdlib/ndarray/with/README.md b/lib/node_modules/@stdlib/ndarray/with/README.md new file mode 100644 index 000000000000..a130fe7e3e39 --- /dev/null +++ b/lib/node_modules/@stdlib/ndarray/with/README.md @@ -0,0 +1,150 @@ + + +# ndarrayWith + +> Return a new ndarray with the element at the specified index replaced with a provided value. + + + +
+ +
+ + + + + +
+ +## Usage + +```javascript +var ndarrayWith = require( '@stdlib/ndarray/with' ); +``` + +#### ndarrayWith( x, index, value ) + +Return a new ndarray with the element at the specified index replaced with a provided value. + +```javascript +var ndarray = require( '@stdlib/ndarray/ctor' ); +var ndarray2array = require( '@stdlib/ndarray/to-array' ); + +var buffer = [ 1.0, 2.0, 3.0, 4.0, 5.0, 6.0 ]; +var shape = [ 3, 2 ]; +var strides = [ 2, 1 ]; +var offset = 0; + +var x = ndarray( 'generic', buffer, shape, strides, offset, 'row-major' ); +// returns + +var arr = ndarray2array( x ); +// returns [ [ 1.0, 2.0 ], [ 3.0, 4.0 ], [ 5.0, 6.0 ] ] + +var out = ndarray2array( ndarrayWith( x, [ 0, 0 ], 3.0 ) ); +// returns [ [ 3.0, 2.0 ], [ 3.0, 4.0 ], [ 5.0, 6.0 ] ] + +out = ndarray2array( ndarrayWith( x, [ 0, 0 ], [ 3.0, 4.0 ] ) ); +// returns [ [ [ 3.0, 4.0 ], 2.0 ], [ 3.0, 4.0 ], [ 5.0, 6.0 ] ] +``` + +The function accepts the following arguments: + +- **x**: an input array. +- **index**: element index array. +- **value**: replacement value. + +
+ + + + + +
+ +## Notes + +- index array should have length equal to number of dimensions of a ndarray. + +
+ + + + + +
+ +## Examples + + + +```javascript +var discreteUniform = require( '@stdlib/random/array/discrete-uniform' ); +var ndarray2array = require( '@stdlib/ndarray/to-array' ); +var array = require( '@stdlib/ndarray/array' ); +var ndarrayWith = require( '@stdlib/ndarray/with' ); + +var buffer = discreteUniform( 27, -10, 10, { + 'dtype': 'float64' +}); +var x = array( buffer, { + 'shape': [ 3, 3, 3 ], + 'dtype': 'float64' +}); +console.log( ndarray2array( x ) ); + +var index = discreteUniform( 3, 0, 2, { + 'dtype': 'generic' +}); +var value = 100; +console.log( 'Index: %s, Value: %s', index, value ); + +var out = ndarrayWith( x, index, value ); +console.log( ndarray2array( out ) ); +``` + +
+ + + + + +
+ +
+ + + + + + + + + + + + + + diff --git a/lib/node_modules/@stdlib/ndarray/with/benchmark/benchmark.js b/lib/node_modules/@stdlib/ndarray/with/benchmark/benchmark.js new file mode 100755 index 000000000000..14aa947dabbb --- /dev/null +++ b/lib/node_modules/@stdlib/ndarray/with/benchmark/benchmark.js @@ -0,0 +1,127 @@ +/** +* @license Apache-2.0 +* +* Copyright (c) 2025 The Stdlib Authors. +* +* Licensed under the Apache License, Version 2.0 (the "License"); +* you may not use this file except in compliance with the License. +* You may obtain a copy of the License at +* +* http://www.apache.org/licenses/LICENSE-2.0 +* +* Unless required by applicable law or agreed to in writing, software +* distributed under the License is distributed on an "AS IS" BASIS, +* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. +* See the License for the specific language governing permissions and +* limitations under the License. +*/ + +'use strict'; + +// MODULES // + +var bench = require( '@stdlib/bench' ); +var ndarray = require( '@stdlib/ndarray/ctor' ); +var shape2strides = require( '@stdlib/ndarray/base/shape2strides' ); +var isndarrayLike = require( '@stdlib/assert/is-ndarray-like' ); +var discreteUniform = require( '@stdlib/random/array/discrete-uniform' ); +var pow = require( '@stdlib/math/base/special/pow' ); +var pkg = require( './../package.json' ).name; +var ndarrayWith = require( './../lib' ); + + +// VARIABLES // + +var xtype = 'generic'; +var orders = ['row-major', 'column-major']; + + +// FUNCTIONS // + +/** +* Creates a benchmark function. +* +* @private +* @param {PositiveInteger} len - array length +* @param {NonNegativeIntegerArray} shape - ndarray shape +* @param {string} xtype - input ndarray data type +* @param {string} order - ndarray memory layout +* @returns {Function} benchmark function +*/ +function createBenchmark(len, shape, xtype, order) { + var strides; + var index; + var xbuf; + var x; + + xbuf = discreteUniform( len, -100, 100, { + 'dtype': xtype + }); + strides = shape2strides( shape, order ); + x = ndarray( xtype, xbuf, shape, strides, 0, order ); + index = discreteUniform( shape.length, 0, 0 ); + + 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 = ndarrayWith( x, index, i ); + if ( out.get.apply( out, index ) !== i ) { + b.fail( 'should not return NaN' ); + } + } + b.toc(); + if ( !isndarrayLike( out ) ) { + b.fail( 'should return an array' ); + } + b.pass( 'benchmark finished' ); + b.end(); + } +} + + +// MAIN // + +/** +* Main execution sequence. +* +* @private +*/ +function main() { + var len; + var min; + var max; + var ord; + var sh; + var t1; + var f; + var i; + var k; + + min = 1; // 10^min + max = 6; // 10^max + + for ( k = 0; k < orders.length; k++ ) { + t1 = xtype; + ord = orders[ k ]; + for ( i = min; i <= max; i++ ) { + len = pow( 10, i ); + + sh = [ len ]; + f = createBenchmark( len, sh, t1, ord ); + bench( pkg+':ndims='+sh.length+',len='+len+',shape=['+sh.join(',')+'],xorder='+ord+',xtype='+t1, f ); + } + } +} + +main(); diff --git a/lib/node_modules/@stdlib/ndarray/with/docs/repl.txt b/lib/node_modules/@stdlib/ndarray/with/docs/repl.txt new file mode 100644 index 000000000000..9f84d5e115e2 --- /dev/null +++ b/lib/node_modules/@stdlib/ndarray/with/docs/repl.txt @@ -0,0 +1,30 @@ + +{{alias}}( x, index, value ) + Return a new ndarray with the element at the specified index replaced with + a provided value. + + Parameters + ---------- + x: ndarray + Input ndarray. + + index: ArrayLikeObject + Input array. + + value: any + Replacement value. + + Returns + ------- + out: ndarray + Output ndarray. + + Examples + -------- + > var x = {{alias:@stdlib/ndarray/array}}( [ [ 1.0, 2.0 ], [ 3.0, 4.0 ] ] ); + > var y = {{alias}}( x, [ 0, 0 ], 0.0 ); + > {{alias:@stdlib/ndarray/to-array}}( y ) + [ [ 0.0, 2.0 ], [ 3.0, 4.0 ] ] + + See Also + -------- diff --git a/lib/node_modules/@stdlib/ndarray/with/docs/types/index.d.ts b/lib/node_modules/@stdlib/ndarray/with/docs/types/index.d.ts new file mode 100755 index 000000000000..a8cdac15285a --- /dev/null +++ b/lib/node_modules/@stdlib/ndarray/with/docs/types/index.d.ts @@ -0,0 +1,612 @@ +/* +* @license Apache-2.0 +* +* Copyright (c) 2025 The Stdlib Authors. +* +* Licensed under the Apache License, Version 2.0 (the "License"); +* you may not use this file except in compliance with the License. +* You may obtain a copy of the License at +* +* http://www.apache.org/licenses/LICENSE-2.0 +* +* Unless required by applicable law or agreed to in writing, software +* distributed under the License is distributed on an "AS IS" BASIS, +* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. +* See the License for the specific language governing permissions and +* limitations under the License. +*/ + +// TypeScript Version: 4.1 + +/// + +import { Collection, AccessorArrayLike } from '@stdlib/types/array'; +import { float64ndarray, float32ndarray, int32ndarray, int16ndarray, int8ndarray, uint32ndarray, uint16ndarray, uint8ndarray, uint8cndarray, genericndarray } from '@stdlib/types/ndarray'; + +/** +* element index array. +*/ +type InputArray = Collection | AccessorArrayLike; + +/** +* Return a new ndarray with the element at the specified index replaced with a provided value. +* +* @param x - input ndarray +* @param index - element index array +* @param value - replacement value +* @returns output ndarray +* +* @example +* var Float64Array = require( '@stdlib/array/float64' ); +* var ndarray = require( '@stdlib/ndarray/ctor' ); +* var ndarray2array = require( '@stdlib/ndarray/to-array' ); +* +* var buffer = new Float64Array( [ 1.0, 2.0, 3.0, 4.0, 5.0, 6.0 ] ); +* var shape = [ 3, 2 ]; +* var strides = [ 2, 1 ]; +* var offset = 0; +* +* var x = ndarray( 'float64', buffer, shape, strides, offset, 'row-major' ); +* // returns +* +* var arr = ndarray2array( x ); +* // returns [ [ 1.0, 2.0 ], [ 3.0, 4.0 ], [ 5.0, 6.0 ] ] +* +* var out = ndarrayWith( x, [ 0, 0 ], 3.0 ); +* // returns [ [ 3.0, 2.0 ], [ 3.0, 4.0 ], [ 5.0, 6.0 ] ] +*/ +declare function ndarrayWith( x: float64ndarray, index: InputArray, value: number ): float64ndarray; + +/** +* Return a new ndarray with the element at the specified index replaced with a provided value. +* +* @param x - input ndarray +* @param index - element index array +* @param value - replacement value +* @returns output ndarray +* +* @example +* var Float64Array = require( '@stdlib/array/float64' ); +* var ndarray = require( '@stdlib/ndarray/ctor' ); +* var ndarray2array = require( '@stdlib/ndarray/to-array' ); +* +* var buffer = new Float64Array( [ 1.0, 2.0, 3.0, 4.0, 5.0, 6.0 ] ); +* var shape = [ 3, 2 ]; +* var strides = [ 2, 1 ]; +* var offset = 0; +* +* var x = ndarray( 'float64', buffer, shape, strides, offset, 'row-major' ); +* // returns +* +* var arr = ndarray2array( x ); +* // returns [ [ 1.0, 2.0 ], [ 3.0, 4.0 ], [ 5.0, 6.0 ] ] +* +* var out = ndarrayWith( x, [ 0, 0 ], [ 3.0, 4.0 ] ); +* // returns [ [ [ 3.0, 4.0 ], 2.0 ], [ 3.0, 4.0 ], [ 5.0, 6.0 ] ] +*/ +declare function ndarrayWith( x: float64ndarray, index: InputArray, value: V ): float64ndarray; + +/** +* Return a new ndarray with the element at the specified index replaced with a provided value. +* +* @param x - input ndarray +* @param index - element index array +* @param value - replacement value +* @returns output ndarray +* +* @example +* var Float32Array = require( '@stdlib/array/float32' ); +* var ndarray = require( '@stdlib/ndarray/ctor' ); +* var ndarray2array = require( '@stdlib/ndarray/to-array' ); +* +* var buffer = new Float32Array( [ 1.0, 2.0, 3.0, 4.0, 5.0, 6.0 ] ); +* var shape = [ 3, 2 ]; +* var strides = [ 2, 1 ]; +* var offset = 0; +* +* var x = ndarray( 'float32', buffer, shape, strides, offset, 'row-major' ); +* // returns +* +* var arr = ndarray2array( x ); +* // returns [ [ 1.0, 2.0 ], [ 3.0, 4.0 ], [ 5.0, 6.0 ] ] +* +* var out = ndarrayWith( x, [ 0, 0 ], 3.0 ); +* // returns [ [ 3.0, 2.0 ], [ 3.0, 4.0 ], [ 5.0, 6.0 ] ] +*/ +declare function ndarrayWith( x: float32ndarray, index: InputArray, value: number ): float32ndarray; + +/** +* Return a new ndarray with the element at the specified index replaced with a provided value. +* +* @param x - input ndarray +* @param index - element index array +* @param value - replacement value +* @returns output ndarray +* +* @example +* var Float32Array = require( '@stdlib/array/float32' ); +* var ndarray = require( '@stdlib/ndarray/ctor' ); +* var ndarray2array = require( '@stdlib/ndarray/to-array' ); +* +* var buffer = new Float32Array( [ 1.0, 2.0, 3.0, 4.0, 5.0, 6.0 ] ); +* var shape = [ 3, 2 ]; +* var strides = [ 2, 1 ]; +* var offset = 0; +* +* var x = ndarray( 'float32', buffer, shape, strides, offset, 'row-major' ); +* // returns +* +* var arr = ndarray2array( x ); +* // returns [ [ 1.0, 2.0 ], [ 3.0, 4.0 ], [ 5.0, 6.0 ] ] +* +* var out = ndarrayWith( x, [ 0, 0 ], [ 3.0, 4.0 ] ); +* // returns [ [ [ 3.0, 4.0 ], 2.0 ], [ 3.0, 4.0 ], [ 5.0, 6.0 ] ] +*/ +declare function ndarrayWith( x: float32ndarray, index: InputArray, value: V ): float32ndarray; + +/** +* Return a new ndarray with the element at the specified index replaced with a provided value. +* +* @param x - input ndarray +* @param index - element index array +* @param value - replacement value +* @returns output ndarray +* +* @example +* var Int32Array = require( '@stdlib/array/int32' ); +* var ndarray = require( '@stdlib/ndarray/ctor' ); +* var ndarray2array = require( '@stdlib/ndarray/to-array' ); +* +* var buffer = new Int32Array( [ 1.0, 2.0, 3.0, 4.0, 5.0, 6.0 ] ); +* var shape = [ 3, 2 ]; +* var strides = [ 2, 1 ]; +* var offset = 0; +* +* var x = ndarray( 'int32', buffer, shape, strides, offset, 'row-major' ); +* // returns +* +* var arr = ndarray2array( x ); +* // returns [ [ 1.0, 2.0 ], [ 3.0, 4.0 ], [ 5.0, 6.0 ] ] +* +* var out = ndarrayWith( x, [ 0, 0 ], 3.0 ); +* // returns [ [ 3.0, 2.0 ], [ 3.0, 4.0 ], [ 5.0, 6.0 ] ] +*/ +declare function ndarrayWith( x: int32ndarray, index: InputArray, value: number ): int32ndarray; + +/** +* Return a new ndarray with the element at the specified index replaced with a provided value. +* +* @param x - input ndarray +* @param index - element index array +* @param value - replacement value +* @returns output ndarray +* +* @example +* var Int32Array = require( '@stdlib/array/int32' ); +* var ndarray = require( '@stdlib/ndarray/ctor' ); +* var ndarray2array = require( '@stdlib/ndarray/to-array' ); +* +* var buffer = new Int32Array( [ 1.0, 2.0, 3.0, 4.0, 5.0, 6.0 ] ); +* var shape = [ 3, 2 ]; +* var strides = [ 2, 1 ]; +* var offset = 0; +* +* var x = ndarray( 'int32', buffer, shape, strides, offset, 'row-major' ); +* // returns +* +* var arr = ndarray2array( x ); +* // returns [ [ 1.0, 2.0 ], [ 3.0, 4.0 ], [ 5.0, 6.0 ] ] +* +* var out = ndarrayWith( x, [ 0, 0 ], [ 3.0, 4.0 ] ); +* // returns [ [ [ 3.0, 4.0 ], 2.0 ], [ 3.0, 4.0 ], [ 5.0, 6.0 ] ] +*/ +declare function ndarrayWith( x: int32ndarray, index: InputArray, value: V ): int32ndarray; + +/** +* Return a new ndarray with the element at the specified index replaced with a provided value. +* +* @param x - input ndarray +* @param index - element index array +* @param value - replacement value +* @returns output ndarray +* +* @example +* var Int16Array = require( '@stdlib/array/int16' ); +* var ndarray = require( '@stdlib/ndarray/ctor' ); +* var ndarray2array = require( '@stdlib/ndarray/to-array' ); +* +* var buffer = new Int16Array( [ 1.0, 2.0, 3.0, 4.0, 5.0, 6.0 ] ); +* var shape = [ 3, 2 ]; +* var strides = [ 2, 1 ]; +* var offset = 0; +* +* var x = ndarray( 'int16', buffer, shape, strides, offset, 'row-major' ); +* // returns +* +* var arr = ndarray2array( x ); +* // returns [ [ 1.0, 2.0 ], [ 3.0, 4.0 ], [ 5.0, 6.0 ] ] +* +* var out = ndarrayWith( x, [ 0, 0 ], 3.0 ); +* // returns [ [ 3.0, 2.0 ], [ 3.0, 4.0 ], [ 5.0, 6.0 ] ] +*/ +declare function ndarrayWith( x: int16ndarray, index: InputArray, value: number ): int16ndarray; + +/** +* Return a new ndarray with the element at the specified index replaced with a provided value. +* +* @param x - input ndarray +* @param index - element index array +* @param value - replacement value +* @returns output ndarray +* +* @example +* var Int16Array = require( '@stdlib/array/int16' ); +* var ndarray = require( '@stdlib/ndarray/ctor' ); +* var ndarray2array = require( '@stdlib/ndarray/to-array' ); +* +* var buffer = new Int16Array( [ 1.0, 2.0, 3.0, 4.0, 5.0, 6.0 ] ); +* var shape = [ 3, 2 ]; +* var strides = [ 2, 1 ]; +* var offset = 0; +* +* var x = ndarray( 'int16', buffer, shape, strides, offset, 'row-major' ); +* // returns +* +* var arr = ndarray2array( x ); +* // returns [ [ 1.0, 2.0 ], [ 3.0, 4.0 ], [ 5.0, 6.0 ] ] +* +* var out = ndarrayWith( x, [ 0, 0 ], [ 3.0, 4.0 ] ); +* // returns [ [ [ 3.0, 4.0 ], 2.0 ], [ 3.0, 4.0 ], [ 5.0, 6.0 ] ] +*/ +declare function ndarrayWith( x: int16ndarray, index: InputArray, value: V ): int16ndarray; + +/** +* Return a new ndarray with the element at the specified index replaced with a provided value. +* +* @param x - input ndarray +* @param index - element index array +* @param value - replacement value +* @returns output ndarray +* +* @example +* var Int8Array = require( '@stdlib/array/int8' ); +* var ndarray = require( '@stdlib/ndarray/ctor' ); +* var ndarray2array = require( '@stdlib/ndarray/to-array' ); +* +* var buffer = new Int8Array( [ 1.0, 2.0, 3.0, 4.0, 5.0, 6.0 ] ); +* var shape = [ 3, 2 ]; +* var strides = [ 2, 1 ]; +* var offset = 0; +* +* var x = ndarray( 'int8', buffer, shape, strides, offset, 'row-major' ); +* // returns +* +* var arr = ndarray2array( x ); +* // returns [ [ 1.0, 2.0 ], [ 3.0, 4.0 ], [ 5.0, 6.0 ] ] +* +* var out = ndarrayWith( x, [ 0, 0 ], 3.0 ); +* // returns [ [ 3.0, 2.0 ], [ 3.0, 4.0 ], [ 5.0, 6.0 ] ] +*/ +declare function ndarrayWith( x: int8ndarray, index: InputArray, value: number ): int8ndarray; + +/** +* Return a new ndarray with the element at the specified index replaced with a provided value. +* +* @param x - input ndarray +* @param index - element index array +* @param value - replacement value +* @returns output ndarray +* +* @example +* var Int8Array = require( '@stdlib/array/int8' ); +* var ndarray = require( '@stdlib/ndarray/ctor' ); +* var ndarray2array = require( '@stdlib/ndarray/to-array' ); +* +* var buffer = new Int8Array( [ 1.0, 2.0, 3.0, 4.0, 5.0, 6.0 ] ); +* var shape = [ 3, 2 ]; +* var strides = [ 2, 1 ]; +* var offset = 0; +* +* var x = ndarray( 'int8', buffer, shape, strides, offset, 'row-major' ); +* // returns +* +* var arr = ndarray2array( x ); +* // returns [ [ 1.0, 2.0 ], [ 3.0, 4.0 ], [ 5.0, 6.0 ] ] +* +* var out = ndarrayWith( x, [ 0, 0 ], [ 3.0, 4.0 ] ); +* // returns [ [ [ 3.0, 4.0 ], 2.0 ], [ 3.0, 4.0 ], [ 5.0, 6.0 ] ] +*/ +declare function ndarrayWith( x: int8ndarray, index: InputArray, value: V ): int8ndarray; + +/** +* Return a new ndarray with the element at the specified index replaced with a provided value. +* +* @param x - input ndarray +* @param index - element index array +* @param value - replacement value +* @returns output ndarray +* +* @example +* var Uint32Array = require( '@stdlib/array/uint32' ); +* var ndarray = require( '@stdlib/ndarray/ctor' ); +* var ndarray2array = require( '@stdlib/ndarray/to-array' ); +* +* var buffer = new Uint32Array( [ 1.0, 2.0, 3.0, 4.0, 5.0, 6.0 ] ); +* var shape = [ 3, 2 ]; +* var strides = [ 2, 1 ]; +* var offset = 0; +* +* var x = ndarray( 'uint32', buffer, shape, strides, offset, 'row-major' ); +* // returns +* +* var arr = ndarray2array( x ); +* // returns [ [ 1.0, 2.0 ], [ 3.0, 4.0 ], [ 5.0, 6.0 ] ] +* +* var out = ndarrayWith( x, [ 0, 0 ], 3.0 ); +* // returns [ [ 3.0, 2.0 ], [ 3.0, 4.0 ], [ 5.0, 6.0 ] ] +*/ +declare function ndarrayWith( x: uint32ndarray, index: InputArray, value: number ): uint32ndarray; + +/** +* Return a new ndarray with the element at the specified index replaced with a provided value. +* +* @param x - input ndarray +* @param index - element index array +* @param value - replacement value +* @returns output ndarray +* +* @example +* var Uint32Array = require( '@stdlib/array/uint32' ); +* var ndarray = require( '@stdlib/ndarray/ctor' ); +* var ndarray2array = require( '@stdlib/ndarray/to-array' ); +* +* var buffer = new Uint32Array( [ 1.0, 2.0, 3.0, 4.0, 5.0, 6.0 ] ); +* var shape = [ 3, 2 ]; +* var strides = [ 2, 1 ]; +* var offset = 0; +* +* var x = ndarray( 'uint32', buffer, shape, strides, offset, 'row-major' ); +* // returns +* +* var arr = ndarray2array( x ); +* // returns [ [ 1.0, 2.0 ], [ 3.0, 4.0 ], [ 5.0, 6.0 ] ] +* +* var out = ndarrayWith( x, [ 0, 0 ], [ 3.0, 4.0 ] ); +* // returns [ [ [ 3.0, 4.0 ], 2.0 ], [ 3.0, 4.0 ], [ 5.0, 6.0 ] ] +*/ +declare function ndarrayWith( x: uint32ndarray, index: InputArray, value: V ): uint32ndarray; + +/** +* Return a new ndarray with the element at the specified index replaced with a provided value. +* +* @param x - input ndarray +* @param index - element index array +* @param value - replacement value +* @returns output ndarray +* +* @example +* var Uint16Array = require( '@stdlib/array/uint16' ); +* var ndarray = require( '@stdlib/ndarray/ctor' ); +* var ndarray2array = require( '@stdlib/ndarray/to-array' ); +* +* var buffer = new Uint16Array( [ 1.0, 2.0, 3.0, 4.0, 5.0, 6.0 ] ); +* var shape = [ 3, 2 ]; +* var strides = [ 2, 1 ]; +* var offset = 0; +* +* var x = ndarray( 'uint16', buffer, shape, strides, offset, 'row-major' ); +* // returns +* +* var arr = ndarray2array( x ); +* // returns [ [ 1.0, 2.0 ], [ 3.0, 4.0 ], [ 5.0, 6.0 ] ] +* +* var out = ndarrayWith( x, [ 0, 0 ], 3.0 ); +* // returns [ [ 3.0, 2.0 ], [ 3.0, 4.0 ], [ 5.0, 6.0 ] ] +*/ +declare function ndarrayWith( x: uint16ndarray, index: InputArray, value: number ): uint16ndarray; + +/** +* Return a new ndarray with the element at the specified index replaced with a provided value. +* +* @param x - input ndarray +* @param index - element index array +* @param value - replacement value +* @returns output ndarray +* +* @example +* var Uint16Array = require( '@stdlib/array/uint16' ); +* var ndarray = require( '@stdlib/ndarray/ctor' ); +* var ndarray2array = require( '@stdlib/ndarray/to-array' ); +* +* var buffer = new Uint16Array( [ 1.0, 2.0, 3.0, 4.0, 5.0, 6.0 ] ); +* var shape = [ 3, 2 ]; +* var strides = [ 2, 1 ]; +* var offset = 0; +* +* var x = ndarray( 'uint16', buffer, shape, strides, offset, 'row-major' ); +* // returns +* +* var arr = ndarray2array( x ); +* // returns [ [ 1.0, 2.0 ], [ 3.0, 4.0 ], [ 5.0, 6.0 ] ] +* +* var out = ndarrayWith( x, [ 0, 0 ], [ 3.0, 4.0 ] ); +* // returns [ [ [ 3.0, 4.0 ], 2.0 ], [ 3.0, 4.0 ], [ 5.0, 6.0 ] ] +*/ +declare function ndarrayWith( x: uint16ndarray, index: InputArray, value: V ): uint16ndarray; + +/** +* Return a new ndarray with the element at the specified index replaced with a provided value. +* +* @param x - input ndarray +* @param index - element index array +* @param value - replacement value +* @returns output ndarray +* +* @example +* var Uint8Array = require( '@stdlib/array/uint8' ); +* var ndarray = require( '@stdlib/ndarray/ctor' ); +* var ndarray2array = require( '@stdlib/ndarray/to-array' ); +* +* var buffer = new Uint8Array( [ 1.0, 2.0, 3.0, 4.0, 5.0, 6.0 ] ); +* var shape = [ 3, 2 ]; +* var strides = [ 2, 1 ]; +* var offset = 0; +* +* var x = ndarray( 'uint8', buffer, shape, strides, offset, 'row-major' ); +* // returns +* +* var arr = ndarray2array( x ); +* // returns [ [ 1.0, 2.0 ], [ 3.0, 4.0 ], [ 5.0, 6.0 ] ] +* +* var out = ndarrayWith( x, [ 0, 0 ], 3.0 ); +* // returns [ [ 3.0, 2.0 ], [ 3.0, 4.0 ], [ 5.0, 6.0 ] ] +*/ +declare function ndarrayWith( x: uint8ndarray, index: InputArray, value: number ): uint8ndarray; + +/** +* Return a new ndarray with the element at the specified index replaced with a provided value. +* +* @param x - input ndarray +* @param index - element index array +* @param value - replacement value +* @returns output ndarray +* +* @example +* var Uint8Array = require( '@stdlib/array/uint8' ); +* var ndarray = require( '@stdlib/ndarray/ctor' ); +* var ndarray2array = require( '@stdlib/ndarray/to-array' ); +* +* var buffer = new Uint8Array( [ 1.0, 2.0, 3.0, 4.0, 5.0, 6.0 ] ); +* var shape = [ 3, 2 ]; +* var strides = [ 2, 1 ]; +* var offset = 0; +* +* var x = ndarray( 'uint8', buffer, shape, strides, offset, 'row-major' ); +* // returns +* +* var arr = ndarray2array( x ); +* // returns [ [ 1.0, 2.0 ], [ 3.0, 4.0 ], [ 5.0, 6.0 ] ] +* +* var out = ndarrayWith( x, [ 0, 0 ], [ 3.0, 4.0 ] ); +* // returns [ [ [ 3.0, 4.0 ], 2.0 ], [ 3.0, 4.0 ], [ 5.0, 6.0 ] ] +*/ +declare function ndarrayWith( x: uint8ndarray, index: InputArray, value: V ): uint8ndarray; + +/** +* Return a new ndarray with the element at the specified index replaced with a provided value. +* +* @param x - input ndarray +* @param index - element index array +* @param value - replacement value +* @returns output ndarray +* +* @example +* var Uint8ClampedArray = require( '@stdlib/array/uint8c' ); +* var ndarray = require( '@stdlib/ndarray/ctor' ); +* var ndarray2array = require( '@stdlib/ndarray/to-array' ); +* +* var buffer = new Uint8ClampedArray( [ 1.0, 2.0, 3.0, 4.0, 5.0, 6.0 ] ); +* var shape = [ 3, 2 ]; +* var strides = [ 2, 1 ]; +* var offset = 0; +* +* var x = ndarray( 'uint8c', buffer, shape, strides, offset, 'row-major' ); +* // returns +* +* var arr = ndarray2array( x ); +* // returns [ [ 1.0, 2.0 ], [ 3.0, 4.0 ], [ 5.0, 6.0 ] ] +* +* var out = ndarrayWith( x, [ 0, 0 ], 3.0 ); +* // returns [ [ 3.0, 2.0 ], [ 3.0, 4.0 ], [ 5.0, 6.0 ] ] +*/ +declare function ndarrayWith( x: uint8cndarray, index: InputArray, value: number ): uint8cndarray; + +/** +* Return a new ndarray with the element at the specified index replaced with a provided value. +* +* @param x - input ndarray +* @param index - element index array +* @param value - replacement value +* @returns output ndarray +* +* @example +* var Uint8ClampedArray = require( '@stdlib/array/uint8c' ); +* var ndarray = require( '@stdlib/ndarray/ctor' ); +* var ndarray2array = require( '@stdlib/ndarray/to-array' ); +* +* var buffer = new Uint8ClampedArray( [ 1.0, 2.0, 3.0, 4.0, 5.0, 6.0 ] ); +* var shape = [ 3, 2 ]; +* var strides = [ 2, 1 ]; +* var offset = 0; +* +* var x = ndarray( 'uint8c', buffer, shape, strides, offset, 'row-major' ); +* // returns +* +* var arr = ndarray2array( x ); +* // returns [ [ 1.0, 2.0 ], [ 3.0, 4.0 ], [ 5.0, 6.0 ] ] +* +* var out = ndarrayWith( x, [ 0, 0 ], [ 3.0, 4.0 ] ); +* // returns [ [ [ 3.0, 4.0 ], 2.0 ], [ 3.0, 4.0 ], [ 5.0, 6.0 ] ] +*/ +declare function ndarrayWith( x: uint8cndarray, index: InputArray, value: V ): uint8cndarray; + +/** +* Return a new ndarray with the element at the specified index replaced with a provided value. +* +* @param x - input ndarray +* @param index - element index array +* @param value - replacement value +* @returns output ndarray +* +* @example +* var ndarray = require( '@stdlib/ndarray/ctor' ); +* var ndarray2array = require( '@stdlib/ndarray/to-array' ); +* +* var buffer = [ 1.0, 2.0, 3.0, 4.0, 5.0, 6.0 ]; +* var shape = [ 3, 2 ]; +* var strides = [ 2, 1 ]; +* var offset = 0; +* +* var x = ndarray( 'generic', buffer, shape, strides, offset, 'row-major' ); +* // returns +* +* var arr = ndarray2array( x ); +* // returns [ [ 1.0, 2.0 ], [ 3.0, 4.0 ], [ 5.0, 6.0 ] ] +* +* var out = ndarrayWith( x, [ 0, 0 ], 3.0 ); +* // returns [ [ 3.0, 2.0 ], [ 3.0, 4.0 ], [ 5.0, 6.0 ] ] +*/ +declare function ndarrayWith( x: genericndarray, index: InputArray, value: number ): genericndarray; + +/** +* Return a new ndarray with the element at the specified index replaced with a provided value. +* +* @param x - input ndarray +* @param index - element index array +* @param value - replacement value +* @returns output ndarray +* +* @example +* var ndarray = require( '@stdlib/ndarray/ctor' ); +* var ndarray2array = require( '@stdlib/ndarray/to-array' ); +* +* var buffer = [ 1.0, 2.0, 3.0, 4.0, 5.0, 6.0 ]; +* var shape = [ 3, 2 ]; +* var strides = [ 2, 1 ]; +* var offset = 0; +* +* var x = ndarray( 'generic', buffer, shape, strides, offset, 'row-major' ); +* // returns +* +* var arr = ndarray2array( x ); +* // returns [ [ 1.0, 2.0 ], [ 3.0, 4.0 ], [ 5.0, 6.0 ] ] +* +* var out = ndarrayWith( x, [ 0, 0 ], [ 3.0, 4.0 ] ); +* // returns [ [ [ 3.0, 4.0 ], 2.0 ], [ 3.0, 4.0 ], [ 5.0, 6.0 ] ] +*/ +declare function ndarrayWith( x: genericndarray, index: InputArray, value: V ): genericndarray; + + +// EXPORTS // + +export = ndarrayWith; diff --git a/lib/node_modules/@stdlib/ndarray/with/docs/types/test.ts b/lib/node_modules/@stdlib/ndarray/with/docs/types/test.ts new file mode 100755 index 000000000000..e6ba53b3ebb6 --- /dev/null +++ b/lib/node_modules/@stdlib/ndarray/with/docs/types/test.ts @@ -0,0 +1,75 @@ +/* +* @license Apache-2.0 +* +* Copyright (c) 2025 The Stdlib Authors. +* +* Licensed under the Apache License, Version 2.0 (the "License"); +* you may not use this file except in compliance with the License. +* You may obtain a copy of the License at +* +* http://www.apache.org/licenses/LICENSE-2.0 +* +* Unless required by applicable law or agreed to in writing, software +* distributed under the License is distributed on an "AS IS" BASIS, +* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. +* See the License for the specific language governing permissions and +* limitations under the License. +*/ + +import zeros = require( '@stdlib/ndarray/base/zeros' ); +import ndarrayWith = require( './index' ); + + +// TESTS // + +// The function returns an updated ndarray... +{ + const sh = [ 2, 2 ]; + const ord = 'row-major'; + + ndarrayWith( zeros( 'float64', sh, ord ), [ 0, 0 ], 0.0 ); // $ExpectType float64ndarray + ndarrayWith( zeros( 'float32', sh, ord ), [ 0, 0 ], 0.0 ); // $ExpectType float32ndarray + ndarrayWith( zeros( 'int32', sh, ord ), [ 0, 0 ], 0.0 ); // $ExpectType int32ndarray + ndarrayWith( zeros( 'int16', sh, ord ), [ 0, 0 ], 0.0 ); // $ExpectType int16ndarray + ndarrayWith( zeros( 'int8', sh, ord ), [ 0, 0 ], 0.0 ); // $ExpectType int8ndarray + ndarrayWith( zeros( 'uint32', sh, ord ), [ 0, 0 ], 0.0 ); // $ExpectType uint32ndarray + ndarrayWith( zeros( 'uint16', sh, ord ), [ 0, 0 ], 0.0 ); // $ExpectType uint16ndarray + ndarrayWith( zeros( 'uint8', sh, ord ), [ 0, 0 ], 0.0 ); // $ExpectType uint8ndarray + ndarrayWith( zeros( 'uint8c', sh, ord ), [ 0, 0 ], 0.0 ); // $ExpectType uint8cndarray + ndarrayWith( zeros( 'generic', sh, ord ), [ 0, 0 ], 0.0 ); // $ExpectType genericndarray +} + +// The compiler throws an error if the function is provided a first argument which is not a ndarray... +{ + ndarrayWith( 5, [ 0, 0 ], 5 ); // $ExpectError + ndarrayWith( true, [ 0, 0 ], 5 ); // $ExpectError + ndarrayWith( false, [ 0, 0 ], 5 ); // $ExpectError + ndarrayWith( null, [ 0, 0 ], 5 ); // $ExpectError + ndarrayWith( undefined, [ 0, 0 ], 5 ); // $ExpectError + ndarrayWith( void 0, [ 0, 0 ], 5 ); // $ExpectError + ndarrayWith( {}, [ 0, 0 ], 5 ); // $ExpectError + ndarrayWith( [ 1 ], [ 0, 0 ], 5 ); // $ExpectError + ndarrayWith( ( x: number ): number => x, [ 0, 0 ], 5 ); // $ExpectError +} + +// The compiler throws an error if the function is provided a callback which is not a collection... +{ + const x = zeros( 'generic', [ 2, 2 ], 'row-major' ); + + ndarrayWith( x, 'abc', 5 ); // $ExpectError + ndarrayWith( x, true, 5 ); // $ExpectError + ndarrayWith( x, false, 5 ); // $ExpectError + ndarrayWith( x, null, 5 ); // $ExpectError + ndarrayWith( x, undefined, 5 ); // $ExpectError + ndarrayWith( x, void 0, 5 ); // $ExpectError + ndarrayWith( x, {}, 5 ); // $ExpectError + ndarrayWith( x, ( x: number ): number => x, 5 ); // $ExpectError +} + +// The compiler throws an error if the function is provided an unsupported number of arguments... +{ + ndarrayWith(); // $ExpectError + ndarrayWith( zeros( 'float64', [ 2, 2 ], 'row-major' ) ); // $ExpectError + ndarrayWith( zeros( 'float64', [ 2, 2 ], 'row-major' ), 0 ); // $ExpectError + ndarrayWith( zeros( 'float64', [ 2, 2 ], 'row-major' ), 0, 0, 5 ); // $ExpectError +} diff --git a/lib/node_modules/@stdlib/ndarray/with/examples/index.js b/lib/node_modules/@stdlib/ndarray/with/examples/index.js new file mode 100755 index 000000000000..1e1cd3f422ad --- /dev/null +++ b/lib/node_modules/@stdlib/ndarray/with/examples/index.js @@ -0,0 +1,42 @@ +/** +* @license Apache-2.0 +* +* Copyright (c) 2025 The Stdlib Authors. +* +* Licensed under the Apache License, Version 2.0 (the "License"); +* you may not use this file except in compliance with the License. +* You may obtain a copy of the License at +* +* http://www.apache.org/licenses/LICENSE-2.0 +* +* Unless required by applicable law or agreed to in writing, software +* distributed under the License is distributed on an "AS IS" BASIS, +* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. +* See the License for the specific language governing permissions and +* limitations under the License. +*/ + +'use strict'; + +var discreteUniform = require( '@stdlib/random/array/discrete-uniform' ); +var ndarray2array = require( '@stdlib/ndarray/to-array' ); +var array = require( '@stdlib/ndarray/array' ); +var ndarrayWith = require( './../lib' ); + +var buffer = discreteUniform( 27, -10, 10, { + 'dtype': 'float64' +}); +var x = array( buffer, { + 'shape': [ 3, 3, 3 ], + 'dtype': 'float64' +}); +console.log( ndarray2array( x ) ); + +var index = discreteUniform( 3, 0, 2, { + 'dtype': 'generic' +}); +var value = 100; +console.log( 'Index: %s, Value: %s', index, value ); + +var out = ndarrayWith( x, index, value ); +console.log( ndarray2array( out ) ); diff --git a/lib/node_modules/@stdlib/ndarray/with/lib/index.js b/lib/node_modules/@stdlib/ndarray/with/lib/index.js new file mode 100755 index 000000000000..60e8a84198ad --- /dev/null +++ b/lib/node_modules/@stdlib/ndarray/with/lib/index.js @@ -0,0 +1,55 @@ +/** +* @license Apache-2.0 +* +* Copyright (c) 2025 The Stdlib Authors. +* +* Licensed under the Apache License, Version 2.0 (the "License"); +* you may not use this file except in compliance with the License. +* You may obtain a copy of the License at +* +* http://www.apache.org/licenses/LICENSE-2.0 +* +* Unless required by applicable law or agreed to in writing, software +* distributed under the License is distributed on an "AS IS" BASIS, +* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. +* See the License for the specific language governing permissions and +* limitations under the License. +*/ + +'use strict'; + +/** +* Return a new ndarray with the element at the specified index replaced with a provided value. +* +* @module @stdlib/ndarray/with +* +* @example +* var ndarray = require( '@stdlib/ndarray/ctor' ); +* var ndarray2array = require( '@stdlib/ndarray/to-array' ); +* var ndarrayWith = require( '@stdlib/ndarray/with' ); +* +* var buffer = [ 1.0, 2.0, 3.0, 4.0, 5.0, 6.0 ]; +* var shape = [ 3, 2 ]; +* var strides = [ 2, 1 ]; +* var offset = 0; +* +* var x = ndarray( 'generic', buffer, shape, strides, offset, 'row-major' ); +* // returns +* +* var arr = ndarray2array( x ); +* // returns [ [ 1.0, 2.0 ], [ 3.0, 4.0 ], [ 5.0, 6.0 ] ] +* +* var out = ndarrayWith( x, [ 0, 0 ], 3.0 ); +* +* var out2arr = ndarray2array( out ); +* // returns [ [ 3.0, 2.0 ], [ 3.0, 4.0 ], [ 5.0, 6.0 ] ] +*/ + +// MODULES // + +var main = require( './main.js' ); + + +// EXPORTS // + +module.exports = main; diff --git a/lib/node_modules/@stdlib/ndarray/with/lib/main.js b/lib/node_modules/@stdlib/ndarray/with/lib/main.js new file mode 100755 index 000000000000..9cae6ac80004 --- /dev/null +++ b/lib/node_modules/@stdlib/ndarray/with/lib/main.js @@ -0,0 +1,103 @@ +/** +* @license Apache-2.0 +* +* Copyright (c) 2025 The Stdlib Authors. +* +* Licensed under the Apache License, Version 2.0 (the "License"); +* you may not use this file except in compliance with the License. +* You may obtain a copy of the License at +* +* http://www.apache.org/licenses/LICENSE-2.0 +* +* Unless required by applicable law or agreed to in writing, software +* distributed under the License is distributed on an "AS IS" BASIS, +* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. +* See the License for the specific language governing permissions and +* limitations under the License. +*/ + +'use strict'; + +// MODULES // + +var format = require( '@stdlib/string/format' ); +var isndarrayLike = require( '@stdlib/assert/is-ndarray-like' ); +var getShape = require( '@stdlib/ndarray/shape' ); +var getDType = require( '@stdlib/ndarray/dtype' ); +var getOrder = require( '@stdlib/ndarray/order' ); +var assign = require( '@stdlib/ndarray/base/assign' ); +var ndarrayZeros = require( '@stdlib/ndarray/base/zeros' ); +var convert = require( '@stdlib/array/convert' ); + + +// MAIN // + +/** +* Return a new ndarray with the element at the specified index replaced with a provided value. +* +* @param {ndarray} x - input ndarray +* @param {Collection} index - element index array +* @param {*} value - replacement value +* @throws {TypeError} first argument must be an ndarray-like object +* @throws {RangeError} number of indices must equal the number of dimensions +* @returns {ndarray} output ndarray +* +* @example +* var ndarray = require( '@stdlib/ndarray/ctor' ); +* var ndarray2array = require( '@stdlib/ndarray/to-array' ); +* +* var buffer = [ 1.0, 2.0, 3.0, 4.0, 5.0, 6.0 ]; +* var shape = [ 3, 2 ]; +* var strides = [ 2, 1 ]; +* var offset = 0; +* +* var x = ndarray( 'generic', buffer, shape, strides, offset, 'row-major' ); +* // returns +* +* var arr = ndarray2array( x ); +* // returns [ [ 1.0, 2.0 ], [ 3.0, 4.0 ], [ 5.0, 6.0 ] ] +* +* var out = ndarrayWith( x, [ 0, 0 ], 3.0 ); +* +* var out2arr = ndarray2array( out ); +* // returns [ [ 3.0, 2.0 ], [ 3.0, 4.0 ], [ 5.0, 6.0 ] ] +*/ +function ndarrayWith( x, index, value ) { + var con1; + var con2; + var ord; + var sh; + var dt; + var y; + if ( !isndarrayLike( x ) ) { + throw new TypeError( format( 'invalid argument. First argument must be an ndarray-like object. Value: `%s`.', x ) ); + } + if ( index.length !== x.ndims ) { + throw new RangeError( format( 'invalid argument. second argument length must match the number of dimensions. Expected number of dimensions: `%s`. index length: `%s`.', x.ndims, index.length ) ); + } + // Resolve the input array datatype: + dt = getDType( x ); + + // Resolve the input array shape: + sh = getShape( x ); + + // Resolve the iteration order: + ord = getOrder( x ); + + y = ndarrayZeros( dt, sh, ord ); + assign( [ x, y ] ); + + con1 = convert( index, 'generic' ); + con1.push( value ); + con2 = convert( con1, dt ); + + // Replace value at index + y.set.apply( y, con2 ); + + return y; +} + + +// EXPORTS // + +module.exports = ndarrayWith; diff --git a/lib/node_modules/@stdlib/ndarray/with/package.json b/lib/node_modules/@stdlib/ndarray/with/package.json new file mode 100755 index 000000000000..99ca21a9119b --- /dev/null +++ b/lib/node_modules/@stdlib/ndarray/with/package.json @@ -0,0 +1,64 @@ +{ + "name": "@stdlib/ndarray/with", + "version": "0.0.0", + "description": "Return a new ndarray with the element at the specified index replaced with a provided value.", + "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", + "strided", + "array", + "ndarray", + "with", + "reject", + "extract", + "assign", + "select", + "take" + ], + "__stdlib__": {} +} diff --git a/lib/node_modules/@stdlib/ndarray/with/test/test.js b/lib/node_modules/@stdlib/ndarray/with/test/test.js new file mode 100644 index 000000000000..43b3ca1ae554 --- /dev/null +++ b/lib/node_modules/@stdlib/ndarray/with/test/test.js @@ -0,0 +1,198 @@ +/** +* @license Apache-2.0 +* +* Copyright (c) 2025 The Stdlib Authors. +* +* Licensed under the Apache License, Version 2.0 (the "License"); +* you may not use this file except in compliance with the License. +* You may obtain a copy of the License at +* +* http://www.apache.org/licenses/LICENSE-2.0 +* +* Unless required by applicable law or agreed to in writing, software +* distributed under the License is distributed on an "AS IS" BASIS, +* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. +* See the License for the specific language governing permissions and +* limitations under the License. +*/ + +'use strict'; + +// MODULES // + +var tape = require( 'tape' ); +var ndarray = require( '@stdlib/ndarray/ctor' ); +var shape2strides = require( '@stdlib/ndarray/base/shape2strides' ); +var strides2offset = require( '@stdlib/ndarray/base/strides2offset' ); +var discreteUniform = require( '@stdlib/random/array/discrete-uniform' ); +var isSameFloat64Array = require( '@stdlib/assert/is-same-float64array' ); +var Float64Array = require( '@stdlib/array/float64' ); +var ndarrayWith = require( './../lib' ); + + +// TESTS // + +tape( 'main export is a function', function test( t ) { + t.ok( true, __filename ); + t.strictEqual( typeof ndarrayWith, '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, + true, + false, + null, + void 0, + [], + {}, + function noop() {}, + { + 'data': true + } + ]; + + 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() { + ndarrayWith( value, [], 0.0 ); + }; + } +}); + +tape( 'the function throws an error if provided a second argument which has length not equal to number of dimensions of a ndarray.', function test( t ) { + var value; + var ord; + var buf; + var sh; + var st; + var dt; + var x; + var o; + + buf = new Float64Array( [ 1.0, -2.0, 3.0, -4.0 ] ); + dt = 'float64'; + ord = 'row-major'; + sh = [ 2, 1, 2 ]; + st = shape2strides( sh, ord ); + o = strides2offset( sh, st ); + + x = ndarray( dt, buf, sh, st, o, ord ); + + value = discreteUniform( 4, 0, 2, { + 'dtype': 'generic' + }); + t.throws( badValue( value ), RangeError, 'throws an error when provided ' + value ); + t.end(); + + function badValue( value ) { + return function badValue() { + ndarrayWith( x, value, 0 ); + }; + } +}); + +tape( 'the function throws an error if provided a index argument which is out of bound.', function test( t ) { + var value; + var ord; + var buf; + var sh; + var st; + var dt; + var x; + var o; + + buf = new Float64Array( [ 1.0, -2.0, 3.0, -4.0 ] ); + dt = 'float64'; + ord = 'row-major'; + sh = [ 2, 1, 2 ]; + st = shape2strides( sh, ord ); + o = strides2offset( sh, st ); + + x = ndarray( dt, buf, sh, st, o, ord ); + + value = discreteUniform( 4, 3, 10, { + 'dtype': 'generic' + }); + t.throws( badValue( value ), RangeError, 'throws an error when provided ' + value ); + t.end(); + + function badValue( value ) { + return function badValue() { + ndarrayWith( x, value, 0 ); + }; + } +}); + +tape( 'the function return a new ndarray with the element at the specified index replaced with a provided value (integer)', function test( t ) { + var expected; + var index; + var ord; + var buf; + var sh; + var st; + var dt; + var o; + var x; + var y; + + buf = new Float64Array( [ 1.0, -2.0, 3.0, -4.0 ] ); + dt = 'float64'; + ord = 'row-major'; + sh = [ 2, 1, 2 ]; + st = shape2strides( sh, ord ); + o = strides2offset( sh, st ); + + x = ndarray( dt, buf, sh, st, o, ord ); + + index = [ 0, 0, 1 ]; + y = ndarrayWith( x, index, 2.0 ); + + buf = new Float64Array( [ 1.0, 2.0, 3.0, -4.0 ] ); + expected = ndarray( dt, buf, sh, st, o, ord ); + + t.equal( isSameFloat64Array( y.data, expected.data ), true, 'returns expected value' ); + + t.end(); +}); + +tape( 'the function return a new ndarray with the element at the specified index replaced with a provided value (array)', function test( t ) { + var expected; + var index; + var ord; + var buf; + var sh; + var st; + var dt; + var o; + var x; + var y; + + buf = new Float64Array( [ 1.0, -2.0, 3.0, -4.0 ] ); + dt = 'float64'; + ord = 'row-major'; + sh = [ 2, 1, 2 ]; + st = shape2strides( sh, ord ); + o = strides2offset( sh, st ); + + x = ndarray( dt, buf, sh, st, o, ord ); + + index = [ 0, 0, 1 ]; + y = ndarrayWith( x, index, [ 2.0, 2.0 ] ); + + buf = new Float64Array( [ 1.0, [ 2.0, 2.0 ], 3.0, -4.0 ] ); + expected = ndarray( dt, buf, sh, st, o, ord ); + + t.equal( isSameFloat64Array( y.data, expected.data ), true, 'returns expected value' ); + + t.end(); +});