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