@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.
Files changed (57) hide show
  1. package/LICENSE +177 -0
  2. package/NOTICE +1 -0
  3. package/README.md +307 -0
  4. package/SECURITY.md +5 -0
  5. package/dist/index.js +101 -0
  6. package/dist/index.js.map +7 -0
  7. package/lib/0d.js +118 -0
  8. package/lib/0d_accessors.js +124 -0
  9. package/lib/10d.js +365 -0
  10. package/lib/10d_accessors.js +374 -0
  11. package/lib/10d_blocked.js +429 -0
  12. package/lib/10d_blocked_accessors.js +438 -0
  13. package/lib/1d.js +186 -0
  14. package/lib/1d_accessors.js +195 -0
  15. package/lib/2d.js +221 -0
  16. package/lib/2d_accessors.js +230 -0
  17. package/lib/2d_blocked.js +245 -0
  18. package/lib/2d_blocked_accessors.js +254 -0
  19. package/lib/3d.js +239 -0
  20. package/lib/3d_accessors.js +248 -0
  21. package/lib/3d_blocked.js +268 -0
  22. package/lib/3d_blocked_accessors.js +277 -0
  23. package/lib/4d.js +257 -0
  24. package/lib/4d_accessors.js +266 -0
  25. package/lib/4d_blocked.js +291 -0
  26. package/lib/4d_blocked_accessors.js +300 -0
  27. package/lib/5d.js +275 -0
  28. package/lib/5d_accessors.js +284 -0
  29. package/lib/5d_blocked.js +314 -0
  30. package/lib/5d_blocked_accessors.js +323 -0
  31. package/lib/6d.js +293 -0
  32. package/lib/6d_accessors.js +302 -0
  33. package/lib/6d_blocked.js +337 -0
  34. package/lib/6d_blocked_accessors.js +346 -0
  35. package/lib/7d.js +311 -0
  36. package/lib/7d_accessors.js +320 -0
  37. package/lib/7d_blocked.js +360 -0
  38. package/lib/7d_blocked_accessors.js +369 -0
  39. package/lib/8d.js +329 -0
  40. package/lib/8d_accessors.js +338 -0
  41. package/lib/8d_blocked.js +383 -0
  42. package/lib/8d_blocked_accessors.js +392 -0
  43. package/lib/9d.js +347 -0
  44. package/lib/9d_accessors.js +356 -0
  45. package/lib/9d_blocked.js +406 -0
  46. package/lib/9d_blocked_accessors.js +415 -0
  47. package/lib/factory.js +133 -0
  48. package/lib/increment_offsets.js +46 -0
  49. package/lib/index.js +175 -0
  50. package/lib/initialize_array_views.js +57 -0
  51. package/lib/main.js +573 -0
  52. package/lib/nd.js +189 -0
  53. package/lib/nd_accessors.js +198 -0
  54. package/lib/offsets.js +42 -0
  55. package/lib/reshape_strategy.js +260 -0
  56. package/lib/set_view_offsets.js +52 -0
  57. package/package.json +91 -0
@@ -0,0 +1,195 @@
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
+ /* eslint-disable max-len */
20
+
21
+ 'use strict';
22
+
23
+ // MODULES //
24
+
25
+ var copyIndexed = require( '@stdlib/array-base-copy-indexed' );
26
+ var incrementOffsets = require( './increment_offsets.js' );
27
+ var setViewOffsets = require( './set_view_offsets.js' );
28
+ var offsets = require( './offsets.js' );
29
+
30
+
31
+ // MAIN //
32
+
33
+ /**
34
+ * Performs a reduction over a list of specified dimensions in two input ndarrays and assigns results to a provided output ndarray.
35
+ *
36
+ * @private
37
+ * @param {Function} fcn - wrapper for a one-dimensional strided array reduction function
38
+ * @param {Array<Object>} arrays - ndarrays
39
+ * @param {Array<Object>} views - initialized ndarray-like objects representing sub-array views
40
+ * @param {IntegerArray} stridesX - loop dimension strides for the first input ndarray
41
+ * @param {IntegerArray} stridesY - loop dimension strides for the second input ndarray
42
+ * @param {Function} strategyX - first input ndarray reshape strategy
43
+ * @param {Function} strategyY - second input ndarray reshape strategy
44
+ * @param {Options} opts - function options
45
+ * @returns {void}
46
+ *
47
+ * @example
48
+ * var toAccessorArray = require( '@stdlib/array-base-to-accessor-array' );
49
+ * var accessors = require( '@stdlib/array-base-accessors' );
50
+ * var Float64Array = require( '@stdlib/array-float64' );
51
+ * var ndarray2array = require( '@stdlib/ndarray-base-to-array' );
52
+ * var gdot = require( '@stdlib/blas-base-ndarray-gdot' );
53
+ *
54
+ * // Create data buffers:
55
+ * var xbuf = toAccessorArray( 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 ] ) );
56
+ * var ybuf = toAccessorArray( 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 zbuf = toAccessorArray( new Float64Array( [ 0.0, 0.0, 0.0 ] ) );
58
+ *
59
+ * // Define the array shapes:
60
+ * var xsh = [ 3, 2, 2 ];
61
+ * var ysh = [ 3, 2, 2 ];
62
+ * var zsh = [ 3 ];
63
+ *
64
+ * // Define the array strides:
65
+ * var sx = [ 4, 2, 1 ];
66
+ * var sy = [ 4, 2, 1 ];
67
+ * var sz = [ 1 ];
68
+ *
69
+ * // Define the index offsets:
70
+ * var ox = 0;
71
+ * var oy = 0;
72
+ * var oz = 0;
73
+ *
74
+ * // Create input ndarray-like objects:
75
+ * var x = {
76
+ * 'dtype': 'float64',
77
+ * 'data': xbuf,
78
+ * 'shape': xsh,
79
+ * 'strides': sx,
80
+ * 'offset': ox,
81
+ * 'order': 'row-major',
82
+ * 'accessors': accessors( xbuf ).accessors
83
+ * };
84
+ * var y = {
85
+ * 'dtype': 'float64',
86
+ * 'data': ybuf,
87
+ * 'shape': ysh,
88
+ * 'strides': sy,
89
+ * 'offset': oy,
90
+ * 'order': 'row-major',
91
+ * 'accessors': accessors( ybuf ).accessors
92
+ * };
93
+ *
94
+ * // Create an output ndarray-like object:
95
+ * var z = {
96
+ * 'dtype': 'float64',
97
+ * 'data': zbuf,
98
+ * 'shape': zsh,
99
+ * 'strides': sz,
100
+ * 'offset': oz,
101
+ * 'order': 'row-major',
102
+ * 'accessors': accessors( zbuf ).accessors
103
+ * };
104
+ *
105
+ * // Initialize ndarray-like objects representing sub-array views:
106
+ * var views = [
107
+ * {
108
+ * 'dtype': x.dtype,
109
+ * 'data': x.data,
110
+ * 'shape': [ 2, 2 ],
111
+ * 'strides': [ 2, 1 ],
112
+ * 'offset': x.offset,
113
+ * 'order': x.order
114
+ * },
115
+ * {
116
+ * 'dtype': y.dtype,
117
+ * 'data': y.data,
118
+ * 'shape': [ 2, 2 ],
119
+ * 'strides': [ 2, 1 ],
120
+ * 'offset': y.offset,
121
+ * 'order': y.order
122
+ * }
123
+ * ];
124
+ *
125
+ * // Define a reshape strategy:
126
+ * function strategy( x ) {
127
+ * return {
128
+ * 'dtype': x.dtype,
129
+ * 'data': x.data,
130
+ * 'shape': [ 4 ],
131
+ * 'strides': [ 1 ],
132
+ * 'offset': x.offset,
133
+ * 'order': x.order
134
+ * };
135
+ * }
136
+ *
137
+ * // Perform a reduction:
138
+ * binary1d( gdot, [ x, y, z ], views, [ 4 ], [ 4 ], strategy, strategy, {} );
139
+ *
140
+ * var arr = ndarray2array( z.data, z.shape, z.strides, z.offset, z.order );
141
+ * // returns [ 30.0, 174.0, 446.0 ]
142
+ */
143
+ function binary1d( fcn, arrays, views, stridesX, stridesY, strategyX, strategyY, opts ) {
144
+ var zbuf;
145
+ var set;
146
+ var dv0;
147
+ var sh;
148
+ var S0;
149
+ var iv;
150
+ var i0;
151
+ var z;
152
+ var v;
153
+ var i;
154
+
155
+ // Note on variable naming convention: S#, dv#, i# where # corresponds to the loop number, with `0` being the innermost loop...
156
+
157
+ // Resolve the output ndarray and associated shape:
158
+ z = arrays[ 2 ];
159
+ sh = z.shape;
160
+
161
+ // Extract loop variables: dimensions and loop offset (pointer) increments...
162
+ S0 = sh[ 0 ];
163
+ dv0 = [
164
+ stridesX[0],
165
+ stridesY[0]
166
+ ];
167
+ for ( i = 2; i < arrays.length; i++ ) {
168
+ dv0.push( arrays[i].strides[0] );
169
+ }
170
+ // Resolve a list of pointers to the first indexed elements in the respective ndarrays:
171
+ iv = offsets( arrays );
172
+
173
+ // Shallow copy the list of views to an internal array so that we can update with reshaped views without impacting the original list of views:
174
+ v = copyIndexed( views );
175
+
176
+ // Cache a reference to the output ndarray buffer:
177
+ zbuf = z.data;
178
+
179
+ // Cache accessors:
180
+ set = z.accessors[ 1 ];
181
+
182
+ // Iterate over the non-reduced ndarray dimensions...
183
+ for ( i0 = 0; i0 < S0; i0++ ) {
184
+ setViewOffsets( views, iv );
185
+ v[ 0 ] = strategyX( views[ 0 ] );
186
+ v[ 1 ] = strategyY( views[ 1 ] );
187
+ set( zbuf, iv[2], fcn( v, opts ) );
188
+ incrementOffsets( iv, dv0 );
189
+ }
190
+ }
191
+
192
+
193
+ // EXPORTS //
194
+
195
+ module.exports = binary1d;
package/lib/2d.js ADDED
@@ -0,0 +1,221 @@
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
+ /* eslint-disable max-len */
20
+
21
+ 'use strict';
22
+
23
+ // MODULES //
24
+
25
+ var copyIndexed = require( '@stdlib/array-base-copy-indexed' );
26
+ var incrementOffsets = require( './increment_offsets.js' );
27
+ var setViewOffsets = require( './set_view_offsets.js' );
28
+ var offsets = require( './offsets.js' );
29
+
30
+
31
+ // MAIN //
32
+
33
+ /**
34
+ * Performs a reduction over a list of specified dimensions in two input ndarrays and assigns results to a provided output ndarray.
35
+ *
36
+ * @private
37
+ * @param {Function} fcn - wrapper for a one-dimensional strided array reduction function
38
+ * @param {Array<Object>} arrays - ndarrays
39
+ * @param {Array<Object>} views - initialized ndarray-like objects representing sub-array views
40
+ * @param {IntegerArray} stridesX - loop dimension strides for the first input ndarray
41
+ * @param {IntegerArray} stridesY - loop dimension strides for the second input ndarray
42
+ * @param {boolean} isRowMajor - boolean indicating if provided arrays are in row-major order
43
+ * @param {Function} strategyX - first input ndarray reshape strategy
44
+ * @param {Function} strategyY - second input ndarray reshape strategy
45
+ * @param {Options} opts - function options
46
+ * @returns {void}
47
+ *
48
+ * @example
49
+ * var Float64Array = require( '@stdlib/array-float64' );
50
+ * var ndarray2array = require( '@stdlib/ndarray-base-to-array' );
51
+ * var gdot = require( '@stdlib/blas-base-ndarray-gdot' );
52
+ *
53
+ * // Create data buffers:
54
+ * 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 ] );
55
+ * 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 ] );
56
+ * var zbuf = new Float64Array( [ 0.0, 0.0, 0.0 ] );
57
+ *
58
+ * // Define the array shapes:
59
+ * var xsh = [ 1, 3, 2, 2 ];
60
+ * var ysh = [ 1, 3, 2, 2 ];
61
+ * var zsh = [ 1, 3 ];
62
+ *
63
+ * // Define the array strides:
64
+ * var sx = [ 12, 4, 2, 1 ];
65
+ * var sy = [ 12, 4, 2, 1 ];
66
+ * var sz = [ 3, 1 ];
67
+ *
68
+ * // Define the index offsets:
69
+ * var ox = 0;
70
+ * var oy = 0;
71
+ * var oz = 0;
72
+ *
73
+ * // Create input ndarray-like objects:
74
+ * var x = {
75
+ * 'dtype': 'float64',
76
+ * 'data': xbuf,
77
+ * 'shape': xsh,
78
+ * 'strides': sx,
79
+ * 'offset': ox,
80
+ * 'order': 'row-major'
81
+ * };
82
+ * var y = {
83
+ * 'dtype': 'float64',
84
+ * 'data': ybuf,
85
+ * 'shape': ysh,
86
+ * 'strides': sy,
87
+ * 'offset': oy,
88
+ * 'order': 'row-major'
89
+ * };
90
+ *
91
+ * // Create an output ndarray-like object:
92
+ * var z = {
93
+ * 'dtype': 'float64',
94
+ * 'data': zbuf,
95
+ * 'shape': zsh,
96
+ * 'strides': sz,
97
+ * 'offset': oz,
98
+ * 'order': 'row-major'
99
+ * };
100
+ *
101
+ * // Initialize ndarray-like objects representing sub-array views:
102
+ * var views = [
103
+ * {
104
+ * 'dtype': x.dtype,
105
+ * 'data': x.data,
106
+ * 'shape': [ 2, 2 ],
107
+ * 'strides': [ 2, 1 ],
108
+ * 'offset': x.offset,
109
+ * 'order': x.order
110
+ * },
111
+ * {
112
+ * 'dtype': y.dtype,
113
+ * 'data': y.data,
114
+ * 'shape': [ 2, 2 ],
115
+ * 'strides': [ 2, 1 ],
116
+ * 'offset': y.offset,
117
+ * 'order': y.order
118
+ * }
119
+ * ];
120
+ *
121
+ * // Define a reshape strategy:
122
+ * function strategy( x ) {
123
+ * return {
124
+ * 'dtype': x.dtype,
125
+ * 'data': x.data,
126
+ * 'shape': [ 4 ],
127
+ * 'strides': [ 1 ],
128
+ * 'offset': x.offset,
129
+ * 'order': x.order
130
+ * };
131
+ * }
132
+ *
133
+ * // Perform a reduction:
134
+ * binary2d( gdot, [ x, y, z ], views, [ 12, 4 ], [ 12, 4 ], true, strategy, strategy, {} );
135
+ *
136
+ * var arr = ndarray2array( z.data, z.shape, z.strides, z.offset, z.order );
137
+ * // returns [ [ 30.0, 174.0, 446.0 ] ]
138
+ */
139
+ function binary2d( fcn, arrays, views, stridesX, stridesY, isRowMajor, strategyX, strategyY, opts ) {
140
+ var zbuf;
141
+ var dv0;
142
+ var dv1;
143
+ var sh;
144
+ var S0;
145
+ var S1;
146
+ var sv;
147
+ var iv;
148
+ var i0;
149
+ var i1;
150
+ var z;
151
+ var v;
152
+ var i;
153
+
154
+ // Note on variable naming convention: S#, dv#, i# where # corresponds to the loop number, with `0` being the innermost loop...
155
+
156
+ // Resolve the output ndarray and associated shape:
157
+ z = arrays[ 2 ];
158
+ sh = z.shape;
159
+
160
+ // Extract loop variables for purposes of loop interchange: dimensions and loop offset (pointer) increments...
161
+ if ( isRowMajor ) {
162
+ // For row-major ndarrays, the last dimensions have the fastest changing indices...
163
+ S0 = sh[ 1 ];
164
+ S1 = sh[ 0 ];
165
+ dv0 = [ // offset increment for innermost loop
166
+ stridesX[1],
167
+ stridesY[1]
168
+ ];
169
+ dv1 = [ // offset increment for outermost loop
170
+ stridesX[0] - ( S0*stridesX[1] ),
171
+ stridesY[0] - ( S0*stridesY[1] )
172
+ ];
173
+ for ( i = 2; i < arrays.length; i++ ) {
174
+ sv = arrays[ i ].strides;
175
+ dv0.push( sv[1] );
176
+ dv1.push( sv[0] - ( S0*sv[1] ) );
177
+ }
178
+ } else { // order === 'column-major'
179
+ // For column-major ndarrays, the first dimensions have the fastest changing indices...
180
+ S0 = sh[ 0 ];
181
+ S1 = sh[ 1 ];
182
+ dv0 = [ // offset increment for innermost loop
183
+ stridesX[0],
184
+ stridesY[0]
185
+ ];
186
+ dv1 = [ // offset increment for outermost loop
187
+ stridesX[1] - ( S0*stridesX[0] ),
188
+ stridesY[1] - ( S0*stridesY[0] )
189
+ ];
190
+ for ( i = 2; i < arrays.length; i++ ) {
191
+ sv = arrays[ i ].strides;
192
+ dv0.push( sv[0] );
193
+ dv1.push( sv[1] - ( S0*sv[0] ) );
194
+ }
195
+ }
196
+ // Resolve a list of pointers to the first indexed elements in the respective ndarrays:
197
+ iv = offsets( arrays );
198
+
199
+ // Shallow copy the list of views to an internal array so that we can update with reshaped views without impacting the original list of views:
200
+ v = copyIndexed( views );
201
+
202
+ // Cache a reference to the output ndarray buffer:
203
+ zbuf = z.data;
204
+
205
+ // Iterate over the non-reduced ndarray dimensions...
206
+ for ( i1 = 0; i1 < S1; i1++ ) {
207
+ for ( i0 = 0; i0 < S0; i0++ ) {
208
+ setViewOffsets( views, iv );
209
+ v[ 0 ] = strategyX( views[ 0 ] );
210
+ v[ 1 ] = strategyY( views[ 1 ] );
211
+ zbuf[ iv[2] ] = fcn( v, opts );
212
+ incrementOffsets( iv, dv0 );
213
+ }
214
+ incrementOffsets( iv, dv1 );
215
+ }
216
+ }
217
+
218
+
219
+ // EXPORTS //
220
+
221
+ module.exports = binary2d;
@@ -0,0 +1,230 @@
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
+ /* eslint-disable max-len */
20
+
21
+ 'use strict';
22
+
23
+ // MODULES //
24
+
25
+ var copyIndexed = require( '@stdlib/array-base-copy-indexed' );
26
+ var incrementOffsets = require( './increment_offsets.js' );
27
+ var setViewOffsets = require( './set_view_offsets.js' );
28
+ var offsets = require( './offsets.js' );
29
+
30
+
31
+ // MAIN //
32
+
33
+ /**
34
+ * Performs a reduction over a list of specified dimensions in two input ndarrays and assigns results to a provided output ndarray.
35
+ *
36
+ * @private
37
+ * @param {Function} fcn - wrapper for a one-dimensional strided array reduction function
38
+ * @param {Array<Object>} arrays - ndarrays
39
+ * @param {Array<Object>} views - initialized ndarray-like objects representing sub-array views
40
+ * @param {IntegerArray} stridesX - loop dimension strides for the first input ndarray
41
+ * @param {IntegerArray} stridesY - loop dimension strides for the second input ndarray
42
+ * @param {boolean} isRowMajor - boolean indicating if provided arrays are in row-major order
43
+ * @param {Function} strategyX - first input ndarray reshape strategy
44
+ * @param {Function} strategyY - second input ndarray reshape strategy
45
+ * @param {Options} opts - function options
46
+ * @returns {void}
47
+ *
48
+ * @example
49
+ * var toAccessorArray = require( '@stdlib/array-base-to-accessor-array' );
50
+ * var accessors = require( '@stdlib/array-base-accessors' );
51
+ * var Float64Array = require( '@stdlib/array-float64' );
52
+ * var ndarray2array = require( '@stdlib/ndarray-base-to-array' );
53
+ * var gdot = require( '@stdlib/blas-base-ndarray-gdot' );
54
+ *
55
+ * // Create data buffers:
56
+ * var xbuf = toAccessorArray( 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 ybuf = toAccessorArray( 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 ] ) );
58
+ * var zbuf = toAccessorArray( new Float64Array( [ 0.0, 0.0, 0.0 ] ) );
59
+ *
60
+ * // Define the array shapes:
61
+ * var xsh = [ 1, 3, 2, 2 ];
62
+ * var ysh = [ 1, 3, 2, 2 ];
63
+ * var zsh = [ 1, 3 ];
64
+ *
65
+ * // Define the array strides:
66
+ * var sx = [ 12, 4, 2, 1 ];
67
+ * var sy = [ 12, 4, 2, 1 ];
68
+ * var sz = [ 3, 1 ];
69
+ *
70
+ * // Define the index offsets:
71
+ * var ox = 0;
72
+ * var oy = 0;
73
+ * var oz = 0;
74
+ *
75
+ * // Create input ndarray-like objects:
76
+ * var x = {
77
+ * 'dtype': 'float64',
78
+ * 'data': xbuf,
79
+ * 'shape': xsh,
80
+ * 'strides': sx,
81
+ * 'offset': ox,
82
+ * 'order': 'row-major',
83
+ * 'accessors': accessors( xbuf ).accessors
84
+ * };
85
+ * var y = {
86
+ * 'dtype': 'float64',
87
+ * 'data': ybuf,
88
+ * 'shape': ysh,
89
+ * 'strides': sy,
90
+ * 'offset': oy,
91
+ * 'order': 'row-major',
92
+ * 'accessors': accessors( ybuf ).accessors
93
+ * };
94
+ *
95
+ * // Create an output ndarray-like object:
96
+ * var z = {
97
+ * 'dtype': 'float64',
98
+ * 'data': zbuf,
99
+ * 'shape': zsh,
100
+ * 'strides': sz,
101
+ * 'offset': oz,
102
+ * 'order': 'row-major',
103
+ * 'accessors': accessors( zbuf ).accessors
104
+ * };
105
+ *
106
+ * // Initialize ndarray-like objects representing sub-array views:
107
+ * var views = [
108
+ * {
109
+ * 'dtype': x.dtype,
110
+ * 'data': x.data,
111
+ * 'shape': [ 2, 2 ],
112
+ * 'strides': [ 2, 1 ],
113
+ * 'offset': x.offset,
114
+ * 'order': x.order
115
+ * },
116
+ * {
117
+ * 'dtype': y.dtype,
118
+ * 'data': y.data,
119
+ * 'shape': [ 2, 2 ],
120
+ * 'strides': [ 2, 1 ],
121
+ * 'offset': y.offset,
122
+ * 'order': y.order
123
+ * }
124
+ * ];
125
+ *
126
+ * // Define a reshape strategy:
127
+ * function strategy( x ) {
128
+ * return {
129
+ * 'dtype': x.dtype,
130
+ * 'data': x.data,
131
+ * 'shape': [ 4 ],
132
+ * 'strides': [ 1 ],
133
+ * 'offset': x.offset,
134
+ * 'order': x.order
135
+ * };
136
+ * }
137
+ *
138
+ * // Perform a reduction:
139
+ * binary2d( gdot, [ x, y, z ], views, [ 12, 4 ], [ 12, 4 ], true, strategy, strategy, {} );
140
+ *
141
+ * var arr = ndarray2array( z.data, z.shape, z.strides, z.offset, z.order );
142
+ * // returns [ [ 30.0, 174.0, 446.0 ] ]
143
+ */
144
+ function binary2d( fcn, arrays, views, stridesX, stridesY, isRowMajor, strategyX, strategyY, opts ) {
145
+ var zbuf;
146
+ var set;
147
+ var dv0;
148
+ var dv1;
149
+ var sh;
150
+ var S0;
151
+ var S1;
152
+ var sv;
153
+ var iv;
154
+ var i0;
155
+ var i1;
156
+ var z;
157
+ var v;
158
+ var i;
159
+
160
+ // Note on variable naming convention: S#, dv#, i# where # corresponds to the loop number, with `0` being the innermost loop...
161
+
162
+ // Resolve the output ndarray and associated shape:
163
+ z = arrays[ 2 ];
164
+ sh = z.shape;
165
+
166
+ // Extract loop variables for purposes of loop interchange: dimensions and loop offset (pointer) increments...
167
+ if ( isRowMajor ) {
168
+ // For row-major ndarrays, the last dimensions have the fastest changing indices...
169
+ S0 = sh[ 1 ];
170
+ S1 = sh[ 0 ];
171
+ dv0 = [ // offset increment for innermost loop
172
+ stridesX[1],
173
+ stridesY[1]
174
+ ];
175
+ dv1 = [ // offset increment for outermost loop
176
+ stridesX[0] - ( S0*stridesX[1] ),
177
+ stridesY[0] - ( S0*stridesY[1] )
178
+ ];
179
+ for ( i = 2; i < arrays.length; i++ ) {
180
+ sv = arrays[ i ].strides;
181
+ dv0.push( sv[1] );
182
+ dv1.push( sv[0] - ( S0*sv[1] ) );
183
+ }
184
+ } else { // order === 'column-major'
185
+ // For column-major ndarrays, the first dimensions have the fastest changing indices...
186
+ S0 = sh[ 0 ];
187
+ S1 = sh[ 1 ];
188
+ dv0 = [ // offset increment for innermost loop
189
+ stridesX[0],
190
+ stridesY[0]
191
+ ];
192
+ dv1 = [ // offset increment for outermost loop
193
+ stridesX[1] - ( S0*stridesX[0] ),
194
+ stridesY[1] - ( S0*stridesY[0] )
195
+ ];
196
+ for ( i = 2; i < arrays.length; i++ ) {
197
+ sv = arrays[ i ].strides;
198
+ dv0.push( sv[0] );
199
+ dv1.push( sv[1] - ( S0*sv[0] ) );
200
+ }
201
+ }
202
+ // Resolve a list of pointers to the first indexed elements in the respective ndarrays:
203
+ iv = offsets( arrays );
204
+
205
+ // Shallow copy the list of views to an internal array so that we can update with reshaped views without impacting the original list of views:
206
+ v = copyIndexed( views );
207
+
208
+ // Cache a reference to the output ndarray buffer:
209
+ zbuf = z.data;
210
+
211
+ // Cache accessors:
212
+ set = z.accessors[ 1 ];
213
+
214
+ // Iterate over the non-reduced ndarray dimensions...
215
+ for ( i1 = 0; i1 < S1; i1++ ) {
216
+ for ( i0 = 0; i0 < S0; i0++ ) {
217
+ setViewOffsets( views, iv );
218
+ v[ 0 ] = strategyX( views[ 0 ] );
219
+ v[ 1 ] = strategyY( views[ 1 ] );
220
+ set( zbuf, iv[ 2 ], fcn( v, opts ) );
221
+ incrementOffsets( iv, dv0 );
222
+ }
223
+ incrementOffsets( iv, dv1 );
224
+ }
225
+ }
226
+
227
+
228
+ // EXPORTS //
229
+
230
+ module.exports = binary2d;