|
| 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 | +# everyBy |
| 22 | + |
| 23 | +> Test whether all elements in an ndarray pass 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 everyBy = require( '@stdlib/ndarray/base/every-by' ); |
| 37 | +``` |
| 38 | + |
| 39 | +#### everyBy( arrays, predicate\[, thisArg] ) |
| 40 | + |
| 41 | +Tests whether all elements in an ndarray pass a test implemented by a predicate function. |
| 42 | + |
| 43 | +<!-- eslint-disable max-len --> |
| 44 | + |
| 45 | +```javascript |
| 46 | +var Float64Array = require( '@stdlib/array/float64' ); |
| 47 | + |
| 48 | +function clbk( value ) { |
| 49 | + return value > 0.0; |
| 50 | +} |
| 51 | + |
| 52 | +// Create a data buffer: |
| 53 | +var xbuf = 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 ] ); |
| 54 | + |
| 55 | +// Define the shape of the input array: |
| 56 | +var shape = [ 3, 1, 2 ]; |
| 57 | + |
| 58 | +// Define the array strides: |
| 59 | +var sx = [ 4, 4, 1 ]; |
| 60 | + |
| 61 | +// Define the index offset: |
| 62 | +var ox = 0; |
| 63 | + |
| 64 | +// Create the input ndarray-like object: |
| 65 | +var x = { |
| 66 | + 'dtype': 'float64', |
| 67 | + 'data': xbuf, |
| 68 | + 'shape': shape, |
| 69 | + 'strides': sx, |
| 70 | + 'offset': ox, |
| 71 | + 'order': 'row-major' |
| 72 | +}; |
| 73 | + |
| 74 | +// Test elements: |
| 75 | +var out = everyBy( [ x ], clbk ); |
| 76 | +// returns true |
| 77 | +``` |
| 78 | + |
| 79 | +The function accepts the following arguments: |
| 80 | + |
| 81 | +- **arrays**: array-like object containing an input ndarray. |
| 82 | +- **predicate**: predicate function. |
| 83 | +- **thisArg**: predicate function execution context (_optional_). |
| 84 | + |
| 85 | +The provided ndarray should be an `object` with the following properties: |
| 86 | + |
| 87 | +- **dtype**: data type. |
| 88 | +- **data**: data buffer. |
| 89 | +- **shape**: dimensions. |
| 90 | +- **strides**: stride lengths. |
| 91 | +- **offset**: index offset. |
| 92 | +- **order**: specifies whether an ndarray is row-major (C-style) or column major (Fortran-style). |
| 93 | + |
| 94 | +The predicate function is provided the following arguments: |
| 95 | + |
| 96 | +- **value**: current array element. |
| 97 | +- **indices**: current array element indices. |
| 98 | +- **arr**: the input ndarray. |
| 99 | + |
| 100 | +To set the predicate function execution context, provide a `thisArg`. |
| 101 | + |
| 102 | +<!-- eslint-disable no-invalid-this, max-len --> |
| 103 | + |
| 104 | +```javascript |
| 105 | +var Float64Array = require( '@stdlib/array/float64' ); |
| 106 | + |
| 107 | +function clbk( value ) { |
| 108 | + this.count += 1; |
| 109 | + return value > 0.0; |
| 110 | +} |
| 111 | + |
| 112 | +// Create a data buffer: |
| 113 | +var xbuf = 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 ] ); |
| 114 | + |
| 115 | +// Define the shape of the input array: |
| 116 | +var shape = [ 3, 1, 2 ]; |
| 117 | + |
| 118 | +// Define the array strides: |
| 119 | +var sx = [ 4, 4, 1 ]; |
| 120 | + |
| 121 | +// Define the index offset: |
| 122 | +var ox = 0; |
| 123 | + |
| 124 | +// Create the input ndarray-like object: |
| 125 | +var x = { |
| 126 | + 'dtype': 'float64', |
| 127 | + 'data': xbuf, |
| 128 | + 'shape': shape, |
| 129 | + 'strides': sx, |
| 130 | + 'offset': ox, |
| 131 | + 'order': 'row-major' |
| 132 | +}; |
| 133 | + |
| 134 | +var ctx = { |
| 135 | + 'count': 0 |
| 136 | +}; |
| 137 | + |
| 138 | +// Test elements: |
| 139 | +var out = everyBy( [ x ], clbk, ctx ); |
| 140 | +// returns true |
| 141 | + |
| 142 | +var count = ctx.count; |
| 143 | +// returns 6 |
| 144 | +``` |
| 145 | + |
| 146 | +</section> |
| 147 | + |
| 148 | +<!-- /.usage --> |
| 149 | + |
| 150 | +<section class="notes"> |
| 151 | + |
| 152 | +## Notes |
| 153 | + |
| 154 | +- For very high-dimensional ndarrays which are non-contiguous, one should consider copying the underlying data to contiguous memory before performing the operation in order to achieve better performance. |
| 155 | +- If provided an empty ndarray, the function returns `true`. |
| 156 | + |
| 157 | +</section> |
| 158 | + |
| 159 | +<!-- /.notes --> |
| 160 | + |
| 161 | +<section class="examples"> |
| 162 | + |
| 163 | +## Examples |
| 164 | + |
| 165 | +<!-- eslint no-undef: "error" --> |
| 166 | + |
| 167 | +```javascript |
| 168 | +var discreteUniform = require( '@stdlib/random/array/discrete-uniform' ); |
| 169 | +var ndarray2array = require( '@stdlib/ndarray/base/to-array' ); |
| 170 | +var everyBy = require( '@stdlib/ndarray/base/every-by' ); |
| 171 | + |
| 172 | +function clbk( value ) { |
| 173 | + return value > 0; |
| 174 | +} |
| 175 | + |
| 176 | +var x = { |
| 177 | + 'dtype': 'generic', |
| 178 | + 'data': discreteUniform( 10, -2, 10, { |
| 179 | + 'dtype': 'generic' |
| 180 | + }), |
| 181 | + 'shape': [ 5, 2 ], |
| 182 | + 'strides': [ 2, 1 ], |
| 183 | + 'offset': 0, |
| 184 | + 'order': 'row-major' |
| 185 | +}; |
| 186 | +console.log( ndarray2array( x.data, x.shape, x.strides, x.offset, x.order ) ); |
| 187 | + |
| 188 | +var out = everyBy( [ x ], clbk ); |
| 189 | +console.log( out ); |
| 190 | +``` |
| 191 | + |
| 192 | +</section> |
| 193 | + |
| 194 | +<!-- /.examples --> |
| 195 | + |
| 196 | +<!-- Section for related `stdlib` packages. Do not manually edit this section, as it is automatically populated. --> |
| 197 | + |
| 198 | +<section class="related"> |
| 199 | + |
| 200 | +</section> |
| 201 | + |
| 202 | +<!-- /.related --> |
| 203 | + |
| 204 | +<section class="links"> |
| 205 | + |
| 206 | +</section> |
| 207 | + |
| 208 | +<!-- /.links --> |
0 commit comments