From 6afd4871d06b6dd5ac10d26f5d7c1794ce697856 Mon Sep 17 00:00:00 2001 From: Athan Reines Date: Thu, 10 Aug 2023 03:19:40 -0400 Subject: [PATCH] feat: add `array/base/ones2d` --- .../@stdlib/array/base/ones2d/README.md | 116 ++++++++++++++++++ .../base/ones2d/benchmark/benchmark.size.js | 96 +++++++++++++++ .../@stdlib/array/base/ones2d/docs/repl.txt | 22 ++++ .../array/base/ones2d/docs/types/index.d.ts | 40 ++++++ .../array/base/ones2d/docs/types/test.ts | 44 +++++++ .../array/base/ones2d/examples/index.js | 35 ++++++ .../@stdlib/array/base/ones2d/lib/index.js | 40 ++++++ .../@stdlib/array/base/ones2d/lib/main.js | 45 +++++++ .../@stdlib/array/base/ones2d/package.json | 70 +++++++++++ .../@stdlib/array/base/ones2d/test/test.js | 89 ++++++++++++++ 10 files changed, 597 insertions(+) create mode 100644 lib/node_modules/@stdlib/array/base/ones2d/README.md create mode 100644 lib/node_modules/@stdlib/array/base/ones2d/benchmark/benchmark.size.js create mode 100644 lib/node_modules/@stdlib/array/base/ones2d/docs/repl.txt create mode 100644 lib/node_modules/@stdlib/array/base/ones2d/docs/types/index.d.ts create mode 100644 lib/node_modules/@stdlib/array/base/ones2d/docs/types/test.ts create mode 100644 lib/node_modules/@stdlib/array/base/ones2d/examples/index.js create mode 100644 lib/node_modules/@stdlib/array/base/ones2d/lib/index.js create mode 100644 lib/node_modules/@stdlib/array/base/ones2d/lib/main.js create mode 100644 lib/node_modules/@stdlib/array/base/ones2d/package.json create mode 100644 lib/node_modules/@stdlib/array/base/ones2d/test/test.js diff --git a/lib/node_modules/@stdlib/array/base/ones2d/README.md b/lib/node_modules/@stdlib/array/base/ones2d/README.md new file mode 100644 index 00000000000..b6fe8de555b --- /dev/null +++ b/lib/node_modules/@stdlib/array/base/ones2d/README.md @@ -0,0 +1,116 @@ + + +# ones2d + +> Create a two-dimensional nested array filled with ones. + + + +
+ +
+ + + + + +
+ +## Usage + +```javascript +var ones2d = require( '@stdlib/array/base/ones2d' ); +``` + +#### ones2d( shape ) + +Returns a two-dimensional nested array filled with ones. + +```javascript +var out = ones2d( [ 2, 3 ] ); +// returns [ [ 1.0, 1.0, 1.0 ], [ 1.0, 1.0, 1.0 ] ] +``` + +
+ + + + + +
+ +
+ + + + + +
+ +## Examples + + + +```javascript +var gscal = require( '@stdlib/blas/base/gscal' ); +var ones2d = require( '@stdlib/array/base/ones2d' ); + +// Create a ones-filled array: +var arr = ones2d( [ 5, 10 ] ); + +// Scale element values... +gscal( arr[ 0 ].length, 2.0, arr[ 0 ], 1 ); +gscal( arr[ 1 ].length, 3.0, arr[ 1 ], 1 ); +gscal( arr[ 2 ].length, 4.0, arr[ 2 ], 1 ); +gscal( arr[ 3 ].length, 5.0, arr[ 3 ], 1 ); +gscal( arr[ 4 ].length, 6.0, arr[ 4 ], 1 ); + +console.log( arr ); +// => [ [ 2.0, 2.0, ... ], [ 3.0, 3.0, ... ], [ 4.0, 4.0, ... ], [ 5.0, 5.0, ... ], [ 6.0, 6.0, ... ] ] +``` + +
+ + + + + +
+ +
+ + + + + + + + + + + + + + diff --git a/lib/node_modules/@stdlib/array/base/ones2d/benchmark/benchmark.size.js b/lib/node_modules/@stdlib/array/base/ones2d/benchmark/benchmark.size.js new file mode 100644 index 00000000000..3a29a1684e3 --- /dev/null +++ b/lib/node_modules/@stdlib/array/base/ones2d/benchmark/benchmark.size.js @@ -0,0 +1,96 @@ +/** +* @license Apache-2.0 +* +* Copyright (c) 2023 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 floor = require( '@stdlib/math/base/special/floor' ); +var sqrt = require( '@stdlib/math/base/special/sqrt' ); +var isArrayArray = require( '@stdlib/assert/is-array-array' ); +var pkg = require( './../package.json' ).name; +var ones2d = require( './../lib' ); + + +// FUNCTIONS // + +/** +* Creates a benchmark function. +* +* @private +* @param {PositiveInteger} N - array lengths +* @returns {Function} benchmark function +*/ +function createBenchmark( N ) { + return benchmark; + + /** + * Benchmark function. + * + * @private + * @param {Benchmark} b - benchmark instance + */ + function benchmark( b ) { + var out; + var i; + + b.tic(); + for ( i = 0; i < b.iterations; i++ ) { + out = ones2d( [ N, N ] ); + if ( typeof out !== 'object' ) { + b.fail( 'should return an array of arrays' ); + } + } + b.toc(); + if ( !isArrayArray( out ) ) { + b.fail( 'should return an array of arrays' ); + } + b.pass( 'benchmark finished' ); + b.end(); + } +} + + +// MAIN // + +/** +* Main execution sequence. +* +* @private +*/ +function main() { + var min; + var max; + var N; + var f; + var i; + + min = 1; // 10^min + max = 6; // 10^max + + for ( i = min; i <= max; i++ ) { + N = floor( sqrt( pow( 10, i ) ) ); + + f = createBenchmark( N ); + bench( pkg+'::square_matrix:size='+(N*N), f ); + } +} + +main(); diff --git a/lib/node_modules/@stdlib/array/base/ones2d/docs/repl.txt b/lib/node_modules/@stdlib/array/base/ones2d/docs/repl.txt new file mode 100644 index 00000000000..31ed962ca4f --- /dev/null +++ b/lib/node_modules/@stdlib/array/base/ones2d/docs/repl.txt @@ -0,0 +1,22 @@ + +{{alias}}( shape ) + Returns a two-dimensional nested array filled with ones. + + Parameters + ---------- + shape: Array + Array shape. + + Returns + ------- + out: Array + Output array. + + Examples + -------- + > var out = {{alias}}( [ 1, 3 ] ) + [ [ 1.0, 1.0, 1.0 ] ] + + See Also + -------- + diff --git a/lib/node_modules/@stdlib/array/base/ones2d/docs/types/index.d.ts b/lib/node_modules/@stdlib/array/base/ones2d/docs/types/index.d.ts new file mode 100644 index 00000000000..237c91d1b1b --- /dev/null +++ b/lib/node_modules/@stdlib/array/base/ones2d/docs/types/index.d.ts @@ -0,0 +1,40 @@ +/* +* @license Apache-2.0 +* +* Copyright (c) 2023 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: 2.0 + +/// + +import { Collection } from '@stdlib/types/object'; + +/** +* Returns a two-dimensional nested array filled with ones. +* +* @param shape - array shape +* @returns output array +* +* @example +* var out = ones2d( [ 1, 3 ] ); +* // returns [ [ 1.0, 1.0, 1.0 ] ] +*/ +declare function ones2d( shape: Collection ): Array>; + + +// EXPORTS // + +export = ones2d; diff --git a/lib/node_modules/@stdlib/array/base/ones2d/docs/types/test.ts b/lib/node_modules/@stdlib/array/base/ones2d/docs/types/test.ts new file mode 100644 index 00000000000..af20a56ffe6 --- /dev/null +++ b/lib/node_modules/@stdlib/array/base/ones2d/docs/types/test.ts @@ -0,0 +1,44 @@ +/* +* @license Apache-2.0 +* +* Copyright (c) 2023 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 ones2d = require( './index' ); + + +// TESTS // + +// The function returns an array of arrays... +{ + ones2d( [ 1, 3 ] ); // $ExpectType number[][] +} + +// The compiler throws an error if the function is provided a first argument which is not an array of numbers... +{ + ones2d( 'abc' ); // $ExpectError + ones2d( true ); // $ExpectError + ones2d( false ); // $ExpectError + ones2d( null ); // $ExpectError + ones2d( [ '1' ] ); // $ExpectError + ones2d( {} ); // $ExpectError + ones2d( ( x: number ): number => x ); // $ExpectError +} + +// The compiler throws an error if the function is provided an unsupported number of arguments... +{ + ones2d(); // $ExpectError + ones2d( [ 1, 3 ], 2 ); // $ExpectError +} diff --git a/lib/node_modules/@stdlib/array/base/ones2d/examples/index.js b/lib/node_modules/@stdlib/array/base/ones2d/examples/index.js new file mode 100644 index 00000000000..1f665c00c17 --- /dev/null +++ b/lib/node_modules/@stdlib/array/base/ones2d/examples/index.js @@ -0,0 +1,35 @@ +/** +* @license Apache-2.0 +* +* Copyright (c) 2023 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 gscal = require( '@stdlib/blas/base/gscal' ); +var ones2d = require( './../lib' ); + +// Create a ones-filled array: +var arr = ones2d( [ 5, 10 ] ); + +// Scale element values... +gscal( arr[ 0 ].length, 2.0, arr[ 0 ], 1 ); +gscal( arr[ 1 ].length, 3.0, arr[ 1 ], 1 ); +gscal( arr[ 2 ].length, 4.0, arr[ 2 ], 1 ); +gscal( arr[ 3 ].length, 5.0, arr[ 3 ], 1 ); +gscal( arr[ 4 ].length, 6.0, arr[ 4 ], 1 ); + +console.log( arr ); +// => [ [ 2.0, 2.0, ... ], [ 3.0, 3.0, ... ], [ 4.0, 4.0, ... ], [ 5.0, 5.0, ... ], [ 6.0, 6.0, ... ] ] diff --git a/lib/node_modules/@stdlib/array/base/ones2d/lib/index.js b/lib/node_modules/@stdlib/array/base/ones2d/lib/index.js new file mode 100644 index 00000000000..12391910da3 --- /dev/null +++ b/lib/node_modules/@stdlib/array/base/ones2d/lib/index.js @@ -0,0 +1,40 @@ +/** +* @license Apache-2.0 +* +* Copyright (c) 2023 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'; + +/** +* Create a two-dimensional nested array filled with ones. +* +* @module @stdlib/array/base/ones2d +* +* @example +* var ones2d = require( '@stdlib/array/base/ones2d' ); +* +* var out = ones2d( [ 1, 3 ] ); +* // returns [ [ 1.0, 1.0, 1.0 ] ] +*/ + +// MODULES // + +var main = require( './main.js' ); + + +// EXPORTS // + +module.exports = main; diff --git a/lib/node_modules/@stdlib/array/base/ones2d/lib/main.js b/lib/node_modules/@stdlib/array/base/ones2d/lib/main.js new file mode 100644 index 00000000000..4fa2f29c3a1 --- /dev/null +++ b/lib/node_modules/@stdlib/array/base/ones2d/lib/main.js @@ -0,0 +1,45 @@ +/** +* @license Apache-2.0 +* +* Copyright (c) 2023 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 filled2d = require( '@stdlib/array/base/filled2d' ); + + +// MAIN // + +/** +* Returns a two-dimensional nested array filled with ones. +* +* @param {NonNegativeIntegerArray} shape - array shape +* @returns {ArrayArray} filled array +* +* @example +* var out = ones2d( [ 1, 3 ] ); +* // returns [ [ 1.0, 1.0, 1.0 ] ] +*/ +function ones2d( shape ) { + return filled2d( 1.0, shape ); +} + + +// EXPORTS // + +module.exports = ones2d; diff --git a/lib/node_modules/@stdlib/array/base/ones2d/package.json b/lib/node_modules/@stdlib/array/base/ones2d/package.json new file mode 100644 index 00000000000..9035c2a7acc --- /dev/null +++ b/lib/node_modules/@stdlib/array/base/ones2d/package.json @@ -0,0 +1,70 @@ +{ + "name": "@stdlib/array/base/ones2d", + "version": "0.0.0", + "description": "Create a two-dimensional nested array filled with ones.", + "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", + "array", + "generic", + "allocate", + "alloc", + "fill", + "filled", + "ones", + "multidimensional", + "matrix", + "strided", + "ndarray", + "2d" + ] +} diff --git a/lib/node_modules/@stdlib/array/base/ones2d/test/test.js b/lib/node_modules/@stdlib/array/base/ones2d/test/test.js new file mode 100644 index 00000000000..e8b2edb94d1 --- /dev/null +++ b/lib/node_modules/@stdlib/array/base/ones2d/test/test.js @@ -0,0 +1,89 @@ +/** +* @license Apache-2.0 +* +* Copyright (c) 2023 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 ones2d = require( './../lib' ); + + +// TESTS // + +tape( 'main export is a function', function test( t ) { + t.ok( true, __filename ); + t.strictEqual( typeof ones2d, 'function', 'main export is a function' ); + t.end(); +}); + +tape( 'the function returns a filled nested array', function test( t ) { + var expected; + var actual; + + expected = [ [ 1.0, 1.0, 1.0 ] ]; + actual = ones2d( [ 1, 3 ] ); + t.deepEqual( actual, expected, 'returns expected value' ); + + expected = [ [ 1.0 ], [ 1.0 ], [ 1.0 ] ]; + actual = ones2d( [ 3, 1 ] ); + t.deepEqual( actual, expected, 'returns expected value' ); + + expected = [ [ 1.0, 1.0 ], [ 1.0, 1.0 ], [ 1.0, 1.0 ] ]; + actual = ones2d( [ 3, 2 ] ); + t.deepEqual( actual, expected, 'returns expected value' ); + + t.end(); +}); + +tape( 'the function returns an empty outer array if provided a shape having a first element equal to zero', function test( t ) { + var expected; + var actual; + + expected = []; + + actual = ones2d( [ 0, 1 ] ); + t.deepEqual( actual, expected, 'returns expected value' ); + + actual = ones2d( [ 0, 0 ] ); + t.deepEqual( actual, expected, 'returns expected value' ); + + actual = ones2d( [ 0, 100 ] ); + t.deepEqual( actual, expected, 'returns expected value' ); + + t.end(); +}); + +tape( 'the function returns empty inner arrays if provided a shape having a second element equal to zero', function test( t ) { + var expected; + var actual; + + expected = [ [] ]; + actual = ones2d( [ 1, 0 ] ); + t.deepEqual( actual, expected, 'returns expected value' ); + + expected = [ [], [] ]; + actual = ones2d( [ 2, 0 ] ); + t.deepEqual( actual, expected, 'returns expected value' ); + + expected = [ [], [], [] ]; + actual = ones2d( [ 3, 0 ] ); + t.deepEqual( actual, expected, 'returns expected value' ); + + t.end(); +});