Skip to content

Commit 5358d7b

Browse files
committed
feat: add nd accessors kernel
--- 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: na - task: lint_repl_help status: na - task: lint_javascript_src status: passed - task: lint_javascript_cli status: na - task: lint_javascript_examples status: na - 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 9d4bbdf commit 5358d7b

File tree

1 file changed

+192
-0
lines changed

1 file changed

+192
-0
lines changed
Lines changed: 192 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,192 @@
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 numel = require( '@stdlib/ndarray/base/numel' );
24+
var vind2bind = require( '@stdlib/ndarray/base/vind2bind' );
25+
26+
27+
// VARIABLES //
28+
29+
var MODE = 'throw';
30+
31+
32+
// MAIN //
33+
34+
/**
35+
* Applies a binary callback to elements in n-dimensional input ndarrays and assigns results to elements in an equivalently shaped output ndarray.
36+
*
37+
* @private
38+
* @param {Object} x - object containing input ndarray meta data
39+
* @param {string} x.dtype - data type
40+
* @param {Collection} x.data - data buffer
41+
* @param {NonNegativeIntegerArray} x.shape - dimensions
42+
* @param {IntegerArray} x.strides - stride lengths
43+
* @param {NonNegativeInteger} x.offset - index offset
44+
* @param {string} x.order - specifies whether `x` is row-major (C-style) or column-major (Fortran-style)
45+
* @param {Array<Function>} x.accessors - data buffer accessors
46+
* @param {Object} y - object containing input ndarray meta data
47+
* @param {string} y.dtype - data type
48+
* @param {Collection} y.data - data buffer
49+
* @param {NonNegativeIntegerArray} y.shape - dimensions
50+
* @param {IntegerArray} y.strides - stride lengths
51+
* @param {NonNegativeInteger} y.offset - index offset
52+
* @param {string} y.order - specifies whether `y` is row-major (C-style) or column-major (Fortran-style)
53+
* @param {Array<Function>} y.accessors - data buffer accessors
54+
* @param {Object} z - object containing output ndarray meta data
55+
* @param {string} z.dtype - data type
56+
* @param {Collection} z.data - data buffer
57+
* @param {NonNegativeIntegerArray} z.shape - dimensions
58+
* @param {IntegerArray} z.strides - stride lengths
59+
* @param {NonNegativeInteger} z.offset - index offset
60+
* @param {string} z.order - specifies whether `z` is row-major (C-style) or column-major (Fortran-style)
61+
* @param {Array<Function>} z.accessors - data buffer accessors
62+
* @param {Callback} fcn - binary callback
63+
* @returns {void}
64+
*
65+
* @example
66+
* var toAccessorArray = require( '@stdlib/array/base/to-accessor-array' );
67+
* var accessors = require( '@stdlib/array/base/accessors' );
68+
* var copy = require( '@stdlib/array/base/copy' );
69+
*
70+
* function fcn( x, y ) {
71+
* return x + y;
72+
* }
73+
*
74+
* // Create data buffers:
75+
* var xbuf = toAccessorArray( [ 1.0, 2.0, 3.0, 4.0, 5.0, 6.0, 7.0, 8.0, 9.0, 10.0, 11.0, 12.0 ] );
76+
* var ybuf = toAccessorArray( [ 1.0, 1.0, 1.0, 1.0, 1.0, 1.0, 1.0, 1.0, 1.0, 1.0, 1.0, 1.0 ] );
77+
* var zbuf = toAccessorArray( [ 0.0, 0.0, 0.0, 0.0, 0.0, 0.0, 0.0, 0.0, 0.0, 0.0, 0.0, 0.0 ] );
78+
*
79+
* // Define the shape of the input and output arrays:
80+
* var shape = [ 6, 2 ];
81+
*
82+
* // Define the array strides:
83+
* var sx = [ 2, 1 ];
84+
* var sy = [ 2, 1 ];
85+
* var sz = [ 2, 1 ];
86+
*
87+
* // Define the index offsets:
88+
* var ox = 0;
89+
* var oy = 0;
90+
* var oz = 0;
91+
*
92+
* // Create the input and output ndarray-like objects:
93+
* var x = {
94+
* 'dtype': 'generic',
95+
* 'data': xbuf,
96+
* 'shape': shape,
97+
* 'strides': sx,
98+
* 'offset': ox,
99+
* 'order': 'row-major',
100+
* 'accessors': accessors( xbuf ).accessors
101+
* };
102+
* var y = {
103+
* 'dtype': 'generic',
104+
* 'data': ybuf,
105+
* 'shape': shape,
106+
* 'strides': sy,
107+
* 'offset': oy,
108+
* 'order': 'row-major',
109+
* 'accessors': accessors( ybuf ).accessors
110+
* };
111+
* var z = {
112+
* 'dtype': 'generic',
113+
* 'data': zbuf,
114+
* 'shape': shape,
115+
* 'strides': sz,
116+
* 'offset': oz,
117+
* 'order': 'row-major',
118+
* 'accessors': accessors( zbuf ).accessors
119+
* };
120+
*
121+
* // Apply the binary function:
122+
* binarynd( x, y, z, fcn );
123+
*
124+
* console.log( copy( z.data ) );
125+
* // => [ 2.0, 3.0, 4.0, 5.0, 6.0, 7.0, 8.0, 9.0, 10.0, 11.0, 12.0, 13.0 ]
126+
*/
127+
function binarynd( x, y, z, fcn ) {
128+
var xbuf;
129+
var ybuf;
130+
var zbuf;
131+
var ordx;
132+
var ordy;
133+
var ordz;
134+
var xget;
135+
var yget;
136+
var zset;
137+
var len;
138+
var sh;
139+
var sx;
140+
var sy;
141+
var sz;
142+
var ox;
143+
var oy;
144+
var oz;
145+
var ix;
146+
var iy;
147+
var iz;
148+
var i;
149+
150+
sh = x.shape;
151+
152+
// Compute the total number of elements over which to iterate:
153+
len = numel( sh );
154+
155+
// Cache references to the input and output ndarray data buffers:
156+
xbuf = x.data;
157+
ybuf = y.data;
158+
zbuf = z.data;
159+
160+
// Cache references to the respective stride arrays:
161+
sx = x.strides;
162+
sy = y.strides;
163+
sz = z.strides;
164+
165+
// Cache the indices of the first indexed elements in the respective ndarrays:
166+
ox = x.offset;
167+
oy = y.offset;
168+
oz = z.offset;
169+
170+
// Cache the respective array orders:
171+
ordx = x.order;
172+
ordy = y.order;
173+
ordz = z.order;
174+
175+
// Cache accessors:
176+
xget = x.accessors[ 0 ];
177+
yget = y.accessors[ 0 ];
178+
zset = z.accessors[ 1 ];
179+
180+
// Iterate over each element based on the linear **view** index, regardless as to how the data is stored in memory...
181+
for ( i = 0; i < len; i++ ) {
182+
ix = vind2bind( sh, sx, ox, ordx, i, MODE );
183+
iy = vind2bind( sh, sy, oy, ordy, i, MODE );
184+
iz = vind2bind( sh, sz, oz, ordz, i, MODE );
185+
zset( zbuf, iz, fcn( xget( xbuf, ix ), yget( ybuf, iy ) ) );
186+
}
187+
}
188+
189+
190+
// EXPORTS //
191+
192+
module.exports = binarynd;

0 commit comments

Comments
 (0)