Skip to content

Commit 6966bbb

Browse files
aman-095kgryte
andauthored
feat: add blas/base/dsyr2
PR-URL: #2712 Ref: #2039 Co-authored-by: Athan Reines <kgryte@gmail.com> Reviewed-by: Athan Reines <kgryte@gmail.com>
1 parent 7246126 commit 6966bbb

40 files changed

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

0 commit comments

Comments
 (0)