Skip to content

Commit cfdd20a

Browse files
authored
feat: add blas/base/ndarray/ssyr
PR-URL: #12838 Reviewed-by: Athan Reines <kgryte@gmail.com> Signed-off-by: Kaustubh Patange <kaustubh.mp007@gmail.com> Ref: #2039
1 parent 4c2733b commit cfdd20a

10 files changed

Lines changed: 1026 additions & 0 deletions

File tree

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

0 commit comments

Comments
 (0)