Skip to content

Commit 3e8d3dc

Browse files
committed
feat: add array/base/filled2d
1 parent d52e01c commit 3e8d3dc

File tree

10 files changed

+636
-0
lines changed

10 files changed

+636
-0
lines changed
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,117 @@
1+
<!--
2+
3+
@license Apache-2.0
4+
5+
Copyright (c) 2023 The Stdlib Authors.
6+
7+
Licensed under the Apache License, Version 2.0 (the "License");
8+
you may not use this file except in compliance with the License.
9+
You may obtain a copy of the License at
10+
11+
http://www.apache.org/licenses/LICENSE-2.0
12+
13+
Unless required by applicable law or agreed to in writing, software
14+
distributed under the License is distributed on an "AS IS" BASIS,
15+
WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
16+
See the License for the specific language governing permissions and
17+
limitations under the License.
18+
19+
-->
20+
21+
# filled2d
22+
23+
> Create a filled two-dimensional nested array.
24+
25+
<!-- Section to include introductory text. Make sure to keep an empty line after the intro `section` element and another before the `/section` close. -->
26+
27+
<section class="intro">
28+
29+
</section>
30+
31+
<!-- /.intro -->
32+
33+
<!-- Package usage documentation. -->
34+
35+
<section class="usage">
36+
37+
## Usage
38+
39+
```javascript
40+
var filled2d = require( '@stdlib/array/base/filled2d' );
41+
```
42+
43+
#### filled2d( value, shape )
44+
45+
Returns a filled two-dimensional nested array.
46+
47+
```javascript
48+
var out = filled2d( 0.0, [ 2, 3 ] );
49+
// returns [ [ 0.0, 0.0, 0.0 ], [ 0.0, 0.0, 0.0 ] ]
50+
```
51+
52+
</section>
53+
54+
<!-- /.usage -->
55+
56+
<!-- Package usage notes. Make sure to keep an empty line after the `section` element and another before the `/section` close. -->
57+
58+
<section class="notes">
59+
60+
</section>
61+
62+
<!-- /.notes -->
63+
64+
<!-- Package usage examples. -->
65+
66+
<section class="examples">
67+
68+
## Examples
69+
70+
<!-- eslint no-undef: "error" -->
71+
72+
```javascript
73+
var filled2d = require( '@stdlib/array/base/filled2d' );
74+
75+
var out = filled2d( 0.0, [ 1, 3 ] );
76+
// returns [ [ 0.0, 0.0, 0.0 ] ]
77+
78+
out = filled2d( 'beep', [ 3, 1 ] );
79+
// returns [ [ 'beep' ], [ 'beep' ], [ 'beep' ] ]
80+
81+
out = filled2d( null, [ 1, 3 ] );
82+
// returns [ [ null, null, null ] ]
83+
84+
out = filled2d( true, [ 3, 1 ] );
85+
// returns [ [ true ], [ true ], [ true ] ]
86+
87+
out = filled2d( void 0, [ 1, 3 ] );
88+
// returns [ [ undefined, undefined, undefined ] ]
89+
```
90+
91+
</section>
92+
93+
<!-- /.examples -->
94+
95+
<!-- 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. -->
96+
97+
<section class="references">
98+
99+
</section>
100+
101+
<!-- /.references -->
102+
103+
<!-- Section for related `stdlib` packages. Do not manually edit this section, as it is automatically populated. -->
104+
105+
<section class="related">
106+
107+
</section>
108+
109+
<!-- /.related -->
110+
111+
<!-- Section for all links. Make sure to keep an empty line after the `section` element and another before the `/section` close. -->
112+
113+
<section class="links">
114+
115+
</section>
116+
117+
<!-- /.links -->
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,96 @@
1+
/**
2+
* @license Apache-2.0
3+
*
4+
* Copyright (c) 2023 The Stdlib Authors.
5+
*
6+
* Licensed under the Apache License, Version 2.0 (the "License");
7+
* you may not use this file except in compliance with the License.
8+
* You may obtain a copy of the License at
9+
*
10+
* http://www.apache.org/licenses/LICENSE-2.0
11+
*
12+
* Unless required by applicable law or agreed to in writing, software
13+
* distributed under the License is distributed on an "AS IS" BASIS,
14+
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
15+
* See the License for the specific language governing permissions and
16+
* limitations under the License.
17+
*/
18+
19+
'use strict';
20+
21+
// MODULES //
22+
23+
var bench = require( '@stdlib/bench' );
24+
var pow = require( '@stdlib/math/base/special/pow' );
25+
var floor = require( '@stdlib/math/base/special/floor' );
26+
var sqrt = require( '@stdlib/math/base/special/sqrt' );
27+
var isArrayArray = require( '@stdlib/assert/is-array-array' );
28+
var pkg = require( './../package.json' ).name;
29+
var filled2d = require( './../lib' );
30+
31+
32+
// FUNCTIONS //
33+
34+
/**
35+
* Creates a benchmark function.
36+
*
37+
* @private
38+
* @param {PositiveInteger} N - array lengths
39+
* @returns {Function} benchmark function
40+
*/
41+
function createBenchmark( N ) {
42+
return benchmark;
43+
44+
/**
45+
* Benchmark function.
46+
*
47+
* @private
48+
* @param {Benchmark} b - benchmark instance
49+
*/
50+
function benchmark( b ) {
51+
var out;
52+
var i;
53+
54+
b.tic();
55+
for ( i = 0; i < b.iterations; i++ ) {
56+
out = filled2d( i, [ N, N ] );
57+
if ( typeof out !== 'object' ) {
58+
b.fail( 'should return an array of arrays' );
59+
}
60+
}
61+
b.toc();
62+
if ( !isArrayArray( out ) ) {
63+
b.fail( 'should return an array of arrays' );
64+
}
65+
b.pass( 'benchmark finished' );
66+
b.end();
67+
}
68+
}
69+
70+
71+
// MAIN //
72+
73+
/**
74+
* Main execution sequence.
75+
*
76+
* @private
77+
*/
78+
function main() {
79+
var min;
80+
var max;
81+
var N;
82+
var f;
83+
var i;
84+
85+
min = 1; // 10^min
86+
max = 6; // 10^max
87+
88+
for ( i = min; i <= max; i++ ) {
89+
N = floor( sqrt( pow( 10, i ) ) );
90+
91+
f = createBenchmark( N );
92+
bench( pkg+'::square_matrix:size='+(N*N), f );
93+
}
94+
}
95+
96+
main();
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,25 @@
1+
2+
{{alias}}( value, shape )
3+
Returns a filled two-dimensional nested array.
4+
5+
Parameters
6+
----------
7+
value: any
8+
Fill value.
9+
10+
shape: Array<integer>
11+
Array shape.
12+
13+
Returns
14+
-------
15+
out: Array
16+
Output array.
17+
18+
Examples
19+
--------
20+
> var out = {{alias}}( 0.0, [ 1, 3 ] )
21+
[ [ 0.0, 0.0, 0.0 ] ]
22+
23+
See Also
24+
--------
25+
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,45 @@
1+
/*
2+
* @license Apache-2.0
3+
*
4+
* Copyright (c) 2023 The Stdlib Authors.
5+
*
6+
* Licensed under the Apache License, Version 2.0 (the "License");
7+
* you may not use this file except in compliance with the License.
8+
* You may obtain a copy of the License at
9+
*
10+
* http://www.apache.org/licenses/LICENSE-2.0
11+
*
12+
* Unless required by applicable law or agreed to in writing, software
13+
* distributed under the License is distributed on an "AS IS" BASIS,
14+
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
15+
* See the License for the specific language governing permissions and
16+
* limitations under the License.
17+
*/
18+
19+
// TypeScript Version: 4.1
20+
21+
/// <reference types="@stdlib/types"/>
22+
23+
import { Collection } from '@stdlib/types/object';
24+
25+
/**
26+
* Returns a filled two-dimensional nested array.
27+
*
28+
* @param value - fill value
29+
* @param shape - array shape
30+
* @returns output array
31+
*
32+
* @example
33+
* var out = filled2d( 0.0, [ 1, 3 ] );
34+
* // returns [ [ 0.0, 0.0, 0.0 ] ]
35+
*
36+
* @example
37+
* var out = filled2d( 'beep', [ 1, 3 ] );
38+
* // returns [ [ 'beep', 'beep', 'beep' ] ]
39+
*/
40+
declare function filled2d<T = unknown>( value: T, shape: Collection<number> ): Array<Array<T>>;
41+
42+
43+
// EXPORTS //
44+
45+
export = filled2d;
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,45 @@
1+
/*
2+
* @license Apache-2.0
3+
*
4+
* Copyright (c) 2023 The Stdlib Authors.
5+
*
6+
* Licensed under the Apache License, Version 2.0 (the "License");
7+
* you may not use this file except in compliance with the License.
8+
* You may obtain a copy of the License at
9+
*
10+
* http://www.apache.org/licenses/LICENSE-2.0
11+
*
12+
* Unless required by applicable law or agreed to in writing, software
13+
* distributed under the License is distributed on an "AS IS" BASIS,
14+
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
15+
* See the License for the specific language governing permissions and
16+
* limitations under the License.
17+
*/
18+
19+
import filled2d = require( './index' );
20+
21+
22+
// TESTS //
23+
24+
// The function returns an array of arrays...
25+
{
26+
filled2d( 0.0, [ 1, 3 ] ); // $ExpectType number[][]
27+
}
28+
29+
// The compiler throws an error if the function is provided a second argument which is not an array of numbers...
30+
{
31+
filled2d( 0.0, 'abc' ); // $ExpectError
32+
filled2d( 0.0, true ); // $ExpectError
33+
filled2d( 0.0, false ); // $ExpectError
34+
filled2d( 0.0, null ); // $ExpectError
35+
filled2d( 0.0, [ '1' ] ); // $ExpectError
36+
filled2d( 0.0, {} ); // $ExpectError
37+
filled2d( 0.0, ( x: number ): number => x ); // $ExpectError
38+
}
39+
40+
// The compiler throws an error if the function is provided an unsupported number of arguments...
41+
{
42+
filled2d(); // $ExpectError
43+
filled2d( 0.0 ); // $ExpectError
44+
filled2d( 0.0, [ 1, 3 ], 2 ); // $ExpectError
45+
}
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,41 @@
1+
/**
2+
* @license Apache-2.0
3+
*
4+
* Copyright (c) 2023 The Stdlib Authors.
5+
*
6+
* Licensed under the Apache License, Version 2.0 (the "License");
7+
* you may not use this file except in compliance with the License.
8+
* You may obtain a copy of the License at
9+
*
10+
* http://www.apache.org/licenses/LICENSE-2.0
11+
*
12+
* Unless required by applicable law or agreed to in writing, software
13+
* distributed under the License is distributed on an "AS IS" BASIS,
14+
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
15+
* See the License for the specific language governing permissions and
16+
* limitations under the License.
17+
*/
18+
19+
'use strict';
20+
21+
var filled2d = require( './../lib' );
22+
23+
var out = filled2d( 0.0, [ 1, 3 ] );
24+
console.log( out );
25+
// => [ [ 0.0, 0.0, 0.0 ] ]
26+
27+
out = filled2d( 'beep', [ 3, 1 ] );
28+
console.log( out );
29+
// => [ [ 'beep' ], [ 'beep' ], [ 'beep' ] ]
30+
31+
out = filled2d( null, [ 1, 3 ] );
32+
console.log( out );
33+
// => [ [ null, null, null ] ]
34+
35+
out = filled2d( true, [ 3, 1 ] );
36+
console.log( out );
37+
// => [ [ true ], [ true ], [ true ] ]
38+
39+
out = filled2d( void 0, [ 1, 3 ] );
40+
console.log( out );
41+
// => [ [ undefined, undefined, undefined ] ]

0 commit comments

Comments
 (0)