Skip to content

Commit 2690141

Browse files
committed
feat: add ndarray/every
--- 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 c533b6d commit 2690141

File tree

14 files changed

+1776
-0
lines changed

14 files changed

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

0 commit comments

Comments
 (0)