@stdlib/ndarray-base-binary-reduce-strided1d 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 +307 -0
- package/SECURITY.md +5 -0
- package/dist/index.js +101 -0
- package/dist/index.js.map +7 -0
- package/lib/0d.js +118 -0
- package/lib/0d_accessors.js +124 -0
- package/lib/10d.js +365 -0
- package/lib/10d_accessors.js +374 -0
- package/lib/10d_blocked.js +429 -0
- package/lib/10d_blocked_accessors.js +438 -0
- package/lib/1d.js +186 -0
- package/lib/1d_accessors.js +195 -0
- package/lib/2d.js +221 -0
- package/lib/2d_accessors.js +230 -0
- package/lib/2d_blocked.js +245 -0
- package/lib/2d_blocked_accessors.js +254 -0
- package/lib/3d.js +239 -0
- package/lib/3d_accessors.js +248 -0
- package/lib/3d_blocked.js +268 -0
- package/lib/3d_blocked_accessors.js +277 -0
- package/lib/4d.js +257 -0
- package/lib/4d_accessors.js +266 -0
- package/lib/4d_blocked.js +291 -0
- package/lib/4d_blocked_accessors.js +300 -0
- package/lib/5d.js +275 -0
- package/lib/5d_accessors.js +284 -0
- package/lib/5d_blocked.js +314 -0
- package/lib/5d_blocked_accessors.js +323 -0
- package/lib/6d.js +293 -0
- package/lib/6d_accessors.js +302 -0
- package/lib/6d_blocked.js +337 -0
- package/lib/6d_blocked_accessors.js +346 -0
- package/lib/7d.js +311 -0
- package/lib/7d_accessors.js +320 -0
- package/lib/7d_blocked.js +360 -0
- package/lib/7d_blocked_accessors.js +369 -0
- package/lib/8d.js +329 -0
- package/lib/8d_accessors.js +338 -0
- package/lib/8d_blocked.js +383 -0
- package/lib/8d_blocked_accessors.js +392 -0
- package/lib/9d.js +347 -0
- package/lib/9d_accessors.js +356 -0
- package/lib/9d_blocked.js +406 -0
- package/lib/9d_blocked_accessors.js +415 -0
- package/lib/factory.js +133 -0
- package/lib/increment_offsets.js +46 -0
- package/lib/index.js +175 -0
- package/lib/initialize_array_views.js +57 -0
- package/lib/main.js +573 -0
- package/lib/nd.js +189 -0
- package/lib/nd_accessors.js +198 -0
- package/lib/offsets.js +42 -0
- package/lib/reshape_strategy.js +260 -0
- package/lib/set_view_offsets.js +52 -0
- package/package.json +91 -0
package/lib/main.js
ADDED
|
@@ -0,0 +1,573 @@
|
|
|
1
|
+
/**
|
|
2
|
+
* @license Apache-2.0
|
|
3
|
+
*
|
|
4
|
+
* Copyright (c) 2025 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 ndarray2object = require( '@stdlib/ndarray-base-ndarraylike2object' );
|
|
24
|
+
var normalizeIndices = require( '@stdlib/ndarray-base-to-unique-normalized-indices' );
|
|
25
|
+
var indicesComplement = require( '@stdlib/array-base-indices-complement' );
|
|
26
|
+
var takeIndexed2 = require( '@stdlib/array-base-take-indexed2' );
|
|
27
|
+
var iterationOrder = require( '@stdlib/ndarray-base-iteration-order' );
|
|
28
|
+
var strides2order = require( '@stdlib/ndarray-base-strides2order' );
|
|
29
|
+
var anyIsEntryIn = require( '@stdlib/array-base-any-is-entry-in' );
|
|
30
|
+
var numel = require( '@stdlib/ndarray-base-numel' );
|
|
31
|
+
var join = require( '@stdlib/array-base-join' );
|
|
32
|
+
var format = require( '@stdlib/string-format' );
|
|
33
|
+
var initializeViews = require( './initialize_array_views.js' );
|
|
34
|
+
var reshapeStrategy = require( './reshape_strategy.js' );
|
|
35
|
+
var blockedaccessorbinary2d = require( './2d_blocked_accessors.js' );
|
|
36
|
+
var blockedaccessorbinary3d = require( './3d_blocked_accessors.js' );
|
|
37
|
+
var blockedaccessorbinary4d = require( './4d_blocked_accessors.js' );
|
|
38
|
+
var blockedaccessorbinary5d = require( './5d_blocked_accessors.js' );
|
|
39
|
+
var blockedaccessorbinary6d = require( './6d_blocked_accessors.js' );
|
|
40
|
+
var blockedaccessorbinary7d = require( './7d_blocked_accessors.js' );
|
|
41
|
+
var blockedaccessorbinary8d = require( './8d_blocked_accessors.js' );
|
|
42
|
+
var blockedaccessorbinary9d = require( './9d_blocked_accessors.js' );
|
|
43
|
+
var blockedaccessorbinary10d = require( './10d_blocked_accessors.js' );
|
|
44
|
+
var blockedbinary2d = require( './2d_blocked.js' );
|
|
45
|
+
var blockedbinary3d = require( './3d_blocked.js' );
|
|
46
|
+
var blockedbinary4d = require( './4d_blocked.js' );
|
|
47
|
+
var blockedbinary5d = require( './5d_blocked.js' );
|
|
48
|
+
var blockedbinary6d = require( './6d_blocked.js' );
|
|
49
|
+
var blockedbinary7d = require( './7d_blocked.js' );
|
|
50
|
+
var blockedbinary8d = require( './8d_blocked.js' );
|
|
51
|
+
var blockedbinary9d = require( './9d_blocked.js' );
|
|
52
|
+
var blockedbinary10d = require( './10d_blocked.js' );
|
|
53
|
+
var accessorbinary0d = require( './0d_accessors.js' );
|
|
54
|
+
var accessorbinary1d = require( './1d_accessors.js' );
|
|
55
|
+
var accessorbinary2d = require( './2d_accessors.js' );
|
|
56
|
+
var accessorbinary3d = require( './3d_accessors.js' );
|
|
57
|
+
var accessorbinary4d = require( './4d_accessors.js' );
|
|
58
|
+
var accessorbinary5d = require( './5d_accessors.js' );
|
|
59
|
+
var accessorbinary6d = require( './6d_accessors.js' );
|
|
60
|
+
var accessorbinary7d = require( './7d_accessors.js' );
|
|
61
|
+
var accessorbinary8d = require( './8d_accessors.js' );
|
|
62
|
+
var accessorbinary9d = require( './9d_accessors.js' );
|
|
63
|
+
var accessorbinary10d = require( './10d_accessors.js' );
|
|
64
|
+
var accessorbinarynd = require( './nd_accessors.js' );
|
|
65
|
+
var binary0d = require( './0d.js' );
|
|
66
|
+
var binary1d = require( './1d.js' );
|
|
67
|
+
var binary2d = require( './2d.js' );
|
|
68
|
+
var binary3d = require( './3d.js' );
|
|
69
|
+
var binary4d = require( './4d.js' );
|
|
70
|
+
var binary5d = require( './5d.js' );
|
|
71
|
+
var binary6d = require( './6d.js' );
|
|
72
|
+
var binary7d = require( './7d.js' );
|
|
73
|
+
var binary8d = require( './8d.js' );
|
|
74
|
+
var binary9d = require( './9d.js' );
|
|
75
|
+
var binary10d = require( './10d.js' );
|
|
76
|
+
var binarynd = require( './nd.js' );
|
|
77
|
+
|
|
78
|
+
|
|
79
|
+
// VARIABLES //
|
|
80
|
+
|
|
81
|
+
var BINARY = [
|
|
82
|
+
binary0d,
|
|
83
|
+
binary1d,
|
|
84
|
+
binary2d,
|
|
85
|
+
binary3d,
|
|
86
|
+
binary4d,
|
|
87
|
+
binary5d,
|
|
88
|
+
binary6d,
|
|
89
|
+
binary7d,
|
|
90
|
+
binary8d,
|
|
91
|
+
binary9d,
|
|
92
|
+
binary10d
|
|
93
|
+
];
|
|
94
|
+
var ACCESSOR_BINARY = [
|
|
95
|
+
accessorbinary0d,
|
|
96
|
+
accessorbinary1d,
|
|
97
|
+
accessorbinary2d,
|
|
98
|
+
accessorbinary3d,
|
|
99
|
+
accessorbinary4d,
|
|
100
|
+
accessorbinary5d,
|
|
101
|
+
accessorbinary6d,
|
|
102
|
+
accessorbinary7d,
|
|
103
|
+
accessorbinary8d,
|
|
104
|
+
accessorbinary9d,
|
|
105
|
+
accessorbinary10d
|
|
106
|
+
];
|
|
107
|
+
var BLOCKED_BINARY = [
|
|
108
|
+
blockedbinary2d, // 0
|
|
109
|
+
blockedbinary3d,
|
|
110
|
+
blockedbinary4d,
|
|
111
|
+
blockedbinary5d,
|
|
112
|
+
blockedbinary6d,
|
|
113
|
+
blockedbinary7d,
|
|
114
|
+
blockedbinary8d,
|
|
115
|
+
blockedbinary9d,
|
|
116
|
+
blockedbinary10d // 8
|
|
117
|
+
];
|
|
118
|
+
var BLOCKED_ACCESSOR_BINARY = [
|
|
119
|
+
blockedaccessorbinary2d, // 0
|
|
120
|
+
blockedaccessorbinary3d,
|
|
121
|
+
blockedaccessorbinary4d,
|
|
122
|
+
blockedaccessorbinary5d,
|
|
123
|
+
blockedaccessorbinary6d,
|
|
124
|
+
blockedaccessorbinary7d,
|
|
125
|
+
blockedaccessorbinary8d,
|
|
126
|
+
blockedaccessorbinary9d,
|
|
127
|
+
blockedaccessorbinary10d // 8
|
|
128
|
+
];
|
|
129
|
+
var MAX_DIMS = BINARY.length - 1;
|
|
130
|
+
|
|
131
|
+
|
|
132
|
+
// FUNCTIONS //
|
|
133
|
+
|
|
134
|
+
/**
|
|
135
|
+
* Returns a boolean indicating if at least one ndarray data buffer implements the accessor protocol.
|
|
136
|
+
*
|
|
137
|
+
* @private
|
|
138
|
+
* @param {ndarrayLike} x - first ndarray
|
|
139
|
+
* @param {ndarrayLike} y - second ndarray
|
|
140
|
+
* @param {ndarrayLike} z - third ndarray
|
|
141
|
+
* @returns {boolean} boolean indicating whether an ndarray data buffer implements the accessor protocol
|
|
142
|
+
*/
|
|
143
|
+
function hasAccessors( x, y, z ) {
|
|
144
|
+
return anyIsEntryIn( [ x, y, z ], 'accessorProtocol', true );
|
|
145
|
+
}
|
|
146
|
+
|
|
147
|
+
|
|
148
|
+
// MAIN //
|
|
149
|
+
|
|
150
|
+
/**
|
|
151
|
+
* Performs a reduction over a list of specified dimensions in two input ndarrays via a one-dimensional strided array binary reduction function and assigns results to a provided output ndarray.
|
|
152
|
+
*
|
|
153
|
+
* @private
|
|
154
|
+
* @param {Function} fcn - wrapper for a one-dimensional strided array reduction function
|
|
155
|
+
* @param {ArrayLikeObject<Object>} arrays - array-like object containing ndarrays
|
|
156
|
+
* @param {IntegerArray} dims - list of dimensions over which to perform a reduction
|
|
157
|
+
* @param {Options} [options] - function options
|
|
158
|
+
* @throws {Error} arrays must have the expected number of dimensions
|
|
159
|
+
* @throws {RangeError} dimension indices must not exceed input ndarray bounds
|
|
160
|
+
* @throws {RangeError} number of dimension indices must not exceed the number of input ndarray dimensions
|
|
161
|
+
* @throws {Error} must provide unique dimension indices
|
|
162
|
+
* @throws {Error} arrays must have the same loop dimension sizes
|
|
163
|
+
* @returns {void}
|
|
164
|
+
*
|
|
165
|
+
* @example
|
|
166
|
+
* var Float64Array = require( '@stdlib/array-float64' );
|
|
167
|
+
* var ndarray2array = require( '@stdlib/ndarray-base-to-array' );
|
|
168
|
+
* var getStride = require( '@stdlib/ndarray-base-stride' );
|
|
169
|
+
* var getOffset = require( '@stdlib/ndarray-base-offset' );
|
|
170
|
+
* var getData = require( '@stdlib/ndarray-base-data-buffer' );
|
|
171
|
+
* var numelDimension = require( '@stdlib/ndarray-base-numel-dimension' );
|
|
172
|
+
* var gdot = require( '@stdlib/blas-base-ndarray-gdot' );
|
|
173
|
+
*
|
|
174
|
+
* // Create data buffers:
|
|
175
|
+
* 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 ] );
|
|
176
|
+
* var ybuf = 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 ] );
|
|
177
|
+
* var zbuf = new Float64Array( [ 0.0, 0.0, 0.0 ] );
|
|
178
|
+
*
|
|
179
|
+
* // Define the array shapes:
|
|
180
|
+
* var xsh = [ 1, 3, 2, 2 ];
|
|
181
|
+
* var ysh = [ 1, 3, 2, 2 ];
|
|
182
|
+
* var zsh = [ 1, 3 ];
|
|
183
|
+
*
|
|
184
|
+
* // Define the array strides:
|
|
185
|
+
* var sx = [ 12, 4, 2, 1 ];
|
|
186
|
+
* var sy = [ 12, 4, 2, 1 ];
|
|
187
|
+
* var sz = [ 3, 1 ];
|
|
188
|
+
*
|
|
189
|
+
* // Define the index offsets:
|
|
190
|
+
* var ox = 0;
|
|
191
|
+
* var oy = 0;
|
|
192
|
+
* var oz = 0;
|
|
193
|
+
*
|
|
194
|
+
* // Create input ndarray-like objects:
|
|
195
|
+
* var x = {
|
|
196
|
+
* 'dtype': 'float64',
|
|
197
|
+
* 'data': xbuf,
|
|
198
|
+
* 'shape': xsh,
|
|
199
|
+
* 'strides': sx,
|
|
200
|
+
* 'offset': ox,
|
|
201
|
+
* 'order': 'row-major'
|
|
202
|
+
* };
|
|
203
|
+
* var y = {
|
|
204
|
+
* 'dtype': 'float64',
|
|
205
|
+
* 'data': ybuf,
|
|
206
|
+
* 'shape': ysh,
|
|
207
|
+
* 'strides': sy,
|
|
208
|
+
* 'offset': oy,
|
|
209
|
+
* 'order': 'row-major'
|
|
210
|
+
* };
|
|
211
|
+
*
|
|
212
|
+
* // Create an output ndarray-like object:
|
|
213
|
+
* var z = {
|
|
214
|
+
* 'dtype': 'float64',
|
|
215
|
+
* 'data': zbuf,
|
|
216
|
+
* 'shape': zsh,
|
|
217
|
+
* 'strides': sz,
|
|
218
|
+
* 'offset': oz,
|
|
219
|
+
* 'order': 'row-major'
|
|
220
|
+
* };
|
|
221
|
+
*
|
|
222
|
+
* // Perform a reduction:
|
|
223
|
+
* binaryReduceStrided1d( gdot, [ x, y, z ], [ 2, 3 ] );
|
|
224
|
+
*
|
|
225
|
+
* var arr = ndarray2array( z.data, z.shape, z.strides, z.offset, z.order );
|
|
226
|
+
* // returns [ [ 30.0, 174.0, 446.0 ] ]
|
|
227
|
+
*
|
|
228
|
+
* @example
|
|
229
|
+
* var Float64Array = require( '@stdlib/array-float64' );
|
|
230
|
+
* var getStride = require( '@stdlib/ndarray-base-stride' );
|
|
231
|
+
* var getOffset = require( '@stdlib/ndarray-base-offset' );
|
|
232
|
+
* var getData = require( '@stdlib/ndarray-base-data-buffer' );
|
|
233
|
+
* var numelDimension = require( '@stdlib/ndarray-base-numel-dimension' );
|
|
234
|
+
* var gdot = require( '@stdlib/blas-base-ndarray-gdot' );
|
|
235
|
+
*
|
|
236
|
+
* // Create data buffers:
|
|
237
|
+
* 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 ] );
|
|
238
|
+
* var ybuf = 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 ] );
|
|
239
|
+
* var zbuf = new Float64Array( [ 0.0 ] );
|
|
240
|
+
*
|
|
241
|
+
* // Define the array shapes:
|
|
242
|
+
* var xsh = [ 1, 3, 2, 2 ];
|
|
243
|
+
* var ysh = [ 1, 3, 2, 2 ];
|
|
244
|
+
* var zsh = [];
|
|
245
|
+
*
|
|
246
|
+
* // Define the array strides:
|
|
247
|
+
* var sx = [ 12, 4, 2, 1 ];
|
|
248
|
+
* var sy = [ 12, 4, 2, 1 ];
|
|
249
|
+
* var sz = [ 0 ];
|
|
250
|
+
*
|
|
251
|
+
* // Define the index offsets:
|
|
252
|
+
* var ox = 0;
|
|
253
|
+
* var oy = 0;
|
|
254
|
+
* var oz = 0;
|
|
255
|
+
*
|
|
256
|
+
* // Create input ndarray-like objects:
|
|
257
|
+
* var x = {
|
|
258
|
+
* 'dtype': 'float64',
|
|
259
|
+
* 'data': xbuf,
|
|
260
|
+
* 'shape': xsh,
|
|
261
|
+
* 'strides': sx,
|
|
262
|
+
* 'offset': ox,
|
|
263
|
+
* 'order': 'row-major'
|
|
264
|
+
* };
|
|
265
|
+
* var y = {
|
|
266
|
+
* 'dtype': 'float64',
|
|
267
|
+
* 'data': ybuf,
|
|
268
|
+
* 'shape': ysh,
|
|
269
|
+
* 'strides': sy,
|
|
270
|
+
* 'offset': oy,
|
|
271
|
+
* 'order': 'row-major'
|
|
272
|
+
* };
|
|
273
|
+
*
|
|
274
|
+
* // Create an output ndarray-like object:
|
|
275
|
+
* var z = {
|
|
276
|
+
* 'dtype': 'float64',
|
|
277
|
+
* 'data': zbuf,
|
|
278
|
+
* 'shape': zsh,
|
|
279
|
+
* 'strides': sz,
|
|
280
|
+
* 'offset': oz,
|
|
281
|
+
* 'order': 'row-major'
|
|
282
|
+
* };
|
|
283
|
+
*
|
|
284
|
+
* // Perform a reduction:
|
|
285
|
+
* binaryReduceStrided1d( gdot, [ x, y, z ], [ 0, 1, 2, 3 ] );
|
|
286
|
+
*
|
|
287
|
+
* var v = z.data;
|
|
288
|
+
* // returns <Float64Array>[ 650.0 ]
|
|
289
|
+
*
|
|
290
|
+
* @example
|
|
291
|
+
* var Float64Array = require( '@stdlib/array-float64' );
|
|
292
|
+
* var ndarray2array = require( '@stdlib/ndarray-base-to-array' );
|
|
293
|
+
* var getStride = require( '@stdlib/ndarray-base-stride' );
|
|
294
|
+
* var getOffset = require( '@stdlib/ndarray-base-offset' );
|
|
295
|
+
* var getData = require( '@stdlib/ndarray-base-data-buffer' );
|
|
296
|
+
* var numelDimension = require( '@stdlib/ndarray-base-numel-dimension' );
|
|
297
|
+
* var gdot = require( '@stdlib/blas-base-ndarray-gdot' );
|
|
298
|
+
*
|
|
299
|
+
* // Create data buffers:
|
|
300
|
+
* 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 ] );
|
|
301
|
+
* var ybuf = 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 ] );
|
|
302
|
+
* var zbuf = new Float64Array( [ 0.0, 0.0, 0.0, 0.0, 0.0, 0.0, 0.0, 0.0, 0.0, 0.0, 0.0, 0.0 ] );
|
|
303
|
+
*
|
|
304
|
+
* // Define the array shapes:
|
|
305
|
+
* var xsh = [ 3, 2, 2 ];
|
|
306
|
+
* var ysh = [ 3, 2, 2 ];
|
|
307
|
+
* var zsh = [ 3, 2, 2 ];
|
|
308
|
+
*
|
|
309
|
+
* // Define the array strides:
|
|
310
|
+
* var sx = [ 4, 2, 1 ];
|
|
311
|
+
* var sy = [ 4, 2, 1 ];
|
|
312
|
+
* var sz = [ 4, 2, 1 ];
|
|
313
|
+
*
|
|
314
|
+
* // Define the index offsets:
|
|
315
|
+
* var ox = 0;
|
|
316
|
+
* var oy = 0;
|
|
317
|
+
* var oz = 0;
|
|
318
|
+
*
|
|
319
|
+
* // Create input ndarray-like objects:
|
|
320
|
+
* var x = {
|
|
321
|
+
* 'dtype': 'float64',
|
|
322
|
+
* 'data': xbuf,
|
|
323
|
+
* 'shape': xsh,
|
|
324
|
+
* 'strides': sx,
|
|
325
|
+
* 'offset': ox,
|
|
326
|
+
* 'order': 'row-major'
|
|
327
|
+
* };
|
|
328
|
+
* var y = {
|
|
329
|
+
* 'dtype': 'float64',
|
|
330
|
+
* 'data': ybuf,
|
|
331
|
+
* 'shape': ysh,
|
|
332
|
+
* 'strides': sy,
|
|
333
|
+
* 'offset': oy,
|
|
334
|
+
* 'order': 'row-major'
|
|
335
|
+
* };
|
|
336
|
+
*
|
|
337
|
+
* // Create an output ndarray-like object:
|
|
338
|
+
* var z = {
|
|
339
|
+
* 'dtype': 'float64',
|
|
340
|
+
* 'data': zbuf,
|
|
341
|
+
* 'shape': zsh,
|
|
342
|
+
* 'strides': sz,
|
|
343
|
+
* 'offset': oz,
|
|
344
|
+
* 'order': 'row-major'
|
|
345
|
+
* };
|
|
346
|
+
*
|
|
347
|
+
* // Perform a reduction:
|
|
348
|
+
* binaryReduceStrided1d( gdot, [ x, y, z ], [] );
|
|
349
|
+
*
|
|
350
|
+
* var arr = ndarray2array( z.data, z.shape, z.strides, z.offset, z.order );
|
|
351
|
+
* // returns [ [ [ 1.0, 4.0 ], [ 9.0, 16.0 ] ], [ [ 25.0, 36.0 ], [ 49.0, 64.0 ] ], [ [ 81.0, 100.0 ], [ 121.0, 144.0 ] ] ]
|
|
352
|
+
*/
|
|
353
|
+
function binaryReduceStrided1d( fcn, arrays, dims, options ) { // eslint-disable-line max-statements
|
|
354
|
+
var strategyX;
|
|
355
|
+
var strategyY;
|
|
356
|
+
var views;
|
|
357
|
+
var ndims;
|
|
358
|
+
var ldims;
|
|
359
|
+
var opts;
|
|
360
|
+
var ordx;
|
|
361
|
+
var ordy;
|
|
362
|
+
var ordz;
|
|
363
|
+
var tmpx;
|
|
364
|
+
var tmpy;
|
|
365
|
+
var arr;
|
|
366
|
+
var len;
|
|
367
|
+
var shx;
|
|
368
|
+
var shy;
|
|
369
|
+
var shc;
|
|
370
|
+
var shl;
|
|
371
|
+
var iox;
|
|
372
|
+
var ioy;
|
|
373
|
+
var ioz;
|
|
374
|
+
var scx;
|
|
375
|
+
var scy;
|
|
376
|
+
var slx;
|
|
377
|
+
var sly;
|
|
378
|
+
var sz;
|
|
379
|
+
var ns;
|
|
380
|
+
var d;
|
|
381
|
+
var s;
|
|
382
|
+
var N;
|
|
383
|
+
var M;
|
|
384
|
+
var K;
|
|
385
|
+
var x;
|
|
386
|
+
var y;
|
|
387
|
+
var z;
|
|
388
|
+
var i;
|
|
389
|
+
var j;
|
|
390
|
+
|
|
391
|
+
if ( arguments.length > 3 ) {
|
|
392
|
+
opts = options;
|
|
393
|
+
} else {
|
|
394
|
+
opts = {};
|
|
395
|
+
}
|
|
396
|
+
// Standardize ndarray meta data...
|
|
397
|
+
N = arrays.length;
|
|
398
|
+
arr = [];
|
|
399
|
+
for ( i = 0; i < N; i++ ) {
|
|
400
|
+
arr.push( ndarray2object( arrays[ i ] ) );
|
|
401
|
+
}
|
|
402
|
+
// Cache references to the input and output arrays:
|
|
403
|
+
x = arr[ 0 ];
|
|
404
|
+
y = arr[ 1 ];
|
|
405
|
+
z = arr[ 2 ];
|
|
406
|
+
|
|
407
|
+
// Resolve the number of input array dimensions:
|
|
408
|
+
shx = x.shape;
|
|
409
|
+
shy = y.shape;
|
|
410
|
+
ndims = shx.length;
|
|
411
|
+
|
|
412
|
+
// Verify that both input arrays have the same shape:
|
|
413
|
+
if ( shx.length !== shy.length ) {
|
|
414
|
+
throw new Error( format( 'invalid argument. Input arrays must have the same number of dimensions. First array dimensions: %d. Second array dimensions: %d.', shx.length, shy.length ) );
|
|
415
|
+
}
|
|
416
|
+
for ( i = 0; i < ndims; i++ ) {
|
|
417
|
+
if ( shx[ i ] !== shy[ i ] ) {
|
|
418
|
+
throw new Error( format( 'invalid argument. Input arrays must have the same shape. First array shape: [%s]. Second array shape: [%s].', join( shx, ',' ), join( shy, ',' ) ) );
|
|
419
|
+
}
|
|
420
|
+
}
|
|
421
|
+
|
|
422
|
+
// Verify that we've been provided a list of unique dimension indices...
|
|
423
|
+
M = dims.length;
|
|
424
|
+
d = normalizeIndices( dims, ndims-1 );
|
|
425
|
+
if ( d === null ) {
|
|
426
|
+
throw new RangeError( format( 'invalid argument. Third argument contains an out-of-bounds dimension index. Value: [%s].', join( dims, ',' ) ) );
|
|
427
|
+
}
|
|
428
|
+
d.sort();
|
|
429
|
+
if ( d.length !== M ) {
|
|
430
|
+
throw new Error( format( 'invalid argument. Third argument must contain a list of unique dimension indices. Value: [%s].', join( dims, ',' ) ) );
|
|
431
|
+
}
|
|
432
|
+
// Check whether we've been provided a valid number of dimensions to reduce...
|
|
433
|
+
if ( M > ndims ) {
|
|
434
|
+
throw new RangeError( format( 'invalid argument. Number of specified dimensions cannot exceed the number of dimensions in the input array. Number of dimensions: %d. Value: [%s].', ndims, join( dims, ',' ) ) );
|
|
435
|
+
}
|
|
436
|
+
// Verify that provided ndarrays have the expected number of dimensions...
|
|
437
|
+
K = ndims - M;
|
|
438
|
+
if ( z.shape.length !== K ) {
|
|
439
|
+
throw new Error( format( 'invalid argument. Output array must have the same number of non-reduced dimensions as input arrays. Input array shape: [%s]. Number of non-reduced dimensions: %d. Output array shape: [%s].', join( shx, ',' ), K, join( z.shape, ',' ) ) );
|
|
440
|
+
}
|
|
441
|
+
|
|
442
|
+
// Resolve the non-reduced ("loop") dimensions and associated strides:
|
|
443
|
+
ldims = indicesComplement( shx.length, d );
|
|
444
|
+
tmpx = takeIndexed2( shx, x.strides, ldims );
|
|
445
|
+
tmpy = takeIndexed2( shy, y.strides, ldims );
|
|
446
|
+
shl = tmpx[ 0 ]; // tmpx[ 0 ] == tmpy[ 0 ]
|
|
447
|
+
slx = tmpx[ 1 ];
|
|
448
|
+
sly = tmpy[ 1 ];
|
|
449
|
+
|
|
450
|
+
// Resolve the reduced ("core") dimensions and associated strides:
|
|
451
|
+
tmpx = takeIndexed2( shx, x.strides, d );
|
|
452
|
+
tmpy = takeIndexed2( shy, y.strides, d );
|
|
453
|
+
shc = tmpx[ 0 ]; // tmpx[ 0 ] == tmpy[ 0 ]
|
|
454
|
+
scx = tmpx[ 1 ];
|
|
455
|
+
scy = tmpy[ 1 ];
|
|
456
|
+
|
|
457
|
+
// Verify that the provided arrays have the same loop dimensions...
|
|
458
|
+
len = 1; // number of elements
|
|
459
|
+
ns = 0; // number of singleton dimensions
|
|
460
|
+
for ( i = 0; i < K; i++ ) {
|
|
461
|
+
s = shl[ i ];
|
|
462
|
+
for ( j = 2; j < N; j++ ) {
|
|
463
|
+
if ( s !== arr[ j ].shape[ i ] ) {
|
|
464
|
+
throw new Error( format( 'invalid argument. Non-reduced dimensions must be consistent across all provided arrays. Input array shape: [%s]. Non-reduced dimension indices: [%s]. Non-reduced dimensions: [%s]. Array shape: [%s] (index: %d).', join( shx, ',' ), join( ldims, ',' ), join( shl, ',' ), join( arr[ j ].shape, ',' ), j ) );
|
|
465
|
+
}
|
|
466
|
+
}
|
|
467
|
+
// Note that, if one of the dimensions is `0`, the length will be `0`...
|
|
468
|
+
len *= s;
|
|
469
|
+
|
|
470
|
+
// Check whether the current dimension is a singleton dimension...
|
|
471
|
+
if ( s === 1 ) {
|
|
472
|
+
ns += 1;
|
|
473
|
+
}
|
|
474
|
+
}
|
|
475
|
+
// Check whether we were provided empty ndarrays...
|
|
476
|
+
if ( len === 0 || ( shc.length && numel( shc ) === 0 ) ) {
|
|
477
|
+
return;
|
|
478
|
+
}
|
|
479
|
+
// Initialize ndarray-like objects for representing sub-array views...
|
|
480
|
+
views = [
|
|
481
|
+
{
|
|
482
|
+
'dtype': x.dtype,
|
|
483
|
+
'data': x.data,
|
|
484
|
+
'shape': shc,
|
|
485
|
+
'strides': scx,
|
|
486
|
+
'offset': x.offset,
|
|
487
|
+
'order': x.order
|
|
488
|
+
},
|
|
489
|
+
{
|
|
490
|
+
'dtype': y.dtype,
|
|
491
|
+
'data': y.data,
|
|
492
|
+
'shape': shc,
|
|
493
|
+
'strides': scy,
|
|
494
|
+
'offset': y.offset,
|
|
495
|
+
'order': y.order
|
|
496
|
+
}
|
|
497
|
+
];
|
|
498
|
+
initializeViews( arr, views );
|
|
499
|
+
|
|
500
|
+
// Determine the strategy for reshaping sub-array views of the input arrays prior to performing a reduction:
|
|
501
|
+
strategyX = reshapeStrategy( views[ 0 ] );
|
|
502
|
+
strategyY = reshapeStrategy( views[ 1 ] );
|
|
503
|
+
|
|
504
|
+
// Determine whether we can avoid iteration altogether...
|
|
505
|
+
if ( K === 0 ) {
|
|
506
|
+
if ( hasAccessors( x, y, z ) ) {
|
|
507
|
+
return ACCESSOR_BINARY[ K ]( fcn, arr, strategyX, strategyY, opts );
|
|
508
|
+
}
|
|
509
|
+
return BINARY[ K ]( fcn, arr, strategyX, strategyY, opts );
|
|
510
|
+
}
|
|
511
|
+
// Determine whether we only have one loop dimension and can thus readily perform one-dimensional iteration...
|
|
512
|
+
if ( K === 1 ) {
|
|
513
|
+
if ( hasAccessors( x, y, z ) ) {
|
|
514
|
+
return ACCESSOR_BINARY[ K ]( fcn, arr, views, slx, sly, strategyX, strategyY, opts ); // eslint-disable-line max-len
|
|
515
|
+
}
|
|
516
|
+
return BINARY[ K ]( fcn, arr, views, slx, sly, strategyX, strategyY, opts ); // eslint-disable-line max-len
|
|
517
|
+
}
|
|
518
|
+
sz = z.strides;
|
|
519
|
+
|
|
520
|
+
// Determine whether the loop dimensions have only **one** non-singleton dimension (e.g., shape=[10,1,1,1]) so that we can treat loop iteration as being equivalent to one-dimensional iteration...
|
|
521
|
+
if ( ns === K-1 ) {
|
|
522
|
+
// Get the index of the non-singleton dimension...
|
|
523
|
+
for ( i = 0; i < K; i++ ) {
|
|
524
|
+
if ( shl[ i ] !== 1 ) {
|
|
525
|
+
break;
|
|
526
|
+
}
|
|
527
|
+
}
|
|
528
|
+
z.shape = [ shl[i] ];
|
|
529
|
+
for ( j = 0; j < N; j++ ) {
|
|
530
|
+
arr[ j ].strides = [ arr[j].strides[i] ];
|
|
531
|
+
}
|
|
532
|
+
slx = [ slx[i] ];
|
|
533
|
+
sly = [ sly[i] ];
|
|
534
|
+
if ( hasAccessors( x, y, z ) ) {
|
|
535
|
+
return ACCESSOR_BINARY[ 1 ]( fcn, arr, views, slx, sly, strategyX, strategyY, opts ); // eslint-disable-line max-len
|
|
536
|
+
}
|
|
537
|
+
return BINARY[ 1 ]( fcn, arr, views, slx, sly, strategyX, strategyY, opts ); // eslint-disable-line max-len
|
|
538
|
+
}
|
|
539
|
+
iox = iterationOrder( slx ); // +/-1
|
|
540
|
+
ioy = iterationOrder( sly ); // +/-1
|
|
541
|
+
ioz = iterationOrder( sz ); // +/-1
|
|
542
|
+
|
|
543
|
+
// Determine whether we can avoid blocked iteration...
|
|
544
|
+
ordx = strides2order( slx );
|
|
545
|
+
ordy = strides2order( sly );
|
|
546
|
+
ordz = strides2order( sz );
|
|
547
|
+
if ( iox !== 0 && ioy !== 0 && ioz !== 0 && ordx === ordz && ordy === ordz && K <= MAX_DIMS ) { // eslint-disable-line max-len
|
|
548
|
+
// So long as iteration for each respective array always moves in the same direction (i.e., no mixed sign strides) and the memory layouts are the same, we can leverage cache-optimal (i.e., normal) nested loops without resorting to blocked iteration...
|
|
549
|
+
if ( hasAccessors( x, y, z ) ) {
|
|
550
|
+
return ACCESSOR_BINARY[ K ]( fcn, arr, views, slx, sly, ordx === 1, strategyX, strategyY, opts ); // eslint-disable-line max-len
|
|
551
|
+
}
|
|
552
|
+
return BINARY[ K ]( fcn, arr, views, slx, sly, ordx === 1, strategyX, strategyY, opts ); // eslint-disable-line max-len
|
|
553
|
+
}
|
|
554
|
+
// At this point, we're either dealing with non-contiguous n-dimensional arrays, high dimensional n-dimensional arrays, and/or arrays having differing memory layouts, so our only hope is that we can still perform blocked iteration...
|
|
555
|
+
|
|
556
|
+
// Determine whether we can perform blocked iteration...
|
|
557
|
+
if ( K <= MAX_DIMS ) {
|
|
558
|
+
if ( hasAccessors( x, y, z ) ) {
|
|
559
|
+
return BLOCKED_ACCESSOR_BINARY[ K-2 ]( fcn, arr, views, slx, sly, strategyX, strategyY, opts ); // eslint-disable-line max-len
|
|
560
|
+
}
|
|
561
|
+
return BLOCKED_BINARY[ K-2 ]( fcn, arr, views, slx, sly, strategyX, strategyY, opts ); // eslint-disable-line max-len
|
|
562
|
+
}
|
|
563
|
+
// Fall-through to linear view iteration without regard for how data is stored in memory (i.e., take the slow path)...
|
|
564
|
+
if ( hasAccessors( x, y, z ) ) {
|
|
565
|
+
return accessorbinarynd( fcn, arr, views, strategyX, strategyY, opts );
|
|
566
|
+
}
|
|
567
|
+
binarynd( fcn, arr, views, strategyX, strategyY, opts );
|
|
568
|
+
}
|
|
569
|
+
|
|
570
|
+
|
|
571
|
+
// EXPORTS //
|
|
572
|
+
|
|
573
|
+
module.exports = binaryReduceStrided1d;
|