Skip to content

Commit 7140ddd

Browse files
kgrytesahil20021008
authored andcommitted
feat: add initial implementation for ndarray/base/unary-reduce-subarray
--- 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: na - task: lint_package_json status: passed - task: lint_repl_help status: na - task: lint_javascript_src status: passed - task: lint_javascript_cli status: na - task: lint_javascript_examples status: passed - task: lint_javascript_tests status: na - 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 18c5f59 commit 7140ddd

File tree

20 files changed

+2628
-0
lines changed

20 files changed

+2628
-0
lines changed
Lines changed: 50 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,50 @@
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+
var discreteUniform = require( '@stdlib/random/array/discrete-uniform' );
22+
var filled = require( '@stdlib/array/base/filled' );
23+
var ndarray2array = require( '@stdlib/ndarray/base/to-array' );
24+
var every = require( '@stdlib/ndarray/base/every' );
25+
var reduceSubarray = require( './../lib' );
26+
27+
var N = 10;
28+
var x = {
29+
'dtype': 'generic',
30+
'data': discreteUniform( N, -5, 5, {
31+
'dtype': 'generic'
32+
}),
33+
'shape': [ 1, 5, 2 ],
34+
'strides': [ 10, 2, 1 ],
35+
'offset': 0,
36+
'order': 'row-major'
37+
};
38+
var y = {
39+
'dtype': 'generic',
40+
'data': filled( false, 2 ),
41+
'shape': [ 1, 2 ],
42+
'strides': [ 2, 1 ],
43+
'offset': 0,
44+
'order': 'row-major'
45+
};
46+
47+
reduceSubarray( every, [ x, y ], [ 1 ] );
48+
49+
console.log( ndarray2array( x.data, x.shape, x.strides, x.offset, x.order ) );
50+
console.log( ndarray2array( y.data, y.shape, y.strides, y.offset, y.order ) );
Lines changed: 92 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,92 @@
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 without = require( '@stdlib/array/base/without' );
24+
25+
26+
// MAIN //
27+
28+
/**
29+
* Performs a reduction over a list of specified dimensions in an input ndarray and assigns results to a provided output ndarray.
30+
*
31+
* @private
32+
* @param {Function} fcn - reduction function
33+
* @param {Array<Object>} arrays - ndarrays
34+
* @param {Options} opts - function options
35+
* @returns {void}
36+
*
37+
* @example
38+
* var Float64Array = require( '@stdlib/array/float64' );
39+
* var filled = require( '@stdlib/array/base/filled' );
40+
* var ndarray2array = require( '@stdlib/ndarray/base/to-array' );
41+
* var base = require( '@stdlib/ndarray/base/every' );
42+
*
43+
* // Create data buffers:
44+
* 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 ] );
45+
* var ybuf = filled( false, 1 );
46+
*
47+
* // Define the array shapes:
48+
* var xsh = [ 2, 2 ];
49+
* var ysh = [];
50+
*
51+
* // Define the array strides:
52+
* var sx = [ 6, 1 ];
53+
* var sy = [ 0 ];
54+
*
55+
* // Define the index offsets:
56+
* var ox = 0;
57+
* var oy = 0;
58+
*
59+
* // Create an input ndarray-like object:
60+
* var x = {
61+
* 'dtype': 'float64',
62+
* 'data': xbuf,
63+
* 'shape': xsh,
64+
* 'strides': sx,
65+
* 'offset': ox,
66+
* 'order': 'row-major'
67+
* };
68+
*
69+
* // Create an output ndarray-like object:
70+
* var y = {
71+
* 'dtype': 'generic',
72+
* 'data': ybuf,
73+
* 'shape': ysh,
74+
* 'strides': sy,
75+
* 'offset': oy,
76+
* 'order': 'row-major'
77+
* };
78+
*
79+
* // Perform a reduction:
80+
* unary0d( base, [ x, y ], {} );
81+
*
82+
* var v = y.data;
83+
* // returns [ true ]
84+
*/
85+
function unary0d( fcn, arrays, opts ) {
86+
arrays[1].data[ arrays[1].offset ] = fcn( without( arrays, 1 ), opts );
87+
}
88+
89+
90+
// EXPORTS //
91+
92+
module.exports = unary0d;
Lines changed: 97 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,97 @@
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 without = require( '@stdlib/array/base/without' );
24+
25+
26+
// MAIN //
27+
28+
/**
29+
* Performs a reduction over a list of specified dimensions in an input ndarray and assigns results to a provided output ndarray.
30+
*
31+
* @private
32+
* @param {Function} fcn - reduction function
33+
* @param {Array<Object>} arrays - ndarrays
34+
* @param {Options} opts - function options
35+
* @returns {void}
36+
*
37+
* @example
38+
* var toAccessorArray = require( '@stdlib/array/base/to-accessor-array' );
39+
* var accessors = require( '@stdlib/array/base/accessors' );
40+
* var Float64Array = require( '@stdlib/array/float64' );
41+
* var filled = require( '@stdlib/array/base/filled' );
42+
* var ndarray2array = require( '@stdlib/ndarray/base/to-array' );
43+
* var base = require( '@stdlib/ndarray/base/every' );
44+
*
45+
* // Create data buffers:
46+
* var xbuf = toAccessorArray( 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 ] ) );
47+
* var ybuf = toAccessorArray( filled( false, 1 ) );
48+
*
49+
* // Define the array shapes:
50+
* var xsh = [ 2, 2 ];
51+
* var ysh = [];
52+
*
53+
* // Define the array strides:
54+
* var sx = [ 6, 1 ];
55+
* var sy = [ 0 ];
56+
*
57+
* // Define the index offsets:
58+
* var ox = 0;
59+
* var oy = 0;
60+
*
61+
* // Create an input ndarray-like object:
62+
* var x = {
63+
* 'dtype': 'float64',
64+
* 'data': xbuf,
65+
* 'shape': xsh,
66+
* 'strides': sx,
67+
* 'offset': ox,
68+
* 'order': 'row-major',
69+
* 'accessors': accessors( xbuf ).accessors
70+
* };
71+
*
72+
* // Create an output ndarray-like object:
73+
* var y = {
74+
* 'dtype': 'generic',
75+
* 'data': ybuf,
76+
* 'shape': ysh,
77+
* 'strides': sy,
78+
* 'offset': oy,
79+
* 'order': 'row-major',
80+
* 'accessors': accessors( ybuf ).accessors
81+
* };
82+
*
83+
* // Perform a reduction:
84+
* unary0d( base, [ x, y ], {} );
85+
*
86+
* var v = y.data.get( 0 );
87+
* // returns true
88+
*/
89+
function unary0d( fcn, arrays, opts ) {
90+
var y = arrays[ 1 ];
91+
y.accessors[ 1 ]( y.data, y.offset, fcn( without( arrays, 1 ), opts ) );
92+
}
93+
94+
95+
// EXPORTS //
96+
97+
module.exports = unary0d;
Lines changed: 140 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,140 @@
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 incrementOffsets = require( './increment_offsets.js' );
24+
var setViewOffsets = require( './set_view_offsets.js' );
25+
var offsets = require( './offsets.js' );
26+
27+
28+
// MAIN //
29+
30+
/**
31+
* Performs a reduction over an input ndarray and assigns results to a provided output ndarray.
32+
*
33+
* @private
34+
* @param {Function} fcn - reduction function
35+
* @param {Array<Object>} arrays - ndarrays
36+
* @param {Array<Object>} views - initialized ndarray-like objects representing sub-array views
37+
* @param {IntegerArray} strides - loop dimension strides for the input ndarray
38+
* @param {Options} opts - function options
39+
* @returns {void}
40+
*
41+
* @example
42+
* var Float64Array = require( '@stdlib/array/float64' );
43+
* var filled = require( '@stdlib/array/base/filled' );
44+
* var ndarray2array = require( '@stdlib/ndarray/base/to-array' );
45+
* var base = require( '@stdlib/ndarray/base/every' );
46+
*
47+
* // Create data buffers:
48+
* 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 ] );
49+
* var ybuf = filled( false, 3 );
50+
*
51+
* // Define the array shapes:
52+
* var xsh = [ 3, 2, 2 ];
53+
* var ysh = [ 3 ];
54+
*
55+
* // Define the array strides:
56+
* var sx = [ 4, 2, 1 ];
57+
* var sy = [ 1 ];
58+
*
59+
* // Define the index offsets:
60+
* var ox = 0;
61+
* var oy = 0;
62+
*
63+
* // Create an input ndarray-like object:
64+
* var x = {
65+
* 'dtype': 'float64',
66+
* 'data': xbuf,
67+
* 'shape': xsh,
68+
* 'strides': sx,
69+
* 'offset': ox,
70+
* 'order': 'row-major'
71+
* };
72+
*
73+
* // Create an output ndarray-like object:
74+
* var y = {
75+
* 'dtype': 'generic',
76+
* 'data': ybuf,
77+
* 'shape': ysh,
78+
* 'strides': sy,
79+
* 'offset': oy,
80+
* 'order': 'row-major'
81+
* };
82+
*
83+
* // Initialize ndarray-like objects representing sub-array views:
84+
* var views = [
85+
* {
86+
* 'dtype': x.dtype,
87+
* 'data': x.data,
88+
* 'shape': [ 2, 2 ],
89+
* 'strides': [ 2, 1 ],
90+
* 'offset': x.offset,
91+
* 'order': x.order
92+
* }
93+
* ];
94+
*
95+
* // Perform a reduction:
96+
* unary1d( base, [ x, y ], views, [ 4 ], {} );
97+
*
98+
* var arr = ndarray2array( y.data, y.shape, y.strides, y.offset, y.order );
99+
* // returns [ true, false, true ]
100+
*/
101+
function unary1d( fcn, arrays, views, strides, opts ) {
102+
var ybuf;
103+
var dv0;
104+
var sh;
105+
var S0;
106+
var iv;
107+
var i0;
108+
var y;
109+
var i;
110+
111+
// Note on variable naming convention: S#, dv#, i# where # corresponds to the loop number, with `0` being the innermost loop...
112+
113+
// Resolve the output ndarray and associated shape:
114+
y = arrays[ 1 ];
115+
sh = y.shape;
116+
117+
// Extract loop variables for purposes of loop interchange: dimensions and loop offset (pointer) increments...
118+
S0 = sh[ 0 ];
119+
dv0 = [ strides[0] ];
120+
for ( i = 1; i < arrays.length; i++ ) {
121+
dv0.push( arrays[i].strides[0] );
122+
}
123+
// Resolve a list of pointers to the first indexed elements in the respective ndarrays:
124+
iv = offsets( arrays );
125+
126+
// Cache a reference to the output ndarray buffer:
127+
ybuf = y.data;
128+
129+
// Iterate over the non-reduced ndarray dimensions...
130+
for ( i0 = 0; i0 < S0; i0++ ) {
131+
setViewOffsets( views, iv );
132+
ybuf[ iv[1] ] = fcn( views, opts );
133+
incrementOffsets( iv, dv0 );
134+
}
135+
}
136+
137+
138+
// EXPORTS //
139+
140+
module.exports = unary1d;

0 commit comments

Comments
 (0)