Skip to content

Commit d389d89

Browse files
committed
feat: add ndarray/base/unary-strided1d
--- type: pre_commit_static_analysis_report description: Results of running static analysis checks when committing changes. report: - task: lint_filenames status: passed - task: lint_editorconfig status: passed - task: lint_markdown status: passed - task: lint_package_json status: passed - task: lint_repl_help status: passed - task: lint_javascript_src status: passed - task: lint_javascript_cli status: na - task: lint_javascript_examples status: passed - task: lint_javascript_tests status: passed - task: lint_javascript_benchmarks status: na - task: lint_python status: na - task: lint_r status: na - task: lint_c_src status: na - task: lint_c_examples status: na - task: lint_c_benchmarks status: na - task: lint_c_tests_fixtures status: na - task: lint_shell status: na - task: lint_typescript_declarations status: na - task: lint_typescript_tests status: na - task: lint_license_headers status: passed ---
1 parent 4534d81 commit d389d89

34 files changed

+8335
-0
lines changed
Lines changed: 235 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,235 @@
1+
<!--
2+
3+
@license Apache-2.0
4+
5+
Copyright (c) 2025 The Stdlib Authors.
6+
7+
Licensed under the Apache License, Version 2.0 (the "License");
8+
you may not use this file except in compliance with the License.
9+
You may obtain a copy of the License at
10+
11+
http://www.apache.org/licenses/LICENSE-2.0
12+
13+
Unless required by applicable law or agreed to in writing, software
14+
distributed under the License is distributed on an "AS IS" BASIS,
15+
WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
16+
See the License for the specific language governing permissions and
17+
limitations under the License.
18+
19+
-->
20+
21+
# unaryStrided1d
22+
23+
> Apply a one-dimensional strided array function to a list of specified dimensions in an input ndarray and assign results to a provided output ndarray.
24+
25+
<section class="intro">
26+
27+
</section>
28+
29+
<!-- /.intro -->
30+
31+
<section class="usage">
32+
33+
## Usage
34+
35+
```javascript
36+
var unaryStrided1d = require( '@stdlib/ndarray/base/unary-strided1d' );
37+
```
38+
39+
#### unaryStrided1d( fcn, arrays, dims\[, options] )
40+
41+
Applies a one-dimensional strided array function to a list of specified dimensions in an input ndarray and assigns results to a provided output ndarray.
42+
43+
<!-- eslint-disable max-len -->
44+
45+
```javascript
46+
var Float64Array = require( '@stdlib/array/float64' );
47+
var ndarray2array = require( '@stdlib/ndarray/base/to-array' );
48+
var getStride = require( '@stdlib/ndarray/base/stride' );
49+
var getOffset = require( '@stdlib/ndarray/base/offset' );
50+
var getData = require( '@stdlib/ndarray/base/data-buffer' );
51+
var numelDimension = require( '@stdlib/ndarray/base/numel-dimension' );
52+
var gcusum = require( '@stdlib/blas/ext/base/gcusum' ).ndarray;
53+
54+
function wrapper( arrays ) {
55+
var x = arrays[ 0 ];
56+
var y = arrays[ 1 ];
57+
var s = arrays[ 2 ];
58+
return gcusum( numelDimension( x, 0 ), getData( s )[ getOffset( s ) ], getData( x ), getStride( x, 0 ), getOffset( x ), getData( y ), getStride( y, 0 ), getOffset( y ) );
59+
}
60+
61+
// Create data buffers:
62+
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 ] );
63+
var ybuf = new Float64Array( [ 0.0, 0.0, 0.0, 0.0, 0.0, 0.0, 0.0, 0.0, 0.0, 0.0, 0.0, 0.0 ] );
64+
65+
// Define the array shapes:
66+
var xsh = [ 1, 3, 2, 2 ];
67+
var ysh = [ 1, 3, 2, 2 ];
68+
69+
// Define the array strides:
70+
var sx = [ 12, 4, 2, 1 ];
71+
var sy = [ 12, 4, 2, 1 ];
72+
73+
// Define the index offsets:
74+
var ox = 0;
75+
var oy = 0;
76+
77+
// Create an input ndarray-like object:
78+
var x = {
79+
'dtype': 'float64',
80+
'data': xbuf,
81+
'shape': xsh,
82+
'strides': sx,
83+
'offset': ox,
84+
'order': 'row-major'
85+
};
86+
87+
// Create an ndarray-like object for the initial sum:
88+
var initial = {
89+
'dtype': 'float64',
90+
'data': new Float64Array( [ 0.0 ] ),
91+
'shape': [ 1, 3 ],
92+
'strides': [ 0, 0 ],
93+
'offset': 0,
94+
'order': 'row-major'
95+
};
96+
97+
// Create an output ndarray-like object:
98+
var y = {
99+
'dtype': 'float64',
100+
'data': ybuf,
101+
'shape': ysh,
102+
'strides': sy,
103+
'offset': oy,
104+
'order': 'row-major'
105+
};
106+
107+
// Apply strided function:
108+
unaryStrided1d( wrapper, [ x, y, initial ], [ 2, 3 ] );
109+
110+
var arr = ndarray2array( y.data, y.shape, y.strides, y.offset, y.order );
111+
// returns [ [ [ [ 1.0, 3.0 ], [ 6.0, 10.0 ] ], [ [ 5.0, 11.0 ], [ 18.0, 26.0 ] ], [ [ 9.0, 19.0 ], [ 30.0, 42.0 ] ] ] ]
112+
```
113+
114+
The function accepts the following arguments:
115+
116+
- **fcn**: function which will be applied to a one-dimensional input subarray and should update a one-dimensional output subarray with results.
117+
- **arrays**: array-like object containing one input ndarray and one output ndarray, followed by any additional ndarray arguments.
118+
- **dims**: list of dimensions to which to apply a strided array function.
119+
- **options**: function options which are passed through to `fcn` (_optional_).
120+
121+
Each provided ndarray should be an object with the following properties:
122+
123+
- **dtype**: data type.
124+
- **data**: data buffer.
125+
- **shape**: dimensions.
126+
- **strides**: stride lengths.
127+
- **offset**: index offset.
128+
- **order**: specifies whether an ndarray is row-major (C-style) or column major (Fortran-style).
129+
130+
#### TODO: document factory method
131+
132+
</section>
133+
134+
<!-- /.usage -->
135+
136+
<section class="notes">
137+
138+
## Notes
139+
140+
- Any additional ndarray arguments are expected to have the same dimensions as the loop dimensions of the input ndarray. When calling the strided array function, any additional ndarray arguments are provided as zero-dimensional ndarray-like objects.
141+
142+
- The strided array function is expected to have the following signature:
143+
144+
```text
145+
fcn( arrays[, options] )
146+
```
147+
148+
where
149+
150+
- **arrays**: array containing a one-dimensional subarray of the input ndarray, a one-dimensional subarray of the output ndarray, and any additional ndarray arguments as zero-dimensional ndarrays.
151+
- **options**: function options (_optional_).
152+
153+
- For very high-dimensional ndarrays which are non-contiguous, one should consider copying the underlying data to contiguous memory before performing a reduction in order to achieve better performance.
154+
155+
</section>
156+
157+
<!-- /.notes -->
158+
159+
<section class="examples">
160+
161+
## Examples
162+
163+
<!-- eslint-disable max-len -->
164+
165+
<!-- eslint no-undef: "error" -->
166+
167+
```javascript
168+
var discreteUniform = require( '@stdlib/random/array/discrete-uniform' );
169+
var zeros = require( '@stdlib/array/base/zeros' );
170+
var ndarray2array = require( '@stdlib/ndarray/base/to-array' );
171+
var numelDimension = require( '@stdlib/ndarray/base/numel-dimension' );
172+
var getData = require( '@stdlib/ndarray/base/data-buffer' );
173+
var getStride = require( '@stdlib/ndarray/base/stride' );
174+
var getOffset = require( '@stdlib/ndarray/base/offset' );
175+
var gcusum = require( '@stdlib/blas/ext/base/gcusum' ).ndarray;
176+
var unaryStrided1d = require( '@stdlib/ndarray/base/unary-strided1d' );
177+
178+
function wrapper( arrays ) {
179+
var x = arrays[ 0 ];
180+
var y = arrays[ 1 ];
181+
var s = arrays[ 2 ];
182+
return gcusum( numelDimension( x, 0 ), getData( s )[ getOffset( s ) ], getData( x ), getStride( x, 0 ), getOffset( x ), getData( y ), getStride( y, 0 ), getOffset( y ) );
183+
}
184+
185+
var N = 10;
186+
var x = {
187+
'dtype': 'generic',
188+
'data': discreteUniform( N, -5, 5, {
189+
'dtype': 'generic'
190+
}),
191+
'shape': [ 1, 5, 2 ],
192+
'strides': [ 10, 2, 1 ],
193+
'offset': 0,
194+
'order': 'row-major'
195+
};
196+
var initial = {
197+
'dtype': 'generic',
198+
'data': [ 0.0 ],
199+
'shape': [ 1, 2 ],
200+
'strides': [ 0, 0 ],
201+
'offset': 0,
202+
'order': 'row-major'
203+
};
204+
var y = {
205+
'dtype': 'generic',
206+
'data': zeros( N ),
207+
'shape': [ 1, 5, 2 ],
208+
'strides': [ 10, 2, 1 ],
209+
'offset': 0,
210+
'order': 'row-major'
211+
};
212+
213+
unaryStrided1d( wrapper, [ x, y, initial ], [ 1 ] );
214+
215+
console.log( ndarray2array( x.data, x.shape, x.strides, x.offset, x.order ) );
216+
console.log( ndarray2array( y.data, y.shape, y.strides, y.offset, y.order ) );
217+
```
218+
219+
</section>
220+
221+
<!-- /.examples -->
222+
223+
<!-- Section for related `stdlib` packages. Do not manually edit this section, as it is automatically populated. -->
224+
225+
<section class="related">
226+
227+
</section>
228+
229+
<!-- /.related -->
230+
231+
<section class="links">
232+
233+
</section>
234+
235+
<!-- /.links -->
Lines changed: 110 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,110 @@
1+
2+
{{alias}}( fcn, arrays, dims[, options] )
3+
Applies a one-dimensional strided array function to a list of specified
4+
dimensions in an input ndarray and assigns results to a provided output
5+
ndarray.
6+
7+
Each provided "ndarray" should be an object with the following properties:
8+
9+
- dtype: data type.
10+
- data: data buffer.
11+
- shape: dimensions.
12+
- strides: stride lengths.
13+
- offset: index offset.
14+
- order: specifies whether an ndarray is row-major (C-style) or column-major
15+
(Fortran-style).
16+
17+
Any additional ndarray arguments are expected to have the same dimensions as
18+
the loop dimensions of the input ndarray. When calling the strided array
19+
function, any additional ndarray arguments are provided as zero-dimensional
20+
ndarray-like objects.
21+
22+
Parameters
23+
----------
24+
fcn: Function
25+
Function which will be applied to a one-dimensional input subarray and
26+
should update a one-dimensional output subarray with results. The
27+
function should have the following signature:
28+
29+
fcn( arrays[, options] )
30+
31+
where
32+
33+
- arrays: array containing a one-dimensional subarray of the input
34+
ndarray, a one-dimensional subarray of the output ndarray, and any
35+
additional ndarray arguments as zero-dimensional ndarrays.
36+
- options: function options.
37+
38+
arrays: ArrayLikeObject<ndarray>
39+
Array-like object containing one input ndarray and one output ndarray,
40+
followed by any additional ndarray arguments.
41+
42+
dims: Array<integer>
43+
List of dimensions to which to apply a strided array function.
44+
45+
options: Object (optional)
46+
Function options.
47+
48+
Examples
49+
--------
50+
// Define ndarray data and meta data...
51+
> var xbuf = new {{alias:@stdlib/array/float64}}( [ 1.0, 2.0, 3.0, 4.0 ] );
52+
> var ybuf = new {{alias:@stdlib/array/float64}}( [ 0.0, 0.0, 0.0, 0.0 ] );
53+
> var dtype = 'float64';
54+
> var shx = [ 2, 2 ];
55+
> var shy = [ 2, 2 ];
56+
> var sx = [ 2, 1 ];
57+
> var sy = [ 2, 1 ];
58+
> var ox = 0;
59+
> var oy = 0;
60+
> var order = 'row-major';
61+
62+
// Define a wrapper for an extended BLAS function...
63+
> var f = {{alias:@stdlib/blas/ext/base/gcusum}}.ndarray;
64+
> function fcn( arrays ) {
65+
... var x = arrays[ 0 ];
66+
... var y = arrays[ 1 ];
67+
... var s = arrays[ 2 ];
68+
... var N = x.shape[ 0 ];
69+
... var dx = x.data;
70+
... var dy = y.data;
71+
... var sx = x.strides[ 0 ];
72+
... var sy = y.strides[ 0 ];
73+
... var ox = x.offset;
74+
... var oy = y.offset;
75+
... var init = s.data[ s.offset ];
76+
... return f( N, init, dx, sx, ox, dy, sy, oy );
77+
... };
78+
79+
// Using minimal ndarray-like objects...
80+
> x = {
81+
... 'dtype': dtype,
82+
... 'data': xbuf,
83+
... 'shape': shx,
84+
... 'strides': sx,
85+
... 'offset': ox,
86+
... 'order': order
87+
... };
88+
> initial = {
89+
... 'dtype': dtype,
90+
... 'data': new {{alias:@stdlib/array/float64}}( [ 0.0 ] ),
91+
... 'shape': [],
92+
... 'strides': [ 0 ],
93+
... 'offset': 0,
94+
... 'order': order
95+
... };
96+
> y = {
97+
... 'dtype': dtype,
98+
... 'data': ybuf,
99+
... 'shape': shy,
100+
... 'strides': sy,
101+
... 'offset': oy,
102+
... 'order': order
103+
... };
104+
> {{alias}}( fcn, [ x, y, initial ], [ 0, 1 ] );
105+
> y.data
106+
<Float64Array>[ 1.0, 3.0, 6.0, 10.0 ]
107+
108+
See Also
109+
--------
110+

0 commit comments

Comments
 (0)