Skip to content

Commit 974a2c4

Browse files
feat: add blas/ext/base/drrss
PR-URL: #8556 Co-authored-by: Athan Reines <kgryte@gmail.com> Reviewed-by: Athan Reines <kgryte@gmail.com>
1 parent df65fec commit 974a2c4

33 files changed

+4026
-0
lines changed
Lines changed: 339 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,339 @@
1+
<!--
2+
3+
@license Apache-2.0
4+
5+
Copyright (c) 2025 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+
# drrss
22+
23+
> Calculate the square root of the [residual sum of squares][wikipedia-residual-sum-of-squares] of two double-precision floating-point strided arrays.
24+
25+
<section class="intro">
26+
27+
The square root of the [residual sum of squares][wikipedia-residual-sum-of-squares] is defined as
28+
29+
<!-- <equation class="equation" label="eq:sqrt_of_residual_sum_of_squares" align="center" raw="d = \sqrt{\sum_{i=0}^{N-1} (y_i - x_i)^2}" alt="Equation for the square root of residual sum of squares."> -->
30+
31+
```math
32+
d = \sqrt{\sum_{i=0}^{N-1} (y_i - x_i)^2}
33+
```
34+
35+
<!-- <div class="equation" align="center" data-raw-text="d = \sqrt{\sum_{i=0}^{N-1} (y_i - x_i)^2}" data-equation="eq:sqrt_of_residual_sum_of_squares">
36+
<img src="" alt="Square root of residual sum of squares.">
37+
<br>
38+
</div> -->
39+
40+
<!-- </equation> -->
41+
42+
</section>
43+
44+
<!-- /.intro -->
45+
46+
<section class="usage">
47+
48+
## Usage
49+
50+
```javascript
51+
var drrss = require( '@stdlib/blas/ext/base/drrss' );
52+
```
53+
54+
#### drrss( N, x, strideX, y, strideY )
55+
56+
Computes the square root of the [residual sum of squares][wikipedia-residual-sum-of-squares] of two double-precision floating-point strided arrays.
57+
58+
```javascript
59+
var Float64Array = require( '@stdlib/array/float64' );
60+
61+
var x = new Float64Array( [ 1.0, -2.0, 2.0 ] );
62+
var y = new Float64Array( [ 1.0, 1.0, -4.0 ] );
63+
64+
var z = drrss( x.length, x, 1, y, 1 );
65+
// returns ~6.7
66+
```
67+
68+
The function has the following parameters:
69+
70+
- **N**: number of indexed elements.
71+
- **x**: first input [`Float64Array`][@stdlib/array/float64].
72+
- **strideX**: stride length for `x`.
73+
- **y**: second input [`Float64Array`][@stdlib/array/float64].
74+
- **strideY**: stride length for `y`.
75+
76+
The `N` and stride parameters determine which elements in strided arrays are accessed at runtime. For example, to compute the [residual sum of squares][wikipedia-residual-sum-of-squares] of every other element in `x` and `y`
77+
78+
```javascript
79+
var Float64Array = require( '@stdlib/array/float64' );
80+
81+
var x = new Float64Array( [ 1.0, 2.0, 2.0, -7.0, -2.0, 3.0, 4.0, 2.0 ] );
82+
var y = new Float64Array( [ 2.0, 1.0, 2.0, 1.0, -2.0, 2.0, 3.0, 4.0 ] );
83+
84+
var z = drrss( x.length, x, 1, y, 1 );
85+
// returns ~8.485
86+
```
87+
88+
Note that indexing is relative to the first index. To introduce an offset, use [`typed array`][mdn-typed-array] views.
89+
90+
<!-- eslint-disable stdlib/capitalized-comments -->
91+
92+
```javascript
93+
var Float64Array = require( '@stdlib/array/float64' );
94+
95+
var x0 = new Float64Array( [ 2.0, 1.0, 2.0, -2.0, -2.0, 2.0, 3.0, 4.0 ] );
96+
var y0 = new Float64Array( [ 8.0, -2.0, 3.0, -2.0, 7.0, -2.0, 0.0, -1.0 ] );
97+
98+
var x1 = new Float64Array( x0.buffer, x0.BYTES_PER_ELEMENT*1 ); // start at 2nd element
99+
var y1 = new Float64Array( y0.buffer, y0.BYTES_PER_ELEMENT*1 ); // start at 2nd element
100+
101+
var z = drrss( 4, x1, 2, y1, 2 );
102+
// returns ~7.071
103+
```
104+
105+
If `N` is less than or equal to `0`, the function returns `0`.
106+
107+
#### drrss.ndarray( N, x, strideX, offsetX, y, strideY, offsetY )
108+
109+
Computes the square root of the [residual sum of squares][wikipedia-residual-sum-of-squares] of two double-precision floating-point strided arrays using alternative indexing semantics.
110+
111+
```javascript
112+
var Float64Array = require( '@stdlib/array/float64' );
113+
114+
var x = new Float64Array( [ 1.0, -2.0, 2.0 ] );
115+
var y = new Float64Array( [ 1.0, 1.0, -4.0 ] );
116+
117+
var z = drrss.ndarray( x.length, x, 1, 0, y, 1, 0 );
118+
// returns ~6.7
119+
```
120+
121+
The function has the following additional parameters:
122+
123+
- **offsetX**: starting index for `x`.
124+
- **offsetY**: starting index for `y`.
125+
126+
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, to calculate the square root of the [residual sum of squares][wikipedia-residual-sum-of-squares] for every other element in `x` and `y` starting from the second element
127+
128+
```javascript
129+
var Float64Array = require( '@stdlib/array/float64' );
130+
131+
var x = new Float64Array( [ 2.0, 1.0, 2.0, -2.0, -2.0, 2.0, 3.0, 4.0, 6.0 ] );
132+
var y = new Float64Array( [ 8.0, -2.0, 3.0, -2.0, 7.0, -2.0, 0.0, -1.0, 4.0 ] );
133+
134+
var z = drrss.ndarray( 4, x, 2, 1, y, 2, 1 );
135+
// returns ~7.071
136+
```
137+
138+
</section>
139+
140+
<!-- /.usage -->
141+
142+
<section class="notes">
143+
144+
## Notes
145+
146+
- If `N <= 0`, both functions return `0.0`.
147+
148+
</section>
149+
150+
<!-- /.notes -->
151+
152+
<section class="examples">
153+
154+
## Examples
155+
156+
<!-- eslint no-undef: "error" -->
157+
158+
```javascript
159+
var discreteUniform = require( '@stdlib/random/array/discrete-uniform' );
160+
var drrss = require( '@stdlib/blas/ext/base/drrss' );
161+
162+
var opts = {
163+
'dtype': 'float64'
164+
};
165+
var x = discreteUniform( 10, -50, 50, opts );
166+
console.log( x );
167+
168+
var y = discreteUniform( 10, -50, 50, opts );
169+
console.log( y );
170+
171+
var d = drrss( x.length, x, 1, y, 1 );
172+
console.log( d );
173+
```
174+
175+
</section>
176+
177+
<!-- /.examples -->
178+
179+
<!-- C interface documentation. -->
180+
181+
* * *
182+
183+
<section class="c">
184+
185+
## C APIs
186+
187+
<!-- Section to include introductory text. Make sure to keep an empty line after the intro `section` element and another before the `/section` close. -->
188+
189+
<section class="intro">
190+
191+
</section>
192+
193+
<!-- /.intro -->
194+
195+
<!-- C usage documentation. -->
196+
197+
<section class="usage">
198+
199+
### Usage
200+
201+
```c
202+
#include "stdlib/blas/ext/base/drrss.h"
203+
```
204+
205+
#### stdlib_strided_drrss( N, \*X, strideX, \*Y, strideY )
206+
207+
Computes the square root of the [residual sum of squares][wikipedia-residual-sum-of-squares] of two double-precision floating-point strided arrays.
208+
209+
```c
210+
const double x[] = { 1.0, -2.0, 2.0 };
211+
const double y[] = { 1.0, 1.0, -4.0 };
212+
213+
double z = stdlib_strided_drrss( 3, x, 1, y, 1 );
214+
// returns ~6.7
215+
```
216+
217+
The function accepts the following arguments:
218+
219+
- **N**: `[in] CBLAS_INT` number of indexed elements.
220+
- **X**: `[in] double*` first input array.
221+
- **strideX**: `[in] CBLAS_INT` stride length for `X`.
222+
- **Y**: `[in] double*` second input array.
223+
- **strideY**: `[in] CBLAS_INT` stride length for `Y`.
224+
225+
```c
226+
double stdlib_strided_drrss( const CBLAS_INT N, const double *X, const CBLAS_INT strideX, const double *Y, const CBLAS_INT strideY );
227+
```
228+
229+
<!--lint ignore maximum-heading-length-->
230+
231+
#### stdlib_strided_drrss_ndarray( N, \*X, strideX, offsetX, \*Y, strideY, offsetY )
232+
233+
<!--lint enable maximum-heading-length-->
234+
235+
Computes the square root of the [residual sum of squares][wikipedia-residual-sum-of-squares] of two double-precision floating-point strided arrays using alternative indexing semantics.
236+
237+
```c
238+
const double x[] = { 1.0, -2.0, 2.0 };
239+
const double y[] = { 1.0, 1.0, -4.0 };
240+
241+
double v = stdlib_strided_drrss_ndarray( 3, x, 1, 0, 1, 0 );
242+
// returns ~6.7
243+
```
244+
245+
The function accepts the following arguments:
246+
247+
- **N**: `[in] CBLAS_INT` number of indexed elements.
248+
- **X**: `[in] double*` first input array.
249+
- **strideX**: `[in] CBLAS_INT` stride length for `X`.
250+
- **offsetX**: `[in] CBLAS_INT` starting index for `X`.
251+
- **Y**: `[in] double*` second input array.
252+
- **strideY**: `[in] CBLAS_INT` stride length for `Y`.
253+
- **offsetY**: `[in] CBLAS_INT` starting index for `Y`.
254+
255+
```c
256+
double stdlib_strided_drrss_ndarray( const CBLAS_INT N, const double *X, const CBLAS_INT strideX, const CBLAS_INT offsetX, const double *Y, const CBLAS_INT strideY, const CBLAS_INT offsetY );
257+
```
258+
259+
</section>
260+
261+
<!-- /.usage -->
262+
263+
<!-- C API usage notes. Make sure to keep an empty line after the `section` element and another before the `/section` close. -->
264+
265+
<section class="notes">
266+
267+
</section>
268+
269+
<!-- /.notes -->
270+
271+
<!-- C API usage examples. -->
272+
273+
<section class="examples">
274+
275+
### Examples
276+
277+
```c
278+
#include "stdlib/blas/ext/base/drrss.h"
279+
#include <stdio.h>
280+
281+
int main( void ) {
282+
// Create two strided arrays:
283+
const double x[] = { 1.0, -2.0, -4.0, 5.0, 0.0, 3.0 };
284+
const double y[] = { 5.0, 12.0, -8.0, 15.0, 9.0, 0.0 };
285+
286+
// Specify the number of elements:
287+
const int N = 5;
288+
289+
// Specify the stride lengths:
290+
const int strideX = 1;
291+
const int strideY = 1;
292+
293+
// Compute the square root of the residual sum of squares of `x` and `y`:
294+
double d = stdlib_strided_drrss( N, x, strideX, y, strideY );
295+
296+
// Print the result:
297+
printf( "drrss: %lf\n", d );
298+
299+
// Specify index offsets:
300+
const int offsetX = 1;
301+
const int offsetY = 1;
302+
303+
// Compute the square root of the residual sum of squares of `x` and `y` with offsets:
304+
d = stdlib_strided_drrss_ndarray( N, x, strideX, offsetX, y, strideY, offsetY );
305+
306+
// Print the result:
307+
printf( "drrss: %lf\n", d );
308+
}
309+
```
310+
311+
</section>
312+
313+
<!-- /.examples -->
314+
315+
</section>
316+
317+
<!-- /.c -->
318+
319+
<!-- Section for related `stdlib` packages. Do not manually edit this section, as it is automatically populated. -->
320+
321+
<section class="related">
322+
323+
</section>
324+
325+
<!-- /.related -->
326+
327+
<!-- Section for all links. Make sure to keep an empty line after the `section` element and another before the `/section` close. -->
328+
329+
<section class="links">
330+
331+
[wikipedia-residual-sum-of-squares]: https://en.wikipedia.org/wiki/Residual_sum_of_squares
332+
333+
[@stdlib/array/float64]: https://github.com/stdlib-js/stdlib/tree/develop/lib/node_modules/%40stdlib/array/float64
334+
335+
[mdn-typed-array]: https://developer.mozilla.org/en-US/docs/Web/JavaScript/Reference/Global_Objects/TypedArray
336+
337+
</section>
338+
339+
<!-- /.links -->

0 commit comments

Comments
 (0)