diff --git a/lib/node_modules/@stdlib/array/base/count-ifs/README.md b/lib/node_modules/@stdlib/array/base/count-ifs/README.md new file mode 100644 index 000000000000..28c07bdbf430 --- /dev/null +++ b/lib/node_modules/@stdlib/array/base/count-ifs/README.md @@ -0,0 +1,157 @@ + + +# countIfs + +> 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`. + + + +
+ +
+ + + + + +
+ +## Usage + +```javascript +var countIfs = require( '@stdlib/array/base/count-ifs' ); +``` + +#### countIfs( x0, predicate0\[, x1, predicate1\[, x2, predicate2\[, ...args]] ) + +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`. + +```javascript +function predicate0( value ) { + return ( value > 0 ); +} + +function predicate1( value ) { + return ( value < 3 ); +} + +var x0 = [ 0, 1, 0, 1, 2 ]; +var x1 = [ 2, 3, 1, 2, 5 ]; + +var out = countIfs( x0, predicate0, x1, predicate1 ); +// returns 1 +``` + +The function has the following parameters: + +- **x0**: first input array. +- **predicate0**: first predicate function. +- **x1**: second input array (_optional_). +- **predicate1**: second predicate function (_optional_). +- **x2**: third input array (_optional_). +- **predicate2**: third predicate function (_optional_). +- **args**: additional input arrays and corresponding predicate functions (_optional_). + +Each predicate function is provided three arguments: + +- **value**: current array element. +- **index**: current array element index. +- **arr**: the corresponding input array. + +
+ + + + + +
+ +## Notes + +- The function assumes that all input arrays have the same length. +- The function supports array-like objects supporting the accessor protocol (e.g., [`Complex128Array`][@stdlib/array/complex128], [`Complex64Array`][@stdlib/array/complex64], etc). + +
+ + + + + +
+ +## Examples + + + + + +```javascript +var discreteUniform = require( '@stdlib/random/array/discrete-uniform' ); +var isPositiveInteger = require( '@stdlib/assert/is-positive-integer' ).isPrimitive; +var isNegativeInteger = require( '@stdlib/assert/is-negative-integer' ).isPrimitive; +var naryFunction = require( '@stdlib/utils/nary-function' ); +var countIfs = require( '@stdlib/array/base/count-ifs' ); + +var x = discreteUniform( 10, -5, 5, { + 'dtype': 'int32' +}); +console.log( x ); + +var y = discreteUniform( 10, -5, 5, { + 'dtype': 'int32' +}); +console.log( y ); + +var out = countIfs( x, naryFunction( isPositiveInteger, 1 ), y, naryFunction( isNegativeInteger, 1 ) ); +console.log( out ); +``` + +
+ + + + + +
+ +
+ + + + + + + + + + + + + + diff --git a/lib/node_modules/@stdlib/array/base/count-ifs/benchmark/benchmark.length.js b/lib/node_modules/@stdlib/array/base/count-ifs/benchmark/benchmark.length.js new file mode 100644 index 000000000000..13d9b2027fe3 --- /dev/null +++ b/lib/node_modules/@stdlib/array/base/count-ifs/benchmark/benchmark.length.js @@ -0,0 +1,123 @@ +/** +* @license Apache-2.0 +* +* Copyright (c) 2025 The Stdlib Authors. +* +* Licensed under the Apache License, Version 2.0 (the "License"); +* you may not use this file except in compliance with the License. +* You may obtain a copy of the License at +* +* http://www.apache.org/licenses/LICENSE-2.0 +* +* Unless required by applicable law or agreed to in writing, software +* distributed under the License is distributed on an "AS IS" BASIS, +* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. +* See the License for the specific language governing permissions and +* limitations under the License. +*/ + +'use strict'; + +// MODULES // + +var bench = require( '@stdlib/bench' ); +var pow = require( '@stdlib/math/base/special/pow' ); +var isNonNegativeInteger = require( '@stdlib/assert/is-nonnegative-integer' ).isPrimitive; +var uniform = require( '@stdlib/random/array/uniform' ); +var pkg = require( './../package.json' ).name; +var countIfs = require( './../lib' ); + + +// FUNCTIONS // + +/** +* First predicate function. +* +* @private +* @param {number} v - value +* @returns {boolean} result +*/ +function predicate0( v ) { + return v > 0.0; +} + +/** +* Second predicate function. +* +* @private +* @param {number} v - value +* @returns {boolean} result +*/ +function predicate1( v ) { + return v < 0.0; +} + +/** +* Creates a benchmark function. +* +* @private +* @param {PositiveInteger} len - array length +* @returns {Function} benchmark function +*/ +function createBenchmark( len ) { + var x0 = uniform( len, -1.0, 1.0, { + 'dype': 'generic' + }); + var x1 = uniform( len, -1.0, 1.0, { + 'dype': 'generic' + }); + return benchmark; + + /** + * Benchmark function. + * + * @private + * @param {Benchmark} b - benchmark instance + */ + function benchmark( b ) { + var out; + var i; + + b.tic(); + for ( i = 0; i < b.iterations; i++ ) { + out = countIfs( x0, predicate0, x1, predicate1 ); + if ( typeof out !== 'number' ) { + b.fail( 'should return a number' ); + } + } + b.toc(); + if ( !isNonNegativeInteger( out ) ) { + b.fail( 'should return a nonnegative integer' ); + } + b.pass( 'benchmark finished' ); + b.end(); + } +} + + +// MAIN // + +/** +* Main execution sequence. +* +* @private +*/ +function main() { + var len; + var min; + var max; + var f; + var i; + + min = 1; // 10^min + max = 6; // 10^max + + for ( i = min; i <= max; i++ ) { + len = pow( 10, i ); + + f = createBenchmark( len ); + bench( pkg+':dtype=generic,predicates=2,len='+len, f ); + } +} + +main(); diff --git a/lib/node_modules/@stdlib/array/base/count-ifs/docs/repl.txt b/lib/node_modules/@stdlib/array/base/count-ifs/docs/repl.txt new file mode 100644 index 000000000000..8a579394f41f --- /dev/null +++ b/lib/node_modules/@stdlib/array/base/count-ifs/docs/repl.txt @@ -0,0 +1,50 @@ + +{{alias}}( x0, predicate0[, x1, predicate1[, x2, predicate2[, ...args]] ) + 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`. + + Each predicate function is provided three arguments: + + - value: current array element. + - index: current array element index. + - arr: the corresponding input array. + + Parameters + ---------- + x0: ArrayLikeObject + First input array. + + predicate0: Function + First predicate function. + + x1: ArrayLikeObject (optional) + Second input array. + + predicate1: Function (optional) + Second predicate function. + + x2: ArrayLikeObject (optional) + Third input array. + + predicate2: Function (optional) + Third predicate function. + + ...args: ArrayLikeObject|Function (optional) + Additional input arrays and corresponding predicate functions. + + Returns + ------- + out: integer + Result. + + Examples + -------- + > function f1( v ) { return ( v > 0 ); }; + > function f2( v ) { return ( v < 0 ); }; + > var out = {{alias}}( [ 0, 1, 1 ], f1, [ 1, -1, -1 ], f2 ) + 2 + + See Also + -------- + diff --git a/lib/node_modules/@stdlib/array/base/count-ifs/docs/types/index.d.ts b/lib/node_modules/@stdlib/array/base/count-ifs/docs/types/index.d.ts new file mode 100644 index 000000000000..75e471419bba --- /dev/null +++ b/lib/node_modules/@stdlib/array/base/count-ifs/docs/types/index.d.ts @@ -0,0 +1,243 @@ +/* +* @license Apache-2.0 +* +* Copyright (c) 2025 The Stdlib Authors. +* +* Licensed under the Apache License, Version 2.0 (the "License"); +* you may not use this file except in compliance with the License. +* You may obtain a copy of the License at +* +* http://www.apache.org/licenses/LICENSE-2.0 +* +* Unless required by applicable law or agreed to in writing, software +* distributed under the License is distributed on an "AS IS" BASIS, +* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. +* See the License for the specific language governing permissions and +* limitations under the License. +*/ + +// TypeScript Version: 4.1 + +/// + +import { Collection, AccessorArrayLike } from '@stdlib/types/array'; + +/** +* Input array. +*/ +type InputArray = Collection | AccessorArrayLike; + +/** +* Returns a boolean indicating whether an element passes a test. +* +* @returns boolean indicating whether an element passes a test +*/ +type Nullary = () => boolean; + +/** +* Returns a boolean indicating whether an element passes a test. +* +* @param value - current array element +* @returns boolean indicating whether an element passes a test +*/ +type Unary = ( value: T ) => boolean; + +/** +* Returns a boolean indicating whether an element passes a test. +* +* @param value - current array element +* @param index - current array element index +* @returns boolean indicating whether an element passes a test +*/ +type Binary = ( value: T, index: number ) => boolean; + +/** +* Returns a boolean indicating whether an element passes a test. +* +* @param value - current array element +* @param index - current array element index +* @param arr - input array +* @returns boolean indicating whether an element passes a test +*/ +type Ternary = ( value: T, index: number, arr: U ) => boolean; + +/** +* Returns a boolean indicating whether an element passes a test. +* +* @param value - current array element +* @param index - current array element index +* @param arr - input array +* @returns boolean indicating whether an element passes a test +*/ +type Predicate = Nullary | Unary | Binary | Ternary; + +/** +* 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`. +* +* @param x0 - input array +* @param predicate0 - predicate function +* @returns result +* +* @example +* function predicate0( v ) { +* return v > 0; +* } +* +* var x0 = [ 0, 1, 0, 1, 1 ]; +* +* var n = countIfs( x0, predicate0 ); +* // returns 3 +*/ +declare function countIfs = InputArray>( x0: U, predicate0: Predicate ): number; + +/** +* 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`. +* +* @param x0 - first input array +* @param predicate0 - first predicate function +* @param x1 - second input array +* @param predicate1 - second predicate function +* @returns result +* +* @example +* function predicate0( v ) { +* return v > 0; +* } +* +* function predicate1( v ) { +* return v < 0; +* } +* +* var x0 = [ 0, 1, 0, 1, 1 ]; +* var x1 = [ -1, 2, 4, -5, -8 ]; +* +* var n = countIfs( x0, predicate0, x1, predicate1 ); +* // returns 2 +*/ +declare function countIfs = InputArray>( x0: U, predicate0: Predicate, x1: U, predicate1: Predicate ): number; + +/** +* 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`. +* +* @param x0 - first input array +* @param predicate0 - first predicate function +* @param x1 - second input array +* @param predicate1 - second predicate function +* @param x2 - third input array +* @param predicate2 - third predicate function +* @returns result +* +* @example +* function predicate0( v ) { +* return v > 0; +* } +* +* function predicate1( v ) { +* return v < 0; +* } +* +* function predicate2( v ) { +* return v % 2 === 0; +* } +* +* var x0 = [ 0, 1, 0, 1, 1 ]; +* var x1 = [ -1, -2, 4, 5, -8 ]; +* var x2 = [ 0, 4, 3, 2, 12 ]; +* +* var n = countIfs( x0, predicate0, x1, predicate1 ); +* // returns 2 +*/ +declare function countIfs = InputArray>( x0: U, predicate0: Predicate, x1: U, predicate1: Predicate, x2: U, predicate2: Predicate ): number; + +/** +* 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`. +* +* @param x0 - first input array +* @param predicate0 - first predicate function +* @param x1 - second input array +* @param predicate1 - second predicate function +* @param x2 - third input array +* @param predicate2 - third predicate function +* @param x3 - fourth input array +* @param predicate3 - fourth predicate function +* @returns result +* +* @example +* function predicate0( v ) { +* return v > 0; +* } +* +* function predicate1( v ) { +* return v < 0; +* } +* +* function predicate2( v ) { +* return v % 2 === 0; +* } +* +* function predicate3( v ) { +* return v % 2 !== 0; +* } +* +* var x0 = [ 0, 1, 0, 1, 1 ]; +* var x1 = [ -1, -2, 4, 5, -8 ]; +* var x2 = [ 0, 4, 3, 2, 12 ]; +* var x3 = [ 2, 9, 3, 6, 5 ]; +* +* var n = countIfs( x0, predicate0, x1, predicate1, x3, predicate3 ); +* // returns 2 +*/ +declare function countIfs = InputArray>( x0: U, predicate0: Predicate, x1: U, predicate1: Predicate, x2: U, predicate2: Predicate, x3: U, predicate3: Predicate ): number; + +/** +* 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`. +* +* @param x0 - first input array +* @param predicate0 - first predicate function +* @param x1 - second input array +* @param predicate1 - second predicate function +* @param x2 - third input array +* @param predicate2 - third predicate function +* @param x3 - fourth input array +* @param predicate3 - fourth predicate function +* @param x4 - fifth input array +* @param predicate4 - fifth predicate function +* @param args - additional input arrays and predicate functions +* @returns result +* +* @example +* function predicate0( v ) { +* return v > 0; +* } +* +* function predicate1( v ) { +* return v < 0; +* } +* +* function predicate2( v ) { +* return v % 2 === 0; +* } +* +* function predicate3( v ) { +* return v % 2 !== 0; +* } +* +* function predicate4( v ) { +* return v === true; +* } +* +* var x0 = [ 0, 1, 0, 1, 1 ]; +* var x1 = [ -1, -2, 4, 5, -8 ]; +* var x2 = [ 0, 4, 3, 2, 12 ]; +* var x3 = [ 2, 9, 3, 6, 5 ]; +* var x4 = [ false, true, false, true, true ] +* +* var n = countIfs( x0, predicate0, x1, predicate1, x3, predicate3, x4, predicate4 ); +* // returns 2 +*/ +declare function countIfs = InputArray>( x0: U, predicate0: Predicate, x1: U, predicate1: Predicate, x2: U, predicate2: Predicate, x3: U, predicate3: Predicate, x4: U, predicate4: Predicate, ...args: Array> ): number; + + +// EXPORTS // + +export = countIfs; diff --git a/lib/node_modules/@stdlib/array/base/count-ifs/docs/types/test.ts b/lib/node_modules/@stdlib/array/base/count-ifs/docs/types/test.ts new file mode 100644 index 000000000000..679095e2a9fd --- /dev/null +++ b/lib/node_modules/@stdlib/array/base/count-ifs/docs/types/test.ts @@ -0,0 +1,194 @@ +/* +* @license Apache-2.0 +* +* Copyright (c) 2025 The Stdlib Authors. +* +* Licensed under the Apache License, Version 2.0 (the "License"); +* you may not use this file except in compliance with the License. +* You may obtain a copy of the License at +* +* http://www.apache.org/licenses/LICENSE-2.0 +* +* Unless required by applicable law or agreed to in writing, software +* distributed under the License is distributed on an "AS IS" BASIS, +* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. +* See the License for the specific language governing permissions and +* limitations under the License. +*/ + +import toAccessorArray = require( '@stdlib/array/base/to-accessor-array' ); +import countIfs = require( './index' ); + +/** +* Tests whether a value is positive. +* +* @param value - input value +* @returns boolean indicating whether an element is positive +*/ +function isPositive( value: number ): boolean { + return ( value > 0 ); +} + +/** +* Tests whether a value is negative. +* +* @param value - input value +* @returns boolean indicating whether an element is negative +*/ +function isNegative( value: number ): boolean { + return ( value < 0 ); +} + + +// TESTS // + +// The function returns a number... +{ + const x = [ 1, 2, 3 ]; + const y = [ -1, -2, -3 ]; + + countIfs( x, isPositive ); // $ExpectType number + countIfs( x, isPositive, y, isNegative ); // $ExpectType number + countIfs( x, isPositive, x, isPositive, y, isNegative ); // $ExpectType number + countIfs( x, isPositive, x, isPositive, x, isPositive, y, isNegative ); // $ExpectType number + countIfs( x, isPositive, x, isPositive, x, isPositive, x, isPositive, y, isNegative ); // $ExpectType number + countIfs( x, isPositive, x, isPositive, x, isPositive, x, isPositive, x, isPositive, y, isNegative ); // $ExpectType number + countIfs( x, isPositive, x, isPositive, x, isPositive, x, isPositive, x, isPositive, x, isPositive, y, isNegative ); // $ExpectType number + + countIfs( new Float64Array( x ), isPositive, new Float64Array( y ), isNegative ); // $ExpectType number + countIfs( new Float32Array( x ), isPositive, new Float32Array( y ), isNegative ); // $ExpectType number + countIfs( new Int32Array( x ), isPositive, new Int32Array( y ), isNegative ); // $ExpectType number + countIfs( new Int16Array( x ), isPositive, new Int16Array( y ), isNegative ); // $ExpectType number + countIfs( new Int8Array( x ), isPositive, new Int8Array( y ), isNegative ); // $ExpectType number + countIfs( new Uint32Array( x ), isPositive, new Uint32Array( y ), isNegative ); // $ExpectType number + countIfs( new Uint16Array( x ), isPositive, new Uint16Array( y ), isNegative ); // $ExpectType number + countIfs( new Uint8Array( x ), isPositive, new Uint8Array( y ), isNegative ); // $ExpectType number + countIfs( new Uint8ClampedArray( x ), isPositive, new Uint8ClampedArray( y ), isNegative ); // $ExpectType number + countIfs( toAccessorArray( x ), isPositive, toAccessorArray( y ), isNegative ); // $ExpectType number +} + +// The compiler throws an error if the function is provided a first argument which is not a collection... +{ + countIfs( 2, isPositive ); // $ExpectError + countIfs( false, isPositive ); // $ExpectError + countIfs( true, isPositive ); // $ExpectError + countIfs( {}, isPositive ); // $ExpectError +} + +// The compiler throws an error if the function is provided a second argument which is not a function... +{ + const x = [ 1, 2, 3 ]; + + countIfs( x, 'abc' ); // $ExpectError + countIfs( x, 2 ); // $ExpectError + countIfs( x, false ); // $ExpectError + countIfs( x, true ); // $ExpectError + countIfs( x, null ); // $ExpectError + countIfs( x, void 0 ); // $ExpectError + countIfs( x, {} ); // $ExpectError + countIfs( x, [] ); // $ExpectError +} + +// The compiler throws an error if the function is provided a third argument which is not a collection... +{ + const x = [ 1, 2, 3 ]; + + countIfs( x, isPositive, 2, isNegative ); // $ExpectError + countIfs( x, isPositive, false, isNegative ); // $ExpectError + countIfs( x, isPositive, true, isNegative ); // $ExpectError + countIfs( x, isPositive, {}, isNegative ); // $ExpectError +} + +// The compiler throws an error if the function is provided a fourth argument which is not a function... +{ + const x = [ 1, 2, 3 ]; + + countIfs( x, isPositive, x, 'abc' ); // $ExpectError + countIfs( x, isPositive, x, 2 ); // $ExpectError + countIfs( x, isPositive, x, false ); // $ExpectError + countIfs( x, isPositive, x, true ); // $ExpectError + countIfs( x, isPositive, x, null ); // $ExpectError + countIfs( x, isPositive, x, void 0 ); // $ExpectError + countIfs( x, isPositive, x, {} ); // $ExpectError + countIfs( x, isPositive, x, [] ); // $ExpectError +} + +// The compiler throws an error if the function is provided a fifth argument which is not a collection... +{ + const x = [ 1, 2, 3 ]; + + countIfs( x, isPositive, x, isPositive, 2, isNegative ); // $ExpectError + countIfs( x, isPositive, x, isPositive, false, isNegative ); // $ExpectError + countIfs( x, isPositive, x, isPositive, true, isNegative ); // $ExpectError + countIfs( x, isPositive, x, isPositive, {}, isNegative ); // $ExpectError +} + +// The compiler throws an error if the function is provided a sixth argument which is not a function... +{ + const x = [ 1, 2, 3 ]; + + countIfs( x, isPositive, x, isPositive, x, 'abc' ); // $ExpectError + countIfs( x, isPositive, x, isPositive, x, 2 ); // $ExpectError + countIfs( x, isPositive, x, isPositive, x, false ); // $ExpectError + countIfs( x, isPositive, x, isPositive, x, true ); // $ExpectError + countIfs( x, isPositive, x, isPositive, x, null ); // $ExpectError + countIfs( x, isPositive, x, isPositive, x, void 0 ); // $ExpectError + countIfs( x, isPositive, x, isPositive, x, {} ); // $ExpectError + countIfs( x, isPositive, x, isPositive, x, [] ); // $ExpectError +} + +// The compiler throws an error if the function is provided a seventh argument which is not a collection... +{ + const x = [ 1, 2, 3 ]; + + countIfs( x, isPositive, x, isPositive, x, isPositive, 2, isNegative ); // $ExpectError + countIfs( x, isPositive, x, isPositive, x, isPositive, false, isNegative ); // $ExpectError + countIfs( x, isPositive, x, isPositive, x, isPositive, true, isNegative ); // $ExpectError + countIfs( x, isPositive, x, isPositive, x, isPositive, {}, isNegative ); // $ExpectError +} + +// The compiler throws an error if the function is provided an eighth argument which is not a function... +{ + const x = [ 1, 2, 3 ]; + + countIfs( x, isPositive, x, isPositive, x, isPositive, x, 'abc' ); // $ExpectError + countIfs( x, isPositive, x, isPositive, x, isPositive, x, 2 ); // $ExpectError + countIfs( x, isPositive, x, isPositive, x, isPositive, x, false ); // $ExpectError + countIfs( x, isPositive, x, isPositive, x, isPositive, x, true ); // $ExpectError + countIfs( x, isPositive, x, isPositive, x, isPositive, x, null ); // $ExpectError + countIfs( x, isPositive, x, isPositive, x, isPositive, x, void 0 ); // $ExpectError + countIfs( x, isPositive, x, isPositive, x, isPositive, x, {} ); // $ExpectError + countIfs( x, isPositive, x, isPositive, x, isPositive, x, [] ); // $ExpectError +} + +// The compiler throws an error if the function is provided a ninth argument which is not a collection... +{ + const x = [ 1, 2, 3 ]; + + countIfs( x, isPositive, x, isPositive, x, isPositive, x, isPositive, 2, isNegative ); // $ExpectError + countIfs( x, isPositive, x, isPositive, x, isPositive, x, isPositive, false, isNegative ); // $ExpectError + countIfs( x, isPositive, x, isPositive, x, isPositive, x, isPositive, true, isNegative ); // $ExpectError + countIfs( x, isPositive, x, isPositive, x, isPositive, x, isPositive, {}, isNegative ); // $ExpectError +} + +// The compiler throws an error if the function is provided a tenth argument which is not a function... +{ + const x = [ 1, 2, 3 ]; + + countIfs( x, isPositive, x, isPositive, x, isPositive, x, isPositive, x, 'abc' ); // $ExpectError + countIfs( x, isPositive, x, isPositive, x, isPositive, x, isPositive, x, 2 ); // $ExpectError + countIfs( x, isPositive, x, isPositive, x, isPositive, x, isPositive, x, false ); // $ExpectError + countIfs( x, isPositive, x, isPositive, x, isPositive, x, isPositive, x, true ); // $ExpectError + countIfs( x, isPositive, x, isPositive, x, isPositive, x, isPositive, x, null ); // $ExpectError + countIfs( x, isPositive, x, isPositive, x, isPositive, x, isPositive, x, void 0 ); // $ExpectError + countIfs( x, isPositive, x, isPositive, x, isPositive, x, isPositive, x, {} ); // $ExpectError + countIfs( x, isPositive, x, isPositive, x, isPositive, x, isPositive, x, [] ); // $ExpectError +} + +// The compiler throws an error if the function is provided an unsupported number of arguments... +{ + const x = [ 1, 2, 3 ]; + + countIfs(); // $ExpectError + countIfs( x ); // $ExpectError +} diff --git a/lib/node_modules/@stdlib/array/base/count-ifs/examples/index.js b/lib/node_modules/@stdlib/array/base/count-ifs/examples/index.js new file mode 100644 index 000000000000..3b148e70f84b --- /dev/null +++ b/lib/node_modules/@stdlib/array/base/count-ifs/examples/index.js @@ -0,0 +1,38 @@ +/** +* @license Apache-2.0 +* +* Copyright (c) 2025 The Stdlib Authors. +* +* Licensed under the Apache License, Version 2.0 (the "License"); +* you may not use this file except in compliance with the License. +* You may obtain a copy of the License at +* +* http://www.apache.org/licenses/LICENSE-2.0 +* +* Unless required by applicable law or agreed to in writing, software +* distributed under the License is distributed on an "AS IS" BASIS, +* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. +* See the License for the specific language governing permissions and +* limitations under the License. +*/ + +'use strict'; + +var discreteUniform = require( '@stdlib/random/array/discrete-uniform' ); +var isPositiveInteger = require( '@stdlib/assert/is-positive-integer' ).isPrimitive; +var isNegativeInteger = require( '@stdlib/assert/is-negative-integer' ).isPrimitive; +var naryFunction = require( '@stdlib/utils/nary-function' ); +var countIfs = require( './../lib' ); + +var x = discreteUniform( 10, -5, 5, { + 'dtype': 'int32' +}); +console.log( x ); + +var y = discreteUniform( 10, -5, 5, { + 'dtype': 'int32' +}); +console.log( y ); + +var out = countIfs( x, naryFunction( isPositiveInteger, 1 ), y, naryFunction( isNegativeInteger, 1 ) ); // eslint-disable-line max-len +console.log( out ); diff --git a/lib/node_modules/@stdlib/array/base/count-ifs/lib/index.js b/lib/node_modules/@stdlib/array/base/count-ifs/lib/index.js new file mode 100644 index 000000000000..3b8632f51c67 --- /dev/null +++ b/lib/node_modules/@stdlib/array/base/count-ifs/lib/index.js @@ -0,0 +1,51 @@ +/** +* @license Apache-2.0 +* +* Copyright (c) 2025 The Stdlib Authors. +* +* Licensed under the Apache License, Version 2.0 (the "License"); +* you may not use this file except in compliance with the License. +* You may obtain a copy of the License at +* +* http://www.apache.org/licenses/LICENSE-2.0 +* +* Unless required by applicable law or agreed to in writing, software +* distributed under the License is distributed on an "AS IS" BASIS, +* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. +* See the License for the specific language governing permissions and +* limitations under the License. +*/ + +'use strict'; + +/** +* 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`. +* +* @module @stdlib/array/base/count-ifs +* +* @example +* var countIfs = require( '@stdlib/array/base/count-ifs' ); +* +* function predicate0( value ) { +* return ( value > 0 ); +* } +* +* function predicate1( value ) { +* return ( value < 3 ); +* } +* +* var x0 = [ 0, 1, 0, 1, 2 ]; +* var x1 = [ 2, 3, 1, 2, 5 ]; +* +* var n = countIfs( x0, predicate0, x1, predicate1 ); +* // returns 1 +*/ + +// MODULES // + +var main = require( './main.js' ); + + +// EXPORTS // + +module.exports = main; diff --git a/lib/node_modules/@stdlib/array/base/count-ifs/lib/main.js b/lib/node_modules/@stdlib/array/base/count-ifs/lib/main.js new file mode 100644 index 000000000000..3a4bb5d7ff3f --- /dev/null +++ b/lib/node_modules/@stdlib/array/base/count-ifs/lib/main.js @@ -0,0 +1,175 @@ +/** +* @license Apache-2.0 +* +* Copyright (c) 2025 The Stdlib Authors. +* +* Licensed under the Apache License, Version 2.0 (the "License"); +* you may not use this file except in compliance with the License. +* You may obtain a copy of the License at +* +* http://www.apache.org/licenses/LICENSE-2.0 +* +* Unless required by applicable law or agreed to in writing, software +* distributed under the License is distributed on an "AS IS" BASIS, +* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. +* See the License for the specific language governing permissions and +* limitations under the License. +*/ + +'use strict'; + +// MODULES // + +var arraylike2object = require( '@stdlib/array/base/arraylike2object' ); +var zeroTo = require( '@stdlib/array/base/zero-to' ); + + +// FUNCTIONS // + +/** +* Returns an array of indices corresponding to the elements of an input array which pass a test implemented by a predicate function. +* +* @private +* @param {Collection} x - input array +* @param {Array} idx - list of indices +* @param {Function} predicate - predicate function +* @returns {Array} updated list of indices +* +* @example +* var arraylike2object = require( '@stdlib/array/base/arraylike2object' ); +* var toAccessorArray = require( '@stdlib/array/base/to-accessor-array' ); +* var zeroTo = require( '@stdlib/array/base/zero-to' ); +* +* function predicate( v ) { +* return ( v > 0 ); +* } +* +* var x = arraylike2object( toAccessorArray( [ -1, 2, 3, -4, 5 ] ) ); +* var idx = zeroTo( 5 ); +* +* var out = accessors( x, idx, predicate ); +* // returns [ 1, 2, 4 ] +*/ +function accessors( x, idx, predicate ) { + var buf; + var get; + var i; + var j; + var k; + + buf = x.data; + get = x.accessors[ 0 ]; + + k = 0; + for ( i = 0; i < idx.length; i++ ) { + j = idx[ i ]; + if ( predicate( get( buf, j ), j, buf ) ) { + idx[ k ] = j; + k += 1; + } + } + idx.length = k; + return idx; +} + +/** +* Returns an array of indices corresponding to the elements of an input array which pass a test implemented by a predicate function. +* +* @private +* @param {Collection} x - input array +* @param {Array} idx - list of indices +* @param {Function} predicate - predicate function +* @returns {Array} updated list of indices +* +* @example +* var zeroTo = require( '@stdlib/array/base/zero-to' ); +* +* function predicate( v ) { +* return ( v > 0 ); +* } +* +* var x = [ -1, 2, 3, -4, 5 ]; +* var idx = zeroTo( 5 ); +* +* var out = indexed( x, idx, predicate ); +* // returns [ 1, 2, 4 ] +*/ +function indexed( x, idx, predicate ) { + var i; + var j; + var k; + + k = 0; + for ( i = 0; i < idx.length; i++ ) { + j = idx[ i ]; + if ( predicate( x[ j ], j, x ) ) { + idx[ k ] = j; + k += 1; + } + } + idx.length = k; + return idx; +} + + +// MAIN // + +/** +* 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`. +* +* @param {Collection} x0 - first input array +* @param {Function} predicate0 - first predicate function +* @param {Collection} [x1] - second input array +* @param {Function} [predicate1] - second predicate function +* @param {Collection} [x2] - third input array +* @param {Function} [predicate2] - third predicate function +* @param {...(Collection|Function)} [args] - additional input arrays and predicate functions +* @returns {NonNegativeInteger} result +* +* @example +* function predicate0( value ) { +* return ( value > 0 ); +* } +* +* function predicate1( value ) { +* return ( value < 3 ); +* } +* +* var x0 = [ 0, 1, 0, 1, 2 ]; +* var x1 = [ 2, 3, 1, 2, 5 ]; +* +* var n = countIfs( x0, predicate0, x1, predicate1 ); +* // returns 1 +*/ +function countIfs() { + var predicates; + var arrays; + var len; + var idx; + var x; + var i; + + arrays = []; + predicates = []; + for ( i = 0; i < arguments.length; i += 2 ) { + arrays.push( arraylike2object( arguments[ i ] ) ); + predicates.push( arguments[ i+1 ] ); + } + + len = arguments[ 0 ].length; + idx = zeroTo( len ); + for ( i = 0; i < arrays.length; i++ ) { + x = arrays[ i ]; + if ( x.accessorProtocol ) { + idx = accessors( x, idx, predicates[ i ] ); + } else { + idx = indexed( x.data, idx, predicates[ i ] ); + } + } + return idx.length; +} + + +// EXPORTS // + +module.exports = countIfs; diff --git a/lib/node_modules/@stdlib/array/base/count-ifs/package.json b/lib/node_modules/@stdlib/array/base/count-ifs/package.json new file mode 100644 index 000000000000..d09fedd5ebd4 --- /dev/null +++ b/lib/node_modules/@stdlib/array/base/count-ifs/package.json @@ -0,0 +1,63 @@ +{ + "name": "@stdlib/array/base/count-ifs", + "version": "0.0.0", + "description": "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`.", + "license": "Apache-2.0", + "author": { + "name": "The Stdlib Authors", + "url": "https://github.com/stdlib-js/stdlib/graphs/contributors" + }, + "contributors": [ + { + "name": "The Stdlib Authors", + "url": "https://github.com/stdlib-js/stdlib/graphs/contributors" + } + ], + "main": "./lib", + "directories": { + "benchmark": "./benchmark", + "doc": "./docs", + "example": "./examples", + "lib": "./lib", + "test": "./test" + }, + "types": "./docs/types", + "scripts": {}, + "homepage": "https://github.com/stdlib-js/stdlib", + "repository": { + "type": "git", + "url": "git://github.com/stdlib-js/stdlib.git" + }, + "bugs": { + "url": "https://github.com/stdlib-js/stdlib/issues" + }, + "dependencies": {}, + "devDependencies": {}, + "engines": { + "node": ">=0.10.0", + "npm": ">2.7.0" + }, + "os": [ + "aix", + "darwin", + "freebsd", + "linux", + "macos", + "openbsd", + "sunos", + "win32", + "windows" + ], + "keywords": [ + "stdlib", + "array", + "count", + "sum", + "summation", + "countif", + "countifs", + "range", + "total", + "truthy" + ] +} diff --git a/lib/node_modules/@stdlib/array/base/count-ifs/test/test.js b/lib/node_modules/@stdlib/array/base/count-ifs/test/test.js new file mode 100644 index 000000000000..bc5ef4b49f81 --- /dev/null +++ b/lib/node_modules/@stdlib/array/base/count-ifs/test/test.js @@ -0,0 +1,222 @@ +/** +* @license Apache-2.0 +* +* Copyright (c) 2025 The Stdlib Authors. +* +* Licensed under the Apache License, Version 2.0 (the "License"); +* you may not use this file except in compliance with the License. +* You may obtain a copy of the License at +* +* http://www.apache.org/licenses/LICENSE-2.0 +* +* Unless required by applicable law or agreed to in writing, software +* distributed under the License is distributed on an "AS IS" BASIS, +* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. +* See the License for the specific language governing permissions and +* limitations under the License. +*/ + +'use strict'; + +// MODULES // + +var tape = require( 'tape' ); +var Complex128Array = require( '@stdlib/array/complex128' ); +var Int32Array = require( '@stdlib/array/int32' ); +var toAccessorArray = require( '@stdlib/array/base/to-accessor-array' ); +var real = require( '@stdlib/complex/float64/real' ); +var imag = require( '@stdlib/complex/float64/imag' ); +var countIfs = require( './../lib' ); + + +// FUNCTIONS // + +function predicate0( value ) { + return value > 0; +} + +function predicate1( value ) { + return value < 0; +} + +function predicate2( value ) { + return value < 3; +} + + +// TESTS // + +tape( 'main export is a function', function test( t ) { + t.ok( true, __filename ); + t.strictEqual( typeof countIfs, 'function', 'main export is a function' ); + t.end(); +}); + +tape( 'the function counts the number of elements in the input arrays which simultaneously pass tests implemented by predicate functions (narrays=1,generic)', function test( t ) { + var actual; + var x0; + + x0 = [ 0, 1, 0, 1, 2 ]; + actual = countIfs( x0, predicate0 ); + + t.strictEqual( actual, 3, 'returns expected value' ); + t.end(); +}); + +tape( 'the function counts the number of elements in the input arrays which simultaneously pass tests implemented by predicate functions (narrays=1,accessors)', function test( t ) { + var actual; + var x0; + + x0 = toAccessorArray( [ 0, 1, 0, 1, 2 ] ); + actual = countIfs( x0, predicate0 ); + + t.strictEqual( actual, 3, 'returns expected value' ); + t.end(); +}); + +tape( 'the function counts the number of elements in the input arrays which simultaneously pass tests implemented by predicate functions (narrays=2,generic)', function test( t ) { + var actual; + var x0; + var x1; + + x0 = [ 0, 1, 0, 1, 2 ]; + x1 = [ 3, -4, -1, 2, -8 ]; + actual = countIfs( x0, predicate0, x1, predicate1 ); + + t.strictEqual( actual, 2, 'returns expected value' ); + t.end(); +}); + +tape( 'the function counts the number of elements in the input arrays which simultaneously pass tests implemented by predicate functions (narrays=2,accessors)', function test( t ) { + var actual; + var x0; + var x1; + + x0 = toAccessorArray( [ 0, 1, 0, 1, 2 ] ); + x1 = toAccessorArray( [ 3, -4, -1, 2, -8 ] ); + actual = countIfs( x0, predicate0, x1, predicate1 ); + + t.strictEqual( actual, 2, 'returns expected value' ); + t.end(); +}); + +tape( 'the function counts the number of elements in the input arrays which simultaneously pass tests implemented by predicate functions (narrays=2,real typed array)', function test( t ) { + var actual; + var x0; + var x1; + + x0 = new Int32Array( [ 0, 1, 0, 1, 2 ] ); + x1 = new Int32Array( [ 3, -4, -1, 2, -8 ] ); + actual = countIfs( x0, predicate0, x1, predicate1 ); + + t.strictEqual( actual, 2, 'returns expected value' ); + t.end(); +}); + +tape( 'the function counts the number of elements in the input arrays which simultaneously pass tests implemented by predicate functions (narrays=2,complex typed array)', function test( t ) { + var actual; + var x0; + var x1; + + x0 = new Complex128Array( [ 3.0, 1.0, 1.0, 0.0, 3.0, 4.0, 0.0, 5.0 ] ); + x1 = new Complex128Array( [ -2.0, -5.0, -3.0, 1.0, -5.0, -1.0, 0.0, 4.0 ] ); + actual = countIfs( x0, predicate0, x1, predicate1 ); + + t.strictEqual( actual, 2, 'returns expected value' ); + t.end(); + + function predicate0( value ) { + return ( + real( value ) > 0 && + imag( value ) > 0 + ); + } + function predicate1( value ) { + return ( + real( value ) < 0 && + imag( value ) < 0 + ); + } +}); + +tape( 'the function counts the number of elements in the input arrays which simultaneously pass tests implemented by predicate functions (narrays=3,generic)', function test( t ) { + var actual; + var x0; + var x1; + var x2; + + x0 = [ 0, 1, 0, 1, 2 ]; + x1 = [ 3, -4, -1, 2, -8 ]; + x2 = [ 1, 2, 3, 4, 5 ]; + actual = countIfs( x0, predicate0, x1, predicate1, x2, predicate2 ); + + t.strictEqual( actual, 1, 'returns expected value' ); + t.end(); +}); + +tape( 'the function counts the number of elements in the input arrays which simultaneously pass tests implemented by predicate functions (narrays=3,accessors)', function test( t ) { + var actual; + var x0; + var x1; + var x2; + + x0 = toAccessorArray( [ 0, 1, 0, 1, 2 ] ); + x1 = toAccessorArray( [ 3, -4, -1, 2, -8 ] ); + x2 = toAccessorArray( [ 1, 2, 3, 4, 5 ] ); + actual = countIfs( x0, predicate0, x1, predicate1, x2, predicate2 ); + + t.strictEqual( actual, 1, 'returns expected value' ); + t.end(); +}); + +tape( 'the function returns `0` if no elements at the same indices pass tests implemented by predicate functions (generic)', function test( t ) { + var actual; + var x0; + var x1; + + x0 = [ -3, -2, -5, -1 ]; + x1 = [ 2, 4, 5, 8 ]; + actual = countIfs( x0, predicate0, x1, predicate1 ); + + t.strictEqual( actual, 0, 'returns expected value' ); + t.end(); +}); + +tape( 'the function returns `0` if no elements at the same indices pass tests implemented by predicate functions (accessors)', function test( t ) { + var actual; + var x0; + var x1; + + x0 = toAccessorArray( [ -3, -2, -5, -1 ] ); + x1 = toAccessorArray( [ 2, 4, 5, 8 ] ); + actual = countIfs( x0, predicate0, x1, predicate1 ); + + t.strictEqual( actual, 0, 'returns expected value' ); + t.end(); +}); + +tape( 'the function returns the number of elements if all elements in every input array pass tests implemented by predicate functions (generic)', function test( t ) { + var actual; + var x0; + var x1; + + x0 = [ 1, 1, 1, 1 ]; + x1 = [ -1, -1, -1, -1 ]; + actual = countIfs( x0, predicate0, x1, predicate1 ); + + t.strictEqual( actual, x0.length, 'returns expected value' ); + t.end(); +}); + +tape( 'the function returns the number of elements if all elements in every input array pass tests implemented by predicate functions (accessors)', function test( t ) { + var actual; + var x0; + var x1; + + x0 = toAccessorArray( [ 1, 1, 1, 1 ] ); + x1 = toAccessorArray( [ -1, -1, -1, -1 ] ); + actual = countIfs( x0, predicate0, x1, predicate1 ); + + t.strictEqual( actual, x0.length, 'returns expected value' ); + t.end(); +});