Skip to content

Commit f1ddfef

Browse files
ShabiShett07kgrytestdlib-bot
authored
feat: add blas/base/gsyr
PR-URL: #7738 Ref: #2039 Co-authored-by: Athan Reines <[email protected]> Reviewed-by: Athan Reines <[email protected]> Co-authored-by: stdlib-bot <[email protected]>
1 parent 49904dd commit f1ddfef

38 files changed

+4279
-0
lines changed
Lines changed: 190 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,190 @@
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+
# gsyr
22+
23+
> Perform the symmetric rank 1 operation `A = α*x*x^T + A`.
24+
25+
<section class="usage">
26+
27+
## Usage
28+
29+
```javascript
30+
var gsyr = require( '@stdlib/blas/base/gsyr' );
31+
```
32+
33+
#### gsyr( order, uplo, N, α, x, sx, A, LDA )
34+
35+
Performs the symmetric rank 1 operation `A = α*x*x^T + A` where `α` is a scalar, `x` is an `N` element vector, and `A` is an `N` by `N` symmetric matrix.
36+
37+
```javascript
38+
var A = [ 1.0, 2.0, 3.0, 2.0, 1.0, 2.0, 3.0, 2.0, 1.0 ];
39+
var x = [ 1.0, 2.0, 3.0 ];
40+
41+
gsyr( 'row-major', 'upper', 3, 1.0, x, 1, A, 3 );
42+
// A => [ 2.0, 4.0, 6.0, 2.0, 5.0, 8.0, 3.0, 2.0, 10.0 ]
43+
```
44+
45+
The function has the following parameters:
46+
47+
- **order**: storage layout.
48+
- **uplo**: specifies whether the upper or lower triangular part of the symmetric matrix `A` should be referenced.
49+
- **N**: number of elements along each dimension of `A`.
50+
- **α**: scalar constant.
51+
- **x**: input array.
52+
- **sx**: stride length for `x`.
53+
- **A**: input matrix stored in linear memory.
54+
- **LDA**: stride of the first dimension of `A` (a.k.a., leading dimension of the matrix `A`).
55+
56+
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,
57+
58+
```javascript
59+
var A = [ 1.0, 2.0, 3.0, 2.0, 1.0, 2.0, 3.0, 2.0, 1.0 ];
60+
var x = [ 3.0, 2.0, 1.0 ];
61+
62+
gsyr( 'row-major', 'upper', 3, 1.0, x, -1, A, 3 );
63+
// A => [ 2.0, 4.0, 6.0, 2.0, 5.0, 8.0, 3.0, 2.0, 10.0 ]
64+
```
65+
66+
Note that indexing is relative to the first index. To introduce an offset, use [`typed array`][mdn-typed-array] views.
67+
68+
<!-- eslint-disable stdlib/capitalized-comments -->
69+
70+
```javascript
71+
var Float64Array = require( '@stdlib/array/float64' );
72+
73+
// Initial arrays...
74+
var x0 = new Float64Array( [ 0.0, 3.0, 2.0, 1.0 ] );
75+
var A = new Float64Array( [ 1.0, 2.0, 3.0, 2.0, 1.0, 2.0, 3.0, 2.0, 1.0 ] );
76+
77+
// Create offset views...
78+
var x1 = new Float64Array( x0.buffer, x0.BYTES_PER_ELEMENT*1 ); // start at 2nd element
79+
80+
gsyr( 'row-major', 'upper', 3, 1.0, x1, -1, A, 3 );
81+
// A => <Float64Array>[ 2.0, 4.0, 6.0, 2.0, 5.0, 8.0, 3.0, 2.0, 10.0 ]
82+
```
83+
84+
#### gsyr.ndarray( uplo, N, α, x, sx, ox, A, sa1, sa2, oa )
85+
86+
Performs the symmetric rank 1 operation `A = α*x*x^T + A`, using alternative indexing semantics and where `α` is a scalar, `x` is an `N` element vector, and `A` is an `N` by `N` symmetric matrix.
87+
88+
```javascript
89+
var A = [ 1.0, 2.0, 3.0, 2.0, 1.0, 2.0, 3.0, 2.0, 1.0 ];
90+
var x = [ 1.0, 2.0, 3.0 ];
91+
92+
gsyr.ndarray( 'upper', 3, 1.0, x, 1, 0, A, 3, 1, 0 );
93+
// A => [ 2.0, 4.0, 6.0, 2.0, 5.0, 8.0, 3.0, 2.0, 10.0 ]
94+
```
95+
96+
The function has the following additional parameters:
97+
98+
- **ox**: starting index for `x`.
99+
- **sa1**: stride of the first dimension of `A`.
100+
- **sa2**: stride of the second dimension of `A`.
101+
- **oa**: starting index for `A`.
102+
103+
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,
104+
105+
```javascript
106+
var A = [ 1.0, 2.0, 3.0, 2.0, 1.0, 2.0, 3.0, 2.0, 1.0 ];
107+
var x = [ 1.0, 2.0, 3.0, 4.0, 5.0 ];
108+
109+
gsyr.ndarray( 'upper', 3, 1.0, x, -2, 4, A, 3, 1, 0 );
110+
// A => [ 26.0, 17.0, 8.0, 2.0, 10.0, 5.0, 3.0, 2.0, 2.0 ]
111+
```
112+
113+
</section>
114+
115+
<!-- /.usage -->
116+
117+
<section class="notes">
118+
119+
## Notes
120+
121+
- `gsyr()` corresponds to the [BLAS][blas] level 2 function [`dsyr`][dsyr] with the exception that this implementation works with any array type, not just Float64Arrays. Depending on the environment, the typed versions ([`dsyr`][@stdlib/blas/base/dsyr], [`ssyr`][@stdlib/blas/base/ssyr], etc.) are likely to be significantly more performant.
122+
- Both functions support array-like objects having getter and setter accessors for array element access (e.g., [`@stdlib/array/base/accessor`][@stdlib/array/base/accessor]).
123+
124+
</section>
125+
126+
<!-- /.notes -->
127+
128+
<section class="examples">
129+
130+
## Examples
131+
132+
<!-- eslint no-undef: "error" -->
133+
134+
```javascript
135+
var discreteUniform = require( '@stdlib/random/array/discrete-uniform' );
136+
var ones = require( '@stdlib/array/ones' );
137+
var gsyr = require( '@stdlib/blas/base/gsyr' );
138+
139+
var opts = {
140+
'dtype': 'generic'
141+
};
142+
143+
var N = 3;
144+
145+
// Create N-by-N symmetric matrices:
146+
var A1 = ones( N*N, opts.dtype );
147+
var A2 = ones( N*N, opts.dtype );
148+
149+
// Create a random vector:
150+
var x = discreteUniform( N, -10.0, 10.0, opts );
151+
152+
gsyr( 'row-major', 'upper', 3, 1.0, x, 1, A1, 3 );
153+
console.log( A1 );
154+
155+
gsyr.ndarray( 'upper', 3, 1.0, x, 1, 0, A2, 3, 1, 0 );
156+
console.log( A2 );
157+
```
158+
159+
</section>
160+
161+
<!-- /.examples -->
162+
163+
<!-- Section for related `stdlib` packages. Do not manually edit this section, as it is automatically populated. -->
164+
165+
<section class="related">
166+
167+
</section>
168+
169+
<!-- /.related -->
170+
171+
<!-- Section for all links. Make sure to keep an empty line after the `section` element and another before the `/section` close. -->
172+
173+
<section class="links">
174+
175+
[blas]: http://www.netlib.org/blas
176+
177+
[dsyr]: https://www.netlib.org/lapack/explore-html/dc/d82/group__her_ga07f0e3f8592107877f12a554a41c7413.html#ga07f0e3f8592107877f12a554a41c7413
178+
179+
[mdn-typed-array]: https://developer.mozilla.org/en-US/docs/Web/JavaScript/Reference/Global_Objects/TypedArray
180+
181+
[@stdlib/blas/base/dsyr]: https://github.com/stdlib-js/stdlib/tree/develop/lib/node_modules/%40stdlib/blas/base/dsyr
182+
183+
[@stdlib/blas/base/ssyr]: https://github.com/stdlib-js/stdlib/tree/develop/lib/node_modules/%40stdlib/blas/base/ssyr
184+
185+
[@stdlib/array/base/accessor]: https://github.com/stdlib-js/stdlib/tree/develop/lib/node_modules/%40stdlib/array/base/accessor
186+
187+
188+
</section>
189+
190+
<!-- /.links -->
Lines changed: 99 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,99 @@
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 isnan = require( '@stdlib/math/base/assert/is-nan' );
26+
var pow = require( '@stdlib/math/base/special/pow' );
27+
var floor = require( '@stdlib/math/base/special/floor' );
28+
var pkg = require( './../package.json' ).name;
29+
var gsyr = require( './../lib' );
30+
31+
32+
// VARIABLES //
33+
34+
var options = {
35+
'dtype': 'generic'
36+
};
37+
38+
39+
// FUNCTIONS //
40+
41+
/**
42+
* Create a benchmark function.
43+
*
44+
* @private
45+
* @param {PositiveInteger} N - array dimension size
46+
* @returns {Function} benchmark function
47+
*/
48+
function createBenchmark( N ) {
49+
var x = uniform( N, -10.0, 10.0, options );
50+
var A = uniform( N*N, -10.0, 10.0, options );
51+
return benchmark;
52+
53+
/**
54+
* Benchmark function.
55+
*
56+
* @private
57+
* @param {Benchmark} b - benchmark instance
58+
*/
59+
function benchmark( b ) {
60+
var z;
61+
var i;
62+
63+
b.tic();
64+
for ( i = 0; i < b.iterations; i++ ) {
65+
z = gsyr( 'row-major', 'upper', N, 1.0, x, 1, A, N );
66+
if ( isnan( z[ i%z.length ] ) ) {
67+
b.fail( 'should not return NaN' );
68+
}
69+
}
70+
b.toc();
71+
if ( isnan( z[ i%z.length ] ) ) {
72+
b.fail( 'should not return NaN' );
73+
}
74+
b.pass( 'benchmark finished' );
75+
b.end();
76+
}
77+
}
78+
79+
80+
// MAIN //
81+
82+
function main() {
83+
var min;
84+
var max;
85+
var N;
86+
var f;
87+
var i;
88+
89+
min = 1; // 10^min
90+
max = 6; // 10^max
91+
92+
for ( i = min; i <= max; i++ ) {
93+
N = floor( pow( pow( 10, i ), 1.0/2.0 ) );
94+
f = createBenchmark( N );
95+
bench( pkg+':size='+(N*N), f );
96+
}
97+
}
98+
99+
main();
Lines changed: 99 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,99 @@
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 isnan = require( '@stdlib/math/base/assert/is-nan' );
26+
var pow = require( '@stdlib/math/base/special/pow' );
27+
var floor = require( '@stdlib/math/base/special/floor' );
28+
var pkg = require( './../package.json' ).name;
29+
var gsyr = require( './../lib' ).ndarray;
30+
31+
32+
// VARIABLES //
33+
34+
var options = {
35+
'dtype': 'generic'
36+
};
37+
38+
39+
// FUNCTIONS //
40+
41+
/**
42+
* Create a benchmark function.
43+
*
44+
* @private
45+
* @param {PositiveInteger} N - array dimension size
46+
* @returns {Function} benchmark function
47+
*/
48+
function createBenchmark( N ) {
49+
var x = uniform( N, -10.0, 10.0, options );
50+
var A = uniform( N*N, -10.0, 10.0, options );
51+
return benchmark;
52+
53+
/**
54+
* Benchmark function.
55+
*
56+
* @private
57+
* @param {Benchmark} b - benchmark instance
58+
*/
59+
function benchmark( b ) {
60+
var z;
61+
var i;
62+
63+
b.tic();
64+
for ( i = 0; i < b.iterations; i++ ) {
65+
z = gsyr( 'upper', N, 1.0, x, 1, 0, A, N, 1, 0 );
66+
if ( isnan( z[ i%z.length ] ) ) {
67+
b.fail( 'should not return NaN' );
68+
}
69+
}
70+
b.toc();
71+
if ( isnan( z[ i%z.length ] ) ) {
72+
b.fail( 'should not return NaN' );
73+
}
74+
b.pass( 'benchmark finished' );
75+
b.end();
76+
}
77+
}
78+
79+
80+
// MAIN //
81+
82+
function main() {
83+
var min;
84+
var max;
85+
var N;
86+
var f;
87+
var i;
88+
89+
min = 1; // 10^min
90+
max = 6; // 10^max
91+
92+
for ( i = min; i <= max; i++ ) {
93+
N = floor( pow( pow( 10, i ), 1.0/2.0 ) );
94+
f = createBenchmark( N );
95+
bench( pkg+':ndarray:size='+(N*N), f );
96+
}
97+
}
98+
99+
main();

0 commit comments

Comments
 (0)