Skip to content

Commit 4e85371

Browse files
committed
feat: add ndarray/count-if
--- type: pre_commit_static_analysis_report description: Results of running static analysis checks when committing changes. report: - task: lint_filenames status: passed - task: lint_editorconfig status: passed - task: lint_markdown status: passed - task: lint_package_json status: passed - task: lint_repl_help status: passed - task: lint_javascript_src status: passed - task: lint_javascript_cli status: na - task: lint_javascript_examples status: passed - task: lint_javascript_tests status: passed - task: lint_javascript_benchmarks status: passed - task: lint_python status: na - task: lint_r status: na - task: lint_c_src status: na - task: lint_c_examples status: na - task: lint_c_benchmarks status: na - task: lint_c_tests_fixtures status: na - task: lint_shell status: na - task: lint_typescript_declarations status: passed - task: lint_typescript_tests status: passed - task: lint_license_headers status: passed ---
1 parent 02934dd commit 4e85371

File tree

14 files changed

+2252
-0
lines changed

14 files changed

+2252
-0
lines changed
Lines changed: 356 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,356 @@
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+
# countIf
22+
23+
> Count the number of truthy elements along one or more [`ndarray`][@stdlib/ndarray/ctor] dimensions.
24+
25+
<section class="intro">
26+
27+
</section>
28+
29+
<!-- /.intro -->
30+
31+
<section class="usage">
32+
33+
## Usage
34+
35+
```javascript
36+
var countIf = require( '@stdlib/ndarray/count-if' );
37+
```
38+
39+
#### countIf( x\[, options], predicate\[, thisArg] )
40+
41+
Counts the number of truthy elements along one or more [`ndarray`][@stdlib/ndarray/ctor] dimensions.
42+
43+
```javascript
44+
var array = require( '@stdlib/ndarray/array' );
45+
46+
function clbk( value ) {
47+
return value > 0.0;
48+
}
49+
50+
// Create an input ndarray:
51+
var x = array( [ [ [ 1.0, 2.0 ] ], [ [ 3.0, 4.0 ] ], [ [ 0.0, 6.0 ] ] ] );
52+
// returns <ndarray>
53+
54+
// Perform operation:
55+
var out = countIf( x, clbk );
56+
// returns <ndarray>
57+
58+
var v = out.get();
59+
// returns 5
60+
```
61+
62+
The function accepts the following arguments:
63+
64+
- **x**: input [`ndarray`][@stdlib/ndarray/ctor].
65+
- **options**: function options (_optional_).
66+
- **predicate**: predicate function.
67+
- **thisArg**: predicate function execution context (_optional_).
68+
69+
The function accepts the following `options`:
70+
71+
- **dims**: list of dimensions over which to perform a reduction.
72+
- **keepdims**: boolean indicating whether the reduced dimensions should be included in the returned [`ndarray`][@stdlib/ndarray/ctor] as singleton dimensions. Default: `false`.
73+
74+
By default, the function performs a reduction over all elements in a provided [`ndarray`][@stdlib/ndarray/ctor]. To reduce specific dimensions, set the `dims` option.
75+
76+
```javascript
77+
var array = require( '@stdlib/ndarray/array' );
78+
var ndarray2array = require( '@stdlib/ndarray/to-array' );
79+
80+
function clbk( value ) {
81+
return value > 0.0;
82+
}
83+
84+
// Create an input ndarray:
85+
var x = array( [ [ [ 1.0, 2.0 ] ], [ [ 3.0, 4.0 ] ], [ [ 0.0, 6.0 ] ] ] );
86+
// returns <ndarray>
87+
88+
// Perform operation:
89+
var opts = {
90+
'dims': [ 1, 2 ]
91+
};
92+
var out = countIf( x, opts, clbk );
93+
// returns <ndarray>
94+
95+
var v = ndarray2array( out );
96+
// returns [ 2, 2, 1 ]
97+
```
98+
99+
By default, the function returns an [`ndarray`][@stdlib/ndarray/ctor] having a shape matching only the non-reduced dimensions of the input [`ndarray`][@stdlib/ndarray/ctor] (i.e., the reduced dimensions are dropped). To include the reduced dimensions as singleton dimensions in the output [`ndarray`][@stdlib/ndarray/ctor], set the `keepdims` option to `true`.
100+
101+
```javascript
102+
var array = require( '@stdlib/ndarray/array' );
103+
var ndarray2array = require( '@stdlib/ndarray/to-array' );
104+
105+
function clbk( value ) {
106+
return value > 0.0;
107+
}
108+
109+
// Create an input ndarray:
110+
var x = array( [ [ [ 1.0, 2.0 ] ], [ [ 3.0, 4.0 ] ], [ [ 0.0, 6.0 ] ] ] );
111+
// returns <ndarray>
112+
113+
// Perform operation:
114+
var opts = {
115+
'dims': [ 1, 2 ],
116+
'keepdims': true
117+
};
118+
var out = countIf( x, opts, clbk );
119+
// returns <ndarray>
120+
121+
var v = ndarray2array( out );
122+
// returns [ [ [ 2 ] ], [ [ 2 ] ], [ [ 1 ] ] ]
123+
```
124+
125+
To set the predicate function execution context, provide a `thisArg`.
126+
127+
<!-- eslint-disable no-invalid-this -->
128+
129+
```javascript
130+
var array = require( '@stdlib/ndarray/array' );
131+
var ndarray2array = require( '@stdlib/ndarray/to-array' );
132+
133+
function clbk( value ) {
134+
this.count += 1;
135+
return value > 0.0;
136+
}
137+
138+
// Create an input ndarray:
139+
var x = array( [ [ [ 1.0, 2.0 ] ], [ [ 3.0, 4.0 ] ], [ [ 0.0, 6.0 ] ] ] );
140+
// returns <ndarray>
141+
142+
// Create a context object:
143+
var ctx = {
144+
'count': 0
145+
};
146+
147+
// Perform operation:
148+
var out = countIf( x, clbk, ctx );
149+
// returns <ndarray>
150+
151+
var v = out.get();
152+
// returns 5
153+
154+
var count = ctx.count;
155+
// returns 6
156+
```
157+
158+
#### countIf.assign( x, out\[, options], predicate\[, thisArg] )
159+
160+
Counts the number of truthy elements along one or more [`ndarray`][@stdlib/ndarray/ctor] dimensions and assigns results to a provided output [`ndarray`][@stdlib/ndarray/ctor].
161+
162+
```javascript
163+
var array = require( '@stdlib/ndarray/array' );
164+
var empty = require( '@stdlib/ndarray/empty' );
165+
166+
function clbk( value ) {
167+
return value > 0.0;
168+
}
169+
170+
// Create an input ndarray:
171+
var x = array( [ [ [ 1.0, 2.0 ] ], [ [ 3.0, 4.0 ] ], [ [ 0.0, 6.0 ] ] ] );
172+
// returns <ndarray>
173+
174+
// Create an output ndarray:
175+
var y = empty( [], {
176+
'dtype': 'int32'
177+
});
178+
179+
// Perform operation:
180+
var out = countIf.assign( x, y, clbk );
181+
// returns <ndarray>
182+
183+
var bool = ( out === y );
184+
// returns true
185+
186+
var v = y.get();
187+
// returns 5
188+
```
189+
190+
The function accepts the following arguments:
191+
192+
- **x**: input [`ndarray`][@stdlib/ndarray/ctor].
193+
- **out**: output [`ndarray`][@stdlib/ndarray/ctor]. The output [`ndarray`][@stdlib/ndarray/ctor] must have a shape matching the non-reduced dimensions of the input [`ndarray`][@stdlib/ndarray/ctor].
194+
- **options**: function options (_optional_).
195+
- **predicate**: predicate function.
196+
- **thisArg**: predicate function execution context (_optional_).
197+
198+
The function accepts the following `options`:
199+
200+
- **dims**: list of dimensions over which to perform a reduction.
201+
202+
By default, the function performs a reduction over all elements in a provided [`ndarray`][@stdlib/ndarray/ctor]. To reduce specific dimensions, set the `dims` option.
203+
204+
```javascript
205+
var array = require( '@stdlib/ndarray/array' );
206+
var empty = require( '@stdlib/ndarray/empty' );
207+
var ndarray2array = require( '@stdlib/ndarray/to-array' );
208+
209+
function clbk( value ) {
210+
return value > 0.0;
211+
}
212+
213+
// Create an input ndarray:
214+
var x = array( [ [ [ 1.0, 2.0 ] ], [ [ 3.0, 4.0 ] ], [ [ 0.0, 6.0 ] ] ] );
215+
// returns <ndarray>
216+
217+
// Create an output ndarray:
218+
var y = empty( [ 3 ], {
219+
'dtype': 'int32'
220+
});
221+
222+
// Perform operation:
223+
var opts = {
224+
'dims': [ 1, 2 ]
225+
};
226+
var out = countIf.assign( x, y, opts, clbk );
227+
228+
var bool = ( out === y );
229+
// returns true
230+
231+
var v = ndarray2array( y );
232+
// returns [ 2, 2, 1 ]
233+
```
234+
235+
</section>
236+
237+
<!-- /.usage -->
238+
239+
<section class="notes">
240+
241+
## Notes
242+
243+
- The predicate function is provided the following arguments:
244+
245+
- **value**: current array element.
246+
- **indices**: current array element indices.
247+
- **arr**: the input ndarray.
248+
249+
</section>
250+
251+
<!-- /.notes -->
252+
253+
<section class="examples">
254+
255+
## Examples
256+
257+
<!-- eslint no-undef: "error" -->
258+
259+
```javascript
260+
var bernoulli = require( '@stdlib/random/base/bernoulli' ).factory;
261+
var ndarray2array = require( '@stdlib/ndarray/to-array' );
262+
var fillBy = require( '@stdlib/ndarray/fill-by' );
263+
var zeros = require( '@stdlib/ndarray/zeros' );
264+
var isPositiveNumber = require( '@stdlib/assert/is-positive-number' ).isPrimitive;
265+
var countIf = require( '@stdlib/ndarray/count-if' );
266+
267+
var x = zeros( [ 2, 4, 5 ], {
268+
'dtype': 'float64'
269+
});
270+
x = fillBy( x, bernoulli( 0.90 ) );
271+
console.log( ndarray2array( x ) );
272+
273+
var y = countIf( x, isPositiveNumber );
274+
console.log( 'countIf(x[:,:,:]) =' );
275+
console.log( y.get() );
276+
277+
var opts = {
278+
'dims': [ 0 ],
279+
'keepdims': true
280+
};
281+
y = countIf( x, opts, isPositiveNumber );
282+
console.log( 'countIf(x[:,j,k]) =' );
283+
console.log( ndarray2array( y ) );
284+
285+
opts = {
286+
'dims': [ 1 ],
287+
'keepdims': true
288+
};
289+
y = countIf( x, opts, isPositiveNumber );
290+
console.log( 'countIf(x[i,:,k]) =' );
291+
console.log( ndarray2array( y ) );
292+
293+
opts = {
294+
'dims': [ 2 ],
295+
'keepdims': true
296+
};
297+
y = countIf( x, opts, isPositiveNumber );
298+
console.log( 'countIf(x[i,j,:]) =' );
299+
console.log( ndarray2array( y ) );
300+
301+
opts = {
302+
'dims': [ 0, 1 ],
303+
'keepdims': true
304+
};
305+
y = countIf( x, opts, isPositiveNumber );
306+
console.log( 'countIf(x[:,:,k]) =' );
307+
console.log( ndarray2array( y ) );
308+
309+
opts = {
310+
'dims': [ 0, 2 ],
311+
'keepdims': true
312+
};
313+
y = countIf( x, opts, isPositiveNumber );
314+
console.log( 'countIf(x[:,j,:]) =' );
315+
console.log( ndarray2array( y ) );
316+
317+
opts = {
318+
'dims': [ 1, 2 ],
319+
'keepdims': true
320+
};
321+
y = countIf( x, opts, isPositiveNumber );
322+
console.log( 'countIf(x[i,:,:]) =' );
323+
console.log( ndarray2array( y ) );
324+
325+
opts = {
326+
'dims': [ 0, 1, 2 ],
327+
'keepdims': true
328+
};
329+
y = countIf( x, opts, isPositiveNumber );
330+
console.log( 'countIf(x[:,:,:]) =' );
331+
console.log( ndarray2array( y ) );
332+
```
333+
334+
</section>
335+
336+
<!-- /.examples -->
337+
338+
<!-- Section for related `stdlib` packages. Do not manually edit this section, as it is automatically populated. -->
339+
340+
<section class="related">
341+
342+
</section>
343+
344+
<!-- /.related -->
345+
346+
<section class="links">
347+
348+
[@stdlib/ndarray/ctor]: https://github.com/stdlib-js/stdlib/tree/develop/lib/node_modules/%40stdlib/ndarray/ctor
349+
350+
<!-- <related-links> -->
351+
352+
<!-- </related-links> -->
353+
354+
</section>
355+
356+
<!-- /.links -->

0 commit comments

Comments
 (0)