Skip to content

Commit 375701a

Browse files
authored
feat: add blas/ext/base/ndarray/ctriu
PR-URL: #14104 Reviewed-by: Athan Reines <kgryte@gmail.com> Signed-off-by: Kaustubh Patange <kaustubh.mp007@gmail.com>
1 parent a6dc0eb commit 375701a

10 files changed

Lines changed: 920 additions & 0 deletions

File tree

Lines changed: 136 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,136 @@
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+
# ctriu
22+
23+
> Copy the upper triangular part of a single-precision complex floating-point matrix `A` to another matrix `B`.
24+
25+
<section class="intro">
26+
27+
</section>
28+
29+
<!-- /.intro -->
30+
31+
<section class="usage">
32+
33+
## Usage
34+
35+
```javascript
36+
var ctriu = require( '@stdlib/blas/ext/base/ndarray/ctriu' );
37+
```
38+
39+
#### ctriu( arrays )
40+
41+
Copies the upper triangular part of a single-precision complex floating-point matrix `A` to another matrix `B`.
42+
43+
<!-- eslint-disable max-len -->
44+
45+
```javascript
46+
var Complex64Matrix = require( '@stdlib/ndarray/matrix/complex64' );
47+
var scalar2ndarray = require( '@stdlib/ndarray/from-scalar' );
48+
49+
var A = new Complex64Matrix( [ [ 1.0, 2.0, 3.0, 4.0 ], [ 5.0, 6.0, 7.0, 8.0 ] ] );
50+
var B = new Complex64Matrix( [ [ 0.0, 0.0, 0.0, 0.0 ], [ 0.0, 0.0, 0.0, 0.0 ] ] );
51+
52+
var k = scalar2ndarray( 0, {
53+
'dtype': 'generic'
54+
});
55+
56+
var out = ctriu( [ A, B, k ] );
57+
// returns <ndarray>[ [ <Complex64>[ 1.0, 2.0 ], <Complex64>[ 3.0, 4.0 ] ], [ <Complex64>[ 0.0, 0.0 ], <Complex64>[ 7.0, 8.0 ] ] ]
58+
59+
var bool = ( out === B );
60+
// returns true
61+
```
62+
63+
The function has the following parameters:
64+
65+
- **arrays**: array-like object containing the following ndarrays:
66+
67+
- a two-dimensional input ndarray corresponding to `A`.
68+
- a two-dimensional output ndarray corresponding to `B`.
69+
- a zero-dimensional ndarray specifying the diagonal below which to ignore. A value equal to zero refers to the main diagonal, greater than zero refers to a diagonal above the main diagonal, and less than zero refers to a diagonal below the main diagonal.
70+
71+
</section>
72+
73+
<!-- /.usage -->
74+
75+
<section class="notes">
76+
77+
## Notes
78+
79+
- Elements outside of the copied region are left unchanged.
80+
81+
</section>
82+
83+
<!-- /.notes -->
84+
85+
<section class="examples">
86+
87+
## Examples
88+
89+
<!-- eslint no-undef: "error" -->
90+
91+
<!-- eslint-disable max-len -->
92+
93+
```javascript
94+
var discreteUniform = require( '@stdlib/random/array/discrete-uniform' );
95+
var Complex64Matrix = require( '@stdlib/ndarray/matrix/complex64' );
96+
var scalar2ndarray = require( '@stdlib/ndarray/from-scalar' );
97+
var ndarray2array = require( '@stdlib/ndarray/to-array' );
98+
var ctriu = require( '@stdlib/blas/ext/base/ndarray/ctriu' );
99+
100+
var opts = {
101+
'dtype': 'float32'
102+
};
103+
104+
var A = new Complex64Matrix( discreteUniform( 5*5*2, -50, 50, opts ).buffer, 0, 5, 5 );
105+
console.log( ndarray2array( A ) );
106+
107+
var B = new Complex64Matrix( discreteUniform( 5*5*2, -50, 50, opts ).buffer, 0, 5, 5 );
108+
console.log( ndarray2array( B ) );
109+
110+
var k = scalar2ndarray( 0, {
111+
'dtype': 'generic'
112+
});
113+
114+
var out = ctriu( [ A, B, k ] );
115+
console.log( ndarray2array( out ) );
116+
```
117+
118+
</section>
119+
120+
<!-- /.examples -->
121+
122+
<!-- Section for related `stdlib` packages. Do not manually edit this section, as it is automatically populated. -->
123+
124+
<section class="related">
125+
126+
</section>
127+
128+
<!-- /.related -->
129+
130+
<!-- Section for all links. Make sure to keep an empty line after the `section` element and another before the `/section` close. -->
131+
132+
<section class="links">
133+
134+
</section>
135+
136+
<!-- /.links -->
Lines changed: 120 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,120 @@
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 uniform = require( '@stdlib/random/array/uniform' );
25+
var Complex64Matrix = require( '@stdlib/ndarray/matrix/complex64' );
26+
var isnanf = require( '@stdlib/math/base/assert/is-nanf' );
27+
var realf = require( '@stdlib/complex/float32/real' );
28+
var pow = require( '@stdlib/math/base/special/pow' );
29+
var format = require( '@stdlib/string/format' );
30+
var scalar2ndarray = require( '@stdlib/ndarray/from-scalar' );
31+
var pkg = require( './../package.json' ).name;
32+
var ctriu = require( './../lib' );
33+
34+
35+
// VARIABLES //
36+
37+
var options = {
38+
'dtype': 'float32'
39+
};
40+
41+
42+
// FUNCTIONS //
43+
44+
/**
45+
* Creates a benchmark function.
46+
*
47+
* @private
48+
* @param {PositiveInteger} len - array length
49+
* @returns {Function} benchmark function
50+
*/
51+
function createBenchmark( len ) {
52+
var abuf;
53+
var bbuf;
54+
var A;
55+
var B;
56+
var k;
57+
58+
abuf = uniform( len*len*2, -100.0, 100.0, options );
59+
bbuf = uniform( len*len*2, -100.0, 100.0, options );
60+
A = new Complex64Matrix( abuf.buffer, 0, len, len );
61+
B = new Complex64Matrix( bbuf.buffer, 0, len, len );
62+
63+
k = scalar2ndarray( 0, {
64+
'dtype': 'generic'
65+
});
66+
67+
return benchmark;
68+
69+
/**
70+
* Benchmark function.
71+
*
72+
* @private
73+
* @param {Benchmark} b - benchmark instance
74+
*/
75+
function benchmark( b ) {
76+
var out;
77+
var i;
78+
79+
b.tic();
80+
for ( i = 0; i < b.iterations; i++ ) {
81+
out = ctriu( [ A, B, k ] );
82+
if ( typeof out !== 'object' ) {
83+
b.fail( 'should return an ndarray' );
84+
}
85+
}
86+
b.toc();
87+
if ( isnanf( realf( out.get( i%len, i%len ) ) ) ) {
88+
b.fail( 'should return an ndarray' );
89+
}
90+
b.pass( 'benchmark finished' );
91+
b.end();
92+
}
93+
}
94+
95+
96+
// MAIN //
97+
98+
/**
99+
* Main execution sequence.
100+
*
101+
* @private
102+
*/
103+
function main() {
104+
var len;
105+
var min;
106+
var max;
107+
var f;
108+
var i;
109+
110+
min = 1; // 10^min
111+
max = 3; // 10^max
112+
113+
for ( i = min; i <= max; i++ ) {
114+
len = pow( 10, i );
115+
f = createBenchmark( len );
116+
bench( format( '%s:len=%d', pkg, len ), f );
117+
}
118+
}
119+
120+
main();
Lines changed: 42 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,42 @@
1+
2+
{{alias}}( arrays )
3+
Copies the upper triangular part of a single-precision complex floating-
4+
point matrix `A` to another matrix `B`.
5+
6+
The diagonal parameter `k` specifies the diagonal below which to ignore. A
7+
value of `k = 0` refers to the main diagonal, `k > 0` refers to a diagonal
8+
above the main diagonal, and `k < 0` refers to a diagonal below the main
9+
diagonal.
10+
11+
Parameters
12+
----------
13+
arrays: ArrayLikeObject<ndarray>
14+
Array-like object containing the following ndarrays:
15+
16+
- a two-dimensional input ndarray corresponding to `A`.
17+
- a two-dimensional output ndarray corresponding to `B`.
18+
- a zero-dimensional ndarray specifying the diagonal below which to
19+
ignore.
20+
21+
Returns
22+
-------
23+
out: ndarray
24+
Output ndarray.
25+
26+
Examples
27+
--------
28+
> var abuf = [ [ 1.0, 2.0 ], [ 3.0, 4.0 ] ];
29+
> var A = new {{alias:@stdlib/ndarray/matrix/complex64}}( abuf );
30+
31+
> var bbuf = [ [ 0.0, 0.0 ], [ 0.0, 0.0 ] ];
32+
> var B = new {{alias:@stdlib/ndarray/matrix/complex64}}( bbuf );
33+
34+
> var k = {{alias:@stdlib/ndarray/from-scalar}}( 0, { 'dtype': 'generic' } );
35+
36+
> {{alias}}( [ A, B, k ] );
37+
> B
38+
<ndarray>[ [ <Complex64>[ 1.0, 2.0 ] ], [ <Complex64>[ 0.0, 0.0 ] ] ]
39+
40+
See Also
41+
--------
42+
Lines changed: 61 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,61 @@
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+
// TypeScript Version: 4.1
20+
21+
/// <reference types="@stdlib/types"/>
22+
23+
import { complex64ndarray, typedndarray } from '@stdlib/types/ndarray';
24+
25+
/**
26+
* Copies the upper triangular part of a single-precision complex floating-point matrix `A` to another matrix `B`.
27+
*
28+
* ## Notes
29+
*
30+
* - The function expects the following ndarrays:
31+
*
32+
* - a two-dimensional input ndarray corresponding to `A`.
33+
* - a two-dimensional output ndarray corresponding to `B`.
34+
* - a zero-dimensional ndarray specifying the diagonal below which to ignore.
35+
*
36+
* @param arrays - array-like object containing ndarrays
37+
* @returns output ndarray
38+
*
39+
* @example
40+
* var Complex64Matrix = require( '@stdlib/ndarray/matrix/complex64' );
41+
* var scalar2ndarray = require( '@stdlib/ndarray/from-scalar' );
42+
*
43+
* var A = new Complex64Matrix( [ [ 1.0, 2.0, 3.0, 4.0 ], [ 5.0, 6.0, 7.0, 8.0 ] ] );
44+
* var B = new Complex64Matrix( [ [ 0.0, 0.0, 0.0, 0.0 ], [ 0.0, 0.0, 0.0, 0.0 ] ] );
45+
*
46+
* var k = scalar2ndarray( 0, {
47+
* 'dtype': 'generic'
48+
* });
49+
*
50+
* var out = ctriu( [ A, B, k ] );
51+
* // returns <ndarray>[ [ <Complex64>[ 1.0, 2.0 ], <Complex64>[ 3.0, 4.0 ] ], [ <Complex64>[ 0.0, 0.0 ], <Complex64>[ 7.0, 8.0 ] ] ]
52+
*
53+
* var bool = ( out === B );
54+
* // returns true
55+
*/
56+
declare function ctriu( arrays: [ complex64ndarray, complex64ndarray, typedndarray<number> ] ): complex64ndarray;
57+
58+
59+
// EXPORTS //
60+
61+
export = ctriu;

0 commit comments

Comments
 (0)