|
| 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 | +# dminkowski |
| 22 | + |
| 23 | +> Compute the Minkowski distance between two double-precision floating-point strided arrays. |
| 24 | +
|
| 25 | +<section class="intro"> |
| 26 | + |
| 27 | +The [minkowski distance][wikipedia-minkowski-distance] is defined as |
| 28 | + |
| 29 | +<!-- <equation class="equation" label="eq:minkowski_distance" align="center" raw="D(A,B) = \left( \sum_{i=0}^{N-1} \left| a_i - b_i \right|^p \right)^{\frac{1}{p}}" alt="Equation for the Minkowski distance."> --> |
| 30 | + |
| 31 | +```math |
| 32 | +D(A,B) = \left( \sum_{i=0}^{N-1} \left| a_i - b_i \right|^p \right)^{\frac{1}{p}} |
| 33 | +``` |
| 34 | + |
| 35 | +<!-- </equation> --> |
| 36 | + |
| 37 | +where `a_i` and `b_i` are the _ith_ components of vectors **A** and **B**, respectively. |
| 38 | + |
| 39 | +</section> |
| 40 | + |
| 41 | +<!-- /.intro --> |
| 42 | + |
| 43 | +<section class="usage"> |
| 44 | + |
| 45 | +## Usage |
| 46 | + |
| 47 | +```javascript |
| 48 | +var dminkowski = require( '@stdlib/stats/strided/distances/dminkowski' ); |
| 49 | +``` |
| 50 | + |
| 51 | +#### dminkowski( N, p, x, strideX, y, strideY ) |
| 52 | + |
| 53 | +Computes the Minkowski distance between two double-precision floating-point strided arrays. |
| 54 | + |
| 55 | +```javascript |
| 56 | +var Float64Array = require( '@stdlib/array/float64' ); |
| 57 | + |
| 58 | +var x = new Float64Array( [ 4.0, 2.0, -3.0, 5.0, -1.0 ] ); |
| 59 | +var y = new Float64Array( [ 2.0, 6.0, -1.0, -4.0, 8.0 ] ); |
| 60 | + |
| 61 | +var z = dminkowski( x.length, 3, x, 1, y, 1 ); |
| 62 | +// returns ~11.543 |
| 63 | +``` |
| 64 | + |
| 65 | +The function has the following parameters: |
| 66 | + |
| 67 | +- **N**: number of indexed elements. |
| 68 | +- **p**: order of the Minkowski norm. |
| 69 | +- **x**: input [`Float64Array`][@stdlib/array/float64]. |
| 70 | +- **strideX**: stride length of `x`. |
| 71 | +- **y**: input [`Float64Array`][@stdlib/array/float64]. |
| 72 | +- **strideY**: stride length of `y`. |
| 73 | + |
| 74 | +The `N` and stride parameters determine which elements in the strided arrays are accessed at runtime. For example, to calculate the Minkowski distance between every other element in `x` and the first `N` elements of `y` in reverse order, |
| 75 | + |
| 76 | +```javascript |
| 77 | +var Float64Array = require( '@stdlib/array/float64' ); |
| 78 | + |
| 79 | +var x = new Float64Array( [ 1.0, 2.0, 3.0, 4.0, 5.0, 6.0 ] ); |
| 80 | +var y = new Float64Array( [ 1.0, 1.0, 1.0, 1.0, 1.0, 1.0 ] ); |
| 81 | + |
| 82 | +var z = dminkowski( 3, 3, x, 2, y, -1 ); |
| 83 | +// returns ~4.16 |
| 84 | +``` |
| 85 | + |
| 86 | +Note that indexing is relative to the first index. To introduce an offset, 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 | +// Initial arrays... |
| 94 | +var x0 = new Float64Array( [ 1.0, 2.0, 3.0, 4.0, 5.0, 6.0 ] ); |
| 95 | +var y0 = new Float64Array( [ 7.0, 8.0, 9.0, 10.0, 11.0, 12.0 ] ); |
| 96 | + |
| 97 | +// Create offset views... |
| 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*3 ); // start at 4th element |
| 100 | + |
| 101 | +var z = dminkowski( 3, 3, x1, 1, y1, 1 ); |
| 102 | +// returns ~11.538 |
| 103 | +``` |
| 104 | + |
| 105 | +#### dminkowski.ndarray( N, p, x, strideX, offsetX, y, strideY, offsetY ) |
| 106 | + |
| 107 | +Computes the Minkowski distance between two double-precision floating-point strided arrays using alternative indexing semantics. |
| 108 | + |
| 109 | +```javascript |
| 110 | +var Float64Array = require( '@stdlib/array/float64' ); |
| 111 | + |
| 112 | +var x = new Float64Array( [ 4.0, 2.0, -3.0, 5.0, -1.0 ] ); |
| 113 | +var y = new Float64Array( [ 2.0, 6.0, -1.0, -4.0, 8.0 ] ); |
| 114 | + |
| 115 | +var z = dminkowski.ndarray( x.length, 3, x, 1, 0, y, 1, 0 ); |
| 116 | +// returns ~11.543 |
| 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 Minkowski distance between every other element in `x` starting from the second element with the last 3 elements in `y` in reverse order |
| 125 | + |
| 126 | +```javascript |
| 127 | +var Float64Array = require( '@stdlib/array/float64' ); |
| 128 | + |
| 129 | +var x = new Float64Array( [ 1.0, 2.0, 3.0, 4.0, 5.0, 6.0 ] ); |
| 130 | +var y = new Float64Array( [ 7.0, 8.0, 9.0, 10.0, 11.0, 12.0 ] ); |
| 131 | + |
| 132 | +var z = dminkowski.ndarray( 3, 3, x, 2, 1, y, -1, y.length-1 ); |
| 133 | +// returns ~11.206 |
| 134 | +``` |
| 135 | + |
| 136 | +</section> |
| 137 | + |
| 138 | +<!-- /.usage --> |
| 139 | + |
| 140 | +<section class="notes"> |
| 141 | + |
| 142 | +## Notes |
| 143 | + |
| 144 | +- If `N <= 0`, both functions return `NaN`. |
| 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 dminkowski = require( '@stdlib/stats/strided/distances/dminkowski' ); |
| 159 | + |
| 160 | +var opts = { |
| 161 | + 'dtype': 'float64' |
| 162 | +}; |
| 163 | +var x = discreteUniform( 10, 0, 100, opts ); |
| 164 | +console.log( x ); |
| 165 | + |
| 166 | +var y = discreteUniform( x.length, 0, 10, opts ); |
| 167 | +console.log( y ); |
| 168 | + |
| 169 | +var out = dminkowski.ndarray( x.length, 3, x, 1, 0, y, -1, y.length-1 ); |
| 170 | +console.log( out ); |
| 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/stats/strided/distances/dminkowski.h" |
| 201 | +``` |
| 202 | + |
| 203 | +#### stdlib_strided_dminkowski( N, p, \*X, strideX, \*Y, strideY ) |
| 204 | + |
| 205 | +Computes the Minkowski distance between two double-precision floating-point strided arrays. |
| 206 | + |
| 207 | +```c |
| 208 | +const double x[] = { 4.0, 2.0, -3.0, 5.0, -1.0 }; |
| 209 | +const double y[] = { 2.0, 6.0, -1.0, -4.0, 8.0 }; |
| 210 | + |
| 211 | +double v = stdlib_strided_dminkowski( 5, 3, x, 1, y, 1 ); |
| 212 | +// returns ~11.543 |
| 213 | +``` |
| 214 | +
|
| 215 | +The function accepts the following arguments: |
| 216 | +
|
| 217 | +- **N**: `[in] CBLAS_INT` number of indexed elements. |
| 218 | +- **p**: `[in] double` order of the Minkowski norm. |
| 219 | +- **X**: `[in] double*` first input array. |
| 220 | +- **strideX**: `[in] CBLAS_INT` stride length of `X`. |
| 221 | +- **Y**: `[in] double*` second input array. |
| 222 | +- **strideY**: `[in] CBLAS_INT` stride length of `Y`. |
| 223 | +
|
| 224 | +```c |
| 225 | +double stdlib_strided_dminkowski( const CBLAS_INT N, const double p, const double *X, const CBLAS_INT strideX, const double *Y, const CBLAS_INT strideY ); |
| 226 | +``` |
| 227 | + |
| 228 | +<!--lint disable maximum-heading-length--> |
| 229 | + |
| 230 | +#### stdlib_strided_dminkowski_ndarray( N, p, \*X, strideX, offsetX, \*Y, strideY, offsetY ) |
| 231 | + |
| 232 | +<!--lint enable maximum-heading-length--> |
| 233 | + |
| 234 | +Computes the Minkowski distance between two double-precision floating-point strided arrays using alternative indexing semantics. |
| 235 | + |
| 236 | +```c |
| 237 | +const double x[] = { 4.0, 2.0, -3.0, 5.0, -1.0 }; |
| 238 | +const double y[] = { 2.0, 6.0, -1.0, -4.0, 8.0 }; |
| 239 | + |
| 240 | +double v = stdlib_strided_dminkowski_ndarray( 5, 3, x, -1, 4, y, -1, 4 ); |
| 241 | +// returns ~11.543 |
| 242 | +``` |
| 243 | +
|
| 244 | +The function accepts the following arguments: |
| 245 | +
|
| 246 | +- **N**: `[in] CBLAS_INT` number of indexed elements. |
| 247 | +- **p**: `[in] double` order of the Minkowski norm. |
| 248 | +- **X**: `[in] double*` first input array. |
| 249 | +- **strideX**: `[in] CBLAS_INT` stride length of `X`. |
| 250 | +- **offsetX**: `[in] CBLAS_INT` starting index for `X`. |
| 251 | +- **Y**: `[in] double*` second input array. |
| 252 | +- **strideY**: `[in] CBLAS_INT` stride length of `Y`. |
| 253 | +- **offsetY**: `[in] CBLAS_INT` starting index for `Y`. |
| 254 | +
|
| 255 | +```c |
| 256 | +double stdlib_strided_dminkowski_ndarray( const CBLAS_INT N, const double p, 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/stats/strided/distances/dminkowski.h" |
| 279 | +#include <stdio.h> |
| 280 | + |
| 281 | +int main( void ) { |
| 282 | + // Create strided arrays: |
| 283 | + const double x[] = { 1.0, -2.0, 3.0, -4.0, 5.0, -6.0, 7.0, -8.0 }; |
| 284 | + const double y[] = { 1.0, -2.0, 3.0, -4.0, 5.0, -6.0, 7.0, -8.0 }; |
| 285 | + |
| 286 | + // Specify the number of elements: |
| 287 | + const int N = 8; |
| 288 | + |
| 289 | + // Specify the order of the Minkowski norm: |
| 290 | + const double p = 3.0; |
| 291 | + |
| 292 | + // Specify strides: |
| 293 | + const int strideX = 1; |
| 294 | + const int strideY = -1; |
| 295 | + |
| 296 | + // Compute the Minkowski distance between `x` and `y`: |
| 297 | + double d = stdlib_strided_dminkowski( N, p, x, strideX, y, strideY ); |
| 298 | + |
| 299 | + // Print the result: |
| 300 | + printf( "Minkowski distance: %lf\n", d ); |
| 301 | + |
| 302 | + // Compute the Minkowski distance between `x` and `y` with offsets: |
| 303 | + d = stdlib_strided_dminkowski_ndarray( N, p, x, strideX, 0, y, strideY, N-1 ); |
| 304 | + |
| 305 | + // Print the result: |
| 306 | + printf( "Minkowski distance: %lf\n", d ); |
| 307 | +} |
| 308 | +``` |
| 309 | +
|
| 310 | +</section> |
| 311 | +
|
| 312 | +<!-- /.examples --> |
| 313 | +
|
| 314 | +</section> |
| 315 | +
|
| 316 | +<!-- /.c --> |
| 317 | +
|
| 318 | +<!-- Section for related `stdlib` packages. Do not manually edit this section, as it is automatically populated. --> |
| 319 | +
|
| 320 | +<section class="related"> |
| 321 | +
|
| 322 | +</section> |
| 323 | +
|
| 324 | +<!-- /.related --> |
| 325 | +
|
| 326 | +<!-- Section for all links. Make sure to keep an empty line after the `section` element and another before the `/section` close. --> |
| 327 | +
|
| 328 | +<section class="links"> |
| 329 | +
|
| 330 | +[@stdlib/array/float64]: https://github.com/stdlib-js/stdlib/tree/develop/lib/node_modules/%40stdlib/array/float64 |
| 331 | +
|
| 332 | +[mdn-typed-array]: https://developer.mozilla.org/en-US/docs/Web/JavaScript/Reference/Global_Objects/TypedArray |
| 333 | +
|
| 334 | +[wikipedia-minkowski-distance]: https://en.wikipedia.org/wiki/Minkowski_distance |
| 335 | +
|
| 336 | +<!-- <related-links> --> |
| 337 | +
|
| 338 | +<!-- </related-links> --> |
| 339 | +
|
| 340 | +</section> |
| 341 | +
|
| 342 | +<!-- /.links --> |
0 commit comments