Skip to content

Commit 6736c64

Browse files
kgrytesahil20021008
authored andcommitted
docs: add README and repl help
--- 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: na - 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 4ce5e8e commit 6736c64

File tree

6 files changed

+290
-12
lines changed

6 files changed

+290
-12
lines changed
Lines changed: 192 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,192 @@
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+
# unaryReduceSubarray
22+
23+
> Perform a reduction over 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 unaryReduceSubarray = require( '@stdlib/ndarray/base/unary-reduce-subarray' );
37+
```
38+
39+
#### unaryReduceSubarray( fcn, arrays, dims\[, options] )
40+
41+
Performs a reduction over 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 filled = require( '@stdlib/array/base/filled' );
48+
var ndarray2array = require( '@stdlib/ndarray/base/to-array' );
49+
var every = require( '@stdlib/ndarray/base/every' );
50+
51+
// Create data buffers:
52+
var xbuf = new Float64Array( [ 1.0, 2.0, 3.0, 4.0, 5.0, 0.0, 7.0, 8.0, 9.0, 10.0, 11.0, 12.0 ] );
53+
var ybuf = filled( false, 3 );
54+
55+
// Define the array shapes:
56+
var xsh = [ 1, 3, 2, 2 ];
57+
var ysh = [ 1, 3 ];
58+
59+
// Define the array strides:
60+
var sx = [ 12, 4, 2, 1 ];
61+
var sy = [ 3, 1 ];
62+
63+
// Define the index offsets:
64+
var ox = 0;
65+
var oy = 0;
66+
67+
// Create an input ndarray-like object:
68+
var x = {
69+
'dtype': 'float64',
70+
'data': xbuf,
71+
'shape': xsh,
72+
'strides': sx,
73+
'offset': ox,
74+
'order': 'row-major'
75+
};
76+
77+
// Create an output ndarray-like object:
78+
var y = {
79+
'dtype': 'generic',
80+
'data': ybuf,
81+
'shape': ysh,
82+
'strides': sy,
83+
'offset': oy,
84+
'order': 'row-major'
85+
};
86+
87+
// Perform a reduction:
88+
unaryReduceSubarray( every, [ x, y ], [ 2, 3 ] );
89+
90+
var arr = ndarray2array( y.data, y.shape, y.strides, y.offset, y.order );
91+
// returns [ [ true, false, true ] ]
92+
```
93+
94+
The function accepts the following arguments:
95+
96+
- **fcn**: function which will be applied to a subarray and should reduce the subarray to a single scalar value.
97+
- **arrays**: array-like object containing one input ndarray and one output ndarray, followed by any additional ndarray arguments.
98+
- **dims**: list of dimensions over which to perform a reduction.
99+
- **options**: function options which are passed through to `fcn` (_optional_).
100+
101+
Each provided ndarray should be an object with the following properties:
102+
103+
- **dtype**: data type.
104+
- **data**: data buffer.
105+
- **shape**: dimensions.
106+
- **strides**: stride lengths.
107+
- **offset**: index offset.
108+
- **order**: specifies whether an ndarray is row-major (C-style) or column major (Fortran-style).
109+
110+
</section>
111+
112+
<!-- /.usage -->
113+
114+
<section class="notes">
115+
116+
## Notes
117+
118+
- The output ndarray and any additional ndarray arguments are expected to have the same dimensions as the non-reduced dimensions of the input ndarray. When calling the reduction function, any additional ndarray arguments are provided as zero-dimensional ndarray-like objects.
119+
120+
- The reduction function is expected to have the following signature:
121+
122+
```text
123+
fcn( arrays[, options] )
124+
```
125+
126+
where
127+
128+
- **arrays**: array containing a subarray from the input ndarray and any additional ndarray arguments as zero-dimensional ndarrays.
129+
- **options**: function options (_optional_).
130+
131+
- 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.
132+
133+
</section>
134+
135+
<!-- /.notes -->
136+
137+
<section class="examples">
138+
139+
## Examples
140+
141+
<!-- eslint no-undef: "error" -->
142+
143+
```javascript
144+
var discreteUniform = require( '@stdlib/random/array/discrete-uniform' );
145+
var filled = require( '@stdlib/array/base/filled' );
146+
var ndarray2array = require( '@stdlib/ndarray/base/to-array' );
147+
var every = require( '@stdlib/ndarray/base/every' );
148+
var unaryReduceSubarray = require( '@stdlib/ndarray/base/unary-reduce-subarray' );
149+
150+
var N = 10;
151+
var x = {
152+
'dtype': 'generic',
153+
'data': discreteUniform( N, -5, 5, {
154+
'dtype': 'generic'
155+
}),
156+
'shape': [ 1, 5, 2 ],
157+
'strides': [ 10, 2, 1 ],
158+
'offset': 0,
159+
'order': 'row-major'
160+
};
161+
var y = {
162+
'dtype': 'generic',
163+
'data': filled( false, 2 ),
164+
'shape': [ 1, 2 ],
165+
'strides': [ 2, 1 ],
166+
'offset': 0,
167+
'order': 'row-major'
168+
};
169+
170+
unaryReduceSubarray( every, [ x, y ], [ 1 ] );
171+
172+
console.log( ndarray2array( x.data, x.shape, x.strides, x.offset, x.order ) );
173+
console.log( ndarray2array( y.data, y.shape, y.strides, y.offset, y.order ) );
174+
```
175+
176+
</section>
177+
178+
<!-- /.examples -->
179+
180+
<!-- Section for related `stdlib` packages. Do not manually edit this section, as it is automatically populated. -->
181+
182+
<section class="related">
183+
184+
</section>
185+
186+
<!-- /.related -->
187+
188+
<section class="links">
189+
190+
</section>
191+
192+
<!-- /.links -->
Lines changed: 86 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,86 @@
1+
2+
{{alias}}( fcn, arrays, dims[, options] )
3+
Performs a reduction over a list of specified dimensions in an input ndarray
4+
and assigns results to a provided output 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+
The output ndarray and any additional ndarray arguments are expected to have
17+
the same dimensions as the non-reduced dimensions of the input ndarray. When
18+
calling the reduction function, any additional ndarray arguments are
19+
provided as zero-dimensional ndarray-like objects.
20+
21+
Parameters
22+
----------
23+
fcn: Function
24+
Function which will be applied to a subarray and should reduce the
25+
subarray to a single scalar value. The function should have the
26+
following signature:
27+
28+
fcn( arrays[, options] )
29+
30+
where
31+
32+
- arrays: array containing subarray from the input ndarray and any
33+
additional ndarray arguments as zero-dimensional ndarrays.
34+
- options: function options.
35+
36+
arrays: ArrayLikeObject<ndarray>
37+
Array-like object containing one input ndarray and one output ndarray,
38+
followed by any additional ndarray arguments.
39+
40+
dims: Array<integer>
41+
List of dimensions over which to perform a reduction.
42+
43+
options: Object (optional)
44+
Function options.
45+
46+
Examples
47+
--------
48+
// Define ndarray data and meta data...
49+
> var xbuf = new {{alias:@stdlib/array/float64}}( [ 1.0, 2.0, 3.0, 4.0 ] );
50+
> var ybuf = new {{alias:@stdlib/array/float64}}( [ 0.0 ] );
51+
> var dtype = 'float64';
52+
> var shx = [ 2, 2 ];
53+
> var shy = [];
54+
> var sx = [ 2, 1 ];
55+
> var sy = [ 0 ];
56+
> var ox = 0;
57+
> var oy = 0;
58+
> var order = 'row-major';
59+
60+
// Define a trivial reduction function...
61+
> function fcn( arrays ) { return arrays[0].data[ arrays[0].offset ]; };
62+
63+
// Using minimal ndarray-like objects...
64+
> x = {
65+
... 'dtype': dtype,
66+
... 'data': xbuf,
67+
... 'shape': shx,
68+
... 'strides': sx,
69+
... 'offset': ox,
70+
... 'order': order
71+
... };
72+
> y = {
73+
... 'dtype': dtype,
74+
... 'data': ybuf,
75+
... 'shape': shy,
76+
... 'strides': sy,
77+
... 'offset': oy,
78+
... 'order': order
79+
... };
80+
> {{alias}}( fcn, [ x, y ], [ 0, 1 ] );
81+
> y.data
82+
<Float64Array>[ 1.0 ]
83+
84+
See Also
85+
--------
86+

lib/node_modules/@stdlib/ndarray/base/unary-reduce-subarray/examples/index.js

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -22,7 +22,7 @@ var discreteUniform = require( '@stdlib/random/array/discrete-uniform' );
2222
var filled = require( '@stdlib/array/base/filled' );
2323
var ndarray2array = require( '@stdlib/ndarray/base/to-array' );
2424
var every = require( '@stdlib/ndarray/base/every' );
25-
var reduceSubarray = require( './../lib' );
25+
var unaryReduceSubarray = require( './../lib' );
2626

2727
var N = 10;
2828
var x = {
@@ -44,7 +44,7 @@ var y = {
4444
'order': 'row-major'
4545
};
4646

47-
reduceSubarray( every, [ x, y ], [ 1 ] );
47+
unaryReduceSubarray( every, [ x, y ], [ 1 ] );
4848

4949
console.log( ndarray2array( x.data, x.shape, x.strides, x.offset, x.order ) );
5050
console.log( ndarray2array( y.data, y.shape, y.strides, y.offset, y.order ) );

lib/node_modules/@stdlib/ndarray/base/unary-reduce-subarray/lib/index.js

Lines changed: 5 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -19,7 +19,7 @@
1919
'use strict';
2020

2121
/**
22-
* Return a function for performing a reduction over a list of specified dimensions in an input ndarray and assigning results to a provided output ndarray.
22+
* Perform a reduction over a list of specified dimensions in an input ndarray and assign results to a provided output ndarray.
2323
*
2424
* @module @stdlib/ndarray/base/unary-reduce-subarray
2525
*
@@ -28,7 +28,7 @@
2828
* var filled = require( '@stdlib/array/base/filled' );
2929
* var ndarray2array = require( '@stdlib/ndarray/base/to-array' );
3030
* var every = require( '@stdlib/ndarray/base/every' );
31-
* var reduceSubarray = require( '@stdlib/ndarray/base/unary-reduce-subarray' );
31+
* var unaryReduceSubarray = require( '@stdlib/ndarray/base/unary-reduce-subarray' );
3232
*
3333
* // Create data buffers:
3434
* var xbuf = new Float64Array( [ 1.0, 2.0, 3.0, 4.0, 5.0, 0.0, 7.0, 8.0, 9.0, 10.0, 11.0, 12.0 ] );
@@ -67,7 +67,7 @@
6767
* };
6868
*
6969
* // Perform a reduction:
70-
* reduceSubarray( every, [ x, y ], [ 2, 3 ] );
70+
* unaryReduceSubarray( every, [ x, y ], [ 2, 3 ] );
7171
*
7272
* var arr = ndarray2array( y.data, y.shape, y.strides, y.offset, y.order );
7373
* // returns [ [ true, false, true ] ]
@@ -77,7 +77,7 @@
7777
* var filled = require( '@stdlib/array/base/filled' );
7878
* var ndarray2array = require( '@stdlib/ndarray/base/to-array' );
7979
* var base = require( '@stdlib/ndarray/base/every' );
80-
* var reduceSubarray = require( '@stdlib/ndarray/base/unary-reduce-subarray' );
80+
* var unaryReduceSubarray = require( '@stdlib/ndarray/base/unary-reduce-subarray' );
8181
*
8282
* // Create data buffers:
8383
* var xbuf = new Float64Array( [ 1.0, 2.0, 3.0, 4.0, 5.0, 0.0, 7.0, 8.0, 9.0, 10.0, 11.0, 12.0 ] );
@@ -116,7 +116,7 @@
116116
* };
117117
*
118118
* // Create a function for performing a reduction over subarrays:
119-
* var every = reduceSubarray.factory( base );
119+
* var every = unaryReduceSubarray.factory( base );
120120
* // returns <Function>
121121
*
122122
* // Perform a reduction:

lib/node_modules/@stdlib/ndarray/base/unary-reduce-subarray/lib/main.js

Lines changed: 3 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -153,12 +153,12 @@ var MAX_DIMS = UNARY.length - 1;
153153
* };
154154
*
155155
* // Perform a reduction:
156-
* reduceSubarray( every, [ x, y ], [ 2, 3 ] );
156+
* unaryReduceSubarray( every, [ x, y ], [ 2, 3 ] );
157157
*
158158
* var arr = ndarray2array( y.data, y.shape, y.strides, y.offset, y.order );
159159
* // returns [ [ true, false, true ] ]
160160
*/
161-
function reduceSubarray( fcn, arrays, dims, options ) { // eslint-disable-line max-statements
161+
function unaryReduceSubarray( fcn, arrays, dims, options ) { // eslint-disable-line max-statements
162162
var views;
163163
var ndims;
164164
var ldims;
@@ -335,4 +335,4 @@ function reduceSubarray( fcn, arrays, dims, options ) { // eslint-disable-line m
335335

336336
// EXPORTS //
337337

338-
module.exports = reduceSubarray;
338+
module.exports = unaryReduceSubarray;

lib/node_modules/@stdlib/ndarray/base/unary-reduce-subarray/test/test.js

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -21,14 +21,14 @@
2121
// MODULES //
2222

2323
var tape = require( 'tape' );
24-
var reduceSubarray = require( './../lib' );
24+
var unaryReduceSubarray = require( './../lib' );
2525

2626

2727
// TESTS //
2828

2929
tape( 'main export is a function', function test( t ) {
3030
t.ok( true, __filename );
31-
t.strictEqual( typeof reduceSubarray, 'function', 'main export is a function' );
31+
t.strictEqual( typeof unaryReduceSubarray, 'function', 'main export is a function' );
3232
t.end();
3333
});
3434

0 commit comments

Comments
 (0)