Skip to content

Commit 3682b36

Browse files
committed
feat: add ndarray/every
--- type: pre_push_report description: Results of running various checks prior to pushing changes. report: - task: run_javascript_examples status: na - task: run_c_examples status: na - task: run_cpp_examples status: na - task: run_javascript_readme_examples status: na - task: run_c_benchmarks status: na - task: run_cpp_benchmarks status: na - task: run_fortran_benchmarks status: na - task: run_javascript_benchmarks status: na - task: run_julia_benchmarks status: na - task: run_python_benchmarks status: na - task: run_r_benchmarks status: na - task: run_javascript_tests status: na ---
1 parent 0734a5f commit 3682b36

File tree

11 files changed

+1600
-0
lines changed

11 files changed

+1600
-0
lines changed
Lines changed: 181 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,181 @@
1+
<!--
2+
3+
@license Apache-2.0
4+
5+
Copyright (c) 2024 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+
> Return a boolean indicating whether every element in the [ndarray][@stdlib/ndarray/ctor] passes 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 every = require( '@stdlib/ndarray/every' );
37+
```
38+
39+
#### every( x, predicate\[, thisArg] )
40+
41+
Return a boolean indicating whether every element in the [ndarray][@stdlib/ndarray/ctor] passes a test implemented by a predicate function.
42+
43+
```javascript
44+
var Float64Array = require( '@stdlib/array/float64' );
45+
var ndarray = require( '@stdlib/ndarray/ctor' );
46+
var ndarray2array = require( '@stdlib/ndarray/to-array' );
47+
48+
function predicate( z ) {
49+
return z > 6.0;
50+
}
51+
52+
var buffer = 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 ] );
53+
var shape = [ 2, 3 ];
54+
var strides = [ 6, 1 ];
55+
var offset = 1;
56+
57+
var x = ndarray( 'float64', buffer, shape, strides, offset, 'row-major' );
58+
// returns <ndarray>
59+
60+
var arr = ndarray2array( x );
61+
// returns [ [ 2.0, 3.0, 4.0 ], [ 8.0, 9.0, 10.0 ] ]
62+
63+
var y = every( x, predicate );
64+
// returns false
65+
```
66+
67+
The function accepts the following arguments:
68+
69+
- **x**: input [ndarray][@stdlib/ndarray/ctor].
70+
- **predicate**: predicate function.
71+
- **thisArg**: predicate function execution context _(optional)_.
72+
73+
To set the `predicate` function execution context, provide a `thisArg`.
74+
75+
<!-- eslint-disable no-invalid-this, max-len -->
76+
77+
```javascript
78+
var Float64Array = require( '@stdlib/array/float64' );
79+
var ndarray = require( '@stdlib/ndarray/ctor' );
80+
var ndarray2array = require( '@stdlib/ndarray/to-array' );
81+
82+
function predicate( z ) {
83+
this.count += 1;
84+
return z > 6.0;
85+
}
86+
87+
var buffer = 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 ] );
88+
var shape = [ 2, 3 ];
89+
var strides = [ 6, 1 ];
90+
var offset = 1;
91+
92+
var x = ndarray( 'float64', buffer, shape, strides, offset, 'row-major' );
93+
// returns <ndarray>
94+
95+
var arr = ndarray2array( x );
96+
// returns [ [ 2.0, 3.0, 4.0 ], [ 8.0, 9.0, 10.0 ] ]
97+
98+
var ctx = {
99+
'count': 0
100+
};
101+
var y = every( x, predicate, ctx );
102+
// returns false
103+
104+
var count = ctx.count;
105+
// returns 4
106+
```
107+
108+
The `predicate` function is provided the following arguments:
109+
110+
- **value**: current array element.
111+
- **indices**: current array element indices.
112+
- **arr**: the input [ndarray][@stdlib/ndarray/ctor].
113+
114+
</section>
115+
116+
<!-- /.usage -->
117+
118+
<section class="notes">
119+
120+
## Notes
121+
122+
- The function **always** returns a boolean.
123+
124+
</section>
125+
126+
<!-- /.notes -->
127+
128+
<section class="examples">
129+
130+
## Examples
131+
132+
<!-- eslint no-undef: "error" -->
133+
134+
```javascript
135+
var discreteUniform = require( '@stdlib/random/array/discrete-uniform' );
136+
var ndarray2array = require( '@stdlib/ndarray/to-array' );
137+
var naryFunction = require( '@stdlib/utils/nary-function' );
138+
var array = require( '@stdlib/ndarray/array' );
139+
var isPositive = require( '@stdlib/assert/is-positive-number' ).isPrimitive;
140+
var every = require( '@stdlib/ndarray/every' );
141+
142+
var buffer = discreteUniform( 10, -100, 100, {
143+
'dtype': 'generic'
144+
});
145+
var x = array( buffer, {
146+
'shape': [ 5, 2 ],
147+
'dtype': 'generic'
148+
});
149+
console.log( ndarray2array( x ) );
150+
151+
var y = every( x, naryFunction( isPositive, 1 ) );
152+
console.log( y );
153+
```
154+
155+
</section>
156+
157+
<!-- /.examples -->
158+
159+
<!-- Section for related `stdlib` packages. Do not manually edit this section, as it is automatically populated. -->
160+
161+
<section class="related">
162+
163+
</section>
164+
165+
<!-- /.related -->
166+
167+
<section class="links">
168+
169+
[@stdlib/ndarray/ctor]: https://github.com/stdlib-js/stdlib/tree/develop/lib/node_modules/%40stdlib/ndarray/ctor
170+
171+
[@stdlib/ndarray/dtypes]: https://github.com/stdlib-js/stdlib/tree/develop/lib/node_modules/%40stdlib/ndarray/dtypes
172+
173+
[@stdlib/ndarray/orders]: https://github.com/stdlib-js/stdlib/tree/develop/lib/node_modules/%40stdlib/ndarray/orders
174+
175+
<!-- <related-links> -->
176+
177+
<!-- </related-links> -->
178+
179+
</section>
180+
181+
<!-- /.links -->
Lines changed: 141 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,141 @@
1+
/**
2+
* @license Apache-2.0
3+
*
4+
* Copyright (c) 2024 The Stdlib Authors.
5+
*
6+
* Licensed under the Apache License, Version 2.0 (the "License");
7+
* you may not use this file except in compliance with the License.
8+
* You may obtain a copy of the License at
9+
*
10+
* http://www.apache.org/licenses/LICENSE-2.0
11+
*
12+
* Unless required by applicable law or agreed to in writing, software
13+
* distributed under the License is distributed on an "AS IS" BASIS,
14+
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
15+
* See the License for the specific language governing permissions and
16+
* limitations under the License.
17+
*/
18+
19+
'use strict';
20+
21+
// MODULES //
22+
23+
var bench = require( '@stdlib/bench' );
24+
var isnan = require( '@stdlib/math/base/assert/is-nan' );
25+
var pow = require( '@stdlib/math/base/special/pow' );
26+
var isBoolean = require( '@stdlib/assert/is-boolean' );
27+
var discreteUniform = require( '@stdlib/random/array/discrete-uniform' );
28+
var shape2strides = require( '@stdlib/ndarray/base/shape2strides' );
29+
var ndarray = require( '@stdlib/ndarray/ctor' );
30+
var pkg = require( './../package.json' ).name;
31+
var every = require( './../lib' );
32+
33+
34+
// VARIABLES //
35+
36+
var xtypes = [ 'generic' ];
37+
var orders = [ 'row-major', 'column-major' ];
38+
39+
40+
// FUNCTIONS //
41+
42+
/**
43+
* Predicate function.
44+
*
45+
* @private
46+
* @param {number} value - array element
47+
* @param {NonNegativeIntegerArray} indices - element indices
48+
* @param {ndarray} arr - input array
49+
* @returns {boolean} result
50+
*/
51+
function predicate( value ) {
52+
return value > 0.0;
53+
}
54+
55+
/**
56+
* Creates a benchmark function.
57+
*
58+
* @private
59+
* @param {PositiveInteger} len - array length
60+
* @param {NonNegativeIntegerArray} shape - ndarray shape
61+
* @param {string} xtype - input ndarray data type
62+
* @returns {Function} benchmark function
63+
*/
64+
function createBenchmark( len, shape, xtype, order ) {
65+
var strides;
66+
var xbuf;
67+
var x;
68+
69+
xbuf = discreteUniform( len, -100, 100, {
70+
'dtype': xtype
71+
});
72+
strides = shape2strides( shape, order );
73+
x = ndarray( xtype, xbuf, shape, strides, 0, order );
74+
75+
return benchmark;
76+
77+
/**
78+
* Benchmark function.
79+
*
80+
* @private
81+
* @param {Benchmark} b - benchmark instance
82+
*/
83+
function benchmark( b ) {
84+
var y;
85+
var i;
86+
87+
b.tic();
88+
for ( i = 0; i < b.iterations; i++ ) {
89+
y = every( x, predicate );
90+
if ( isnan( y ) ) {
91+
b.fail( 'should not return NaN' );
92+
}
93+
}
94+
b.toc();
95+
if ( !isBoolean( y ) ) {
96+
b.fail( 'should return an boolean' );
97+
}
98+
b.pass( 'benchmark finished' );
99+
b.end();
100+
}
101+
}
102+
103+
104+
// MAIN //
105+
106+
/**
107+
* Main execution sequence.
108+
*
109+
* @private
110+
*/
111+
function main() {
112+
var len;
113+
var min;
114+
var max;
115+
var ord;
116+
var sh;
117+
var t1;
118+
var f;
119+
var i;
120+
var j;
121+
var k;
122+
123+
min = 1; // 10^min
124+
max = 6; // 10^max
125+
126+
for ( k = 0; k < orders.length; k++ ) {
127+
ord = orders[ k ];
128+
for ( j = 0; j < xtypes.length; j++ ) {
129+
t1 = xtypes[ j ];
130+
for ( i = min; i <= max; i++ ) {
131+
len = pow( 10, i );
132+
133+
sh = [ len ];
134+
f = createBenchmark( len, sh, t1, ord );
135+
bench( pkg+':ndims='+sh.length+',len='+len+',shape=['+sh.join(',')+'],xorder='+ord+',yorder='+ord+',xtype='+t1, f );
136+
}
137+
}
138+
}
139+
}
140+
141+
main();

0 commit comments

Comments
 (0)