|
| 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 | +# countIfs |
| 22 | + |
| 23 | +> Perform element-wise evaluation of one or more input arrays according to provided predicate functions and count the number of elements for which all predicates respectively return `true`. |
| 24 | +
|
| 25 | +<!-- Section to include introductory text. Make sure to keep an empty line after the intro `section` element and another before the `/section` close. --> |
| 26 | + |
| 27 | +<section class="intro"> |
| 28 | + |
| 29 | +</section> |
| 30 | + |
| 31 | +<!-- /.intro --> |
| 32 | + |
| 33 | +<!-- Package usage documentation. --> |
| 34 | + |
| 35 | +<section class="usage"> |
| 36 | + |
| 37 | +## Usage |
| 38 | + |
| 39 | +```javascript |
| 40 | +var countIfs = require( '@stdlib/array/base/count-ifs' ); |
| 41 | +``` |
| 42 | + |
| 43 | +#### countIfs( x0, predicate0\[, x1, predicate1\[, x2, predicate2\[, ...args]] ) |
| 44 | + |
| 45 | +Performs element-wise evaluation of one or more input arrays according to provided predicate functions and counts the number of elements for which all predicates respectively return `true`. |
| 46 | + |
| 47 | +```javascript |
| 48 | +function predicate0( value ) { |
| 49 | + return ( value > 0 ); |
| 50 | +} |
| 51 | + |
| 52 | +function predicate1( value ) { |
| 53 | + return ( value < 3 ); |
| 54 | +} |
| 55 | + |
| 56 | +var x0 = [ 0, 1, 0, 1, 2 ]; |
| 57 | +var x1 = [ 2, 3, 1, 2, 5 ]; |
| 58 | + |
| 59 | +var out = countIfs( x0, predicate0, x1, predicate1 ); |
| 60 | +// returns 1 |
| 61 | +``` |
| 62 | + |
| 63 | +The function has the following parameters: |
| 64 | + |
| 65 | +- **x0**: first input array. |
| 66 | +- **predicate0**: first predicate function. |
| 67 | +- **x1**: second input array (_optional_). |
| 68 | +- **predicate1**: second predicate function (_optional_). |
| 69 | +- **x2**: third input array (_optional_). |
| 70 | +- **predicate2**: third predicate function (_optional_). |
| 71 | +- **args**: additional input arrays and corresponding predicate functions (_optional_). |
| 72 | + |
| 73 | +Each predicate function is provided three arguments: |
| 74 | + |
| 75 | +- **value**: current array element. |
| 76 | +- **index**: current array element index. |
| 77 | +- **arr**: the corresponding input array. |
| 78 | + |
| 79 | +</section> |
| 80 | + |
| 81 | +<!-- /.usage --> |
| 82 | + |
| 83 | +<!-- Package usage notes. Make sure to keep an empty line after the `section` element and another before the `/section` close. --> |
| 84 | + |
| 85 | +<section class="notes"> |
| 86 | + |
| 87 | +## Notes |
| 88 | + |
| 89 | +- The function assumes that all input arrays have the same length. |
| 90 | +- The function supports array-like objects supporting the accessor protocol (e.g., [`Complex128Array`][@stdlib/array/complex128], [`Complex64Array`][@stdlib/array/complex64], etc). |
| 91 | + |
| 92 | +</section> |
| 93 | + |
| 94 | +<!-- /.notes --> |
| 95 | + |
| 96 | +<!-- Package usage examples. --> |
| 97 | + |
| 98 | +<section class="examples"> |
| 99 | + |
| 100 | +## Examples |
| 101 | + |
| 102 | +<!-- eslint no-undef: "error" --> |
| 103 | + |
| 104 | +<!-- eslint-disable max-len --> |
| 105 | + |
| 106 | +```javascript |
| 107 | +var discreteUniform = require( '@stdlib/random/array/discrete-uniform' ); |
| 108 | +var isPositiveInteger = require( '@stdlib/assert/is-positive-integer' ).isPrimitive; |
| 109 | +var isNegativeInteger = require( '@stdlib/assert/is-negative-integer' ).isPrimitive; |
| 110 | +var naryFunction = require( '@stdlib/utils/nary-function' ); |
| 111 | +var countIfs = require( '@stdlib/array/base/count-ifs' ); |
| 112 | + |
| 113 | +var x = discreteUniform( 10, -5, 5, { |
| 114 | + 'dtype': 'int32' |
| 115 | +}); |
| 116 | +console.log( x ); |
| 117 | + |
| 118 | +var y = discreteUniform( 10, -5, 5, { |
| 119 | + 'dtype': 'int32' |
| 120 | +}); |
| 121 | +console.log( y ); |
| 122 | + |
| 123 | +var out = countIfs( x, naryFunction( isPositiveInteger, 1 ), y, naryFunction( isNegativeInteger, 1 ) ); |
| 124 | +console.log( out ); |
| 125 | +``` |
| 126 | + |
| 127 | +</section> |
| 128 | + |
| 129 | +<!-- /.examples --> |
| 130 | + |
| 131 | +<!-- Section to include cited references. If references are included, add a horizontal rule *before* the section. Make sure to keep an empty line after the `section` element and another before the `/section` close. --> |
| 132 | + |
| 133 | +<section class="references"> |
| 134 | + |
| 135 | +</section> |
| 136 | + |
| 137 | +<!-- /.references --> |
| 138 | + |
| 139 | +<!-- Section for related `stdlib` packages. Do not manually edit this section, as it is automatically populated. --> |
| 140 | + |
| 141 | +<section class="related"> |
| 142 | + |
| 143 | +</section> |
| 144 | + |
| 145 | +<!-- /.related --> |
| 146 | + |
| 147 | +<!-- Section for all links. Make sure to keep an empty line after the `section` element and another before the `/section` close. --> |
| 148 | + |
| 149 | +<section class="links"> |
| 150 | + |
| 151 | +[@stdlib/array/complex128]: https://github.com/stdlib-js/stdlib/tree/develop/lib/node_modules/%40stdlib/array/complex128 |
| 152 | + |
| 153 | +[@stdlib/array/complex64]: https://github.com/stdlib-js/stdlib/tree/develop/lib/node_modules/%40stdlib/array/complex64 |
| 154 | + |
| 155 | +</section> |
| 156 | + |
| 157 | +<!-- /.links --> |
0 commit comments