@stdlib/ndarray-filter 0.1.0
This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
- package/LICENSE +177 -0
- package/NOTICE +1 -0
- package/README.md +321 -0
- package/SECURITY.md +5 -0
- package/dist/index.d.ts +3 -0
- package/dist/index.js +19 -0
- package/dist/index.js.map +7 -0
- package/docs/types/index.d.ts +1478 -0
- package/lib/index.js +51 -0
- package/lib/main.js +192 -0
- package/package.json +81 -0
package/lib/index.js
ADDED
|
@@ -0,0 +1,51 @@
|
|
|
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
|
+
/**
|
|
22
|
+
* Return a shallow copy of an ndarray containing only those elements which pass a test implemented by a predicate function.
|
|
23
|
+
*
|
|
24
|
+
* @module @stdlib/ndarray-filter
|
|
25
|
+
*
|
|
26
|
+
* @example
|
|
27
|
+
* var isEven = require( '@stdlib/assert-is-even' ).isPrimitive;
|
|
28
|
+
* var Float64Array = require( '@stdlib/array-float64' );
|
|
29
|
+
* var ndarray = require( '@stdlib/ndarray-ctor' );
|
|
30
|
+
* var filter = require( '@stdlib/ndarray-filter' );
|
|
31
|
+
*
|
|
32
|
+
* 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 ] );
|
|
33
|
+
* var shape = [ 2, 3 ];
|
|
34
|
+
* var strides = [ 6, 1 ];
|
|
35
|
+
* var offset = 1;
|
|
36
|
+
*
|
|
37
|
+
* var x = ndarray( 'float64', buffer, shape, strides, offset, 'row-major' );
|
|
38
|
+
* // returns <ndarray>
|
|
39
|
+
*
|
|
40
|
+
* var y = filter( x, isEven );
|
|
41
|
+
* // returns <ndarray>[ 2.0, 4.0, 8.0, 10.0 ]
|
|
42
|
+
*/
|
|
43
|
+
|
|
44
|
+
// MODULES //
|
|
45
|
+
|
|
46
|
+
var main = require( './main.js' );
|
|
47
|
+
|
|
48
|
+
|
|
49
|
+
// EXPORTS //
|
|
50
|
+
|
|
51
|
+
module.exports = main;
|
package/lib/main.js
ADDED
|
@@ -0,0 +1,192 @@
|
|
|
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 isPlainObject = require( '@stdlib/assert-is-plain-object' );
|
|
24
|
+
var isFunction = require( '@stdlib/assert-is-function' );
|
|
25
|
+
var isndarrayLike = require( '@stdlib/assert-is-ndarray-like' );
|
|
26
|
+
var isOrder = require( '@stdlib/ndarray-base-assert-is-order' );
|
|
27
|
+
var hasOwnProp = require( '@stdlib/assert-has-own-property' );
|
|
28
|
+
var ctors = require( '@stdlib/ndarray-base-buffer-ctors' );
|
|
29
|
+
var zeros = require( '@stdlib/array-base-zeros' );
|
|
30
|
+
var getShape = require( '@stdlib/ndarray-shape' );
|
|
31
|
+
var getDType = require( '@stdlib/ndarray-dtype' );
|
|
32
|
+
var getOrder = require( '@stdlib/ndarray-order' );
|
|
33
|
+
var numel = require( '@stdlib/ndarray-base-numel' );
|
|
34
|
+
var nextCartesianIndex = require( '@stdlib/ndarray-base-next-cartesian-index' ).assign;
|
|
35
|
+
var gcopy = require( '@stdlib/blas-base-gcopy' );
|
|
36
|
+
var format = require( '@stdlib/string-format' );
|
|
37
|
+
|
|
38
|
+
|
|
39
|
+
// MAIN //
|
|
40
|
+
|
|
41
|
+
/**
|
|
42
|
+
* Returns a shallow copy of an ndarray containing only those elements which pass a test implemented by a predicate function.
|
|
43
|
+
*
|
|
44
|
+
* @param {ndarray} x - input ndarray
|
|
45
|
+
* @param {Options} [options] - function options
|
|
46
|
+
* @param {string} [options.dtype] - output array data type
|
|
47
|
+
* @param {boolean} [options.order] - index iteration order
|
|
48
|
+
* @param {Callback} predicate - predicate function
|
|
49
|
+
* @param {*} [thisArg] - predicate execution context
|
|
50
|
+
* @throws {TypeError} first argument must be an ndarray-like object
|
|
51
|
+
* @throws {TypeError} callback argument must be a function
|
|
52
|
+
* @throws {TypeError} options argument must be an object
|
|
53
|
+
* @returns {ndarray} output ndarray
|
|
54
|
+
*
|
|
55
|
+
* @example
|
|
56
|
+
* var isEven = require( '@stdlib/assert-is-even' ).isPrimitive;
|
|
57
|
+
* var Float64Array = require( '@stdlib/array-float64' );
|
|
58
|
+
* var ndarray = require( '@stdlib/ndarray-ctor' );
|
|
59
|
+
*
|
|
60
|
+
* 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 ] );
|
|
61
|
+
* var shape = [ 2, 3 ];
|
|
62
|
+
* var strides = [ 6, 1 ];
|
|
63
|
+
* var offset = 1;
|
|
64
|
+
*
|
|
65
|
+
* var x = ndarray( 'float64', buffer, shape, strides, offset, 'row-major' );
|
|
66
|
+
* // returns <ndarray>
|
|
67
|
+
*
|
|
68
|
+
* var y = filter( x, isEven );
|
|
69
|
+
* // returns <ndarray>[ 2.0, 4.0, 8.0, 10.0 ]
|
|
70
|
+
*/
|
|
71
|
+
function filter( x, options, predicate, thisArg ) {
|
|
72
|
+
var hasOpts;
|
|
73
|
+
var ndims;
|
|
74
|
+
var cache;
|
|
75
|
+
var clbk;
|
|
76
|
+
var opts;
|
|
77
|
+
var ctor;
|
|
78
|
+
var ctx;
|
|
79
|
+
var ord;
|
|
80
|
+
var dim;
|
|
81
|
+
var idx;
|
|
82
|
+
var buf;
|
|
83
|
+
var dt;
|
|
84
|
+
var sh;
|
|
85
|
+
var N;
|
|
86
|
+
var y;
|
|
87
|
+
var v;
|
|
88
|
+
var i;
|
|
89
|
+
if ( !isndarrayLike( x ) ) {
|
|
90
|
+
throw new TypeError( format( 'invalid argument. First argument must be an ndarray-like object. Value: `%s`.', x ) );
|
|
91
|
+
}
|
|
92
|
+
if ( arguments.length < 3 ) {
|
|
93
|
+
clbk = options;
|
|
94
|
+
} else if ( arguments.length === 3 ) {
|
|
95
|
+
if ( isFunction( options ) ) {
|
|
96
|
+
clbk = options;
|
|
97
|
+
ctx = predicate;
|
|
98
|
+
} else {
|
|
99
|
+
hasOpts = true;
|
|
100
|
+
opts = options;
|
|
101
|
+
clbk = predicate;
|
|
102
|
+
}
|
|
103
|
+
} else {
|
|
104
|
+
hasOpts = true;
|
|
105
|
+
opts = options;
|
|
106
|
+
clbk = predicate;
|
|
107
|
+
ctx = thisArg;
|
|
108
|
+
}
|
|
109
|
+
if ( !isFunction( clbk ) ) {
|
|
110
|
+
throw new TypeError( format( 'invalid argument. Callback argument must be a function. Value: `%s`.', clbk ) );
|
|
111
|
+
}
|
|
112
|
+
if ( hasOpts ) {
|
|
113
|
+
if ( !isPlainObject( opts ) ) {
|
|
114
|
+
throw new TypeError( format( 'invalid argument. Options argument must be an object. Value: `%s`.', opts ) );
|
|
115
|
+
}
|
|
116
|
+
if ( hasOwnProp( opts, 'dtype' ) ) {
|
|
117
|
+
dt = opts.dtype;
|
|
118
|
+
} else {
|
|
119
|
+
dt = getDType( x );
|
|
120
|
+
}
|
|
121
|
+
if ( hasOwnProp( opts, 'order' ) ) {
|
|
122
|
+
if ( !isOrder( opts.order ) ) {
|
|
123
|
+
throw new TypeError( format( 'invalid option. `%s` option must be a recognized order. Option: `%s`.', 'order', opts.order ) );
|
|
124
|
+
}
|
|
125
|
+
ord = opts.order;
|
|
126
|
+
}
|
|
127
|
+
} else {
|
|
128
|
+
dt = getDType( x );
|
|
129
|
+
}
|
|
130
|
+
// Resolve an output array buffer constructor:
|
|
131
|
+
ctor = ctors( dt );
|
|
132
|
+
if ( ctor === null ) {
|
|
133
|
+
// The only way we should get here is if the user provided an unsupported data type, as `getDType` should error if the input array has an unrecognized/unsupported data type...
|
|
134
|
+
throw new TypeError( format( 'invalid option. `%s` option must be a recognized data type. Option: `%s`.', 'dtype', opts.dtype ) );
|
|
135
|
+
}
|
|
136
|
+
// Resolve the iteration order:
|
|
137
|
+
if ( ord === void 0 ) {
|
|
138
|
+
ord = getOrder( x );
|
|
139
|
+
}
|
|
140
|
+
// Resolve the input array shape:
|
|
141
|
+
sh = getShape( x );
|
|
142
|
+
|
|
143
|
+
// Compute the number of array elements:
|
|
144
|
+
N = numel( sh );
|
|
145
|
+
|
|
146
|
+
// Retrieve the number of dimensions:
|
|
147
|
+
ndims = sh.length;
|
|
148
|
+
|
|
149
|
+
// Resolve the dimension in which indices should iterate fastest:
|
|
150
|
+
if ( ord === 'row-major' ) {
|
|
151
|
+
dim = ndims - 1;
|
|
152
|
+
} else { // ord === 'column-major'
|
|
153
|
+
dim = 0;
|
|
154
|
+
}
|
|
155
|
+
// Initialize an index array workspace:
|
|
156
|
+
idx = zeros( ndims );
|
|
157
|
+
|
|
158
|
+
// Initialize a value cache for those elements which pass a predicate function (note: unfortunately, we use an associative array here, as no other good options. If we use a "generic" array, we are limited to 2^32-1 elements. If we allocate, say, a Float64Array buffer for storing indices, we risk materializing lazily-materialized input ndarray values again (e.g., lazy accessor ndarray), which could be expensive. If we allocate a workspace buffer of equal size to the input ndarray to store materialized values, we'd then need to perform another copy in order to shrink the buffer, as, otherwise, could be holding on to significantly more memory than needed for the returned ndarray. There are likely other options, but all involve complexity, so the simplest option is used here.):
|
|
159
|
+
cache = {
|
|
160
|
+
'length': 0
|
|
161
|
+
};
|
|
162
|
+
|
|
163
|
+
// Filter elements according to a predicate function...
|
|
164
|
+
for ( i = 0; i < N; i++ ) {
|
|
165
|
+
if ( i > 0 ) {
|
|
166
|
+
idx = nextCartesianIndex( sh, ord, idx, dim, idx );
|
|
167
|
+
}
|
|
168
|
+
v = x.get.apply( x, idx );
|
|
169
|
+
if ( clbk.call( ctx, v, idx.slice(), x ) ) {
|
|
170
|
+
cache[ cache.length ] = v;
|
|
171
|
+
cache.length += 1;
|
|
172
|
+
}
|
|
173
|
+
}
|
|
174
|
+
// Retrieve the number of cached elements:
|
|
175
|
+
N = cache.length;
|
|
176
|
+
|
|
177
|
+
// Allocate an output array buffer:
|
|
178
|
+
buf = new ctor( N );
|
|
179
|
+
|
|
180
|
+
// Copy cached elements to the output array buffer:
|
|
181
|
+
gcopy( N, cache, 1, buf, 1 );
|
|
182
|
+
|
|
183
|
+
// Create an output ndarray:
|
|
184
|
+
y = new x.constructor( dt, buf, [ N ], [ 1 ], 0, ord );
|
|
185
|
+
|
|
186
|
+
return y;
|
|
187
|
+
}
|
|
188
|
+
|
|
189
|
+
|
|
190
|
+
// EXPORTS //
|
|
191
|
+
|
|
192
|
+
module.exports = filter;
|
package/package.json
ADDED
|
@@ -0,0 +1,81 @@
|
|
|
1
|
+
{
|
|
2
|
+
"name": "@stdlib/ndarray-filter",
|
|
3
|
+
"version": "0.1.0",
|
|
4
|
+
"description": "Return a shallow copy of an ndarray containing only those elements which pass a test implemented by a predicate function.",
|
|
5
|
+
"license": "Apache-2.0",
|
|
6
|
+
"author": {
|
|
7
|
+
"name": "The Stdlib Authors",
|
|
8
|
+
"url": "https://github.com/stdlib-js/stdlib/graphs/contributors"
|
|
9
|
+
},
|
|
10
|
+
"contributors": [
|
|
11
|
+
{
|
|
12
|
+
"name": "The Stdlib Authors",
|
|
13
|
+
"url": "https://github.com/stdlib-js/stdlib/graphs/contributors"
|
|
14
|
+
}
|
|
15
|
+
],
|
|
16
|
+
"main": "./lib",
|
|
17
|
+
"directories": {
|
|
18
|
+
"doc": "./docs",
|
|
19
|
+
"lib": "./lib",
|
|
20
|
+
"dist": "./dist"
|
|
21
|
+
},
|
|
22
|
+
"types": "./docs/types",
|
|
23
|
+
"scripts": {},
|
|
24
|
+
"homepage": "https://stdlib.io",
|
|
25
|
+
"repository": {
|
|
26
|
+
"type": "git",
|
|
27
|
+
"url": "git://github.com/stdlib-js/ndarray-filter.git"
|
|
28
|
+
},
|
|
29
|
+
"bugs": {
|
|
30
|
+
"url": "https://github.com/stdlib-js/stdlib/issues"
|
|
31
|
+
},
|
|
32
|
+
"dependencies": {
|
|
33
|
+
"@stdlib/array-base-zeros": "^0.2.2",
|
|
34
|
+
"@stdlib/assert-has-own-property": "^0.2.2",
|
|
35
|
+
"@stdlib/assert-is-function": "^0.2.2",
|
|
36
|
+
"@stdlib/assert-is-ndarray-like": "^0.2.2",
|
|
37
|
+
"@stdlib/assert-is-plain-object": "^0.2.2",
|
|
38
|
+
"@stdlib/blas-base-gcopy": "^0.2.2",
|
|
39
|
+
"@stdlib/ndarray-base-assert-is-order": "^0.2.2",
|
|
40
|
+
"@stdlib/ndarray-base-buffer-ctors": "^0.3.0",
|
|
41
|
+
"@stdlib/ndarray-base-next-cartesian-index": "^0.2.2",
|
|
42
|
+
"@stdlib/ndarray-base-numel": "^0.2.2",
|
|
43
|
+
"@stdlib/ndarray-dtype": "^0.2.2",
|
|
44
|
+
"@stdlib/ndarray-order": "^0.2.2",
|
|
45
|
+
"@stdlib/ndarray-shape": "^0.2.2",
|
|
46
|
+
"@stdlib/string-format": "^0.2.2",
|
|
47
|
+
"@stdlib/error-tools-fmtprodmsg": "^0.2.2"
|
|
48
|
+
},
|
|
49
|
+
"devDependencies": {},
|
|
50
|
+
"engines": {
|
|
51
|
+
"node": ">=0.10.0",
|
|
52
|
+
"npm": ">2.7.0"
|
|
53
|
+
},
|
|
54
|
+
"os": [
|
|
55
|
+
"aix",
|
|
56
|
+
"darwin",
|
|
57
|
+
"freebsd",
|
|
58
|
+
"linux",
|
|
59
|
+
"macos",
|
|
60
|
+
"openbsd",
|
|
61
|
+
"sunos",
|
|
62
|
+
"win32",
|
|
63
|
+
"windows"
|
|
64
|
+
],
|
|
65
|
+
"keywords": [
|
|
66
|
+
"stdlib",
|
|
67
|
+
"strided",
|
|
68
|
+
"array",
|
|
69
|
+
"ndarray",
|
|
70
|
+
"filter",
|
|
71
|
+
"reject",
|
|
72
|
+
"extract",
|
|
73
|
+
"copy",
|
|
74
|
+
"select",
|
|
75
|
+
"take"
|
|
76
|
+
],
|
|
77
|
+
"funding": {
|
|
78
|
+
"type": "opencollective",
|
|
79
|
+
"url": "https://opencollective.com/stdlib"
|
|
80
|
+
}
|
|
81
|
+
}
|