Skip to content

Commit 7631d15

Browse files
committed
feat: add nd 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 9eb7bd3 commit 7631d15

File tree

3 files changed

+375
-0
lines changed

3 files changed

+375
-0
lines changed
Lines changed: 120 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,120 @@
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+
* Tests whether an ndarray contains a specified value.
36+
*
37+
* @private
38+
* @param {Object} x - object containing 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 {*} value - search element
46+
* @returns {boolean} result
47+
*
48+
* @example
49+
* var Float64Array = require( '@stdlib/array/float64' );
50+
*
51+
* // Create a data buffer:
52+
* var xbuf = new Float64Array( [ 1.0, 2.0, 3.0, 4.0, 5.0, 6.0, 7.0, 8.0 ] );
53+
*
54+
* // Define the shape of the input array:
55+
* var shape = [ 2, 2 ];
56+
*
57+
* // Define the array strides:
58+
* var sx = [ 4, 1 ];
59+
*
60+
* // Define the index offset:
61+
* var ox = 0;
62+
*
63+
* // Create the input ndarray-like object:
64+
* var x = {
65+
* 'dtype': 'float64',
66+
* 'data': xbuf,
67+
* 'shape': shape,
68+
* 'strides': sx,
69+
* 'offset': ox,
70+
* 'order': 'row-major'
71+
* };
72+
*
73+
* // Perform reduction:
74+
* var out = includesnd( x, 6.0 );
75+
* // returns true
76+
*
77+
* out = includesnd( x, 100.0 );
78+
* // returns false
79+
*/
80+
function includesnd( x, value ) {
81+
var xbuf;
82+
var ordx;
83+
var len;
84+
var sh;
85+
var sx;
86+
var ox;
87+
var ix;
88+
var i;
89+
90+
sh = x.shape;
91+
92+
// Compute the total number of elements over which to iterate:
93+
len = numel( sh );
94+
95+
// Cache a reference to the output ndarray data buffer:
96+
xbuf = x.data;
97+
98+
// Cache a reference to the stride array:
99+
sx = x.strides;
100+
101+
// Cache the index of the first indexed element:
102+
ox = x.offset;
103+
104+
// Cache the array order:
105+
ordx = x.order;
106+
107+
// Iterate over each element based on the linear **view** index, regardless as to how the data is stored in memory...
108+
for ( i = 0; i < len; i++ ) {
109+
ix = vind2bind( sh, sx, ox, ordx, i, MODE );
110+
if ( xbuf[ ix ] === value ) {
111+
return true;
112+
}
113+
}
114+
return false;
115+
}
116+
117+
118+
// EXPORTS //
119+
120+
module.exports = includesnd;
Lines changed: 127 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,127 @@
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+
* Tests whether an ndarray contains a specified value.
36+
*
37+
* @private
38+
* @param {Object} x - object containing 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 {*} value - search element
47+
* @returns {boolean} result
48+
*
49+
* @example
50+
* var toAccessorArray = require( '@stdlib/array/base/to-accessor-array' );
51+
* var accessors = require( '@stdlib/array/base/accessors' );
52+
*
53+
* // Create a data buffer:
54+
* var xbuf = toAccessorArray( [ 1.0, 2.0, 3.0, 4.0, 5.0, 6.0, 7.0, 8.0 ] );
55+
*
56+
* // Define the shape of the input array:
57+
* var shape = [ 2, 2 ];
58+
*
59+
* // Define the array strides:
60+
* var sx = [ 4, 1 ];
61+
*
62+
* // Define the index offset:
63+
* var ox = 0;
64+
*
65+
* // Create the input ndarray-like object:
66+
* var x = {
67+
* 'dtype': 'generic',
68+
* 'data': xbuf,
69+
* 'shape': shape,
70+
* 'strides': sx,
71+
* 'offset': ox,
72+
* 'order': 'row-major',
73+
* 'accessors': accessors( xbuf ).accessors
74+
* };
75+
*
76+
* // Perform reduction:
77+
* var out = includesnd( x, 6.0 );
78+
* // returns true
79+
*
80+
* out = includesnd( x, 100.0 );
81+
* // returns false
82+
*/
83+
function includesnd( x, value ) {
84+
var xbuf;
85+
var ordx;
86+
var len;
87+
var get;
88+
var sh;
89+
var sx;
90+
var ox;
91+
var ix;
92+
var i;
93+
94+
sh = x.shape;
95+
96+
// Compute the total number of elements over which to iterate:
97+
len = numel( sh );
98+
99+
// Cache a reference to the output ndarray data buffer:
100+
xbuf = x.data;
101+
102+
// Cache a reference to the stride array:
103+
sx = x.strides;
104+
105+
// Cache the index of the first indexed element:
106+
ox = x.offset;
107+
108+
// Cache the array order:
109+
ordx = x.order;
110+
111+
// Cache accessor:
112+
get = x.accessors[ 0 ];
113+
114+
// Iterate over each element based on the linear **view** index, regardless as to how the data is stored in memory...
115+
for ( i = 0; i < len; i++ ) {
116+
ix = vind2bind( sh, sx, ox, ordx, i, MODE );
117+
if ( get( xbuf, ix ) === value ) {
118+
return true;
119+
}
120+
}
121+
return false;
122+
}
123+
124+
125+
// EXPORTS //
126+
127+
module.exports = includesnd;
Lines changed: 128 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,128 @@
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+
* Tests whether a reinterpreted complex number ndarray contains a specified value.
36+
*
37+
* @private
38+
* @param {Object} x - object containing 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 {ComplexLike} value - search element
46+
* @returns {boolean} result
47+
*
48+
* @example
49+
* var Float64Array = require( '@stdlib/array/float64' );
50+
*
51+
* // Create a data buffer:
52+
* var xbuf = new Float64Array( [ 1.0, 2.0, 3.0, 4.0, 5.0, 6.0, 7.0, 8.0 ] );
53+
*
54+
* // Define the shape of the input array:
55+
* var shape = [ 2, 2 ];
56+
*
57+
* // Define the array strides:
58+
* var sx = [ 4, 2 ];
59+
*
60+
* // Define the index offset:
61+
* var ox = 0;
62+
*
63+
* // Create the input ndarray-like object:
64+
* var x = {
65+
* 'dtype': 'complex128',
66+
* 'data': xbuf,
67+
* 'shape': shape,
68+
* 'strides': sx,
69+
* 'offset': ox,
70+
* 'order': 'row-major'
71+
* };
72+
*
73+
* // Perform reduction:
74+
* var v = {
75+
* 're': 3.0,
76+
* 'im': 4.0
77+
* };
78+
* var out = includesnd( x, v );
79+
* // returns true
80+
*
81+
* v = {
82+
* 're': -3.0,
83+
* 'im': -4.0
84+
* };
85+
* out = includesnd( x, v );
86+
* // returns false
87+
*/
88+
function includesnd( x, value ) {
89+
var xbuf;
90+
var ordx;
91+
var len;
92+
var sh;
93+
var sx;
94+
var ox;
95+
var ix;
96+
var i;
97+
98+
sh = x.shape;
99+
100+
// Compute the total number of elements over which to iterate:
101+
len = numel( sh );
102+
103+
// Cache a reference to the output ndarray data buffer:
104+
xbuf = x.data;
105+
106+
// Cache a reference to the stride array:
107+
sx = x.strides;
108+
109+
// Cache the index of the first indexed element:
110+
ox = x.offset;
111+
112+
// Cache the array order:
113+
ordx = x.order;
114+
115+
// Iterate over each element based on the linear **view** index, regardless as to how the data is stored in memory...
116+
for ( i = 0; i < len; i++ ) {
117+
ix = vind2bind( sh, sx, ox, ordx, i, MODE );
118+
if ( xbuf[ ix ] === value.re && xbuf[ ix+1 ] === value.im ) {
119+
return true;
120+
}
121+
}
122+
return false;
123+
}
124+
125+
126+
// EXPORTS //
127+
128+
module.exports = includesnd;

0 commit comments

Comments
 (0)