@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/index.js ADDED
@@ -0,0 +1,153 @@
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
+ /**
22
+ * Apply a one-dimensional strided array function to a list of specified dimensions in an ndarray.
23
+ *
24
+ * @module @stdlib/ndarray-base-nullary-strided1d
25
+ *
26
+ * @example
27
+ * var ndarray2array = require( '@stdlib/ndarray-base-to-array' );
28
+ * var getStride = require( '@stdlib/ndarray-base-stride' );
29
+ * var getOffset = require( '@stdlib/ndarray-base-offset' );
30
+ * var getData = require( '@stdlib/ndarray-base-data-buffer' );
31
+ * var numelDimension = require( '@stdlib/ndarray-base-numel-dimension' );
32
+ * var ndarraylike2scalar = require( '@stdlib/ndarray-base-ndarraylike2scalar' );
33
+ * var gsorthp = require( '@stdlib/blas-ext-base-gsorthp' ).ndarray;
34
+ * var nullaryStrided1d = require( '@stdlib/ndarray-base-nullary-strided1d' );
35
+ *
36
+ * function wrapper( arrays ) {
37
+ * var x = arrays[ 0 ];
38
+ * var o = arrays[ 1 ];
39
+ * return gsorthp( numelDimension( x, 0 ), ndarraylike2scalar( o ), getData( x ), getStride( x, 0 ), getOffset( x ) );
40
+ * }
41
+ *
42
+ * // Create a data buffer:
43
+ * 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 ];
44
+ *
45
+ * // Define an array shape:
46
+ * var xsh = [ 1, 3, 2, 2 ];
47
+ *
48
+ * // Define the array strides:
49
+ * var sx = [ 12, 4, 2, 1 ];
50
+ *
51
+ * // Define the index offset:
52
+ * var ox = 0;
53
+ *
54
+ * // Create an ndarray-like object:
55
+ * var x = {
56
+ * 'dtype': 'generic',
57
+ * 'data': xbuf,
58
+ * 'shape': xsh,
59
+ * 'strides': sx,
60
+ * 'offset': ox,
61
+ * 'order': 'row-major'
62
+ * };
63
+ *
64
+ * // Create an ndarray-like object for the sort order:
65
+ * var sortOrder = {
66
+ * 'dtype': 'generic',
67
+ * 'data': [ 1.0 ],
68
+ * 'shape': [ 1, 3 ],
69
+ * 'strides': [ 0, 0 ],
70
+ * 'offset': 0,
71
+ * 'order': 'row-major'
72
+ * };
73
+ *
74
+ * // Apply strided function:
75
+ * nullaryStrided1d( wrapper, [ x, sortOrder ], [ 2, 3 ] );
76
+ *
77
+ * var arr = ndarray2array( x.data, x.shape, x.strides, x.offset, x.order );
78
+ * // 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 ] ] ] ]
79
+ *
80
+ * @example
81
+ * var ndarray2array = require( '@stdlib/ndarray-base-to-array' );
82
+ * var getStride = require( '@stdlib/ndarray-base-stride' );
83
+ * var getOffset = require( '@stdlib/ndarray-base-offset' );
84
+ * var getData = require( '@stdlib/ndarray-base-data-buffer' );
85
+ * var numelDimension = require( '@stdlib/ndarray-base-numel-dimension' );
86
+ * var ndarraylike2scalar = require( '@stdlib/ndarray-base-ndarraylike2scalar' );
87
+ * var gsorthp = require( '@stdlib/blas-ext-base-gsorthp' ).ndarray;
88
+ * var nullaryStrided1d = require( '@stdlib/ndarray-base-nullary-strided1d' );
89
+ *
90
+ * function wrapper( arrays ) {
91
+ * var x = arrays[ 0 ];
92
+ * var o = arrays[ 1 ];
93
+ * return gsorthp( numelDimension( x, 0 ), ndarraylike2scalar( o ), getData( x ), getStride( x, 0 ), getOffset( x ) );
94
+ * }
95
+ *
96
+ * // Create a data buffer:
97
+ * 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 ];
98
+ *
99
+ * // Define an array shape:
100
+ * var xsh = [ 1, 3, 2, 2 ];
101
+ *
102
+ * // Define the array strides:
103
+ * var sx = [ 12, 4, 2, 1 ];
104
+ *
105
+ * // Define the index offset:
106
+ * var ox = 0;
107
+ *
108
+ * // Create an ndarray-like object:
109
+ * var x = {
110
+ * 'dtype': 'generic',
111
+ * 'data': xbuf,
112
+ * 'shape': xsh,
113
+ * 'strides': sx,
114
+ * 'offset': ox,
115
+ * 'order': 'row-major'
116
+ * };
117
+ *
118
+ * // Create an ndarray-like object for the sort order:
119
+ * var sortOrder = {
120
+ * 'dtype': 'generic',
121
+ * 'data': [ 1.0 ],
122
+ * 'shape': [ 1, 3 ],
123
+ * 'strides': [ 0, 0 ],
124
+ * 'offset': 0,
125
+ * 'order': 'row-major'
126
+ * };
127
+ *
128
+ * // Create a function for applying a strided function:
129
+ * var sorthp = nullaryStrided1d.factory( wrapper );
130
+ * // returns <Function>
131
+ *
132
+ * // Apply strided function:
133
+ * sorthp( [ x, sortOrder ], [ 2, 3 ] );
134
+ *
135
+ * var arr = ndarray2array( x.data, x.shape, x.strides, x.offset, x.order );
136
+ * // 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 ] ] ] ]
137
+ */
138
+
139
+ // MODULES //
140
+
141
+ var setReadOnly = require( '@stdlib/utils-define-nonenumerable-read-only-property' );
142
+ var factory = require( './factory.js' );
143
+ var main = require( './main.js' );
144
+
145
+
146
+ // MAIN //
147
+
148
+ setReadOnly( main, 'factory', factory );
149
+
150
+
151
+ // EXPORTS //
152
+
153
+ module.exports = main;
@@ -0,0 +1,57 @@
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
+ * Initialize ndarray-like objects for representing zero-dimensional sub-array views of ancillary ndarray arguments.
25
+ *
26
+ * ## Notes
27
+ *
28
+ * - This function ignores the first ndarray-like object, which is assumed to be the input ndarray.
29
+ * - This function mutates the provided output array.
30
+ *
31
+ * @private
32
+ * @param {ArrayLikeObject<Object>} arrays - list of ndarray-like objects
33
+ * @param {Array<Object>} out - output array
34
+ * @returns {Array<Object>} output array
35
+ */
36
+ function initializeViews( arrays, out ) {
37
+ var v;
38
+ var i;
39
+
40
+ for ( i = 1; i < arrays.length; i++ ) {
41
+ v = arrays[ i ];
42
+ out.push({
43
+ 'dtype': v.dtype,
44
+ 'data': v.data,
45
+ 'shape': [],
46
+ 'strides': [ 0 ],
47
+ 'offset': v.offset,
48
+ 'order': v.order
49
+ });
50
+ }
51
+ return out;
52
+ }
53
+
54
+
55
+ // EXPORTS //
56
+
57
+ module.exports = initializeViews;
package/lib/main.js ADDED
@@ -0,0 +1,155 @@
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 factory = require( './main_factory.js' );
24
+
25
+
26
+ // MAIN //
27
+
28
+ /**
29
+ * Applies a one-dimensional strided array function to a list of specified dimensions in an ndarray.
30
+ *
31
+ * @name nullaryStrided1d
32
+ * @type {Function}
33
+ * @param {Function} fcn - wrapper for a one-dimensional strided array function
34
+ * @param {ArrayLikeObject<Object>} arrays - array-like object containing ndarrays
35
+ * @param {IntegerArray} dims - list of dimensions to which to apply a strided array function
36
+ * @param {Options} [options] - function options
37
+ * @throws {Error} arrays must have the expected number of dimensions
38
+ * @throws {RangeError} dimension indices must not exceed target ndarray bounds
39
+ * @throws {RangeError} number of dimension indices must not exceed the number of target ndarray dimensions
40
+ * @throws {Error} must provide unique dimension indices
41
+ * @throws {Error} arrays must have the same loop dimension sizes
42
+ * @returns {void}
43
+ *
44
+ * @example
45
+ * var ndarray2array = require( '@stdlib/ndarray-base-to-array' );
46
+ * var getStride = require( '@stdlib/ndarray-base-stride' );
47
+ * var getOffset = require( '@stdlib/ndarray-base-offset' );
48
+ * var getData = require( '@stdlib/ndarray-base-data-buffer' );
49
+ * var numelDimension = require( '@stdlib/ndarray-base-numel-dimension' );
50
+ * var ndarraylike2scalar = require( '@stdlib/ndarray-base-ndarraylike2scalar' );
51
+ * var gsorthp = require( '@stdlib/blas-ext-base-gsorthp' ).ndarray;
52
+ *
53
+ * function wrapper( arrays ) {
54
+ * var x = arrays[ 0 ];
55
+ * var o = arrays[ 1 ];
56
+ * return gsorthp( numelDimension( x, 0 ), ndarraylike2scalar( o ), getData( x ), getStride( x, 0 ), getOffset( x ) );
57
+ * }
58
+ *
59
+ * // Create a data buffer:
60
+ * 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 ];
61
+ *
62
+ * // Define an array shape:
63
+ * var xsh = [ 1, 3, 2, 2 ];
64
+ *
65
+ * // Define the array strides:
66
+ * var sx = [ 12, 4, 2, 1 ];
67
+ *
68
+ * // Define the index offset:
69
+ * var ox = 0;
70
+ *
71
+ * // Create an ndarray-like object:
72
+ * var x = {
73
+ * 'dtype': 'generic',
74
+ * 'data': xbuf,
75
+ * 'shape': xsh,
76
+ * 'strides': sx,
77
+ * 'offset': ox,
78
+ * 'order': 'row-major'
79
+ * };
80
+ *
81
+ * // Create an ndarray-like object for the sort order:
82
+ * var sortOrder = {
83
+ * 'dtype': 'generic',
84
+ * 'data': [ 1.0 ],
85
+ * 'shape': [ 1, 3 ],
86
+ * 'strides': [ 0, 0 ],
87
+ * 'offset': 0,
88
+ * 'order': 'row-major'
89
+ * };
90
+ *
91
+ * // Apply strided function:
92
+ * nullaryStrided1d( wrapper, [ x, sortOrder ], [ 2, 3 ] );
93
+ *
94
+ * var arr = ndarray2array( x.data, x.shape, x.strides, x.offset, x.order );
95
+ * // 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 ] ] ] ]
96
+ *
97
+ * @example
98
+ * var ndarray2array = require( '@stdlib/ndarray-base-to-array' );
99
+ * var getStride = require( '@stdlib/ndarray-base-stride' );
100
+ * var getOffset = require( '@stdlib/ndarray-base-offset' );
101
+ * var getData = require( '@stdlib/ndarray-base-data-buffer' );
102
+ * var numelDimension = require( '@stdlib/ndarray-base-numel-dimension' );
103
+ * var ndarraylike2scalar = require( '@stdlib/ndarray-base-ndarraylike2scalar' );
104
+ * var gsorthp = require( '@stdlib/blas-ext-base-gsorthp' ).ndarray;
105
+ *
106
+ * function wrapper( arrays ) {
107
+ * var x = arrays[ 0 ];
108
+ * var o = arrays[ 1 ];
109
+ * return gsorthp( numelDimension( x, 0 ), ndarraylike2scalar( o ), getData( x ), getStride( x, 0 ), getOffset( x ) );
110
+ * }
111
+ *
112
+ * // Create a data buffer:
113
+ * 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 ];
114
+ *
115
+ * // Define an array shape:
116
+ * var xsh = [ 3, 2, 2 ];
117
+ *
118
+ * // Define the array strides:
119
+ * var sx = [ 4, 2, 1 ];
120
+ *
121
+ * // Define the index offset:
122
+ * var ox = 0;
123
+ *
124
+ * // Create an ndarray-like object:
125
+ * var x = {
126
+ * 'dtype': 'generic',
127
+ * 'data': xbuf,
128
+ * 'shape': xsh,
129
+ * 'strides': sx,
130
+ * 'offset': ox,
131
+ * 'order': 'row-major'
132
+ * };
133
+ *
134
+ * // Create an ndarray-like object for the sort order:
135
+ * var sortOrder = {
136
+ * 'dtype': 'generic',
137
+ * 'data': [ 1.0 ],
138
+ * 'shape': [],
139
+ * 'strides': [ 0 ],
140
+ * 'offset': 0,
141
+ * 'order': 'row-major'
142
+ * };
143
+ *
144
+ * // Apply strided function:
145
+ * nullaryStrided1d( wrapper, [ x, sortOrder ], [ 0, 1, 2 ] );
146
+ *
147
+ * var arr = ndarray2array( x.data, x.shape, x.strides, x.offset, x.order );
148
+ * // returns [ [ [ 1.0, 2.0 ], [ 3.0, 4.0 ] ], [ [ 5.0, 6.0 ], [ 7.0, 8.0 ] ], [ [ 9.0, 10.0 ], [ 11.0, 12.0 ] ] ]
149
+ */
150
+ var nullaryStrided1d = factory();
151
+
152
+
153
+ // EXPORTS //
154
+
155
+ module.exports = nullaryStrided1d;