From 62b9955f7b5f64b608e094384712f01c65bb9721 Mon Sep 17 00:00:00 2001 From: headlessNode Date: Mon, 10 Nov 2025 13:57:33 +0500 Subject: [PATCH 1/3] feat: add ndarray/copy --- type: pre_commit_static_analysis_report description: Results of running static analysis checks when committing changes. report: - task: lint_filenames status: passed - task: lint_editorconfig status: passed - task: lint_markdown status: passed - task: lint_package_json status: passed - task: lint_repl_help status: passed - task: lint_javascript_src status: passed - task: lint_javascript_cli status: na - task: lint_javascript_examples status: passed - task: lint_javascript_tests status: passed - task: lint_javascript_benchmarks status: passed - task: lint_python status: na - task: lint_r status: na - task: lint_c_src status: na - task: lint_c_examples status: na - task: lint_c_benchmarks status: na - task: lint_c_tests_fixtures status: na - task: lint_shell status: na - task: lint_typescript_declarations status: passed - task: lint_typescript_tests status: passed - task: lint_license_headers status: passed --- --- .../@stdlib/ndarray/copy/README.md | 174 ++++ .../ndarray/copy/benchmark/benchmark.js | 57 ++ .../@stdlib/ndarray/copy/docs/repl.txt | 70 ++ .../ndarray/copy/docs/types/index.d.ts | 95 ++ .../@stdlib/ndarray/copy/docs/types/test.ts | 114 +++ .../@stdlib/ndarray/copy/examples/index.js | 40 + .../@stdlib/ndarray/copy/lib/index.js | 48 + .../@stdlib/ndarray/copy/lib/main.js | 118 +++ .../@stdlib/ndarray/copy/package.json | 63 ++ .../@stdlib/ndarray/copy/test/test.js | 863 ++++++++++++++++++ 10 files changed, 1642 insertions(+) create mode 100644 lib/node_modules/@stdlib/ndarray/copy/README.md create mode 100644 lib/node_modules/@stdlib/ndarray/copy/benchmark/benchmark.js create mode 100644 lib/node_modules/@stdlib/ndarray/copy/docs/repl.txt create mode 100644 lib/node_modules/@stdlib/ndarray/copy/docs/types/index.d.ts create mode 100644 lib/node_modules/@stdlib/ndarray/copy/docs/types/test.ts create mode 100644 lib/node_modules/@stdlib/ndarray/copy/examples/index.js create mode 100644 lib/node_modules/@stdlib/ndarray/copy/lib/index.js create mode 100644 lib/node_modules/@stdlib/ndarray/copy/lib/main.js create mode 100644 lib/node_modules/@stdlib/ndarray/copy/package.json create mode 100644 lib/node_modules/@stdlib/ndarray/copy/test/test.js diff --git a/lib/node_modules/@stdlib/ndarray/copy/README.md b/lib/node_modules/@stdlib/ndarray/copy/README.md new file mode 100644 index 000000000000..7f2e73164546 --- /dev/null +++ b/lib/node_modules/@stdlib/ndarray/copy/README.md @@ -0,0 +1,174 @@ + + +# copy + +> Copy an input [ndarray][@stdlib/ndarray/ctor] to a new [ndarray][@stdlib/ndarray/ctor] having the same shape and [data type][@stdlib/ndarray/dtypes]. + + + +
+ +
+ + + + + +
+ +## Usage + +```javascript +var copy = require( '@stdlib/ndarray/copy' ); +``` + +#### copy( x\[, options] ) + +Copies an input [ndarray][@stdlib/ndarray/ctor] to a new [ndarray][@stdlib/ndarray/ctor] having the same shape and [data type][@stdlib/ndarray/dtypes]. + +```javascript +var getShape = require( '@stdlib/ndarray/shape' ); +var zeros = require( '@stdlib/ndarray/zeros' ); + +var x = zeros( [ 2, 2 ] ); +// returns + +var y = copy( x ); +// returns + +var sh = getShape( y ); +// returns [ 2, 2 ] +``` + +The function supports the following `options`: + +- **dtype**: output [ndarray][@stdlib/ndarray/ctor] [data type][@stdlib/ndarray/dtypes]. Overrides the input ndarray's inferred [data type][@stdlib/ndarray/dtypes]. +- **order**: specifies whether the output [ndarray][@stdlib/ndarray/ctor] should be `'row-major'` (C-style) or `'column-major'` (Fortran-style). Overrides the input ndarray's inferred order. +- **mode**: specifies how to handle indices which exceed array dimensions (see [`ndarray`][@stdlib/ndarray/ctor]). Default: `'throw'`. +- **submode**: a mode array which specifies for each dimension how to handle subscripts which exceed array dimensions (see [`ndarray`][@stdlib/ndarray/ctor]). If provided fewer modes than dimensions, the constructor recycles modes using modulo arithmetic. Default: `[ options.mode ]`. + +To override either the `dtype` or `order`, specify the corresponding option. For example, to override the inferred [data type][@stdlib/ndarray/dtypes], + +```javascript +var getShape = require( '@stdlib/ndarray/shape' ); +var getDtype = require( '@stdlib/ndarray/dtype' ); +var zeros = require( '@stdlib/ndarray/zeros' ); + +var x = zeros( [ 2, 2 ] ); +// returns + +var dt = getDtype( x ); +// returns 'float64' + +var y = copy( x, { + 'dtype': 'float32' +}); +// returns + +var sh = getShape( y ); +// returns [ 2, 2 ] + +dt = getDtype( y ); +// returns 'float32' +``` + +
+ + + + + +
+ +## Notes + +- The function performs a full copy in which an ndarray's underlying data is copied to a new underlying data buffer. + +
+ + + + + +
+ +## Examples + + + +```javascript +var bernoulli = require( '@stdlib/random/array/bernoulli' ); +var array = require( '@stdlib/ndarray/array' ); +var ndarray2array = require( '@stdlib/ndarray/to-array' ); +var copy = require( '@stdlib/ndarray/copy' ); + +var xbuf = bernoulli( 10, 0.9, { + 'dtype': 'generic' +}); +var x = array({ + 'dtype': 'generic', + 'data': xbuf, + 'shape': [ 5, 2 ], + 'strides': [ 2, 1 ], + 'offset': 0, + 'order': 'row-major' +}); +console.log( ndarray2array( x ) ); + +var o = copy( x ); +console.log( ndarray2array( o ) ); +``` + +
+ + + + + +
+ +
+ + + + + + + + + + + + + + diff --git a/lib/node_modules/@stdlib/ndarray/copy/benchmark/benchmark.js b/lib/node_modules/@stdlib/ndarray/copy/benchmark/benchmark.js new file mode 100644 index 000000000000..ba844e7e54f2 --- /dev/null +++ b/lib/node_modules/@stdlib/ndarray/copy/benchmark/benchmark.js @@ -0,0 +1,57 @@ +/** +* @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 isndarray = require( '@stdlib/assert/is-ndarray-like' ); +var zeros = require( '@stdlib/ndarray/zeros' ); +var pkg = require( './../package.json' ).name; +var copy = require( './../lib' ); + + +// MAIN // + +bench( pkg, function benchmark( b ) { + var values; + var out; + var i; + + values = [ + zeros( [ 10 ] ), + zeros( [ 5, 5 ] ), + zeros( [ 3, 3, 3 ] ), + zeros( [ 2, 2, 2, 2 ] ) + ]; + + b.tic(); + for ( i = 0; i < b.iterations; i++ ) { + out = copy( values[ i%values.length ] ); + if ( typeof out !== 'object' ) { + b.fail( 'should return an ndarray' ); + } + } + b.toc(); + if ( !isndarray( out ) ) { + b.fail( 'should return an ndarray' ); + } + b.pass( 'benchmark finished' ); + b.end(); +}); diff --git a/lib/node_modules/@stdlib/ndarray/copy/docs/repl.txt b/lib/node_modules/@stdlib/ndarray/copy/docs/repl.txt new file mode 100644 index 000000000000..d558330d46fc --- /dev/null +++ b/lib/node_modules/@stdlib/ndarray/copy/docs/repl.txt @@ -0,0 +1,70 @@ + +{{alias}}( x[, options] ) + Copy an input ndarray to a new ndarray having the same shape and data type. + + The function infers the following attributes from the input array: + + - shape: array shape. + - dtype: underlying array data type. + - order: whether the array order is row-major (C-style) or column-major + (Fortran-style). + + The function performs a full copy in which an ndarray's underlying data is + copied to a new underlying data buffer. + + Parameters + ---------- + x: ndarray + Input array. + + options: Object (optional) + Options. + + options.dtype: string (optional) + Array data type. Overrides the input array's inferred data type. + + options.order: string (optional) + Array order (either 'row-major' (C-style) or 'column-major' (Fortran- + style)). Overrides the input array's inferred order. + + options.mode: string (optional) + Specifies how to handle indices which exceed array dimensions. If equal + to 'throw', an ndarray instance throws an error when an index exceeds + array dimensions. If equal to 'normalize', an ndarray instance + normalizes negative indices and throws an error when an index exceeds + array dimensions. If equal to 'wrap', an ndarray instance wraps around + indices exceeding array dimensions using modulo arithmetic. If equal to + 'clamp', an ndarray instance sets an index exceeding array dimensions + to either `0` (minimum index) or the maximum index. Default: 'throw'. + + options.submode: Array (optional) + Specifies how to handle subscripts which exceed array dimensions. If a + mode for a corresponding dimension is equal to 'throw', an ndarray + instance throws an error when a subscript exceeds array dimensions. If + equal to 'normalize', an ndarray instance normalizes negative + subscripts and throws an error when a subscript exceeds array + dimensions. If equal to 'wrap', an ndarray instance wraps around + subscripts exceeding array dimensions using modulo arithmetic. If equal + to 'clamp', an ndarray instance sets a subscript exceeding array + dimensions to either `0` (minimum index) or the maximum index. If the + number of modes is fewer than the number of dimensions, the function + recycles modes using modulo arithmetic. Default: [ options.mode ]. + + Returns + ------- + out: ndarray + Output array. + + Examples + -------- + > var x = {{alias:@stdlib/ndarray/base/zeros}}( 'float64', [ 2, 2 ], 'row-major' ) + + > var sh = {{alias:@stdlib/ndarray/shape}}( x ) + [ 2, 2 ] + > var y = {{alias}}( x ) + + > sh = {{alias:@stdlib/ndarray/shape}}( y ) + [ 2, 2 ] + + See Also + -------- diff --git a/lib/node_modules/@stdlib/ndarray/copy/docs/types/index.d.ts b/lib/node_modules/@stdlib/ndarray/copy/docs/types/index.d.ts new file mode 100644 index 000000000000..05dcd7c0bc0a --- /dev/null +++ b/lib/node_modules/@stdlib/ndarray/copy/docs/types/index.d.ts @@ -0,0 +1,95 @@ +/* +* @license Apache-2.0 +* +* Copyright (c) 2025 The Stdlib Authors. +* +* Licensed under the Apache License, Version 2.0 (the "License"); +* you may not use this file except in compliance with the License. +* You may obtain a copy of the License at +* +* http://www.apache.org/licenses/LICENSE-2.0 +* +* Unless required by applicable law or agreed to in writing, software +* distributed under the License is distributed on an "AS IS" BASIS, +* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. +* See the License for the specific language governing permissions and +* limitations under the License. +*/ + +/* eslint-disable max-lines */ + +// TypeScript Version: 4.1 + +/// + +import { Order, ndarray, DataType, Mode } from '@stdlib/types/ndarray'; + +/** +* Interface describing function options. +*/ +interface Options { + /** + * Underlying data type. + * + * ## Notes + * + * - This option overrides the input array's inferred data type. + */ + dtype?: DataType; + + /** + * Array order (either 'row-major' (C-style) or 'column-major' (Fortran-style)). + * + * ## Notes + * + * - If provided, this option overrides the input array's inferred order. + */ + order?: Order; + + /** + * Specifies how to handle a linear index which exceeds array dimensions (default: 'throw'). + */ + mode?: Mode; + + /** + * Specifies how to handle subscripts which exceed array dimensions on a per dimension basis (default: ['throw']). + */ + submode?: Array; +} + +/** +* Copies an input ndarray to a new ndarray having the same shape and data type. +* +* ## Notes +* +* - The function performs a full copy in which an ndarray's underlying data is copied to a new underlying data buffer. +* +* @param x - input array +* @param options - options +* @param options.order - specifies whether the output array is 'row-major' (C-style) or 'column-major' (Fortran-style) +* @param options.mode - specifies how to handle a linear index which exceeds array dimensions +* @param options.submode - specifies how to handle subscripts which exceed array dimensions on a per dimension basis +* @returns output array +* +* @example +* var getShape = require( '@stdlib/ndarray/shape' ); +* var zeros = require( '@stdlib/ndarray/zeros' ); +* +* var x = zeros( [ 2, 2 ] ); +* // returns +* +* var sh = getShape( x ); +* // returns [ 2, 2 ] +* +* var y = copy( x ); +* // returns +* +* sh = getShape( y ); +* // returns [ 2, 2 ] +*/ +declare function copy( x: T, options?: Options ): T; + + +// EXPORTS // + +export = copy; diff --git a/lib/node_modules/@stdlib/ndarray/copy/docs/types/test.ts b/lib/node_modules/@stdlib/ndarray/copy/docs/types/test.ts new file mode 100644 index 000000000000..654d73b87f5c --- /dev/null +++ b/lib/node_modules/@stdlib/ndarray/copy/docs/types/test.ts @@ -0,0 +1,114 @@ +/* +* @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/zeros' ); +import copy = require( './index' ); + + +// TESTS // + +// The function returns an ndarray... +{ + copy( zeros( [ 2, 2 ] ) ); // $ExpectType float64ndarray + copy( zeros( [ 2, 2 ] ), {} ); // $ExpectType float64ndarray + copy( zeros( [ 2, 2 ] ), { 'order': 'column-major' } ); // $ExpectType float64ndarray + copy( zeros( [ 2, 2] ), { 'dtype': 'float64' } ); // $ExpectType float64ndarray +} + +// The compiler throws an error if the function is provided a first argument which is not an ndarray-like object... +{ + copy( '10' ); // $ExpectError + copy( 10 ); // $ExpectError + copy( false ); // $ExpectError + copy( true ); // $ExpectError + copy( null ); // $ExpectError + copy( [] ); // $ExpectError + copy( {} ); // $ExpectError + copy( ( x: number ): number => x ); // $ExpectError +} + +// The compiler throws an error if the function is provided a second argument is not an options object... +{ + const x = zeros( [ 2, 2 ] ); + + copy( x, '10' ); // $ExpectError + copy( x, 10 ); // $ExpectError + copy( x, false ); // $ExpectError + copy( x, true ); // $ExpectError + copy( x, [] ); // $ExpectError + copy( x, ( x: number ): number => x ); // $ExpectError +} + +// The compiler throws an error if the function is provided a `dtype` option which is not a valid data type... +{ + const x = zeros( [ 2, 2 ] ); + + copy( x, { 'dtype': '10' } ); // $ExpectError + copy( x, { 'dtype': 10 } ); // $ExpectError + copy( x, { 'dtype': null } ); // $ExpectError + copy( x, { 'dtype': false } ); // $ExpectError + copy( x, { 'dtype': true } ); // $ExpectError + copy( x, { 'dtype': [] } ); // $ExpectError + copy( x, { 'dtype': {} } ); // $ExpectError + copy( x, { 'dtype': ( x: number ): number => x } ); // $ExpectError +} + +// The compiler throws an error if the function is provided an `order` option which is not a valid order... +{ + const x = zeros( [ 2, 2 ] ); + + copy( x, { 'order': '10' } ); // $ExpectError + copy( x, { 'order': 10 } ); // $ExpectError + copy( x, { 'order': false } ); // $ExpectError + copy( x, { 'order': true } ); // $ExpectError + copy( x, { 'order': [] } ); // $ExpectError + copy( x, { 'order': {} } ); // $ExpectError + copy( x, { 'order': ( x: number ): number => x } ); // $ExpectError +} + +// The compiler throws an error if the function is provided a `mode` option which is not a valid mode... +{ + const x = zeros( [ 2, 2 ] ); + + copy( x, { 'mode': '5' } ); // $ExpectError + copy( x, { 'mode': 5 } ); // $ExpectError + copy( x, { 'mode': false } ); // $ExpectError + copy( x, { 'mode': true } ); // $ExpectError + copy( x, { 'mode': [ '5' ] } ); // $ExpectError + copy( x, { 'mode': {} } ); // $ExpectError + copy( x, { 'mode': ( x: number ): number => x } ); // $ExpectError +} + +// The compiler throws an error if the function is provided a `submode` option which is not valid... +{ + const x = zeros( [ 2, 2 ] ); + + copy( x, { 'submode': '5' } ); // $ExpectError + copy( x, { 'submode': 5 } ); // $ExpectError + copy( x, { 'submode': false } ); // $ExpectError + copy( x, { 'submode': true } ); // $ExpectError + copy( x, { 'submode': [ '5' ] } ); // $ExpectError + copy( x, { 'submode': {} } ); // $ExpectError + copy( x, { 'submode': ( x: number ): number => x } ); // $ExpectError +} + +// The compiler throws an error if the function is provided an unsupported number of arguments... +{ + copy(); // $ExpectError + copy( zeros( [ 2, 2 ] ), {}, 1 ); // $ExpectError +} diff --git a/lib/node_modules/@stdlib/ndarray/copy/examples/index.js b/lib/node_modules/@stdlib/ndarray/copy/examples/index.js new file mode 100644 index 000000000000..8160908faa0d --- /dev/null +++ b/lib/node_modules/@stdlib/ndarray/copy/examples/index.js @@ -0,0 +1,40 @@ +/** +* @license Apache-2.0 +* +* Copyright (c) 2025 The Stdlib Authors. +* +* Licensed under the Apache License, Version 2.0 (the "License"); +* you may not use this file except in compliance with the License. +* You may obtain a copy of the License at +* +* http://www.apache.org/licenses/LICENSE-2.0 +* +* Unless required by applicable law or agreed to in writing, software +* distributed under the License is distributed on an "AS IS" BASIS, +* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. +* See the License for the specific language governing permissions and +* limitations under the License. +*/ + +'use strict'; + +var bernoulli = require( '@stdlib/random/array/bernoulli' ); +var array = require( '@stdlib/ndarray/array' ); +var ndarray2array = require( '@stdlib/ndarray/to-array' ); +var copy = require( './../lib' ); + +var xbuf = bernoulli( 10, 0.9, { + 'dtype': 'generic' +}); +var x = array({ + 'dtype': 'generic', + 'data': xbuf, + 'shape': [ 5, 2 ], + 'strides': [ 2, 1 ], + 'offset': 0, + 'order': 'row-major' +}); +console.log( ndarray2array( x ) ); + +var o = copy( x ); +console.log( ndarray2array( o ) ); diff --git a/lib/node_modules/@stdlib/ndarray/copy/lib/index.js b/lib/node_modules/@stdlib/ndarray/copy/lib/index.js new file mode 100644 index 000000000000..4ff1d117919b --- /dev/null +++ b/lib/node_modules/@stdlib/ndarray/copy/lib/index.js @@ -0,0 +1,48 @@ +/** +* @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'; + +/** +* Copy an input ndarray to a new ndarray having the same shape and data type. +* +* @module @stdlib/ndarray/copy +* +* @example +* var zeros = require( '@stdlib/ndarray/zeros' ); +* var getShape = require( '@stdlib/ndarray/shape' ); +* var copy = require( '@stdlib/ndarray/copy' ); +* +* var x = zeros( [ 2, 2 ] ); +* // returns +* +* var y = copy( x ); +* // returns +* +* var sh = getShape( y ); +* // returns [ 2, 2 ] +*/ + +// MODULES // + +var main = require( './main.js' ); + + +// EXPORTS // + +module.exports = main; diff --git a/lib/node_modules/@stdlib/ndarray/copy/lib/main.js b/lib/node_modules/@stdlib/ndarray/copy/lib/main.js new file mode 100644 index 000000000000..d86d590df3b9 --- /dev/null +++ b/lib/node_modules/@stdlib/ndarray/copy/lib/main.js @@ -0,0 +1,118 @@ +/** +* @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 isndarrayLike = require( '@stdlib/assert/is-ndarray-like' ); +var isPlainObject = require( '@stdlib/assert/is-plain-object' ); +var isMostlySafeCast = require( '@stdlib/ndarray/base/assert/is-mostly-safe-data-type-cast' ); +var hasOwnProp = require( '@stdlib/assert/has-own-property' ); +var getDType = require( '@stdlib/ndarray/dtype' ); +var getShape = require( '@stdlib/ndarray/shape' ); +var getOrder = require( '@stdlib/ndarray/order' ); +var empty = require( '@stdlib/ndarray/empty' ); +var assign = require( '@stdlib/ndarray/base/assign' ); +var format = require( '@stdlib/string/format' ); + + +// MAIN // + +/** +* Copy an input ndarray to a new ndarray having the same shape and data type. +* +* @param {ndarray} x - input array +* @param {Options} [options] - function options +* @param {string} [options.dtype] - output array data type (overrides the input array's inferred data type) +* @param {string} [options.order] - specifies whether the output array should be 'row-major' (C-style) or 'column-major' (Fortran-style) (overrides the input array's inferred order) +* @param {(NonNegativeIntegerArray|NonNegativeInteger)} [options.shape] - output array shape (overrides the input array's inferred shape) +* @param {string} [options.mode="throw"] - specifies how to handle indices which exceed array dimensions +* @param {StringArray} [options.submode=["throw"]] - specifies how to handle subscripts which exceed array dimensions on a per dimension basis +* @throws {TypeError} first argument must be an ndarray-like object +* @throws {TypeError} options argument must be an object +* @throws {TypeError} `dtype` option must be a supported ndarray data type +* @throws {TypeError} `order` option must be a supported order +* @throws {TypeError} `shape` option must be either a nonnegative integer or an array of nonnegative integers +* @throws {TypeError} must provide valid options +* @returns {ndarray} ndarray +* +* @example +* var getShape = require( '@stdlib/ndarray/shape' ); +* var zeros = require( '@stdlib/ndarray/zeros' ); +* +* var x = zeros( [ 2, 2 ] ); +* // returns +* +* var y = copy( x ); +* // returns +* +* var sh = getShape( y ); +* // returns [ 2, 2 ] +*/ +function copy( x ) { + var options; + var opts; + var sh; + var o; + + if ( !isndarrayLike( x ) ) { + throw new TypeError( format( 'invalid argument. First argument must be an ndarray-like object. Value: `%s`.', x ) ); + } + // Resolve function options: + opts = {}; + if ( arguments.length > 1 ) { + options = arguments[ 1 ]; + if ( !isPlainObject( options ) ) { + throw new TypeError( format( 'invalid argument. Options argument must be an object. Value: `%s`.', options ) ); + } + if ( hasOwnProp( options, 'dtype' ) ) { + if ( !isMostlySafeCast( getDType( x ), options.dtype ) ) { + throw new TypeError( format( 'invalid option. `%s` option must be safely castable. Option: `%s`.', 'dtype', options.dtype ) ); + } + opts.dtype = options.dtype; + } else { + opts.dtype = getDType( x ); + } + if ( hasOwnProp( options, 'order' ) ) { + opts.order = options.order; + } else { + opts.order = getOrder( x ); + } + if ( hasOwnProp( options, 'mode' ) ) { + opts.mode = options.mode; + } + if ( hasOwnProp( options, 'submode' ) ) { + opts.submode = options.submode; + } + } else { + opts.dtype = getDType( x ); + opts.order = getOrder( x ); + } + sh = getShape( x ); + + // Create a copy of the input ndarray: + o = empty( sh, opts ); + assign( [ x, o ] ); + return o; +} + + +// EXPORTS // + +module.exports = copy; diff --git a/lib/node_modules/@stdlib/ndarray/copy/package.json b/lib/node_modules/@stdlib/ndarray/copy/package.json new file mode 100644 index 000000000000..753d7c2cf031 --- /dev/null +++ b/lib/node_modules/@stdlib/ndarray/copy/package.json @@ -0,0 +1,63 @@ +{ + "name": "@stdlib/ndarray/copy", + "version": "0.0.0", + "description": "Copy an input ndarray to a new ndarray having the same shape and data type.", + "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", + "copy", + "duplicate" + ] +} diff --git a/lib/node_modules/@stdlib/ndarray/copy/test/test.js b/lib/node_modules/@stdlib/ndarray/copy/test/test.js new file mode 100644 index 000000000000..c33d2634b310 --- /dev/null +++ b/lib/node_modules/@stdlib/ndarray/copy/test/test.js @@ -0,0 +1,863 @@ +/** +* @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 isEqualDataType = require( '@stdlib/ndarray/base/assert/is-equal-data-type' ); +var Float32Array = require( '@stdlib/array/float32' ); +var Int32Array = require( '@stdlib/array/int32' ); +var Uint32Array = require( '@stdlib/array/uint32' ); +var Int16Array = require( '@stdlib/array/int16' ); +var Uint16Array = require( '@stdlib/array/uint16' ); +var Complex64Array = require( '@stdlib/array/complex64' ); +var Complex128Array = require( '@stdlib/array/complex128' ); +var BooleanArray = require( '@stdlib/array/bool' ); +var instanceOf = require( '@stdlib/assert/instance-of' ); +var ndarray = require( '@stdlib/ndarray/ctor' ); +var zeros = require( '@stdlib/ndarray/base/zeros' ); +var getShape = require( '@stdlib/ndarray/shape' ); +var getOrder = require( '@stdlib/ndarray/order' ); +var getData = require( '@stdlib/ndarray/data-buffer' ); +var getDType = require( '@stdlib/ndarray/dtype' ); +var ndarray2array = require( '@stdlib/ndarray/to-array' ); +var copy = require( './../lib' ); + + +// TESTS // + +tape( 'main export is a function', function test( t ) { + t.ok( true, __filename ); + t.strictEqual( typeof copy, 'function', 'main export is a function' ); + t.end(); +}); + +tape( 'the function throws an error if provided a first argument which is not an ndarray-like object', 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() { + copy( value ); + }; + } +}); + +tape( 'the function throws an error if provided a first argument which is not an ndarray-like object (options)', 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() { + copy( value, {} ); + }; + } +}); + +tape( 'the function throws an error if provided an options argument which is not an object', function test( t ) { + var values; + var i; + + values = [ + '5', + 5, + 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() { + copy( zeros( 'generic', [ 2, 2 ], 'row-major' ), value ); + }; + } +}); + +tape( 'the function throws an error if provided a `dtype` option which is not a recognized data type', function test( t ) { + var values; + var i; + + values = [ + '5', + 5, + 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() { + copy( zeros( 'generic', [ 2, 2 ], 'row-major' ), { + 'dtype': value + }); + }; + } +}); + +tape( 'the function throws an error if provided an `order` option which is not a recognized order', function test( t ) { + var values; + var i; + + values = [ + '5', + 5, + 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() { + copy( zeros( 'generic', [ 2, 2 ], 'row-major' ), { + 'order': value + }); + }; + } +}); + +tape( 'the function throws an error if provided a `mode` option which is not a recognized mode', function test( t ) { + var values; + var i; + + values = [ + '5', + 'beep', + 'THROW', + 5, + null, + true, + false, + [], + {} + ]; + + 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() { + copy( zeros( 'generic', [ 2, 2 ], 'row-major' ), { + 'mode': value + }); + }; + } +}); + +tape( 'the function throws an error if provided an invalid `submode` option', function test( t ) { + var values; + var i; + + values = [ + '5', + 'beep', + 'THROW', + 5, + null, + true, + false, + {} + ]; + + 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() { + copy( zeros( 'generic', [ 2, 2 ], 'row-major' ), { + 'submode': [ value ] + }); + }; + } +}); + +tape( 'the function copies an input ndarray to a new ndarray (dtype=float64, inferred)', function test( t ) { + var expected; + var actual; + var x; + + x = zeros( 'float64', [ 2, 2 ], 'row-major' ); + + actual = copy( x ); + expected = [ [ 0.0, 0.0 ], [ 0.0, 0.0 ] ]; + + t.strictEqual( isEqualDataType( getDType( actual ), getDType( x ) ), true, 'returns expected value' ); + t.deepEqual( getShape( actual ), [ 2, 2 ], 'returns expected value' ); + t.strictEqual( getOrder( actual ), 'row-major', 'returns expected value' ); + t.notEqual( getData( actual ), getData( x ), 'returns expected value' ); + t.deepEqual( ndarray2array( actual ), expected, 'returns expected value' ); + + t.end(); +}); + +tape( 'the function copies an input ndarray to a new ndarray (dtype=float64, options)', function test( t ) { + var expected; + var actual; + var x; + + x = zeros( 'float64', [ 4 ], 'row-major' ); + + actual = copy( x, { + 'dtype': 'float32', + 'order': 'column-major' + }); + expected = [ 0.0, 0.0, 0.0, 0.0 ]; + + t.strictEqual( instanceOf( actual, ndarray ), true, 'returns expected value' ); + t.strictEqual( isEqualDataType( getDType( actual ), 'float32' ), true, 'returns expected value' ); + t.deepEqual( getShape( actual ), [ 4 ], 'returns expected value' ); + t.strictEqual( instanceOf( getData( actual ), Float32Array ), true, 'returns expected value' ); + t.strictEqual( getOrder( actual ), 'column-major', 'returns expected value' ); + t.notEqual( getData( actual ), getData( x ), 'returns expected value' ); + t.deepEqual( ndarray2array( actual ), expected, 'returns expected value' ); + + t.end(); +}); + +tape( 'the function copies an input ndarray to a new ndarray (dtype=float32, inferred)', function test( t ) { + var expected; + var actual; + var x; + + x = zeros( 'float32', [ 2, 2 ], 'row-major' ); + + actual = copy( x ); + expected = [ [ 0.0, 0.0 ], [ 0.0, 0.0 ] ]; + + t.strictEqual( isEqualDataType( getDType( actual ), getDType( x ) ), true, 'returns expected value' ); + t.deepEqual( getShape( actual ), [ 2, 2 ], 'returns expected value' ); + t.strictEqual( getOrder( actual ), 'row-major', 'returns expected value' ); + t.notEqual( getData( actual ), getData( x ), 'returns expected value' ); + t.deepEqual( ndarray2array( actual ), expected, 'returns expected value' ); + + t.end(); +}); + +tape( 'the function copies an input ndarray to a new ndarray (dtype=float32, options)', function test( t ) { + var expected; + var actual; + var x; + + x = zeros( 'float64', [ 2, 2 ], 'row-major' ); + + actual = copy( x, { + 'dtype': 'float32', + 'order': 'column-major' + }); + expected = [ [ 0.0, 0.0 ], [ 0.0, 0.0 ] ]; + + t.strictEqual( instanceOf( actual, ndarray ), true, 'returns expected value' ); + t.strictEqual( isEqualDataType( getDType( actual ), 'float32' ), true, 'returns expected value' ); + t.deepEqual( getShape( actual ), [ 2, 2 ], 'returns expected value' ); + t.strictEqual( instanceOf( getData( actual ), Float32Array ), true, 'returns expected value' ); + t.strictEqual( getOrder( actual ), 'column-major', 'returns expected value' ); + t.notEqual( getData( actual ), getData( x ), 'returns expected value' ); + t.deepEqual( ndarray2array( actual ), expected, 'returns expected value' ); + + t.end(); +}); + +tape( 'the function copies an input ndarray to a new ndarray (dtype=int32, inferred)', function test( t ) { + var expected; + var actual; + var x; + + x = zeros( 'int32', [ 2, 2 ], 'row-major' ); + + actual = copy( x ); + expected = [ [ 0.0, 0.0 ], [ 0.0, 0.0 ] ]; + + t.strictEqual( isEqualDataType( getDType( actual ), getDType( x ) ), true, 'returns expected value' ); + t.deepEqual( getShape( actual ), [ 2, 2 ], 'returns expected value' ); + t.strictEqual( getOrder( actual ), 'row-major', 'returns expected value' ); + t.notEqual( getData( actual ), getData( x ), 'returns expected value' ); + t.deepEqual( ndarray2array( actual ), expected, 'returns expected value' ); + + t.end(); +}); + +tape( 'the function copies an input ndarray to a new ndarray (dtype=int32, options)', function test( t ) { + var expected; + var actual; + var x; + + x = zeros( 'int16', [ 2, 2 ], 'row-major' ); + + actual = copy( x, { + 'dtype': 'int32', + 'order': 'column-major' + }); + expected = [ [ 0.0, 0.0 ], [ 0.0, 0.0 ] ]; + + t.strictEqual( instanceOf( actual, ndarray ), true, 'returns expected value' ); + t.strictEqual( isEqualDataType( getDType( actual ), 'int32' ), true, 'returns expected value' ); + t.deepEqual( getShape( actual ), [ 2, 2 ], 'returns expected value' ); + t.strictEqual( instanceOf( getData( actual ), Int32Array ), true, 'returns expected value' ); + t.strictEqual( getOrder( actual ), 'column-major', 'returns expected value' ); + t.notEqual( getData( actual ), getData( x ), 'returns expected value' ); + t.deepEqual( ndarray2array( actual ), expected, 'returns expected value' ); + + t.end(); +}); + +tape( 'the function copies an input ndarray to a new ndarray (dtype=int16, inferred)', function test( t ) { + var expected; + var actual; + var x; + + x = zeros( 'int16', [ 2, 2 ], 'row-major' ); + + actual = copy( x ); + expected = [ [ 0.0, 0.0 ], [ 0.0, 0.0 ] ]; + + t.strictEqual( isEqualDataType( getDType( actual ), getDType( x ) ), true, 'returns expected value' ); + t.deepEqual( getShape( actual ), [ 2, 2 ], 'returns expected value' ); + t.strictEqual( getOrder( actual ), 'row-major', 'returns expected value' ); + t.notEqual( getData( actual ), getData( x ), 'returns expected value' ); + t.deepEqual( ndarray2array( actual ), expected, 'returns expected value' ); + + t.end(); +}); + +tape( 'the function copies an input ndarray to a new ndarray (dtype=int16, options)', function test( t ) { + var expected; + var actual; + var x; + + x = zeros( 'int8', [ 2, 2 ], 'row-major' ); + + actual = copy( x, { + 'dtype': 'int16', + 'order': 'column-major' + }); + expected = [ [ 0.0, 0.0 ], [ 0.0, 0.0 ] ]; + + t.strictEqual( instanceOf( actual, ndarray ), true, 'returns expected value' ); + t.strictEqual( isEqualDataType( getDType( actual ), 'int16' ), true, 'returns expected value' ); + t.deepEqual( getShape( actual ), [ 2, 2 ], 'returns expected value' ); + t.strictEqual( instanceOf( getData( actual ), Int16Array ), true, 'returns expected value' ); + t.strictEqual( getOrder( actual ), 'column-major', 'returns expected value' ); + t.notEqual( getData( actual ), getData( x ), 'returns expected value' ); + t.deepEqual( ndarray2array( actual ), expected, 'returns expected value' ); + + t.end(); +}); + +tape( 'the function copies an input ndarray to a new ndarray (dtype=int8, inferred)', function test( t ) { + var expected; + var actual; + var x; + + x = zeros( 'int8', [ 2, 2 ], 'row-major' ); + + actual = copy( x ); + expected = [ [ 0.0, 0.0 ], [ 0.0, 0.0 ] ]; + + t.strictEqual( isEqualDataType( getDType( actual ), getDType( x ) ), true, 'returns expected value' ); + t.deepEqual( getShape( actual ), [ 2, 2 ], 'returns expected value' ); + t.strictEqual( getOrder( actual ), 'row-major', 'returns expected value' ); + t.notEqual( getData( actual ), getData( x ), 'returns expected value' ); + t.deepEqual( ndarray2array( actual ), expected, 'returns expected value' ); + + t.end(); +}); + +tape( 'the function copies an input ndarray to a new ndarray (dtype=int8, options)', function test( t ) { + var expected; + var actual; + var x; + + x = zeros( 'int8', [ 2, 2 ], 'row-major' ); + + actual = copy( x, { + 'dtype': 'generic', + 'order': 'column-major' + }); + expected = [ [ 0.0, 0.0 ], [ 0.0, 0.0 ] ]; + + t.strictEqual( instanceOf( actual, ndarray ), true, 'returns expected value' ); + t.strictEqual( isEqualDataType( getDType( actual ), 'generic' ), true, 'returns expected value' ); + t.deepEqual( getShape( actual ), [ 2, 2 ], 'returns expected value' ); + t.strictEqual( getOrder( actual ), 'column-major', 'returns expected value' ); + t.notEqual( getData( actual ), getData( x ), 'returns expected value' ); + t.deepEqual( ndarray2array( actual ), expected, 'returns expected value' ); + + t.end(); +}); + +tape( 'the function copies an input ndarray to a new ndarray (dtype=uint32, inferred)', function test( t ) { + var expected; + var actual; + var x; + + x = zeros( 'uint32', [ 2, 2 ], 'row-major' ); + + actual = copy( x ); + expected = [ [ 0.0, 0.0 ], [ 0.0, 0.0 ] ]; + + t.strictEqual( isEqualDataType( getDType( actual ), getDType( x ) ), true, 'returns expected value' ); + t.deepEqual( getShape( actual ), [ 2, 2 ], 'returns expected value' ); + t.strictEqual( getOrder( actual ), 'row-major', 'returns expected value' ); + t.notEqual( getData( actual ), getData( x ), 'returns expected value' ); + t.deepEqual( ndarray2array( actual ), expected, 'returns expected value' ); + + t.end(); +}); + +tape( 'the function copies an input ndarray to a new ndarray (dtype=uint32, options)', function test( t ) { + var expected; + var actual; + var x; + + x = zeros( 'uint16', [ 2, 2 ], 'row-major' ); + + actual = copy( x, { + 'dtype': 'uint32', + 'order': 'column-major' + }); + expected = [ [ 0.0, 0.0 ], [ 0.0, 0.0 ] ]; + + t.strictEqual( instanceOf( actual, ndarray ), true, 'returns expected value' ); + t.strictEqual( isEqualDataType( getDType( actual ), 'uint32' ), true, 'returns expected value' ); + t.deepEqual( getShape( actual ), [ 2, 2 ], 'returns expected value' ); + t.strictEqual( instanceOf( getData( actual ), Uint32Array ), true, 'returns expected value' ); + t.strictEqual( getOrder( actual ), 'column-major', 'returns expected value' ); + t.notEqual( getData( actual ), getData( x ), 'returns expected value' ); + t.deepEqual( ndarray2array( actual ), expected, 'returns expected value' ); + + t.end(); +}); + +tape( 'the function copies an input ndarray to a new ndarray (dtype=uint16, inferred)', function test( t ) { + var expected; + var actual; + var x; + + x = zeros( 'uint16', [ 2, 2 ], 'row-major' ); + + actual = copy( x ); + expected = [ [ 0.0, 0.0 ], [ 0.0, 0.0 ] ]; + + t.strictEqual( isEqualDataType( getDType( actual ), getDType( x ) ), true, 'returns expected value' ); + t.deepEqual( getShape( actual ), [ 2, 2 ], 'returns expected value' ); + t.strictEqual( getOrder( actual ), 'row-major', 'returns expected value' ); + t.notEqual( getData( actual ), getData( x ), 'returns expected value' ); + t.deepEqual( ndarray2array( actual ), expected, 'returns expected value' ); + + t.end(); +}); + +tape( 'the function copies an input ndarray to a new ndarray (dtype=uint16, options)', function test( t ) { + var expected; + var actual; + var x; + + x = zeros( 'uint8', [ 2, 2 ], 'row-major' ); + + actual = copy( x, { + 'dtype': 'uint16', + 'order': 'column-major' + }); + expected = [ [ 0.0, 0.0 ], [ 0.0, 0.0 ] ]; + + t.strictEqual( instanceOf( actual, ndarray ), true, 'returns expected value' ); + t.strictEqual( isEqualDataType( getDType( actual ), 'uint16' ), true, 'returns expected value' ); + t.deepEqual( getShape( actual ), [ 2, 2 ], 'returns expected value' ); + t.strictEqual( instanceOf( getData( actual ), Uint16Array ), true, 'returns expected value' ); + t.strictEqual( getOrder( actual ), 'column-major', 'returns expected value' ); + t.notEqual( getData( actual ), getData( x ), 'returns expected value' ); + t.deepEqual( ndarray2array( actual ), expected, 'returns expected value' ); + + t.end(); +}); + +tape( 'the function copies an input ndarray to a new ndarray (dtype=uint8, inferred)', function test( t ) { + var expected; + var actual; + var x; + + x = zeros( 'uint8', [ 2, 2 ], 'row-major' ); + + actual = copy( x ); + expected = [ [ 0.0, 0.0 ], [ 0.0, 0.0 ] ]; + + t.strictEqual( isEqualDataType( getDType( actual ), getDType( x ) ), true, 'returns expected value' ); + t.deepEqual( getShape( actual ), [ 2, 2 ], 'returns expected value' ); + t.strictEqual( getOrder( actual ), 'row-major', 'returns expected value' ); + t.notEqual( getData( actual ), getData( x ), 'returns expected value' ); + t.deepEqual( ndarray2array( actual ), expected, 'returns expected value' ); + + t.end(); +}); + +tape( 'the function copies an input ndarray to a new ndarray (dtype=uint8, options)', function test( t ) { + var expected; + var actual; + var x; + + x = zeros( 'uint8', [ 2, 2 ], 'row-major' ); + + actual = copy( x, { + 'dtype': 'generic', + 'order': 'column-major' + }); + expected = [ [ 0.0, 0.0 ], [ 0.0, 0.0 ] ]; + + t.strictEqual( instanceOf( actual, ndarray ), true, 'returns expected value' ); + t.strictEqual( isEqualDataType( getDType( actual ), 'generic' ), true, 'returns expected value' ); + t.deepEqual( getShape( actual ), [ 2, 2 ], 'returns expected value' ); + t.strictEqual( getOrder( actual ), 'column-major', 'returns expected value' ); + t.notEqual( getData( actual ), getData( x ), 'returns expected value' ); + t.deepEqual( ndarray2array( actual ), expected, 'returns expected value' ); + + t.end(); +}); + +tape( 'the function copies an input ndarray to a new ndarray (dtype=uint8c, inferred)', function test( t ) { + var expected; + var actual; + var x; + + x = zeros( 'uint8c', [ 2, 2 ], 'row-major' ); + + actual = copy( x ); + expected = [ [ 0.0, 0.0 ], [ 0.0, 0.0 ] ]; + + t.strictEqual( isEqualDataType( getDType( actual ), getDType( x ) ), true, 'returns expected value' ); + t.deepEqual( getShape( actual ), [ 2, 2 ], 'returns expected value' ); + t.strictEqual( getOrder( actual ), 'row-major', 'returns expected value' ); + t.notEqual( getData( actual ), getData( x ), 'returns expected value' ); + t.deepEqual( ndarray2array( actual ), expected, 'returns expected value' ); + + t.end(); +}); + +tape( 'the function copies an input ndarray to a new ndarray (dtype=uint8c, options)', function test( t ) { + var expected; + var actual; + var x; + + x = zeros( 'uint8c', [ 2, 2 ], 'row-major' ); + + actual = copy( x, { + 'dtype': 'generic', + 'order': 'column-major' + }); + expected = [ [ 0.0, 0.0 ], [ 0.0, 0.0 ] ]; + + t.strictEqual( instanceOf( actual, ndarray ), true, 'returns expected value' ); + t.strictEqual( isEqualDataType( getDType( actual ), 'generic' ), true, 'returns expected value' ); + t.deepEqual( getShape( actual ), [ 2, 2 ], 'returns expected value' ); + t.strictEqual( getOrder( actual ), 'column-major', 'returns expected value' ); + t.notEqual( getData( actual ), getData( x ), 'returns expected value' ); + t.deepEqual( ndarray2array( actual ), expected, 'returns expected value' ); + + t.end(); +}); + +tape( 'the function copies an input ndarray to a new ndarray (dtype=complex128, inferred)', function test( t ) { + var actual; + var x; + + x = zeros( 'complex128', [ 2, 2 ], 'row-major' ); + + actual = copy( x ); + + t.strictEqual( isEqualDataType( getDType( actual ), getDType( x ) ), true, 'returns expected value' ); + t.deepEqual( getShape( actual ), [ 2, 2 ], 'returns expected value' ); + t.strictEqual( getOrder( actual ), 'row-major', 'returns expected value' ); + t.notEqual( getData( actual ), getData( x ), 'returns expected value' ); + t.deepEqual( getData( actual ), getData( x ), 'returns expected value' ); + + t.end(); +}); + +tape( 'the function copies an input ndarray to a new ndarray (dtype=complex128, options)', function test( t ) { + var actual; + var x; + + x = zeros( 'complex64', [ 2, 2 ], 'row-major' ); + + actual = copy( x, { + 'dtype': 'complex128', + 'order': 'column-major' + }); + + t.strictEqual( instanceOf( actual, ndarray ), true, 'returns expected value' ); + t.strictEqual( isEqualDataType( getDType( actual ), 'complex128' ), true, 'returns expected value' ); + t.deepEqual( getShape( actual ), [ 2, 2 ], 'returns expected value' ); + t.strictEqual( instanceOf( getData( actual ), Complex128Array ), true, 'returns expected value' ); + t.strictEqual( getOrder( actual ), 'column-major', 'returns expected value' ); + t.notEqual( getData( actual ), getData( x ), 'returns expected value' ); + t.deepEqual( getData( actual ), getData( x ), 'returns expected value' ); + + t.end(); +}); + +tape( 'the function copies an input ndarray to a new ndarray (dtype=complex64, inferred)', function test( t ) { + var actual; + var x; + + x = zeros( 'complex64', [ 2, 2 ], 'row-major' ); + + actual = copy( x ); + + t.strictEqual( isEqualDataType( getDType( actual ), getDType( x ) ), true, 'returns expected value' ); + t.deepEqual( getShape( actual ), [ 2, 2 ], 'returns expected value' ); + t.strictEqual( getOrder( actual ), 'row-major', 'returns expected value' ); + t.notEqual( getData( actual ), getData( x ), 'returns expected value' ); + t.deepEqual( getData( actual ), getData( x ), 'returns expected value' ); + + t.end(); +}); + +tape( 'the function copies an input ndarray to a new ndarray (dtype=complex64, options)', function test( t ) { + var actual; + var x; + + x = zeros( 'complex128', [ 2, 2 ], 'row-major' ); + + actual = copy( x, { + 'shape': [ 2, 2 ], + 'dtype': 'complex64', + 'order': 'column-major' + }); + + t.strictEqual( instanceOf( actual, ndarray ), true, 'returns expected value' ); + t.strictEqual( isEqualDataType( getDType( actual ), 'complex64' ), true, 'returns expected value' ); + t.deepEqual( getShape( actual ), [ 2, 2 ], 'returns expected value' ); + t.strictEqual( instanceOf( getData( actual ), Complex64Array ), true, 'returns expected value' ); + t.strictEqual( getOrder( actual ), 'column-major', 'returns expected value' ); + t.notEqual( getData( actual ), getData( x ), 'returns expected value' ); + t.deepEqual( getData( actual ), getData( x ), 'returns expected value' ); + + t.end(); +}); + +tape( 'the function copies an input ndarray to a new ndarray (dtype=bool, inferred)', function test( t ) { + var expected; + var actual; + var x; + + x = zeros( 'bool', [ 2, 2 ], 'row-major' ); + + actual = copy( x ); + expected = [ [ false, false ], [ false, false ] ]; + + t.strictEqual( isEqualDataType( getDType( actual ), getDType( x ) ), true, 'returns expected value' ); + t.deepEqual( getShape( actual ), [ 2, 2 ], 'returns expected value' ); + t.strictEqual( getOrder( actual ), 'row-major', 'returns expected value' ); + t.notEqual( getData( actual ), getData( x ), 'returns expected value' ); + t.deepEqual( ndarray2array( actual ), expected, 'returns expected value' ); + + t.end(); +}); + +tape( 'the function copies an input ndarray to a new ndarray (dtype=bool, options)', function test( t ) { + var expected; + var actual; + var x; + + x = zeros( 'bool', [ 2, 2 ], 'row-major' ); + + actual = copy( x, { + 'order': 'column-major' + }); + expected = [ [ false, false ], [ false, false ] ]; + + t.strictEqual( instanceOf( actual, ndarray ), true, 'returns expected value' ); + t.strictEqual( isEqualDataType( getDType( actual ), 'bool' ), true, 'returns expected value' ); + t.deepEqual( getShape( actual ), [ 2, 2 ], 'returns expected value' ); + t.strictEqual( instanceOf( getData( actual ), BooleanArray ), true, 'returns expected value' ); + t.strictEqual( getOrder( actual ), 'column-major', 'returns expected value' ); + t.notEqual( getData( actual ), getData( x ), 'returns expected value' ); + t.deepEqual( ndarray2array( actual ), expected, 'returns expected value' ); + + t.end(); +}); + +tape( 'the function supports zero-dimensional arrays', function test( t ) { + var actual; + var x; + + x = { + 'dtype': 'generic', + 'ndims': 0, + 'length': 0, + 'data': [ 0 ], + 'shape': [], + 'strides': [ 0 ], + 'offset': 0, + 'order': 'row-major', + 'flags': {}, + 'get': noop, + 'set': noop + }; + actual = copy( x ); + + t.strictEqual( instanceOf( actual, ndarray ), true, 'returns expected value' ); + t.strictEqual( getDType( actual ), 'generic', 'returns expected value' ); + t.deepEqual( getShape( actual ), getShape( x ), 'returns expected value' ); + t.strictEqual( instanceOf( getData( actual ), Array ), true, 'returns expected value' ); + t.notEqual( getData( actual ), getData( x ), 'returns expected value' ); + + t.end(); + + function noop() {} +}); + +tape( 'the function supports empty arrays', function test( t ) { + var expected; + var actual; + var x; + + expected = []; + + x = { + 'dtype': 'generic', + 'ndims': 3, + 'length': 0, + 'data': [], + 'shape': [ 2, 0, 2 ], + 'strides': [ 0, 2, 1 ], + 'offset': 0, + 'order': 'row-major', + 'flags': {}, + 'get': noop, + 'set': noop + }; + actual = copy( x ); + + t.strictEqual( instanceOf( actual, ndarray ), true, 'returns expected value' ); + t.strictEqual( getDType( actual ), 'generic', 'returns expected value' ); + t.deepEqual( getShape( actual ), [ 2, 0, 2 ], 'returns expected value' ); + t.strictEqual( instanceOf( getData( actual ), Array ), true, 'returns expected value' ); + t.notEqual( getData( actual ), getData( x ), 'returns expected value' ); + t.deepEqual( ndarray2array( actual ), expected, 'returns expected value' ); + t.strictEqual( getOrder( actual ), 'row-major', 'returns expected value' ); + + t.end(); + + function noop() {} +}); + +tape( 'the function supports specifying array index modes and submodes', function test( t ) { + var expected; + var actual; + var opts; + var x; + + opts = { + 'mode': 'clamp', + 'submode': [ 'wrap' ] + }; + x = zeros( 'generic', [ 2, 2, 2 ], 'row-major' ); + + actual = copy( x, opts ); + expected = [ [ [ 0, 0 ], [ 0, 0 ] ], [ [ 0, 0 ], [ 0, 0 ] ] ]; + + t.strictEqual( instanceOf( actual, ndarray ), true, 'returns expected value' ); + t.strictEqual( getDType( actual ), 'generic', 'returns expected value' ); + t.deepEqual( getShape( actual ), [ 2, 2, 2 ], 'returns expected value' ); + t.strictEqual( instanceOf( getData( actual ), Array ), true, 'returns expected value' ); + t.notEqual( getData( actual ), getData( x ), 'returns expected value' ); + t.deepEqual( ndarray2array( actual ), expected, 'returns expected value' ); + t.strictEqual( getOrder( actual ), 'row-major', 'returns expected value' ); + + t.strictEqual( actual.iget( actual.length+10 ), 0, 'returns expected value' ); + actual.iset( actual.length+10, 1 ); + t.strictEqual( actual.iget( actual.length+10 ), 1, 'returns expected value' ); + + t.strictEqual( actual.get( 2, 2, 2 ), 0, 'returns expected value' ); + actual.set( 2, 2, 2, 3 ); + t.strictEqual( actual.get( 0, 0, 0 ), 3, 'returns expected value' ); + t.strictEqual( actual.get( 2, 2, 2 ), 3, 'returns expected value' ); + + t.end(); +}); From ca4e0db0f330e00f21c426c67026727d9c8110fe Mon Sep 17 00:00:00 2001 From: Athan Date: Tue, 11 Nov 2025 23:01:37 -0800 Subject: [PATCH 2/3] chore: clean-up --- type: pre_commit_static_analysis_report description: Results of running static analysis checks when committing changes. report: - task: lint_filenames status: passed - task: lint_editorconfig status: passed - task: lint_markdown status: passed - task: lint_package_json status: passed - task: lint_repl_help status: passed - task: lint_javascript_src status: passed - task: lint_javascript_cli status: na - task: lint_javascript_examples status: passed - task: lint_javascript_tests status: passed - task: lint_javascript_benchmarks status: passed - task: lint_python status: na - task: lint_r status: na - task: lint_c_src status: na - task: lint_c_examples status: na - task: lint_c_benchmarks status: na - task: lint_c_tests_fixtures status: na - task: lint_shell status: na - task: lint_typescript_declarations status: passed - task: lint_typescript_tests status: passed - task: lint_license_headers status: passed --- --- .../@stdlib/ndarray/copy/README.md | 21 +--- .../ndarray/copy/benchmark/benchmark.js | 4 +- .../ndarray/copy/benchmark/benchmark.size.js | 109 ++++++++++++++++++ .../@stdlib/ndarray/copy/docs/repl.txt | 2 +- .../ndarray/copy/docs/types/index.d.ts | 55 ++++++++- .../@stdlib/ndarray/copy/docs/types/test.ts | 2 +- .../@stdlib/ndarray/copy/examples/index.js | 17 +-- .../@stdlib/ndarray/copy/lib/main.js | 20 ++-- .../@stdlib/ndarray/copy/package.json | 4 +- .../@stdlib/ndarray/copy/test/test.js | 99 +++++++++++----- 10 files changed, 257 insertions(+), 76 deletions(-) create mode 100644 lib/node_modules/@stdlib/ndarray/copy/benchmark/benchmark.size.js diff --git a/lib/node_modules/@stdlib/ndarray/copy/README.md b/lib/node_modules/@stdlib/ndarray/copy/README.md index 7f2e73164546..b11301f37f6f 100644 --- a/lib/node_modules/@stdlib/ndarray/copy/README.md +++ b/lib/node_modules/@stdlib/ndarray/copy/README.md @@ -75,7 +75,7 @@ var zeros = require( '@stdlib/ndarray/zeros' ); var x = zeros( [ 2, 2 ] ); // returns -var dt = getDtype( x ); +var dt = String( getDtype( x ) ); // returns 'float64' var y = copy( x, { @@ -86,7 +86,7 @@ var y = copy( x, { var sh = getShape( y ); // returns [ 2, 2 ] -dt = getDtype( y ); +dt = String( getDtype( y ) ); // returns 'float32' ``` @@ -115,26 +115,17 @@ dt = getDtype( y ); ```javascript -var bernoulli = require( '@stdlib/random/array/bernoulli' ); -var array = require( '@stdlib/ndarray/array' ); +var uniform = require( '@stdlib/random/uniform' ); var ndarray2array = require( '@stdlib/ndarray/to-array' ); var copy = require( '@stdlib/ndarray/copy' ); -var xbuf = bernoulli( 10, 0.9, { +var x = uniform( [ 5, 2 ], -10.0, 10.0, { 'dtype': 'generic' }); -var x = array({ - 'dtype': 'generic', - 'data': xbuf, - 'shape': [ 5, 2 ], - 'strides': [ 2, 1 ], - 'offset': 0, - 'order': 'row-major' -}); console.log( ndarray2array( x ) ); -var o = copy( x ); -console.log( ndarray2array( o ) ); +var y = copy( x ); +console.log( ndarray2array( y ) ); ``` diff --git a/lib/node_modules/@stdlib/ndarray/copy/benchmark/benchmark.js b/lib/node_modules/@stdlib/ndarray/copy/benchmark/benchmark.js index ba844e7e54f2..94275f24c0c9 100644 --- a/lib/node_modules/@stdlib/ndarray/copy/benchmark/benchmark.js +++ b/lib/node_modules/@stdlib/ndarray/copy/benchmark/benchmark.js @@ -35,10 +35,10 @@ bench( pkg, function benchmark( b ) { var i; values = [ - zeros( [ 10 ] ), + zeros( [ 25 ] ), zeros( [ 5, 5 ] ), zeros( [ 3, 3, 3 ] ), - zeros( [ 2, 2, 2, 2 ] ) + zeros( [ 2, 2, 2, 3 ] ) ]; b.tic(); diff --git a/lib/node_modules/@stdlib/ndarray/copy/benchmark/benchmark.size.js b/lib/node_modules/@stdlib/ndarray/copy/benchmark/benchmark.size.js new file mode 100644 index 000000000000..7818e77e5e55 --- /dev/null +++ b/lib/node_modules/@stdlib/ndarray/copy/benchmark/benchmark.size.js @@ -0,0 +1,109 @@ +/** +* @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 pow = require( '@stdlib/math/base/special/pow' ); +var isndarrayLike = require( '@stdlib/assert/is-ndarray-like' ); +var zeros = require( '@stdlib/ndarray/zeros' ); +var dtypes = require( '@stdlib/ndarray/dtypes' ); +var pkg = require( './../package.json' ).name; +var copy = require( './../lib' ); + + +// VARIABLES // + +var DTYPES = dtypes(); + + +// FUNCTIONS // + +/** +* Creates a benchmark function. +* +* @private +* @param {PositiveInteger} len - array length +* @param {*} dtype - data type +* @returns {Function} benchmark function +*/ +function createBenchmark( len, dtype ) { + var x = zeros( [ len ], { + 'dtype': dtype + }); + return benchmark; + + /** + * Benchmark function. + * + * @private + * @param {Benchmark} b - benchmark instance + */ + function benchmark( b ) { + var arr; + var i; + + b.tic(); + for ( i = 0; i < b.iterations; i++ ) { + arr = copy( x ); + if ( arr.length !== len ) { + b.fail( 'unexpected length' ); + } + } + b.toc(); + if ( !isndarrayLike( arr ) ) { + b.fail( 'should return an ndarray' ); + } + b.pass( 'benchmark finished' ); + b.end(); + } +} + + +// MAIN // + +/** +* Main execution sequence. +* +* @private +*/ +function main() { + var len; + var min; + var max; + var dt; + var f; + var i; + var j; + + min = 1; // 10^min + max = 6; // 10^max + + for ( j = 0; j < DTYPES.length; j++ ) { + dt = DTYPES[ j ]; + for ( i = min; i <= max; i++ ) { + len = pow( 10, i ); + f = createBenchmark( len, dt ); + bench( pkg+':dtype='+String( dt )+',size='+len, f ); + } + } +} + +main(); diff --git a/lib/node_modules/@stdlib/ndarray/copy/docs/repl.txt b/lib/node_modules/@stdlib/ndarray/copy/docs/repl.txt index d558330d46fc..56a788b840f1 100644 --- a/lib/node_modules/@stdlib/ndarray/copy/docs/repl.txt +++ b/lib/node_modules/@stdlib/ndarray/copy/docs/repl.txt @@ -57,7 +57,7 @@ Examples -------- - > var x = {{alias:@stdlib/ndarray/base/zeros}}( 'float64', [ 2, 2 ], 'row-major' ) + > var x = {{alias:@stdlib/ndarray/zeros}}( [ 2, 2 ] ) > var sh = {{alias:@stdlib/ndarray/shape}}( x ) [ 2, 2 ] diff --git a/lib/node_modules/@stdlib/ndarray/copy/docs/types/index.d.ts b/lib/node_modules/@stdlib/ndarray/copy/docs/types/index.d.ts index 05dcd7c0bc0a..1640e865412a 100644 --- a/lib/node_modules/@stdlib/ndarray/copy/docs/types/index.d.ts +++ b/lib/node_modules/@stdlib/ndarray/copy/docs/types/index.d.ts @@ -25,9 +25,9 @@ import { Order, ndarray, DataType, Mode } from '@stdlib/types/ndarray'; /** -* Interface describing function options. +* Interface describing "base" function options. */ -interface Options { +interface BaseOptions { /** * Underlying data type. * @@ -57,6 +57,55 @@ interface Options { submode?: Array; } +/** +* Interface describing function options. +*/ +interface Options extends BaseOptions { + /** + * Underlying data type. + * + * ## Notes + * + * - This option overrides the input array's inferred data type. + */ + dtype: DataType; +} + +/** +* Copies an input ndarray to a new ndarray having the same shape and data type. +* +* ## Notes +* +* - The function performs a full copy in which an ndarray's underlying data is copied to a new underlying data buffer. +* +* @param x - input array +* @param options - options +* @param options.dtype - output array data type +* @param options.order - specifies whether the output array is 'row-major' (C-style) or 'column-major' (Fortran-style) +* @param options.mode - specifies how to handle a linear index which exceeds array dimensions +* @param options.submode - specifies how to handle subscripts which exceed array dimensions on a per dimension basis +* @returns output array +* +* @example +* var getShape = require( '@stdlib/ndarray/shape' ); +* var zeros = require( '@stdlib/ndarray/zeros' ); +* +* var x = zeros( [ 2, 2 ] ); +* // returns +* +* var sh = getShape( x ); +* // returns [ 2, 2 ] +* +* var y = copy( x, { +* 'dtype': 'float32' +* }); +* // returns +* +* sh = getShape( y ); +* // returns [ 2, 2 ] +*/ +declare function copy( x: T, options: Options ): ndarray; // FIXME: we lose type specificity here. We should leverage the ndarray type map to ensure we resolve the correct output array type. + /** * Copies an input ndarray to a new ndarray having the same shape and data type. * @@ -87,7 +136,7 @@ interface Options { * sh = getShape( y ); * // returns [ 2, 2 ] */ -declare function copy( x: T, options?: Options ): T; +declare function copy( x: T, options?: BaseOptions ): T; // EXPORTS // diff --git a/lib/node_modules/@stdlib/ndarray/copy/docs/types/test.ts b/lib/node_modules/@stdlib/ndarray/copy/docs/types/test.ts index 654d73b87f5c..b25a91149849 100644 --- a/lib/node_modules/@stdlib/ndarray/copy/docs/types/test.ts +++ b/lib/node_modules/@stdlib/ndarray/copy/docs/types/test.ts @@ -27,7 +27,7 @@ import copy = require( './index' ); copy( zeros( [ 2, 2 ] ) ); // $ExpectType float64ndarray copy( zeros( [ 2, 2 ] ), {} ); // $ExpectType float64ndarray copy( zeros( [ 2, 2 ] ), { 'order': 'column-major' } ); // $ExpectType float64ndarray - copy( zeros( [ 2, 2] ), { 'dtype': 'float64' } ); // $ExpectType float64ndarray + copy( zeros( [ 2, 2] ), { 'dtype': 'float64' } ); // $ExpectType ndarray } // The compiler throws an error if the function is provided a first argument which is not an ndarray-like object... diff --git a/lib/node_modules/@stdlib/ndarray/copy/examples/index.js b/lib/node_modules/@stdlib/ndarray/copy/examples/index.js index 8160908faa0d..5d5e32c01667 100644 --- a/lib/node_modules/@stdlib/ndarray/copy/examples/index.js +++ b/lib/node_modules/@stdlib/ndarray/copy/examples/index.js @@ -18,23 +18,14 @@ 'use strict'; -var bernoulli = require( '@stdlib/random/array/bernoulli' ); -var array = require( '@stdlib/ndarray/array' ); +var uniform = require( '@stdlib/random/uniform' ); var ndarray2array = require( '@stdlib/ndarray/to-array' ); var copy = require( './../lib' ); -var xbuf = bernoulli( 10, 0.9, { +var x = uniform( [ 5, 2 ], -10.0, 10.0, { 'dtype': 'generic' }); -var x = array({ - 'dtype': 'generic', - 'data': xbuf, - 'shape': [ 5, 2 ], - 'strides': [ 2, 1 ], - 'offset': 0, - 'order': 'row-major' -}); console.log( ndarray2array( x ) ); -var o = copy( x ); -console.log( ndarray2array( o ) ); +var y = copy( x ); +console.log( ndarray2array( y ) ); diff --git a/lib/node_modules/@stdlib/ndarray/copy/lib/main.js b/lib/node_modules/@stdlib/ndarray/copy/lib/main.js index d86d590df3b9..22e17edbf6c5 100644 --- a/lib/node_modules/@stdlib/ndarray/copy/lib/main.js +++ b/lib/node_modules/@stdlib/ndarray/copy/lib/main.js @@ -35,20 +35,18 @@ var format = require( '@stdlib/string/format' ); // MAIN // /** -* Copy an input ndarray to a new ndarray having the same shape and data type. +* Copies an input ndarray to a new ndarray having the same shape and data type. * * @param {ndarray} x - input array * @param {Options} [options] - function options -* @param {string} [options.dtype] - output array data type (overrides the input array's inferred data type) +* @param {*} [options.dtype] - output array data type (overrides the input array's inferred data type) * @param {string} [options.order] - specifies whether the output array should be 'row-major' (C-style) or 'column-major' (Fortran-style) (overrides the input array's inferred order) -* @param {(NonNegativeIntegerArray|NonNegativeInteger)} [options.shape] - output array shape (overrides the input array's inferred shape) * @param {string} [options.mode="throw"] - specifies how to handle indices which exceed array dimensions * @param {StringArray} [options.submode=["throw"]] - specifies how to handle subscripts which exceed array dimensions on a per dimension basis * @throws {TypeError} first argument must be an ndarray-like object * @throws {TypeError} options argument must be an object * @throws {TypeError} `dtype` option must be a supported ndarray data type * @throws {TypeError} `order` option must be a supported order -* @throws {TypeError} `shape` option must be either a nonnegative integer or an array of nonnegative integers * @throws {TypeError} must provide valid options * @returns {ndarray} ndarray * @@ -69,25 +67,26 @@ function copy( x ) { var options; var opts; var sh; + var dt; var o; if ( !isndarrayLike( x ) ) { throw new TypeError( format( 'invalid argument. First argument must be an ndarray-like object. Value: `%s`.', x ) ); } - // Resolve function options: opts = {}; if ( arguments.length > 1 ) { options = arguments[ 1 ]; if ( !isPlainObject( options ) ) { throw new TypeError( format( 'invalid argument. Options argument must be an object. Value: `%s`.', options ) ); } + dt = getDType( x ); if ( hasOwnProp( options, 'dtype' ) ) { - if ( !isMostlySafeCast( getDType( x ), options.dtype ) ) { - throw new TypeError( format( 'invalid option. `%s` option must be safely castable. Option: `%s`.', 'dtype', options.dtype ) ); + if ( !isMostlySafeCast( dt, options.dtype ) ) { + throw new TypeError( format( 'invalid option. First argument cannot be safely cast to the specified data type. Input data type: %s. Option: `%s`.', String( dt ), String( options.dtype ) ) ); } opts.dtype = options.dtype; } else { - opts.dtype = getDType( x ); + opts.dtype = dt; } if ( hasOwnProp( options, 'order' ) ) { opts.order = options.order; @@ -106,9 +105,12 @@ function copy( x ) { } sh = getShape( x ); - // Create a copy of the input ndarray: + // Initialize an output array: o = empty( sh, opts ); + + // Copy the input array to the output array: assign( [ x, o ] ); + return o; } diff --git a/lib/node_modules/@stdlib/ndarray/copy/package.json b/lib/node_modules/@stdlib/ndarray/copy/package.json index 753d7c2cf031..60084f9438da 100644 --- a/lib/node_modules/@stdlib/ndarray/copy/package.json +++ b/lib/node_modules/@stdlib/ndarray/copy/package.json @@ -58,6 +58,8 @@ "ndarray", "matrix", "copy", - "duplicate" + "duplicate", + "cast", + "convert" ] } diff --git a/lib/node_modules/@stdlib/ndarray/copy/test/test.js b/lib/node_modules/@stdlib/ndarray/copy/test/test.js index c33d2634b310..1cd465ced3ee 100644 --- a/lib/node_modules/@stdlib/ndarray/copy/test/test.js +++ b/lib/node_modules/@stdlib/ndarray/copy/test/test.js @@ -21,7 +21,10 @@ // MODULES // var tape = require( 'tape' ); +var isSameComplex128Array = require( '@stdlib/assert/is-same-complex128array' ); +var isSameComplex64Array = require( '@stdlib/assert/is-same-complex64array' ); var isEqualDataType = require( '@stdlib/ndarray/base/assert/is-equal-data-type' ); +var Float64Array = require( '@stdlib/array/float64' ); var Float32Array = require( '@stdlib/array/float32' ); var Int32Array = require( '@stdlib/array/int32' ); var Uint32Array = require( '@stdlib/array/uint32' ); @@ -33,6 +36,8 @@ var BooleanArray = require( '@stdlib/array/bool' ); var instanceOf = require( '@stdlib/assert/instance-of' ); var ndarray = require( '@stdlib/ndarray/ctor' ); var zeros = require( '@stdlib/ndarray/base/zeros' ); +var empty = require( '@stdlib/ndarray/base/empty' ); +var uniform = require( '@stdlib/random/uniform' ); var getShape = require( '@stdlib/ndarray/shape' ); var getOrder = require( '@stdlib/ndarray/order' ); var getData = require( '@stdlib/ndarray/data-buffer' ); @@ -168,6 +173,34 @@ tape( 'the function throws an error if provided a `dtype` option which is not a } }); +tape( 'the function throws an error if provided a first argument which cannot be safely cast to a specified `dtype` option', function test( t ) { + var values; + var x; + var i; + + x = empty( 'bool', [ 2, 2 ], 'row-major' ); + + values = [ + 'float64', + 'float32', + 'complex128', + 'int8' + ]; + + 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() { + copy( x, { + 'dtype': value + }); + }; + } +}); + tape( 'the function throws an error if provided an `order` option which is not a recognized order', function test( t ) { var values; var i; @@ -258,20 +291,20 @@ tape( 'the function throws an error if provided an invalid `submode` option', fu }); tape( 'the function copies an input ndarray to a new ndarray (dtype=float64, inferred)', function test( t ) { - var expected; var actual; var x; - x = zeros( 'float64', [ 2, 2 ], 'row-major' ); + x = uniform( [ 2, 2 ], -10.0, 10.0, { + 'dtype': 'float64' + }); actual = copy( x ); - expected = [ [ 0.0, 0.0 ], [ 0.0, 0.0 ] ]; t.strictEqual( isEqualDataType( getDType( actual ), getDType( x ) ), true, 'returns expected value' ); t.deepEqual( getShape( actual ), [ 2, 2 ], 'returns expected value' ); t.strictEqual( getOrder( actual ), 'row-major', 'returns expected value' ); t.notEqual( getData( actual ), getData( x ), 'returns expected value' ); - t.deepEqual( ndarray2array( actual ), expected, 'returns expected value' ); + t.deepEqual( getData( actual ), getData( x ), 'returns expected value' ); t.end(); }); @@ -281,18 +314,18 @@ tape( 'the function copies an input ndarray to a new ndarray (dtype=float64, opt var actual; var x; - x = zeros( 'float64', [ 4 ], 'row-major' ); + x = zeros( 'float32', [ 4 ], 'row-major' ); actual = copy( x, { - 'dtype': 'float32', + 'dtype': 'float64', 'order': 'column-major' }); expected = [ 0.0, 0.0, 0.0, 0.0 ]; t.strictEqual( instanceOf( actual, ndarray ), true, 'returns expected value' ); - t.strictEqual( isEqualDataType( getDType( actual ), 'float32' ), true, 'returns expected value' ); + t.strictEqual( isEqualDataType( getDType( actual ), 'float64' ), true, 'returns expected value' ); t.deepEqual( getShape( actual ), [ 4 ], 'returns expected value' ); - t.strictEqual( instanceOf( getData( actual ), Float32Array ), true, 'returns expected value' ); + t.strictEqual( instanceOf( getData( actual ), Float64Array ), true, 'returns expected value' ); t.strictEqual( getOrder( actual ), 'column-major', 'returns expected value' ); t.notEqual( getData( actual ), getData( x ), 'returns expected value' ); t.deepEqual( ndarray2array( actual ), expected, 'returns expected value' ); @@ -301,20 +334,20 @@ tape( 'the function copies an input ndarray to a new ndarray (dtype=float64, opt }); tape( 'the function copies an input ndarray to a new ndarray (dtype=float32, inferred)', function test( t ) { - var expected; var actual; var x; - x = zeros( 'float32', [ 2, 2 ], 'row-major' ); + x = uniform( [ 2, 2 ], -10.0, 10.0, { + 'dtype': 'float32' + }); actual = copy( x ); - expected = [ [ 0.0, 0.0 ], [ 0.0, 0.0 ] ]; t.strictEqual( isEqualDataType( getDType( actual ), getDType( x ) ), true, 'returns expected value' ); t.deepEqual( getShape( actual ), [ 2, 2 ], 'returns expected value' ); t.strictEqual( getOrder( actual ), 'row-major', 'returns expected value' ); t.notEqual( getData( actual ), getData( x ), 'returns expected value' ); - t.deepEqual( ndarray2array( actual ), expected, 'returns expected value' ); + t.deepEqual( getData( actual ), getData( x ), 'returns expected value' ); t.end(); }); @@ -351,7 +384,7 @@ tape( 'the function copies an input ndarray to a new ndarray (dtype=int32, infer x = zeros( 'int32', [ 2, 2 ], 'row-major' ); actual = copy( x ); - expected = [ [ 0.0, 0.0 ], [ 0.0, 0.0 ] ]; + expected = [ [ 0, 0 ], [ 0, 0 ] ]; t.strictEqual( isEqualDataType( getDType( actual ), getDType( x ) ), true, 'returns expected value' ); t.deepEqual( getShape( actual ), [ 2, 2 ], 'returns expected value' ); @@ -373,7 +406,7 @@ tape( 'the function copies an input ndarray to a new ndarray (dtype=int32, optio 'dtype': 'int32', 'order': 'column-major' }); - expected = [ [ 0.0, 0.0 ], [ 0.0, 0.0 ] ]; + expected = [ [ 0, 0 ], [ 0, 0 ] ]; t.strictEqual( instanceOf( actual, ndarray ), true, 'returns expected value' ); t.strictEqual( isEqualDataType( getDType( actual ), 'int32' ), true, 'returns expected value' ); @@ -394,7 +427,7 @@ tape( 'the function copies an input ndarray to a new ndarray (dtype=int16, infer x = zeros( 'int16', [ 2, 2 ], 'row-major' ); actual = copy( x ); - expected = [ [ 0.0, 0.0 ], [ 0.0, 0.0 ] ]; + expected = [ [ 0, 0 ], [ 0, 0 ] ]; t.strictEqual( isEqualDataType( getDType( actual ), getDType( x ) ), true, 'returns expected value' ); t.deepEqual( getShape( actual ), [ 2, 2 ], 'returns expected value' ); @@ -416,7 +449,7 @@ tape( 'the function copies an input ndarray to a new ndarray (dtype=int16, optio 'dtype': 'int16', 'order': 'column-major' }); - expected = [ [ 0.0, 0.0 ], [ 0.0, 0.0 ] ]; + expected = [ [ 0, 0 ], [ 0, 0 ] ]; t.strictEqual( instanceOf( actual, ndarray ), true, 'returns expected value' ); t.strictEqual( isEqualDataType( getDType( actual ), 'int16' ), true, 'returns expected value' ); @@ -437,7 +470,7 @@ tape( 'the function copies an input ndarray to a new ndarray (dtype=int8, inferr x = zeros( 'int8', [ 2, 2 ], 'row-major' ); actual = copy( x ); - expected = [ [ 0.0, 0.0 ], [ 0.0, 0.0 ] ]; + expected = [ [ 0, 0 ], [ 0, 0 ] ]; t.strictEqual( isEqualDataType( getDType( actual ), getDType( x ) ), true, 'returns expected value' ); t.deepEqual( getShape( actual ), [ 2, 2 ], 'returns expected value' ); @@ -459,7 +492,7 @@ tape( 'the function copies an input ndarray to a new ndarray (dtype=int8, option 'dtype': 'generic', 'order': 'column-major' }); - expected = [ [ 0.0, 0.0 ], [ 0.0, 0.0 ] ]; + expected = [ [ 0, 0 ], [ 0, 0 ] ]; t.strictEqual( instanceOf( actual, ndarray ), true, 'returns expected value' ); t.strictEqual( isEqualDataType( getDType( actual ), 'generic' ), true, 'returns expected value' ); @@ -479,7 +512,7 @@ tape( 'the function copies an input ndarray to a new ndarray (dtype=uint32, infe x = zeros( 'uint32', [ 2, 2 ], 'row-major' ); actual = copy( x ); - expected = [ [ 0.0, 0.0 ], [ 0.0, 0.0 ] ]; + expected = [ [ 0, 0 ], [ 0, 0 ] ]; t.strictEqual( isEqualDataType( getDType( actual ), getDType( x ) ), true, 'returns expected value' ); t.deepEqual( getShape( actual ), [ 2, 2 ], 'returns expected value' ); @@ -501,7 +534,7 @@ tape( 'the function copies an input ndarray to a new ndarray (dtype=uint32, opti 'dtype': 'uint32', 'order': 'column-major' }); - expected = [ [ 0.0, 0.0 ], [ 0.0, 0.0 ] ]; + expected = [ [ 0, 0 ], [ 0, 0 ] ]; t.strictEqual( instanceOf( actual, ndarray ), true, 'returns expected value' ); t.strictEqual( isEqualDataType( getDType( actual ), 'uint32' ), true, 'returns expected value' ); @@ -522,7 +555,7 @@ tape( 'the function copies an input ndarray to a new ndarray (dtype=uint16, infe x = zeros( 'uint16', [ 2, 2 ], 'row-major' ); actual = copy( x ); - expected = [ [ 0.0, 0.0 ], [ 0.0, 0.0 ] ]; + expected = [ [ 0, 0 ], [ 0, 0 ] ]; t.strictEqual( isEqualDataType( getDType( actual ), getDType( x ) ), true, 'returns expected value' ); t.deepEqual( getShape( actual ), [ 2, 2 ], 'returns expected value' ); @@ -544,7 +577,7 @@ tape( 'the function copies an input ndarray to a new ndarray (dtype=uint16, opti 'dtype': 'uint16', 'order': 'column-major' }); - expected = [ [ 0.0, 0.0 ], [ 0.0, 0.0 ] ]; + expected = [ [ 0, 0 ], [ 0, 0 ] ]; t.strictEqual( instanceOf( actual, ndarray ), true, 'returns expected value' ); t.strictEqual( isEqualDataType( getDType( actual ), 'uint16' ), true, 'returns expected value' ); @@ -565,7 +598,7 @@ tape( 'the function copies an input ndarray to a new ndarray (dtype=uint8, infer x = zeros( 'uint8', [ 2, 2 ], 'row-major' ); actual = copy( x ); - expected = [ [ 0.0, 0.0 ], [ 0.0, 0.0 ] ]; + expected = [ [ 0, 0 ], [ 0, 0 ] ]; t.strictEqual( isEqualDataType( getDType( actual ), getDType( x ) ), true, 'returns expected value' ); t.deepEqual( getShape( actual ), [ 2, 2 ], 'returns expected value' ); @@ -587,7 +620,7 @@ tape( 'the function copies an input ndarray to a new ndarray (dtype=uint8, optio 'dtype': 'generic', 'order': 'column-major' }); - expected = [ [ 0.0, 0.0 ], [ 0.0, 0.0 ] ]; + expected = [ [ 0, 0 ], [ 0, 0 ] ]; t.strictEqual( instanceOf( actual, ndarray ), true, 'returns expected value' ); t.strictEqual( isEqualDataType( getDType( actual ), 'generic' ), true, 'returns expected value' ); @@ -607,7 +640,7 @@ tape( 'the function copies an input ndarray to a new ndarray (dtype=uint8c, infe x = zeros( 'uint8c', [ 2, 2 ], 'row-major' ); actual = copy( x ); - expected = [ [ 0.0, 0.0 ], [ 0.0, 0.0 ] ]; + expected = [ [ 0, 0 ], [ 0, 0 ] ]; t.strictEqual( isEqualDataType( getDType( actual ), getDType( x ) ), true, 'returns expected value' ); t.deepEqual( getShape( actual ), [ 2, 2 ], 'returns expected value' ); @@ -629,7 +662,7 @@ tape( 'the function copies an input ndarray to a new ndarray (dtype=uint8c, opti 'dtype': 'generic', 'order': 'column-major' }); - expected = [ [ 0.0, 0.0 ], [ 0.0, 0.0 ] ]; + expected = [ [ 0, 0 ], [ 0, 0 ] ]; t.strictEqual( instanceOf( actual, ndarray ), true, 'returns expected value' ); t.strictEqual( isEqualDataType( getDType( actual ), 'generic' ), true, 'returns expected value' ); @@ -653,12 +686,13 @@ tape( 'the function copies an input ndarray to a new ndarray (dtype=complex128, t.deepEqual( getShape( actual ), [ 2, 2 ], 'returns expected value' ); t.strictEqual( getOrder( actual ), 'row-major', 'returns expected value' ); t.notEqual( getData( actual ), getData( x ), 'returns expected value' ); - t.deepEqual( getData( actual ), getData( x ), 'returns expected value' ); + t.strictEqual( isSameComplex128Array( getData( actual ), getData( x ) ), true, 'returns expected value' ); t.end(); }); tape( 'the function copies an input ndarray to a new ndarray (dtype=complex128, options)', function test( t ) { + var expected; var actual; var x; @@ -668,6 +702,7 @@ tape( 'the function copies an input ndarray to a new ndarray (dtype=complex128, 'dtype': 'complex128', 'order': 'column-major' }); + expected = new Complex128Array( 4 ); t.strictEqual( instanceOf( actual, ndarray ), true, 'returns expected value' ); t.strictEqual( isEqualDataType( getDType( actual ), 'complex128' ), true, 'returns expected value' ); @@ -675,7 +710,7 @@ tape( 'the function copies an input ndarray to a new ndarray (dtype=complex128, t.strictEqual( instanceOf( getData( actual ), Complex128Array ), true, 'returns expected value' ); t.strictEqual( getOrder( actual ), 'column-major', 'returns expected value' ); t.notEqual( getData( actual ), getData( x ), 'returns expected value' ); - t.deepEqual( getData( actual ), getData( x ), 'returns expected value' ); + t.strictEqual( isSameComplex128Array( getData( actual ), expected ), true, 'returns expected value' ); t.end(); }); @@ -692,12 +727,13 @@ tape( 'the function copies an input ndarray to a new ndarray (dtype=complex64, i t.deepEqual( getShape( actual ), [ 2, 2 ], 'returns expected value' ); t.strictEqual( getOrder( actual ), 'row-major', 'returns expected value' ); t.notEqual( getData( actual ), getData( x ), 'returns expected value' ); - t.deepEqual( getData( actual ), getData( x ), 'returns expected value' ); + t.strictEqual( isSameComplex64Array( getData( actual ), getData( x ) ), true, 'returns expected value' ); t.end(); }); tape( 'the function copies an input ndarray to a new ndarray (dtype=complex64, options)', function test( t ) { + var expected; var actual; var x; @@ -708,6 +744,7 @@ tape( 'the function copies an input ndarray to a new ndarray (dtype=complex64, o 'dtype': 'complex64', 'order': 'column-major' }); + expected = new Complex64Array( 4 ); t.strictEqual( instanceOf( actual, ndarray ), true, 'returns expected value' ); t.strictEqual( isEqualDataType( getDType( actual ), 'complex64' ), true, 'returns expected value' ); @@ -715,7 +752,7 @@ tape( 'the function copies an input ndarray to a new ndarray (dtype=complex64, o t.strictEqual( instanceOf( getData( actual ), Complex64Array ), true, 'returns expected value' ); t.strictEqual( getOrder( actual ), 'column-major', 'returns expected value' ); t.notEqual( getData( actual ), getData( x ), 'returns expected value' ); - t.deepEqual( getData( actual ), getData( x ), 'returns expected value' ); + t.strictEqual( isSameComplex64Array( getData( actual ), expected ), true, 'returns expected value' ); t.end(); }); @@ -770,7 +807,7 @@ tape( 'the function supports zero-dimensional arrays', function test( t ) { 'dtype': 'generic', 'ndims': 0, 'length': 0, - 'data': [ 0 ], + 'data': [ 1 ], 'shape': [], 'strides': [ 0 ], 'offset': 0, From 78eb228bdb48ad725d0f969857c15d0c673ea99e Mon Sep 17 00:00:00 2001 From: Athan Date: Tue, 11 Nov 2025 23:05:29 -0800 Subject: [PATCH 3/3] bench: fix dtypes --- type: pre_commit_static_analysis_report description: Results of running static analysis checks when committing changes. report: - task: lint_filenames status: passed - task: lint_editorconfig status: passed - task: lint_markdown status: na - task: lint_package_json status: na - task: lint_repl_help status: na - task: lint_javascript_src status: na - task: lint_javascript_cli status: na - task: lint_javascript_examples status: na - task: lint_javascript_tests status: na - task: lint_javascript_benchmarks status: passed - task: lint_python status: na - task: lint_r status: na - task: lint_c_src status: na - task: lint_c_examples status: na - task: lint_c_benchmarks status: na - task: lint_c_tests_fixtures status: na - task: lint_shell status: na - task: lint_typescript_declarations status: passed - task: lint_typescript_tests status: na - task: lint_license_headers status: passed --- --- .../@stdlib/ndarray/copy/benchmark/benchmark.size.js | 12 ++++++++++-- 1 file changed, 10 insertions(+), 2 deletions(-) diff --git a/lib/node_modules/@stdlib/ndarray/copy/benchmark/benchmark.size.js b/lib/node_modules/@stdlib/ndarray/copy/benchmark/benchmark.size.js index 7818e77e5e55..592ab92a7212 100644 --- a/lib/node_modules/@stdlib/ndarray/copy/benchmark/benchmark.size.js +++ b/lib/node_modules/@stdlib/ndarray/copy/benchmark/benchmark.size.js @@ -24,14 +24,22 @@ var bench = require( '@stdlib/bench' ); var pow = require( '@stdlib/math/base/special/pow' ); var isndarrayLike = require( '@stdlib/assert/is-ndarray-like' ); var zeros = require( '@stdlib/ndarray/zeros' ); -var dtypes = require( '@stdlib/ndarray/dtypes' ); var pkg = require( './../package.json' ).name; var copy = require( './../lib' ); // VARIABLES // -var DTYPES = dtypes(); +var DTYPES = [ + 'float64', + 'float32', + 'generic', + 'int32', + 'uint32', + 'uint8', + 'complex128', + 'complex64' +]; // FUNCTIONS //