Skip to content

Commit 01d6f06

Browse files
Shabareesh ShettyShabareesh Shetty
authored andcommitted
feat: add blas/base/gsyr
--- type: pre_commit_static_analysis_report description: Results of running static analysis checks when committing changes. report: - task: lint_filenames status: passed - task: lint_editorconfig status: passed - task: lint_markdown status: passed - task: lint_package_json status: passed - task: lint_repl_help status: passed - task: lint_javascript_src status: passed - task: lint_javascript_cli status: na - task: lint_javascript_examples status: passed - task: lint_javascript_tests status: passed - task: lint_javascript_benchmarks status: passed - task: lint_python status: na - task: lint_r status: na - task: lint_c_src status: na - task: lint_c_examples status: na - task: lint_c_benchmarks status: na - task: lint_c_tests_fixtures status: na - task: lint_shell status: na - task: lint_typescript_declarations status: passed - task: lint_typescript_tests status: passed - task: lint_license_headers status: passed ---
1 parent ad9b000 commit 01d6f06

38 files changed

+4268
-0
lines changed
Lines changed: 196 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,196 @@
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+
# 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 Float64Array = require( '@stdlib/array/float64' );
39+
40+
var A = [ 1.0, 2.0, 3.0, 2.0, 1.0, 2.0, 3.0, 2.0, 1.0 ];
41+
var x = [ 1.0, 2.0, 3.0 ];
42+
43+
gsyr( 'row-major', 'upper', 3, 1.0, x, 1, A, 3 );
44+
// A => [ 2.0, 4.0, 6.0, 2.0, 5.0, 8.0, 3.0, 2.0, 10.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 symmetric matrix `A` should be referenced.
51+
- **N**: number of elements along each dimension of `A`.
52+
- **α**: scalar constant.
53+
- **x**: input array.
54+
- **sx**: stride length for `x`.
55+
- **A**: input matrix stored in linear memory.
56+
- **LDA**: stride of the first dimension of `A` (a.k.a., leading dimension of the matrix `A`).
57+
58+
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,
59+
60+
```javascript
61+
var A = [ 1.0, 2.0, 3.0, 2.0, 1.0, 2.0, 3.0, 2.0, 1.0 ];
62+
var x = [ 3.0, 2.0, 1.0 ];
63+
64+
gsyr( 'row-major', 'upper', 3, 1.0, x, -1, A, 3 );
65+
// A => <Float64Array>[ 2.0, 4.0, 6.0, 2.0, 5.0, 8.0, 3.0, 2.0, 10.0 ]
66+
```
67+
68+
Note that indexing is relative to the first index. To introduce an offset, use [`typed array`][mdn-typed-array] views.
69+
70+
<!-- eslint-disable stdlib/capitalized-comments -->
71+
72+
```javascript
73+
var Float64Array = require( '@stdlib/array/float64' );
74+
75+
// Initial arrays...
76+
var x0 = new Float64Array( [ 0.0, 3.0, 2.0, 1.0 ] );
77+
var A = new Float64Array( [ 1.0, 2.0, 3.0, 2.0, 1.0, 2.0, 3.0, 2.0, 1.0 ] );
78+
79+
// Create offset views...
80+
var x1 = new Float64Array( x0.buffer, x0.BYTES_PER_ELEMENT*1 ); // start at 2nd element
81+
82+
gsyr( 'row-major', 'upper', 3, 1.0, x1, -1, A, 3 );
83+
// A => <Float64Array>[ 2.0, 4.0, 6.0, 2.0, 5.0, 8.0, 3.0, 2.0, 10.0 ]
84+
```
85+
86+
#### gsyr.ndarray( uplo, N, α, x, sx, ox, A, sa1, sa2, oa )
87+
88+
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.
89+
90+
```javascript
91+
var Float64Array = require( '@stdlib/array/float64' );
92+
93+
var A = [ 1.0, 2.0, 3.0, 2.0, 1.0, 2.0, 3.0, 2.0, 1.0 ];
94+
var x = [ 1.0, 2.0, 3.0 ];
95+
96+
gsyr.ndarray( 'upper', 3, 1.0, x, 1, 0, A, 3, 1, 0 );
97+
// A => <Float64Array>[ 2.0, 4.0, 6.0, 2.0, 5.0, 8.0, 3.0, 2.0, 10.0 ]
98+
```
99+
100+
The function has the following additional parameters:
101+
102+
- **ox**: starting index for `x`.
103+
- **sa1**: stride of the first dimension of `A`.
104+
- **sa2**: stride of the second dimension of `A`.
105+
- **oa**: starting index for `A`.
106+
107+
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,
108+
109+
```javascript
110+
var Float64Array = require( '@stdlib/array/float64' );
111+
112+
var A = [ 1.0, 2.0, 3.0, 2.0, 1.0, 2.0, 3.0, 2.0, 1.0 ];
113+
var x = [ 1.0, 2.0, 3.0, 4.0, 5.0 ];
114+
115+
gsyr.ndarray( 'upper', 3, 1.0, x, -2, 4, A, 3, 1, 0 );
116+
// A => [ 26.0, 17.0, 8.0, 2.0, 10.0, 5.0, 3.0, 2.0, 2.0 ]
117+
```
118+
119+
</section>
120+
121+
<!-- /.usage -->
122+
123+
<section class="notes">
124+
125+
## Notes
126+
127+
- `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.
128+
- 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]).
129+
130+
</section>
131+
132+
<!-- /.notes -->
133+
134+
<section class="examples">
135+
136+
## Examples
137+
138+
<!-- eslint no-undef: "error" -->
139+
140+
```javascript
141+
var discreteUniform = require( '@stdlib/random/array/discrete-uniform' );
142+
var ones = require( '@stdlib/array/ones' );
143+
var gsyr = require( '@stdlib/blas/base/gsyr' );
144+
145+
var opts = {
146+
'dtype': 'gsyr'
147+
};
148+
149+
var N = 3;
150+
151+
// Create N-by-N symmetric matrices:
152+
var A1 = ones( N*N, opts.dtype );
153+
var A2 = ones( N*N, opts.dtype );
154+
155+
// Create a random vector:
156+
var x = discreteUniform( N, -10.0, 10.0, opts );
157+
158+
gsyr( 'row-major', 'upper', 3, 1.0, x, 1, A1, 3 );
159+
console.log( A1 );
160+
161+
gsyr.ndarray( 'upper', 3, 1.0, x, 1, 0, A2, 3, 1, 0 );
162+
console.log( A2 );
163+
```
164+
165+
</section>
166+
167+
<!-- /.examples -->
168+
169+
<!-- Section for related `stdlib` packages. Do not manually edit this section, as it is automatically populated. -->
170+
171+
<section class="related">
172+
173+
</section>
174+
175+
<!-- /.related -->
176+
177+
<!-- Section for all links. Make sure to keep an empty line after the `section` element and another before the `/section` close. -->
178+
179+
<section class="links">
180+
181+
[blas]: http://www.netlib.org/blas
182+
183+
[dsyr]: https://www.netlib.org/lapack/explore-html/dc/d82/group__her_ga07f0e3f8592107877f12a554a41c7413.html#ga07f0e3f8592107877f12a554a41c7413
184+
185+
[mdn-typed-array]: https://developer.mozilla.org/en-US/docs/Web/JavaScript/Reference/Global_Objects/TypedArray
186+
187+
[@stdlib/blas/base/dsyr]: https://github.com/stdlib-js/stdlib/tree/develop/lib/node_modules/%40stdlib/blas/base/dsyr
188+
189+
[@stdlib/blas/base/ssyr]: https://github.com/stdlib-js/stdlib/tree/develop/lib/node_modules/%40stdlib/blas/base/ssyr
190+
191+
[@stdlib/array/base/accessor]: https://github.com/stdlib-js/stdlib/tree/develop/lib/node_modules/%40stdlib/array/base/accessor
192+
193+
194+
</section>
195+
196+
<!-- /.links -->
Lines changed: 93 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,93 @@
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': 'float64'
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+
function benchmark( b ) {
54+
var z;
55+
var i;
56+
57+
b.tic();
58+
for ( i = 0; i < b.iterations; i++ ) {
59+
z = gsyr( 'row-major', 'upper', N, 1.0, x, 1, A, N );
60+
if ( isnan( z[ i%z.length ] ) ) {
61+
b.fail( 'should not return NaN' );
62+
}
63+
}
64+
b.toc();
65+
if ( isnan( z[ i%z.length ] ) ) {
66+
b.fail( 'should not return NaN' );
67+
}
68+
b.pass( 'benchmark finished' );
69+
b.end();
70+
}
71+
}
72+
73+
74+
// MAIN //
75+
76+
function main() {
77+
var min;
78+
var max;
79+
var N;
80+
var f;
81+
var i;
82+
83+
min = 1; // 10^min
84+
max = 6; // 10^max
85+
86+
for ( i = min; i <= max; i++ ) {
87+
N = floor( pow( pow( 10, i ), 1.0/2.0 ) );
88+
f = createBenchmark( N );
89+
bench( pkg+':size='+(N*N), f );
90+
}
91+
}
92+
93+
main();
Lines changed: 93 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,93 @@
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': 'float64'
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+
function benchmark( b ) {
54+
var z;
55+
var i;
56+
57+
b.tic();
58+
for ( i = 0; i < b.iterations; i++ ) {
59+
z = gsyr( 'upper', N, 1.0, x, 1, 0, A, N, 1, 0 );
60+
if ( isnan( z[ i%z.length ] ) ) {
61+
b.fail( 'should not return NaN' );
62+
}
63+
}
64+
b.toc();
65+
if ( isnan( z[ i%z.length ] ) ) {
66+
b.fail( 'should not return NaN' );
67+
}
68+
b.pass( 'benchmark finished' );
69+
b.end();
70+
}
71+
}
72+
73+
74+
// MAIN //
75+
76+
function main() {
77+
var min;
78+
var max;
79+
var N;
80+
var f;
81+
var i;
82+
83+
min = 1; // 10^min
84+
max = 6; // 10^max
85+
86+
for ( i = min; i <= max; i++ ) {
87+
N = floor( pow( pow( 10, i ), 1.0/2.0 ) );
88+
f = createBenchmark( N );
89+
bench( pkg+':ndarray:size='+(N*N), f );
90+
}
91+
}
92+
93+
main();

0 commit comments

Comments
 (0)