Skip to content

Commit 41a2534

Browse files
committed
feat: add array/base/symmetric-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 5738d4d commit 41a2534

File tree

10 files changed

+838
-0
lines changed

10 files changed

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

0 commit comments

Comments
 (0)