@stdlib/ndarray-base-nullary-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/lib/2d.js ADDED
@@ -0,0 +1,208 @@
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 copyIndexed = require( '@stdlib/array-base-copy-indexed' );
24
+ var incrementOffsets = require( './increment_offsets.js' );
25
+ var setViewOffsets = require( './set_view_offsets.js' );
26
+ var offsets = require( './offsets.js' );
27
+
28
+
29
+ // MAIN //
30
+
31
+ /**
32
+ * Applies a one-dimensional strided array function to a list of specified dimensions in an ndarray.
33
+ *
34
+ * @private
35
+ * @param {Function} fcn - wrapper for a one-dimensional strided array function
36
+ * @param {Array<Object>} arrays - ndarrays
37
+ * @param {Array<Object>} views - initialized ndarray-like objects representing sub-array views
38
+ * @param {NonNegativeIntegerArray} shape - loop dimensions
39
+ * @param {IntegerArray} stridesX - loop dimension strides for the ndarray
40
+ * @param {boolean} isRowMajor - boolean indicating if provided arrays are in row-major order
41
+ * @param {Object} strategyX - strategy for marshaling data to and from an ndarray view
42
+ * @param {Options} opts - function options
43
+ * @returns {void}
44
+ *
45
+ * @example
46
+ * var ndarray2array = require( '@stdlib/ndarray-base-to-array' );
47
+ * var getStride = require( '@stdlib/ndarray-base-stride' );
48
+ * var getOffset = require( '@stdlib/ndarray-base-offset' );
49
+ * var getData = require( '@stdlib/ndarray-base-data-buffer' );
50
+ * var numelDimension = require( '@stdlib/ndarray-base-numel-dimension' );
51
+ * var ndarraylike2scalar = require( '@stdlib/ndarray-base-ndarraylike2scalar' );
52
+ * var gsorthp = require( '@stdlib/blas-ext-base-gsorthp' ).ndarray;
53
+ *
54
+ * function wrapper( arrays ) {
55
+ * var x = arrays[ 0 ];
56
+ * var o = arrays[ 1 ];
57
+ * return gsorthp( numelDimension( x, 0 ), ndarraylike2scalar( o ), getData( x ), getStride( x, 0 ), getOffset( x ) );
58
+ * }
59
+ *
60
+ * // Create a data buffer:
61
+ * var xbuf = [ 12.0, 11.0, 10.0, 9.0, 8.0, 7.0, 6.0, 5.0, 4.0, 3.0, 2.0, 1.0 ];
62
+ *
63
+ * // Define an array shape:
64
+ * var xsh = [ 1, 3, 2, 2 ];
65
+ *
66
+ * // Define the array strides:
67
+ * var sx = [ 12, 4, 2, 1 ];
68
+ *
69
+ * // Define the index offset:
70
+ * var ox = 0;
71
+ *
72
+ * // Create an ndarray-like object:
73
+ * var x = {
74
+ * 'dtype': 'generic',
75
+ * 'data': xbuf,
76
+ * 'shape': xsh,
77
+ * 'strides': sx,
78
+ * 'offset': ox,
79
+ * 'order': 'row-major'
80
+ * };
81
+ *
82
+ * // Create an ndarray-like object for the sort order:
83
+ * var sortOrder = {
84
+ * 'dtype': 'generic',
85
+ * 'data': [ 1.0 ],
86
+ * 'shape': [ 1, 3 ],
87
+ * 'strides': [ 0, 0 ],
88
+ * 'offset': 0,
89
+ * 'order': 'row-major'
90
+ * };
91
+ *
92
+ * // Initialize ndarray-like objects representing sub-array views:
93
+ * var views = [
94
+ * {
95
+ * 'dtype': x.dtype,
96
+ * 'data': x.data,
97
+ * 'shape': [ 2, 2 ],
98
+ * 'strides': [ 2, 1 ],
99
+ * 'offset': x.offset,
100
+ * 'order': x.order
101
+ * },
102
+ * {
103
+ * 'dtype': sortOrder.dtype,
104
+ * 'data': sortOrder.data,
105
+ * 'shape': [],
106
+ * 'strides': [ 0 ],
107
+ * 'offset': sortOrder.offset,
108
+ * 'order': sortOrder.order
109
+ * }
110
+ * ];
111
+ *
112
+ * // Define an input strategy:
113
+ * function inputStrategy( x ) {
114
+ * return {
115
+ * 'dtype': x.dtype,
116
+ * 'data': x.data,
117
+ * 'shape': [ 4 ],
118
+ * 'strides': [ 1 ],
119
+ * 'offset': x.offset,
120
+ * 'order': x.order
121
+ * };
122
+ * }
123
+ *
124
+ * // Define an output strategy:
125
+ * function outputStrategy( x ) {
126
+ * return x;
127
+ * }
128
+ *
129
+ * var strategy = {
130
+ * 'input': inputStrategy,
131
+ * 'output': outputStrategy
132
+ * }
133
+ *
134
+ * // Apply strided function:
135
+ * nullary2d( wrapper, [ x, sortOrder ], views, [ 1, 3 ], [ 12, 4 ], true, strategy, {} );
136
+ *
137
+ * var arr = ndarray2array( x.data, x.shape, x.strides, x.offset, x.order );
138
+ * // returns [ [ [ [ 9.0, 10.0 ], [ 11.0, 12.0 ] ], [ [ 5.0, 6.0 ], [ 7.0, 8.0 ] ], [ [ 1.0, 2.0 ], [ 3.0, 4.0 ] ] ] ]
139
+ */
140
+ function nullary2d( fcn, arrays, views, shape, stridesX, isRowMajor, strategyX, opts ) { // eslint-disable-line max-len
141
+ var dv0;
142
+ var dv1;
143
+ var S0;
144
+ var S1;
145
+ var sv;
146
+ var iv;
147
+ var i0;
148
+ var i1;
149
+ var v;
150
+ var i;
151
+
152
+ // Note on variable naming convention: S#, dv#, i# where # corresponds to the loop number, with `0` being the innermost loop...
153
+
154
+ // Extract loop variables for purposes of loop interchange: dimensions and loop offset (pointer) increments...
155
+ if ( isRowMajor ) {
156
+ // For row-major ndarrays, the last dimensions have the fastest changing indices...
157
+ S0 = shape[ 1 ];
158
+ S1 = shape[ 0 ];
159
+ dv0 = [ // offset increment for innermost loop
160
+ stridesX[1]
161
+ ];
162
+ dv1 = [ // offset increment for outermost loop
163
+ stridesX[0] - ( S0*stridesX[1] )
164
+ ];
165
+ for ( i = 1; i < arrays.length; i++ ) {
166
+ sv = arrays[ i ].strides;
167
+ dv0.push( sv[1] );
168
+ dv1.push( sv[0] - ( S0*sv[1] ) );
169
+ }
170
+ } else { // order === 'column-major'
171
+ // For column-major ndarrays, the first dimensions have the fastest changing indices...
172
+ S0 = shape[ 0 ];
173
+ S1 = shape[ 1 ];
174
+ dv0 = [ // offset increment for innermost loop
175
+ stridesX[0]
176
+ ];
177
+ dv1 = [ // offset increment for outermost loop
178
+ stridesX[1] - ( S0*stridesX[0] )
179
+ ];
180
+ for ( i = 1; i < arrays.length; i++ ) {
181
+ sv = arrays[ i ].strides;
182
+ dv0.push( sv[0] );
183
+ dv1.push( sv[1] - ( S0*sv[0] ) );
184
+ }
185
+ }
186
+ // Resolve a list of pointers to the first indexed elements in the respective ndarrays:
187
+ iv = offsets( arrays );
188
+
189
+ // 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:
190
+ v = copyIndexed( views );
191
+
192
+ // Iterate over the loop dimensions...
193
+ for ( i1 = 0; i1 < S1; i1++ ) {
194
+ for ( i0 = 0; i0 < S0; i0++ ) {
195
+ setViewOffsets( views, iv );
196
+ v[ 0 ] = strategyX.input( views[ 0 ] );
197
+ fcn( v, opts );
198
+ strategyX.output( views[ 0 ]);
199
+ incrementOffsets( iv, dv0 );
200
+ }
201
+ incrementOffsets( iv, dv1 );
202
+ }
203
+ }
204
+
205
+
206
+ // EXPORTS //
207
+
208
+ module.exports = nullary2d;
@@ -0,0 +1,238 @@
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 loopOrder = require( '@stdlib/ndarray-base-nullary-loop-interchange-order' );
24
+ var blockSize = require( '@stdlib/ndarray-base-nullary-tiling-block-size' );
25
+ var takeIndexed = require( '@stdlib/array-base-take-indexed' );
26
+ var copyIndexed = require( '@stdlib/array-base-copy-indexed' );
27
+ var zeros = require( '@stdlib/array-base-zeros' );
28
+ var incrementOffsets = require( './increment_offsets.js' );
29
+ var setViewOffsets = require( './set_view_offsets.js' );
30
+ var offsets = require( './offsets.js' );
31
+
32
+
33
+ // MAIN //
34
+
35
+ /**
36
+ * Applies a one-dimensional strided array function to a list of specified dimensions in an ndarray via loop-blocking.
37
+ *
38
+ * @private
39
+ * @param {Function} fcn - wrapper for a one-dimensional strided array function
40
+ * @param {Array<Object>} arrays - ndarrays
41
+ * @param {Array<Object>} views - initialized ndarray-like objects representing sub-array views
42
+ * @param {NonNegativeIntegerArray} shape - loop dimensions
43
+ * @param {IntegerArray} stridesX - loop dimension strides for the ndarray
44
+ * @param {Object} strategyX - strategy for marshaling data to and from an ndarray view
45
+ * @param {Options} opts - function options
46
+ * @returns {void}
47
+ *
48
+ * @example
49
+ * var ndarray2array = require( '@stdlib/ndarray-base-to-array' );
50
+ * var getStride = require( '@stdlib/ndarray-base-stride' );
51
+ * var getOffset = require( '@stdlib/ndarray-base-offset' );
52
+ * var getData = require( '@stdlib/ndarray-base-data-buffer' );
53
+ * var numelDimension = require( '@stdlib/ndarray-base-numel-dimension' );
54
+ * var ndarraylike2scalar = require( '@stdlib/ndarray-base-ndarraylike2scalar' );
55
+ * var gsorthp = require( '@stdlib/blas-ext-base-gsorthp' ).ndarray;
56
+ *
57
+ * function wrapper( arrays ) {
58
+ * var x = arrays[ 0 ];
59
+ * var o = arrays[ 1 ];
60
+ * return gsorthp( numelDimension( x, 0 ), ndarraylike2scalar( o ), getData( x ), getStride( x, 0 ), getOffset( x ) );
61
+ * }
62
+ *
63
+ * // Create a data buffer:
64
+ * var xbuf = [ 12.0, 11.0, 10.0, 9.0, 8.0, 7.0, 6.0, 5.0, 4.0, 3.0, 2.0, 1.0 ];
65
+ *
66
+ * // Define an array shape:
67
+ * var xsh = [ 1, 3, 2, 2 ];
68
+ *
69
+ * // Define the array strides:
70
+ * var sx = [ 12, 4, 2, 1 ];
71
+ *
72
+ * // Define the index offset:
73
+ * var ox = 0;
74
+ *
75
+ * // Create an ndarray-like object:
76
+ * var x = {
77
+ * 'dtype': 'generic',
78
+ * 'data': xbuf,
79
+ * 'shape': xsh,
80
+ * 'strides': sx,
81
+ * 'offset': ox,
82
+ * 'order': 'row-major'
83
+ * };
84
+ *
85
+ * // Create an ndarray-like object for the sort order:
86
+ * var sortOrder = {
87
+ * 'dtype': 'generic',
88
+ * 'data': [ 1.0 ],
89
+ * 'shape': [ 1, 3 ],
90
+ * 'strides': [ 0, 0 ],
91
+ * 'offset': 0,
92
+ * 'order': 'row-major'
93
+ * };
94
+ *
95
+ * // Initialize ndarray-like objects representing sub-array views:
96
+ * var views = [
97
+ * {
98
+ * 'dtype': x.dtype,
99
+ * 'data': x.data,
100
+ * 'shape': [ 2, 2 ],
101
+ * 'strides': [ 2, 1 ],
102
+ * 'offset': x.offset,
103
+ * 'order': x.order
104
+ * },
105
+ * {
106
+ * 'dtype': sortOrder.dtype,
107
+ * 'data': sortOrder.data,
108
+ * 'shape': [],
109
+ * 'strides': [ 0 ],
110
+ * 'offset': sortOrder.offset,
111
+ * 'order': sortOrder.order
112
+ * }
113
+ * ];
114
+ *
115
+ * // Define an input strategy:
116
+ * function inputStrategy( x ) {
117
+ * return {
118
+ * 'dtype': x.dtype,
119
+ * 'data': x.data,
120
+ * 'shape': [ 4 ],
121
+ * 'strides': [ 1 ],
122
+ * 'offset': x.offset,
123
+ * 'order': x.order
124
+ * };
125
+ * }
126
+ *
127
+ * // Define an output strategy:
128
+ * function outputStrategy( x ) {
129
+ * return x;
130
+ * }
131
+ *
132
+ * var strategy = {
133
+ * 'input': inputStrategy,
134
+ * 'output': outputStrategy
135
+ * }
136
+ *
137
+ * // Apply strided function:
138
+ * blockednullary2d( wrapper, [ x, sortOrder ], views, [ 1, 3 ], [ 12, 4 ], strategy, {} );
139
+ *
140
+ * var arr = ndarray2array( x.data, x.shape, x.strides, x.offset, x.order );
141
+ * // returns [ [ [ [ 9.0, 10.0 ], [ 11.0, 12.0 ] ], [ [ 5.0, 6.0 ], [ 7.0, 8.0 ] ], [ [ 1.0, 2.0 ], [ 3.0, 4.0 ] ] ] ]
142
+ */
143
+ function blockednullary2d( fcn, arrays, views, shape, stridesX, strategyX, opts ) { // eslint-disable-line max-len
144
+ var bsize;
145
+ var dv0;
146
+ var dv1;
147
+ var ov1;
148
+ var sh;
149
+ var s0;
150
+ var s1;
151
+ var sv;
152
+ var ov;
153
+ var iv;
154
+ var i0;
155
+ var i1;
156
+ var j0;
157
+ var j1;
158
+ var N;
159
+ var x;
160
+ var v;
161
+ var o;
162
+ var k;
163
+
164
+ // Note on variable naming convention: S#, dv#, i#, j# where # corresponds to the loop number, with `0` being the innermost loop...
165
+
166
+ N = arrays.length;
167
+ x = arrays[ 0 ];
168
+
169
+ // Resolve the loop interchange order:
170
+ o = loopOrder( shape, stridesX );
171
+ sh = o.sh;
172
+ sv = [ o.sx ];
173
+ for ( k = 1; k < N; k++ ) {
174
+ sv.push( takeIndexed( arrays[k].strides, o.idx ) );
175
+ }
176
+ // Determine the block size:
177
+ bsize = blockSize( x.dtype );
178
+
179
+ // Resolve a list of pointers to the first indexed elements in the respective ndarrays:
180
+ ov = offsets( arrays );
181
+
182
+ // Cache offset increments for the innermost loop...
183
+ dv0 = [];
184
+ for ( k = 0; k < N; k++ ) {
185
+ dv0.push( sv[k][0] );
186
+ }
187
+ // Initialize loop variables...
188
+ ov1 = zeros( N );
189
+ dv1 = zeros( N );
190
+ iv = zeros( N );
191
+
192
+ // 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:
193
+ v = copyIndexed( views );
194
+
195
+ // Iterate over blocks...
196
+ for ( j1 = sh[1]; j1 > 0; ) {
197
+ if ( j1 < bsize ) {
198
+ s1 = j1;
199
+ j1 = 0;
200
+ } else {
201
+ s1 = bsize;
202
+ j1 -= bsize;
203
+ }
204
+ for ( k = 0; k < N; k++ ) {
205
+ ov1[ k ] = ov[k] + ( j1*sv[k][1] );
206
+ }
207
+ for ( j0 = sh[0]; j0 > 0; ) {
208
+ if ( j0 < bsize ) {
209
+ s0 = j0;
210
+ j0 = 0;
211
+ } else {
212
+ s0 = bsize;
213
+ j0 -= bsize;
214
+ }
215
+ // Compute index offsets and loop offset increments for the first ndarray elements in the current block...
216
+ for ( k = 0; k < N; k++ ) {
217
+ iv[ k ] = ov1[k] + ( j0*sv[k][0] );
218
+ dv1[ k ] = sv[k][1] - ( s0*sv[k][0] );
219
+ }
220
+ // Iterate over the loop dimensions...
221
+ for ( i1 = 0; i1 < s1; i1++ ) {
222
+ for ( i0 = 0; i0 < s0; i0++ ) {
223
+ setViewOffsets( views, iv );
224
+ v[ 0 ] = strategyX.input( views[ 0 ] );
225
+ fcn( v, opts );
226
+ strategyX.output( views[ 0 ] );
227
+ incrementOffsets( iv, dv0 );
228
+ }
229
+ incrementOffsets( iv, dv1 );
230
+ }
231
+ }
232
+ }
233
+ }
234
+
235
+
236
+ // EXPORTS //
237
+
238
+ module.exports = blockednullary2d;
package/lib/3d.js ADDED
@@ -0,0 +1,224 @@
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 copyIndexed = require( '@stdlib/array-base-copy-indexed' );
24
+ var incrementOffsets = require( './increment_offsets.js' );
25
+ var setViewOffsets = require( './set_view_offsets.js' );
26
+ var offsets = require( './offsets.js' );
27
+
28
+
29
+ // MAIN //
30
+
31
+ /**
32
+ * Applies a one-dimensional strided array function to a list of specified dimensions in an ndarray.
33
+ *
34
+ * @private
35
+ * @param {Function} fcn - wrapper for a one-dimensional strided array reduction function
36
+ * @param {Array<Object>} arrays - ndarrays
37
+ * @param {Array<Object>} views - initialized ndarray-like objects representing sub-array views
38
+ * @param {NonNegativeIntegerArray} shape - loop dimensions
39
+ * @param {IntegerArray} stridesX - loop dimension strides for the ndarray
40
+ * @param {boolean} isRowMajor - boolean indicating if provided arrays are in row-major order
41
+ * @param {Object} strategyX - strategy for marshaling data to and from an ndarray view
42
+ * @param {Options} opts - function options
43
+ * @returns {void}
44
+ *
45
+ * @example
46
+ * var ndarray2array = require( '@stdlib/ndarray-base-to-array' );
47
+ * var getStride = require( '@stdlib/ndarray-base-stride' );
48
+ * var getOffset = require( '@stdlib/ndarray-base-offset' );
49
+ * var getData = require( '@stdlib/ndarray-base-data-buffer' );
50
+ * var numelDimension = require( '@stdlib/ndarray-base-numel-dimension' );
51
+ * var ndarraylike2scalar = require( '@stdlib/ndarray-base-ndarraylike2scalar' );
52
+ * var gsorthp = require( '@stdlib/blas-ext-base-gsorthp' ).ndarray;
53
+ *
54
+ * function wrapper( arrays ) {
55
+ * var x = arrays[ 0 ];
56
+ * var o = arrays[ 1 ];
57
+ * return gsorthp( numelDimension( x, 0 ), ndarraylike2scalar( o ), getData( x ), getStride( x, 0 ), getOffset( x ) );
58
+ * }
59
+ *
60
+ * // Create a data buffer:
61
+ * var xbuf = [ 12.0, 11.0, 10.0, 9.0, 8.0, 7.0, 6.0, 5.0, 4.0, 3.0, 2.0, 1.0 ];
62
+ *
63
+ * // Define an array shape:
64
+ * var xsh = [ 1, 1, 3, 2, 2 ];
65
+ *
66
+ * // Define the array strides:
67
+ * var sx = [ 12, 12, 4, 2, 1 ];
68
+ *
69
+ * // Define the index offset:
70
+ * var ox = 0;
71
+ *
72
+ * // Create an ndarray-like object:
73
+ * var x = {
74
+ * 'dtype': 'generic',
75
+ * 'data': xbuf,
76
+ * 'shape': xsh,
77
+ * 'strides': sx,
78
+ * 'offset': ox,
79
+ * 'order': 'row-major'
80
+ * };
81
+ *
82
+ * // Create an ndarray-like object for the sort order:
83
+ * var sortOrder = {
84
+ * 'dtype': 'generic',
85
+ * 'data': [ 1.0 ],
86
+ * 'shape': [ 1, 1, 3 ],
87
+ * 'strides': [ 0, 0, 0 ],
88
+ * 'offset': 0,
89
+ * 'order': 'row-major'
90
+ * };
91
+ *
92
+ * // Initialize ndarray-like objects representing sub-array views:
93
+ * var views = [
94
+ * {
95
+ * 'dtype': x.dtype,
96
+ * 'data': x.data,
97
+ * 'shape': [ 2, 2 ],
98
+ * 'strides': [ 2, 1 ],
99
+ * 'offset': x.offset,
100
+ * 'order': x.order
101
+ * },
102
+ * {
103
+ * 'dtype': sortOrder.dtype,
104
+ * 'data': sortOrder.data,
105
+ * 'shape': [],
106
+ * 'strides': [ 0 ],
107
+ * 'offset': sortOrder.offset,
108
+ * 'order': sortOrder.order
109
+ * }
110
+ * ];
111
+ *
112
+ * // Define an input strategy:
113
+ * function inputStrategy( x ) {
114
+ * return {
115
+ * 'dtype': x.dtype,
116
+ * 'data': x.data,
117
+ * 'shape': [ 4 ],
118
+ * 'strides': [ 1 ],
119
+ * 'offset': x.offset,
120
+ * 'order': x.order
121
+ * };
122
+ * }
123
+ *
124
+ * // Define an output strategy:
125
+ * function outputStrategy( x ) {
126
+ * return x;
127
+ * }
128
+ *
129
+ * var strategy = {
130
+ * 'input': inputStrategy,
131
+ * 'output': outputStrategy
132
+ * }
133
+ *
134
+ * // Apply strided function:
135
+ * nullary3d( wrapper, [ x, sortOrder ], views, [ 1, 1, 3 ], [ 12, 12, 4 ], true, strategy, {} );
136
+ *
137
+ * var arr = ndarray2array( x.data, x.shape, x.strides, x.offset, x.order );
138
+ * // returns [ [ [ [ [ 9.0, 10.0 ], [ 11.0, 12.0 ] ], [ [ 5.0, 6.0 ], [ 7.0, 8.0 ] ], [ [ 1.0, 2.0 ], [ 3.0, 4.0 ] ] ] ] ]
139
+ */
140
+ function nullary3d( fcn, arrays, views, shape, stridesX, isRowMajor, strategyX, opts ) { // eslint-disable-line max-len
141
+ var dv0;
142
+ var dv1;
143
+ var dv2;
144
+ var S0;
145
+ var S1;
146
+ var S2;
147
+ var sv;
148
+ var iv;
149
+ var i0;
150
+ var i1;
151
+ var i2;
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
+ // Extract loop variables for purposes of loop interchange: dimensions and loop offset (pointer) increments...
158
+ if ( isRowMajor ) {
159
+ // For row-major ndarrays, the last dimensions have the fastest changing indices...
160
+ S0 = shape[ 2 ];
161
+ S1 = shape[ 1 ];
162
+ S2 = shape[ 0 ];
163
+ dv0 = [ // offset increment for innermost loop
164
+ stridesX[2]
165
+ ];
166
+ dv1 = [
167
+ stridesX[1] - ( S0*stridesX[2] )
168
+ ];
169
+ dv2 = [ // offset increment for outermost loop
170
+ stridesX[0] - ( S1*stridesX[1] )
171
+ ];
172
+ for ( i = 1; i < arrays.length; i++ ) {
173
+ sv = arrays[ i ].strides;
174
+ dv0.push( sv[2] );
175
+ dv1.push( sv[1] - ( S0*sv[2] ) );
176
+ dv2.push( sv[0] - ( S1*sv[1] ) );
177
+ }
178
+ } else { // order === 'column-major'
179
+ // For column-major ndarrays, the first dimensions have the fastest changing indices...
180
+ S0 = shape[ 0 ];
181
+ S1 = shape[ 1 ];
182
+ S2 = shape[ 2 ];
183
+ dv0 = [ // offset increment for innermost loop
184
+ stridesX[0]
185
+ ];
186
+ dv1 = [
187
+ stridesX[1] - ( S0*stridesX[0] )
188
+ ];
189
+ dv2 = [ // offset increment for outermost loop
190
+ stridesX[2] - ( S1*stridesX[1] )
191
+ ];
192
+ for ( i = 1; i < arrays.length; i++ ) {
193
+ sv = arrays[ i ].strides;
194
+ dv0.push( sv[0] );
195
+ dv1.push( sv[1] - ( S0*sv[0] ) );
196
+ dv2.push( sv[2] - ( S1*sv[1] ) );
197
+ }
198
+ }
199
+ // Resolve a list of pointers to the first indexed elements in the respective ndarrays:
200
+ iv = offsets( arrays );
201
+
202
+ // 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:
203
+ v = copyIndexed( views );
204
+
205
+ // Iterate over the loop dimensions...
206
+ for ( i2 = 0; i2 < S2; i2++ ) {
207
+ for ( i1 = 0; i1 < S1; i1++ ) {
208
+ for ( i0 = 0; i0 < S0; i0++ ) {
209
+ setViewOffsets( views, iv );
210
+ v[ 0 ] = strategyX.input( views[ 0 ] );
211
+ fcn( v, opts );
212
+ strategyX.output( views[ 0 ] );
213
+ incrementOffsets( iv, dv0 );
214
+ }
215
+ incrementOffsets( iv, dv1 );
216
+ }
217
+ incrementOffsets( iv, dv2 );
218
+ }
219
+ }
220
+
221
+
222
+ // EXPORTS //
223
+
224
+ module.exports = nullary3d;