Skip to content

Commit eafd304

Browse files
committed
feat: add blas/base/ctrmv
1 parent f344e9d commit eafd304

Some content is hidden

Large Commits have some content hidden by default. Use the searchbox below for content that may be hidden.

50 files changed

+4381
-0
lines changed
Lines changed: 269 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,269 @@
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+
# ctrmv
22+
23+
> Perform one of the matrix-vector operations `x = A*x`, or `x = A**T*x`, or `x = A**H*x`.
24+
25+
<section class="usage">
26+
27+
## Usage
28+
29+
```javascript
30+
var ctrmv = require( '@stdlib/blas/base/ctrmv' );
31+
```
32+
33+
#### ctrmv( order, uplo, trans, diag, N, x, strideX, A, LDA )
34+
35+
Performs one of the matrix-vector operations `x = A*x`, or `x = A**T*x`, or `x = A**H*x`, where `x` is an `N` element vector and `A` is an `N` by `N` unit, or non-unit, upper or lower triangular matrix.
36+
37+
```javascript
38+
var Complex64Array = require( '@stdlib/array/complex64' );
39+
40+
var x = new Complex64Array( [ 1.0, 1.0, 2.0, 2.0, 3.0, 3.0 ] );
41+
var A = new Complex64Array( [ 1.0, 1.0, 0.0, 0.0, 0.0, 0.0, 1.0, 1.0, 1.0, 1.0, 0.0, 0.0, 1.0, 1.0, 1.0, 1.0, 1.0, 1.0 ] );
42+
43+
ctrmv( 'column-major', 'upper', 'no-transpose', 'non-unit', 3, x, 1, A, 3 );
44+
// x => <Complex64Array>[ 0.0, 12.0, 0.0, 10.0, 0.0, 6.0 ]
45+
```
46+
47+
The function has the following parameters:
48+
49+
- **order**: storage layout.
50+
- **uplo**: specifies whether the upper or lower triangular part of the matrix `A` is supplied.
51+
- **trans**: specifies whether `A` should be transposed, conjugate-transposed, or not transposed.
52+
- **diag**: specifies whether `A` has a unit diagonal.
53+
- **N**: specifies the order of the matrix `A`.
54+
- **x**: input vector [`Complex64Array`][@stdlib/array/complex64].
55+
- **strideX**: index increment for `x`.
56+
- **A**: input matrix stored in linear memory as [`Complex64Array`][@stdlib/array/complex64].
57+
- **LDA**: stride of the first dimension of `A` (a.k.a., leading dimension of the matrix `A`).
58+
59+
The stride parameters determine how elements in the input arrays are accessed at runtime. For example, to iterate over the elements of `x` in reverse order,
60+
61+
```javascript
62+
var Complex64Array = require( '@stdlib/array/complex64' );
63+
64+
var x = new Complex64Array( [ 3.0, 3.0, 2.0, 2.0, 1.0, 1.0 ] );
65+
var A = new Complex64Array( [ 1.0, 1.0, 0.0, 0.0, 0.0, 0.0, 1.0, 1.0, 1.0, 1.0, 0.0, 0.0, 1.0, 1.0, 1.0, 1.0, 1.0, 1.0 ] );
66+
67+
ctrmv( 'column-major', 'upper', 'no-transpose', 'non-unit', 3, x, -1, A, 3 );
68+
// x => <Complex64Array>[ 6.0, 0.0, 10.0, 0.0, 12.0, 0.0 ]
69+
```
70+
71+
Note that indexing is relative to the first index. To introduce an offset, use [`typed array`][mdn-typed-array] views.
72+
73+
<!-- eslint-disable stdlib/capitalized-comments -->
74+
75+
```javascript
76+
var Complex64Array = require( '@stdlib/array/complex64' );
77+
78+
// Initial array:
79+
var x0 = new Complex64Array( [ 1.0, 1.0, 2.0, 2.0, 3.0, 3.0 ] );
80+
81+
// Define a input matrix:
82+
var A = new Complex64Array( [ 1.0, 1.0, 0.0, 0.0, 0.0, 0.0, 1.0, 1.0, 1.0, 1.0, 0.0, 0.0, 1.0, 1.0, 1.0, 1.0, 1.0, 1.0 ] );
83+
84+
// Create an offset view:
85+
var x1 = new Complex64Array( x0.buffer, x0.BYTES_PER_ELEMENT*1 ); // start at 2nd element
86+
87+
ctrmv( 'column-major', 'upper', 'no-transpose', 'non-unit', 3, x1, 1, A, 3 );
88+
// x1 => <Complex64Array>[ 0.0, 12.0, 0.0, 10.0, 0.0, 6.0 ]
89+
```
90+
91+
#### ctrmv.ndarray( uplo, trans, diag, N, x, strideX, offsetX, A, strideA1, strideA2, offsetA )
92+
93+
Performs one of the matrix-vector operations `x = A*x`, or `x = A**T*x`, or `x = A**H*x`, using alternative indexing semantics and where `x` is an `N` element vector and `A` is an `N` by `N` unit, or non-unit, upper or lower triangular matrix.
94+
95+
```javascript
96+
var Complex64Array = require( '@stdlib/array/complex64' );
97+
98+
var x = new Complex64Array( [ 1.0, 1.0, 2.0, 2.0, 3.0, 3.0 ] );
99+
var A = new Complex64Array( [ 1.0, 1.0, 0.0, 0.0, 0.0, 0.0, 1.0, 1.0, 1.0, 1.0, 0.0, 0.0, 1.0, 1.0, 1.0, 1.0, 1.0, 1.0 ] );
100+
101+
ctrmv.ndarray( 'upper', 'no-transpose', 'non-unit', 3, x, 1, 0, A, 3, 1, 0 );
102+
// x => <Complex64Array>[ 0.0, 12.0, 0.0, 10.0, 0.0, 6.0 ]
103+
```
104+
105+
The function has the following additional parameters:
106+
107+
- **strideA1**: stride of the first dimension of `A`.
108+
- **strideA2**: stride of the second dimension of `A`.
109+
- **offsetX**: starting index for `x`.
110+
- **offsetA**: starting index for `A`.
111+
112+
While [`typed array`][mdn-typed-array] views mandate a view offset based on the underlying buffer, the offset parameter supports indexing semantics based on a starting index. For example, to scale every other value in the input strided array starting from the second element,
113+
114+
```javascript
115+
var Complex64Array = require( '@stdlib/array/complex64' );
116+
117+
var x = new Complex64Array( [ 3.0, 3.0, 2.0, 2.0, 1.0, 1.0 ] );
118+
var A = new Complex64Array( [ 0.0, 0.0, 1.0, 1.0, 0.0, 0.0, 0.0, 0.0, 1.0, 1.0, 1.0, 1.0, 0.0, 0.0, 1.0, 1.0, 1.0, 1.0, 1.0, 1.0 ] );
119+
120+
ctrmv.ndarray( 'upper', 'no-transpose', 'non-unit', 3, x, -1, 2, A, 3, 1, 1 );
121+
// x => <Complex64Array>[ 0.0, 6.0, 0.0, 10.0, 0.0, 12.0 ]
122+
```
123+
124+
</section>
125+
126+
<!-- /.usage -->
127+
128+
<section class="notes">
129+
130+
## Notes
131+
132+
- If `N = 0` both functions return `x` unchanged.
133+
- `ctrmv()` corresponds to the [BLAS][blas] level 2 function [`ctrmv`][ctrmv].
134+
135+
</section>
136+
137+
<!-- /.notes -->
138+
139+
<section class="examples">
140+
141+
## Examples
142+
143+
<!-- eslint no-undef: "error" -->
144+
145+
```javascript
146+
var discreteUniform = require( '@stdlib/random/base/discrete-uniform' );
147+
var filledarrayBy = require( '@stdlib/array/filled-by' );
148+
var Complex64 = require( '@stdlib/complex/float32/ctor' );
149+
var logEach = require( '@stdlib/console/log-each' );
150+
var ctrmv = require( '@stdlib/blas/base/ctrmv' );
151+
152+
function rand() {
153+
return new Complex64( discreteUniform( 0, 10 ), discreteUniform( -5, 5 ) );
154+
}
155+
156+
var x = filledarrayBy( 3, 'complex64', rand );
157+
console.log( x.get( 0 ).toString() );
158+
159+
var A = filledarrayBy( 9, 'complex64', rand );
160+
console.log( A.get( 0 ).toString() );
161+
162+
ctrmv( 'column-major', 'upper', 'no-transpose', 'non-unit', 3, x, 1, A, 3 );
163+
164+
// Print the results:
165+
logEach( '(%s)', x );
166+
167+
ctrmv.ndarray( 'upper', 'no-transpose', 'non-unit', 3, x, 1, 0, A, 1, 3, 0 );
168+
169+
// Print the results:
170+
logEach( '(%s)', x );
171+
```
172+
173+
</section>
174+
175+
<!-- /.examples -->
176+
177+
<!-- C interface documentation. -->
178+
179+
* * *
180+
181+
<section class="c">
182+
183+
## C APIs
184+
185+
<!-- Section to include introductory text. Make sure to keep an empty line after the intro `section` element and another before the `/section` close. -->
186+
187+
<section class="intro">
188+
189+
</section>
190+
191+
<!-- /.intro -->
192+
193+
<!-- C usage documentation. -->
194+
195+
<section class="usage">
196+
197+
### Usage
198+
199+
```c
200+
TODO
201+
```
202+
203+
#### TODO
204+
205+
TODO.
206+
207+
```c
208+
TODO
209+
```
210+
211+
TODO
212+
213+
```c
214+
TODO
215+
```
216+
217+
</section>
218+
219+
<!-- /.usage -->
220+
221+
<!-- C API usage notes. Make sure to keep an empty line after the `section` element and another before the `/section` close. -->
222+
223+
<section class="notes">
224+
225+
</section>
226+
227+
<!-- /.notes -->
228+
229+
<!-- C API usage examples. -->
230+
231+
<section class="examples">
232+
233+
### Examples
234+
235+
```c
236+
TODO
237+
```
238+
239+
</section>
240+
241+
<!-- /.examples -->
242+
243+
</section>
244+
245+
<!-- /.c -->
246+
247+
<!-- Section for related `stdlib` packages. Do not manually edit this section, as it is automatically populated. -->
248+
249+
<section class="related">
250+
251+
</section>
252+
253+
<!-- /.related -->
254+
255+
<!-- Section for all links. Make sure to keep an empty line after the `section` element and another before the `/section` close. -->
256+
257+
<section class="links">
258+
259+
[blas]: http://www.netlib.org/blas
260+
261+
[ctrmv]: https://www.netlib.org/lapack/explore-html/d6/d1c/group__trmv_ga0adaf80ae1dfe117390bd7030fd865f1.html
262+
263+
[mdn-typed-array]: https://developer.mozilla.org/en-US/docs/Web/JavaScript/Reference/Global_Objects/TypedArray
264+
265+
[@stdlib/array/complex64]: https://github.com/stdlib-js/stdlib/tree/develop/lib/node_modules/%40stdlib/array/complex64
266+
267+
</section>
268+
269+
<!-- /.links -->
Lines changed: 111 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,111 @@
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 uniform = require( '@stdlib/random/array/uniform' );
25+
var isnanf = require( '@stdlib/math/base/assert/is-nanf' );
26+
var pow = require( '@stdlib/math/base/special/pow' );
27+
var Complex64Array = require( '@stdlib/array/complex64' );
28+
var pkg = require( './../package.json' ).name;
29+
var ctrmv = require( './../lib/ctrmv.js' );
30+
31+
32+
// VARIABLES //
33+
34+
var options = {
35+
'dtype': 'float32'
36+
};
37+
38+
39+
// FUNCTIONS //
40+
41+
/**
42+
* Creates a benchmark function.
43+
*
44+
* @private
45+
* @param {PositiveInteger} len - array length
46+
* @returns {Function} benchmark function
47+
*/
48+
function createBenchmark( len ) {
49+
var xbuf;
50+
var Abuf;
51+
var x;
52+
var A;
53+
54+
xbuf = uniform( len*2, -100.0, 100.0, options );
55+
x = new Complex64Array( xbuf.buffer );
56+
Abuf = uniform( (len*len)*2, -100.0, 100.0, options );
57+
A = new Complex64Array( Abuf.buffer );
58+
59+
return benchmark;
60+
61+
/**
62+
* Benchmark function.
63+
*
64+
* @private
65+
* @param {Benchmark} b - benchmark instance
66+
*/
67+
function benchmark( b ) {
68+
var i;
69+
70+
b.tic();
71+
for ( i = 0; i < b.iterations; i++ ) {
72+
ctrmv( 'row-major', 'upper', 'no-transpose', 'non-unit', len, x, 1, A, len );
73+
if ( isnanf( Abuf[ i%(len*2) ] ) ) {
74+
b.fail( 'should not return NaN' );
75+
}
76+
}
77+
b.toc();
78+
if ( isnanf( Abuf[ i%(len*2) ] ) ) {
79+
b.fail( 'should not return NaN' );
80+
}
81+
b.pass( 'benchmark finished' );
82+
b.end();
83+
}
84+
}
85+
86+
87+
// MAIN //
88+
89+
/**
90+
* Main execution sequence.
91+
*
92+
* @private
93+
*/
94+
function main() {
95+
var len;
96+
var min;
97+
var max;
98+
var f;
99+
var i;
100+
101+
min = 1; // 10^min
102+
max = 4; // 10^max
103+
104+
for ( i = min; i <= max; i++ ) {
105+
len = pow( 10, i );
106+
f = createBenchmark( len );
107+
bench( pkg+':len='+len, f );
108+
}
109+
}
110+
111+
main();

0 commit comments

Comments
 (0)