Skip to content

Commit da12183

Browse files
aman-095kgryte
andauthored
feat: add blas/base/dspr
PR-URL: #2794 Ref: #2039 Co-authored-by: Athan Reines <kgryte@gmail.com> Reviewed-by: Athan Reines <kgryte@gmail.com>
1 parent abcf36e commit da12183

38 files changed

+2859
-92
lines changed
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,255 @@
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+
# dspr
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 dspr = require( '@stdlib/blas/base/dspr' );
31+
```
32+
33+
#### dspr( order, uplo, N, α, x, sx, AP )
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 supplied in packed form.
36+
37+
```javascript
38+
var Float64Array = require( '@stdlib/array/float64' );
39+
40+
var AP = new Float64Array( [ 1.0, 2.0, 3.0, 1.0, 2.0, 1.0 ] );
41+
var x = new Float64Array( [ 1.0, 2.0, 3.0 ] );
42+
43+
dspr( 'row-major', 'upper', 3, 1.0, x, 1, AP );
44+
// AP => <Float64Array>[ 2.0, 4.0, 6.0, 5.0, 8.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` is supplied.
51+
- **N**: number of elements along each dimension of `A`.
52+
- **α**: scalar constant.
53+
- **x**: input [`Float64Array`][mdn-float64array].
54+
- **sx**: index increment for `x`.
55+
- **AP**: packed form of a symmetric matrix `A` stored as a [`Float64Array`][mdn-float64array].
56+
57+
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,
58+
59+
```javascript
60+
var Float64Array = require( '@stdlib/array/float64' );
61+
62+
var AP = new Float64Array( [ 1.0, 2.0, 3.0, 1.0, 2.0, 1.0 ] );
63+
var x = new Float64Array( [ 3.0, 2.0, 1.0 ] );
64+
65+
dspr( 'row-major', 'upper', 3, 1.0, x, -1, AP );
66+
// AP => <Float64Array>[ 2.0, 4.0, 6.0, 5.0, 8.0, 10.0 ]
67+
```
68+
69+
Note that indexing is relative to the first index. To introduce an offset, use [`typed array`][mdn-typed-array] views.
70+
71+
<!-- eslint-disable stdlib/capitalized-comments -->
72+
73+
```javascript
74+
var Float64Array = require( '@stdlib/array/float64' );
75+
76+
// Initial arrays...
77+
var x0 = new Float64Array( [ 0.0, 3.0, 2.0, 1.0 ] );
78+
var AP = new Float64Array( [ 1.0, 2.0, 3.0, 1.0, 2.0, 1.0 ] );
79+
80+
// Create offset views...
81+
var x1 = new Float64Array( x0.buffer, x0.BYTES_PER_ELEMENT*1 ); // start at 2nd element
82+
83+
dspr( 'row-major', 'upper', 3, 1.0, x1, -1, AP );
84+
// AP => <Float64Array>[ 2.0, 4.0, 6.0, 5.0, 8.0, 10.0 ]
85+
```
86+
87+
#### dspr.ndarray( uplo, N, α, x, sx, ox, AP, sap, oap )
88+
89+
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 supplied in packed form.
90+
91+
```javascript
92+
var Float64Array = require( '@stdlib/array/float64' );
93+
94+
var AP = new Float64Array( [ 1.0, 1.0, 2.0, 1.0, 2.0, 3.0 ] );
95+
var x = new Float64Array( [ 1.0, 2.0, 3.0 ] );
96+
97+
dspr.ndarray( 'row-major', 'lower', 3, 1.0, x, 1, 0, AP, 1, 0 );
98+
// AP => <Float64Array>[ 2.0, 3.0, 6.0, 4.0, 8.0, 12.0 ]
99+
```
100+
101+
The function has the following additional parameters:
102+
103+
- **ox**: starting index for `x`.
104+
- **sap**: `AP` stride length.
105+
- **oap**: starting index for `AP`.
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 AP = new Float64Array( [ 1.0, 2.0, 3.0, 1.0, 2.0, 1.0 ] );
113+
var x = new Float64Array( [ 3.0, 2.0, 1.0 ] );
114+
115+
dspr.ndarray( 'row-major', 'upper', 3, 1.0, x, -1, 2, AP, 1, 0 );
116+
// AP => <Float64Array>[ 2.0, 4.0, 6.0, 5.0, 8.0, 10.0 ]
117+
```
118+
119+
</section>
120+
121+
<!-- /.usage -->
122+
123+
<section class="notes">
124+
125+
## Notes
126+
127+
- `dspr()` corresponds to the [BLAS][blas] level 2 function [`dspr`][blas-dspr].
128+
129+
</section>
130+
131+
<!-- /.notes -->
132+
133+
<section class="examples">
134+
135+
## Examples
136+
137+
<!-- eslint no-undef: "error" -->
138+
139+
```javascript
140+
var discreteUniform = require( '@stdlib/random/array/discrete-uniform' );
141+
var dspr = require( '@stdlib/blas/base/dspr' );
142+
143+
var opts = {
144+
'dtype': 'float64'
145+
};
146+
147+
var N = 5;
148+
149+
var AP = discreteUniform( N * ( N + 1 ) / 2, -10.0, 10.0, opts );
150+
var x = discreteUniform( N, -10.0, 10.0, opts );
151+
152+
dspr( 'column-major', 'upper', N, 1.0, x, 1, AP );
153+
console.log( AP );
154+
155+
dspr.ndarray( 'column-major', 'upper', N, 1.0, x, 1, 0, AP, 1, 0 );
156+
console.log( AP );
157+
```
158+
159+
</section>
160+
161+
<!-- /.examples -->
162+
163+
<!-- C interface documentation. -->
164+
165+
* * *
166+
167+
<section class="c">
168+
169+
## C APIs
170+
171+
<!-- Section to include introductory text. Make sure to keep an empty line after the intro `section` element and another before the `/section` close. -->
172+
173+
<section class="intro">
174+
175+
</section>
176+
177+
<!-- /.intro -->
178+
179+
<!-- C usage documentation. -->
180+
181+
<section class="usage">
182+
183+
### Usage
184+
185+
```c
186+
TODO
187+
```
188+
189+
#### TODO
190+
191+
TODO.
192+
193+
```c
194+
TODO
195+
```
196+
197+
TODO
198+
199+
```c
200+
TODO
201+
```
202+
203+
</section>
204+
205+
<!-- /.usage -->
206+
207+
<!-- C API usage notes. Make sure to keep an empty line after the `section` element and another before the `/section` close. -->
208+
209+
<section class="notes">
210+
211+
</section>
212+
213+
<!-- /.notes -->
214+
215+
<!-- C API usage examples. -->
216+
217+
<section class="examples">
218+
219+
### Examples
220+
221+
```c
222+
TODO
223+
```
224+
225+
</section>
226+
227+
<!-- /.examples -->
228+
229+
</section>
230+
231+
<!-- /.c -->
232+
233+
<!-- Section for related `stdlib` packages. Do not manually edit this section, as it is automatically populated. -->
234+
235+
<section class="related">
236+
237+
</section>
238+
239+
<!-- /.related -->
240+
241+
<!-- Section for all links. Make sure to keep an empty line after the `section` element and another before the `/section` close. -->
242+
243+
<section class="links">
244+
245+
[blas]: http://www.netlib.org/blas
246+
247+
[blas-dspr]: https://www.netlib.org/lapack/explore-html/d5/df9/group__hpr_gaa5d4297738fb1391709c645a7c2bee5e.html#gaa5d4297738fb1391709c645a7c2bee5e
248+
249+
[mdn-float64array]: https://developer.mozilla.org/en-US/docs/Web/JavaScript/Reference/Global_Objects/Float64Array
250+
251+
[mdn-typed-array]: https://developer.mozilla.org/en-US/docs/Web/JavaScript/Reference/Global_Objects/TypedArray
252+
253+
</section>
254+
255+
<!-- /.links -->
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,104 @@
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 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 dspr = require( './../lib/dspr.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 AP = uniform( N * ( N + 1 ) / 2, -10.0, 10.0, options );
50+
var x = uniform( N, -10.0, 10.0, options );
51+
return benchmark;
52+
53+
/**
54+
* Benchmark function.
55+
*
56+
* @private
57+
* @param {Benchmark} b - benchmark instance
58+
*/
59+
function benchmark( b ) {
60+
var z;
61+
var i;
62+
63+
b.tic();
64+
for ( i = 0; i < b.iterations; i++ ) {
65+
z = dspr( 'row-major', 'upper', N, 1.0, x, 1, AP );
66+
if ( isnan( z[ i%z.length ] ) ) {
67+
b.fail( 'should not return NaN' );
68+
}
69+
}
70+
b.toc();
71+
if ( isnan( z[ i%z.length ] ) ) {
72+
b.fail( 'should not return NaN' );
73+
}
74+
b.pass( 'benchmark finished' );
75+
b.end();
76+
}
77+
}
78+
79+
80+
// MAIN //
81+
82+
/**
83+
* Main execution sequence.
84+
*
85+
* @private
86+
*/
87+
function main() {
88+
var len;
89+
var min;
90+
var max;
91+
var f;
92+
var i;
93+
94+
min = 1; // 10^min
95+
max = 6; // 10^max
96+
97+
for ( i = min; i <= max; i++ ) {
98+
len = floor( pow( pow( 10, i ), 1.0/2.0 ) );
99+
f = createBenchmark( len );
100+
bench( pkg+':size='+( len * ( len + 1 ) / 2 ), f );
101+
}
102+
}
103+
104+
main();

0 commit comments

Comments
 (0)