Skip to content

Commit 4b8fd47

Browse files
committed
feat: add math/array/tools/unary-factory
--- 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 17a0a25 commit 4b8fd47

File tree

12 files changed

+1877
-0
lines changed

12 files changed

+1877
-0
lines changed
Lines changed: 196 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,196 @@
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+
# factory
22+
23+
> Create a function 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 factory = require( '@stdlib/math/array/tools/unary-factory' );
31+
```
32+
33+
#### factory( fcn, idtypes, odtypes, policy )
34+
35+
Returns a function 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 = factory( 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( 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 = factory( abs, dtypes, dtypes, policy );
64+
65+
var v = unary( [ -1.0, 2.0, -3.0, 4.0 ] );
66+
// returns [ 1.0, 2.0, 3.0, 4.0 ]
67+
```
68+
69+
The function has the following parameters:
70+
71+
- **x**: input array.
72+
- **options**: function options.
73+
74+
The function 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 function 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 = factory( abs, dtypes, dtypes, policy );
87+
88+
var v = unary( [ -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.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 = factory( 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 function returned by `factory`. 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 factory = require( '@stdlib/math/array/tools/unary-factory' );
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 = factory( 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( x );
167+
168+
// Resolve the output array data type:
169+
var dt = dtype( y );
170+
console.log( dt );
171+
172+
// Print the results:
173+
logEach( 'sin(%0.5f) = %0.5f', x, y );
174+
```
175+
176+
</section>
177+
178+
<!-- /.examples -->
179+
180+
<!-- Section for related `stdlib` packages. Do not manually edit this section, as it is automatically populated. -->
181+
182+
<section class="related">
183+
184+
</section>
185+
186+
<!-- /.related -->
187+
188+
<!-- Section for all links. Make sure to keep an empty line after the `section` element and another before the `/section` close. -->
189+
190+
<section class="links">
191+
192+
[@stdlib/array/base/accessor]: https://github.com/stdlib-js/stdlib/tree/develop/lib/node_modules/%40stdlib/array/base/accessor
193+
194+
</section>
195+
196+
<!-- /.links -->
Lines changed: 115 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,115 @@
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 zeros = require( '@stdlib/array/zeros' );
30+
var pkg = require( './../package.json' ).name;
31+
var factory = require( './../lib' );
32+
33+
34+
// FUNCTIONS //
35+
36+
/**
37+
* Creates a benchmark function.
38+
*
39+
* @private
40+
* @param {PositiveInteger} len - array length
41+
* @returns {Function} benchmark function
42+
*/
43+
function createBenchmark( len ) {
44+
var dt;
45+
var f;
46+
47+
dt = dtypes( 'real_floating_point' );
48+
f = factory( identity, dt, dt, 'same' );
49+
50+
return benchmark;
51+
52+
/**
53+
* Benchmark function.
54+
*
55+
* @private
56+
* @param {Benchmark} b - benchmark instance
57+
*/
58+
function benchmark( b ) {
59+
var values;
60+
var out;
61+
var o;
62+
var i;
63+
64+
values = [
65+
uniform( len, -50.0, 50.0, {
66+
'dtype': 'float64'
67+
}),
68+
uniform( len, -50.0, 50.0, {
69+
'dtype': 'float64'
70+
})
71+
];
72+
out = zeros( len, 'float64' );
73+
74+
b.tic();
75+
for ( i = 0; i < b.iterations; i++ ) {
76+
o = f.assign( values[ i%values.length ], out );
77+
if ( isnan( o[ i%len ] ) ) {
78+
b.fail( 'should not return NaN' );
79+
}
80+
}
81+
b.toc();
82+
if ( isnan( o[ i%len ] ) ) {
83+
b.fail( 'should not return NaN' );
84+
}
85+
b.pass( 'benchmark finished' );
86+
b.end();
87+
}
88+
}
89+
90+
91+
// MAIN //
92+
93+
/**
94+
* Main execution sequence.
95+
*
96+
* @private
97+
*/
98+
function main() {
99+
var len;
100+
var min;
101+
var max;
102+
var f;
103+
var i;
104+
105+
min = 1; // 10^min
106+
max = 6; // 10^max
107+
108+
for ( i = min; i <= max; i++ ) {
109+
len = pow( 10, i );
110+
f = createBenchmark( len );
111+
bench( pkg+':assign:len='+len, f );
112+
}
113+
}
114+
115+
main();
Lines changed: 60 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,60 @@
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 abs = require( '@stdlib/math/base/special/abs' );
25+
var sin = require( '@stdlib/math/base/special/sin' );
26+
var pkg = require( './../package.json' ).name;
27+
var factory = require( './../lib' );
28+
29+
30+
// MAIN //
31+
32+
bench( pkg+'::factory', function benchmark( b ) {
33+
var values;
34+
var dtypes;
35+
var v;
36+
var i;
37+
38+
values = [
39+
abs,
40+
sin
41+
];
42+
dtypes = [
43+
'float64',
44+
'float32'
45+
];
46+
47+
b.tic();
48+
for ( i = 0; i < b.iterations; i++ ) {
49+
v = factory( values[ i%values.length ], dtypes, dtypes, 'same' );
50+
if ( typeof v !== 'function' ) {
51+
b.fail( 'should return a function' );
52+
}
53+
}
54+
b.toc();
55+
if ( typeof v !== 'function' ) {
56+
b.fail( 'should return a function' );
57+
}
58+
b.pass( 'benchmark finished' );
59+
b.end();
60+
});

0 commit comments

Comments
 (0)