Skip to content

Commit 8c74427

Browse files
Pranavchikukgryte
andauthored
feat: add lapack/base/dlassq
PR-URL: #2668 Ref: #2464 Co-authored-by: Athan Reines <kgryte@gmail.com> Reviewed-by: Athan Reines <kgryte@gmail.com>
1 parent 39a6773 commit 8c74427

File tree

16 files changed

+1923
-0
lines changed

16 files changed

+1923
-0
lines changed
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,257 @@
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+
# dlassq
22+
23+
> Return an updated sum of squares represented in scaled form.
24+
25+
<section class="intro">
26+
27+
This routine returns the values $s_{textrm{out}}$ and $\textrm{ss}_{\textrm{out}}$ such that
28+
29+
<!-- <equation class="equation" label="eq:sum_of_squares" align="center" raw="s_{\textrm{out}}^2 \cdot \textrm{ss}_{\textrm{out}} = x_0^2 + \ldots + x_{N-1}^2 + s_{\textrm{in}}^2 \cdot \textrm{ss}_{\textrm{in}}" alt="Sum of squares represented in scaled form"> -->
30+
31+
<div class="equation" align="center" data-raw-text="s_{\textrm{out}}^2 \cdot \textrm{ss}_{\textrm{out}} = x_0^2 + \ldots + x_{N-1}^2 + s_{\textrm{in}}^2 \cdot \textrm{ss}_{\textrm{in}}" data-equation="eq:sum_of_squares">
32+
<img src="" alt="Sum of squares represented in scaled form">
33+
<br>
34+
</div>
35+
36+
<!-- </equation> -->
37+
38+
where $x_i = X_{(i-1) \cdot \textrm{sx}}$ and $\textrm{sx}$ is the stride of `X`. The value of $\textrm{ss}_{\textrm{in}}$ is assumed to be nonnegative.
39+
40+
</section>
41+
42+
<!-- /.intro -->
43+
44+
<section class="usage">
45+
46+
## Usage
47+
48+
```javascript
49+
var dlassq = require( '@stdlib/lapack/base/dlassq' );
50+
```
51+
52+
#### dlassq( N, X, strideX, scale, sumsq )
53+
54+
Returns an updated sum of squares represented in scaled form.
55+
56+
```javascript
57+
var Float64Array = require( '@stdlib/array/float64' );
58+
59+
var X = new Float64Array( [ 1.0, 2.0, 3.0, 4.0 ] );
60+
61+
var out = dlassq( 4, X, 1, 1.0, 0.0 );
62+
// returns <Float64Array>[ 1.0, 30.0 ]
63+
```
64+
65+
The function has the following parameters:
66+
67+
- **N**: number of indexed elements.
68+
- **X**: input [`Float64Array`][mdn-float64array].
69+
- **strideX**: stride length for `X`.
70+
- **scale**: scaling factor.
71+
- **sumsq**: basic sum of squares from which output is factored out.
72+
73+
Note that indexing is relative to the first index. To introduce an offset, use [`typed array`][mdn-typed-array] views.
74+
75+
<!-- eslint-disable stdlib/capitalized-comments -->
76+
77+
```javascript
78+
var Float64Array = require( '@stdlib/array/float64' );
79+
80+
// Initial array:
81+
var X0 = new Float64Array( [ 0.0, 1.0, 2.0, 3.0, 4.0 ] );
82+
83+
// Create an offset view:
84+
var X1 = new Float64Array( X0.buffer, X0.BYTES_PER_ELEMENT*1 ); // start at 2nd element
85+
86+
// Compute the sum of squares:
87+
var out = dlassq( X1.length, X1, 1, 1.0, 0.0 );
88+
// returns <Float64Array>[ 1.0, 30.0 ]
89+
```
90+
91+
The returned [`Float64Array`][mdn-float64array] contains an updated scale factor and an updated sum of squares, respectively.
92+
93+
#### dlassq.ndarray( N, X, sx, ox, scale, sumsq, out, so, oo )
94+
95+
Returns an updated sum of squares represented in scaled form using alternative indexing semantics.
96+
97+
```javascript
98+
var Float64Array = require( '@stdlib/array/float64' );
99+
100+
var X = new Float64Array( [ 1.0, 2.0, 3.0, 4.0 ] );
101+
var out = new Float64Array( [ 0.0, 0.0 ] );
102+
103+
dlassq.ndarray( 4, X, 1, 0, 1.0, 0.0, out, 1, 0 );
104+
// out => <Float64Array>[ 1.0, 30.0 ]
105+
```
106+
107+
The function has the following additional parameters:
108+
109+
- **ox**: starting index for `X`.
110+
- **out**: output [`Float64Array`][mdn-float64array]
111+
- **so**: stride length for `out`.
112+
- **oo**: starting index for `out`.
113+
114+
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,
115+
116+
<!-- eslint-disable max-len -->
117+
118+
```javascript
119+
var Float64Array = require( '@stdlib/array/float64' );
120+
121+
var X = new Float64Array( [ 1.0, 0.0, 2.0, 0.0, 3.0, 0.0, 4.0 ] );
122+
var out = new Float64Array( [ 0.0, 0.0, 999.9, 0.0, 999.9 ] );
123+
124+
dlassq.ndarray( 4, X, 2, 0, 1.0, 0.0, out, 2, 1 );
125+
// out => <Float64Array>[ 0.0, 1.0, 999.9, 30.0, 999.9 ]
126+
```
127+
128+
</section>
129+
130+
<!-- /.usage -->
131+
132+
<section class="notes">
133+
134+
## Notes
135+
136+
- `dlassq()` corresponds to the [LAPACK][LAPACK] function [`dlassq`][lapack-dlassq].
137+
138+
</section>
139+
140+
<!-- /.notes -->
141+
142+
<section class="examples">
143+
144+
## Examples
145+
146+
<!-- eslint no-undef: "error" -->
147+
148+
```javascript
149+
var discreteUniform = require( '@stdlib/random/array/discrete-uniform' );
150+
var dlassq = require( '@stdlib/lapack/base/dlassq' );
151+
152+
var X = discreteUniform( 10, -10, 10, {
153+
'dtype': 'float64'
154+
});
155+
console.log( X );
156+
157+
var out = dlassq( X.length, X, 1, 1.0, 0.0 );
158+
console.log( out );
159+
```
160+
161+
</section>
162+
163+
<!-- /.examples -->
164+
165+
<!-- C interface documentation. -->
166+
167+
* * *
168+
169+
<section class="c">
170+
171+
## C APIs
172+
173+
<!-- Section to include introductory text. Make sure to keep an empty line after the intro `section` element and another before the `/section` close. -->
174+
175+
<section class="intro">
176+
177+
</section>
178+
179+
<!-- /.intro -->
180+
181+
<!-- C usage documentation. -->
182+
183+
<section class="usage">
184+
185+
### Usage
186+
187+
```c
188+
TODO
189+
```
190+
191+
#### TODO
192+
193+
TODO.
194+
195+
```c
196+
TODO
197+
```
198+
199+
TODO
200+
201+
```c
202+
TODO
203+
```
204+
205+
</section>
206+
207+
<!-- /.usage -->
208+
209+
<!-- C API usage notes. Make sure to keep an empty line after the `section` element and another before the `/section` close. -->
210+
211+
<section class="notes">
212+
213+
</section>
214+
215+
<!-- /.notes -->
216+
217+
<!-- C API usage examples. -->
218+
219+
<section class="examples">
220+
221+
### Examples
222+
223+
```c
224+
TODO
225+
```
226+
227+
</section>
228+
229+
<!-- /.examples -->
230+
231+
</section>
232+
233+
<!-- /.c -->
234+
235+
<!-- Section for related `stdlib` packages. Do not manually edit this section, as it is automatically populated. -->
236+
237+
<section class="related">
238+
239+
</section>
240+
241+
<!-- /.related -->
242+
243+
<!-- Section for all links. Make sure to keep an empty line after the `section` element and another before the `/section` close. -->
244+
245+
<section class="links">
246+
247+
[lapack]: https://www.netlib.org/lapack/explore-html/
248+
249+
[lapack-dlassq]: https://www.netlib.org/lapack/explore-html/d8/d76/group__lassq_gae8f40b0a34771b4f2d9c863de3af7be5.html#gae8f40b0a34771b4f2d9c863de3af7be5
250+
251+
[mdn-float64array]: https://developer.mozilla.org/en-US/docs/Web/JavaScript/Reference/Global_Objects/Float64Array
252+
253+
[mdn-typed-array]: https://developer.mozilla.org/en-US/docs/Web/JavaScript/Reference/Global_Objects/TypedArray
254+
255+
</section>
256+
257+
<!-- /.links -->
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,96 @@
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 pkg = require( './../package.json' ).name;
28+
var dlassq = require( './../lib/dlassq.js' );
29+
30+
31+
// VARIABLES //
32+
33+
var options = {
34+
'dtype': 'float64'
35+
};
36+
37+
38+
// FUNCTIONS //
39+
40+
/**
41+
* Creates a benchmark function.
42+
*
43+
* @private
44+
* @param {PositiveInteger} len - array length
45+
* @returns {Function} benchmark function
46+
*/
47+
function createBenchmark( len ) {
48+
var x = uniform( len, -100.0, 100.0, options );
49+
return benchmark;
50+
51+
function benchmark( b ) {
52+
var out;
53+
var i;
54+
55+
b.tic();
56+
for ( i = 0; i < b.iterations; i++ ) {
57+
out = dlassq( x.length, x, 1, 1.0, 0.0 );
58+
if ( isnan( out[ 0 ] ) ) {
59+
b.fail( 'should not return NaN' );
60+
}
61+
}
62+
b.toc();
63+
if ( isnan( out[ 1 ] ) ) {
64+
b.fail( 'should not return NaN' );
65+
}
66+
b.pass( 'benchmark finished' );
67+
b.end();
68+
}
69+
}
70+
71+
72+
// MAIN //
73+
74+
/**
75+
* Main execution sequence.
76+
*
77+
* @private
78+
*/
79+
function main() {
80+
var len;
81+
var min;
82+
var max;
83+
var f;
84+
var i;
85+
86+
min = 1; // 10^min
87+
max = 6; // 10^max
88+
89+
for ( i = min; i <= max; i++ ) {
90+
len = pow( 10, i );
91+
f = createBenchmark( len );
92+
bench( pkg+':len='+len, f );
93+
}
94+
}
95+
96+
main();

0 commit comments

Comments
 (0)