Skip to content

Commit 444f507

Browse files
aman-095kgrytestdlib-bot
authored andcommitted
feat: add C implementation for blas/base/sspr
PR-URL: stdlib-js#4491 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 a5f92da commit 444f507

26 files changed

+2824
-14
lines changed

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

+77-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,63 @@ console.log( AP );
183183
### Usage
184184

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

189-
#### TODO
189+
#### c_sspr( order, uplo, N, alpha, \*X, strideX, \*AP )
190190

191-
TODO.
191+
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 supplied in packed form.
192+
193+
```c
194+
#include "stdlib/blas/base/shared.h"
195+
196+
float AP[] = { 1.0f, 2.0f, 3.0f, 1.0f, 2.0f, 1.0f };
197+
const float x[] = { 1.0f, 2.0f, 3.0f };
198+
199+
c_sspr( CblasColMajor, CblasUpper, 3, 1.0f, x, 1, AP );
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 vector.
209+
- **strideX**: `[in] CBLAS_INT` stride length for `X`.
210+
- **AP**: `[inout] float*` packed form of a symmetric matrix `A`.
192211
193212
```c
194-
TODO
213+
void c_sspr( const CBLAS_LAYOUT order, const CBLAS_UPLO uplo, const CBLAS_INT N, const float alpha, const float *X, const CBLAS_INT strideX, float *AP )
195214
```
196215

197-
TODO
216+
#### c_sspr_ndarray( order, uplo, N, alpha, \*X, strideX, \*AP, strideAP, offsetAP )
217+
218+
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 supplied in packed form using alternative indexing semantics.
219+
220+
```c
221+
#include "stdlib/blas/base/shared.h"
222+
223+
float AP[] = { 1.0f, 2.0f, 3.0f, 1.0f, 2.0f, 1.0f };
224+
const float x[] = { 1.0f, 2.0f, 3.0f };
225+
226+
c_sspr_ndarray( CblasColMajor, CblasUpper, 3, 1.0f, x, 1, AP, 1, 0 );
227+
```
228+
229+
The function accepts the following arguments:
230+
231+
- **order**: `[in] CBLAS_LAYOUT` storage layout.
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 vector.
236+
- **strideX**: `[in] CBLAS_INT` stride length for `X`.
237+
- **AP**: `[inout] float*` packed form of a symmetric matrix `A`.
238+
- **strideAP**: `[in] CBLAS_INT` stride length for `AP`.
239+
- **offsetAP**: `[in] CBLAS_INT` starting index for `AP`.
198240
199241
```c
200-
TODO
242+
void c_sspr_ndarray( const CBLAS_LAYOUT order, const CBLAS_UPLO uplo, const CBLAS_INT N, const float alpha, const float *X, const CBLAS_INT strideX, const CBLAS_INT offsetX, float *AP, const CBLAS_INT strideAP, const CBLAS_INT offsetAP )
201243
```
202244

203245
</section>
@@ -219,7 +261,34 @@ TODO
219261
### Examples
220262

221263
```c
222-
TODO
264+
#include "stdlib/blas/base/sspr.h"
265+
#include "stdlib/blas/base/shared.h"
266+
#include <stdio.h>
267+
268+
int main( void ) {
269+
// Create strided arrays:
270+
float AP[] = { 1.0f, 2.0f, 3.0f, 1.0f, 2.0f, 1.0f };
271+
const float x[] = { 1.0f, 2.0f, 3.0f };
272+
273+
// Specify the number of elements along each dimension of `A`:
274+
const int N = 3;
275+
276+
// Perform the symmetric rank 1 operation `A = α*x*x^T + A`:
277+
c_sspr( CblasRowMajor, CblasUpper, N, 1.0f, x, 1, AP );
278+
279+
// Print the result:
280+
for ( int i = 0; i < N*(N+1)/2; i++ ) {
281+
printf( "AP[ %i ] = %f\n", i, AP[ i ] );
282+
}
283+
284+
// Perform the symmetric rank 1 operation `A = α*x*x^T + A` using alternative indexing semantics:
285+
c_sspr_ndarray( CblasRowMajor, CblasUpper, N, 1.0f, x, 1, 0, AP, 1, 0 );
286+
287+
// Print the result:
288+
for ( int i = 0; i < N*(N+1)/2; i++ ) {
289+
printf( "AP[ %i ] = %f\n", i, AP[ i ] );
290+
}
291+
}
223292
```
224293
225294
</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 uniform = require( '@stdlib/random/array/uniform' );
26+
var isnanf = require( '@stdlib/math/base/assert/is-nanf' );
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 sspr = tryRequire( resolve( __dirname, './../lib/sspr.native.js' ) );
36+
var opts = {
37+
'skip': ( sspr 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 AP = uniform( N * ( N + 1 ) / 2, -10.0, 10.0, options );
55+
var x = uniform( N, -10.0, 10.0, options );
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 = sspr( 'row-major', 'upper', N, 1.0, x, 1, AP );
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 len;
94+
var min;
95+
var max;
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+
len = floor( pow( pow( 10, i ), 1.0/2.0 ) );
104+
f = createBenchmark( len );
105+
bench( pkg+'::native:size='+( len * ( len + 1 ) / 2 ), 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 uniform = require( '@stdlib/random/array/uniform' );
26+
var isnanf = require( '@stdlib/math/base/assert/is-nanf' );
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 sspr = tryRequire( resolve( __dirname, './../lib/ndarray.native.js' ) );
36+
var opts = {
37+
'skip': ( sspr 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 AP = uniform( N * ( N + 1 ) / 2, -10.0, 10.0, options );
55+
var x = uniform( N, -10.0, 10.0, options );
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 = sspr( 'row-major', 'upper', N, 1.0, x, 1, 0, AP, 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 len;
94+
var min;
95+
var max;
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+
len = floor( pow( pow( 10, i ), 1.0/2.0 ) );
104+
f = createBenchmark( len );
105+
bench( pkg+'::native:ndarray:size='+( len * ( len + 1 ) / 2 ), opts, f );
106+
}
107+
}
108+
109+
main();

0 commit comments

Comments
 (0)