Skip to content

Commit

Permalink
feat: add array/base/ones2d
Browse files Browse the repository at this point in the history
  • Loading branch information
kgryte committed Aug 10, 2023
1 parent 456fe68 commit 6afd487
Show file tree
Hide file tree
Showing 10 changed files with 597 additions and 0 deletions.
116 changes: 116 additions & 0 deletions lib/node_modules/@stdlib/array/base/ones2d/README.md
Original file line number Diff line number Diff line change
@@ -0,0 +1,116 @@
<!--
@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.
-->

# ones2d

> Create a two-dimensional nested array filled with ones.
<!-- Section to include introductory text. Make sure to keep an empty line after the intro `section` element and another before the `/section` close. -->

<section class="intro">

</section>

<!-- /.intro -->

<!-- Package usage documentation. -->

<section class="usage">

## 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 ] ]
```

</section>

<!-- /.usage -->

<!-- Package usage notes. Make sure to keep an empty line after the `section` element and another before the `/section` close. -->

<section class="notes">

</section>

<!-- /.notes -->

<!-- Package usage examples. -->

<section class="examples">

## Examples

<!-- eslint no-undef: "error" -->

```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 );

This comment has been minimized.

Copy link
@Planeshifter

Planeshifter Aug 10, 2023

Member

This example is a bit confusing; IMO, the example worked for the utility that creates an array with 0 as these are commonly used as placeholder values, but here the question comes up why one would fill with 1 in the first place. Maybe remove one of the gscal calls to leave one row of ones here?

This comment has been minimized.

Copy link
@kgryte

kgryte Aug 10, 2023

Author Member

@Planeshifter How else does one get nested arrays starting at 2 and ending with 6? At least in NumPy code, it is fairly common to leverage the multiplicative identity to fill an ndarray with value patterns. E.g.,

x = np.ones((4,4))
x[0,:] *= 2.0
x[1,:] *= 3.0
x[2,:] *= 4.0
x[3,:] *= 5.0

This comment has been minimized.

Copy link
@Planeshifter

Planeshifter Aug 10, 2023

Member

Yeah, makes sense! Thanks for the explanation.

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, ... ] ]
```

</section>

<!-- /.examples -->

<!-- Section to include cited references. If references are included, add a horizontal rule *before* the section. Make sure to keep an empty line after the `section` element and another before the `/section` close. -->

<section class="references">

</section>

<!-- /.references -->

<!-- Section for related `stdlib` packages. Do not manually edit this section, as it is automatically populated. -->

<section class="related">

</section>

<!-- /.related -->

<!-- Section for all links. Make sure to keep an empty line after the `section` element and another before the `/section` close. -->

<section class="links">

</section>

<!-- /.links -->
Original file line number Diff line number Diff line change
@@ -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();
22 changes: 22 additions & 0 deletions lib/node_modules/@stdlib/array/base/ones2d/docs/repl.txt
Original file line number Diff line number Diff line change
@@ -0,0 +1,22 @@

{{alias}}( shape )
Returns a two-dimensional nested array filled with ones.

Parameters
----------
shape: Array<integer>
Array shape.

Returns
-------
out: Array
Output array.

Examples
--------
> var out = {{alias}}( [ 1, 3 ] )
[ [ 1.0, 1.0, 1.0 ] ]

See Also
--------

40 changes: 40 additions & 0 deletions lib/node_modules/@stdlib/array/base/ones2d/docs/types/index.d.ts
Original file line number Diff line number Diff line change
@@ -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

/// <reference types="@stdlib/types"/>

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<number> ): Array<Array<number>>;


// EXPORTS //

export = ones2d;
44 changes: 44 additions & 0 deletions lib/node_modules/@stdlib/array/base/ones2d/docs/types/test.ts
Original file line number Diff line number Diff line change
@@ -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
}
35 changes: 35 additions & 0 deletions lib/node_modules/@stdlib/array/base/ones2d/examples/index.js
Original file line number Diff line number Diff line change
@@ -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, ... ] ]
40 changes: 40 additions & 0 deletions lib/node_modules/@stdlib/array/base/ones2d/lib/index.js
Original file line number Diff line number Diff line change
@@ -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;
Loading

0 comments on commit 6afd487

Please sign in to comment.