@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/nd.js
ADDED
|
@@ -0,0 +1,189 @@
|
|
|
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 numel = require( '@stdlib/ndarray-base-numel' );
|
|
24
|
+
var vind2bind = require( '@stdlib/ndarray-base-vind2bind' );
|
|
25
|
+
var copyIndexed = require( '@stdlib/array-base-copy-indexed' );
|
|
26
|
+
var zeros = require( '@stdlib/array-base-zeros' );
|
|
27
|
+
var setViewOffsets = require( './set_view_offsets.js' );
|
|
28
|
+
var offsets = require( './offsets.js' );
|
|
29
|
+
|
|
30
|
+
|
|
31
|
+
// VARIABLES //
|
|
32
|
+
|
|
33
|
+
var MODE = 'throw';
|
|
34
|
+
|
|
35
|
+
|
|
36
|
+
// MAIN //
|
|
37
|
+
|
|
38
|
+
/**
|
|
39
|
+
* Performs a reduction over two input ndarrays and assigns results to a provided output ndarray.
|
|
40
|
+
*
|
|
41
|
+
* @private
|
|
42
|
+
* @param {Function} fcn - wrapper for a one-dimensional strided array reduction function
|
|
43
|
+
* @param {Array<Object>} arrays - ndarrays
|
|
44
|
+
* @param {Array<Object>} views - initialized ndarray-like objects representing sub-array views
|
|
45
|
+
* @param {Function} strategyX - first input ndarray reshape strategy
|
|
46
|
+
* @param {Function} strategyY - second input ndarray reshape strategy
|
|
47
|
+
* @param {Options} opts - function options
|
|
48
|
+
* @returns {void}
|
|
49
|
+
*
|
|
50
|
+
* @example
|
|
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 = 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 = 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 = new Float64Array( [ 0.0, 0.0, 0.0 ] );
|
|
59
|
+
*
|
|
60
|
+
* // Define the array shapes:
|
|
61
|
+
* var xsh = [ 3, 2, 2 ];
|
|
62
|
+
* var ysh = [ 3, 2, 2 ];
|
|
63
|
+
* var zsh = [ 3 ];
|
|
64
|
+
*
|
|
65
|
+
* // Define the array strides:
|
|
66
|
+
* var sx = [ 4, 2, 1 ];
|
|
67
|
+
* var sy = [ 4, 2, 1 ];
|
|
68
|
+
* var sz = [ 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
|
+
* };
|
|
84
|
+
* var y = {
|
|
85
|
+
* 'dtype': 'float64',
|
|
86
|
+
* 'data': ybuf,
|
|
87
|
+
* 'shape': ysh,
|
|
88
|
+
* 'strides': sy,
|
|
89
|
+
* 'offset': oy,
|
|
90
|
+
* 'order': 'row-major'
|
|
91
|
+
* };
|
|
92
|
+
*
|
|
93
|
+
* // Create an output ndarray-like object:
|
|
94
|
+
* var z = {
|
|
95
|
+
* 'dtype': 'float64',
|
|
96
|
+
* 'data': zbuf,
|
|
97
|
+
* 'shape': zsh,
|
|
98
|
+
* 'strides': sz,
|
|
99
|
+
* 'offset': oz,
|
|
100
|
+
* 'order': 'row-major'
|
|
101
|
+
* };
|
|
102
|
+
*
|
|
103
|
+
* // Initialize ndarray-like objects representing sub-array views:
|
|
104
|
+
* var views = [
|
|
105
|
+
* {
|
|
106
|
+
* 'dtype': x.dtype,
|
|
107
|
+
* 'data': x.data,
|
|
108
|
+
* 'shape': [ 2, 2 ],
|
|
109
|
+
* 'strides': [ 2, 1 ],
|
|
110
|
+
* 'offset': x.offset,
|
|
111
|
+
* 'order': x.order
|
|
112
|
+
* },
|
|
113
|
+
* {
|
|
114
|
+
* 'dtype': y.dtype,
|
|
115
|
+
* 'data': y.data,
|
|
116
|
+
* 'shape': [ 2, 2 ],
|
|
117
|
+
* 'strides': [ 2, 1 ],
|
|
118
|
+
* 'offset': y.offset,
|
|
119
|
+
* 'order': y.order
|
|
120
|
+
* }
|
|
121
|
+
* ];
|
|
122
|
+
*
|
|
123
|
+
* // Define a reshape strategy:
|
|
124
|
+
* function strategy( x ) {
|
|
125
|
+
* return {
|
|
126
|
+
* 'dtype': x.dtype,
|
|
127
|
+
* 'data': x.data,
|
|
128
|
+
* 'shape': [ 4 ],
|
|
129
|
+
* 'strides': [ 1 ],
|
|
130
|
+
* 'offset': x.offset,
|
|
131
|
+
* 'order': x.order
|
|
132
|
+
* };
|
|
133
|
+
* }
|
|
134
|
+
*
|
|
135
|
+
* // Perform a reduction:
|
|
136
|
+
* binarynd( gdot, [ x, y, z ], views, strategy, strategy, {} );
|
|
137
|
+
*
|
|
138
|
+
* var arr = ndarray2array( z.data, z.shape, z.strides, z.offset, z.order );
|
|
139
|
+
* // returns [ 30.0, 174.0, 446.0 ]
|
|
140
|
+
*/
|
|
141
|
+
function binarynd( fcn, arrays, views, strategyX, strategyY, opts ) {
|
|
142
|
+
var zbuf;
|
|
143
|
+
var len;
|
|
144
|
+
var arr;
|
|
145
|
+
var sh;
|
|
146
|
+
var iv;
|
|
147
|
+
var io;
|
|
148
|
+
var N;
|
|
149
|
+
var z;
|
|
150
|
+
var v;
|
|
151
|
+
var i;
|
|
152
|
+
var j;
|
|
153
|
+
|
|
154
|
+
N = arrays.length;
|
|
155
|
+
|
|
156
|
+
// Resolve the output ndarray and associated shape:
|
|
157
|
+
z = arrays[ 2 ];
|
|
158
|
+
sh = z.shape;
|
|
159
|
+
|
|
160
|
+
// Compute the total number of elements over which to iterate:
|
|
161
|
+
len = numel( sh );
|
|
162
|
+
|
|
163
|
+
// Resolve a list of pointers to the first indexed elements in the respective ndarrays:
|
|
164
|
+
iv = offsets( arrays );
|
|
165
|
+
|
|
166
|
+
// 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:
|
|
167
|
+
v = copyIndexed( views );
|
|
168
|
+
|
|
169
|
+
// Cache a reference to the output ndarray buffer:
|
|
170
|
+
zbuf = z.data;
|
|
171
|
+
|
|
172
|
+
// Iterate based on the linear **view** index, regardless as to how the data is stored in memory...
|
|
173
|
+
io = zeros( N );
|
|
174
|
+
for ( i = 0; i < len; i++ ) {
|
|
175
|
+
for ( j = 0; j < N; j++ ) {
|
|
176
|
+
arr = arrays[ j ];
|
|
177
|
+
io[ j ] = vind2bind( sh, arr.strides, iv[ j ], arr.order, i, MODE );
|
|
178
|
+
}
|
|
179
|
+
setViewOffsets( views, io );
|
|
180
|
+
v[ 0 ] = strategyX( views[ 0 ] );
|
|
181
|
+
v[ 1 ] = strategyY( views[ 1 ] );
|
|
182
|
+
zbuf[ io[2] ] = fcn( v, opts );
|
|
183
|
+
}
|
|
184
|
+
}
|
|
185
|
+
|
|
186
|
+
|
|
187
|
+
// EXPORTS //
|
|
188
|
+
|
|
189
|
+
module.exports = binarynd;
|
|
@@ -0,0 +1,198 @@
|
|
|
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 numel = require( '@stdlib/ndarray-base-numel' );
|
|
24
|
+
var vind2bind = require( '@stdlib/ndarray-base-vind2bind' );
|
|
25
|
+
var copyIndexed = require( '@stdlib/array-base-copy-indexed' );
|
|
26
|
+
var zeros = require( '@stdlib/array-base-zeros' );
|
|
27
|
+
var setViewOffsets = require( './set_view_offsets.js' );
|
|
28
|
+
var offsets = require( './offsets.js' );
|
|
29
|
+
|
|
30
|
+
|
|
31
|
+
// VARIABLES //
|
|
32
|
+
|
|
33
|
+
var MODE = 'throw';
|
|
34
|
+
|
|
35
|
+
|
|
36
|
+
// MAIN //
|
|
37
|
+
|
|
38
|
+
/**
|
|
39
|
+
* Performs a reduction over two input ndarrays and assigns results to a provided output ndarray.
|
|
40
|
+
*
|
|
41
|
+
* @private
|
|
42
|
+
* @param {Function} fcn - wrapper for a one-dimensional strided array reduction function
|
|
43
|
+
* @param {Array<Object>} arrays - ndarrays
|
|
44
|
+
* @param {Array<Object>} views - initialized ndarray-like objects representing sub-array views
|
|
45
|
+
* @param {Function} strategyX - first input ndarray reshape strategy
|
|
46
|
+
* @param {Function} strategyY - second input ndarray reshape strategy
|
|
47
|
+
* @param {Options} opts - function options
|
|
48
|
+
* @returns {void}
|
|
49
|
+
*
|
|
50
|
+
* @example
|
|
51
|
+
* var toAccessorArray = require( '@stdlib/array-base-to-accessor-array' );
|
|
52
|
+
* var accessors = require( '@stdlib/array-base-accessors' );
|
|
53
|
+
* var Float64Array = require( '@stdlib/array-float64' );
|
|
54
|
+
* var ndarray2array = require( '@stdlib/ndarray-base-to-array' );
|
|
55
|
+
* var gdot = require( '@stdlib/blas-base-ndarray-gdot' );
|
|
56
|
+
*
|
|
57
|
+
* // Create data buffers:
|
|
58
|
+
* 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 ] ) );
|
|
59
|
+
* 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 ] ) );
|
|
60
|
+
* var zbuf = toAccessorArray( new Float64Array( [ 0.0, 0.0, 0.0 ] ) );
|
|
61
|
+
*
|
|
62
|
+
* // Define the array shapes:
|
|
63
|
+
* var xsh = [ 3, 2, 2 ];
|
|
64
|
+
* var ysh = [ 3, 2, 2 ];
|
|
65
|
+
* var zsh = [ 3 ];
|
|
66
|
+
*
|
|
67
|
+
* // Define the array strides:
|
|
68
|
+
* var sx = [ 4, 2, 1 ];
|
|
69
|
+
* var sy = [ 4, 2, 1 ];
|
|
70
|
+
* var sz = [ 1 ];
|
|
71
|
+
*
|
|
72
|
+
* // Define the index offsets:
|
|
73
|
+
* var ox = 0;
|
|
74
|
+
* var oy = 0;
|
|
75
|
+
* var oz = 0;
|
|
76
|
+
*
|
|
77
|
+
* // Create input ndarray-like objects:
|
|
78
|
+
* var x = {
|
|
79
|
+
* 'dtype': 'float64',
|
|
80
|
+
* 'data': xbuf,
|
|
81
|
+
* 'shape': xsh,
|
|
82
|
+
* 'strides': sx,
|
|
83
|
+
* 'offset': ox,
|
|
84
|
+
* 'order': 'row-major',
|
|
85
|
+
* 'accessors': accessors( xbuf ).accessors
|
|
86
|
+
* };
|
|
87
|
+
* var y = {
|
|
88
|
+
* 'dtype': 'float64',
|
|
89
|
+
* 'data': ybuf,
|
|
90
|
+
* 'shape': ysh,
|
|
91
|
+
* 'strides': sy,
|
|
92
|
+
* 'offset': oy,
|
|
93
|
+
* 'order': 'row-major',
|
|
94
|
+
* 'accessors': accessors( ybuf ).accessors
|
|
95
|
+
* };
|
|
96
|
+
*
|
|
97
|
+
* // Create an output ndarray-like object:
|
|
98
|
+
* var z = {
|
|
99
|
+
* 'dtype': 'float64',
|
|
100
|
+
* 'data': zbuf,
|
|
101
|
+
* 'shape': zsh,
|
|
102
|
+
* 'strides': sz,
|
|
103
|
+
* 'offset': oz,
|
|
104
|
+
* 'order': 'row-major',
|
|
105
|
+
* 'accessors': accessors( zbuf ).accessors
|
|
106
|
+
* };
|
|
107
|
+
*
|
|
108
|
+
* // Initialize ndarray-like objects representing sub-array views:
|
|
109
|
+
* var views = [
|
|
110
|
+
* {
|
|
111
|
+
* 'dtype': x.dtype,
|
|
112
|
+
* 'data': x.data,
|
|
113
|
+
* 'shape': [ 2, 2 ],
|
|
114
|
+
* 'strides': [ 2, 1 ],
|
|
115
|
+
* 'offset': x.offset,
|
|
116
|
+
* 'order': x.order
|
|
117
|
+
* },
|
|
118
|
+
* {
|
|
119
|
+
* 'dtype': y.dtype,
|
|
120
|
+
* 'data': y.data,
|
|
121
|
+
* 'shape': [ 2, 2 ],
|
|
122
|
+
* 'strides': [ 2, 1 ],
|
|
123
|
+
* 'offset': y.offset,
|
|
124
|
+
* 'order': y.order
|
|
125
|
+
* }
|
|
126
|
+
* ];
|
|
127
|
+
*
|
|
128
|
+
* // Define a reshape strategy:
|
|
129
|
+
* function strategy( x ) {
|
|
130
|
+
* return {
|
|
131
|
+
* 'dtype': x.dtype,
|
|
132
|
+
* 'data': x.data,
|
|
133
|
+
* 'shape': [ 4 ],
|
|
134
|
+
* 'strides': [ 1 ],
|
|
135
|
+
* 'offset': x.offset,
|
|
136
|
+
* 'order': x.order
|
|
137
|
+
* };
|
|
138
|
+
* }
|
|
139
|
+
*
|
|
140
|
+
* // Perform a reduction:
|
|
141
|
+
* binarynd( gdot, [ x, y, z ], views, strategy, strategy, {} );
|
|
142
|
+
*
|
|
143
|
+
* var arr = ndarray2array( z.data, z.shape, z.strides, z.offset, z.order );
|
|
144
|
+
* // returns [ 30.0, 174.0, 446.0 ]
|
|
145
|
+
*/
|
|
146
|
+
function binarynd( fcn, arrays, views, strategyX, strategyY, opts ) {
|
|
147
|
+
var zbuf;
|
|
148
|
+
var len;
|
|
149
|
+
var arr;
|
|
150
|
+
var set;
|
|
151
|
+
var sh;
|
|
152
|
+
var iv;
|
|
153
|
+
var io;
|
|
154
|
+
var N;
|
|
155
|
+
var z;
|
|
156
|
+
var v;
|
|
157
|
+
var i;
|
|
158
|
+
var j;
|
|
159
|
+
|
|
160
|
+
N = arrays.length;
|
|
161
|
+
|
|
162
|
+
// Resolve the output ndarray and associated shape:
|
|
163
|
+
z = arrays[ 2 ];
|
|
164
|
+
sh = z.shape;
|
|
165
|
+
|
|
166
|
+
// Compute the total number of elements over which to iterate:
|
|
167
|
+
len = numel( sh );
|
|
168
|
+
|
|
169
|
+
// Resolve a list of pointers to the first indexed elements in the respective ndarrays:
|
|
170
|
+
iv = offsets( arrays );
|
|
171
|
+
|
|
172
|
+
// 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:
|
|
173
|
+
v = copyIndexed( views );
|
|
174
|
+
|
|
175
|
+
// Cache a reference to the output ndarray buffer:
|
|
176
|
+
zbuf = z.data;
|
|
177
|
+
|
|
178
|
+
// Cache accessors:
|
|
179
|
+
set = z.accessors[ 1 ];
|
|
180
|
+
|
|
181
|
+
// Iterate based on the linear **view** index, regardless as to how the data is stored in memory...
|
|
182
|
+
io = zeros( N );
|
|
183
|
+
for ( i = 0; i < len; i++ ) {
|
|
184
|
+
for ( j = 0; j < N; j++ ) {
|
|
185
|
+
arr = arrays[ j ];
|
|
186
|
+
io[ j ] = vind2bind( sh, arr.strides, iv[ j ], arr.order, i, MODE );
|
|
187
|
+
}
|
|
188
|
+
setViewOffsets( views, io );
|
|
189
|
+
v[ 0 ] = strategyX( views[ 0 ] );
|
|
190
|
+
v[ 1 ] = strategyY( views[ 1 ] );
|
|
191
|
+
set( zbuf, io[2], fcn( v, opts ) );
|
|
192
|
+
}
|
|
193
|
+
}
|
|
194
|
+
|
|
195
|
+
|
|
196
|
+
// EXPORTS //
|
|
197
|
+
|
|
198
|
+
module.exports = binarynd;
|
package/lib/offsets.js
ADDED
|
@@ -0,0 +1,42 @@
|
|
|
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
|
+
// MAIN //
|
|
22
|
+
|
|
23
|
+
/**
|
|
24
|
+
* Resolves index offsets from a list of ndarray-like objects.
|
|
25
|
+
*
|
|
26
|
+
* @private
|
|
27
|
+
* @param {ArrayLikeObject<Object>} arrays - list of ndarray-like objects
|
|
28
|
+
* @returns {NonNegativeIntegerArray} list of offsets
|
|
29
|
+
*/
|
|
30
|
+
function offsets( arrays ) {
|
|
31
|
+
var out = [];
|
|
32
|
+
var i;
|
|
33
|
+
for ( i = 0; i < arrays.length; i++ ) {
|
|
34
|
+
out.push( arrays[ i ].offset );
|
|
35
|
+
}
|
|
36
|
+
return out;
|
|
37
|
+
}
|
|
38
|
+
|
|
39
|
+
|
|
40
|
+
// EXPORTS //
|
|
41
|
+
|
|
42
|
+
module.exports = offsets;
|
|
@@ -0,0 +1,260 @@
|
|
|
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 iterationOrder = require( '@stdlib/ndarray-base-iteration-order' );
|
|
24
|
+
var minmaxViewBufferIndex = require( '@stdlib/ndarray-base-minmax-view-buffer-index' ).assign;
|
|
25
|
+
var ndarraylike2object = require( '@stdlib/ndarray-base-ndarraylike2object' );
|
|
26
|
+
var assign = require( '@stdlib/ndarray-base-assign' );
|
|
27
|
+
var ndarraylike2ndarray = require( '@stdlib/ndarray-base-ndarraylike2ndarray' );
|
|
28
|
+
var emptyLike = require( '@stdlib/ndarray-base-empty-like' );
|
|
29
|
+
|
|
30
|
+
|
|
31
|
+
// FUNCTIONS //
|
|
32
|
+
|
|
33
|
+
/**
|
|
34
|
+
* Returns an input ndarray.
|
|
35
|
+
*
|
|
36
|
+
* @private
|
|
37
|
+
* @param {ndarrayLike} x - input ndarray
|
|
38
|
+
* @returns {ndarrayLike} input ndarray
|
|
39
|
+
*/
|
|
40
|
+
function identity( x ) {
|
|
41
|
+
return x;
|
|
42
|
+
}
|
|
43
|
+
|
|
44
|
+
/**
|
|
45
|
+
* Broadcasts a zero-dimensional ndarray to a one-dimensional ndarray view containing a single element.
|
|
46
|
+
*
|
|
47
|
+
* @private
|
|
48
|
+
* @param {ndarrayLike} x - input ndarray
|
|
49
|
+
* @returns {ndarrayLike} broadcasted ndarray view
|
|
50
|
+
*/
|
|
51
|
+
function broadcast( x ) {
|
|
52
|
+
// NOTE: the following properties must be set in the exact same order as in `x` in order to ensure that the returned object has the same hidden shape as the input ndarray-like object...
|
|
53
|
+
return {
|
|
54
|
+
'dtype': x.dtype,
|
|
55
|
+
'data': x.data,
|
|
56
|
+
'shape': [ 1 ],
|
|
57
|
+
'strides': [ 0 ],
|
|
58
|
+
'offset': x.offset,
|
|
59
|
+
'order': x.order
|
|
60
|
+
};
|
|
61
|
+
}
|
|
62
|
+
|
|
63
|
+
/**
|
|
64
|
+
* Returns a function which returns an ndarray view in which the singleton dimensions are removed from an input ndarray having only a single non-singleton dimension.
|
|
65
|
+
*
|
|
66
|
+
* @private
|
|
67
|
+
* @param {ndarrayLike} arr - original ndarray
|
|
68
|
+
* @param {NonNegativeInteger} index - index of the non-singleton dimension
|
|
69
|
+
* @returns {Function} function for returning an ndarray view
|
|
70
|
+
*/
|
|
71
|
+
function squeeze( arr, index ) {
|
|
72
|
+
var sh = [ arr.shape[ index ] ];
|
|
73
|
+
var sx = [ arr.strides[ index ] ];
|
|
74
|
+
return reshape;
|
|
75
|
+
|
|
76
|
+
/**
|
|
77
|
+
* Returns an ndarray view in which the singleton dimensions are removed from an input ndarray having only a single non-singleton dimension.
|
|
78
|
+
*
|
|
79
|
+
* @private
|
|
80
|
+
* @param {ndarrayLike} x - input ndarray
|
|
81
|
+
* @returns {ndarrayLike} a squeezed ndarray view
|
|
82
|
+
*/
|
|
83
|
+
function reshape( x ) {
|
|
84
|
+
// NOTE: the following properties must be set in the exact same order as in `arr` in order to ensure that the returned object has the same hidden shape as the input ndarray-like object...
|
|
85
|
+
return {
|
|
86
|
+
'dtype': x.dtype,
|
|
87
|
+
'data': x.data,
|
|
88
|
+
'shape': sh,
|
|
89
|
+
'strides': sx,
|
|
90
|
+
'offset': x.offset,
|
|
91
|
+
'order': x.order
|
|
92
|
+
};
|
|
93
|
+
}
|
|
94
|
+
}
|
|
95
|
+
|
|
96
|
+
/**
|
|
97
|
+
* Returns a function which returns a one-dimensional ndarray view of a contiguous input ndarray having more than one dimension.
|
|
98
|
+
*
|
|
99
|
+
* @private
|
|
100
|
+
* @param {NonNegativeInteger} len - number of elements in an ndarray
|
|
101
|
+
* @param {integer} iox - iteration order
|
|
102
|
+
* @returns {Function} function for returning a one-dimensional ndarray view
|
|
103
|
+
*/
|
|
104
|
+
function contiguous( len, iox ) {
|
|
105
|
+
var xmmv;
|
|
106
|
+
var ind;
|
|
107
|
+
var sh;
|
|
108
|
+
var sx;
|
|
109
|
+
|
|
110
|
+
// Resolve the index of the min/max view buffer element which is the first indexed element...
|
|
111
|
+
if ( iox === 1 ) {
|
|
112
|
+
ind = 0;
|
|
113
|
+
} else {
|
|
114
|
+
ind = 1;
|
|
115
|
+
}
|
|
116
|
+
// Initialize an array for storing the min/max view buffer elements:
|
|
117
|
+
xmmv = [ 0, 0 ]; // [ min, max ]
|
|
118
|
+
|
|
119
|
+
// Initialize the output one-dimensional view's shape and strides:
|
|
120
|
+
sh = [ len ];
|
|
121
|
+
sx = [ iox ];
|
|
122
|
+
|
|
123
|
+
return reshape;
|
|
124
|
+
|
|
125
|
+
/**
|
|
126
|
+
* Returns a one-dimensional ndarray view of a contiguous input ndarray having more than one dimension.
|
|
127
|
+
*
|
|
128
|
+
* @private
|
|
129
|
+
* @param {ndarrayLike} x - input ndarray
|
|
130
|
+
* @returns {ndarrayLike} a one-dimensional ndarray view
|
|
131
|
+
*/
|
|
132
|
+
function reshape( x ) {
|
|
133
|
+
// Resolve the minimum and maximum linear indices in the underlying data buffer which are accessible to the input ndarray view:
|
|
134
|
+
minmaxViewBufferIndex( x.shape, x.strides, x.offset, xmmv );
|
|
135
|
+
|
|
136
|
+
// NOTE: the following properties must be set in the exact same order as in `x` in order to ensure that the returned object has the same hidden shape as the input ndarray-like object...
|
|
137
|
+
return {
|
|
138
|
+
'dtype': x.dtype,
|
|
139
|
+
'data': x.data,
|
|
140
|
+
'shape': sh,
|
|
141
|
+
'strides': sx,
|
|
142
|
+
'offset': xmmv[ ind ], // the index of the first indexed element
|
|
143
|
+
'order': x.order
|
|
144
|
+
};
|
|
145
|
+
}
|
|
146
|
+
}
|
|
147
|
+
|
|
148
|
+
/**
|
|
149
|
+
* Returns a function which copies an input ndarray to a contiguous ndarray workspace.
|
|
150
|
+
*
|
|
151
|
+
* @private
|
|
152
|
+
* @param {NonNegativeInteger} len - number of elements in an ndarray
|
|
153
|
+
* @param {ndarrayLike} workspace - ndarray workspace
|
|
154
|
+
* @returns {Function} function which copies an input ndarray to a contiguous ndarray workspace
|
|
155
|
+
*/
|
|
156
|
+
function copy( len, workspace ) {
|
|
157
|
+
// NOTE: the following properties must be set in the exact same order as in the input ndarray-like object in order to ensure that the returned object has the same hidden shape...
|
|
158
|
+
var view = {
|
|
159
|
+
'dtype': workspace.dtype,
|
|
160
|
+
'data': workspace.data,
|
|
161
|
+
'shape': [ len ],
|
|
162
|
+
'strides': [ 1 ],
|
|
163
|
+
'offset': workspace.offset,
|
|
164
|
+
'order': workspace.order
|
|
165
|
+
};
|
|
166
|
+
return reshape;
|
|
167
|
+
|
|
168
|
+
/**
|
|
169
|
+
* Copies an input ndarray to a contiguous ndarray workspace and returns a one-dimensional workspace view.
|
|
170
|
+
*
|
|
171
|
+
* @private
|
|
172
|
+
* @param {ndarrayLike} x - input ndarray
|
|
173
|
+
* @returns {ndarrayLike} one-dimensional workspace view
|
|
174
|
+
*/
|
|
175
|
+
function reshape( x ) {
|
|
176
|
+
assign( [ x, workspace ] );
|
|
177
|
+
return view;
|
|
178
|
+
}
|
|
179
|
+
}
|
|
180
|
+
|
|
181
|
+
|
|
182
|
+
// MAIN //
|
|
183
|
+
|
|
184
|
+
/**
|
|
185
|
+
* Returns a function for reshaping input ndarrays which have the same data type, shape, and strides as a provided ndarray.
|
|
186
|
+
*
|
|
187
|
+
* @private
|
|
188
|
+
* @param {ndarrayLike} x - input ndarray
|
|
189
|
+
* @param {*} x.dtype - input ndarray data type
|
|
190
|
+
* @param {Collection} x.data - input ndarray data buffer
|
|
191
|
+
* @param {NonNegativeIntegerArray} x.shape - input ndarray shape
|
|
192
|
+
* @param {IntegerArray} x.strides - input ndarray strides
|
|
193
|
+
* @param {NonNegativeInteger} x.offset - input ndarray index offset
|
|
194
|
+
* @param {string} x.order - input ndarray memory layout
|
|
195
|
+
* @returns {Function} function implementing a reshape strategy
|
|
196
|
+
*/
|
|
197
|
+
function strategy( x ) {
|
|
198
|
+
var ndims;
|
|
199
|
+
var xmmv;
|
|
200
|
+
var len;
|
|
201
|
+
var iox;
|
|
202
|
+
var sh;
|
|
203
|
+
var ns;
|
|
204
|
+
var i;
|
|
205
|
+
|
|
206
|
+
// Resolve the number of array dimensions:
|
|
207
|
+
sh = x.shape;
|
|
208
|
+
ndims = sh.length;
|
|
209
|
+
|
|
210
|
+
// Check whether the ndarray is zero-dimensional...
|
|
211
|
+
if ( ndims === 0 ) {
|
|
212
|
+
return broadcast;
|
|
213
|
+
}
|
|
214
|
+
// Check whether the ndarray is already one-dimensional...
|
|
215
|
+
if ( ndims === 1 ) {
|
|
216
|
+
return identity;
|
|
217
|
+
}
|
|
218
|
+
// Determine the number of singleton dimensions...
|
|
219
|
+
len = 1; // number of elements
|
|
220
|
+
ns = 0; // number of singleton dimensions
|
|
221
|
+
for ( i = 0; i < ndims; i++ ) {
|
|
222
|
+
// Check whether the current dimension is a singleton dimension...
|
|
223
|
+
if ( sh[ i ] === 1 ) {
|
|
224
|
+
ns += 1;
|
|
225
|
+
}
|
|
226
|
+
len *= sh[ i ];
|
|
227
|
+
}
|
|
228
|
+
// Determine whether the ndarray has only **one** non-singleton dimension (e.g., ndims=4, shape=[10,1,1,1]) so that we can simply create an ndarray view without the singleton dimensions...
|
|
229
|
+
if ( ns === ndims-1 ) {
|
|
230
|
+
// Get the index of the non-singleton dimension...
|
|
231
|
+
for ( i = 0; i < ndims; i++ ) {
|
|
232
|
+
if ( sh[ i ] !== 1 ) {
|
|
233
|
+
break;
|
|
234
|
+
}
|
|
235
|
+
}
|
|
236
|
+
return squeeze( x, i );
|
|
237
|
+
}
|
|
238
|
+
iox = iterationOrder( x.strides ); // +/-1
|
|
239
|
+
|
|
240
|
+
// Determine whether we can avoid copying data...
|
|
241
|
+
if ( iox !== 0 ) {
|
|
242
|
+
// Determine the minimum and maximum linear indices which are accessible by the ndarray view:
|
|
243
|
+
xmmv = minmaxViewBufferIndex( sh, x.strides, x.offset, [ 0, 0 ] );
|
|
244
|
+
|
|
245
|
+
// Determine whether we can ignore shape (and strides) and create a new one-dimensional ndarray view...
|
|
246
|
+
if ( len === ( xmmv[1]-xmmv[0]+1 ) ) {
|
|
247
|
+
return contiguous( len, iox );
|
|
248
|
+
}
|
|
249
|
+
// The ndarray is non-contiguous, so we cannot directly interpret as a one-dimensional ndarray...
|
|
250
|
+
|
|
251
|
+
// Fall-through to copying to a workspace ndarray...
|
|
252
|
+
}
|
|
253
|
+
// At this point, we're dealing with a non-contiguous multi-dimensional ndarray, so we need to copy to a contiguous workspace:
|
|
254
|
+
return copy( len, ndarraylike2object( emptyLike( ndarraylike2ndarray( x ) ) ) ); // eslint-disable-line max-len
|
|
255
|
+
}
|
|
256
|
+
|
|
257
|
+
|
|
258
|
+
// EXPORTS //
|
|
259
|
+
|
|
260
|
+
module.exports = strategy;
|