Skip to content

Commit 148d9d1

Browse files
committed
feat: add 0d implementation
1 parent c65133f commit 148d9d1

2 files changed

Lines changed: 258 additions & 0 deletions

File tree

Lines changed: 129 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,129 @@
1+
/**
2+
* @license Apache-2.0
3+
*
4+
* Copyright (c) 2026 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+
// MAIN //
22+
23+
/**
24+
* Applies a condition to two zero-dimensional input ndarrays according to a zero-dimensional boolean ndarray and assigns the result to elements in a zero-dimensional output ndarray.
25+
*
26+
* @private
27+
* @param {Object} condition - object containing boolean condition ndarray meta data
28+
* @param {*} condition.dtype - data type
29+
* @param {Collection} condition.data - data buffer
30+
* @param {NonNegativeIntegerArray} condition.shape - dimensions
31+
* @param {IntegerArray} condition.strides - stride lengths
32+
* @param {NonNegativeInteger} condition.offset - index offset
33+
* @param {string} condition.order - specifies whether `condition` is row-major (C-style) or column-major (Fortran-style)
34+
* @param {Object} x - object containing first input ndarray meta data
35+
* @param {*} 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 {Object} y - object containing second input ndarray meta data
42+
* @param {*} y.dtype - data type
43+
* @param {Collection} y.data - data buffer
44+
* @param {NonNegativeIntegerArray} y.shape - dimensions
45+
* @param {IntegerArray} y.strides - stride lengths
46+
* @param {NonNegativeInteger} y.offset - index offset
47+
* @param {string} y.order - specifies whether `y` is row-major (C-style) or column-major (Fortran-style)
48+
* @param {Object} out - object containing output ndarray meta data
49+
* @param {*} out.dtype - data type
50+
* @param {Collection} out.data - data buffer
51+
* @param {NonNegativeIntegerArray} out.shape - dimensions
52+
* @param {IntegerArray} out.strides - stride lengths
53+
* @param {NonNegativeInteger} out.offset - index offset
54+
* @param {string} out.order - specifies whether `out` is row-major (C-style) or column-major (Fortran-style)
55+
* @returns {void}
56+
*
57+
* @example
58+
* var Float64Array = require( '@stdlib/array/float64' );
59+
* var Uint8Array = require( '@stdlib/array/uint8' );
60+
*
61+
* // Create data buffers:
62+
* var cbuf = new Uint8Array( [ 1 ] );
63+
* var xbuf = new Float64Array( [ 1.0 ] );
64+
* var ybuf = new Float64Array( [ 2.0 ] );
65+
* var obuf = new Float64Array( 1 );
66+
*
67+
* // Define the shape of the arrays (0-dimensional = scalar):
68+
* var shape = [];
69+
*
70+
* // Define the array strides:
71+
* var sc = [ 0 ];
72+
* var sx = [ 0 ];
73+
* var sy = [ 0 ];
74+
* var so = [ 0 ];
75+
*
76+
* // Define the index offsets:
77+
* var oc = 0;
78+
* var ox = 0;
79+
* var oy = 0;
80+
* var oo = 0;
81+
*
82+
* // Create the ndarray-like objects:
83+
* var condition = {
84+
* 'dtype': 'uint8',
85+
* 'data': cbuf,
86+
* 'shape': shape,
87+
* 'strides': sc,
88+
* 'offset': oc,
89+
* 'order': 'row-major'
90+
* };
91+
* var x = {
92+
* 'dtype': 'float64',
93+
* 'data': xbuf,
94+
* 'shape': shape,
95+
* 'strides': sx,
96+
* 'offset': ox,
97+
* 'order': 'row-major'
98+
* };
99+
* var y = {
100+
* 'dtype': 'float64',
101+
* 'data': ybuf,
102+
* 'shape': shape,
103+
* 'strides': sy,
104+
* 'offset': oy,
105+
* 'order': 'row-major'
106+
* };
107+
* var out = {
108+
* 'dtype': 'float64',
109+
* 'data': obuf,
110+
* 'shape': shape,
111+
* 'strides': so,
112+
* 'offset': oo,
113+
* 'order': 'row-major'
114+
* };
115+
*
116+
* // Apply the condition:
117+
* where0d( condition, x, y, out );
118+
*
119+
* console.log( out.data );
120+
* // => <Float64Array>[ 1.0 ]
121+
*/
122+
function where0d( condition, x, y, out ) {
123+
out.data[ out.offset ] = ( condition.data[ condition.offset ] ) ? x.data[ x.offset ] : y.data[ y.offset ];
124+
}
125+
126+
127+
// EXPORTS //
128+
129+
module.exports = where0d;
Lines changed: 129 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,129 @@
1+
/**
2+
* @license Apache-2.0
3+
*
4+
* Copyright (c) 2026 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+
// MAIN //
22+
23+
/**
24+
* Applies a condition to two zero-dimensional input ndarrays according to a zero-dimensional boolean ndarray and assigns the result to elements in a zero-dimensional output ndarray.
25+
*
26+
* @private
27+
* @param {Object} condition - object containing boolean condition ndarray meta data
28+
* @param {*} condition.dtype - data type
29+
* @param {Collection} condition.data - data buffer
30+
* @param {NonNegativeIntegerArray} condition.shape - dimensions
31+
* @param {IntegerArray} condition.strides - stride lengths
32+
* @param {NonNegativeInteger} condition.offset - index offset
33+
* @param {string} condition.order - specifies whether `condition` is row-major (C-style) or column-major (Fortran-style)
34+
* @param {Object} x - object containing first input ndarray meta data
35+
* @param {*} 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 {Object} y - object containing second input ndarray meta data
42+
* @param {*} y.dtype - data type
43+
* @param {Collection} y.data - data buffer
44+
* @param {NonNegativeIntegerArray} y.shape - dimensions
45+
* @param {IntegerArray} y.strides - stride lengths
46+
* @param {NonNegativeInteger} y.offset - index offset
47+
* @param {string} y.order - specifies whether `y` is row-major (C-style) or column-major (Fortran-style)
48+
* @param {Object} out - object containing output ndarray meta data
49+
* @param {*} out.dtype - data type
50+
* @param {Collection} out.data - data buffer
51+
* @param {NonNegativeIntegerArray} out.shape - dimensions
52+
* @param {IntegerArray} out.strides - stride lengths
53+
* @param {NonNegativeInteger} out.offset - index offset
54+
* @param {string} out.order - specifies whether `out` is row-major (C-style) or column-major (Fortran-style)
55+
* @returns {void}
56+
*
57+
* @example
58+
* var Float64Array = require( '@stdlib/array/float64' );
59+
* var Uint8Array = require( '@stdlib/array/uint8' );
60+
*
61+
* // Create data buffers:
62+
* var cbuf = new Uint8Array( [ 1 ] );
63+
* var xbuf = new Float64Array( [ 1.0 ] );
64+
* var ybuf = new Float64Array( [ 2.0 ] );
65+
* var obuf = new Float64Array( 1 );
66+
*
67+
* // Define the shape of the arrays (0-dimensional = scalar):
68+
* var shape = [];
69+
*
70+
* // Define the array strides:
71+
* var sc = [ 0 ];
72+
* var sx = [ 0 ];
73+
* var sy = [ 0 ];
74+
* var so = [ 0 ];
75+
*
76+
* // Define the index offsets:
77+
* var oc = 0;
78+
* var ox = 0;
79+
* var oy = 0;
80+
* var oo = 0;
81+
*
82+
* // Create the ndarray-like objects:
83+
* var condition = {
84+
* 'dtype': 'uint8',
85+
* 'data': cbuf,
86+
* 'shape': shape,
87+
* 'strides': sc,
88+
* 'offset': oc,
89+
* 'order': 'row-major'
90+
* };
91+
* var x = {
92+
* 'dtype': 'float64',
93+
* 'data': xbuf,
94+
* 'shape': shape,
95+
* 'strides': sx,
96+
* 'offset': ox,
97+
* 'order': 'row-major'
98+
* };
99+
* var y = {
100+
* 'dtype': 'float64',
101+
* 'data': ybuf,
102+
* 'shape': shape,
103+
* 'strides': sy,
104+
* 'offset': oy,
105+
* 'order': 'row-major'
106+
* };
107+
* var out = {
108+
* 'dtype': 'float64',
109+
* 'data': obuf,
110+
* 'shape': shape,
111+
* 'strides': so,
112+
* 'offset': oo,
113+
* 'order': 'row-major'
114+
* };
115+
*
116+
* // Apply the condition:
117+
* where0d( condition, x, y, out );
118+
*
119+
* console.log( out.data );
120+
* // => <Float64Array>[ 1.0 ]
121+
*/
122+
function where0d( condition, x, y, out ) {
123+
out.accessors[ 1 ]( out.data, out.offset, ( condition.accessors[ 0 ]( condition.data, condition.offset ) ) ? x.accessors[ 0 ]( x.data, x.offset ) : y.accessors[ 0 ]( y.data, y.offset ) );
124+
}
125+
126+
127+
// EXPORTS //
128+
129+
module.exports = where0d;

0 commit comments

Comments
 (0)