Skip to content

Commit f9bb0d7

Browse files
committed
feat: add array/base/symmetric-banded/to-compact
--- 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 b24dd4f commit f9bb0d7

File tree

10 files changed

+1069
-0
lines changed

10 files changed

+1069
-0
lines changed
Lines changed: 166 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,166 @@
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+
# toCompact
22+
23+
> Convert a two-dimensional symmetric banded nested array to compact banded storage.
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 toCompact = require( '@stdlib/array/base/symmetric-banded/to-compact' );
41+
```
42+
43+
#### toCompact( uplo, arr, k, colexicographic )
44+
45+
Converts a two-dimensional symmetric banded nested array to compact banded storage.
46+
47+
```javascript
48+
var x = [ [ -1, 2, 0 ], [ 2, -2, 4 ], [ 0, 4, -3 ] ];
49+
50+
var out = toCompact( 'upper', x, 1, false );
51+
// returns [ [ 0, 2, 4 ], [ -1, -2, -3 ] ]
52+
```
53+
54+
The function accepts the following arguments:
55+
56+
- **uplo**: specifies whether to reference the upper or lower triangular part of the input array.
57+
- **arr**: input two-dimensional nested array.
58+
- **k**: number of super-/sub-diagonals.
59+
- **colexicographic**: boolean specifying whether to store diagonals in colexicographic access order.
60+
61+
</section>
62+
63+
<!-- /.usage -->
64+
65+
<!-- Package usage notes. Make sure to keep an empty line after the `section` element and another before the `/section` close. -->
66+
67+
<section class="notes">
68+
69+
</section>
70+
71+
<!-- /.notes -->
72+
73+
<!-- Package usage examples. -->
74+
75+
<section class="examples">
76+
77+
## Examples
78+
79+
<!-- eslint-disable no-multi-spaces -->
80+
81+
<!-- eslint no-undef: "error" -->
82+
83+
```javascript
84+
var toCompact = require( '@stdlib/array/base/symmetric-banded/to-compact' );
85+
86+
// Define a symmetric banded matrix:
87+
var A = [
88+
[ 1, 2, 3, 0, 0 ],
89+
[ 2, 4, 5, 6, 0 ],
90+
[ 3, 5, 7, 8, 9 ],
91+
[ 0, 6, 8, 10, 11 ],
92+
[ 0, 0, 9, 11, 12 ]
93+
];
94+
95+
// Convert the upper triangle to lexicographic compact form:
96+
var AC = toCompact( 'upper', A, 2, false );
97+
/* e.g., returns =>
98+
[
99+
[ 0, 0, 3, 6, 9 ],
100+
[ 0, 2, 5, 8, 11 ],
101+
[ 1, 4, 7, 10, 12 ]
102+
]
103+
*/
104+
105+
// Convert the lower triangle to lexicographic compact form:
106+
AC = toCompact( 'lower', A, 2, false );
107+
/* e.g., returns =>
108+
[
109+
[ 1, 4, 7, 10, 12 ],
110+
[ 2, 5, 8, 11, 0 ],
111+
[ 3, 6, 9, 0, 0 ]
112+
]
113+
*/
114+
115+
// Convert the upper triangle to colexicographic compact form:
116+
AC = toCompact( 'upper', A, 2, true );
117+
/* e.g., returns =>
118+
[
119+
[ 1, 2, 3 ],
120+
[ 4, 5, 6 ],
121+
[ 7, 8, 9 ],
122+
[ 10, 11, 0 ],
123+
[ 12, 0, 0 ]
124+
]
125+
*/
126+
127+
// Convert the lower triangle to colexicographic compact form:
128+
AC = toCompact( 'lower', A, 2, true );
129+
/* e.g., returns =>
130+
[
131+
[ 0, 0, 1 ],
132+
[ 0, 2, 4 ],
133+
[ 3, 5, 7 ],
134+
[ 6, 8, 10 ],
135+
[ 9, 11, 12 ]
136+
]
137+
*/
138+
```
139+
140+
</section>
141+
142+
<!-- /.examples -->
143+
144+
<!-- 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. -->
145+
146+
<section class="references">
147+
148+
</section>
149+
150+
<!-- /.references -->
151+
152+
<!-- Section for related `stdlib` packages. Do not manually edit this section, as it is automatically populated. -->
153+
154+
<section class="related">
155+
156+
</section>
157+
158+
<!-- /.related -->
159+
160+
<!-- Section for all links. Make sure to keep an empty line after the `section` element and another before the `/section` close. -->
161+
162+
<section class="links">
163+
164+
</section>
165+
166+
<!-- /.links -->
Lines changed: 123 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,123 @@
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 filled2dBy = require( '@stdlib/array/base/symmetric-banded/filled2d-by' );
30+
var format = require( '@stdlib/string/format' );
31+
var pkg = require( './../package.json' ).name;
32+
var toCompact = require( './../lib' );
33+
34+
35+
// VARIABLES //
36+
37+
var orders = [
38+
true,
39+
false
40+
];
41+
var uplo = [
42+
'upper',
43+
'lower'
44+
];
45+
46+
47+
// FUNCTIONS //
48+
49+
/**
50+
* Creates a benchmark function.
51+
*
52+
* @private
53+
* @param {PositiveInteger} N - array lengths
54+
* @param {string} uplo - specifies whether to reference to upper or lower triangle
55+
* @param {boolean} colexicographic - boolean indicating whether to store diagonals in colexicographic access order
56+
* @param {NonNegativeInteger} k - number of super-/sub-diagonals
57+
* @returns {Function} benchmark function
58+
*/
59+
function createBenchmark( N, uplo, colexicographic, k ) {
60+
var x = filled2dBy( N, k, 0, constantFunction( N ) );
61+
return benchmark;
62+
63+
/**
64+
* Benchmark function.
65+
*
66+
* @private
67+
* @param {Benchmark} b - benchmark instance
68+
*/
69+
function benchmark( b ) {
70+
var out;
71+
var i;
72+
73+
b.tic();
74+
for ( i = 0; i < b.iterations; i++ ) {
75+
out = toCompact( uplo, x, k, colexicographic );
76+
if ( typeof out !== 'object' ) {
77+
b.fail( 'should return an array of arrays' );
78+
}
79+
}
80+
b.toc();
81+
if ( !isArrayArray( out ) ) {
82+
b.fail( 'should return an array of arrays' );
83+
}
84+
b.pass( 'benchmark finished' );
85+
b.end();
86+
}
87+
}
88+
89+
90+
// MAIN //
91+
92+
/**
93+
* Main execution sequence.
94+
*
95+
* @private
96+
*/
97+
function main() {
98+
var min;
99+
var max;
100+
var N;
101+
var f;
102+
var i;
103+
var o;
104+
var u;
105+
var k;
106+
107+
min = 1; // 10^min
108+
max = 6; // 10^max
109+
110+
k = 2;
111+
for ( u = 0; u < uplo.length; u++ ) {
112+
for ( o = 0; o < orders.length; o++ ) {
113+
for ( i = min; i <= max; i++ ) {
114+
N = floor( sqrt( pow( 10, i ) ) );
115+
116+
f = createBenchmark( N, uplo[ u ], orders[ o ], k );
117+
bench( format( '%s:colexicographic=%s,uplo=%s,k=%d,size=%d', pkg, orders[ o ], uplo[ u ], k, N*N ), f );
118+
}
119+
}
120+
}
121+
}
122+
123+
main();
Lines changed: 34 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,34 @@
1+
2+
{{alias}}( uplo, arr, k, colexicographic )
3+
Converts a two-dimensional symmetric banded nested array to compact banded
4+
storage.
5+
6+
Parameters
7+
----------
8+
uplo: string
9+
Specifies whether to reference to the upper or lower triangular part of
10+
the input array. Must be either 'upper' or 'lower'.
11+
12+
arr: Array<Array>
13+
Input array.
14+
15+
k: integer
16+
Number of super-/sub-diagonals.
17+
18+
colexicographic: boolean
19+
Specifies whether to store diagonals in colexicographic access order.
20+
21+
Returns
22+
-------
23+
out: Array
24+
Output array.
25+
26+
Examples
27+
--------
28+
> var x = [ [ -1, 2, 0 ], [ 2, -3, 4 ], [ 0, 4, -5 ] ];
29+
> var out = {{alias}}( 'upper', x, 1, false )
30+
[ [ 0, 2, 4 ], [ -1, -3, -5 ] ]
31+
32+
See Also
33+
--------
34+

0 commit comments

Comments
 (0)