|
| 1 | +<!-- |
| 2 | +
|
| 3 | +@license Apache-2.0 |
| 4 | +
|
| 5 | +Copyright (c) 2024 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 | +# reject |
| 22 | + |
| 23 | +> Return a shallow copy of an [ndarray][@stdlib/ndarray/ctor] containing only those elements which fail a test implemented by a predicate function. |
| 24 | +
|
| 25 | +<section class="intro"> |
| 26 | + |
| 27 | +</section> |
| 28 | + |
| 29 | +<!-- /.intro --> |
| 30 | + |
| 31 | +<section class="usage"> |
| 32 | + |
| 33 | +## Usage |
| 34 | + |
| 35 | +```javascript |
| 36 | +var reject = require( '@stdlib/ndarray/reject' ); |
| 37 | +``` |
| 38 | + |
| 39 | +#### reject( x\[, options], predicate\[, thisArg] ) |
| 40 | + |
| 41 | +Returns a shallow copy of an [ndarray][@stdlib/ndarray/ctor] containing only those elements which fail a test implemented by a `predicate` function. |
| 42 | + |
| 43 | +<!-- eslint-disable max-len --> |
| 44 | + |
| 45 | +```javascript |
| 46 | +var Float64Array = require( '@stdlib/array/float64' ); |
| 47 | +var ndarray = require( '@stdlib/ndarray/ctor' ); |
| 48 | +var ndarray2array = require( '@stdlib/ndarray/to-array' ); |
| 49 | + |
| 50 | +function predicate( z ) { |
| 51 | + return z <= 6.0; |
| 52 | +} |
| 53 | + |
| 54 | +var buffer = new Float64Array( [ 1.0, 2.0, 3.0, 4.0, 5.0, 6.0, 7.0, 8.0, 9.0, 10.0, 11.0, 12.0 ] ); |
| 55 | +var shape = [ 2, 3 ]; |
| 56 | +var strides = [ 6, 1 ]; |
| 57 | +var offset = 1; |
| 58 | + |
| 59 | +var x = ndarray( 'float64', buffer, shape, strides, offset, 'row-major' ); |
| 60 | +// returns <ndarray> |
| 61 | + |
| 62 | +var y = reject( x, predicate ); |
| 63 | +// returns <ndarray> |
| 64 | + |
| 65 | +var arr = ndarray2array( y ); |
| 66 | +// returns [ 8.0, 9.0, 10.0 ] |
| 67 | +``` |
| 68 | + |
| 69 | +The function accepts the following arguments: |
| 70 | + |
| 71 | +- **x**: input [ndarray][@stdlib/ndarray/ctor]. |
| 72 | +- **options**: function options. |
| 73 | +- **predicate**: predicate function. |
| 74 | +- **thisArg**: predicate function execution context. |
| 75 | + |
| 76 | +The function accepts the following options: |
| 77 | + |
| 78 | +- **dtype**: output ndarray [data type][@stdlib/ndarray/dtypes]. If not specified, the output ndarray [data type][@stdlib/ndarray/dtypes] is inferred from the input [ndarray][@stdlib/ndarray/ctor]. |
| 79 | +- **order**: index iteration order. By default, the function iterates over elements according to the [layout order][@stdlib/ndarray/orders] of the provided [ndarray][@stdlib/ndarray/ctor]. Accordingly, for row-major input [ndarrays][@stdlib/ndarray/ctor], the last dimension indices increment fastest. For column-major input [ndarrays][@stdlib/ndarray/ctor], the first dimension indices increment fastest. To override the inferred order and ensure that indices increment in a specific manor, regardless of the input [ndarray][@stdlib/ndarray/ctor]'s layout order, explicitly set the iteration order. Note, however, that iterating according to an order which does not match that of the input [ndarray][@stdlib/ndarray/ctor] may, in some circumstances, result in performance degradation due to cache misses. Must be either `'row-major'` or `'column-major'`. |
| 80 | + |
| 81 | +By default, the output ndarray [data type][@stdlib/ndarray/dtypes] is inferred from the input [ndarray][@stdlib/ndarray/ctor]. To return an ndarray with a different [data type][@stdlib/ndarray/dtypes], specify the `dtype` option. |
| 82 | + |
| 83 | +<!-- eslint-disable max-len --> |
| 84 | + |
| 85 | +```javascript |
| 86 | +var Float64Array = require( '@stdlib/array/float64' ); |
| 87 | +var ndarray = require( '@stdlib/ndarray/ctor' ); |
| 88 | +var dtype = require( '@stdlib/ndarray/dtype' ); |
| 89 | +var ndarray2array = require( '@stdlib/ndarray/to-array' ); |
| 90 | + |
| 91 | +function predicate( z ) { |
| 92 | + return z <= 6.0; |
| 93 | +} |
| 94 | + |
| 95 | +var buffer = new Float64Array( [ 1.0, 2.0, 3.0, 4.0, 5.0, 6.0, 7.0, 8.0, 9.0, 10.0, 11.0, 12.0 ] ); |
| 96 | +var shape = [ 2, 3 ]; |
| 97 | +var strides = [ 6, 1 ]; |
| 98 | +var offset = 1; |
| 99 | + |
| 100 | +var x = ndarray( 'float64', buffer, shape, strides, offset, 'row-major' ); |
| 101 | +// returns <ndarray> |
| 102 | + |
| 103 | +var opts = { |
| 104 | + 'dtype': 'float32' |
| 105 | +}; |
| 106 | +var y = reject( x, opts, predicate ); |
| 107 | +// returns <ndarray> |
| 108 | + |
| 109 | +var dt = dtype( y ); |
| 110 | +// returns 'float32' |
| 111 | + |
| 112 | +var arr = ndarray2array( y ); |
| 113 | +// returns [ 8.0, 9.0, 10.0 ] |
| 114 | +``` |
| 115 | + |
| 116 | +The `predicate` function is provided the following arguments: |
| 117 | + |
| 118 | +- **value**: current array element. |
| 119 | +- **indices**: current array element indices. |
| 120 | +- **arr**: the input [ndarray][@stdlib/ndarray/ctor]. |
| 121 | + |
| 122 | +</section> |
| 123 | + |
| 124 | +<!-- /.usage --> |
| 125 | + |
| 126 | +<section class="notes"> |
| 127 | + |
| 128 | +## Notes |
| 129 | + |
| 130 | +- The function does **not** perform explicit casting (e.g., from a real-valued floating-point number to a complex floating-point number). Any such casting should be performed **prior to** calling this function. |
| 131 | +- The function **always** returns a one-dimensional [ndarray][@stdlib/ndarray/ctor]. |
| 132 | + |
| 133 | +</section> |
| 134 | + |
| 135 | +<!-- /.notes --> |
| 136 | + |
| 137 | +<section class="examples"> |
| 138 | + |
| 139 | +## Examples |
| 140 | + |
| 141 | +<!-- eslint no-undef: "error" --> |
| 142 | + |
| 143 | +```javascript |
| 144 | +var discreteUniform = require( '@stdlib/random/array/discrete-uniform' ); |
| 145 | +var ndarray2array = require( '@stdlib/ndarray/to-array' ); |
| 146 | +var naryFunction = require( '@stdlib/utils/nary-function' ); |
| 147 | +var array = require( '@stdlib/ndarray/array' ); |
| 148 | +var isPositive = require( '@stdlib/assert/is-positive-number' ).isPrimitive; |
| 149 | +var reject = require( '@stdlib/ndarray/reject' ); |
| 150 | + |
| 151 | +var buffer = discreteUniform( 10, -100, 100, { |
| 152 | + 'dtype': 'generic' |
| 153 | +}); |
| 154 | +var x = array( buffer, { |
| 155 | + 'shape': [ 5, 2 ], |
| 156 | + 'dtype': 'generic' |
| 157 | +}); |
| 158 | +console.log( ndarray2array( x ) ); |
| 159 | + |
| 160 | +var y = reject( x, naryFunction( isPositive, 1 ) ); |
| 161 | +console.log( ndarray2array( y ) ); |
| 162 | +``` |
| 163 | + |
| 164 | +</section> |
| 165 | + |
| 166 | +<!-- /.examples --> |
| 167 | + |
| 168 | +<!-- Section for related `stdlib` packages. Do not manually edit this section, as it is automatically populated. --> |
| 169 | + |
| 170 | +<section class="related"> |
| 171 | + |
| 172 | +</section> |
| 173 | + |
| 174 | +<!-- /.related --> |
| 175 | + |
| 176 | +<section class="links"> |
| 177 | + |
| 178 | +[@stdlib/ndarray/ctor]: https://github.com/stdlib-js/stdlib/tree/develop/lib/node_modules/%40stdlib/ndarray/ctor |
| 179 | + |
| 180 | +[@stdlib/ndarray/dtypes]: https://github.com/stdlib-js/stdlib/tree/develop/lib/node_modules/%40stdlib/ndarray/dtypes |
| 181 | + |
| 182 | +[@stdlib/ndarray/orders]: https://github.com/stdlib-js/stdlib/tree/develop/lib/node_modules/%40stdlib/ndarray/orders |
| 183 | + |
| 184 | +<!-- <related-links> --> |
| 185 | + |
| 186 | +<!-- </related-links> --> |
| 187 | + |
| 188 | +</section> |
| 189 | + |
| 190 | +<!-- /.links --> |
0 commit comments