Skip to content

Commit 4bc9fed

Browse files
committed
feat: add ndarray/base/nullary-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 0b79b81 commit 4bc9fed

File tree

23 files changed

+3275
-0
lines changed

23 files changed

+3275
-0
lines changed
Lines changed: 212 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,212 @@
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+
# nullaryStrided1d
22+
23+
> Apply a one-dimensional strided array function to a list of specified dimensions in an input 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 nullaryStrided1d = require( '@stdlib/ndarray/base/nullary-strided1d' );
37+
```
38+
39+
#### nullaryStrided1d( fcn, arrays, dims\[, options] )
40+
41+
Applies a one-dimensional strided array function to a list of specified dimensions in an input ndarray.
42+
43+
<!-- eslint-disable max-len -->
44+
45+
```javascript
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 data buffers:
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 the array shapes:
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 offsets:
70+
var ox = 0;
71+
72+
// Create an input 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+
// Apply strided function:
93+
nullaryStrided1d( wrapper, [ x, sortOrder ], [ 2, 3 ] );
94+
95+
var arr = ndarray2array( x.data, x.shape, x.strides, x.offset, x.order );
96+
// 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 ] ] ] ]
97+
```
98+
99+
The function accepts the following arguments:
100+
101+
- **fcn**: function which will be applied to a one-dimensional input subarray.
102+
- **arrays**: array-like object containing one input ndarray followed by any additional ndarray arguments.
103+
- **dims**: list of dimensions to which to apply a strided array function.
104+
- **options**: function options which are passed through to `fcn` (_optional_).
105+
106+
Each provided ndarray should be an object with the following properties:
107+
108+
- **dtype**: data type.
109+
- **data**: data buffer.
110+
- **shape**: dimensions.
111+
- **strides**: stride lengths.
112+
- **offset**: index offset.
113+
- **order**: specifies whether an ndarray is row-major (C-style) or column major (Fortran-style).
114+
115+
#### TODO: document factory method
116+
117+
</section>
118+
119+
<!-- /.usage -->
120+
121+
<section class="notes">
122+
123+
## Notes
124+
125+
- 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.
126+
127+
- The strided array function is expected to have the following signature:
128+
129+
```text
130+
fcn( arrays[, options] )
131+
```
132+
133+
where
134+
135+
- **arrays**: array containing a one-dimensional subarray of the input ndarray and any additional ndarray arguments as zero-dimensional ndarrays.
136+
- **options**: function options (_optional_).
137+
138+
- The function iterates over ndarray elements according to the memory layout of the input ndarray.
139+
140+
- For very high-dimensional ndarrays which are non-contiguous, one should consider copying the underlying data to contiguous memory before performing an operation in order to achieve better performance.
141+
142+
</section>
143+
144+
<!-- /.notes -->
145+
146+
<section class="examples">
147+
148+
## Examples
149+
150+
<!-- eslint-disable max-len -->
151+
152+
<!-- eslint no-undef: "error" -->
153+
154+
```javascript
155+
var discreteUniform = require( '@stdlib/random/array/discrete-uniform' );
156+
var ndarray2array = require( '@stdlib/ndarray/base/to-array' );
157+
var numelDimension = require( '@stdlib/ndarray/base/numel-dimension' );
158+
var getData = require( '@stdlib/ndarray/base/data-buffer' );
159+
var getStride = require( '@stdlib/ndarray/base/stride' );
160+
var getOffset = require( '@stdlib/ndarray/base/offset' );
161+
var ndarraylike2scalar = require( '@stdlib/ndarray/base/ndarraylike2scalar' );
162+
var gsorthp = require( '@stdlib/blas/ext/base/gsorthp' ).ndarray;
163+
var nullaryStrided1d = require( '@stdlib/ndarray/base/nullary-strided1d' );
164+
165+
function wrapper( arrays ) {
166+
var x = arrays[ 0 ];
167+
var o = arrays[ 1 ];
168+
return gsorthp( numelDimension( x, 0 ), ndarraylike2scalar( o ), getData( x ), getStride( x, 0 ), getOffset( x ) );
169+
}
170+
171+
var N = 10;
172+
var x = {
173+
'dtype': 'generic',
174+
'data': discreteUniform( N, -5, 5, {
175+
'dtype': 'generic'
176+
}),
177+
'shape': [ 1, 5, 2 ],
178+
'strides': [ 10, 2, 1 ],
179+
'offset': 0,
180+
'order': 'row-major'
181+
};
182+
var sortOrder = {
183+
'dtype': 'generic',
184+
'data': [ 0.0 ],
185+
'shape': [ 1, 2 ],
186+
'strides': [ 0, 0 ],
187+
'offset': 0,
188+
'order': 'row-major'
189+
};
190+
191+
nullaryStrided1d( wrapper, [ x, sortOrder ], [ 1 ] );
192+
193+
console.log( ndarray2array( x.data, x.shape, x.strides, x.offset, x.order ) );
194+
```
195+
196+
</section>
197+
198+
<!-- /.examples -->
199+
200+
<!-- Section for related `stdlib` packages. Do not manually edit this section, as it is automatically populated. -->
201+
202+
<section class="related">
203+
204+
</section>
205+
206+
<!-- /.related -->
207+
208+
<section class="links">
209+
210+
</section>
211+
212+
<!-- /.links -->
Lines changed: 92 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,92 @@
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.
5+
6+
Each provided "ndarray" should be an object with the following properties:
7+
8+
- dtype: data type.
9+
- data: data buffer.
10+
- shape: dimensions.
11+
- strides: stride lengths.
12+
- offset: index offset.
13+
- order: specifies whether an ndarray is row-major (C-style) or column-major
14+
(Fortran-style).
15+
16+
Any additional ndarray arguments are expected to have the same dimensions as
17+
the loop dimensions of the input ndarray. When calling the strided array
18+
function, any additional ndarray arguments are provided as zero-dimensional
19+
ndarray-like objects.
20+
21+
Parameters
22+
----------
23+
fcn: Function
24+
Function which will be applied to a one-dimensional input subarray. The
25+
function should have the following signature:
26+
27+
fcn( arrays[, options] )
28+
29+
where
30+
31+
- arrays: array containing a one-dimensional subarray of the input
32+
ndarray and any additional ndarray arguments as zero-dimensional
33+
ndarrays.
34+
- options: function options.
35+
36+
arrays: ArrayLikeObject<ndarray>
37+
Array-like object containing an input ndarray followed by any additional
38+
ndarray arguments.
39+
40+
dims: Array<integer>
41+
List of dimensions to which to apply a strided array function.
42+
43+
options: Object (optional)
44+
Function options.
45+
46+
Examples
47+
--------
48+
// Define ndarray data and meta data...
49+
> var xbuf = [ 4.0, 3.0, 2.0, 1.0 ];
50+
> var dtype = 'generic';
51+
> var shx = [ 2, 2 ];
52+
> var sx = [ 2, 1 ];
53+
> var ox = 0;
54+
> var order = 'row-major';
55+
56+
// Define a wrapper for an extended BLAS function...
57+
> var f = {{alias:@stdlib/blas/ext/base/gsorthp}}.ndarray;
58+
> function fcn( arrays ) {
59+
... var x = arrays[ 0 ];
60+
... var o = arrays[ 1 ];
61+
... var N = x.shape[ 0 ];
62+
... var dx = x.data;
63+
... var sx = x.strides[ 0 ];
64+
... var ox = x.offset;
65+
... var init = o.data[ o.offset ];
66+
... return f( N, init, dx, sx, ox );
67+
... };
68+
69+
// Using minimal ndarray-like objects...
70+
> var x = {
71+
... 'dtype': dtype,
72+
... 'data': xbuf,
73+
... 'shape': shx,
74+
... 'strides': sx,
75+
... 'offset': ox,
76+
... 'order': order
77+
... };
78+
> var sortOrder = {
79+
... 'dtype': dtype,
80+
... 'data': [ 1.0 ],
81+
... 'shape': [],
82+
... 'strides': [ 0 ],
83+
... 'offset': 0,
84+
... 'order': order
85+
... };
86+
> {{alias}}( fcn, [ x, sortOrder ], [ 0, 1 ] );
87+
> x.data
88+
[ 1.0, 2.0, 3.0, 4.0 ]
89+
90+
See Also
91+
--------
92+
Lines changed: 61 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,61 @@
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+
/* eslint-disable max-len */
20+
21+
'use strict';
22+
23+
var discreteUniform = require( '@stdlib/random/array/discrete-uniform' );
24+
var ndarray2array = require( '@stdlib/ndarray/base/to-array' );
25+
var numelDimension = require( '@stdlib/ndarray/base/numel-dimension' );
26+
var getData = require( '@stdlib/ndarray/base/data-buffer' );
27+
var getStride = require( '@stdlib/ndarray/base/stride' );
28+
var getOffset = require( '@stdlib/ndarray/base/offset' );
29+
var ndarraylike2scalar = require( '@stdlib/ndarray/base/ndarraylike2scalar' );
30+
var gsorthp = require( '@stdlib/blas/ext/base/gsorthp' ).ndarray;
31+
var nullaryStrided1d = require( './../lib' );
32+
33+
function wrapper( arrays ) {
34+
var x = arrays[ 0 ];
35+
var o = arrays[ 1 ];
36+
return gsorthp( numelDimension( x, 0 ), ndarraylike2scalar( o ), getData( x ), getStride( x, 0 ), getOffset( x ) );
37+
}
38+
39+
var N = 10;
40+
var x = {
41+
'dtype': 'generic',
42+
'data': discreteUniform( N, -5, 5, {
43+
'dtype': 'generic'
44+
}),
45+
'shape': [ 1, 5, 2 ],
46+
'strides': [ 10, 2, 1 ],
47+
'offset': 0,
48+
'order': 'row-major'
49+
};
50+
var sortOrder = {
51+
'dtype': 'generic',
52+
'data': [ 0.0 ],
53+
'shape': [ 1, 2 ],
54+
'strides': [ 0, 0 ],
55+
'offset': 0,
56+
'order': 'row-major'
57+
};
58+
59+
nullaryStrided1d( wrapper, [ x, sortOrder ], [ 0 ] );
60+
61+
console.log( ndarray2array( x.data, x.shape, x.strides, x.offset, x.order ) );

0 commit comments

Comments
 (0)