Skip to content

Commit 43ccbfb

Browse files
committed
feat: add ndarray/reject
1 parent 1cc3e09 commit 43ccbfb

File tree

11 files changed

+3607
-0
lines changed

11 files changed

+3607
-0
lines changed
Lines changed: 190 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,190 @@
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+
# reject
22+
23+
> Return a shallow copy of an [ndarray][@stdlib/ndarray/ctor] containing only those elements which fail 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 reject = require( '@stdlib/ndarray/reject' );
37+
```
38+
39+
#### reject( x\[, options], predicate\[, thisArg] )
40+
41+
Returns a shallow copy of an [ndarray][@stdlib/ndarray/ctor] containing only those elements which fail a test implemented by a `predicate` function.
42+
43+
<!-- eslint-disable max-len -->
44+
45+
```javascript
46+
var Float64Array = require( '@stdlib/array/float64' );
47+
var ndarray = require( '@stdlib/ndarray/ctor' );
48+
var ndarray2array = require( '@stdlib/ndarray/to-array' );
49+
50+
function predicate( z ) {
51+
return z <= 6.0;
52+
}
53+
54+
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 ] );
55+
var shape = [ 2, 3 ];
56+
var strides = [ 6, 1 ];
57+
var offset = 1;
58+
59+
var x = ndarray( 'float64', buffer, shape, strides, offset, 'row-major' );
60+
// returns <ndarray>
61+
62+
var y = reject( x, predicate );
63+
// returns <ndarray>
64+
65+
var arr = ndarray2array( y );
66+
// returns [ 8.0, 9.0, 10.0 ]
67+
```
68+
69+
The function accepts the following arguments:
70+
71+
- **x**: input [ndarray][@stdlib/ndarray/ctor].
72+
- **options**: function options.
73+
- **predicate**: predicate function.
74+
- **thisArg**: predicate function execution context.
75+
76+
The function accepts the following options:
77+
78+
- **dtype**: output ndarray [data type][@stdlib/ndarray/dtypes]. If not specified, the output ndarray [data type][@stdlib/ndarray/dtypes] is inferred from the input [ndarray][@stdlib/ndarray/ctor].
79+
- **order**: index iteration order. By default, the function iterates over elements according to the [layout order][@stdlib/ndarray/orders] of the provided [ndarray][@stdlib/ndarray/ctor]. Accordingly, for row-major input [ndarrays][@stdlib/ndarray/ctor], the last dimension indices increment fastest. For column-major input [ndarrays][@stdlib/ndarray/ctor], the first dimension indices increment fastest. To override the inferred order and ensure that indices increment in a specific manor, regardless of the input [ndarray][@stdlib/ndarray/ctor]'s layout order, explicitly set the iteration order. Note, however, that iterating according to an order which does not match that of the input [ndarray][@stdlib/ndarray/ctor] may, in some circumstances, result in performance degradation due to cache misses. Must be either `'row-major'` or `'column-major'`.
80+
81+
By default, the output ndarray [data type][@stdlib/ndarray/dtypes] is inferred from the input [ndarray][@stdlib/ndarray/ctor]. To return an ndarray with a different [data type][@stdlib/ndarray/dtypes], specify the `dtype` option.
82+
83+
<!-- eslint-disable max-len -->
84+
85+
```javascript
86+
var Float64Array = require( '@stdlib/array/float64' );
87+
var ndarray = require( '@stdlib/ndarray/ctor' );
88+
var dtype = require( '@stdlib/ndarray/dtype' );
89+
var ndarray2array = require( '@stdlib/ndarray/to-array' );
90+
91+
function predicate( z ) {
92+
return z <= 6.0;
93+
}
94+
95+
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 ] );
96+
var shape = [ 2, 3 ];
97+
var strides = [ 6, 1 ];
98+
var offset = 1;
99+
100+
var x = ndarray( 'float64', buffer, shape, strides, offset, 'row-major' );
101+
// returns <ndarray>
102+
103+
var opts = {
104+
'dtype': 'float32'
105+
};
106+
var y = reject( x, opts, predicate );
107+
// returns <ndarray>
108+
109+
var dt = dtype( y );
110+
// returns 'float32'
111+
112+
var arr = ndarray2array( y );
113+
// returns [ 8.0, 9.0, 10.0 ]
114+
```
115+
116+
The `predicate` function is provided the following arguments:
117+
118+
- **value**: current array element.
119+
- **indices**: current array element indices.
120+
- **arr**: the input [ndarray][@stdlib/ndarray/ctor].
121+
122+
</section>
123+
124+
<!-- /.usage -->
125+
126+
<section class="notes">
127+
128+
## Notes
129+
130+
- The function does **not** perform explicit casting (e.g., from a real-valued floating-point number to a complex floating-point number). Any such casting should be performed **prior to** calling this function.
131+
- The function **always** returns a one-dimensional [ndarray][@stdlib/ndarray/ctor].
132+
133+
</section>
134+
135+
<!-- /.notes -->
136+
137+
<section class="examples">
138+
139+
## Examples
140+
141+
<!-- eslint no-undef: "error" -->
142+
143+
```javascript
144+
var discreteUniform = require( '@stdlib/random/array/discrete-uniform' );
145+
var ndarray2array = require( '@stdlib/ndarray/to-array' );
146+
var naryFunction = require( '@stdlib/utils/nary-function' );
147+
var array = require( '@stdlib/ndarray/array' );
148+
var isPositive = require( '@stdlib/assert/is-positive-number' ).isPrimitive;
149+
var reject = require( '@stdlib/ndarray/reject' );
150+
151+
var buffer = discreteUniform( 10, -100, 100, {
152+
'dtype': 'generic'
153+
});
154+
var x = array( buffer, {
155+
'shape': [ 5, 2 ],
156+
'dtype': 'generic'
157+
});
158+
console.log( ndarray2array( x ) );
159+
160+
var y = reject( x, naryFunction( isPositive, 1 ) );
161+
console.log( ndarray2array( y ) );
162+
```
163+
164+
</section>
165+
166+
<!-- /.examples -->
167+
168+
<!-- Section for related `stdlib` packages. Do not manually edit this section, as it is automatically populated. -->
169+
170+
<section class="related">
171+
172+
</section>
173+
174+
<!-- /.related -->
175+
176+
<section class="links">
177+
178+
[@stdlib/ndarray/ctor]: https://github.com/stdlib-js/stdlib/tree/develop/lib/node_modules/%40stdlib/ndarray/ctor
179+
180+
[@stdlib/ndarray/dtypes]: https://github.com/stdlib-js/stdlib/tree/develop/lib/node_modules/%40stdlib/ndarray/dtypes
181+
182+
[@stdlib/ndarray/orders]: https://github.com/stdlib-js/stdlib/tree/develop/lib/node_modules/%40stdlib/ndarray/orders
183+
184+
<!-- <related-links> -->
185+
186+
<!-- </related-links> -->
187+
188+
</section>
189+
190+
<!-- /.links -->
Lines changed: 150 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,150 @@
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 isndarrayLike = require( '@stdlib/assert/is-ndarray-like' );
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 reject = require( './../lib' );
32+
33+
34+
// VARIABLES //
35+
36+
var xtypes = [ 'generic' ];
37+
var ytypes = [ 'float64' ];
38+
var orders = [ 'row-major', 'column-major' ];
39+
40+
41+
// FUNCTIONS //
42+
43+
/**
44+
* Predicate function.
45+
*
46+
* @private
47+
* @param {number} value - array element
48+
* @param {NonNegativeIntegerArray} indices - element indices
49+
* @param {ndarray} arr - input array
50+
* @returns {boolean} result
51+
*/
52+
function predicate( value ) {
53+
return value > 0.0;
54+
}
55+
56+
/**
57+
* Creates a benchmark function.
58+
*
59+
* @private
60+
* @param {PositiveInteger} len - array length
61+
* @param {NonNegativeIntegerArray} shape - ndarray shape
62+
* @param {string} xtype - input ndarray data type
63+
* @param {string} ytype - output ndarray data type
64+
* @param {string} order - ndarray memory layout
65+
* @returns {Function} benchmark function
66+
*/
67+
function createBenchmark( len, shape, xtype, ytype, order ) {
68+
var strides;
69+
var opts;
70+
var xbuf;
71+
var x;
72+
73+
xbuf = discreteUniform( len, -100, 100, {
74+
'dtype': xtype
75+
});
76+
strides = shape2strides( shape, order );
77+
x = ndarray( xtype, xbuf, shape, strides, 0, order );
78+
opts = {
79+
'dtype': ytype
80+
};
81+
82+
return benchmark;
83+
84+
/**
85+
* Benchmark function.
86+
*
87+
* @private
88+
* @param {Benchmark} b - benchmark instance
89+
*/
90+
function benchmark( b ) {
91+
var y;
92+
var i;
93+
94+
b.tic();
95+
for ( i = 0; i < b.iterations; i++ ) {
96+
y = reject( x, opts, predicate );
97+
if ( isnan( y.data[ i%y.length ] ) ) {
98+
b.fail( 'should not return NaN' );
99+
}
100+
}
101+
b.toc();
102+
if ( !isndarrayLike( y ) ) {
103+
b.fail( 'should return an ndarray' );
104+
}
105+
b.pass( 'benchmark finished' );
106+
b.end();
107+
}
108+
}
109+
110+
111+
// MAIN //
112+
113+
/**
114+
* Main execution sequence.
115+
*
116+
* @private
117+
*/
118+
function main() {
119+
var len;
120+
var min;
121+
var max;
122+
var ord;
123+
var sh;
124+
var t1;
125+
var t2;
126+
var f;
127+
var i;
128+
var j;
129+
var k;
130+
131+
min = 1; // 10^min
132+
max = 6; // 10^max
133+
134+
for ( k = 0; k < orders.length; k++ ) {
135+
ord = orders[ k ];
136+
for ( j = 0; j < xtypes.length; j++ ) {
137+
t1 = xtypes[ j ];
138+
t2 = ytypes[ j ];
139+
for ( i = min; i <= max; i++ ) {
140+
len = pow( 10, i );
141+
142+
sh = [ len ];
143+
f = createBenchmark( len, sh, t1, t2, ord );
144+
bench( pkg+':ndims='+sh.length+',len='+len+',shape=['+sh.join(',')+'],xorder='+ord+',yorder='+ord+',xtype='+t1+',ytype='+t2, f );
145+
}
146+
}
147+
}
148+
}
149+
150+
main();

0 commit comments

Comments
 (0)