|
| 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 | +# toSortedhp |
| 22 | + |
| 23 | +> Return a new [ndarray][@stdlib/ndarray/ctor] containing the elements of an input [ndarray][@stdlib/ndarray/ctor] sorted along one or more [ndarray][@stdlib/ndarray/ctor] dimensions using heapsort. |
| 24 | +
|
| 25 | +<section class="usage"> |
| 26 | + |
| 27 | +## Usage |
| 28 | + |
| 29 | +```javascript |
| 30 | +var toSortedhp = require( '@stdlib/blas/ext/to-sortedhp' ); |
| 31 | +``` |
| 32 | + |
| 33 | +#### toSortedhp( x\[, sortOrder]\[, options] ) |
| 34 | + |
| 35 | +Returns a new [ndarray][@stdlib/ndarray/ctor] containing the elements of an input [ndarray][@stdlib/ndarray/ctor] sorted along one or more [ndarray][@stdlib/ndarray/ctor] dimensions using heapsort. |
| 36 | + |
| 37 | +```javascript |
| 38 | +var ndarray2array = require( '@stdlib/ndarray/to-array' ); |
| 39 | +var array = require( '@stdlib/ndarray/array' ); |
| 40 | + |
| 41 | +var x = array( [ -1.0, 2.0, -3.0 ] ); |
| 42 | + |
| 43 | +var y = toSortedhp( x ); |
| 44 | +// returns <ndarray> |
| 45 | + |
| 46 | +var arr = ndarray2array( y ); |
| 47 | +// returns [ -3.0, -1.0, 2.0 ] |
| 48 | +``` |
| 49 | + |
| 50 | +The function has the following parameters: |
| 51 | + |
| 52 | +- **x**: input [ndarray][@stdlib/ndarray/ctor]. Must have a real-valued or "generic" [data type][@stdlib/ndarray/dtypes]. |
| 53 | +- **sortOrder**: sort order (_optional_). May be either a scalar value, string, or an [ndarray][@stdlib/ndarray/ctor] having a real-valued or "generic" [data type][@stdlib/ndarray/dtypes]. If provided an [ndarray][@stdlib/ndarray/ctor], the value must have a shape which is [broadcast-compatible][@stdlib/ndarray/base/broadcast-shapes] with the complement of the shape defined by `options.dims`. For example, given the input shape `[2, 3, 4]` and `options.dims=[0]`, an [ndarray][@stdlib/ndarray/ctor] sort order must have a shape which is [broadcast-compatible][@stdlib/ndarray/base/broadcast-shapes] with the shape `[3, 4]`. Similarly, when performing the operation over all elements in a provided input [ndarray][@stdlib/ndarray/ctor], an [ndarray][@stdlib/ndarray/ctor] sort order must be a zero-dimensional [ndarray][@stdlib/ndarray/ctor]. By default, the sort order is `1` (i.e., increasing order). |
| 54 | +- **options**: function options (_optional_). |
| 55 | + |
| 56 | +The function accepts the following options: |
| 57 | + |
| 58 | +- **dims**: list of dimensions over which to perform operation. If not provided, the function performs the operation over all elements in a provided input [ndarray][@stdlib/ndarray/ctor]. |
| 59 | + |
| 60 | +By default, the function sorts elements in increasing order. To sort in a different order, provide a `sortOrder` argument. |
| 61 | + |
| 62 | +```javascript |
| 63 | +var ndarray2array = require( '@stdlib/ndarray/to-array' ); |
| 64 | +var array = require( '@stdlib/ndarray/array' ); |
| 65 | + |
| 66 | +var x = array( [ -1.0, 2.0, -3.0 ] ); |
| 67 | + |
| 68 | +var y = toSortedhp( x, -1.0 ); |
| 69 | +// returns <ndarray> |
| 70 | + |
| 71 | +var arr = ndarray2array( y ); |
| 72 | +// returns [ 2.0, -1.0, -3.0 ] |
| 73 | +``` |
| 74 | + |
| 75 | +In addition to numeric values, one can specify the sort order via one of the following string literals: `'ascending'`, `'asc'`, `'descending'`, or `'desc'`. The first two literals indicate to sort in ascending (i.e., increasing) order. The last two literals indicate to sort in descending (i.e., decreasing) order. |
| 76 | + |
| 77 | +```javascript |
| 78 | +var ndarray2array = require( '@stdlib/ndarray/to-array' ); |
| 79 | +var array = require( '@stdlib/ndarray/array' ); |
| 80 | + |
| 81 | +var x = array( [ -1.0, 2.0, -3.0 ] ); |
| 82 | + |
| 83 | +// Sort in ascending order: |
| 84 | +var y = toSortedhp( x, 'asc' ); |
| 85 | +// returns <ndarray> |
| 86 | + |
| 87 | +var arr = ndarray2array( y ); |
| 88 | +// returns [ -3.0, -1.0, 2.0 ] |
| 89 | + |
| 90 | +// Sort in descending order: |
| 91 | +y = toSortedhp( x, 'descending' ); |
| 92 | +// returns <ndarray> |
| 93 | + |
| 94 | +arr = ndarray2array( y ); |
| 95 | +// returns [ 2.0, -1.0, -3.0 ] |
| 96 | +``` |
| 97 | + |
| 98 | +By default, the function performs the operation over all elements in a provided input [ndarray][@stdlib/ndarray/ctor]. To perform the operation over specific dimensions, provide a `dims` option. |
| 99 | + |
| 100 | +```javascript |
| 101 | +var ndarray2array = require( '@stdlib/ndarray/to-array' ); |
| 102 | +var array = require( '@stdlib/ndarray/array' ); |
| 103 | + |
| 104 | +var x = array( [ -1.0, 2.0, -3.0, 4.0 ], { |
| 105 | + 'shape': [ 2, 2 ], |
| 106 | + 'order': 'row-major' |
| 107 | +}); |
| 108 | + |
| 109 | +var v = ndarray2array( x ); |
| 110 | +// returns [ [ -1.0, 2.0 ], [ -3.0, 4.0 ] ] |
| 111 | + |
| 112 | +var y = toSortedhp( x, { |
| 113 | + 'dims': [ 0 ] |
| 114 | +}); |
| 115 | +// returns <ndarray> |
| 116 | + |
| 117 | +v = ndarray2array( y ); |
| 118 | +// returns [ [ -3.0, 2.0 ], [ -1.0, 4.0 ] ] |
| 119 | +``` |
| 120 | + |
| 121 | +#### toSortedhp.assign( x, y\[, sortOrder]\[, options] ) |
| 122 | + |
| 123 | +Sorts the elements of an input [ndarray][@stdlib/ndarray/ctor] along one or more [ndarray][@stdlib/ndarray/ctor] dimensions using heapsort and assigns the results to an output [ndarray][@stdlib/ndarray/ctor]. |
| 124 | + |
| 125 | +```javascript |
| 126 | +var ndarray2array = require( '@stdlib/ndarray/to-array' ); |
| 127 | +var zeros = require( '@stdlib/ndarray/zeros' ); |
| 128 | +var array = require( '@stdlib/ndarray/array' ); |
| 129 | + |
| 130 | +var x = array( [ -1.0, 2.0, -3.0 ] ); |
| 131 | +var y = zeros( [ 3 ] ); |
| 132 | + |
| 133 | +var out = toSortedhp.assign( x, y ); |
| 134 | +// returns <ndarray> |
| 135 | + |
| 136 | +var arr = ndarray2array( out ); |
| 137 | +// returns [ -3.0, -1.0, 2.0 ] |
| 138 | + |
| 139 | +var bool = ( y === out ); |
| 140 | +// returns true |
| 141 | +``` |
| 142 | + |
| 143 | +The function has the following parameters: |
| 144 | + |
| 145 | +- **x**: input [ndarray][@stdlib/ndarray/ctor]. Must have a real-valued or "generic" [data type][@stdlib/ndarray/dtypes]. |
| 146 | +- **y**: output [ndarray][@stdlib/ndarray/ctor]. Must have a real-valued or "generic" [data type][@stdlib/ndarray/dtypes]. |
| 147 | +- **sortOrder**: sort order (_optional_). May be either a scalar value, string, or an [ndarray][@stdlib/ndarray/ctor] having a real-valued or "generic" [data type][@stdlib/ndarray/dtypes]. If provided an [ndarray][@stdlib/ndarray/ctor], the value must have a shape which is [broadcast-compatible][@stdlib/ndarray/base/broadcast-shapes] with the complement of the shape defined by `options.dims`. For example, given the input shape `[2, 3, 4]` and `options.dims=[0]`, an [ndarray][@stdlib/ndarray/ctor] sort order must have a shape which is [broadcast-compatible][@stdlib/ndarray/base/broadcast-shapes] with the shape `[3, 4]`. Similarly, when performing the operation over all elements in a provided input [ndarray][@stdlib/ndarray/ctor], an [ndarray][@stdlib/ndarray/ctor] sort order must be a zero-dimensional [ndarray][@stdlib/ndarray/ctor]. By default, the sort order is `1` (i.e., increasing order). |
| 148 | +- **options**: function options (_optional_). |
| 149 | + |
| 150 | +The function accepts the following options: |
| 151 | + |
| 152 | +- **dims**: list of dimensions over which to perform operation. If not provided, the function performs the operation over all elements in a provided input [ndarray][@stdlib/ndarray/ctor]. |
| 153 | + |
| 154 | +</section> |
| 155 | + |
| 156 | +<!-- /.usage --> |
| 157 | + |
| 158 | +<section class="notes"> |
| 159 | + |
| 160 | +## Notes |
| 161 | + |
| 162 | +- If `sortOrder < 0.0` or is either `'desc'` or `'descending'`, the input [ndarray][@stdlib/ndarray/ctor] is sorted in **decreasing** order. If `sortOrder > 0.0` or is either `'asc'` or `'ascending'`, the input [ndarray][@stdlib/ndarray/ctor] is sorted in **increasing** order. If `sortOrder == 0.0`, the input [ndarray][@stdlib/ndarray/ctor] is left unchanged. |
| 163 | +- The algorithm distinguishes between `-0` and `+0`. When sorted in increasing order, `-0` is sorted before `+0`. When sorted in decreasing order, `-0` is sorted after `+0`. |
| 164 | +- The algorithm sorts `NaN` values to the end. When sorted in increasing order, `NaN` values are sorted last. When sorted in decreasing order, `NaN` values are sorted first. |
| 165 | +- The algorithm has space complexity `O(1)` and time complexity `O(N log2 N)`. |
| 166 | +- The algorithm is **unstable**, meaning that the algorithm may change the order of [ndarray][@stdlib/ndarray/ctor] elements which are equal or equivalent (e.g., `NaN` values). |
| 167 | +- The function iterates over [ndarray][@stdlib/ndarray/ctor] elements according to the memory layout of the input [ndarray][@stdlib/ndarray/ctor]. Accordingly, performance degradation is possible when operating over multiple dimensions of a large non-contiguous multi-dimensional input [ndarray][@stdlib/ndarray/ctor]. In such scenarios, one may want to copy an input [ndarray][@stdlib/ndarray/ctor] to contiguous memory before sorting. |
| 168 | + |
| 169 | +</section> |
| 170 | + |
| 171 | +<!-- /.notes --> |
| 172 | + |
| 173 | +<section class="examples"> |
| 174 | + |
| 175 | +## Examples |
| 176 | + |
| 177 | +<!-- eslint no-undef: "error" --> |
| 178 | + |
| 179 | +```javascript |
| 180 | +var discreteUniform = require( '@stdlib/random/array/discrete-uniform' ); |
| 181 | +var ndarray2array = require( '@stdlib/ndarray/to-array' ); |
| 182 | +var ndarray = require( '@stdlib/ndarray/ctor' ); |
| 183 | +var toSortedhp = require( '@stdlib/blas/ext/to-sortedhp' ); |
| 184 | + |
| 185 | +// Generate an array of random numbers: |
| 186 | +var xbuf = discreteUniform( 25, -20, 20, { |
| 187 | + 'dtype': 'generic' |
| 188 | +}); |
| 189 | + |
| 190 | +// Wrap in an ndarray: |
| 191 | +var x = new ndarray( 'generic', xbuf, [ 5, 5 ], [ 5, 1 ], 0, 'row-major' ); |
| 192 | +console.log( ndarray2array( x ) ); |
| 193 | + |
| 194 | +// Perform operation: |
| 195 | +var out = toSortedhp( x, { |
| 196 | + 'dims': [ 0 ] |
| 197 | +}); |
| 198 | + |
| 199 | +// Print the results: |
| 200 | +console.log( ndarray2array( out ) ); |
| 201 | +``` |
| 202 | + |
| 203 | +</section> |
| 204 | + |
| 205 | +<!-- /.examples --> |
| 206 | + |
| 207 | +<!-- Section for related `stdlib` packages. Do not manually edit this section, as it is automatically populated. --> |
| 208 | + |
| 209 | +<section class="related"> |
| 210 | + |
| 211 | +</section> |
| 212 | + |
| 213 | +<!-- /.related --> |
| 214 | + |
| 215 | +<!-- Section for all links. Make sure to keep an empty line after the `section` element and another before the `/section` close. --> |
| 216 | + |
| 217 | +<section class="links"> |
| 218 | + |
| 219 | +[@stdlib/ndarray/ctor]: https://github.com/stdlib-js/stdlib/tree/develop/lib/node_modules/%40stdlib/ndarray/ctor |
| 220 | + |
| 221 | +[@stdlib/ndarray/dtypes]: https://github.com/stdlib-js/stdlib/tree/develop/lib/node_modules/%40stdlib/ndarray/dtypes |
| 222 | + |
| 223 | +[@stdlib/ndarray/base/broadcast-shapes]: https://github.com/stdlib-js/stdlib/tree/develop/lib/node_modules/%40stdlib/ndarray/base/broadcast-shapes |
| 224 | + |
| 225 | +</section> |
| 226 | + |
| 227 | +<!-- /.links --> |
0 commit comments