Skip to content

Commit 96b7277

Browse files
committed
feat: add blas/ext/base/gtriu2tril
--- 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_pkg_readmes status: passed - task: lint_markdown_docs status: na - task: lint_markdown status: na - 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 b5be51a commit 96b7277

16 files changed

Lines changed: 2664 additions & 0 deletions

File tree

Lines changed: 191 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,191 @@
1+
<!--
2+
3+
@license Apache-2.0
4+
5+
Copyright (c) 2026 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+
# gtriu2tril
22+
23+
> Reflect the upper triangular part of a matrix A into the lower triangular part of another matrix B.
24+
25+
<section class="usage">
26+
27+
## Usage
28+
29+
```javascript
30+
var gtriu2tril = require( '@stdlib/blas/ext/base/gtriu2tril' );
31+
```
32+
33+
#### gtriu2tril( order, M, N, k, A, LDA, B, LDB )
34+
35+
Reflects the upper triangular part of a matrix `A` into the lower triangular part of another matrix `B`.
36+
37+
```javascript
38+
var A = [ 1.0, 2.0, 3.0, 4.0 ];
39+
var B = [ 0.0, 0.0, 0.0, 0.0 ];
40+
41+
gtriu2tril( 'row-major', 2, 2, 0, A, 2, B, 2 );
42+
// B => [ 1.0, 0.0, 2.0, 4.0 ]
43+
```
44+
45+
The function has the following parameters:
46+
47+
- **order**: storage layout.
48+
- **M**: number of rows in `A`.
49+
- **N**: number of columns in `A`.
50+
- **k**: diagonal below which to ignore. A value of `k = 0` refers to the main diagonal and `k > 0` refers to a diagonal above the main diagonal. When `k > 0`, the function reflects only part of the upper triangle, excluding the main diagonal and the first `k-1` super-diagonals.
51+
- **A**: input matrix.
52+
- **LDA**: stride of the first dimension of `A` (a.k.a., leading dimension of the matrix `A`).
53+
- **B**: output matrix.
54+
- **LDB**: stride of the first dimension of `B` (a.k.a., leading dimension of the matrix `B`).
55+
56+
Setting the `k` parameter to a value greater than `0` excludes the main diagonal (and, for larger values, additional super-diagonals). For example, to reflect only the elements strictly above the main diagonal,
57+
58+
```javascript
59+
var A = [ 1.0, 2.0, 3.0, 4.0 ];
60+
var B = [ 0.0, 0.0, 0.0, 0.0 ];
61+
62+
gtriu2tril( 'row-major', 2, 2, 1, A, 2, B, 2 );
63+
// B => [ 0.0, 0.0, 2.0, 0.0 ]
64+
```
65+
66+
Note that indexing is relative to the first index. To introduce an offset, use [`typed array`][mdn-typed-array] views.
67+
68+
<!-- eslint-disable stdlib/capitalized-comments -->
69+
70+
```javascript
71+
var Float64Array = require( '@stdlib/array/float64' );
72+
73+
// Initial arrays...
74+
var A0 = new Float64Array( [ 1.0, 2.0, 3.0, 4.0, 5.0 ] );
75+
var B0 = new Float64Array( 5 );
76+
77+
// Create offset views...
78+
var A1 = new Float64Array( A0.buffer, A0.BYTES_PER_ELEMENT*1 ); // start at 2nd element
79+
var B1 = new Float64Array( B0.buffer, B0.BYTES_PER_ELEMENT*1 ); // start at 2nd element
80+
81+
gtriu2tril( 'row-major', 2, 2, 0, A1, 2, B1, 2 );
82+
// B0 => <Float64Array>[ 0.0, 2.0, 0.0, 3.0, 5.0 ]
83+
```
84+
85+
#### gtriu2tril.ndarray( M, N, k, A, sa1, sa2, oa, B, sb1, sb2, ob )
86+
87+
Reflects the upper triangular part of a matrix `A` into the lower triangular part of another matrix `B` using alternative indexing semantics.
88+
89+
```javascript
90+
var A = [ 1.0, 2.0, 3.0, 4.0 ];
91+
var B = [ 0.0, 0.0, 0.0, 0.0 ];
92+
93+
gtriu2tril.ndarray( 2, 2, 0, A, 2, 1, 0, B, 2, 1, 0 );
94+
// B => [ 1.0, 0.0, 2.0, 4.0 ]
95+
```
96+
97+
The function has the following parameters:
98+
99+
- **M**: number of rows in `A`.
100+
- **N**: number of columns in `A`.
101+
- **k**: diagonal below which to ignore.
102+
- **A**: input matrix.
103+
- **sa1**: stride of the first dimension of `A`.
104+
- **sa2**: stride of the second dimension of `A`.
105+
- **oa**: starting index for `A`.
106+
- **B**: output matrix.
107+
- **sb1**: stride of the first dimension of `B`.
108+
- **sb2**: stride of the second dimension of `B`.
109+
- **ob**: starting index for `B`.
110+
111+
While [`typed array`][mdn-typed-array] views mandate a view offset based on the underlying buffer, the offset parameters support indexing semantics based on starting indices. For example,
112+
113+
```javascript
114+
var A = [ 1.0, 2.0, 3.0, 4.0 ];
115+
var B = [ 0.0, 0.0, 0.0, 0.0, 0.0, 0.0 ];
116+
117+
gtriu2tril.ndarray( 2, 2, 0, A, 2, 1, 0, B, 2, 1, 2 );
118+
// B => [ 0.0, 0.0, 1.0, 0.0, 2.0, 4.0 ]
119+
```
120+
121+
</section>
122+
123+
<!-- /.usage -->
124+
125+
<section class="notes">
126+
127+
## Notes
128+
129+
- Both functions support array-like objects having getter and setter accessors for array element access (e.g., [`@stdlib/array/base/accessor`][@stdlib/array/base/accessor]).
130+
- Elements outside of the reflected region are left unchanged.
131+
132+
</section>
133+
134+
<!-- /.notes -->
135+
136+
<section class="examples">
137+
138+
## Examples
139+
140+
<!-- eslint no-undef: "error" -->
141+
142+
<!-- eslint-disable max-len -->
143+
144+
```javascript
145+
var ndarray2array = require( '@stdlib/ndarray/base/to-array' );
146+
var uniform = require( '@stdlib/random/array/discrete-uniform' );
147+
var numel = require( '@stdlib/ndarray/base/numel' );
148+
var shape2strides = require( '@stdlib/ndarray/base/shape2strides' );
149+
var gtriu2tril = require( '@stdlib/blas/ext/base/gtriu2tril' );
150+
151+
var shape = [ 5, 8 ];
152+
var order = 'row-major';
153+
var strides = shape2strides( shape, order );
154+
155+
var N = numel( shape );
156+
157+
var A = uniform( N, -10, 10, {
158+
'dtype': 'generic'
159+
});
160+
console.log( ndarray2array( A, shape, strides, 0, order ) );
161+
162+
var B = uniform( N, -10, 10, {
163+
'dtype': 'generic'
164+
});
165+
var shapeB = [ shape[ 1 ], shape[ 0 ] ];
166+
var stridesB = shape2strides( shapeB, order );
167+
console.log( ndarray2array( B, shapeB, stridesB, 0, order ) );
168+
169+
gtriu2tril( order, shape[ 0 ], shape[ 1 ], 0, A, strides[ 0 ], B, stridesB[ 0 ] );
170+
console.log( ndarray2array( B, shapeB, stridesB, 0, order ) );
171+
```
172+
173+
</section>
174+
175+
<!-- /.examples -->
176+
177+
<section class="related">
178+
179+
</section>
180+
181+
<!-- /.related -->
182+
183+
<section class="links">
184+
185+
[mdn-typed-array]: https://developer.mozilla.org/en-US/docs/Web/JavaScript/Reference/Global_Objects/TypedArray
186+
187+
[@stdlib/array/base/accessor]: https://github.com/stdlib-js/stdlib/tree/develop/lib/node_modules/%40stdlib/array/base/accessor
188+
189+
</section>
190+
191+
<!-- /.links -->
Lines changed: 112 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,112 @@
1+
/**
2+
* @license Apache-2.0
3+
*
4+
* Copyright (c) 2026 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 isnan = require( '@stdlib/math/base/assert/is-nan' );
25+
var zeros = require( '@stdlib/array/zeros' );
26+
var pow = require( '@stdlib/math/base/special/pow' );
27+
var floor = require( '@stdlib/math/base/special/floor' );
28+
var format = require( '@stdlib/string/format' );
29+
var pkg = require( './../package.json' ).name;
30+
var gtriu2tril = require( './../lib' );
31+
32+
33+
// VARIABLES //
34+
35+
var LAYOUTS = [
36+
'row-major',
37+
'column-major'
38+
];
39+
40+
41+
// FUNCTIONS //
42+
43+
/**
44+
* Creates a benchmark function.
45+
*
46+
* @private
47+
* @param {string} order - storage layout
48+
* @param {PositiveInteger} N - number of elements along each dimension
49+
* @returns {Function} benchmark function
50+
*/
51+
function createBenchmark( order, N ) {
52+
var A = zeros( N*N, 'generic' );
53+
var B = zeros( N*N, 'generic' );
54+
return benchmark;
55+
56+
/**
57+
* Benchmark function.
58+
*
59+
* @private
60+
* @param {Benchmark} b - benchmark instance
61+
*/
62+
function benchmark( b ) {
63+
var z;
64+
var i;
65+
66+
b.tic();
67+
for ( i = 0; i < b.iterations; i++ ) {
68+
z = gtriu2tril( order, N, N, 0, A, N, B, N );
69+
if ( isnan( z[ i%z.length ] ) ) {
70+
b.fail( 'should not return NaN' );
71+
}
72+
}
73+
b.toc();
74+
if ( isnan( z[ i%z.length ] ) ) {
75+
b.fail( 'should not return NaN' );
76+
}
77+
b.pass( 'benchmark finished' );
78+
b.end();
79+
}
80+
}
81+
82+
83+
// MAIN //
84+
85+
/**
86+
* Main execution sequence.
87+
*
88+
* @private
89+
*/
90+
function main() {
91+
var min;
92+
var max;
93+
var ord;
94+
var N;
95+
var f;
96+
var i;
97+
var k;
98+
99+
min = 1; // 10^min
100+
max = 6; // 10^max
101+
102+
for ( k = 0; k < LAYOUTS.length; k++ ) {
103+
ord = LAYOUTS[ k ];
104+
for ( i = min; i <= max; i++ ) {
105+
N = floor( pow( pow( 10, i ), 1.0/2.0 ) );
106+
f = createBenchmark( ord, N );
107+
bench( format( '%s::square_matrix:order=%s,size=%d', pkg, ord, N*N ), f );
108+
}
109+
}
110+
}
111+
112+
main();

0 commit comments

Comments
 (0)