@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.
@@ -0,0 +1,300 @@
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 copyToWorkspace( 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
+ * Returns a function which copies from a contiguous ndarray workspace to an input ndarray.
183
+ *
184
+ * @private
185
+ * @param {ndarrayLike} workspace - ndarray workspace
186
+ * @returns {Function} function which copies from a contiguous ndarray workspace to an input ndarray
187
+ */
188
+ function copyFromWorkspace( workspace ) {
189
+ return copy;
190
+
191
+ /**
192
+ * Copies from a contiguous ndarray workspace to an input ndarray.
193
+ *
194
+ * @private
195
+ * @param {ndarrayLike} x - input ndarray
196
+ * @returns {ndarrayLike} input ndarray
197
+ */
198
+ function copy( x ) {
199
+ assign( [ workspace, x ] );
200
+ return x;
201
+ }
202
+ }
203
+
204
+
205
+ // MAIN //
206
+
207
+ /**
208
+ * Returns an object for reshaping input ndarrays which have the same data type, shape, and strides as a provided ndarray.
209
+ *
210
+ * @private
211
+ * @param {ndarrayLike} x - input ndarray
212
+ * @param {string} x.dtype - input ndarray data type
213
+ * @param {Collection} x.data - input ndarray data buffer
214
+ * @param {NonNegativeIntegerArray} x.shape - input ndarray shape
215
+ * @param {IntegerArray} x.strides - input ndarray strides
216
+ * @param {NonNegativeInteger} x.offset - input ndarray index offset
217
+ * @param {string} x.order - input ndarray memory layout
218
+ * @returns {Object} object containing methods implementing a reshape strategy
219
+ */
220
+ function strategy( x ) {
221
+ var workspace;
222
+ var ndims;
223
+ var xmmv;
224
+ var len;
225
+ var iox;
226
+ var sh;
227
+ var ns;
228
+ var i;
229
+
230
+ // Resolve the number of array dimensions:
231
+ sh = x.shape;
232
+ ndims = sh.length;
233
+
234
+ // Check whether the ndarray is zero-dimensional...
235
+ if ( ndims === 0 ) {
236
+ return {
237
+ 'input': broadcast,
238
+ 'output': identity
239
+ };
240
+ }
241
+ // Check whether the ndarray is already one-dimensional...
242
+ if ( ndims === 1 ) {
243
+ return {
244
+ 'input': identity,
245
+ 'output': identity
246
+ };
247
+ }
248
+ // Determine the number of singleton dimensions...
249
+ len = 1; // number of elements
250
+ ns = 0; // number of singleton dimensions
251
+ for ( i = 0; i < ndims; i++ ) {
252
+ // Check whether the current dimension is a singleton dimension...
253
+ if ( sh[ i ] === 1 ) {
254
+ ns += 1;
255
+ }
256
+ len *= sh[ i ];
257
+ }
258
+ // 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...
259
+ if ( ns === ndims-1 ) {
260
+ // Get the index of the non-singleton dimension...
261
+ for ( i = 0; i < ndims; i++ ) {
262
+ if ( sh[ i ] !== 1 ) {
263
+ break;
264
+ }
265
+ }
266
+ return {
267
+ 'input': squeeze( x, i ),
268
+ 'output': identity
269
+ };
270
+ }
271
+ iox = iterationOrder( x.strides ); // +/-1
272
+
273
+ // Determine whether we can avoid copying data...
274
+ if ( iox !== 0 ) {
275
+ // Determine the minimum and maximum linear indices which are accessible by the ndarray view:
276
+ xmmv = minmaxViewBufferIndex( sh, x.strides, x.offset, [ 0, 0 ] );
277
+
278
+ // Determine whether we can ignore shape (and strides) and create a new one-dimensional ndarray view...
279
+ if ( len === ( xmmv[1]-xmmv[0]+1 ) ) {
280
+ return {
281
+ 'input': contiguous( len, iox ),
282
+ 'output': identity
283
+ };
284
+ }
285
+ // The ndarray is non-contiguous, so we cannot directly interpret as a one-dimensional ndarray...
286
+
287
+ // Fall-through to copying to a workspace ndarray...
288
+ }
289
+ // At this point, we're dealing with a non-contiguous multi-dimensional ndarray, so we need to copy to a contiguous workspace:
290
+ workspace = ndarraylike2object( emptyLike( ndarraylike2ndarray( x ) ) );
291
+ return {
292
+ 'input': copyToWorkspace( len, workspace ),
293
+ 'output': copyFromWorkspace( workspace )
294
+ };
295
+ }
296
+
297
+
298
+ // EXPORTS //
299
+
300
+ module.exports = strategy;
@@ -0,0 +1,66 @@
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 isObject = require( '@stdlib/assert-is-plain-object' );
24
+ var hasOwnProp = require( '@stdlib/assert-has-own-property' );
25
+ var isBoolean = require( '@stdlib/assert-is-boolean' ).isPrimitive;
26
+ var format = require( '@stdlib/string-format' );
27
+
28
+
29
+ // MAIN //
30
+
31
+ /**
32
+ * Validates function options.
33
+ *
34
+ * @private
35
+ * @param {Object} opts - destination object
36
+ * @param {Options} options - function options
37
+ * @param {boolean} [options.strictTraversalOrder] - boolean indicating whether the order of element traversal must match the memory layout order of an input ndarray
38
+ * @returns {(Error|null)} null or an error object
39
+ *
40
+ * @example
41
+ * var opts = {};
42
+ * var options = {
43
+ * 'strictTraversalOrder': true
44
+ * };
45
+ * var err = validate( opts, options );
46
+ * if ( err ) {
47
+ * throw err;
48
+ * }
49
+ */
50
+ function validate( opts, options ) {
51
+ if ( !isObject( options ) ) {
52
+ return new TypeError( format( 'invalid argument. Options argument must be an object. Value: `%s`.', options ) );
53
+ }
54
+ if ( hasOwnProp( options, 'strictTraversalOrder' ) ) {
55
+ opts.strictTraversalOrder = options.strictTraversalOrder;
56
+ if ( !isBoolean( opts.strictTraversalOrder ) ) {
57
+ return new TypeError( format( 'invalid option. `%s` option must be a boolean. Option: `%s`.', 'strictTraversalOrder', opts.strictTraversalOrder ) );
58
+ }
59
+ }
60
+ return null;
61
+ }
62
+
63
+
64
+ // EXPORTS //
65
+
66
+ module.exports = validate;
package/package.json ADDED
@@ -0,0 +1,89 @@
1
+ {
2
+ "name": "@stdlib/ndarray-base-nullary-strided1d",
3
+ "version": "0.1.0",
4
+ "description": "Apply a one-dimensional strided array function to a list of specified dimensions in an ndarray.",
5
+ "license": "Apache-2.0",
6
+ "author": {
7
+ "name": "The Stdlib Authors",
8
+ "url": "https://github.com/stdlib-js/stdlib/graphs/contributors"
9
+ },
10
+ "contributors": [
11
+ {
12
+ "name": "The Stdlib Authors",
13
+ "url": "https://github.com/stdlib-js/stdlib/graphs/contributors"
14
+ }
15
+ ],
16
+ "main": "./lib",
17
+ "directories": {
18
+ "doc": "./docs",
19
+ "lib": "./lib",
20
+ "dist": "./dist"
21
+ },
22
+ "types": "./docs/types",
23
+ "scripts": {},
24
+ "homepage": "https://stdlib.io",
25
+ "repository": {
26
+ "type": "git",
27
+ "url": "git://github.com/stdlib-js/ndarray-base-nullary-strided1d.git"
28
+ },
29
+ "bugs": {
30
+ "url": "https://github.com/stdlib-js/stdlib/issues"
31
+ },
32
+ "dependencies": {
33
+ "@stdlib/array-base-copy-indexed": "^0.2.2",
34
+ "@stdlib/array-base-indices-complement": "^0.1.0",
35
+ "@stdlib/array-base-join": "^0.1.1",
36
+ "@stdlib/array-base-take-indexed": "^0.2.2",
37
+ "@stdlib/array-base-take-indexed2": "^0.1.0",
38
+ "@stdlib/array-base-zeros": "^0.2.2",
39
+ "@stdlib/assert-has-own-property": "^0.2.2",
40
+ "@stdlib/assert-is-boolean": "^0.2.2",
41
+ "@stdlib/assert-is-function": "^0.2.2",
42
+ "@stdlib/assert-is-plain-object": "^0.2.2",
43
+ "@stdlib/ndarray-base-assign": "^0.1.1",
44
+ "@stdlib/ndarray-base-empty-like": "^0.3.0",
45
+ "@stdlib/ndarray-base-iteration-order": "^0.2.2",
46
+ "@stdlib/ndarray-base-minmax-view-buffer-index": "^0.2.2",
47
+ "@stdlib/ndarray-base-ndarraylike2ndarray": "github:stdlib-js/ndarray-base-ndarraylike2ndarray#main",
48
+ "@stdlib/ndarray-base-ndarraylike2object": "^0.2.2",
49
+ "@stdlib/ndarray-base-nullary-loop-interchange-order": "^0.2.2",
50
+ "@stdlib/ndarray-base-nullary-tiling-block-size": "^0.2.2",
51
+ "@stdlib/ndarray-base-numel": "^0.2.2",
52
+ "@stdlib/ndarray-base-strides2order": "^0.2.2",
53
+ "@stdlib/ndarray-base-to-unique-normalized-indices": "^0.1.0",
54
+ "@stdlib/ndarray-base-vind2bind": "^0.2.2",
55
+ "@stdlib/string-format": "^0.2.2",
56
+ "@stdlib/utils-define-nonenumerable-read-only-property": "^0.2.2",
57
+ "@stdlib/error-tools-fmtprodmsg": "^0.2.2"
58
+ },
59
+ "devDependencies": {},
60
+ "engines": {
61
+ "node": ">=0.10.0",
62
+ "npm": ">2.7.0"
63
+ },
64
+ "os": [
65
+ "aix",
66
+ "darwin",
67
+ "freebsd",
68
+ "linux",
69
+ "macos",
70
+ "openbsd",
71
+ "sunos",
72
+ "win32",
73
+ "windows"
74
+ ],
75
+ "keywords": [
76
+ "stdlib",
77
+ "base",
78
+ "strided",
79
+ "array",
80
+ "ndarray",
81
+ "nullary",
82
+ "apply",
83
+ "vector"
84
+ ],
85
+ "funding": {
86
+ "type": "opencollective",
87
+ "url": "https://opencollective.com/stdlib"
88
+ }
89
+ }