Skip to content

Commit 683f593

Browse files
committed
feat: add math/array/tools/unary
--- 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 08c640b commit 683f593

File tree

13 files changed

+2243
-0
lines changed

13 files changed

+2243
-0
lines changed
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,195 @@
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+
# Unary
22+
23+
> Constructor for applying a unary function to each element in an input array.
24+
25+
<section class="usage">
26+
27+
## Usage
28+
29+
```javascript
30+
var Unary = require( '@stdlib/math/array/tools/unary' );
31+
```
32+
33+
#### Unary( fcn, idtypes, odtypes, policy )
34+
35+
Constructor for applying a unary function to each element in an input array.
36+
37+
```javascript
38+
var abs = require( '@stdlib/math/base/special/abs' );
39+
40+
var dtypes = [ 'float64', 'float32', 'generic' ];
41+
var policy = 'same';
42+
43+
var unary = new Unary( abs, dtypes, dtypes, policy );
44+
```
45+
46+
The constructor has the following parameters:
47+
48+
- **fcn**: unary function to apply.
49+
- **idtypes**: list of supported input data types.
50+
- **odtypes**: list of supported input data types.
51+
- **policy**: output data type policy.
52+
53+
#### Unary.prototype.apply( x\[, options] )
54+
55+
Applies a unary function to each element in a provided input array.
56+
57+
```javascript
58+
var abs = require( '@stdlib/math/base/special/abs' );
59+
60+
var dtypes = [ 'float64', 'float32', 'generic' ];
61+
var policy = 'same';
62+
63+
var unary = new Unary( abs, dtypes, dtypes, policy );
64+
65+
var v = unary.apply( [ -1.0, 2.0, -3.0, 4.0 ] );
66+
// returns [ 1.0, 2.0, 3.0, 4.0 ]
67+
```
68+
69+
The method has the following parameters:
70+
71+
- **x**: input array.
72+
- **options**: function options.
73+
74+
The method accepts the following options:
75+
76+
- **dtype**: output array data type. Setting this option, overrides the output data type policy.
77+
78+
By default, the method returns an array having a data type determined by the output data type policy. To override the default behavior, set the `dtype` option.
79+
80+
```javascript
81+
var abs = require( '@stdlib/math/base/special/abs' );
82+
83+
var dtypes = [ 'float64', 'float32', 'generic' ];
84+
var policy = 'same';
85+
86+
var unary = new Unary( abs, dtypes, dtypes, policy );
87+
88+
var v = unary.apply( [ -1.0, 2.0, -3.0, 4.0 ], {
89+
'dtype': 'float64'
90+
});
91+
// returns <Float64Array>[ 1.0, 2.0, 3.0, 4.0 ]
92+
```
93+
94+
#### Unary.prototype.assign( x, out )
95+
96+
Applies a unary function to each element in a provided input array and assigns results to a provided output array.
97+
98+
```javascript
99+
var abs = require( '@stdlib/math/base/special/abs' );
100+
var zeros = require( '@stdlib/array/zeros' );
101+
102+
var dtypes = [ 'float64', 'float32', 'generic' ];
103+
var policy = 'same';
104+
105+
var unary = new Unary( abs, dtypes, dtypes, policy );
106+
107+
var out = zeros( 4, 'float64' );
108+
// returns <Float64Array>[ 0.0, 0.0, 0.0, 0.0 ]
109+
110+
var v = unary.assign( [ -1.0, 2.0, -3.0, 4.0 ], out );
111+
// returns <Float64Array>[ 1.0, 2.0, 3.0, 4.0 ]
112+
113+
var bool = ( v === out );
114+
// returns true
115+
```
116+
117+
The method has the following parameters:
118+
119+
- **x**: input array.
120+
- **out**: output array.
121+
122+
</section>
123+
124+
<!-- /.usage -->
125+
126+
<section class="notes">
127+
128+
## Notes
129+
130+
- The output data type policy only applies to the `apply` method. For the `assign` method, the output array is allowed to be any array-like object, including [accessor arrays][@stdlib/array/base/accessor].
131+
132+
</section>
133+
134+
<!-- /.notes -->
135+
136+
<section class="examples">
137+
138+
## Examples
139+
140+
<!-- eslint no-undef: "error" -->
141+
142+
```javascript
143+
var base = require( '@stdlib/math/base/special/sin' );
144+
var uniform = require( '@stdlib/random/array/uniform' );
145+
var dtypes = require( '@stdlib/array/dtypes' );
146+
var dtype = require( '@stdlib/array/dtype' );
147+
var logEach = require( '@stdlib/console/log-each' );
148+
var Unary = require( '@stdlib/math/array/tools/unary' );
149+
150+
// Define the supported input and output data types:
151+
var idt = dtypes( 'real_and_generic' );
152+
var odt = dtypes( 'real_floating_point_and_generic' );
153+
154+
// Define the policy mapping an input data type to an output data type:
155+
var policy = 'real_floating_point_and_generic';
156+
157+
// Create an interface for computing the element-wise sine:
158+
var sin = new Unary( base, idt, odt, policy );
159+
160+
// Generate an array of random numbers:
161+
var x = uniform( 10, -1.0, 1.0, {
162+
'dtype': 'generic'
163+
});
164+
165+
// Compute the element-wise sine:
166+
var y = sin.apply( x );
167+
168+
// Resolve the output array data type:
169+
var dt = dtype( y );
170+
console.log( dt );
171+
172+
// Print the results:
173+
```
174+
175+
</section>
176+
177+
<!-- /.examples -->
178+
179+
<!-- Section for related `stdlib` packages. Do not manually edit this section, as it is automatically populated. -->
180+
181+
<section class="related">
182+
183+
</section>
184+
185+
<!-- /.related -->
186+
187+
<!-- Section for all links. Make sure to keep an empty line after the `section` element and another before the `/section` close. -->
188+
189+
<section class="links">
190+
191+
[@stdlib/array/base/accessor]: https://github.com/stdlib-js/stdlib/tree/develop/lib/node_modules/%40stdlib/array/base/accessor
192+
193+
</section>
194+
195+
<!-- /.links -->
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,112 @@
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 isnan = require( '@stdlib/math/base/assert/is-nan' );
25+
var pow = require( '@stdlib/math/base/special/pow' );
26+
var identity = require( '@stdlib/math/base/special/identity' );
27+
var dtypes = require( '@stdlib/array/dtypes' );
28+
var uniform = require( '@stdlib/random/array/uniform' );
29+
var pkg = require( './../package.json' ).name;
30+
var Unary = require( './../lib' );
31+
32+
33+
// FUNCTIONS //
34+
35+
/**
36+
* Creates a benchmark function.
37+
*
38+
* @private
39+
* @param {PositiveInteger} len - array length
40+
* @returns {Function} benchmark function
41+
*/
42+
function createBenchmark( len ) {
43+
var unary;
44+
var dt;
45+
46+
dt = dtypes( 'real_floating_point' );
47+
unary = new Unary( identity, dt, dt, 'same' );
48+
49+
return benchmark;
50+
51+
/**
52+
* Benchmark function.
53+
*
54+
* @private
55+
* @param {Benchmark} b - benchmark instance
56+
*/
57+
function benchmark( b ) {
58+
var values;
59+
var o;
60+
var i;
61+
62+
values = [
63+
uniform( len, -50.0, 50.0, {
64+
'dtype': 'float64'
65+
}),
66+
uniform( len, -50.0, 50.0, {
67+
'dtype': 'float64'
68+
})
69+
];
70+
71+
b.tic();
72+
for ( i = 0; i < b.iterations; i++ ) {
73+
o = unary.apply( values[ i%values.length ] );
74+
if ( isnan( o[ i%len ] ) ) {
75+
b.fail( 'should not return NaN' );
76+
}
77+
}
78+
b.toc();
79+
if ( isnan( o[ i%len ] ) ) {
80+
b.fail( 'should not return NaN' );
81+
}
82+
b.pass( 'benchmark finished' );
83+
b.end();
84+
}
85+
}
86+
87+
88+
// MAIN //
89+
90+
/**
91+
* Main execution sequence.
92+
*
93+
* @private
94+
*/
95+
function main() {
96+
var len;
97+
var min;
98+
var max;
99+
var f;
100+
var i;
101+
102+
min = 1; // 10^min
103+
max = 6; // 10^max
104+
105+
for ( i = min; i <= max; i++ ) {
106+
len = pow( 10, i );
107+
f = createBenchmark( len );
108+
bench( pkg+':apply:len='+len, f );
109+
}
110+
}
111+
112+
main();

0 commit comments

Comments
 (0)