Skip to content

Commit c65133f

Browse files
committed
feat: create 2d version with its tests and benchmarks
1 parent eab49ad commit c65133f

6 files changed

Lines changed: 972 additions & 0 deletions

File tree

Lines changed: 61 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,61 @@
1+
<!--
2+
3+
@license Apache-2.0
4+
5+
Copyright (c) 2026 The Stdlib Authors.
6+
7+
Licensed under the Apache License, Version 2.0 (the "License");
8+
you may not use this file except in compliance with the License.
9+
You may obtain a copy of the License at
10+
11+
http://www.apache.org/licenses/LICENSE-2.0
12+
13+
Unless required by applicable law or agreed to in writing, software
14+
distributed under the License is distributed on an "AS IS" BASIS,
15+
WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
16+
See the License for the specific language governing permissions and
17+
limitations under the License.
18+
19+
-->
20+
21+
# Where
22+
23+
> Apply a condition to elements in two input ndarrays and assign results to elements in an output ndarray.
24+
25+
<section class="intro">
26+
27+
</section>
28+
29+
<!-- /.intro -->
30+
31+
<section class="usage">
32+
33+
34+
</section>
35+
36+
<!-- /.usage -->
37+
38+
<section class="examples">
39+
40+
</section>
41+
42+
<!-- /.examples -->
43+
44+
<!-- Section for related `stdlib` packages. -->
45+
46+
* * *
47+
48+
<section class="related">
49+
50+
51+
</section>
52+
53+
<!-- /.related -->
54+
55+
<!-- Section for all links. Make sure to keep an empty line after the `section` element and another before the `/section` close. -->
56+
57+
<section class="links">
58+
59+
</section>
60+
61+
<!-- /.links -->
Lines changed: 169 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,169 @@
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+
// MODULES //
22+
23+
var bench = require( '@stdlib/bench' );
24+
var discreteUniform = require( '@stdlib/random/base/discrete-uniform' ).factory;
25+
var bernoulli = require( '@stdlib/random/base/bernoulli' ).factory;
26+
var isnan = require( '@stdlib/math/base/assert/is-nan' );
27+
var pow = require( '@stdlib/math/base/special/pow' );
28+
var floor = require( '@stdlib/math/base/special/floor' );
29+
var sqrt = require( '@stdlib/math/base/special/sqrt' );
30+
var filledarray = require( '@stdlib/array/filled' );
31+
var filledarrayBy = require( '@stdlib/array/filled-by' );
32+
var shape2strides = require( '@stdlib/ndarray/base/shape2strides' );
33+
var pkg = require( './../package.json' ).name;
34+
var where2d = require( './../lib/2d.js' );
35+
36+
37+
// VARIABLES //
38+
39+
var types = [ 'float64' ];
40+
var order = 'column-major';
41+
42+
43+
// FUNCTIONS //
44+
45+
/**
46+
* Creates a benchmark function.
47+
*
48+
* @private
49+
* @param {PositiveInteger} len - ndarray length
50+
* @param {NonNegativeIntegerArray} shape - ndarray shape
51+
* @param {string} xtype - first input ndarray data type
52+
* @param {string} ytype - second input ndarray data type
53+
* @returns {Function} benchmark function
54+
*/
55+
function createBenchmark( len, shape, xtype, ytype ) {
56+
var condition;
57+
var out;
58+
var x;
59+
var y;
60+
condition = filledarrayBy( len, 'uint8', bernoulli( 0.5 ) );
61+
x = filledarrayBy( len, xtype, discreteUniform( -100, 100 ) );
62+
y = filledarrayBy( len, ytype, discreteUniform( -100, 100 ) );
63+
out = filledarray( 0.0, len, xtype );
64+
65+
condition = {
66+
'dtype': 'uint8',
67+
'data': condition,
68+
'shape': shape,
69+
'strides': shape2strides( shape, order ),
70+
'offset': 0,
71+
'order': order
72+
};
73+
x = {
74+
'dtype': xtype,
75+
'data': x,
76+
'shape': shape,
77+
'strides': shape2strides( shape, order ),
78+
'offset': 0,
79+
'order': order
80+
};
81+
y = {
82+
'dtype': ytype,
83+
'data': y,
84+
'shape': shape,
85+
'strides': shape2strides( shape, order ),
86+
'offset': 0,
87+
'order': order
88+
};
89+
out = {
90+
'dtype': xtype,
91+
'data': out,
92+
'shape': shape,
93+
'strides': shape2strides( shape, order ),
94+
'offset': 0,
95+
'order': order
96+
};
97+
return benchmark;
98+
99+
/**
100+
* Benchmark function.
101+
*
102+
* @private
103+
* @param {Benchmark} b - benchmark instance
104+
*/
105+
function benchmark( b ) {
106+
var i;
107+
108+
b.tic();
109+
for ( i = 0; i < b.iterations; i++ ) {
110+
where2d( condition, x, y, out, false );
111+
if ( isnan( out.data[ i%len ] ) ) {
112+
b.fail( 'should not return NaN' );
113+
}
114+
}
115+
b.toc();
116+
if ( isnan( out.data[ i%len ] ) ) {
117+
b.fail( 'should not return NaN' );
118+
}
119+
b.pass( 'benchmark finished' );
120+
b.end();
121+
}
122+
}
123+
124+
125+
// MAIN //
126+
127+
/**
128+
* Main execution sequence.
129+
*
130+
* @private
131+
*/
132+
function main() {
133+
var len;
134+
var min;
135+
var max;
136+
var sh;
137+
var t1;
138+
var t2;
139+
var f;
140+
var i;
141+
var j;
142+
143+
min = 1; // 10^min
144+
max = 6; // 10^max
145+
146+
for ( j = 0; j < types.length; j++ ) {
147+
t1 = types[ j ];
148+
t2 = types[ j ];
149+
for ( i = min; i <= max; i++ ) {
150+
len = pow( 10, i );
151+
152+
sh = [ len/2, 2 ];
153+
f = createBenchmark( len, sh, t1, t2 );
154+
bench( pkg+':ndims='+sh.length+',len='+len+',shape=['+sh.join(',')+'],order='+order+',xtype='+t1+',ytype='+t2, f );
155+
156+
sh = [ 2, len/2 ];
157+
f = createBenchmark( len, sh, t1, t2 );
158+
bench( pkg+':ndims='+sh.length+',len='+len+',shape=['+sh.join(',')+'],order='+order+',xtype='+t1+',ytype='+t2, f );
159+
160+
len = floor( sqrt( len ) );
161+
sh = [ len, len ];
162+
len *= len;
163+
f = createBenchmark( len, sh, t1, t2 );
164+
bench( pkg+':ndims='+sh.length+',len='+len+',shape=['+sh.join(',')+'],order='+order+',xtype='+t1+',ytype='+t2, f );
165+
}
166+
}
167+
}
168+
169+
main();

0 commit comments

Comments
 (0)