Skip to content

Commit 9eb7bd3

Browse files
committed
feat: add 9d kernels
--- 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 175e813 commit 9eb7bd3

File tree

6 files changed

+1491
-0
lines changed

6 files changed

+1491
-0
lines changed
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,198 @@
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-depth */
20+
21+
'use strict';
22+
23+
// MODULES //
24+
25+
var isRowMajor = require( '@stdlib/ndarray/base/assert/is-row-major-string' );
26+
27+
28+
// MAIN //
29+
30+
/**
31+
* Tests whether an ndarray contains a specified value.
32+
*
33+
* @private
34+
* @param {Object} x - object containing ndarray meta data
35+
* @param {string} x.dtype - data type
36+
* @param {Collection} x.data - data buffer
37+
* @param {NonNegativeIntegerArray} x.shape - dimensions
38+
* @param {IntegerArray} x.strides - stride lengths
39+
* @param {NonNegativeInteger} x.offset - index offset
40+
* @param {string} x.order - specifies whether `x` is row-major (C-style) or column-major (Fortran-style)
41+
* @param {*} value - search element
42+
* @returns {boolean} result
43+
*
44+
* @example
45+
* var Float64Array = require( '@stdlib/array/float64' );
46+
*
47+
* // Create a data buffer:
48+
* 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 ] );
49+
*
50+
* // Define the shape of the input array:
51+
* var shape = [ 1, 1, 1, 1, 1, 1, 3, 1, 2 ];
52+
*
53+
* // Define the array strides:
54+
* var sx = [ 12, 12, 12, 12, 12, 12, 4, 4, 1 ];
55+
*
56+
* // Define the index offset:
57+
* var ox = 0;
58+
*
59+
* // Create the input ndarray-like object:
60+
* var x = {
61+
* 'dtype': 'float64',
62+
* 'data': xbuf,
63+
* 'shape': shape,
64+
* 'strides': sx,
65+
* 'offset': ox,
66+
* 'order': 'row-major'
67+
* };
68+
*
69+
* // Perform reduction:
70+
* var out = includes9d( x, 6.0 );
71+
* // returns true
72+
*
73+
* out = includes9d( x, 100.0 );
74+
* // returns false
75+
*/
76+
function includes9d( x, value ) {
77+
var xbuf;
78+
var dx0;
79+
var dx1;
80+
var dx2;
81+
var dx3;
82+
var dx4;
83+
var dx5;
84+
var dx6;
85+
var dx7;
86+
var dx8;
87+
var sh;
88+
var S0;
89+
var S1;
90+
var S2;
91+
var S3;
92+
var S4;
93+
var S5;
94+
var S6;
95+
var S7;
96+
var S8;
97+
var sx;
98+
var ix;
99+
var i0;
100+
var i1;
101+
var i2;
102+
var i3;
103+
var i4;
104+
var i5;
105+
var i6;
106+
var i7;
107+
var i8;
108+
109+
// Note on variable naming convention: S#, dx#, dy#, i# where # corresponds to the loop number, with `0` being the innermost loop...
110+
111+
// Extract loop variables for purposes of loop interchange: dimensions and loop offset (pointer) increments...
112+
sh = x.shape;
113+
sx = x.strides;
114+
if ( isRowMajor( x.order ) ) {
115+
// For row-major ndarrays, the last dimensions have the fastest changing indices...
116+
S0 = sh[ 8 ];
117+
S1 = sh[ 7 ];
118+
S2 = sh[ 6 ];
119+
S3 = sh[ 5 ];
120+
S4 = sh[ 4 ];
121+
S5 = sh[ 3 ];
122+
S6 = sh[ 2 ];
123+
S7 = sh[ 1 ];
124+
S8 = sh[ 0 ];
125+
dx0 = sx[ 8 ]; // offset increment for innermost loop
126+
dx1 = sx[ 7 ] - ( S0*sx[8] );
127+
dx2 = sx[ 6 ] - ( S1*sx[7] );
128+
dx3 = sx[ 5 ] - ( S2*sx[6] );
129+
dx4 = sx[ 4 ] - ( S3*sx[5] );
130+
dx5 = sx[ 3 ] - ( S4*sx[4] );
131+
dx6 = sx[ 2 ] - ( S5*sx[3] );
132+
dx7 = sx[ 1 ] - ( S6*sx[2] );
133+
dx8 = sx[ 0 ] - ( S7*sx[1] ); // offset increment for outermost loop
134+
} else { // order === 'column-major'
135+
// For column-major ndarrays, the first dimensions have the fastest changing indices...
136+
S0 = sh[ 0 ];
137+
S1 = sh[ 1 ];
138+
S2 = sh[ 2 ];
139+
S3 = sh[ 3 ];
140+
S4 = sh[ 4 ];
141+
S5 = sh[ 5 ];
142+
S6 = sh[ 6 ];
143+
S7 = sh[ 7 ];
144+
S8 = sh[ 8 ];
145+
dx0 = sx[ 0 ]; // offset increment for innermost loop
146+
dx1 = sx[ 1 ] - ( S0*sx[0] );
147+
dx2 = sx[ 2 ] - ( S1*sx[1] );
148+
dx3 = sx[ 3 ] - ( S2*sx[2] );
149+
dx4 = sx[ 4 ] - ( S3*sx[3] );
150+
dx5 = sx[ 5 ] - ( S4*sx[4] );
151+
dx6 = sx[ 6 ] - ( S5*sx[5] );
152+
dx7 = sx[ 7 ] - ( S6*sx[6] );
153+
dx8 = sx[ 8 ] - ( S7*sx[7] ); // offset increment for outermost loop
154+
}
155+
// Set a pointer to the first indexed element:
156+
ix = x.offset;
157+
158+
// Cache a reference to the input ndarray buffer:
159+
xbuf = x.data;
160+
161+
// Iterate over the ndarray dimensions...
162+
for ( i8 = 0; i8 < S8; i8++ ) {
163+
for ( i7 = 0; i7 < S7; i7++ ) {
164+
for ( i6 = 0; i6 < S6; i6++ ) {
165+
for ( i5 = 0; i5 < S5; i5++ ) {
166+
for ( i4 = 0; i4 < S4; i4++ ) {
167+
for ( i3 = 0; i3 < S3; i3++ ) {
168+
for ( i2 = 0; i2 < S2; i2++ ) {
169+
for ( i1 = 0; i1 < S1; i1++ ) {
170+
for ( i0 = 0; i0 < S0; i0++ ) {
171+
if ( xbuf[ ix ] === value ) {
172+
return true;
173+
}
174+
ix += dx0;
175+
}
176+
ix += dx1;
177+
}
178+
ix += dx2;
179+
}
180+
ix += dx3;
181+
}
182+
ix += dx4;
183+
}
184+
ix += dx5;
185+
}
186+
ix += dx6;
187+
}
188+
ix += dx7;
189+
}
190+
ix += dx8;
191+
}
192+
return false;
193+
}
194+
195+
196+
// EXPORTS //
197+
198+
module.exports = includes9d;

0 commit comments

Comments
 (0)