Skip to content

Commit ff98e57

Browse files
committed
feat: add blas/ext/base/ndarray/sfill-not-equal
--- 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_pkg_readmes status: passed - task: lint_markdown_docs status: na - task: lint_markdown status: na - 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 ae1746f commit ff98e57

10 files changed

Lines changed: 965 additions & 0 deletions

File tree

Lines changed: 133 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,133 @@
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+
# sfillNotEqual
22+
23+
> Replace elements in a one-dimensional ndarray not equal to a provided search element with a specified scalar constant.
24+
25+
<section class="intro">
26+
27+
</section>
28+
29+
<!-- /.intro -->
30+
31+
<section class="usage">
32+
33+
## Usage
34+
35+
```javascript
36+
var sfillNotEqual = require( '@stdlib/blas/ext/base/ndarray/sfill-not-equal' );
37+
```
38+
39+
#### sfillNotEqual( arrays )
40+
41+
Replaces elements in a one-dimensional ndarray not equal to a provided search element with a specified scalar constant.
42+
43+
```javascript
44+
var Float32Array = require( '@stdlib/array/float32' );
45+
var vector = require( '@stdlib/ndarray/vector/ctor' );
46+
var scalar2ndarray = require( '@stdlib/ndarray/from-scalar' );
47+
48+
var x = vector( new Float32Array( [ 0.0, -2.0, 3.0, 0.0, 4.0, -6.0 ] ), 'float32' );
49+
50+
var searchElement = scalar2ndarray( 0.0, {
51+
'dtype': 'float32'
52+
});
53+
54+
var alpha = scalar2ndarray( 5.0, {
55+
'dtype': 'float32'
56+
});
57+
58+
sfillNotEqual( [ x, searchElement, alpha ] );
59+
// x => <ndarray>[ 0.0, 5.0, 5.0, 0.0, 5.0, 5.0 ]
60+
```
61+
62+
The function has the following parameters:
63+
64+
- **arrays**: array-like object containing the following ndarrays:
65+
66+
- a one-dimensional input ndarray.
67+
- a zero-dimensional ndarray containing the search element.
68+
- a zero-dimensional ndarray containing the scalar constant.
69+
70+
</section>
71+
72+
<!-- /.usage -->
73+
74+
<section class="notes">
75+
76+
## Notes
77+
78+
- The input ndarray is modified **in-place** (i.e., the input ndarray is **mutated**).
79+
- When comparing elements, the function checks for equality using the strict equality operator `===`. As a consequence, `NaN` values are considered distinct (i.e., as `NaN !== NaN` always evaluates to `true`, `NaN` elements are always replaced), and `-0` and `+0` are considered the same.
80+
81+
</section>
82+
83+
<!-- /.notes -->
84+
85+
<section class="examples">
86+
87+
## Examples
88+
89+
<!-- eslint no-undef: "error" -->
90+
91+
```javascript
92+
var discreteUniform = require( '@stdlib/random/discrete-uniform' );
93+
var scalar2ndarray = require( '@stdlib/ndarray/from-scalar' );
94+
var ndarray2array = require( '@stdlib/ndarray/to-array' );
95+
var ndarraylike2scalar = require( '@stdlib/ndarray/ndarraylike2scalar' );
96+
var sfillNotEqual = require( '@stdlib/blas/ext/base/ndarray/sfill-not-equal' );
97+
98+
var opts = {
99+
'dtype': 'float32'
100+
};
101+
102+
var x = discreteUniform( [ 10 ], 0, 3, opts );
103+
console.log( ndarray2array( x ) );
104+
105+
var searchElement = scalar2ndarray( 1, opts );
106+
console.log( 'Search Element: %d', ndarraylike2scalar( searchElement ) );
107+
108+
var alpha = scalar2ndarray( 5, opts );
109+
console.log( 'Alpha: %d', ndarraylike2scalar( alpha ) );
110+
111+
sfillNotEqual( [ x, searchElement, alpha ] );
112+
console.log( ndarray2array( x ) );
113+
```
114+
115+
</section>
116+
117+
<!-- /.examples -->
118+
119+
<!-- Section for related `stdlib` packages. Do not manually edit this section, as it is automatically populated. -->
120+
121+
<section class="related">
122+
123+
</section>
124+
125+
<!-- /.related -->
126+
127+
<!-- Section for all links. Make sure to keep an empty line after the `section` element and another before the `/section` close. -->
128+
129+
<section class="links">
130+
131+
</section>
132+
133+
<!-- /.links -->
Lines changed: 109 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,109 @@
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 uniform = require( '@stdlib/random/uniform' );
25+
var scalar2ndarray = require( '@stdlib/ndarray/from-scalar' );
26+
var pow = require( '@stdlib/math/base/special/pow' );
27+
var format = require( '@stdlib/string/format' );
28+
var pkg = require( './../package.json' ).name;
29+
var sfillNotEqual = require( './../lib' );
30+
31+
32+
// VARIABLES //
33+
34+
var options = {
35+
'dtype': 'float32'
36+
};
37+
38+
39+
// FUNCTIONS //
40+
41+
/**
42+
* Creates a benchmark function.
43+
*
44+
* @private
45+
* @param {PositiveInteger} len - ndarray length
46+
* @returns {Function} benchmark function
47+
*/
48+
function createBenchmark( len ) {
49+
var searchElement;
50+
var alpha;
51+
var x;
52+
53+
x = uniform( [ len ], -100.0, 100.0, options );
54+
searchElement = scalar2ndarray( 0.0, options );
55+
alpha = scalar2ndarray( 5.0, options );
56+
return benchmark;
57+
58+
/**
59+
* Benchmark function.
60+
*
61+
* @private
62+
* @param {Benchmark} b - benchmark instance
63+
*/
64+
function benchmark( b ) {
65+
var out;
66+
var i;
67+
68+
b.tic();
69+
for ( i = 0; i < b.iterations; i++ ) {
70+
out = sfillNotEqual( [ x, searchElement, alpha ] );
71+
if ( typeof out !== 'object' ) {
72+
b.fail( 'should return an ndarray' );
73+
}
74+
}
75+
b.toc();
76+
if ( typeof out !== 'object' ) {
77+
b.fail( 'should return an ndarray' );
78+
}
79+
b.pass( 'benchmark finished' );
80+
b.end();
81+
}
82+
}
83+
84+
85+
// MAIN //
86+
87+
/**
88+
* Main execution sequence.
89+
*
90+
* @private
91+
*/
92+
function main() {
93+
var len;
94+
var min;
95+
var max;
96+
var f;
97+
var i;
98+
99+
min = 1; // 10^min
100+
max = 6; // 10^max
101+
102+
for ( i = min; i <= max; i++ ) {
103+
len = pow( 10, i );
104+
f = createBenchmark( len );
105+
bench( format( '%s:len=%d', pkg, len ), f );
106+
}
107+
}
108+
109+
main();
Lines changed: 40 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,40 @@
1+
2+
{{alias}}( arrays )
3+
Replaces elements in a one-dimensional ndarray not equal to a provided
4+
search element with a specified scalar constant.
5+
6+
The input ndarray is modified *in-place* (i.e., the input ndarray is
7+
*mutated*).
8+
9+
When comparing elements, the function checks for equality using the strict
10+
equality operator `===`. As a consequence, `NaN` values are considered
11+
distinct (i.e., as `NaN !== NaN` always evaluates to `true`, `NaN` elements
12+
are always replaced), and `-0` and `+0` are considered the same.
13+
14+
Parameters
15+
----------
16+
arrays: ArrayLikeObject<ndarray>
17+
Array-like object containing the following ndarrays:
18+
19+
- a one-dimensional input ndarray.
20+
- a zero-dimensional ndarray containing the search element.
21+
- a zero-dimensional ndarray containing the scalar constant.
22+
23+
Returns
24+
-------
25+
out: ndarray
26+
Input ndarray.
27+
28+
Examples
29+
--------
30+
> var buf = new ({{alias:@stdlib/array/float32}})( [ 0.0, -2.0, 3.0, 0.0, 4.0, -6.0 ] );
31+
> var x = {{alias:@stdlib/ndarray/vector/ctor}}( buf, 'float32' );
32+
> var opts = { 'dtype': 'float32' };
33+
> var v = {{alias:@stdlib/ndarray/from-scalar}}( 0.0, opts );
34+
> var alpha = {{alias:@stdlib/ndarray/from-scalar}}( 5.0, opts );
35+
> {{alias}}( [ x, v, alpha ] )
36+
<ndarray>[ 0.0, 5.0, 5.0, 0.0, 5.0, 5.0 ]
37+
38+
See Also
39+
--------
40+
Lines changed: 64 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,64 @@
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+
// TypeScript Version: 4.1
20+
21+
/// <reference types="@stdlib/types"/>
22+
23+
import { float32ndarray, typedndarray } from '@stdlib/types/ndarray';
24+
25+
/**
26+
* Replaces elements in a one-dimensional ndarray not equal to a provided search element with a specified scalar constant.
27+
*
28+
* ## Notes
29+
*
30+
* - The function expects the following ndarrays:
31+
*
32+
* - a one-dimensional input ndarray.
33+
* - a zero-dimensional ndarray containing the search element.
34+
* - a zero-dimensional ndarray containing the scalar constant.
35+
*
36+
* - When comparing elements, the function checks for equality using the strict equality operator `===`. As a consequence, `NaN` values are considered distinct (i.e., as `NaN !== NaN` always evaluates to `true`, `NaN` elements are always replaced), and `-0` and `+0` are considered the same.
37+
*
38+
* @param arrays - array-like object containing ndarrays
39+
* @returns input ndarray
40+
*
41+
* @example
42+
* var Float32Array = require( '@stdlib/array/float32' );
43+
* var vector = require( '@stdlib/ndarray/vector/ctor' );
44+
* var scalar2ndarray = require( '@stdlib/ndarray/from-scalar' );
45+
*
46+
* var x = vector( new Float32Array( [ 0.0, -2.0, 3.0, 0.0, 4.0, -6.0 ] ), 'float32' );
47+
*
48+
* var searchElement = scalar2ndarray( 0.0, {
49+
* 'dtype': 'float32'
50+
* });
51+
*
52+
* var alpha = scalar2ndarray( 5.0, {
53+
* 'dtype': 'float32'
54+
* });
55+
*
56+
* var out = sfillNotEqual( [ x, searchElement, alpha ] );
57+
* // returns <ndarray>[ 0.0, 5.0, 5.0, 0.0, 5.0, 5.0 ]
58+
*/
59+
declare function sfillNotEqual( arrays: [ float32ndarray, typedndarray<number>, typedndarray<number> ] ): float32ndarray;
60+
61+
62+
// EXPORTS //
63+
64+
export = sfillNotEqual;

0 commit comments

Comments
 (0)