Skip to content

Commit 75e2622

Browse files
ShabiShett07kgrytestdlib-botaman-095
authored
feat: add C implementation for blas/base/dsyr2
PR-URL: #6572 Ref: #2039 Co-authored-by: Athan Reines <[email protected]> Reviewed-by: Athan Reines <[email protected]> Co-authored-by: stdlib-bot <[email protected]> Co-authored-by: Aman Bhansali <[email protected]> Reviewed-by: Aman Bhansali <[email protected]>
1 parent 518f815 commit 75e2622

30 files changed

+4159
-58
lines changed

lib/node_modules/@stdlib/blas/base/dsyr2/README.md

Lines changed: 122 additions & 22 deletions
Original file line numberDiff line numberDiff line change
@@ -37,12 +37,12 @@ Performs the symmetric rank 2 operation `A = α*x*y^T + α*y*x^T + A`, where `α
3737
```javascript
3838
var Float64Array = require( '@stdlib/array/float64' );
3939

40-
var A = new Float64Array( [ 1.0, 2.0, 3.0, 0.0, 1.0, 2.0, 0.0, 0.0, 1.0 ] );
40+
var A = new Float64Array( [ 1.0, 2.0, 3.0, 2.0, 1.0, 2.0, 3.0, 2.0, 1.0 ] );
4141
var x = new Float64Array( [ 1.0, 2.0, 3.0 ] );
4242
var y = new Float64Array( [ 1.0, 2.0, 3.0 ] );
4343

4444
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 ]
45+
// A => <Float64Array>[ 3.0, 6.0, 9.0, 2.0, 9.0, 14.0, 3.0, 2.0, 19.0 ]
4646
```
4747

4848
The function has the following parameters:
@@ -52,9 +52,9 @@ The function has the following parameters:
5252
- **N**: number of elements along each dimension of `A`.
5353
- **α**: scalar constant.
5454
- **x**: first input [`Float64Array`][mdn-float64array].
55-
- **sx**: index increment for `x`.
55+
- **sx**: stride length for `x`.
5656
- **y**: second input [`Float64Array`][mdn-float64array].
57-
- **sy**: index increment for `y`.
57+
- **sy**: stride length for `y`.
5858
- **A**: input matrix stored in linear memory as a [`Float64Array`][mdn-float64array].
5959
- **lda**: stride of the first dimension of `A` (a.k.a., leading dimension of the matrix `A`).
6060

@@ -63,12 +63,12 @@ The stride parameters determine how elements in the input arrays are accessed at
6363
```javascript
6464
var Float64Array = require( '@stdlib/array/float64' );
6565

66-
var A = new Float64Array( [ 1.0, 2.0, 3.0, 0.0, 1.0, 2.0, 0.0, 0.0, 1.0 ] );
66+
var A = new Float64Array( [ 1.0, 2.0, 3.0, 2.0, 1.0, 2.0, 3.0, 2.0, 1.0 ] );
6767
var x = new Float64Array( [ 1.0, 2.0, 3.0, 4.0, 5.0 ] );
6868
var y = new Float64Array( [ 1.0, 2.0, 3.0 ] );
6969

7070
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 ]
71+
// A => <Float64Array>[ 3.0, 7.0, 11.0, 2.0, 13.0, 21.0, 3.0, 2.0, 31.0 ]
7272
```
7373

7474
Note that indexing is relative to the first index. To introduce an offset, use [`typed array`][mdn-typed-array] views.
@@ -81,14 +81,14 @@ var Float64Array = require( '@stdlib/array/float64' );
8181
// Initial arrays...
8282
var x0 = new Float64Array( [ 0.0, 1.0, 1.0, 1.0 ] );
8383
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 ] );
84+
var A = new Float64Array( [ 1.0, 2.0, 3.0, 2.0, 1.0, 2.0, 3.0, 2.0, 1.0 ] );
8585

8686
// Create offset views...
8787
var x1 = new Float64Array( x0.buffer, x0.BYTES_PER_ELEMENT*1 ); // start at 2nd element
8888
var y1 = new Float64Array( y0.buffer, y0.BYTES_PER_ELEMENT*1 ); // start at 2nd element
8989

9090
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 ]
91+
// A => <Float64Array>[ 3.0, 5.0, 7.0, 2.0, 5.0, 7.0, 3.0, 2.0, 7.0 ]
9292
```
9393

9494
#### dsyr2.ndarray( uplo, N, α, x, sx, ox, y, sy, oy, A, sa1, sa2, oa )
@@ -98,12 +98,12 @@ Performs the symmetric rank 2 operation `A = α*x*y^T + α*y*x^T + A`, using alt
9898
```javascript
9999
var Float64Array = require( '@stdlib/array/float64' );
100100

101-
var A = new Float64Array( [ 1.0, 2.0, 3.0, 0.0, 1.0, 2.0, 0.0, 0.0, 1.0 ] );
101+
var A = new Float64Array( [ 1.0, 2.0, 3.0, 2.0, 1.0, 2.0, 3.0, 2.0, 1.0 ] );
102102
var x = new Float64Array( [ 1.0, 2.0, 3.0 ] );
103103
var y = new Float64Array( [ 1.0, 2.0, 3.0 ] );
104104

105105
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 ]
106+
// A => <Float64Array>[ 3.0, 6.0, 9.0, 2.0, 9.0, 14.0, 3.0, 2.0, 19.0 ]
107107
```
108108

109109
The function has the following additional parameters:
@@ -119,12 +119,12 @@ While [`typed array`][mdn-typed-array] views mandate a view offset based on the
119119
```javascript
120120
var Float64Array = require( '@stdlib/array/float64' );
121121

122-
var A = new Float64Array( [ 1.0, 2.0, 3.0, 0.0, 1.0, 2.0, 0.0, 0.0, 1.0 ] );
122+
var A = new Float64Array( [ 1.0, 2.0, 3.0, 2.0, 1.0, 2.0, 3.0, 2.0, 1.0 ] );
123123
var x = new Float64Array( [ 1.0, 2.0, 3.0, 4.0, 5.0 ] );
124124
var y = new Float64Array( [ 1.0, 2.0, 3.0 ] );
125125

126126
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 ]
127+
// A => <Float64Array>[ 11.0, 15.0, 19.0, 2.0, 13.0, 13.0, 3.0, 2.0, 7.0 ]
128128
```
129129

130130
</section>
@@ -158,12 +158,19 @@ var opts = {
158158

159159
var N = 3;
160160

161-
var A = ones( N*N, opts.dtype );
161+
// Create N-by-N symmetric matrices:
162+
var A1 = ones( N*N, opts.dtype );
163+
var A2 = ones( N*N, opts.dtype );
164+
165+
// Create random vectors:
162166
var x = discreteUniform( N, -10.0, 10.0, opts );
163167
var y = discreteUniform( N, -10.0, 10.0, opts );
164168

165-
dsyr2( 'row-major', 'upper', 3, 1.0, x, 1, y, 1, A, 3 );
166-
console.log( A );
169+
dsyr2( 'row-major', 'upper', 3, 1.0, x, 1, y, 1, A1, 3 );
170+
console.log( A1 );
171+
172+
dsyr2.ndarray( 'upper', 3, 1.0, x, 1, 0, y, 1, 0, A2, 3, 1, 0 );
173+
console.log( A2 );
167174
```
168175

169176
</section>
@@ -193,21 +200,74 @@ console.log( A );
193200
### Usage
194201

195202
```c
196-
TODO
203+
#include "stdlib/blas/base/dsyr2.h"
204+
```
205+
206+
#### c_dsyr2( order, uplo, N, alpha, \*X, sx, \*Y, sy, \*A, LDA )
207+
208+
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.
209+
210+
```c
211+
#include "stdlib/blas/base/shared.h"
212+
213+
double A[] = { 1.0, 2.0, 3.0, 2.0, 1.0, 2.0, 3.0, 2.0, 1.0 };
214+
const double x[] = { 1.0, 2.0, 3.0 };
215+
const double y[] = { 1.0, 2.0, 3.0 };
216+
217+
c_dsyr2( CblasColMajor, CblasUpper, 3, 1.0, x, 1, y, 1, A, 3 );
197218
```
198219
199-
#### TODO
220+
The function accepts the following arguments:
200221
201-
TODO.
222+
- **order**: `[in] CBLAS_LAYOUT` storage layout.
223+
- **uplo**: `[in] CBLAS_UPLO` specifies whether the upper or lower triangular part of the symmetric matrix `A` should be referenced.
224+
- **N**: `[in] CBLAS_INT` number of elements along each dimension of `A`.
225+
- **alpha**: `[in] double` scalar constant.
226+
- **X**: `[in] double*` first input array.
227+
- **strideX**: `[in] CBLAS_INT` stride length for `X`.
228+
- **Y**: `[in] double*` second input array.
229+
- **strideY**: `[in] CBLAS_INT` stride length for `Y`.
230+
- **A**: `[inout] double*` input matrix.
231+
- **LDA**: `[in] CBLAS_INT` stride of the first dimension of `A` (a.k.a., leading dimension of the matrix `A`).
202232
203233
```c
204-
TODO
234+
void c_dsyr2( const CBLAS_LAYOUT order, const CBLAS_UPLO uplo, const CBLAS_INT N, const double alpha, const double *X, const CBLAS_INT strideX, const double *Y, const CBLAS_INT strideY, double *A, const CBLAS_INT LDA )
235+
```
236+
237+
<!-- lint disable maximum-heading-length -->
238+
239+
#### c_dsyr2_ndarray( uplo, N, alpha, \*X, sx, ox, \*Y, sy, oy, \*A, sa1, sa2, oa )
240+
241+
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.
242+
243+
```c
244+
#include "stdlib/blas/base/shared.h"
245+
246+
double A[] = { 1.0, 2.0, 3.0, 2.0, 1.0, 2.0, 3.0, 2.0, 1.0 };
247+
const double x[] = { 1.0, 2.0, 3.0 };
248+
const double y[] = { 1.0, 2.0, 3.0 };
249+
250+
c_dsyr2_ndarray( CblasUpper, 3, 1.0, x, 1, 0, y, 1, 0, A, 3, 1, 0 );
205251
```
206252
207-
TODO
253+
The function accepts the following arguments:
254+
255+
- **uplo**: `[in] CBLAS_UPLO` specifies whether the upper or lower triangular part of the symmetric matrix `A` should be referenced.
256+
- **N**: `[in] CBLAS_INT` number of elements along each dimension of `A`.
257+
- **alpha**: `[in] double` scalar constant.
258+
- **X**: `[in] double*` first input array.
259+
- **sx**: `[in] CBLAS_INT` stride length for `X`.
260+
- **ox**: `[in] CBLAS_INT` starting index for `X`.
261+
- **Y**: `[in] double` second input array.
262+
- **sy**: `[in] CBLAS_INT` stride length for `Y`.
263+
- **oy**: `[in] CBLAS_INT` starting index for `Y`.
264+
- **A**: `[inout] double*` input matrix.
265+
- **sa1**: `[in] CBLAS_INT` stride of the first dimension of `A`.
266+
- **sa2**: `[in] CBLAS_INT` stride of the second dimension of `A`.
267+
- **oa**: `[in] CBLAS_INT` starting index for `A`.
208268
209269
```c
210-
TODO
270+
void c_dsyr2_ndarray( const CBLAS_UPLO uplo, const CBLAS_INT N, const double alpha, const double *X, const CBLAS_INT strideX, const CBLAS_INT offsetX, const double *Y, CBLAS_INT strideY, const CBLAS_INT offsetY, double *A, const CBLAS_INT strideA1, const CBLAS_INT strideA2, const CBLAS_INT offsetA )
211271
```
212272

213273
</section>
@@ -229,7 +289,47 @@ TODO
229289
### Examples
230290

231291
```c
232-
TODO
292+
#include "stdlib/blas/base/dsyr2.h"
293+
#include "stdlib/blas/base/shared.h"
294+
#include <stdio.h>
295+
296+
int main( void ) {
297+
// Define 3x3 symmetric matrices stored in row-major layout:
298+
double A1[ 3*3 ] = {
299+
1.0, 2.0, 3.0,
300+
2.0, 1.0, 2.0,
301+
3.0, 2.0, 1.0
302+
};
303+
304+
double A2[ 3*3 ] = {
305+
1.0, 2.0, 3.0,
306+
2.0, 1.0, 2.0,
307+
3.0, 2.0, 1.0
308+
};
309+
310+
// Define `x` and `y` vectors:
311+
const double x[ 3 ] = { 1.0, 2.0, 3.0 };
312+
const double y[ 3 ] = { 1.0, 2.0, 3.0 };
313+
314+
// Specify the number of elements along each dimension of `A1` and `A2`:
315+
const int N = 3;
316+
317+
// Perform the symmetric rank 2 operation `A = α*x*y^T + α*y*x^T + A`:
318+
c_dsyr2( CblasColMajor, CblasUpper, N, 1.0, x, 1, y, 1, A1, N );
319+
320+
// Print the result:
321+
for ( int i = 0; i < N*N; i++ ) {
322+
printf( "A1[ %i ] = %lf\n", i, A1[ i ] );
323+
}
324+
325+
// Perform the symmetric rank 2 operation `A = α*x*y^T + α*y*x^T + A` using alternative indexing semantics:
326+
c_dsyr2_ndarray( CblasUpper, N, 1.0, x, 1, 0, y, 1, 0, A2, N, 1, 0 );
327+
328+
// Print the result:
329+
for ( int i = 0; i < N*N; i++ ) {
330+
printf( "A2[ %i ] = %lf\n", i, A2[ i ] );
331+
}
332+
}
233333
```
234334
235335
</section>
Lines changed: 110 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,110 @@
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 resolve = require( 'path' ).resolve;
24+
var bench = require( '@stdlib/bench' );
25+
var isnan = require( '@stdlib/math/base/assert/is-nan' );
26+
var ones = require( '@stdlib/array/ones' );
27+
var pow = require( '@stdlib/math/base/special/pow' );
28+
var floor = require( '@stdlib/math/base/special/floor' );
29+
var tryRequire = require( '@stdlib/utils/try-require' );
30+
var pkg = require( './../package.json' ).name;
31+
32+
33+
// VARIABLES //
34+
35+
var dsyr2 = tryRequire( resolve( __dirname, './../lib/dsyr2.native.js' ) );
36+
var opts = {
37+
'skip': ( dsyr2 instanceof Error )
38+
};
39+
var options = {
40+
'dtype': 'float64'
41+
};
42+
43+
44+
// FUNCTIONS //
45+
46+
/**
47+
* Creates a benchmark function.
48+
*
49+
* @private
50+
* @param {PositiveInteger} N - number of elements along each dimension
51+
* @returns {Function} benchmark function
52+
*/
53+
function createBenchmark( N ) {
54+
var x = ones( N, options.dtype );
55+
var y = ones( N, options.dtype );
56+
var A = ones( N*N, options.dtype );
57+
return benchmark;
58+
59+
/**
60+
* Benchmark function.
61+
*
62+
* @private
63+
* @param {Benchmark} b - benchmark instance
64+
*/
65+
function benchmark( b ) {
66+
var z;
67+
var i;
68+
69+
b.tic();
70+
for ( i = 0; i < b.iterations; i++ ) {
71+
z = dsyr2( 'row-major', 'upper', N, 1.0, x, 1, y, 1, A, N );
72+
if ( isnan( z[ i%z.length ] ) ) {
73+
b.fail( 'should not return NaN' );
74+
}
75+
}
76+
b.toc();
77+
if ( isnan( z[ i%z.length ] ) ) {
78+
b.fail( 'should not return NaN' );
79+
}
80+
b.pass( 'benchmark finished' );
81+
b.end();
82+
}
83+
}
84+
85+
86+
// MAIN //
87+
88+
/**
89+
* Main execution sequence.
90+
*
91+
* @private
92+
*/
93+
function main() {
94+
var min;
95+
var max;
96+
var N;
97+
var f;
98+
var i;
99+
100+
min = 1; // 10^min
101+
max = 6; // 10^max
102+
103+
for ( i = min; i <= max; i++ ) {
104+
N = floor( pow( pow( 10, i ), 1.0/2.0 ) );
105+
f = createBenchmark( N );
106+
bench( pkg+'::native:size='+(N*N), opts, f );
107+
}
108+
}
109+
110+
main();

0 commit comments

Comments
 (0)