Skip to content

Commit 62744b5

Browse files
aman-095kgryte
andauthored
feat: add blas/base/ssymv
PR-URL: #2305 Ref: #2039 Co-authored-by: Athan Reines <kgryte@gmail.com> Reviewed-by: Athan Reines <kgryte@gmail.com>
1 parent 642d473 commit 62744b5

27 files changed

+3312
-33
lines changed
Lines changed: 263 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,263 @@
1+
<!--
2+
3+
@license Apache-2.0
4+
5+
Copyright (c) 2024 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+
# ssymv
22+
23+
> Perform the matrix-vector operation `y = α*A*x + β*y` where `α` and `β` are scalars, `x` and `y` are `N` element vectors, and `A` is an `N` by `N` symmetric matrix.
24+
25+
<section class = "usage">
26+
27+
## Usage
28+
29+
```javascript
30+
var ssymv = require( '@stdlib/blas/base/ssymv' );
31+
```
32+
33+
#### ssymv( order, uplo, N, α, A, LDA, x, sx, β, y, sy )
34+
35+
Performs the matrix-vector operation `y = α*A*x + β*y` where `α` and `β` are scalars, `x` and `y` are `N` element vectors, and `A` is an `N` by `N` symmetric matrix.
36+
37+
```javascript
38+
var Float32Array = require( '@stdlib/array/float32' );
39+
40+
var A = new Float32Array( [ 1.0, 0.0, 0.0, 0.0, 2.0, 0.0, 0.0, 0.0, 3.0 ] );
41+
var x = new Float32Array( [ 1.0, 1.0, 1.0 ] );
42+
var y = new Float32Array( [ 0.0, 0.0, 0.0 ] );
43+
44+
ssymv( 'row-major', 'lower', 3, 1.0, A, 3, x, 1, 0.0, y, 1 );
45+
// y => <Float32Array>[ 1.0, 2.0, 3.0 ]
46+
```
47+
48+
The function has the following parameters:
49+
50+
- **order**: storage layout.
51+
- **uplo**: specifies whether the upper or lower triangular part of the symmetric matrix `A` should be referenced.
52+
- **N**: number of elements along each dimension of `A`.
53+
- **α**: scalar constant.
54+
- **A**: input matrix stored in linear memory as a [`Float32Array`][mdn-float32array].
55+
- **lda**: stride of the first dimension of `A` (a.k.a., leading dimension of the matrix `A`).
56+
- **x**: input [`Float32Array`][mdn-float32array].
57+
- **sx**: index increment for `x`.
58+
- **β**: scalar constant.
59+
- **y**: output [`Float32Array`][mdn-float32array].
60+
- **sy**: index increment for `y`.
61+
62+
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,
63+
64+
```javascript
65+
var Float32Array = require( '@stdlib/array/float32' );
66+
67+
var A = new Float32Array( [ 1.0, 0.0, 0.0, 0.0, 2.0, 0.0, 0.0, 0.0, 3.0 ] );
68+
var x = new Float32Array( [ 1.0, 2.0, 3.0 ] );
69+
var y = new Float32Array( [ 1.0, 2.0, 3.0 ] );
70+
71+
ssymv( 'row-major', 'upper', 3, 2.0, A, 3, x, -1, 1.0, y, 1 );
72+
// y => <Float32Array>[ 7.0, 10.0, 9.0 ]
73+
```
74+
75+
Note that indexing is relative to the first index. To introduce an offset, use [`typed array`][mdn-typed-array] views.
76+
77+
<!-- eslint-disable stdlib/capitalized-comments -->
78+
79+
```javascript
80+
var Float32Array = require( '@stdlib/array/float32' );
81+
82+
// Initial arrays...
83+
var x0 = new Float32Array( [ 1.0, 1.0, 1.0, 1.0 ] );
84+
var y0 = new Float32Array( [ 1.0, 1.0, 1.0, 1.0 ] );
85+
var A = new Float32Array( [ 1.0, 0.0, 0.0, 0.0, 2.0, 0.0, 0.0, 0.0, 3.0 ] );
86+
87+
// Create offset views...
88+
var x1 = new Float32Array( x0.buffer, x0.BYTES_PER_ELEMENT*1 ); // start at 2nd element
89+
var y1 = new Float32Array( y0.buffer, y0.BYTES_PER_ELEMENT*1 ); // start at 2nd element
90+
91+
ssymv( 'row-major', 'upper', 3, 1.0, A, 3, x1, -1, 1.0, y1, -1 );
92+
// y0 => <Float32Array>[ 1.0, 4.0, 3.0, 2.0 ]
93+
```
94+
95+
#### ssymv.ndarray( order, uplo, N, α, A, LDA, x, sx, ox, β, y, sy, oy )
96+
97+
Performs the matrix-vector operation `y = α*A*x + β*y` using alternative indexing semantics and where `α` and `β` are scalars, `x` and `y` are `N` element vectors, and `A` is an `N` by `N` symmetric matrix.
98+
99+
```javascript
100+
var Float32Array = require( '@stdlib/array/float32' );
101+
102+
var A = new Float32Array( [ 1.0, 0.0, 0.0, 0.0, 2.0, 0.0, 0.0, 0.0, 3.0 ] );
103+
var x = new Float32Array( [ 1.0, 2.0, 3.0 ] );
104+
var y = new Float32Array( [ 1.0, 2.0, 3.0 ] );
105+
106+
ssymv.ndarray( 'row-major', 'upper', 3, 2.0, A, 3, x, -1, 2, 1.0, y, 1, 0 );
107+
// y => <Float32Array>[ 7.0, 10.0, 9.0 ]
108+
```
109+
110+
The function has the following additional parameters:
111+
112+
- **ox**: starting index for `x`.
113+
- **oy**: starting index for `y`.
114+
115+
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,
116+
117+
```javascript
118+
var Float32Array = require( '@stdlib/array/float32' );
119+
120+
var A = new Float32Array( [ 1.0, 0.0, 0.0, 0.0, 2.0, 0.0, 0.0, 0.0, 3.0 ] );
121+
var x = new Float32Array( [ 1.0, 1.0, 1.0 ] );
122+
var y = new Float32Array( [ 1.0, 1.0, 1.0 ] );
123+
124+
ssymv.ndarray( 'row-major', 'lower', 3, 1.0, A, 3, x, -1, 2, 1.0, y, -1, 2 );
125+
// y => <Float32Array>[ 4.0, 3.0, 2.0 ]
126+
```
127+
128+
</section>
129+
130+
<!-- /.usage -->
131+
132+
<section class="notes">
133+
134+
## Notes
135+
136+
- `ssymv()` corresponds to the [BLAS][blas] level 2 function [`ssymv`][ssymv].
137+
138+
</section>
139+
140+
<!-- /.notes -->
141+
142+
<section class="examples">
143+
144+
## Examples
145+
146+
<!-- eslint no-undef: "error" -->
147+
148+
```javascript
149+
var discreteUniform = require( '@stdlib/random/array/discrete-uniform' );
150+
var ones = require( '@stdlib/array/ones' );
151+
var ssymv = require( '@stdlib/blas/base/ssymv' );
152+
153+
var opts = {
154+
'dtype': 'float32'
155+
};
156+
157+
var N = 3;
158+
var A = ones( N*N, opts.dtype );
159+
160+
var x = discreteUniform( N, 0, 255, opts );
161+
var y = discreteUniform( N, 0, 255, opts );
162+
163+
ssymv.ndarray( 'row-major', 'upper', N, 1.0, A, N, x, 1, 0, 1.0, y, 1, 0 );
164+
console.log( y );
165+
```
166+
167+
</section>
168+
169+
<!-- /.examples -->
170+
171+
<!-- C interface documentation. -->
172+
173+
* * *
174+
175+
<section class="c">
176+
177+
## C APIs
178+
179+
<!-- Section to include introductory text. Make sure to keep an empty line after the intro `section` element and another before the `/section` close. -->
180+
181+
<section class="intro">
182+
183+
</section>
184+
185+
<!-- /.intro -->
186+
187+
<!-- C usage documentation. -->
188+
189+
<section class="usage">
190+
191+
### Usage
192+
193+
```c
194+
TODO
195+
```
196+
197+
#### TODO
198+
199+
TODO.
200+
201+
```c
202+
TODO
203+
```
204+
205+
TODO
206+
207+
```c
208+
TODO
209+
```
210+
211+
</section>
212+
213+
<!-- /.usage -->
214+
215+
<!-- C API usage notes. Make sure to keep an empty line after the `section` element and another before the `/section` close. -->
216+
217+
<section class="notes">
218+
219+
</section>
220+
221+
<!-- /.notes -->
222+
223+
<!-- C API usage examples. -->
224+
225+
<section class="examples">
226+
227+
### Examples
228+
229+
```c
230+
TODO
231+
```
232+
233+
</section>
234+
235+
<!-- /.examples -->
236+
237+
</section>
238+
239+
<!-- /.c -->
240+
241+
<!-- Section for related `stdlib` packages. Do not manually edit this section, as it is automatically populated. -->
242+
243+
<section class="related">
244+
245+
</section>
246+
247+
<!-- /.related -->
248+
249+
<!-- Section for all links. Make sure to keep an empty line after the `section` element and another before the `/section` close. -->
250+
251+
<section class="links">
252+
253+
[blas]: http://www.netlib.org/blas
254+
255+
[ssymv]: https://netlib.org/lapack/explore-html/d2/d94/ssymv_8f.html
256+
257+
[mdn-float32array]: https://developer.mozilla.org/en-US/docs/Web/JavaScript/Reference/Global_Objects/Float32Array
258+
259+
[mdn-typed-array]: https://developer.mozilla.org/en-US/docs/Web/JavaScript/Reference/Global_Objects/TypedArray
260+
261+
</section>
262+
263+
<!-- /.links -->
Lines changed: 106 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,106 @@
1+
/**
2+
* @license Apache-2.0
3+
*
4+
* Copyright (c) 2024 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 ones = require( '@stdlib/array/ones' );
27+
var pow = require( '@stdlib/math/base/special/pow' );
28+
var floor = require( '@stdlib/math/base/special/floor' );
29+
var pkg = require( './../package.json' ).name;
30+
var ssymv = require( './../lib/ssymv.js' );
31+
32+
33+
// VARIABLES //
34+
35+
var options = {
36+
'dtype': 'float32'
37+
};
38+
39+
40+
// FUNCTIONS //
41+
42+
/**
43+
* Creates a benchmark function.
44+
*
45+
* @private
46+
* @param {PositiveInteger} N - number of elements along each dimension
47+
* @returns {Function} benchmark function
48+
*/
49+
function createBenchmark( N ) {
50+
var x = uniform( N, -10.0, 10.0, options );
51+
var y = uniform( N, -10.0, 10.0, options );
52+
var A = ones( N*N, options.dtype );
53+
return benchmark;
54+
55+
/**
56+
* Benchmark function.
57+
*
58+
* @private
59+
* @param {Benchmark} b - benchmark instance
60+
*/
61+
function benchmark( b ) {
62+
var z;
63+
var i;
64+
65+
b.tic();
66+
for ( i = 0; i < b.iterations; i++ ) {
67+
z = ssymv( 'row-major', 'upper', N, 1.0, A, N, x, 1, 1.0, y, 1 );
68+
if ( isnanf( z[ i%z.length ] ) ) {
69+
b.fail( 'should not return NaN' );
70+
}
71+
}
72+
b.toc();
73+
if ( isnanf( z[ i%z.length ] ) ) {
74+
b.fail( 'should not return NaN' );
75+
}
76+
b.pass( 'benchmark finished' );
77+
b.end();
78+
}
79+
}
80+
81+
82+
// MAIN //
83+
84+
/**
85+
* Main execution sequence.
86+
*
87+
* @private
88+
*/
89+
function main() {
90+
var min;
91+
var max;
92+
var N;
93+
var f;
94+
var i;
95+
96+
min = 1; // 10^min
97+
max = 6; // 10^max
98+
99+
for ( i = min; i <= max; i++ ) {
100+
N = floor( pow( pow( 10, i ), 1.0/2.0 ) );
101+
f = createBenchmark( N );
102+
bench( pkg+':size='+(N*N), f );
103+
}
104+
}
105+
106+
main();

0 commit comments

Comments
 (0)