Skip to content

Commit f58fbed

Browse files
aman-095kgrytestdlib-bot
authored andcommitted
feat: add C implementation for blas/base/ssyr
PR-URL: stdlib-js#2877 Ref: stdlib-js#2039 Co-authored-by: Athan Reines <[email protected]> Reviewed-by: Athan Reines <[email protected]> Co-authored-by: stdlib-bot <[email protected]>
1 parent dd5b674 commit f58fbed

23 files changed

+2908
-10
lines changed

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

+79-8
Original file line numberDiff line numberDiff line change
@@ -2,7 +2,7 @@
22
33
@license Apache-2.0
44
5-
Copyright (c) 2024 The Stdlib Authors.
5+
Copyright (c) 2025 The Stdlib Authors.
66
77
Licensed under the Apache License, Version 2.0 (the "License");
88
you may not use this file except in compliance with the License.
@@ -183,21 +183,65 @@ console.log( A );
183183
### Usage
184184

185185
```c
186-
TODO
186+
#include "stdlib/blas/base/ssyr.h"
187187
```
188188

189-
#### TODO
189+
#### c_ssyr( order, uplo, N, alpha, \*X, strideX, \*A, LDA )
190190

191-
TODO.
191+
Performs the symmetric rank 1 operation `A = α*x*x^T + A`.
192192

193193
```c
194-
TODO
194+
#include "stdlib/blas/base/shared.h"
195+
196+
float A[] = { 1.0f, 0.0f, 0.0f, 2.0f, 1.0f, 0.0f, 3.0f, 2.0f, 1.0f };
197+
const float x[] = { 1.0f, 2.0f, 3.0f };
198+
199+
c_ssyr( CblasColMajor, CblasUpper, 3, 1.0f, x, 1, A, 3 );
200+
```
201+
202+
The function accepts the following arguments:
203+
204+
- **order**: `[in] CBLAS_LAYOUT` storage layout.
205+
- **uplo**: `[in] CBLAS_UPLO` specifies whether the upper or lower triangular part of the symmetric matrix `A` should be referenced.
206+
- **N**: `[in] CBLAS_INT` number of elements along each dimension of `A`.
207+
- **alpha**: `[in] float` scalar.
208+
- **X**: `[in] float*` input array.
209+
- **strideX**: `[in] CBLAS_INT` index increment for `X`.
210+
- **A**: `[inout] float*` input matrix.
211+
- **LDA**: `[in] CBLAS_INT` stride of the first dimension of `A` (a.k.a., leading dimension of the matrix `A`).
212+
213+
```c
214+
void c_ssyr( const CBLAS_LAYOUT order, const CBLAS_UPLO uplo, const CBLAS_INT N, const float alpha, const float *X, const CBLAS_INT strideX, float *A, const CBLAS_INT LDA )
195215
```
196216

197-
TODO
217+
#### c_ssyr_ndarray( uplo, N, alpha, \*X, strideX, offsetX, \*A, sa1, sa2, oa )
218+
219+
Performs the symmetric rank 1 operation `A = α*x*x^T + A` using alternative indexing semantics.
220+
221+
```c
222+
#include "stdlib/blas/base/shared.h"
223+
224+
float A[] = { 1.0f, 2.0f, 3.0f, 0.0f, 1.0f, 2.0f, 0.0f, 0.0f, 1.0f };
225+
const float x[] = { 1.0f, 2.0f, 3.0f };
226+
227+
c_ssyr_ndarray( CblasUpper, 3, 1.0f, x, 1, 0, A, 3, 1, 0 );
228+
```
229+
230+
The function accepts the following arguments:
231+
232+
- **uplo**: `[in] CBLAS_UPLO` specifies whether the upper or lower triangular part of the symmetric matrix `A` should be referenced.
233+
- **N**: `[in] CBLAS_INT` number of elements along each dimension of `A`.
234+
- **alpha**: `[in] float` scalar.
235+
- **X**: `[in] float*` input array.
236+
- **strideX**: `[in] CBLAS_INT` index increment for `X`.
237+
- **offsetX**: `[in] CBLAS_INT` starting index for `X`.
238+
- **A**: `[inout] float*` input matrix.
239+
- **sa1**: `[in] CBLAS_INT` stride of the first dimension of `A`.
240+
- **sa2**: `[in] CBLAS_INT` stride of the second dimension of `A`.
241+
- **oa**: `[in] CBLAS_INT` starting index for `A`.
198242
199243
```c
200-
TODO
244+
void c_ssyr_ndarray( const CBLAS_UPLO uplo, const CBLAS_INT N, const float alpha, const float *X, const CBLAS_INT strideX, const CBLAS_INT offsetX, float *A, const CBLAS_INT sa1, const CBLAS_INT sa2, const CBLAS_INT oa )
201245
```
202246

203247
</section>
@@ -219,7 +263,34 @@ TODO
219263
### Examples
220264

221265
```c
222-
TODO
266+
#include "stdlib/blas/base/ssyr.h"
267+
#include "stdlib/blas/base/shared.h"
268+
#include <stdio.h>
269+
270+
int main( void ) {
271+
// Create strided arrays:
272+
float A[] = { 1.0f, 0.0f, 0.0f, 2.0f, 1.0f, 0.0f, 3.0f, 2.0f, 1.0f };
273+
const float x[] = { 1.0f, 2.0f, 3.0f };
274+
275+
// Specify the number of elements along each dimension of `A`:
276+
const int N = 3;
277+
278+
// Perform the symmetric rank 1 operation `A = α*x*x^T + A`:
279+
c_ssyr( CblasColMajor, CblasUpper, N, 1.0f, x, 1, A, N );
280+
281+
// Print the result:
282+
for ( int i = 0; i < N*N; i++ ) {
283+
printf( "A[ %i ] = %f\n", i, A[ i ] );
284+
}
285+
286+
// Perform the symmetric rank 1 operation `A = α*x*x^T + A`:
287+
c_ssyr_ndarray( CblasUpper, N, 1.0f, x, 1, 0, A, N, 1, 0 );
288+
289+
// Print the result:
290+
for ( int i = 0; i < N*N; i++ ) {
291+
printf( "A[ %i ] = %f\n", i, A[ i ] );
292+
}
293+
}
223294
```
224295
225296
</section>
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,109 @@
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 isnanf = require( '@stdlib/math/base/assert/is-nanf' );
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 ssyr = tryRequire( resolve( __dirname, './../lib/ssyr.native.js' ) );
36+
var opts = {
37+
'skip': ( ssyr instanceof Error )
38+
};
39+
var options = {
40+
'dtype': 'float32'
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 A = ones( N*N, options.dtype );
56+
return benchmark;
57+
58+
/**
59+
* Benchmark function.
60+
*
61+
* @private
62+
* @param {Benchmark} b - benchmark instance
63+
*/
64+
function benchmark( b ) {
65+
var z;
66+
var i;
67+
68+
b.tic();
69+
for ( i = 0; i < b.iterations; i++ ) {
70+
z = ssyr( 'row-major', 'upper', N, 1.0, x, 1, A, N );
71+
if ( isnanf( z[ i%z.length ] ) ) {
72+
b.fail( 'should not return NaN' );
73+
}
74+
}
75+
b.toc();
76+
if ( isnanf( z[ i%z.length ] ) ) {
77+
b.fail( 'should not return NaN' );
78+
}
79+
b.pass( 'benchmark finished' );
80+
b.end();
81+
}
82+
}
83+
84+
85+
// MAIN //
86+
87+
/**
88+
* Main execution sequence.
89+
*
90+
* @private
91+
*/
92+
function main() {
93+
var min;
94+
var max;
95+
var N;
96+
var f;
97+
var i;
98+
99+
min = 1; // 10^min
100+
max = 6; // 10^max
101+
102+
for ( i = min; i <= max; i++ ) {
103+
N = floor( pow( pow( 10, i ), 1.0/2.0 ) );
104+
f = createBenchmark( N );
105+
bench( pkg+'::native:size='+(N*N), opts, f );
106+
}
107+
}
108+
109+
main();
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,109 @@
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 isnanf = require( '@stdlib/math/base/assert/is-nanf' );
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 ssyr = tryRequire( resolve( __dirname, './../lib/ndarray.native.js' ) );
36+
var opts = {
37+
'skip': ( ssyr instanceof Error )
38+
};
39+
var options = {
40+
'dtype': 'float32'
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 A = ones( N*N, options.dtype );
56+
return benchmark;
57+
58+
/**
59+
* Benchmark function.
60+
*
61+
* @private
62+
* @param {Benchmark} b - benchmark instance
63+
*/
64+
function benchmark( b ) {
65+
var z;
66+
var i;
67+
68+
b.tic();
69+
for ( i = 0; i < b.iterations; i++ ) {
70+
z = ssyr( 'upper', N, 1.0, x, 1, 0, A, N, 1, 0 );
71+
if ( isnanf( z[ i%z.length ] ) ) {
72+
b.fail( 'should not return NaN' );
73+
}
74+
}
75+
b.toc();
76+
if ( isnanf( z[ i%z.length ] ) ) {
77+
b.fail( 'should not return NaN' );
78+
}
79+
b.pass( 'benchmark finished' );
80+
b.end();
81+
}
82+
}
83+
84+
85+
// MAIN //
86+
87+
/**
88+
* Main execution sequence.
89+
*
90+
* @private
91+
*/
92+
function main() {
93+
var min;
94+
var max;
95+
var N;
96+
var f;
97+
var i;
98+
99+
min = 1; // 10^min
100+
max = 6; // 10^max
101+
102+
for ( i = min; i <= max; i++ ) {
103+
N = floor( pow( pow( 10, i ), 1.0/2.0 ) );
104+
f = createBenchmark( N );
105+
bench( pkg+'::native:ndarray:size='+(N*N), opts, f );
106+
}
107+
}
108+
109+
main();

0 commit comments

Comments
 (0)