|
| 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 | +# dcityblock |
| 22 | + |
| 23 | +> Compute the city block (Manhattan) distance between two double-precision floating-point strided arrays. |
| 24 | +
|
| 25 | +<section class="intro"> |
| 26 | + |
| 27 | +The [city block distance][wikipedia-taxicab-geometry] (also known as the Manhattan or taxicab distance) is defined as |
| 28 | + |
| 29 | +<!-- <equation class="equation" label="eq:cityblock_distance" align="center" raw="D(\mathbf{A}, \mathbf{B}) = \left\| \mathbf{A} - \mathbf{B} \right\|_{\text{T}} = \sum_{i=0}^{N-1} \left| a_{i} - b_{i} \right|" alt="Equation for city block (Manhattan) distance."> --> |
| 30 | + |
| 31 | +```math |
| 32 | +D(\mathbf{A}, \mathbf{B}) = \left\| \mathbf{A} - \mathbf{B} \right\|_{\text{T}} = \sum_{i=0}^{N-1} \left| a_{i} - b_{i} \right| |
| 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 dcityblock = require( '@stdlib/stats/strided/distances/dcityblock' ); |
| 49 | +``` |
| 50 | + |
| 51 | +#### dcityblock( N, x, strideX, y, strideY ) |
| 52 | + |
| 53 | +Computes the city block (Manhattan) 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 = dcityblock( x.length, x, 1, y, 1 ); |
| 62 | +// returns 26.0 |
| 63 | +``` |
| 64 | + |
| 65 | +The function has the following parameters: |
| 66 | + |
| 67 | +- **N**: number of indexed elements. |
| 68 | +- **x**: input [`Float64Array`][@stdlib/array/float64]. |
| 69 | +- **strideX**: stride length of `x`. |
| 70 | +- **y**: input [`Float64Array`][@stdlib/array/float64]. |
| 71 | +- **strideY**: stride length of `y`. |
| 72 | + |
| 73 | +The `N` and stride parameters determine which elements in the strided arrays are accessed at runtime. For example, to calculate the city block (Manhattan) distance between every other element in `x` and the first `N` elements of `y` in reverse order, |
| 74 | + |
| 75 | +```javascript |
| 76 | +var Float64Array = require( '@stdlib/array/float64' ); |
| 77 | + |
| 78 | +var x = new Float64Array( [ 1.0, 2.0, 3.0, 4.0, 5.0, 6.0 ] ); |
| 79 | +var y = new Float64Array( [ 1.0, 1.0, 1.0, 1.0, 1.0, 1.0 ] ); |
| 80 | + |
| 81 | +var z = dcityblock( 3, x, 2, y, -1 ); |
| 82 | +// returns 6.0 |
| 83 | +``` |
| 84 | + |
| 85 | +Note that indexing is relative to the first index. To introduce an offset, use [`typed array`][mdn-typed-array] views. |
| 86 | + |
| 87 | +<!-- eslint-disable stdlib/capitalized-comments --> |
| 88 | + |
| 89 | +```javascript |
| 90 | +var Float64Array = require( '@stdlib/array/float64' ); |
| 91 | + |
| 92 | +// Initial arrays... |
| 93 | +var x0 = new Float64Array( [ 1.0, 2.0, 3.0, 4.0, 5.0, 6.0 ] ); |
| 94 | +var y0 = new Float64Array( [ 7.0, 8.0, 9.0, 10.0, 11.0, 12.0 ] ); |
| 95 | + |
| 96 | +// Create offset views... |
| 97 | +var x1 = new Float64Array( x0.buffer, x0.BYTES_PER_ELEMENT*1 ); // start at 2nd element |
| 98 | +var y1 = new Float64Array( y0.buffer, y0.BYTES_PER_ELEMENT*3 ); // start at 4th element |
| 99 | + |
| 100 | +var z = dcityblock( 3, x1, 1, y1, 1 ); |
| 101 | +// returns 24.0 |
| 102 | +``` |
| 103 | + |
| 104 | +#### dcityblock.ndarray( N, x, strideX, offsetX, y, strideY, offsetY ) |
| 105 | + |
| 106 | +Computes the city block (Manhattan) distance between two double-precision floating-point strided arrays using alternative indexing semantics. |
| 107 | + |
| 108 | +```javascript |
| 109 | +var Float64Array = require( '@stdlib/array/float64' ); |
| 110 | + |
| 111 | +var x = new Float64Array( [ 4.0, 2.0, -3.0, 5.0, -1.0 ] ); |
| 112 | +var y = new Float64Array( [ 2.0, 6.0, -1.0, -4.0, 8.0 ] ); |
| 113 | + |
| 114 | +var z = dcityblock.ndarray( x.length, x, 1, 0, y, 1, 0 ); |
| 115 | +// returns 26.0 |
| 116 | +``` |
| 117 | + |
| 118 | +The function has the following additional parameters: |
| 119 | + |
| 120 | +- **offsetX**: starting index for `x`. |
| 121 | +- **offsetY**: starting index for `y`. |
| 122 | + |
| 123 | +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 city block (Manhattan) distance between every other element in `x` starting from the second element with the last 3 elements in `y` in reverse order |
| 124 | + |
| 125 | +```javascript |
| 126 | +var Float64Array = require( '@stdlib/array/float64' ); |
| 127 | + |
| 128 | +var x = new Float64Array( [ 1.0, 2.0, 3.0, 4.0, 5.0, 6.0 ] ); |
| 129 | +var y = new Float64Array( [ 7.0, 8.0, 9.0, 10.0, 11.0, 12.0 ] ); |
| 130 | + |
| 131 | +var z = dcityblock.ndarray( 3, x, 2, 1, y, -1, y.length-1 ); |
| 132 | +// returns 21.0 |
| 133 | +``` |
| 134 | + |
| 135 | +</section> |
| 136 | + |
| 137 | +<!-- /.usage --> |
| 138 | + |
| 139 | +<section class="notes"> |
| 140 | + |
| 141 | +## Notes |
| 142 | + |
| 143 | +- If `N <= 0`, both functions return `NaN`. |
| 144 | + |
| 145 | +</section> |
| 146 | + |
| 147 | +<!-- /.notes --> |
| 148 | + |
| 149 | +<section class="examples"> |
| 150 | + |
| 151 | +## Examples |
| 152 | + |
| 153 | +<!-- eslint no-undef: "error" --> |
| 154 | + |
| 155 | +```javascript |
| 156 | +var discreteUniform = require( '@stdlib/random/array/discrete-uniform' ); |
| 157 | +var dcityblock = require( '@stdlib/stats/strided/distances/dcityblock' ); |
| 158 | + |
| 159 | +var opts = { |
| 160 | + 'dtype': 'float64' |
| 161 | +}; |
| 162 | +var x = discreteUniform( 10, 0, 100, opts ); |
| 163 | +console.log( x ); |
| 164 | + |
| 165 | +var y = discreteUniform( x.length, 0, 10, opts ); |
| 166 | +console.log( y ); |
| 167 | + |
| 168 | +var out = dcityblock.ndarray( x.length, x, 1, 0, y, -1, y.length-1 ); |
| 169 | +console.log( out ); |
| 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/stats/strided/distances/dcityblock.h" |
| 200 | +``` |
| 201 | + |
| 202 | +#### stdlib_strided_dcityblock( N, \*X, strideX, \*Y, strideY ) |
| 203 | + |
| 204 | +Computes the city block (Manhattan) distance between two double-precision floating-point strided arrays. |
| 205 | + |
| 206 | +```c |
| 207 | +const double x[] = { 4.0, 2.0, -3.0, 5.0, -1.0 }; |
| 208 | +const double y[] = { 2.0, 6.0, -1.0, -4.0, 8.0 }; |
| 209 | + |
| 210 | +double v = stdlib_strided_dcityblock( 5, x, 1, y, 1 ); |
| 211 | +// returns 26.0 |
| 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` stride length of `X`. |
| 219 | +- **Y**: `[in] double*` second input array. |
| 220 | +- **strideY**: `[in] CBLAS_INT` stride length of `Y`. |
| 221 | +
|
| 222 | +```c |
| 223 | +double stdlib_strided_dcityblock( const CBLAS_INT N, const double *X, const CBLAS_INT strideX, const double *Y, const CBLAS_INT strideY ); |
| 224 | +``` |
| 225 | + |
| 226 | +<!--lint disable maximum-heading-length--> |
| 227 | + |
| 228 | +#### stdlib_strided_dcityblock_ndarray( N, \*X, strideX, offsetX, \*Y, strideY, offsetY ) |
| 229 | + |
| 230 | +<!--lint enable maximum-heading-length--> |
| 231 | + |
| 232 | +Computes the city block (Manhattan) distance between two double-precision floating-point strided arrays using alternative indexing semantics. |
| 233 | + |
| 234 | +```c |
| 235 | +const double x[] = { 4.0, 2.0, -3.0, 5.0, -1.0 }; |
| 236 | +const double y[] = { 2.0, 6.0, -1.0, -4.0, 8.0 }; |
| 237 | + |
| 238 | +double v = stdlib_strided_dcityblock_ndarray( 5, x, -1, 4, y, -1, 4 ); |
| 239 | +// returns 26.0 |
| 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` stride length of `X`. |
| 247 | +- **offsetX**: `[in] CBLAS_INT` starting index for `X`. |
| 248 | +- **Y**: `[in] double*` second input array. |
| 249 | +- **strideY**: `[in] CBLAS_INT` stride length of `Y`. |
| 250 | +- **offsetY**: `[in] CBLAS_INT` starting index for `Y`. |
| 251 | +
|
| 252 | +```c |
| 253 | +double stdlib_strided_dcityblock_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/stats/strided/distances/dcityblock.h" |
| 276 | +#include <stdio.h> |
| 277 | + |
| 278 | +int main( void ) { |
| 279 | + // Create strided arrays: |
| 280 | + const double x[] = { 1.0, -2.0, 3.0, -4.0, 5.0, -6.0, 7.0, -8.0 }; |
| 281 | + const double y[] = { 1.0, -2.0, 3.0, -4.0, 5.0, -6.0, 7.0, -8.0 }; |
| 282 | + |
| 283 | + // Specify the number of elements: |
| 284 | + const int N = 8; |
| 285 | + |
| 286 | + // Specify strides: |
| 287 | + const int strideX = 1; |
| 288 | + const int strideY = -1; |
| 289 | + |
| 290 | + // Compute the city block (Manhattan) distance between `x` and `y`: |
| 291 | + double d = stdlib_strided_dcityblock( N, x, strideX, y, strideY ); |
| 292 | + |
| 293 | + // Print the result: |
| 294 | + printf( "City block (Manhattan) distance: %lf\n", d ); |
| 295 | + |
| 296 | + // Compute the city block (Manhattan) distance between `x` and `y` with offsets: |
| 297 | + d = stdlib_strided_dcityblock_ndarray( N, x, strideX, 0, y, strideY, N-1 ); |
| 298 | + |
| 299 | + // Print the result: |
| 300 | + printf( "City block (Manhattan) distance: %lf\n", d ); |
| 301 | +} |
| 302 | +``` |
| 303 | +
|
| 304 | +</section> |
| 305 | +
|
| 306 | +<!-- /.examples --> |
| 307 | +
|
| 308 | +</section> |
| 309 | +
|
| 310 | +<!-- /.c --> |
| 311 | +
|
| 312 | +<!-- Section for related `stdlib` packages. Do not manually edit this section, as it is automatically populated. --> |
| 313 | +
|
| 314 | +<section class="related"> |
| 315 | +
|
| 316 | +</section> |
| 317 | +
|
| 318 | +<!-- /.related --> |
| 319 | +
|
| 320 | +<!-- Section for all links. Make sure to keep an empty line after the `section` element and another before the `/section` close. --> |
| 321 | +
|
| 322 | +<section class="links"> |
| 323 | +
|
| 324 | +[@stdlib/array/float64]: https://github.com/stdlib-js/stdlib/tree/develop/lib/node_modules/%40stdlib/array/float64 |
| 325 | +
|
| 326 | +[mdn-typed-array]: https://developer.mozilla.org/en-US/docs/Web/JavaScript/Reference/Global_Objects/TypedArray |
| 327 | +
|
| 328 | +[wikipedia-taxicab-geometry]: https://en.wikipedia.org/wiki/Taxicab_geometry |
| 329 | +
|
| 330 | +<!-- <related-links> --> |
| 331 | +
|
| 332 | +<!-- </related-links> --> |
| 333 | +
|
| 334 | +</section> |
| 335 | +
|
| 336 | +<!-- /.links --> |
0 commit comments