Skip to content

Commit 68f59cc

Browse files
committed
feat: add ndarray/base/some
--- 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: passed - task: lint_package_json status: passed - task: lint_repl_help status: passed - task: lint_javascript_src status: passed - task: lint_javascript_cli status: na - task: lint_javascript_examples status: passed - task: lint_javascript_tests status: passed - task: lint_javascript_benchmarks status: passed - 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: passed - task: lint_typescript_tests status: passed - task: lint_license_headers status: passed ---
1 parent 3192ef5 commit 68f59cc

Some content is hidden

Large Commits have some content hidden by default. Use the searchbox below for content that may be hidden.

47 files changed

+8393
-0
lines changed
Lines changed: 156 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,156 @@
1+
<!--
2+
3+
@license Apache-2.0
4+
5+
Copyright (c) 2025 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+
# some
22+
23+
> Test whether at least `n` elements in an ndarray are truthy.
24+
25+
<section class="intro">
26+
27+
</section>
28+
29+
<!-- /.intro -->
30+
31+
<section class="usage">
32+
33+
## Usage
34+
35+
```javascript
36+
var some = require( '@stdlib/ndarray/base/some' );
37+
```
38+
39+
#### some( arrays )
40+
41+
Tests whether at least `n` elements in an ndarray are truthy.
42+
43+
<!-- eslint-disable max-len -->
44+
45+
```javascript
46+
var Float64Array = require( '@stdlib/array/float64' );
47+
var scalar2ndarray = require( '@stdlib/ndarray/from-scalar' );
48+
49+
// Create a data buffer:
50+
var xbuf = new Float64Array( [ 1.0, 2.0, 3.0, 4.0, 5.0, 0.0, 7.0, 8.0, 9.0, 10.0, 11.0, 12.0 ] );
51+
52+
// Define the shape of the input array:
53+
var shape = [ 3, 1, 2 ];
54+
55+
// Define the array strides:
56+
var sx = [ 4, 4, 1 ];
57+
58+
// Define the index offset:
59+
var ox = 0;
60+
61+
// Create the input ndarray-like object:
62+
var x = {
63+
'dtype': 'float64',
64+
'data': xbuf,
65+
'shape': shape,
66+
'strides': sx,
67+
'offset': ox,
68+
'order': 'row-major'
69+
};
70+
71+
// Define the success criterion:
72+
var n = scalar2ndarray( 3, {
73+
'dtype': 'generic'
74+
});
75+
76+
// Test elements:
77+
var out = some( [ x, n ] );
78+
// returns true
79+
```
80+
81+
The function accepts the following arguments:
82+
83+
- **arrays**: array-like object containing an input ndarray and a zero-dimensional ndarray specifying the minimum number of elements in the input ndarray that must be truthy.
84+
85+
Each provided ndarray should be an `object` with the following properties:
86+
87+
- **dtype**: data type.
88+
- **data**: data buffer.
89+
- **shape**: dimensions.
90+
- **strides**: stride lengths.
91+
- **offset**: index offset.
92+
- **order**: specifies whether an ndarray is row-major (C-style) or column major (Fortran-style).
93+
94+
</section>
95+
96+
<!-- /.usage -->
97+
98+
<section class="notes">
99+
100+
## Notes
101+
102+
- For very high-dimensional ndarrays which are non-contiguous, one should consider copying the underlying data to contiguous memory before performing the operation in order to achieve better performance.
103+
104+
</section>
105+
106+
<!-- /.notes -->
107+
108+
<section class="examples">
109+
110+
## Examples
111+
112+
<!-- eslint no-undef: "error" -->
113+
114+
```javascript
115+
var discreteUniform = require( '@stdlib/random/array/discrete-uniform' );
116+
var scalar2ndarray = require( '@stdlib/ndarray/from-scalar' );
117+
var ndarray2array = require( '@stdlib/ndarray/base/to-array' );
118+
var some = require( '@stdlib/ndarray/base/some' );
119+
120+
var x = {
121+
'dtype': 'generic',
122+
'data': discreteUniform( 10, -2, 10, {
123+
'dtype': 'generic'
124+
}),
125+
'shape': [ 5, 2 ],
126+
'strides': [ 2, 1 ],
127+
'offset': 0,
128+
'order': 'row-major'
129+
};
130+
console.log( ndarray2array( x.data, x.shape, x.strides, x.offset, x.order ) );
131+
132+
var n = scalar2ndarray( 5, {
133+
'dtype': 'generic'
134+
});
135+
136+
var out = some( [ x, n ] );
137+
console.log( out );
138+
```
139+
140+
</section>
141+
142+
<!-- /.examples -->
143+
144+
<!-- Section for related `stdlib` packages. Do not manually edit this section, as it is automatically populated. -->
145+
146+
<section class="related">
147+
148+
</section>
149+
150+
<!-- /.related -->
151+
152+
<section class="links">
153+
154+
</section>
155+
156+
<!-- /.links -->
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 bench = require( '@stdlib/bench' );
24+
var isBoolean = require( '@stdlib/assert/is-boolean' ).isPrimitive;
25+
var pow = require( '@stdlib/math/base/special/pow' );
26+
var scalar2ndarray = require( '@stdlib/ndarray/from-scalar' );
27+
var discreteUniform = require( '@stdlib/random/array/discrete-uniform' );
28+
var shape2strides = require( '@stdlib/ndarray/base/shape2strides' );
29+
var pkg = require( './../package.json' ).name;
30+
var some = require( './../lib' );
31+
32+
33+
// VARIABLES //
34+
35+
var types = [ 'float64' ];
36+
var order = 'column-major';
37+
38+
39+
// FUNCTIONS //
40+
41+
/**
42+
* Creates a benchmark function.
43+
*
44+
* @private
45+
* @param {PositiveInteger} len - ndarray length
46+
* @param {NonNegativeIntegerArray} shape - ndarray shape
47+
* @param {string} xtype - ndarray data type
48+
* @returns {Function} benchmark function
49+
*/
50+
function createBenchmark( len, shape, xtype ) {
51+
var x;
52+
var n;
53+
54+
x = discreteUniform( len, 1, 100 );
55+
x = {
56+
'dtype': xtype,
57+
'data': x,
58+
'shape': shape,
59+
'strides': shape2strides( shape, order ),
60+
'offset': 0,
61+
'order': order
62+
};
63+
n = scalar2ndarray( len, {
64+
'dtype': 'generic',
65+
'order': order
66+
});
67+
return benchmark;
68+
69+
/**
70+
* Benchmark function.
71+
*
72+
* @private
73+
* @param {Benchmark} b - benchmark instance
74+
*/
75+
function benchmark( b ) {
76+
var out;
77+
var i;
78+
79+
b.tic();
80+
for ( i = 0; i < b.iterations; i++ ) {
81+
out = some( [ x, n ] );
82+
if ( typeof out !== 'boolean' ) {
83+
b.fail( 'should return a boolean' );
84+
}
85+
}
86+
b.toc();
87+
if ( !isBoolean( out ) ) {
88+
b.fail( 'should return a boolean' );
89+
}
90+
b.pass( 'benchmark finished' );
91+
b.end();
92+
}
93+
}
94+
95+
96+
// MAIN //
97+
98+
/**
99+
* Main execution sequence.
100+
*
101+
* @private
102+
*/
103+
function main() {
104+
var len;
105+
var min;
106+
var max;
107+
var sh;
108+
var t1;
109+
var f;
110+
var i;
111+
var j;
112+
113+
min = 1; // 10^min
114+
max = 6; // 10^max
115+
116+
for ( j = 0; j < types.length; j++ ) {
117+
t1 = types[ j ];
118+
for ( i = min; i <= max; i++ ) {
119+
len = pow( 10, i );
120+
121+
sh = [ len ];
122+
f = createBenchmark( len, sh, t1 );
123+
bench( pkg+':ndims='+sh.length+',len='+len+',shape=['+sh.join(',')+'],xorder='+order+',xtype='+t1, f );
124+
}
125+
}
126+
}
127+
128+
main();

0 commit comments

Comments
 (0)