Skip to content

Commit 6f20f70

Browse files
committed
feat: add array/base/banded/filled2d-by
--- 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 ---
1 parent 8e00e52 commit 6f20f70

File tree

10 files changed

+921
-0
lines changed

10 files changed

+921
-0
lines changed
Lines changed: 155 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,155 @@
1+
<!--
2+
3+
@license Apache-2.0
4+
5+
Copyright (c) 2025 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+
# filled2dBy
22+
23+
> Create a filled two-dimensional banded nested array according to a provided callback function.
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 filled2dBy = require( '@stdlib/array/base/banded/filled2d-by' );
41+
```
42+
43+
#### filled2dBy( shape, ku, kl, fill, clbk\[, thisArg] )
44+
45+
Returns a filled two-dimensional banded nested array according to a provided callback function.
46+
47+
```javascript
48+
function clbk( idx ) {
49+
return idx[ 0 ] + idx[ 1 ];
50+
}
51+
52+
var out = filled2dBy( [ 3, 3 ], 1, 1, 0, clbk );
53+
// returns [ [ 0, 1, 0 ], [ 1, 2, 3 ], [ 0, 3, 4 ] ]
54+
```
55+
56+
The function accepts the following arguments:
57+
58+
- **shape**: array shape.
59+
- **ku**: number of super-diagonals.
60+
- **kl**: number of sub-diagonals.
61+
- **fill**: fill value for elements outside the band.
62+
- **clbk**: callback function.
63+
- **thisArg**: callback function execution context (_optional_).
64+
65+
When invoked, a callback function is provided a single argument:
66+
67+
- **indices**: current array element indices.
68+
69+
To set the callback execution context, provide a `thisArg`.
70+
71+
<!-- eslint-disable no-invalid-this -->
72+
73+
```javascript
74+
function clbk() {
75+
this.count += 1;
76+
return 1;
77+
}
78+
79+
var ctx = {
80+
'count': 0
81+
};
82+
83+
var out = filled2dBy( [ 2, 2 ], 1, 0, 0, clbk, ctx );
84+
// returns [ [ 1, 1 ], [ 0, 1 ] ];
85+
86+
var cnt = ctx.count;
87+
// returns 3
88+
```
89+
90+
</section>
91+
92+
<!-- /.usage -->
93+
94+
<!-- Package usage notes. Make sure to keep an empty line after the `section` element and another before the `/section` close. -->
95+
96+
<section class="notes">
97+
98+
## Notes
99+
100+
- As the output array is banded, the callback function is only invoked for the elements residing within the band.
101+
102+
</section>
103+
104+
<!-- /.notes -->
105+
106+
<!-- Package usage examples. -->
107+
108+
<section class="examples">
109+
110+
## Examples
111+
112+
<!-- eslint no-undef: "error" -->
113+
114+
```javascript
115+
var constantFunction = require( '@stdlib/utils/constant-function' );
116+
var filled2dBy = require( '@stdlib/array/base/banded/filled2d-by' );
117+
118+
function clbk( indices ) {
119+
return indices[ 0 ] + indices[ 1 ];
120+
}
121+
122+
var out = filled2dBy( [ 4, 4 ], 1, 1, 0, clbk );
123+
// returns [ [ 0, 1, 0, 0 ], [ 1, 2, 3, 0 ], [ 0, 3, 4, 5 ], [ 0, 0, 5, 6 ] ]
124+
125+
out = filled2dBy( [ 3, 3 ], 1, 0, null, constantFunction( 'beep' ) );
126+
// returns [ [ 'beep', 'beep', null ], [ null, 'beep', 'beep' ], [ null, null, 'beep' ] ]
127+
```
128+
129+
</section>
130+
131+
<!-- /.examples -->
132+
133+
<!-- 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. -->
134+
135+
<section class="references">
136+
137+
</section>
138+
139+
<!-- /.references -->
140+
141+
<!-- Section for related `stdlib` packages. Do not manually edit this section, as it is automatically populated. -->
142+
143+
<section class="related">
144+
145+
</section>
146+
147+
<!-- /.related -->
148+
149+
<!-- Section for all links. Make sure to keep an empty line after the `section` element and another before the `/section` close. -->
150+
151+
<section class="links">
152+
153+
</section>
154+
155+
<!-- /.links -->
Lines changed: 104 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,104 @@
1+
/**
2+
* @license Apache-2.0
3+
*
4+
* Copyright (c) 2025 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 constantFunction = require( '@stdlib/utils/constant-function' );
29+
var pkg = require( './../package.json' ).name;
30+
var filled2dBy = require( './../lib' );
31+
32+
33+
// FUNCTIONS //
34+
35+
/**
36+
* Creates a benchmark function.
37+
*
38+
* @private
39+
* @param {PositiveInteger} N - array lengths
40+
* @param {NonNegativeInteger} ku - number of super-diagonals
41+
* @param {NonNegativeInteger} kl - number of sub-diagonals
42+
* @returns {Function} benchmark function
43+
*/
44+
function createBenchmark( N, ku, kl ) {
45+
return benchmark;
46+
47+
/**
48+
* Benchmark function.
49+
*
50+
* @private
51+
* @param {Benchmark} b - benchmark instance
52+
*/
53+
function benchmark( b ) {
54+
var out;
55+
var f;
56+
var i;
57+
58+
f = constantFunction( N );
59+
60+
b.tic();
61+
for ( i = 0; i < b.iterations; i++ ) {
62+
out = filled2dBy( [ N, N ], ku, kl, 0, f );
63+
if ( typeof out !== 'object' ) {
64+
b.fail( 'should return an array of arrays' );
65+
}
66+
}
67+
b.toc();
68+
if ( !isArrayArray( out ) ) {
69+
b.fail( 'should return an array of arrays' );
70+
}
71+
b.pass( 'benchmark finished' );
72+
b.end();
73+
}
74+
}
75+
76+
77+
// MAIN //
78+
79+
/**
80+
* Main execution sequence.
81+
*
82+
* @private
83+
*/
84+
function main() {
85+
var min;
86+
var max;
87+
var N;
88+
var f;
89+
var i;
90+
var k;
91+
92+
min = 1; // 10^min
93+
max = 6; // 10^max
94+
95+
k = 1;
96+
for ( i = min; i <= max; i++ ) {
97+
N = floor( sqrt( pow( 10, i ) ) );
98+
99+
f = createBenchmark( N, k );
100+
bench( pkg+':ku='+k+',kl='+k+',size='+(N*N), f );
101+
}
102+
}
103+
104+
main();
Lines changed: 46 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,46 @@
1+
2+
{{alias}}( shape, ku, kl, fill, clbk[, thisArg] )
3+
Returns a filled two-dimensional banded nested array according to a provided
4+
callback function.
5+
6+
The callback function is provided one argument:
7+
8+
- indices: current array element indices.
9+
10+
As the output array is banded, the provided callback is only invoked for
11+
elements within the band.
12+
13+
Parameters
14+
----------
15+
shape: Array<integer>
16+
Array shape.
17+
18+
ku: integer
19+
Number of super-diagonals.
20+
21+
kl: integer
22+
Number of sub-diagonals.
23+
24+
fill: any
25+
Fill value for elements outside the band.
26+
27+
clbk: Function
28+
Callback function.
29+
30+
thisArg: any (optional)
31+
Callback execution context.
32+
33+
Returns
34+
-------
35+
out: Array
36+
Output array.
37+
38+
Examples
39+
--------
40+
> function clbk( idx ) { return idx[ 0 ] + idx[ 1 ]; };
41+
> var out = {{alias}}( [ 3, 3 ], 1, 1, 0, clbk )
42+
[ [ 0, 1, 0 ], [ 1, 2, 3 ], [ 0, 3, 4 ] ]
43+
44+
See Also
45+
--------
46+
Lines changed: 77 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,77 @@
1+
/*
2+
* @license Apache-2.0
3+
*
4+
* Copyright (c) 2025 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 { Shape2D } from '@stdlib/types/ndarray';
24+
25+
/**
26+
* Nullary callback function.
27+
*
28+
* @returns fill value
29+
*/
30+
type Nullary<T, V> = ( this: V ) => T;
31+
32+
/**
33+
* Unary callback function.
34+
*
35+
* @param indices - current array element indices
36+
* @returns fill value
37+
*/
38+
type Unary<T, V> = ( this: V, indices: Array<number> ) => T;
39+
40+
/**
41+
* Callback function.
42+
*
43+
* @param indices - current array element indices
44+
* @returns fill value
45+
*/
46+
type Callback<T, V> = Nullary<T, V> | Unary<T, V>;
47+
48+
/**
49+
* Two-dimensional nested array.
50+
*/
51+
type Array2D<T> = Array<Array<T>>;
52+
53+
/**
54+
* Returns a filled two-dimensional banded nested array according to a provided callback function.
55+
*
56+
* @param shape - array shape
57+
* @param ku - number of super-diagonals
58+
* @param kl - number of sub-diagonals
59+
* @param fill - fill value for values outside the band
60+
* @param clbk - callback function
61+
* @param thisArg - callback function execution context
62+
* @returns output array
63+
*
64+
* @example
65+
* function clbk( indices ) {
66+
* return indices[ 0 ] + indices[ 1 ];
67+
* }
68+
*
69+
* var out = filled2dBy( [ 3, 3 ], 1, 1, 0, clbk );
70+
* // returns [ [ 0, 1, 0 ], [ 1, 2, 3 ], [ 0, 3, 4 ] ]
71+
*/
72+
declare function filled2dBy<T = unknown, U = unknown, V = unknown>( shape: Shape2D, ku: number, kl: number, fill: U, clbk: Callback<T, V>, thisArg?: ThisParameterType<Callback<T, V>> ): Array2D<T | U>;
73+
74+
75+
// EXPORTS //
76+
77+
export = filled2dBy;

0 commit comments

Comments
 (0)