Skip to content

Commit 79fe4bc

Browse files
feat: add blas/ext/base/drss
--- type: pre_commit_static_analysis_report description: Results of running static analysis checks when committing changes. report: - task: lint_filenames status: passed - task: lint_editorconfig status: passed - task: lint_markdown status: passed - task: lint_package_json status: passed - task: lint_repl_help status: passed - task: lint_javascript_src status: passed - task: lint_javascript_cli status: na - task: lint_javascript_examples status: passed - task: lint_javascript_tests status: passed - task: lint_javascript_benchmarks status: passed - task: lint_python status: na - task: lint_r status: na - task: lint_c_src status: passed - task: lint_c_examples status: passed - task: lint_c_benchmarks status: passed - task: lint_c_tests_fixtures status: na - task: lint_shell status: na - task: lint_typescript_declarations status: passed - task: lint_typescript_tests status: passed - task: lint_license_headers status: passed ---
1 parent b4e82b3 commit 79fe4bc

33 files changed

+3742
-0
lines changed
Lines changed: 343 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,343 @@
1+
<!--
2+
3+
@license Apache-2.0
4+
5+
Copyright (c) 2026 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+
# drss
22+
23+
> Calculate 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 [residual sum of squares][wikipedia-residual-sum-of-squares] (also referred to as the **sum of squared residuals** (SSR) and the **sum of squared errors** (SSE)) is defined as
28+
29+
<!-- <equation class="equation" label="eq:residual_sum_of_squares" align="center" raw="d = \sum_{i=0}^{N-1} (y_i - x_i)^2" alt="Equation for residual sum of squares."> -->
30+
31+
```math
32+
\mathop{\mathrm{RSS}} = \sum_{i=0}^{N-1} (y_i - x_i)^2
33+
```
34+
35+
<!-- <div class="equation" align="center" data-raw-text="d = \sum_{i=0}^{N-1} (y_i - x_i)^2" data-equation="eq:residual_sum_of_squares">
36+
<img src="" alt="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 drss = require( '@stdlib/blas/ext/base/drss' );
52+
```
53+
54+
#### drss( N, x, strideX, y, strideY )
55+
56+
Computes 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 = drss( x.length, x, 1, y, 1 );
65+
// returns 45.0
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 = drss( x.length, x, 1, y, 1 );
85+
// returns 72.0
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 = drss( 4, x1, 2, y1, 2 );
102+
// returns 50.0
103+
```
104+
105+
#### drss.ndarray( N, x, strideX, offsetX, y, strideY, offsetY )
106+
107+
Computes the [residual sum of squares][wikipedia-residual-sum-of-squares] of two double-precision floating-point strided arrays and alternative indexing semantics.
108+
109+
```javascript
110+
var Float64Array = require( '@stdlib/array/float64' );
111+
112+
var x = new Float64Array( [ 1.0, -2.0, 2.0 ] );
113+
var y = new Float64Array( [ 1.0, 1.0, -4.0 ] );
114+
115+
var z = drss.ndarray( x.length, x, 1, 0, y, 1, 0 );
116+
// returns 45.0
117+
```
118+
119+
The function has the following additional parameters:
120+
121+
- **offsetX**: starting index for `x`.
122+
- **offsetY**: starting index for `y`.
123+
124+
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 [residual sum of squares][wikipedia-residual-sum-of-squares] for every other element in `x` and `y` starting from the second element
125+
126+
```javascript
127+
var Float64Array = require( '@stdlib/array/float64' );
128+
129+
var x = new Float64Array( [ 2.0, 1.0, 2.0, -2.0, -2.0, 2.0, 3.0, 4.0, 6.0 ] );
130+
var y = new Float64Array( [ 8.0, -2.0, 3.0, -2.0, 7.0, -2.0, 0.0, -1.0, 4.0 ] );
131+
132+
var z = drss.ndarray( 4, x, 2, 1, y, 2, 1 );
133+
// returns 50.0
134+
```
135+
136+
</section>
137+
138+
<!-- /.usage -->
139+
140+
<section class="notes">
141+
142+
## Notes
143+
144+
- If `N <= 0`, both functions return `0.0`.
145+
146+
</section>
147+
148+
<!-- /.notes -->
149+
150+
<section class="examples">
151+
152+
## Examples
153+
154+
<!-- eslint no-undef: "error" -->
155+
156+
```javascript
157+
var discreteUniform = require( '@stdlib/random/array/discrete-uniform' );
158+
var drss = require( '@stdlib/blas/ext/base/drss' );
159+
160+
var opts = {
161+
'dtype': 'float64'
162+
};
163+
var x = discreteUniform( 10, -50, 50, opts );
164+
console.log( x );
165+
166+
var y = discreteUniform( 10, -50, 50, opts );
167+
console.log( y );
168+
169+
var d = drss( x.length, x, 1, y, 1 );
170+
console.log( d );
171+
```
172+
173+
</section>
174+
175+
<!-- /.examples -->
176+
177+
<!-- C interface documentation. -->
178+
179+
* * *
180+
181+
<section class="c">
182+
183+
## C APIs
184+
185+
<!-- Section to include introductory text. Make sure to keep an empty line after the intro `section` element and another before the `/section` close. -->
186+
187+
<section class="intro">
188+
189+
</section>
190+
191+
<!-- /.intro -->
192+
193+
<!-- C usage documentation. -->
194+
195+
<section class="usage">
196+
197+
### Usage
198+
199+
```c
200+
#include "stdlib/blas/ext/base/drss.h"
201+
```
202+
203+
#### stdlib_strided_drss( N, \*X, strideX, \*Y, strideY )
204+
205+
Computes the [residual sum of squares][wikipedia-residual-sum-of-squares] of two double-precision floating-point strided arrays.
206+
207+
```c
208+
const double x[] = { 1.0, -2.0, 2.0 };
209+
const double y[] = { 1.0, 1.0, -4.0 };
210+
211+
double z = stdlib_strided_drss( 3, x, 1, y, 1 );
212+
// returns 45.0
213+
```
214+
215+
The function accepts the following arguments:
216+
217+
- **N**: `[in] CBLAS_INT` number of indexed elements.
218+
- **X**: `[in] double*` first input array.
219+
- **strideX**: `[in] CBLAS_INT` stride length for `X`.
220+
- **Y**: `[in] double*` second input array.
221+
- **strideY**: `[in] CBLAS_INT` stride length for `Y`.
222+
223+
```c
224+
double stdlib_strided_drss( const CBLAS_INT N, const double *X, const CBLAS_INT strideX, const double *Y, const CBLAS_INT strideY );
225+
```
226+
227+
<!--lint ignore maximum-heading-length-->
228+
229+
#### stdlib_strided_drss_ndarray( N, \*X, strideX, offsetX, \*Y, strideY, offsetY )
230+
231+
<!--lint enable maximum-heading-length-->
232+
233+
Computes the [residual sum of squares][wikipedia-residual-sum-of-squares] of two double-precision floating-point strided arrays and alternative indexing semantics.
234+
235+
```c
236+
const double x[] = { 1.0, -2.0, 2.0 };
237+
const double y[] = { 1.0, 1.0, -4.0 };
238+
239+
double v = stdlib_strided_drss_ndarray( 3, x, 1, 0, 1, 0 );
240+
// returns 45.0
241+
```
242+
243+
The function accepts the following arguments:
244+
245+
- **N**: `[in] CBLAS_INT` number of indexed elements.
246+
- **X**: `[in] double*` first input array.
247+
- **strideX**: `[in] CBLAS_INT` stride length for `X`.
248+
- **offsetX**: `[in] CBLAS_INT` starting index for `X`.
249+
- **Y**: `[in] double*` second input array.
250+
- **strideY**: `[in] CBLAS_INT` stride length for `Y`.
251+
- **offsetY**: `[in] CBLAS_INT` starting index for `Y`.
252+
253+
```c
254+
double stdlib_strided_drss_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 );
255+
```
256+
257+
</section>
258+
259+
<!-- /.usage -->
260+
261+
<!-- C API usage notes. Make sure to keep an empty line after the `section` element and another before the `/section` close. -->
262+
263+
<section class="notes">
264+
265+
</section>
266+
267+
<!-- /.notes -->
268+
269+
<!-- C API usage examples. -->
270+
271+
<section class="examples">
272+
273+
### Examples
274+
275+
```c
276+
#include "stdlib/blas/ext/base/drss.h"
277+
#include <stdio.h>
278+
279+
int main( void ) {
280+
// Create two strided arrays:
281+
const double x[] = { 1.0, -2.0, -4.0, 5.0, 0.0, 3.0 };
282+
const double y[] = { 5.0, 12.0, -8.0, 15.0, 9.0, 0.0 };
283+
284+
// Specify the number of elements:
285+
const int N = 5;
286+
287+
// Specify the stride lengths:
288+
const int strideX = 1;
289+
const int strideY = 1;
290+
291+
// Compute the residual sum of squares of `x` and `y`:
292+
double d = stdlib_strided_drss( N, x, strideX, y, strideY );
293+
294+
// Print the result:
295+
printf( "drss: %lf\n", d );
296+
297+
// Specify index offsets:
298+
const int offsetX = 1;
299+
const int offsetY = 1;
300+
301+
// Compute the residual sum of squares of `x` and `y` with offsets:
302+
d = stdlib_strided_drss_ndarray( N, x, strideX, offsetX, y, strideY, offsetY );
303+
304+
// Print the result:
305+
printf( "drss: %lf\n", d );
306+
}
307+
```
308+
309+
</section>
310+
311+
<!-- /.examples -->
312+
313+
</section>
314+
315+
<!-- /.c -->
316+
317+
<section class="references">
318+
319+
</section>
320+
321+
<!-- /.references -->
322+
323+
<!-- Section for related `stdlib` packages. Do not manually edit this section, as it is automatically populated. -->
324+
325+
<section class="related">
326+
327+
</section>
328+
329+
<!-- /.related -->
330+
331+
<!-- Section for all links. Make sure to keep an empty line after the `section` element and another before the `/section` close. -->
332+
333+
<section class="links">
334+
335+
[wikipedia-residual-sum-of-squares]: https://en.wikipedia.org/wiki/Residual_sum_of_squares
336+
337+
[@stdlib/array/float64]: https://github.com/stdlib-js/stdlib/tree/develop/lib/node_modules/%40stdlib/array/float64
338+
339+
[mdn-typed-array]: https://developer.mozilla.org/en-US/docs/Web/JavaScript/Reference/Global_Objects/TypedArray
340+
341+
</section>
342+
343+
<!-- /.links -->

0 commit comments

Comments
 (0)