Skip to content

Commit f47f033

Browse files
committed
feat: add blas/base/ndarray/sspmv
1 parent 4f96827 commit f47f033

10 files changed

Lines changed: 1002 additions & 0 deletions

File tree

Lines changed: 135 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,135 @@
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+
# sspmv
22+
23+
> Perform the matrix-vector operation `y = alpha*A*x + beta*y` for a symmetric matrix supplied in packed form `A`.
24+
25+
<section class="intro">
26+
27+
</section>
28+
29+
<!-- /.intro -->
30+
31+
<section class="usage">
32+
33+
## Usage
34+
35+
```javascript
36+
var sspmv = require( '@stdlib/blas/base/ndarray/sspmv' );
37+
```
38+
39+
#### sspmv( arrays )
40+
41+
Performs the matrix-vector operation `y = alpha*A*x + beta*y` for a symmetric matrix supplied in packed form `A`, where `alpha` and `beta` are scalars, `x` and `y` are ndarrays, and `A` is an `N` by `N` matrix.
42+
43+
```javascript
44+
var Float32Vector = require( '@stdlib/ndarray/vector/float32' );
45+
var scalar2ndarray = require( '@stdlib/ndarray/from-scalar' );
46+
var ndarray = require( '@stdlib/ndarray/base/ctor' );
47+
var Float32Array = require( '@stdlib/array/float32' );
48+
49+
var A = new ndarray( 'float32', new Float32Array( [ 1.0, 2.0, 3.0, 1.0, 2.0, 1.0 ] ), [ 6 ], [ 1 ], 0, 'row-major' );
50+
var x = new Float32Vector( [ 1.0, 2.0, 3.0 ] );
51+
var y = new Float32Vector( [ 4.0, 5.0, 6.0 ] );
52+
53+
var alpha = scalar2ndarray( 3.0, {
54+
'dtype': 'float32'
55+
});
56+
var beta = scalar2ndarray( 2.0, {
57+
'dtype': 'float32'
58+
});
59+
60+
var out = sspmv( [ A, x, y, alpha, beta ] );
61+
// returns <ndarray>[ 50.0, 40.0, 42.0 ]
62+
63+
var bool = ( out === y );
64+
// returns true
65+
```
66+
67+
The function has the following parameters:
68+
69+
- **arrays**: array-like object containing the following ndarrays:
70+
71+
- a one-dimensional symmetric input ndarray.
72+
- first one-dimensional input ndarray.
73+
- second one-dimensional input ndarray.
74+
- first zero-dimensional ndarray containing a scalar constant.
75+
- second zero-dimensional ndarray containing a scalar constant.
76+
77+
</section>
78+
79+
<!-- /.usage -->
80+
81+
<section class="notes">
82+
83+
</section>
84+
85+
<!-- /.notes -->
86+
87+
<section class="examples">
88+
89+
## Examples
90+
91+
<!-- eslint no-undef: "error" -->
92+
93+
```javascript
94+
var discreteUniform = require( '@stdlib/random/array/discrete-uniform' );
95+
var ndarray = require( '@stdlib/ndarray/base/ctor' );
96+
var Float32Array = require( '@stdlib/array/float32' );
97+
var Float32Vector = require( '@stdlib/ndarray/vector/float32' );
98+
var scalar2ndarray = require( '@stdlib/ndarray/from-scalar' );
99+
var ndarray2array = require( '@stdlib/ndarray/to-array' );
100+
var sspmv = require( '@stdlib/blas/base/ndarray/sspmv' );
101+
102+
var opts = {
103+
'dtype': 'float32'
104+
};
105+
106+
var A = new ndarray( 'float32', new Float32Array( discreteUniform( 10, 0, 10, opts ) ), [ 10 ], [ 1 ], 0, 'row-major' );
107+
var x = new Float32Vector( discreteUniform( 4, 0, 10, opts ) );
108+
var y = new Float32Vector( discreteUniform( 4, 0, 10, opts ) );
109+
110+
var alpha = scalar2ndarray( 3.0, opts );
111+
var beta = scalar2ndarray( 2.0, opts );
112+
113+
var out = sspmv( [ A, x, y, alpha, beta ] );
114+
console.log( ndarray2array( out ) );
115+
```
116+
117+
</section>
118+
119+
<!-- /.examples -->
120+
121+
<!-- Section for related `stdlib` packages. Do not manually edit this section, as it is automatically populated. -->
122+
123+
<section class="related">
124+
125+
</section>
126+
127+
<!-- /.related -->
128+
129+
<!-- Section for all links. Make sure to keep an empty line after the `section` element and another before the `/section` close. -->
130+
131+
<section class="links">
132+
133+
</section>
134+
135+
<!-- /.links -->
Lines changed: 116 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,116 @@
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/uniform' );
25+
var isnanf = require( '@stdlib/math/base/assert/is-nanf' );
26+
var pow = require( '@stdlib/math/base/special/pow' );
27+
var scalar2ndarray = require( '@stdlib/ndarray/from-scalar' );
28+
var format = require( '@stdlib/string/format' );
29+
var pkg = require( './../package.json' ).name;
30+
var sspmv = require( './../lib' );
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} len - array length
47+
* @returns {Function} benchmark function
48+
*/
49+
function createBenchmark( len ) {
50+
var alpha;
51+
var beta;
52+
var x;
53+
var y;
54+
var A;
55+
56+
A = uniform( [ (len * (len + 1)) / 2 ], -100.0, 100.0, options );
57+
x = uniform( [ len ], -100.0, 100.0, options );
58+
y = uniform( [ len ], -100.0, 100.0, options );
59+
60+
alpha = scalar2ndarray( 1.0, options );
61+
beta = scalar2ndarray( 1.0, options );
62+
63+
return benchmark;
64+
65+
/**
66+
* Benchmark function.
67+
*
68+
* @private
69+
* @param {Benchmark} b - benchmark instance
70+
*/
71+
function benchmark( b ) {
72+
var z;
73+
var i;
74+
75+
b.tic();
76+
for ( i = 0; i < b.iterations; i++ ) {
77+
z = sspmv( [ A, x, y, alpha, beta ] );
78+
if ( typeof z !== 'object' ) {
79+
b.fail( 'should return an ndarray' );
80+
}
81+
}
82+
b.toc();
83+
if ( isnanf( z.get( i%len ) ) ) {
84+
b.fail( 'should not return NaN' );
85+
}
86+
b.pass( 'benchmark finished' );
87+
b.end();
88+
}
89+
}
90+
91+
92+
// MAIN //
93+
94+
/**
95+
* Main execution sequence.
96+
*
97+
* @private
98+
*/
99+
function main() {
100+
var len;
101+
var min;
102+
var max;
103+
var f;
104+
var i;
105+
106+
min = 1; // 10^min
107+
max = 3; // 10^max
108+
109+
for ( i = min; i <= max; i++ ) {
110+
len = pow( 10, i );
111+
f = createBenchmark( len );
112+
bench( format( '%s:len=%d', pkg, len ), f );
113+
}
114+
}
115+
116+
main();
Lines changed: 40 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,40 @@
1+
2+
{{alias}}( arrays )
3+
Performs the matrix-vector operation `y = alpha*A*x + beta*y` for a
4+
symmetric matrix supplied in packed form `A`, where `alpha` and `beta`
5+
are scalars, `x` and `y` are ndarrays, and `A` is an `N` by `N` matrix.
6+
7+
Parameters
8+
----------
9+
arrays: ArrayLikeObject<ndarray>
10+
Array-like object containing the following ndarrays:
11+
12+
- a one-dimensional symmetric input ndarray.
13+
- first one-dimensional input ndarray.
14+
- second one-dimensional input ndarray.
15+
- first zero-dimensional ndarray containing a scalar constant.
16+
- second zero-dimensional ndarray containing a scalar constant.
17+
18+
Returns
19+
-------
20+
out: ndarray
21+
Output ndarray.
22+
23+
Examples
24+
--------
25+
> var buf = new {{alias:@stdlib/array/float32}}( [ 2.0, 1.0, 2.0 ] );
26+
> var sh = [ 3 ];
27+
> var st = [ 1 ];
28+
> var A = new {{alias:@stdlib/ndarray/base/ctor}}( 'float32', buf, sh, st, 0, 'row-major' );
29+
> var x = new {{alias:@stdlib/ndarray/vector/float32}}( [ 1.0, 2.0 ] );
30+
> var y = new {{alias:@stdlib/ndarray/vector/float32}}( [ 3.0, 4.0 ] );
31+
> var alpha = {{alias:@stdlib/ndarray/from-scalar}}( 2.0, { 'dtype': 'float32' });
32+
> var beta = {{alias:@stdlib/ndarray/from-scalar}}( 3.0, { 'dtype': 'float32' });
33+
34+
> {{alias}}( [ A, x, y, alpha, beta ] );
35+
> y
36+
<ndarray>[ 17.0, 22.0 ]
37+
38+
See Also
39+
--------
40+
Lines changed: 69 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,69 @@
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 { float32ndarray } from '@stdlib/types/ndarray';
24+
25+
/**
26+
* Performs the matrix-vector operation `y = alpha*A*x + beta*y` for a symmetric matrix supplied in packed form `A`, where `alpha` and `beta` are scalars, `x` and `y` are ndarrays, and `A` is an `N` by `N` matrix.
27+
*
28+
* ## Notes
29+
*
30+
* - The function expects the following ndarrays:
31+
*
32+
* - first one-dimensional symmetric input ndarray.
33+
* - second one-dimensional input ndarray.
34+
* - third one-dimensional input ndarray.
35+
* - first zero-dimensional ndarray containing a scalar constant.
36+
* - second zero-dimensional ndarray containing a scalar constant.
37+
*
38+
* @param arrays - array-like object containing ndarrays
39+
* @returns output ndarray
40+
*
41+
* @example
42+
* var Float32Vector = require( '@stdlib/ndarray/vector/float32' );
43+
* var scalar2ndarray = require( '@stdlib/ndarray/from-scalar' );
44+
* var ndarray = require( '@stdlib/ndarray/base/ctor' );
45+
* var Float32Array = require( '@stdlib/array/float32' );
46+
*
47+
* var A = new ndarray( 'float32', new Float32Array( [ 1.0, 2.0, 3.0, 1.0, 2.0, 1.0 ] ), [ 6 ], [ 1 ], 0, 'row-major' );
48+
* var x = new Float32Vector( [ 1.0, 2.0, 3.0 ] );
49+
* var y = new Float32Vector( [ 4.0, 5.0, 6.0 ] );
50+
*
51+
* var alpha = scalar2ndarray( 3.0, {
52+
* 'dtype': 'float32'
53+
* });
54+
* var beta = scalar2ndarray( 2.0, {
55+
* 'dtype': 'float32'
56+
* });
57+
*
58+
* var out = sspmv( [ A, x, y, alpha, beta ] );
59+
* // returns <ndarray>[ 50.0, 40.0, 42.0 ]
60+
*
61+
* var bool = ( out === y );
62+
* // returns true
63+
*/
64+
declare function sspmv( arrays: [ float32ndarray, float32ndarray, float32ndarray, float32ndarray, float32ndarray ] ): float32ndarray;
65+
66+
67+
// EXPORTS //
68+
69+
export = sspmv;

0 commit comments

Comments
 (0)