Skip to content

Commit 6ff153f

Browse files
committed
feat: add ndarray/filter-map
1 parent 8d1be60 commit 6ff153f

File tree

11 files changed

+3942
-0
lines changed

11 files changed

+3942
-0
lines changed
Lines changed: 267 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,267 @@
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+
# filterMap
22+
23+
> Filter and map elements in an input [ndarray][@stdlib/ndarray/ctor] to elements in a new output [ndarray][@stdlib/ndarray/ctor] according to a callback 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 filterMap = require( '@stdlib/ndarray/filter-map' );
37+
```
38+
39+
#### filterMap( x\[, options], fcn\[, thisArg] )
40+
41+
Filters and maps elements in an input [ndarray][@stdlib/ndarray/ctor] to elements in a new output [ndarray][@stdlib/ndarray/ctor] according to a callback 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 fcn( z ) {
51+
if ( z > 5.0 ) {
52+
return z * 10.0;
53+
}
54+
}
55+
56+
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 ] );
57+
var shape = [ 2, 3 ];
58+
var strides = [ 6, 1 ];
59+
var offset = 1;
60+
61+
var x = ndarray( 'float64', buffer, shape, strides, offset, 'row-major' );
62+
// returns <ndarray>
63+
64+
var y = filterMap( x, fcn );
65+
// returns <ndarray>
66+
67+
var arr = ndarray2array( y );
68+
// returns [ 80.0, 90.0, 100.0 ]
69+
```
70+
71+
The function accepts the following arguments:
72+
73+
- **x**: input [ndarray][@stdlib/ndarray/ctor].
74+
- **options**: function options _(optional)_.
75+
- **fcn**: callback function.
76+
- **thisArg**: callback function execution context _(optional)_.
77+
78+
The function accepts the following options:
79+
80+
- **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].
81+
- **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'`.
82+
83+
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.
84+
85+
<!-- eslint-disable max-len -->
86+
87+
```javascript
88+
var Float64Array = require( '@stdlib/array/float64' );
89+
var ndarray = require( '@stdlib/ndarray/ctor' );
90+
var dtype = require( '@stdlib/ndarray/dtype' );
91+
var ndarray2array = require( '@stdlib/ndarray/to-array' );
92+
93+
function fcn( z ) {
94+
if ( z > 5.0 ) {
95+
return z * 10.0;
96+
}
97+
}
98+
99+
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 ] );
100+
var shape = [ 2, 3 ];
101+
var strides = [ 6, 1 ];
102+
var offset = 1;
103+
104+
var x = ndarray( 'float64', buffer, shape, strides, offset, 'row-major' );
105+
// returns <ndarray>
106+
107+
var opts = {
108+
'dtype': 'float32'
109+
};
110+
var y = filterMap( x, opts, fcn );
111+
// returns <ndarray>
112+
113+
var dt = dtype( y );
114+
// returns 'float32'
115+
116+
var arr = ndarray2array( y );
117+
// returns [ 80.0, 90.0, 100.0 ]
118+
```
119+
120+
To set the callback function execution context, provide a `thisArg`.
121+
122+
<!-- eslint-disable no-invalid-this, max-len -->
123+
124+
```javascript
125+
var Float64Array = require( '@stdlib/array/float64' );
126+
var ndarray = require( '@stdlib/ndarray/ctor' );
127+
var ndarray2array = require( '@stdlib/ndarray/to-array' );
128+
129+
function fcn( z ) {
130+
this.count += 1;
131+
if ( z > 5.0 ) {
132+
return z * 10.0;
133+
}
134+
}
135+
136+
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 ] );
137+
var shape = [ 2, 3 ];
138+
var strides = [ 6, 1 ];
139+
var offset = 1;
140+
141+
var x = ndarray( 'float64', buffer, shape, strides, offset, 'row-major' );
142+
// returns <ndarray>
143+
144+
var ctx = {
145+
'count': 0
146+
};
147+
var y = filterMap( x, fcn, ctx );
148+
// returns <ndarray>
149+
150+
var arr = ndarray2array( y );
151+
// returns [ 80.0, 90.0, 100.0 ]
152+
153+
var count = ctx.count;
154+
// returns 6
155+
```
156+
157+
The callback function is provided the following arguments:
158+
159+
- **value**: current array element.
160+
- **indices**: current array element indices.
161+
- **arr**: the input [ndarray][@stdlib/ndarray/ctor].
162+
163+
</section>
164+
165+
<!-- /.usage -->
166+
167+
<section class="notes">
168+
169+
## Notes
170+
171+
- 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 by a provided callback function.
172+
173+
<!-- eslint-disable max-len -->
174+
175+
```javascript
176+
var Float64Array = require( '@stdlib/array/float64' );
177+
var ndarray = require( '@stdlib/ndarray/ctor' );
178+
var Complex128 = require( '@stdlib/complex/float64/ctor' );
179+
var ndarray2array = require( '@stdlib/ndarray/to-array' );
180+
181+
function fcn( z ) {
182+
if ( z > 5.0 ) {
183+
return new Complex128( z, 0.0 );
184+
}
185+
}
186+
187+
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 ] );
188+
var shape = [ 2, 3 ];
189+
var strides = [ 6, 1 ];
190+
var offset = 1;
191+
192+
var x = ndarray( 'float64', buffer, shape, strides, offset, 'row-major' );
193+
// returns <ndarray>
194+
195+
var opts = {
196+
'dtype': 'complex128'
197+
};
198+
var y = filterMap( x, opts, fcn );
199+
// returns <ndarray>
200+
```
201+
202+
- If a provided callback function returns `undefined`, the function skips the respective [ndarray][@stdlib/ndarray/ctor] element. If the callback function returns a value other than `undefined`, the function stores the callback's return value in the output [ndarray][@stdlib/ndarray/ctor].
203+
204+
- The function **always** returns a one-dimensional [ndarray][@stdlib/ndarray/ctor].
205+
206+
</section>
207+
208+
<!-- /.notes -->
209+
210+
<section class="examples">
211+
212+
## Examples
213+
214+
<!-- eslint no-undef: "error" -->
215+
216+
```javascript
217+
var discreteUniform = require( '@stdlib/random/array/discrete-uniform' );
218+
var ndarray2array = require( '@stdlib/ndarray/to-array' );
219+
var array = require( '@stdlib/ndarray/array' );
220+
var filterMap = require( '@stdlib/ndarray/filter-map' );
221+
222+
function fcn( v ) {
223+
if ( v > 0 ) {
224+
return v * 100;
225+
}
226+
}
227+
228+
var buffer = discreteUniform( 10, -100, 100, {
229+
'dtype': 'generic'
230+
});
231+
var x = array( buffer, {
232+
'shape': [ 5, 2 ],
233+
'dtype': 'generic'
234+
});
235+
console.log( ndarray2array( x ) );
236+
237+
var y = filterMap( x, fcn );
238+
console.log( ndarray2array( y ) );
239+
```
240+
241+
</section>
242+
243+
<!-- /.examples -->
244+
245+
<!-- Section for related `stdlib` packages. Do not manually edit this section, as it is automatically populated. -->
246+
247+
<section class="related">
248+
249+
</section>
250+
251+
<!-- /.related -->
252+
253+
<section class="links">
254+
255+
[@stdlib/ndarray/ctor]: https://github.com/stdlib-js/stdlib/tree/develop/lib/node_modules/%40stdlib/ndarray/ctor
256+
257+
[@stdlib/ndarray/dtypes]: https://github.com/stdlib-js/stdlib/tree/develop/lib/node_modules/%40stdlib/ndarray/dtypes
258+
259+
[@stdlib/ndarray/orders]: https://github.com/stdlib-js/stdlib/tree/develop/lib/node_modules/%40stdlib/ndarray/orders
260+
261+
<!-- <related-links> -->
262+
263+
<!-- </related-links> -->
264+
265+
</section>
266+
267+
<!-- /.links -->

0 commit comments

Comments
 (0)