|
| 1 | +<!-- |
| 2 | +
|
| 3 | +@license Apache-2.0 |
| 4 | +
|
| 5 | +Copyright (c) 2020 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 | +# smediansorted |
| 22 | + |
| 23 | +> Calculate the median value of a sorted single-precision floating-point strided array. |
| 24 | +
|
| 25 | +<section class="intro"> |
| 26 | + |
| 27 | +</section> |
| 28 | + |
| 29 | +<!-- /.intro --> |
| 30 | + |
| 31 | +<section class="usage"> |
| 32 | + |
| 33 | +## Usage |
| 34 | + |
| 35 | +```javascript |
| 36 | +var smediansorted = require( '@stdlib/stats/strided/smediansorted' ); |
| 37 | +``` |
| 38 | + |
| 39 | +#### smediansorted( N, x, strideX ) |
| 40 | + |
| 41 | +Computes the median value of a sorted single-precision floating-point strided array `x`. |
| 42 | + |
| 43 | +```javascript |
| 44 | +var Float32Array = require( '@stdlib/array/float32' ); |
| 45 | + |
| 46 | +var x = new Float32Array( [ 1.0, 2.0, 3.0 ] ); |
| 47 | +var v = smediansorted( x.length, x, 1 ); |
| 48 | +// returns 2.0 |
| 49 | + |
| 50 | +x = new Float32Array( [ 3.0, 2.0, 1.0 ] ); |
| 51 | +v = smediansorted( x.length, x, 1 ); |
| 52 | +// returns 2.0 |
| 53 | +``` |
| 54 | + |
| 55 | +The function has the following parameters: |
| 56 | + |
| 57 | +- **N**: number of indexed elements. |
| 58 | +- **x**: sorted input [`Float32Array`][@stdlib/array/float32]. |
| 59 | +- **strideX**: stride length for `x`. |
| 60 | + |
| 61 | +The `N` and stride parameters determine which elements in the strided array are accessed at runtime. For example, to compute the median value of every other element in `x`, |
| 62 | + |
| 63 | +```javascript |
| 64 | +var Float32Array = require( '@stdlib/array/float32' ); |
| 65 | + |
| 66 | +var x = new Float32Array( [ 1.0, 2.0, 2.0, -7.0, 3.0, 3.0, 4.0, 2.0 ] ); |
| 67 | + |
| 68 | +var v = smediansorted( 4, x, 2 ); |
| 69 | +// returns 2.5 |
| 70 | +``` |
| 71 | + |
| 72 | +Note that indexing is relative to the first index. To introduce an offset, use [`typed array`][mdn-typed-array] views. |
| 73 | + |
| 74 | +<!-- eslint-disable stdlib/capitalized-comments --> |
| 75 | + |
| 76 | +```javascript |
| 77 | +var Float32Array = require( '@stdlib/array/float32' ); |
| 78 | + |
| 79 | +var x0 = new Float32Array( [ 2.0, 1.0, 2.0, 2.0, -2.0, 2.0, 3.0, 4.0 ] ); |
| 80 | +var x1 = new Float32Array( x0.buffer, x0.BYTES_PER_ELEMENT*1 ); // start at 2nd element |
| 81 | + |
| 82 | +var v = smediansorted( 4, x1, 2 ); |
| 83 | +// returns 2.0 |
| 84 | +``` |
| 85 | + |
| 86 | +#### smediansorted.ndarray( N, x, strideX, offsetX ) |
| 87 | + |
| 88 | +Computes the median value of a sorted single-precision floating-point strided array using alternative indexing semantics. |
| 89 | + |
| 90 | +```javascript |
| 91 | +var Float32Array = require( '@stdlib/array/float32' ); |
| 92 | + |
| 93 | +var x = new Float32Array( [ 1.0, 2.0, 3.0 ] ); |
| 94 | + |
| 95 | +var v = smediansorted.ndarray( x.length, x, 1, 0 ); |
| 96 | +// returns 2.0 |
| 97 | +``` |
| 98 | + |
| 99 | +The function has the following additional parameters: |
| 100 | + |
| 101 | +- **offset**: starting index for `x`. |
| 102 | + |
| 103 | +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 median value for every other element in `x` starting from the second element |
| 104 | + |
| 105 | +```javascript |
| 106 | +var Float32Array = require( '@stdlib/array/float32' ); |
| 107 | + |
| 108 | +var x = new Float32Array( [ 2.0, 1.0, 2.0, 2.0, -2.0, 2.0, 3.0, 4.0 ] ); |
| 109 | + |
| 110 | +var v = smediansorted.ndarray( 4, x, 2, 1 ); |
| 111 | +// returns 2.0 |
| 112 | +``` |
| 113 | + |
| 114 | +</section> |
| 115 | + |
| 116 | +<!-- /.usage --> |
| 117 | + |
| 118 | +<section class="notes"> |
| 119 | + |
| 120 | +## Notes |
| 121 | + |
| 122 | +- If `N <= 0`, both functions return `NaN`. |
| 123 | +- The input strided array must be sorted in either **strictly** ascending or descending order. |
| 124 | + |
| 125 | +</section> |
| 126 | + |
| 127 | +<!-- /.notes --> |
| 128 | + |
| 129 | +<section class="examples"> |
| 130 | + |
| 131 | +## Examples |
| 132 | + |
| 133 | +<!-- eslint no-undef: "error" --> |
| 134 | + |
| 135 | +```javascript |
| 136 | +var linspace = require( '@stdlib/array/linspace' ); |
| 137 | +var smediansorted = require( '@stdlib/stats/strided/smediansorted' ); |
| 138 | + |
| 139 | +var options = { |
| 140 | + 'dtype': 'float32' |
| 141 | +}; |
| 142 | +var x = linspace( -5.0, 5.0, 10, options ); |
| 143 | +console.log( x ); |
| 144 | + |
| 145 | +var v = smediansorted( x.length, x, 1 ); |
| 146 | +console.log( v ); |
| 147 | +``` |
| 148 | + |
| 149 | +</section> |
| 150 | + |
| 151 | +<!-- /.examples --> |
| 152 | + |
| 153 | +<!-- C interface documentation. --> |
| 154 | + |
| 155 | +* * * |
| 156 | + |
| 157 | +<section class="c"> |
| 158 | + |
| 159 | +## C APIs |
| 160 | + |
| 161 | +<!-- Section to include introductory text. Make sure to keep an empty line after the intro `section` element and another before the `/section` close. --> |
| 162 | + |
| 163 | +<section class="intro"> |
| 164 | + |
| 165 | +</section> |
| 166 | + |
| 167 | +<!-- /.intro --> |
| 168 | + |
| 169 | +<!-- C usage documentation. --> |
| 170 | + |
| 171 | +<section class="usage"> |
| 172 | + |
| 173 | +### Usage |
| 174 | + |
| 175 | +```c |
| 176 | +#include "stdlib/stats/strided/smediansorted.h" |
| 177 | +``` |
| 178 | + |
| 179 | +#### stdlib_strided_smediansorted( N, \*X, strideX ) |
| 180 | + |
| 181 | +Computes the median value of a sorted single-precision floating-point strided array. |
| 182 | + |
| 183 | +```c |
| 184 | +const float x[] = { 1.0f, 2.0f, 3.0f }; |
| 185 | + |
| 186 | +float v = stdlib_strided_smediansorted( 3, x, 1 ); |
| 187 | +// returns 2.0f |
| 188 | +``` |
| 189 | +
|
| 190 | +The function accepts the following arguments: |
| 191 | +
|
| 192 | +- **N**: `[in] CBLAS_INT` number of indexed elements. |
| 193 | +- **X**: `[in] float*` input array. |
| 194 | +- **strideX**: `[in] CBLAS_INT` stride length for `X`. |
| 195 | +
|
| 196 | +```c |
| 197 | +float stdlib_strided_smediansorted( const CBLAS_INT N, const float *X, const CBLAS_INT strideX ); |
| 198 | +``` |
| 199 | + |
| 200 | +#### stdlib_strided_smediansorted_ndarray( N, \*X, strideX, offsetX ) |
| 201 | + |
| 202 | +Computes the median value of a sorted single-precision floating-point strided array using alternative indexing semantics. |
| 203 | + |
| 204 | +```c |
| 205 | +const float x[] = { 1.0f, 2.0f, 3.0f }; |
| 206 | + |
| 207 | +float v = stdlib_strided_smediansorted_ndarray( 3, x, 1, 0 ); |
| 208 | +// returns 2.0f |
| 209 | +``` |
| 210 | +
|
| 211 | +The function accepts the following arguments: |
| 212 | +
|
| 213 | +- **N**: `[in] CBLAS_INT` number of indexed elements. |
| 214 | +- **X**: `[in] float*` input array. |
| 215 | +- **strideX**: `[in] CBLAS_INT` stride length for `X`. |
| 216 | +- **offsetX**: `[in] CBLAS_INT` starting index for `X`. |
| 217 | +
|
| 218 | +```c |
| 219 | +float stdlib_strided_smediansorted_ndarray( const CBLAS_INT N, const float *X, const CBLAS_INT strideX, const CBLAS_INT offsetX ); |
| 220 | +``` |
| 221 | + |
| 222 | +</section> |
| 223 | + |
| 224 | +<!-- /.usage --> |
| 225 | + |
| 226 | +<!-- C API usage notes. Make sure to keep an empty line after the `section` element and another before the `/section` close. --> |
| 227 | + |
| 228 | +<section class="notes"> |
| 229 | + |
| 230 | +</section> |
| 231 | + |
| 232 | +<!-- /.notes --> |
| 233 | + |
| 234 | +<!-- C API usage examples. --> |
| 235 | + |
| 236 | +<section class="examples"> |
| 237 | + |
| 238 | +### Examples |
| 239 | + |
| 240 | +```c |
| 241 | +#include "stdlib/stats/strided/smediansorted.h" |
| 242 | +#include <stdio.h> |
| 243 | + |
| 244 | +int main( void ) { |
| 245 | + // Create a strided array: |
| 246 | + const float x[] = { 1.0f, 2.0f, 3.0f, 4.0f, 5.0f, 6.0f, 7.0f, 8.0f }; |
| 247 | + |
| 248 | + // Specify the number of elements: |
| 249 | + const int N = 4; |
| 250 | + |
| 251 | + // Specify the stride length: |
| 252 | + const int strideX = 2; |
| 253 | + |
| 254 | + // Compute the median value: |
| 255 | + float v = stdlib_strided_smediansorted( N, x, strideX ); |
| 256 | + |
| 257 | + // Print the result: |
| 258 | + printf( "median: %f\n", v ); |
| 259 | +} |
| 260 | +``` |
| 261 | +
|
| 262 | +</section> |
| 263 | +
|
| 264 | +<!-- /.examples --> |
| 265 | +
|
| 266 | +</section> |
| 267 | +
|
| 268 | +<!-- /.c --> |
| 269 | +
|
| 270 | +<!-- Section for related `stdlib` packages. Do not manually edit this section, as it is automatically populated. --> |
| 271 | +
|
| 272 | +<section class="related"> |
| 273 | +
|
| 274 | +* * * |
| 275 | +
|
| 276 | +## See Also |
| 277 | +
|
| 278 | +- <span class="package-name">[`@stdlib/stats/strided/dmediansorted`][@stdlib/stats/strided/dmediansorted]</span><span class="delimiter">: </span><span class="description">calculate the median value of a sorted double-precision floating-point strided array.</span> |
| 279 | +- <span class="package-name">[`@stdlib/stats/base/mediansorted`][@stdlib/stats/base/mediansorted]</span><span class="delimiter">: </span><span class="description">calculate the median value of a sorted strided array.</span> |
| 280 | +- <span class="package-name">[`@stdlib/stats/base/smean`][@stdlib/stats/base/smean]</span><span class="delimiter">: </span><span class="description">calculate the arithmetic mean of a single-precision floating-point strided array.</span> |
| 281 | +
|
| 282 | +</section> |
| 283 | +
|
| 284 | +<!-- /.related --> |
| 285 | +
|
| 286 | +<!-- Section for all links. Make sure to keep an empty line after the `section` element and another before the `/section` close. --> |
| 287 | +
|
| 288 | +<section class="links"> |
| 289 | +
|
| 290 | +[@stdlib/array/float32]: https://github.com/stdlib-js/stdlib/tree/develop/lib/node_modules/%40stdlib/array/float32 |
| 291 | +
|
| 292 | +[mdn-typed-array]: https://developer.mozilla.org/en-US/docs/Web/JavaScript/Reference/Global_Objects/TypedArray |
| 293 | +
|
| 294 | +<!-- <related-links> --> |
| 295 | +
|
| 296 | +[@stdlib/stats/strided/dmediansorted]: https://github.com/stdlib-js/stdlib/tree/develop/lib/node_modules/%40stdlib/stats/strided/dmediansorted |
| 297 | +
|
| 298 | +[@stdlib/stats/base/mediansorted]: https://github.com/stdlib-js/stdlib/tree/develop/lib/node_modules/%40stdlib/stats/base/mediansorted |
| 299 | +
|
| 300 | +[@stdlib/stats/base/smean]: https://github.com/stdlib-js/stdlib/tree/develop/lib/node_modules/%40stdlib/stats/base/smean |
| 301 | +
|
| 302 | +<!-- </related-links> --> |
| 303 | +
|
| 304 | +</section> |
| 305 | +
|
| 306 | +<!-- /.links --> |
0 commit comments