From 75c5741cc0ced8df13fa7db53d6ead01122fd92c Mon Sep 17 00:00:00 2001 From: stdlib-bot Date: Fri, 16 Aug 2024 09:10:22 +0000 Subject: [PATCH] Auto-generated commit --- CHANGELOG.md | 26 +++ base/lib/index.js | 9 + base/ndarraylike2ndarray/README.md | 124 ++++++++++++++ .../benchmark/benchmark.js | 161 ++++++++++++++++++ base/ndarraylike2ndarray/docs/repl.txt | 23 +++ .../ndarraylike2ndarray/docs/types/index.d.ts | 44 +++++ base/ndarraylike2ndarray/docs/types/test.ts | 77 +++++++++ base/ndarraylike2ndarray/examples/index.js | 38 +++++ base/ndarraylike2ndarray/lib/index.js | 43 +++++ base/ndarraylike2ndarray/lib/main.js | 67 ++++++++ base/ndarraylike2ndarray/package.json | 67 ++++++++ base/ndarraylike2ndarray/test/test.js | 108 ++++++++++++ base/ndarraylike2object/docs/repl.txt | 3 +- base/ndarraylike2object/docs/types/index.d.ts | 2 +- base/ndarraylike2object/lib/main.js | 2 +- 15 files changed, 791 insertions(+), 3 deletions(-) create mode 100644 base/ndarraylike2ndarray/README.md create mode 100644 base/ndarraylike2ndarray/benchmark/benchmark.js create mode 100644 base/ndarraylike2ndarray/docs/repl.txt create mode 100644 base/ndarraylike2ndarray/docs/types/index.d.ts create mode 100644 base/ndarraylike2ndarray/docs/types/test.ts create mode 100644 base/ndarraylike2ndarray/examples/index.js create mode 100644 base/ndarraylike2ndarray/lib/index.js create mode 100644 base/ndarraylike2ndarray/lib/main.js create mode 100644 base/ndarraylike2ndarray/package.json create mode 100644 base/ndarraylike2ndarray/test/test.js diff --git a/CHANGELOG.md b/CHANGELOG.md index bea80fc9..c96e6fdc 100644 --- a/CHANGELOG.md +++ b/CHANGELOG.md @@ -64,6 +64,7 @@ ##### Features +- [`54262c8`](https://github.com/stdlib-js/stdlib/commit/54262c89e70eae566591c6e87ece69b68ca09488) - add `ndarraylike2ndarray` to namespace - [`6e4b9eb`](https://github.com/stdlib-js/stdlib/commit/6e4b9ebc31d9629446019e37e31bfe9b180b675c) - update namespace TypeScript declarations [(#2681)](https://github.com/stdlib-js/stdlib/pull/2681) - [`d31e751`](https://github.com/stdlib-js/stdlib/commit/d31e7515b71dc5b76751173c7724d73d943b1473) - add `forEach` to namespace - [`de17de3`](https://github.com/stdlib-js/stdlib/commit/de17de32867461079aae166d5cecbecb1da7f922) - update namespace TypeScript declarations [(#2593)](https://github.com/stdlib-js/stdlib/pull/2593) @@ -298,6 +299,28 @@ +
+ +#### [@stdlib/ndarray/base/ndarraylike2ndarray](https://github.com/stdlib-js/stdlib/tree/develop/lib/node_modules/%40stdlib/ndarray/base/ndarraylike2ndarray) + +
+ +
+ +##### Features + +- [`5d01561`](https://github.com/stdlib-js/stdlib/commit/5d015616e9731e40d20f7d4dca6b136ae47cc9bc) - add `ndarray/base/ndarraylike2ndarray` + +
+ + + +
+ +
+ + +
#### [@stdlib/ndarray/base/nullary](https://github.com/stdlib-js/stdlib/tree/develop/lib/node_modules/%40stdlib/ndarray/base/nullary) @@ -989,6 +1012,9 @@ A total of 4 people contributed to this release. Thank you to the following cont
+- [`dd48932`](https://github.com/stdlib-js/stdlib/commit/dd489326b8dcee32f41f2ef7c2bafcaa4eb6ce46) - **docs:** fix descriptions _(by Athan Reines)_ +- [`54262c8`](https://github.com/stdlib-js/stdlib/commit/54262c89e70eae566591c6e87ece69b68ca09488) - **feat:** add `ndarraylike2ndarray` to namespace _(by Athan Reines)_ +- [`5d01561`](https://github.com/stdlib-js/stdlib/commit/5d015616e9731e40d20f7d4dca6b136ae47cc9bc) - **feat:** add `ndarray/base/ndarraylike2ndarray` _(by Athan Reines)_ - [`72ed2e1`](https://github.com/stdlib-js/stdlib/commit/72ed2e1e6331858c078564e22b6dca041f5daaca) - **feat:** add `ndarray/base/map` [(#2715)](https://github.com/stdlib-js/stdlib/pull/2715) _(by Muhammad Haris, Athan Reines)_ - [`96c7ddf`](https://github.com/stdlib-js/stdlib/commit/96c7ddfdbcecc6ff60fcb56681db16463d19020e) - **fix:** use computed order and fix strides in examples _(by Athan Reines)_ - [`e2b7fb5`](https://github.com/stdlib-js/stdlib/commit/e2b7fb5df61f15ae7dbf148ec0c0412ff434b123) - **fix:** use computed order and fix strides in examples _(by Athan Reines)_ diff --git a/base/lib/index.js b/base/lib/index.js index 48dc2d3c..ac8a7816 100644 --- a/base/lib/index.js +++ b/base/lib/index.js @@ -445,6 +445,15 @@ setReadOnly( ns, 'minViewBufferIndex', require( './../../base/min-view-buffer-in */ setReadOnly( ns, 'minmaxViewBufferIndex', require( './../../base/minmax-view-buffer-index' ) ); +/** +* @name ndarraylike2ndarray +* @memberof ns +* @readonly +* @type {Function} +* @see {@link module:@stdlib/ndarray/base/ndarraylike2ndarray} +*/ +setReadOnly( ns, 'ndarraylike2ndarray', require( './../../base/ndarraylike2ndarray' ) ); + /** * @name ndarraylike2object * @memberof ns diff --git a/base/ndarraylike2ndarray/README.md b/base/ndarraylike2ndarray/README.md new file mode 100644 index 00000000..a40ba5f4 --- /dev/null +++ b/base/ndarraylike2ndarray/README.md @@ -0,0 +1,124 @@ + + +# ndarraylike2ndarray + +> Convert an ndarray-like object to an [`ndarray`][@stdlib/ndarray/base/ctor]. + + + +
+ +
+ + + + + +
+ +## Usage + +```javascript +var ndarraylike2ndarray = require( '@stdlib/ndarray/base/ndarraylike2ndarray' ); +``` + +#### ndarraylike2ndarray( x ) + +Converts an ndarray-like object to an [`ndarray`][@stdlib/ndarray/base/ctor]. + +```javascript +var array = require( '@stdlib/ndarray/array' ); + +var arr = array( [ [ 1, 2 ], [ 3, 4 ] ] ); +var out = ndarraylike2ndarray( arr ); +// returns +``` + +
+ + + + + +
+ +
+ + + + + +
+ +## Examples + + + +```javascript +var array = require( '@stdlib/ndarray/array' ); +var ndarraylike2ndarray = require( '@stdlib/ndarray/base/ndarraylike2ndarray' ); + +// Create an ndarray: +var x = array( [ [ 1, 2 ], [ 3, 4 ] ] ); + +// Convert to a "base" ndarray: +var out = ndarraylike2ndarray( x ); +// returns + +// Print various properties: +console.log( 'dtype: %s', out.dtype ); +console.log( 'ndims: %d', out.shape.length ); +console.log( 'length: %d', out.length ); +console.log( 'shape: [ %s ]', out.shape.join( ', ' ) ); +console.log( 'strides: [ %s ]', out.strides.join( ', ' ) ); +console.log( 'offset: %d', out.offset ); +console.log( 'order: %s', out.order ); +``` + +
+ + + + + +
+ +
+ + + + + + + + + + + + + + diff --git a/base/ndarraylike2ndarray/benchmark/benchmark.js b/base/ndarraylike2ndarray/benchmark/benchmark.js new file mode 100644 index 00000000..4cb97344 --- /dev/null +++ b/base/ndarraylike2ndarray/benchmark/benchmark.js @@ -0,0 +1,161 @@ +/** +* @license Apache-2.0 +* +* Copyright (c) 2024 The Stdlib Authors. +* +* Licensed under the Apache License, Version 2.0 (the "License"); +* you may not use this file except in compliance with the License. +* You may obtain a copy of the License at +* +* http://www.apache.org/licenses/LICENSE-2.0 +* +* Unless required by applicable law or agreed to in writing, software +* distributed under the License is distributed on an "AS IS" BASIS, +* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. +* See the License for the specific language governing permissions and +* limitations under the License. +*/ + +'use strict'; + +// MODULES // + +var bench = require( '@stdlib/bench' ); +var Float64Array = require( '@stdlib/array/float64' ); +var ndarrayBase = require( './../../../base/ctor' ); +var ndarray = require( './../../../ctor' ); +var isCollection = require( '@stdlib/assert/is-collection' ); +var pkg = require( './../package.json' ).name; +var ndarraylike2ndarray = require( './../lib' ); + + +// MAIN // + +bench( pkg+'::base_ndarray', function benchmark( b ) { + var strides; + var values; + var buffer; + var offset; + var dtype; + var shape; + var order; + var out; + var i; + + dtype = 'float64'; + buffer = new Float64Array( 4 ); + shape = [ 2, 2 ]; + strides = [ 2, 1 ]; + offset = 0; + order = 'row-major'; + + values = [ + ndarrayBase( dtype, buffer, shape, strides, offset, order ), + ndarrayBase( dtype, buffer, shape, strides, offset, order ), + ndarrayBase( dtype, buffer, shape, strides, offset, order ), + ndarrayBase( dtype, buffer, shape, strides, offset, order ), + ndarrayBase( dtype, buffer, shape, strides, offset, order ) + ]; + + b.tic(); + for ( i = 0; i < b.iterations; i++ ) { + out = ndarraylike2ndarray( values[ i%values.length ] ); + if ( typeof out !== 'object' ) { + b.fail( 'should return an object' ); + } + } + b.toc(); + if ( !isCollection( out.data ) ) { + b.fail( 'should return a collection' ); + } + b.pass( 'benchmark finished' ); + b.end(); +}); + +bench( pkg+'::ndarray', function benchmark( b ) { + var strides; + var values; + var buffer; + var offset; + var dtype; + var shape; + var order; + var out; + var i; + + dtype = 'float64'; + buffer = new Float64Array( 4 ); + shape = [ 2, 2 ]; + strides = [ 2, 1 ]; + offset = 0; + order = 'row-major'; + + values = [ + ndarray( dtype, buffer, shape, strides, offset, order ), + ndarray( dtype, buffer, shape, strides, offset, order ), + ndarray( dtype, buffer, shape, strides, offset, order ), + ndarray( dtype, buffer, shape, strides, offset, order ), + ndarray( dtype, buffer, shape, strides, offset, order ) + ]; + + b.tic(); + for ( i = 0; i < b.iterations; i++ ) { + out = ndarraylike2ndarray( values[ i%values.length ] ); + if ( typeof out !== 'object' ) { + b.fail( 'should return an object' ); + } + } + b.toc(); + if ( !isCollection( out.data ) ) { + b.fail( 'should return a collection' ); + } + b.pass( 'benchmark finished' ); + b.end(); +}); + +bench( pkg+'::ndarray_like', function benchmark( b ) { + var strides; + var values; + var buffer; + var offset; + var dtype; + var shape; + var order; + var out; + var obj; + var i; + + dtype = 'float64'; + buffer = new Float64Array( 4 ); + shape = [ 2, 2 ]; + strides = [ 2, 1 ]; + offset = 0; + order = 'row-major'; + + values = []; + for ( i = 0; i < 5; i++ ) { + obj = { + 'dtype': dtype, + 'data': buffer, + 'shape': shape, + 'strides': strides, + 'offset': offset, + 'order': order + }; + values.push( obj ); + } + + b.tic(); + for ( i = 0; i < b.iterations; i++ ) { + out = ndarraylike2ndarray( values[ i%values.length ] ); + if ( typeof out !== 'object' ) { + b.fail( 'should return an object' ); + } + } + b.toc(); + if ( !isCollection( out.data ) ) { + b.fail( 'should return a collection' ); + } + b.pass( 'benchmark finished' ); + b.end(); +}); diff --git a/base/ndarraylike2ndarray/docs/repl.txt b/base/ndarraylike2ndarray/docs/repl.txt new file mode 100644 index 00000000..b84dc19e --- /dev/null +++ b/base/ndarraylike2ndarray/docs/repl.txt @@ -0,0 +1,23 @@ + +{{alias}}( x ) + Converts an ndarray-like object to an ndarray. + + Parameters + ---------- + x: ndarrayLike + Input ndarray-like object. + + Returns + ------- + out: ndarray + Output array. + + Examples + -------- + > var arr = {{alias:@stdlib/ndarray/array}}( [ [ 1, 2 ], [ 3, 4 ] ] ); + > var out = {{alias}}( arr ) + + + See Also + -------- + diff --git a/base/ndarraylike2ndarray/docs/types/index.d.ts b/base/ndarraylike2ndarray/docs/types/index.d.ts new file mode 100644 index 00000000..be7ec77d --- /dev/null +++ b/base/ndarraylike2ndarray/docs/types/index.d.ts @@ -0,0 +1,44 @@ +/* +* @license Apache-2.0 +* +* Copyright (c) 2024 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 { typedndarray } from '@stdlib/types/ndarray'; + +/** +* Converts an ndarray-like object to an ndarray. +* +* @param x - input ndarray +* @returns ndarray +* +* @example +* var array = require( '@stdlib/ndarray/array' ); +* +* var x = array( [ [ 1, 2, 3 ], [ 4, 5, 6 ] ] ); +* +* var out = ndarraylike2ndarray( x ); +* // returns +*/ +declare function ndarraylike2ndarray( x: typedndarray ): typedndarray; + + +// EXPORTS // + +export = ndarraylike2ndarray; diff --git a/base/ndarraylike2ndarray/docs/types/test.ts b/base/ndarraylike2ndarray/docs/types/test.ts new file mode 100644 index 00000000..beb2e84e --- /dev/null +++ b/base/ndarraylike2ndarray/docs/types/test.ts @@ -0,0 +1,77 @@ +/* +* @license Apache-2.0 +* +* Copyright (c) 2024 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 { typedndarray } from '@stdlib/types/ndarray'; +import ndarraylike2ndarray = require( './index' ); + +/** +* Mock function to create an ndarray-like object. +* +* @return ndarray-like object +*/ +function array(): typedndarray { + const obj: typedndarray = { + 'byteLength': 80, + 'BYTES_PER_ELEMENT': 8, + 'data': new Float64Array( 10 ), + 'dtype': 'float64', + 'flags': { + 'ROW_MAJOR_CONTIGUOUS': true, + 'COLUMN_MAJOR_CONTIGUOUS': false + }, + 'length': 10, + 'ndims': 1, + 'offset': 0, + 'order': 'row-major', + 'shape': [ 10 ], + 'strides': [ 1 ], + 'get': (): number => 0, + 'set': (): typedndarray => obj + }; + return obj; +} + + +// TESTS // + +// The function returns an ndarray... +{ + const x = array(); + ndarraylike2ndarray( x ); // $ExpectType typedndarray +} + +// The compiler throws an error if the function is not provided an ndarray... +{ + ndarraylike2ndarray( '5' ); // $ExpectError + ndarraylike2ndarray( 123 ); // $ExpectError + ndarraylike2ndarray( true ); // $ExpectError + ndarraylike2ndarray( false ); // $ExpectError + ndarraylike2ndarray( null ); // $ExpectError + ndarraylike2ndarray( [] ); // $ExpectError + ndarraylike2ndarray( {} ); // $ExpectError + ndarraylike2ndarray( ( x: number ): number => x ); // $ExpectError +} + +// The compiler throws an error if the function is provided an unsupported number of arguments... +{ + const x = array(); + ndarraylike2ndarray(); // $ExpectError + ndarraylike2ndarray( x, 5 ); // $ExpectError +} diff --git a/base/ndarraylike2ndarray/examples/index.js b/base/ndarraylike2ndarray/examples/index.js new file mode 100644 index 00000000..92e016ab --- /dev/null +++ b/base/ndarraylike2ndarray/examples/index.js @@ -0,0 +1,38 @@ +/** +* @license Apache-2.0 +* +* Copyright (c) 2024 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 array = require( './../../../array' ); +var ndarraylike2ndarray = require( './../lib' ); + +// Create an ndarray: +var x = array( [ [ 1, 2 ], [ 3, 4 ] ] ); + +// Convert to a "base" ndarray: +var out = ndarraylike2ndarray( x ); +// returns + +// Print various properties: +console.log( 'dtype: %s', out.dtype ); +console.log( 'ndims: %d', out.shape.length ); +console.log( 'length: %d', out.length ); +console.log( 'shape: [ %s ]', out.shape.join( ', ' ) ); +console.log( 'strides: [ %s ]', out.strides.join( ', ' ) ); +console.log( 'offset: %d', out.offset ); +console.log( 'order: %s', out.order ); diff --git a/base/ndarraylike2ndarray/lib/index.js b/base/ndarraylike2ndarray/lib/index.js new file mode 100644 index 00000000..d3c483ef --- /dev/null +++ b/base/ndarraylike2ndarray/lib/index.js @@ -0,0 +1,43 @@ +/** +* @license Apache-2.0 +* +* Copyright (c) 2024 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'; + +/** +* Convert an ndarray-like object to an ndarray. +* +* @module @stdlib/ndarray/base/ndarraylike2ndarray +* +* @example +* var array = require( '@stdlib/ndarray/array' ); +* var ndarraylike2ndarray = require( '@stdlib/ndarray/base/ndarraylike2ndarray' ); +* +* var x = array( [ [ 1, 2, 3 ], [ 4, 5, 6 ] ] ); +* +* var out = ndarraylike2ndarray( x ); +* // returns +*/ + +// MODULES // + +var main = require( './main.js' ); + + +// EXPORTS // + +module.exports = main; diff --git a/base/ndarraylike2ndarray/lib/main.js b/base/ndarraylike2ndarray/lib/main.js new file mode 100644 index 00000000..053a4dc2 --- /dev/null +++ b/base/ndarraylike2ndarray/lib/main.js @@ -0,0 +1,67 @@ +/** +* @license Apache-2.0 +* +* Copyright (c) 2024 The Stdlib Authors. +* +* Licensed under the Apache License, Version 2.0 (the "License"); +* you may not use this file except in compliance with the License. +* You may obtain a copy of the License at +* +* http://www.apache.org/licenses/LICENSE-2.0 +* +* Unless required by applicable law or agreed to in writing, software +* distributed under the License is distributed on an "AS IS" BASIS, +* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. +* See the License for the specific language governing permissions and +* limitations under the License. +*/ + +'use strict'; + +// MODULES // + +var getDType = require( './../../../dtype' ); +var getShape = require( './../../../shape' ); +var getStrides = require( './../../../strides' ); +var getOffset = require( './../../../offset' ); +var getOrder = require( './../../../order' ); +var getData = require( './../../../data-buffer' ); +var ndarray = require( './../../../base/ctor' ); +var defaults = require( './../../../defaults' ); + + +// VARIABLES // + +var DEFAULT_ORDER = defaults( 'order' ); + + +// MAIN // + +/** +* Converts an ndarray-like object to an ndarray. +* +* @param {ndarrayLike} x - ndarray-like object +* @param {string} x.dtype - data type +* @param {Collection} x.data - data buffer +* @param {NonNegativeIntegerArray} x.shape - dimensions +* @param {IntegerArray} x.strides - stride lengths +* @param {NonNegativeInteger} x.offset - index offset +* @param {string} x.order - specifies whether `x` is row-major (C-style) or column-major (Fortran-style) +* @returns {ndarray} ndarray +* +* @example +* var array = require( '@stdlib/ndarray/array' ); +* +* var x = array( [ [ 1, 2, 3 ], [ 4, 5, 6 ] ] ); +* +* var out = ndarraylike2ndarray( x ); +* // returns +*/ +function ndarraylike2ndarray( x ) { + return new ndarray( getDType( x ), getData( x ), getShape( x ), getStrides( x ), getOffset( x ), getOrder( x ) || DEFAULT_ORDER ); // eslint-disable-line max-len +} + + +// EXPORTS // + +module.exports = ndarraylike2ndarray; diff --git a/base/ndarraylike2ndarray/package.json b/base/ndarraylike2ndarray/package.json new file mode 100644 index 00000000..26782ce5 --- /dev/null +++ b/base/ndarraylike2ndarray/package.json @@ -0,0 +1,67 @@ +{ + "name": "@stdlib/ndarray/base/ndarraylike2ndarray", + "version": "0.0.0", + "description": "Convert an ndarray-like object to an ndarray.", + "license": "Apache-2.0", + "author": { + "name": "The Stdlib Authors", + "url": "https://github.com/stdlib-js/stdlib/graphs/contributors" + }, + "contributors": [ + { + "name": "The Stdlib Authors", + "url": "https://github.com/stdlib-js/stdlib/graphs/contributors" + } + ], + "main": "./lib", + "directories": { + "benchmark": "./benchmark", + "doc": "./docs", + "example": "./examples", + "lib": "./lib", + "test": "./test" + }, + "types": "./docs/types", + "scripts": {}, + "homepage": "https://github.com/stdlib-js/stdlib", + "repository": { + "type": "git", + "url": "git://github.com/stdlib-js/stdlib.git" + }, + "bugs": { + "url": "https://github.com/stdlib-js/stdlib/issues" + }, + "dependencies": {}, + "devDependencies": {}, + "engines": { + "node": ">=0.10.0", + "npm": ">2.7.0" + }, + "os": [ + "aix", + "darwin", + "freebsd", + "linux", + "macos", + "openbsd", + "sunos", + "win32", + "windows" + ], + "keywords": [ + "stdlib", + "stdtypes", + "types", + "base", + "ndarray", + "standardize", + "multidimensional", + "array", + "convert", + "utilities", + "utility", + "utils", + "util" + ], + "__stdlib__": {} +} diff --git a/base/ndarraylike2ndarray/test/test.js b/base/ndarraylike2ndarray/test/test.js new file mode 100644 index 00000000..d77f2fcc --- /dev/null +++ b/base/ndarraylike2ndarray/test/test.js @@ -0,0 +1,108 @@ +/** +* @license Apache-2.0 +* +* Copyright (c) 2024 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 Float64Array = require( '@stdlib/array/float64' ); +var array = require( './../../../array' ); +var ndarray = require( './../../../base/ctor' ); +var ndarraylike2ndarray = require( './../lib' ); + + +// TESTS // + +tape( 'main export is a function', function test( t ) { + t.ok( true, __filename ); + t.strictEqual( typeof ndarraylike2ndarray, 'function', 'main export is a function' ); + t.end(); +}); + +tape( 'the function converts an ndarray-like object to an ndarray (ndarray)', function test( t ) { + var expected; + var actual; + var xbuf; + var x; + + xbuf = new Float64Array( 10 ); + x = array( xbuf ); + + expected = { + 'data': x.data, + 'dtype': x.dtype, + 'length': x.length, + 'shape': x.shape, + 'strides': x.strides, + 'offset': x.offset, + 'order': x.order + }; + actual = ndarraylike2ndarray( x ); + + t.strictEqual( actual instanceof ndarray, true, 'returns expected value' ); + t.strictEqual( actual.data, expected.data, 'returns expected value' ); + t.strictEqual( actual.dtype, expected.dtype, 'returns expected value' ); + t.strictEqual( actual.length, expected.length, 'returns expected value' ); + t.deepEqual( actual.shape, expected.shape, 'returns expected value' ); + t.deepEqual( actual.strides, expected.strides, 'returns expected value' ); + t.strictEqual( actual.offset, expected.offset, 'returns expected value' ); + t.strictEqual( actual.order, expected.order, 'returns expected value' ); + + t.end(); +}); + +tape( 'the function converts an ndarray-like object to an ndarray (ndarray-like)', function test( t ) { + var expected; + var actual; + var xbuf; + var x; + + xbuf = new Float64Array( 10 ); + x = { + 'data': xbuf, + 'dtype': 'float64', + 'length': 10, + 'shape': [ 2, 5 ], + 'strides': [ 5, 1 ], + 'offset': 0, + 'order': 'row-major' + }; + + expected = { + 'data': x.data, + 'dtype': x.dtype, + 'length': x.length, + 'shape': x.shape, + 'strides': x.strides, + 'offset': x.offset, + 'order': x.order + }; + actual = ndarraylike2ndarray( x ); + + t.strictEqual( actual instanceof ndarray, true, 'returns expected value' ); + t.strictEqual( actual.data, expected.data, 'returns expected value' ); + t.strictEqual( actual.dtype, expected.dtype, 'returns expected value' ); + t.strictEqual( actual.length, expected.length, 'returns expected value' ); + t.deepEqual( actual.shape, expected.shape, 'returns expected value' ); + t.deepEqual( actual.strides, expected.strides, 'returns expected value' ); + t.strictEqual( actual.offset, expected.offset, 'returns expected value' ); + t.strictEqual( actual.order, expected.order, 'returns expected value' ); + + t.end(); +}); diff --git a/base/ndarraylike2object/docs/repl.txt b/base/ndarraylike2object/docs/repl.txt index 07a0cd27..993f2f49 100644 --- a/base/ndarraylike2object/docs/repl.txt +++ b/base/ndarraylike2object/docs/repl.txt @@ -1,6 +1,7 @@ {{alias}}( x ) - Converts an ndarray-like to an object likely to have the same "shape". + Converts an ndarray-like object to an object likely to have the same + "shape". The returned object has the following properties: diff --git a/base/ndarraylike2object/docs/types/index.d.ts b/base/ndarraylike2object/docs/types/index.d.ts index 1d302c47..9a47107b 100644 --- a/base/ndarraylike2object/docs/types/index.d.ts +++ b/base/ndarraylike2object/docs/types/index.d.ts @@ -98,7 +98,7 @@ interface ndarrayObject { } /** -* Converts an ndarray-like to an object likely to have the same "shape". +* Converts an ndarray-like object to an object likely to have the same "shape". * * ## Notes * diff --git a/base/ndarraylike2object/lib/main.js b/base/ndarraylike2object/lib/main.js index 83d03b82..649a467f 100644 --- a/base/ndarraylike2object/lib/main.js +++ b/base/ndarraylike2object/lib/main.js @@ -37,7 +37,7 @@ var getData = require( './../../../base/data-buffer' ); // MAIN // /** -* Converts an ndarray-like to an object likely to have the same "shape". +* Converts an ndarray-like object to an object likely to have the same "shape". * * ## Notes *