@stdlib/math-array-tools-unary 0.1.0
This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
- package/LICENSE +177 -0
- package/NOTICE +1 -0
- package/README.md +287 -0
- package/SECURITY.md +5 -0
- package/dist/index.d.ts +3 -0
- package/dist/index.js +7 -0
- package/dist/index.js.map +7 -0
- package/docs/types/index.d.ts +223 -0
- package/lib/index.js +50 -0
- package/lib/main.js +217 -0
- package/lib/validate.js +68 -0
- package/package.json +84 -0
package/LICENSE
ADDED
|
@@ -0,0 +1,177 @@
|
|
|
1
|
+
|
|
2
|
+
Apache License
|
|
3
|
+
Version 2.0, January 2004
|
|
4
|
+
http://www.apache.org/licenses/
|
|
5
|
+
|
|
6
|
+
TERMS AND CONDITIONS FOR USE, REPRODUCTION, AND DISTRIBUTION
|
|
7
|
+
|
|
8
|
+
1. Definitions.
|
|
9
|
+
|
|
10
|
+
"License" shall mean the terms and conditions for use, reproduction,
|
|
11
|
+
and distribution as defined by Sections 1 through 9 of this document.
|
|
12
|
+
|
|
13
|
+
"Licensor" shall mean the copyright owner or entity authorized by
|
|
14
|
+
the copyright owner that is granting the License.
|
|
15
|
+
|
|
16
|
+
"Legal Entity" shall mean the union of the acting entity and all
|
|
17
|
+
other entities that control, are controlled by, or are under common
|
|
18
|
+
control with that entity. For the purposes of this definition,
|
|
19
|
+
"control" means (i) the power, direct or indirect, to cause the
|
|
20
|
+
direction or management of such entity, whether by contract or
|
|
21
|
+
otherwise, or (ii) ownership of fifty percent (50%) or more of the
|
|
22
|
+
outstanding shares, or (iii) beneficial ownership of such entity.
|
|
23
|
+
|
|
24
|
+
"You" (or "Your") shall mean an individual or Legal Entity
|
|
25
|
+
exercising permissions granted by this License.
|
|
26
|
+
|
|
27
|
+
"Source" form shall mean the preferred form for making modifications,
|
|
28
|
+
including but not limited to software source code, documentation
|
|
29
|
+
source, and configuration files.
|
|
30
|
+
|
|
31
|
+
"Object" form shall mean any form resulting from mechanical
|
|
32
|
+
transformation or translation of a Source form, including but
|
|
33
|
+
not limited to compiled object code, generated documentation,
|
|
34
|
+
and conversions to other media types.
|
|
35
|
+
|
|
36
|
+
"Work" shall mean the work of authorship, whether in Source or
|
|
37
|
+
Object form, made available under the License, as indicated by a
|
|
38
|
+
copyright notice that is included in or attached to the work
|
|
39
|
+
(an example is provided in the Appendix below).
|
|
40
|
+
|
|
41
|
+
"Derivative Works" shall mean any work, whether in Source or Object
|
|
42
|
+
form, that is based on (or derived from) the Work and for which the
|
|
43
|
+
editorial revisions, annotations, elaborations, or other modifications
|
|
44
|
+
represent, as a whole, an original work of authorship. For the purposes
|
|
45
|
+
of this License, Derivative Works shall not include works that remain
|
|
46
|
+
separable from, or merely link (or bind by name) to the interfaces of,
|
|
47
|
+
the Work and Derivative Works thereof.
|
|
48
|
+
|
|
49
|
+
"Contribution" shall mean any work of authorship, including
|
|
50
|
+
the original version of the Work and any modifications or additions
|
|
51
|
+
to that Work or Derivative Works thereof, that is intentionally
|
|
52
|
+
submitted to Licensor for inclusion in the Work by the copyright owner
|
|
53
|
+
or by an individual or Legal Entity authorized to submit on behalf of
|
|
54
|
+
the copyright owner. For the purposes of this definition, "submitted"
|
|
55
|
+
means any form of electronic, verbal, or written communication sent
|
|
56
|
+
to the Licensor or its representatives, including but not limited to
|
|
57
|
+
communication on electronic mailing lists, source code control systems,
|
|
58
|
+
and issue tracking systems that are managed by, or on behalf of, the
|
|
59
|
+
Licensor for the purpose of discussing and improving the Work, but
|
|
60
|
+
excluding communication that is conspicuously marked or otherwise
|
|
61
|
+
designated in writing by the copyright owner as "Not a Contribution."
|
|
62
|
+
|
|
63
|
+
"Contributor" shall mean Licensor and any individual or Legal Entity
|
|
64
|
+
on behalf of whom a Contribution has been received by Licensor and
|
|
65
|
+
subsequently incorporated within the Work.
|
|
66
|
+
|
|
67
|
+
2. Grant of Copyright License. Subject to the terms and conditions of
|
|
68
|
+
this License, each Contributor hereby grants to You a perpetual,
|
|
69
|
+
worldwide, non-exclusive, no-charge, royalty-free, irrevocable
|
|
70
|
+
copyright license to reproduce, prepare Derivative Works of,
|
|
71
|
+
publicly display, publicly perform, sublicense, and distribute the
|
|
72
|
+
Work and such Derivative Works in Source or Object form.
|
|
73
|
+
|
|
74
|
+
3. Grant of Patent License. Subject to the terms and conditions of
|
|
75
|
+
this License, each Contributor hereby grants to You a perpetual,
|
|
76
|
+
worldwide, non-exclusive, no-charge, royalty-free, irrevocable
|
|
77
|
+
(except as stated in this section) patent license to make, have made,
|
|
78
|
+
use, offer to sell, sell, import, and otherwise transfer the Work,
|
|
79
|
+
where such license applies only to those patent claims licensable
|
|
80
|
+
by such Contributor that are necessarily infringed by their
|
|
81
|
+
Contribution(s) alone or by combination of their Contribution(s)
|
|
82
|
+
with the Work to which such Contribution(s) was submitted. If You
|
|
83
|
+
institute patent litigation against any entity (including a
|
|
84
|
+
cross-claim or counterclaim in a lawsuit) alleging that the Work
|
|
85
|
+
or a Contribution incorporated within the Work constitutes direct
|
|
86
|
+
or contributory patent infringement, then any patent licenses
|
|
87
|
+
granted to You under this License for that Work shall terminate
|
|
88
|
+
as of the date such litigation is filed.
|
|
89
|
+
|
|
90
|
+
4. Redistribution. You may reproduce and distribute copies of the
|
|
91
|
+
Work or Derivative Works thereof in any medium, with or without
|
|
92
|
+
modifications, and in Source or Object form, provided that You
|
|
93
|
+
meet the following conditions:
|
|
94
|
+
|
|
95
|
+
(a) You must give any other recipients of the Work or
|
|
96
|
+
Derivative Works a copy of this License; and
|
|
97
|
+
|
|
98
|
+
(b) You must cause any modified files to carry prominent notices
|
|
99
|
+
stating that You changed the files; and
|
|
100
|
+
|
|
101
|
+
(c) You must retain, in the Source form of any Derivative Works
|
|
102
|
+
that You distribute, all copyright, patent, trademark, and
|
|
103
|
+
attribution notices from the Source form of the Work,
|
|
104
|
+
excluding those notices that do not pertain to any part of
|
|
105
|
+
the Derivative Works; and
|
|
106
|
+
|
|
107
|
+
(d) If the Work includes a "NOTICE" text file as part of its
|
|
108
|
+
distribution, then any Derivative Works that You distribute must
|
|
109
|
+
include a readable copy of the attribution notices contained
|
|
110
|
+
within such NOTICE file, excluding those notices that do not
|
|
111
|
+
pertain to any part of the Derivative Works, in at least one
|
|
112
|
+
of the following places: within a NOTICE text file distributed
|
|
113
|
+
as part of the Derivative Works; within the Source form or
|
|
114
|
+
documentation, if provided along with the Derivative Works; or,
|
|
115
|
+
within a display generated by the Derivative Works, if and
|
|
116
|
+
wherever such third-party notices normally appear. The contents
|
|
117
|
+
of the NOTICE file are for informational purposes only and
|
|
118
|
+
do not modify the License. You may add Your own attribution
|
|
119
|
+
notices within Derivative Works that You distribute, alongside
|
|
120
|
+
or as an addendum to the NOTICE text from the Work, provided
|
|
121
|
+
that such additional attribution notices cannot be construed
|
|
122
|
+
as modifying the License.
|
|
123
|
+
|
|
124
|
+
You may add Your own copyright statement to Your modifications and
|
|
125
|
+
may provide additional or different license terms and conditions
|
|
126
|
+
for use, reproduction, or distribution of Your modifications, or
|
|
127
|
+
for any such Derivative Works as a whole, provided Your use,
|
|
128
|
+
reproduction, and distribution of the Work otherwise complies with
|
|
129
|
+
the conditions stated in this License.
|
|
130
|
+
|
|
131
|
+
5. Submission of Contributions. Unless You explicitly state otherwise,
|
|
132
|
+
any Contribution intentionally submitted for inclusion in the Work
|
|
133
|
+
by You to the Licensor shall be under the terms and conditions of
|
|
134
|
+
this License, without any additional terms or conditions.
|
|
135
|
+
Notwithstanding the above, nothing herein shall supersede or modify
|
|
136
|
+
the terms of any separate license agreement you may have executed
|
|
137
|
+
with Licensor regarding such Contributions.
|
|
138
|
+
|
|
139
|
+
6. Trademarks. This License does not grant permission to use the trade
|
|
140
|
+
names, trademarks, service marks, or product names of the Licensor,
|
|
141
|
+
except as required for reasonable and customary use in describing the
|
|
142
|
+
origin of the Work and reproducing the content of the NOTICE file.
|
|
143
|
+
|
|
144
|
+
7. Disclaimer of Warranty. Unless required by applicable law or
|
|
145
|
+
agreed to in writing, Licensor provides the Work (and each
|
|
146
|
+
Contributor provides its Contributions) on an "AS IS" BASIS,
|
|
147
|
+
WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or
|
|
148
|
+
implied, including, without limitation, any warranties or conditions
|
|
149
|
+
of TITLE, NON-INFRINGEMENT, MERCHANTABILITY, or FITNESS FOR A
|
|
150
|
+
PARTICULAR PURPOSE. You are solely responsible for determining the
|
|
151
|
+
appropriateness of using or redistributing the Work and assume any
|
|
152
|
+
risks associated with Your exercise of permissions under this License.
|
|
153
|
+
|
|
154
|
+
8. Limitation of Liability. In no event and under no legal theory,
|
|
155
|
+
whether in tort (including negligence), contract, or otherwise,
|
|
156
|
+
unless required by applicable law (such as deliberate and grossly
|
|
157
|
+
negligent acts) or agreed to in writing, shall any Contributor be
|
|
158
|
+
liable to You for damages, including any direct, indirect, special,
|
|
159
|
+
incidental, or consequential damages of any character arising as a
|
|
160
|
+
result of this License or out of the use or inability to use the
|
|
161
|
+
Work (including but not limited to damages for loss of goodwill,
|
|
162
|
+
work stoppage, computer failure or malfunction, or any and all
|
|
163
|
+
other commercial damages or losses), even if such Contributor
|
|
164
|
+
has been advised of the possibility of such damages.
|
|
165
|
+
|
|
166
|
+
9. Accepting Warranty or Additional Liability. While redistributing
|
|
167
|
+
the Work or Derivative Works thereof, You may choose to offer,
|
|
168
|
+
and charge a fee for, acceptance of support, warranty, indemnity,
|
|
169
|
+
or other liability obligations and/or rights consistent with this
|
|
170
|
+
License. However, in accepting such obligations, You may act only
|
|
171
|
+
on Your own behalf and on Your sole responsibility, not on behalf
|
|
172
|
+
of any other Contributor, and only if You agree to indemnify,
|
|
173
|
+
defend, and hold each Contributor harmless for any liability
|
|
174
|
+
incurred by, or claims asserted against, such Contributor by reason
|
|
175
|
+
of your accepting any such warranty or additional liability.
|
|
176
|
+
|
|
177
|
+
END OF TERMS AND CONDITIONS
|
package/NOTICE
ADDED
|
@@ -0,0 +1 @@
|
|
|
1
|
+
Copyright (c) 2016-2026 The Stdlib Authors.
|
package/README.md
ADDED
|
@@ -0,0 +1,287 @@
|
|
|
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
|
+
|
|
22
|
+
<details>
|
|
23
|
+
<summary>
|
|
24
|
+
About stdlib...
|
|
25
|
+
</summary>
|
|
26
|
+
<p>We believe in a future in which the web is a preferred environment for numerical computation. To help realize this future, we've built stdlib. stdlib is a standard library, with an emphasis on numerical and scientific computation, written in JavaScript (and C) for execution in browsers and in Node.js.</p>
|
|
27
|
+
<p>The library is fully decomposable, being architected in such a way that you can swap out and mix and match APIs and functionality to cater to your exact preferences and use cases.</p>
|
|
28
|
+
<p>When you use stdlib, you can be absolutely certain that you are using the most thorough, rigorous, well-written, studied, documented, tested, measured, and high-quality code out there.</p>
|
|
29
|
+
<p>To join us in bringing numerical computing to the web, get started by checking us out on <a href="https://github.com/stdlib-js/stdlib">GitHub</a>, and please consider <a href="https://opencollective.com/stdlib">financially supporting stdlib</a>. We greatly appreciate your continued support!</p>
|
|
30
|
+
</details>
|
|
31
|
+
|
|
32
|
+
# Unary
|
|
33
|
+
|
|
34
|
+
[![NPM version][npm-image]][npm-url] [![Build Status][test-image]][test-url] [![Coverage Status][coverage-image]][coverage-url] <!-- [![dependencies][dependencies-image]][dependencies-url] -->
|
|
35
|
+
|
|
36
|
+
> Constructor for applying a unary function to each element in an input array.
|
|
37
|
+
|
|
38
|
+
<section class="installation">
|
|
39
|
+
|
|
40
|
+
## Installation
|
|
41
|
+
|
|
42
|
+
```bash
|
|
43
|
+
npm install @stdlib/math-array-tools-unary
|
|
44
|
+
```
|
|
45
|
+
|
|
46
|
+
</section>
|
|
47
|
+
|
|
48
|
+
<section class="usage">
|
|
49
|
+
|
|
50
|
+
## Usage
|
|
51
|
+
|
|
52
|
+
```javascript
|
|
53
|
+
var Unary = require( '@stdlib/math-array-tools-unary' );
|
|
54
|
+
```
|
|
55
|
+
|
|
56
|
+
#### Unary( fcn, idtypes, odtypes, policy )
|
|
57
|
+
|
|
58
|
+
Constructor for applying a unary function to each element in an input array.
|
|
59
|
+
|
|
60
|
+
```javascript
|
|
61
|
+
var abs = require( '@stdlib/math-base-special-abs' );
|
|
62
|
+
|
|
63
|
+
var dtypes = [ 'float64', 'float32', 'generic' ];
|
|
64
|
+
var policy = 'same';
|
|
65
|
+
|
|
66
|
+
var unary = new Unary( abs, dtypes, dtypes, policy );
|
|
67
|
+
```
|
|
68
|
+
|
|
69
|
+
The constructor has the following parameters:
|
|
70
|
+
|
|
71
|
+
- **fcn**: unary function to apply.
|
|
72
|
+
- **idtypes**: list of supported input data types.
|
|
73
|
+
- **odtypes**: list of supported input data types.
|
|
74
|
+
- **policy**: output data type policy.
|
|
75
|
+
|
|
76
|
+
#### Unary.prototype.apply( x\[, options] )
|
|
77
|
+
|
|
78
|
+
Applies a unary function to each element in a provided input array.
|
|
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
|
+
// returns [ 1.0, 2.0, 3.0, 4.0 ]
|
|
90
|
+
```
|
|
91
|
+
|
|
92
|
+
The method has the following parameters:
|
|
93
|
+
|
|
94
|
+
- **x**: input array.
|
|
95
|
+
- **options**: function options.
|
|
96
|
+
|
|
97
|
+
The method accepts the following options:
|
|
98
|
+
|
|
99
|
+
- **dtype**: output array data type. Setting this option, overrides the output data type policy.
|
|
100
|
+
|
|
101
|
+
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.
|
|
102
|
+
|
|
103
|
+
```javascript
|
|
104
|
+
var abs = require( '@stdlib/math-base-special-abs' );
|
|
105
|
+
|
|
106
|
+
var dtypes = [ 'float64', 'float32', 'generic' ];
|
|
107
|
+
var policy = 'same';
|
|
108
|
+
|
|
109
|
+
var unary = new Unary( abs, dtypes, dtypes, policy );
|
|
110
|
+
|
|
111
|
+
var v = unary.apply( [ -1.0, 2.0, -3.0, 4.0 ], {
|
|
112
|
+
'dtype': 'float64'
|
|
113
|
+
});
|
|
114
|
+
// returns <Float64Array>[ 1.0, 2.0, 3.0, 4.0 ]
|
|
115
|
+
```
|
|
116
|
+
|
|
117
|
+
#### Unary.prototype.assign( x, out )
|
|
118
|
+
|
|
119
|
+
Applies a unary function to each element in a provided input array and assigns results to a provided output array.
|
|
120
|
+
|
|
121
|
+
```javascript
|
|
122
|
+
var abs = require( '@stdlib/math-base-special-abs' );
|
|
123
|
+
var zeros = require( '@stdlib/array-zeros' );
|
|
124
|
+
|
|
125
|
+
var dtypes = [ 'float64', 'float32', 'generic' ];
|
|
126
|
+
var policy = 'same';
|
|
127
|
+
|
|
128
|
+
var unary = new Unary( abs, dtypes, dtypes, policy );
|
|
129
|
+
|
|
130
|
+
var out = zeros( 4, 'float64' );
|
|
131
|
+
// returns <Float64Array>[ 0.0, 0.0, 0.0, 0.0 ]
|
|
132
|
+
|
|
133
|
+
var v = unary.assign( [ -1.0, 2.0, -3.0, 4.0 ], out );
|
|
134
|
+
// returns <Float64Array>[ 1.0, 2.0, 3.0, 4.0 ]
|
|
135
|
+
|
|
136
|
+
var bool = ( v === out );
|
|
137
|
+
// returns true
|
|
138
|
+
```
|
|
139
|
+
|
|
140
|
+
The method has the following parameters:
|
|
141
|
+
|
|
142
|
+
- **x**: input array.
|
|
143
|
+
- **out**: output array.
|
|
144
|
+
|
|
145
|
+
</section>
|
|
146
|
+
|
|
147
|
+
<!-- /.usage -->
|
|
148
|
+
|
|
149
|
+
<section class="notes">
|
|
150
|
+
|
|
151
|
+
## Notes
|
|
152
|
+
|
|
153
|
+
- 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].
|
|
154
|
+
|
|
155
|
+
</section>
|
|
156
|
+
|
|
157
|
+
<!-- /.notes -->
|
|
158
|
+
|
|
159
|
+
<section class="examples">
|
|
160
|
+
|
|
161
|
+
## Examples
|
|
162
|
+
|
|
163
|
+
<!-- eslint no-undef: "error" -->
|
|
164
|
+
|
|
165
|
+
```javascript
|
|
166
|
+
var base = require( '@stdlib/math-base-special-sin' );
|
|
167
|
+
var uniform = require( '@stdlib/random-array-uniform' );
|
|
168
|
+
var dtypes = require( '@stdlib/array-dtypes' );
|
|
169
|
+
var dtype = require( '@stdlib/array-dtype' );
|
|
170
|
+
var logEach = require( '@stdlib/console-log-each' );
|
|
171
|
+
var Unary = require( '@stdlib/math-array-tools-unary' );
|
|
172
|
+
|
|
173
|
+
// Define the supported input and output data types:
|
|
174
|
+
var idt = dtypes( 'real_and_generic' );
|
|
175
|
+
var odt = dtypes( 'real_floating_point_and_generic' );
|
|
176
|
+
|
|
177
|
+
// Define the policy mapping an input data type to an output data type:
|
|
178
|
+
var policy = 'real_floating_point_and_generic';
|
|
179
|
+
|
|
180
|
+
// Create an interface for computing the element-wise sine:
|
|
181
|
+
var sin = new Unary( base, idt, odt, policy );
|
|
182
|
+
|
|
183
|
+
// Generate an array of random numbers:
|
|
184
|
+
var x = uniform( 10, -1.0, 1.0, {
|
|
185
|
+
'dtype': 'generic'
|
|
186
|
+
});
|
|
187
|
+
|
|
188
|
+
// Compute the element-wise sine:
|
|
189
|
+
var y = sin.apply( x );
|
|
190
|
+
|
|
191
|
+
// Resolve the output array data type:
|
|
192
|
+
var dt = dtype( y );
|
|
193
|
+
console.log( dt );
|
|
194
|
+
|
|
195
|
+
// Print the results:
|
|
196
|
+
logEach( 'sin(%0.5f) = %0.5f', x, y );
|
|
197
|
+
```
|
|
198
|
+
|
|
199
|
+
</section>
|
|
200
|
+
|
|
201
|
+
<!-- /.examples -->
|
|
202
|
+
|
|
203
|
+
<!-- Section for related `stdlib` packages. Do not manually edit this section, as it is automatically populated. -->
|
|
204
|
+
|
|
205
|
+
<section class="related">
|
|
206
|
+
|
|
207
|
+
</section>
|
|
208
|
+
|
|
209
|
+
<!-- /.related -->
|
|
210
|
+
|
|
211
|
+
<!-- Section for all links. Make sure to keep an empty line after the `section` element and another before the `/section` close. -->
|
|
212
|
+
|
|
213
|
+
|
|
214
|
+
<section class="main-repo" >
|
|
215
|
+
|
|
216
|
+
* * *
|
|
217
|
+
|
|
218
|
+
## Notice
|
|
219
|
+
|
|
220
|
+
This package is part of [stdlib][stdlib], a standard library for JavaScript and Node.js, with an emphasis on numerical and scientific computing. The library provides a collection of robust, high performance libraries for mathematics, statistics, streams, utilities, and more.
|
|
221
|
+
|
|
222
|
+
For more information on the project, filing bug reports and feature requests, and guidance on how to develop [stdlib][stdlib], see the main project [repository][stdlib].
|
|
223
|
+
|
|
224
|
+
#### Community
|
|
225
|
+
|
|
226
|
+
[![Chat][chat-image]][chat-url]
|
|
227
|
+
|
|
228
|
+
---
|
|
229
|
+
|
|
230
|
+
## License
|
|
231
|
+
|
|
232
|
+
See [LICENSE][stdlib-license].
|
|
233
|
+
|
|
234
|
+
|
|
235
|
+
## Copyright
|
|
236
|
+
|
|
237
|
+
Copyright © 2016-2026. The Stdlib [Authors][stdlib-authors].
|
|
238
|
+
|
|
239
|
+
</section>
|
|
240
|
+
|
|
241
|
+
<!-- /.stdlib -->
|
|
242
|
+
|
|
243
|
+
<!-- Section for all links. Make sure to keep an empty line after the `section` element and another before the `/section` close. -->
|
|
244
|
+
|
|
245
|
+
<section class="links">
|
|
246
|
+
|
|
247
|
+
[npm-image]: http://img.shields.io/npm/v/@stdlib/math-array-tools-unary.svg
|
|
248
|
+
[npm-url]: https://npmjs.org/package/@stdlib/math-array-tools-unary
|
|
249
|
+
|
|
250
|
+
[test-image]: https://github.com/stdlib-js/math-array-tools-unary/actions/workflows/test.yml/badge.svg?branch=v0.1.0
|
|
251
|
+
[test-url]: https://github.com/stdlib-js/math-array-tools-unary/actions/workflows/test.yml?query=branch:v0.1.0
|
|
252
|
+
|
|
253
|
+
[coverage-image]: https://img.shields.io/codecov/c/github/stdlib-js/math-array-tools-unary/main.svg
|
|
254
|
+
[coverage-url]: https://codecov.io/github/stdlib-js/math-array-tools-unary?branch=main
|
|
255
|
+
|
|
256
|
+
<!--
|
|
257
|
+
|
|
258
|
+
[dependencies-image]: https://img.shields.io/david/stdlib-js/math-array-tools-unary.svg
|
|
259
|
+
[dependencies-url]: https://david-dm.org/stdlib-js/math-array-tools-unary/main
|
|
260
|
+
|
|
261
|
+
-->
|
|
262
|
+
|
|
263
|
+
[chat-image]: https://img.shields.io/badge/zulip-join_chat-brightgreen.svg
|
|
264
|
+
[chat-url]: https://stdlib.zulipchat.com
|
|
265
|
+
|
|
266
|
+
[stdlib]: https://github.com/stdlib-js/stdlib
|
|
267
|
+
|
|
268
|
+
[stdlib-authors]: https://github.com/stdlib-js/stdlib/graphs/contributors
|
|
269
|
+
|
|
270
|
+
[umd]: https://github.com/umdjs/umd
|
|
271
|
+
[es-module]: https://developer.mozilla.org/en-US/docs/Web/JavaScript/Guide/Modules
|
|
272
|
+
|
|
273
|
+
[deno-url]: https://github.com/stdlib-js/math-array-tools-unary/tree/deno
|
|
274
|
+
[deno-readme]: https://github.com/stdlib-js/math-array-tools-unary/blob/deno/README.md
|
|
275
|
+
[umd-url]: https://github.com/stdlib-js/math-array-tools-unary/tree/umd
|
|
276
|
+
[umd-readme]: https://github.com/stdlib-js/math-array-tools-unary/blob/umd/README.md
|
|
277
|
+
[esm-url]: https://github.com/stdlib-js/math-array-tools-unary/tree/esm
|
|
278
|
+
[esm-readme]: https://github.com/stdlib-js/math-array-tools-unary/blob/esm/README.md
|
|
279
|
+
[branches-url]: https://github.com/stdlib-js/math-array-tools-unary/blob/main/branches.md
|
|
280
|
+
|
|
281
|
+
[stdlib-license]: https://raw.githubusercontent.com/stdlib-js/math-array-tools-unary/main/LICENSE
|
|
282
|
+
|
|
283
|
+
[@stdlib/array/base/accessor]: https://www.npmjs.com/package/@stdlib/array-base-accessor
|
|
284
|
+
|
|
285
|
+
</section>
|
|
286
|
+
|
|
287
|
+
<!-- /.links -->
|
package/SECURITY.md
ADDED
package/dist/index.d.ts
ADDED
package/dist/index.js
ADDED
|
@@ -0,0 +1,7 @@
|
|
|
1
|
+
"use strict";var p=function(t,r){return function(){return r||t((r={exports:{}}).exports,r),r.exports}};var h=p(function(B,v){
|
|
2
|
+
var _=require('@stdlib/assert-is-plain-object/dist'),j=require('@stdlib/assert-has-own-property/dist'),V=require('@stdlib/array-base-assert-contains/dist'),y=require('@stdlib/error-tools-fmtprodmsg/dist');function F(t,r,e){return _(e)?j(e,"dtype")&&(t.dtype=e.dtype,!V(r,t.dtype))?new TypeError(y('27x4S',"dtype",r.join('", "'),t.dtype)):null:new TypeError(y('27x2V',e));}v.exports=F
|
|
3
|
+
});var E=p(function(G,b){
|
|
4
|
+
var d=require('@stdlib/utils-define-nonenumerable-read-only-property/dist'),O=require('@stdlib/assert-is-function/dist'),u=require('@stdlib/assert-is-collection/dist'),m=require('@stdlib/array-base-assert-is-data-type/dist'),D=require('@stdlib/ndarray-base-assert-is-output-data-type-policy/dist'),w=require('@stdlib/array-base-assert-contains/dist'),c=require('@stdlib/strided-base-unary/dist'),k=require('@stdlib/ndarray-base-unary-output-dtype/dist'),P=require('@stdlib/array-empty/dist'),q=require('@stdlib/array-dtype/dist'),g=require('@stdlib/array-base-every-by/dist'),f=require('@stdlib/array-base-copy/dist'),i=require('@stdlib/error-tools-fmtprodmsg/dist'),C=h(),T="generic";function s(t,r,e,a){if(!(this instanceof s))return new s(t,r,e,a);if(!O(t))throw new TypeError(i('27x3c',t));if(!u(r)||r.length<1||!g(r,m))throw new TypeError(i('27xHW',r));if(!u(e)||e.length<1||!g(e,m))throw new TypeError(i('27xHX',e));if(!D(a))throw new TypeError(i('27xHY',a));return this._fcn=t,this._idtypes=f(r),this._odtypes=f(e),this._policy=a,this}d(s.prototype,"apply",function(r,e){var a,o,l,n;if(!u(r))throw new TypeError(i('27x2O',r));if(n=q(r)||T,!w(this._idtypes,n))throw new TypeError(i('27xHZ',this._idtypes.join('", "'),n));if(a={},arguments.length>1&&(o=C(a,this._odtypes,e),o))throw o;return n=a.dtype||k(n,this._policy),l=P(r.length,n),c([r,l],[r.length],[1,1],this._fcn),l});d(s.prototype,"assign",function(r,e){var a;if(!u(r))throw new TypeError(i('27x2O',r));if(!u(e))throw new TypeError(i('27x2y',e));if(a=q(r)||T,!w(this._idtypes,a))throw new TypeError(i('27xHZ',this._idtypes.join('", "'),a));return c([r,e],[r.length],[1,1],this._fcn),e});b.exports=s
|
|
5
|
+
});var R=E();module.exports=R;
|
|
6
|
+
/** @license Apache-2.0 */
|
|
7
|
+
//# sourceMappingURL=index.js.map
|
|
@@ -0,0 +1,7 @@
|
|
|
1
|
+
{
|
|
2
|
+
"version": 3,
|
|
3
|
+
"sources": ["../lib/validate.js", "../lib/main.js", "../lib/index.js"],
|
|
4
|
+
"sourcesContent": ["/**\n* @license Apache-2.0\n*\n* Copyright (c) 2025 The Stdlib Authors.\n*\n* Licensed under the Apache License, Version 2.0 (the \"License\");\n* you may not use this file except in compliance with the License.\n* You may obtain a copy of the License at\n*\n* http://www.apache.org/licenses/LICENSE-2.0\n*\n* Unless required by applicable law or agreed to in writing, software\n* distributed under the License is distributed on an \"AS IS\" BASIS,\n* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.\n* See the License for the specific language governing permissions and\n* limitations under the License.\n*/\n\n'use strict';\n\n// MODULES //\n\nvar isObject = require( '@stdlib/assert-is-plain-object' );\nvar hasOwnProp = require( '@stdlib/assert-has-own-property' );\nvar contains = require( '@stdlib/array-base-assert-contains' );\nvar format = require( '@stdlib/string-format' );\n\n\n// MAIN //\n\n/**\n* Validates function options.\n*\n* @private\n* @param {Object} opts - destination object\n* @param {Array} dtypes - list of supported output data types\n* @param {Options} options - function options\n* @param {string} [options.dtype] - output array data type\n* @returns {(Error|null)} null or an error object\n*\n* @example\n* var opts = {};\n* var dtypes = [ 'float64', 'float32', 'generic' ];\n* var options = {\n* 'dtype': 'float64'\n* };\n* var err = validate( opts, dtypes, options );\n* if ( err ) {\n* throw err;\n* }\n*/\nfunction validate( opts, dtypes, options ) {\n\tif ( !isObject( options ) ) {\n\t\treturn new TypeError( format( 'invalid argument. Options argument must be an object. Value: `%s`.', options ) );\n\t}\n\tif ( hasOwnProp( options, 'dtype' ) ) {\n\t\topts.dtype = options.dtype;\n\t\tif ( !contains( dtypes, opts.dtype ) ) {\n\t\t\treturn new TypeError( format( 'invalid option. `%s` option must be one of the following: \"%s\". Option: `%s`.', 'dtype', dtypes.join( '\", \"' ), opts.dtype ) );\n\t\t}\n\t}\n\treturn null;\n}\n\n\n// EXPORTS //\n\nmodule.exports = validate;\n", "/**\n* @license Apache-2.0\n*\n* Copyright (c) 2025 The Stdlib Authors.\n*\n* Licensed under the Apache License, Version 2.0 (the \"License\");\n* you may not use this file except in compliance with the License.\n* You may obtain a copy of the License at\n*\n* http://www.apache.org/licenses/LICENSE-2.0\n*\n* Unless required by applicable law or agreed to in writing, software\n* distributed under the License is distributed on an \"AS IS\" BASIS,\n* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.\n* See the License for the specific language governing permissions and\n* limitations under the License.\n*/\n\n/* eslint-disable no-restricted-syntax, no-invalid-this */\n\n'use strict';\n\n// MODULES //\n\nvar setReadOnly = require( '@stdlib/utils-define-nonenumerable-read-only-property' );\nvar isFunction = require( '@stdlib/assert-is-function' );\nvar isCollection = require( '@stdlib/assert-is-collection' );\nvar isDataType = require( '@stdlib/array-base-assert-is-data-type' );\nvar isOutputDataTypePolicy = require( '@stdlib/ndarray-base-assert-is-output-data-type-policy' );\nvar contains = require( '@stdlib/array-base-assert-contains' );\nvar unary = require( '@stdlib/strided-base-unary' );\nvar odtype = require( '@stdlib/ndarray-base-unary-output-dtype' );\nvar empty = require( '@stdlib/array-empty' );\nvar dtype = require( '@stdlib/array-dtype' );\nvar everyBy = require( '@stdlib/array-base-every-by' );\nvar copy = require( '@stdlib/array-base-copy' );\nvar format = require( '@stdlib/string-format' );\nvar validate = require( './validate.js' );\n\n\n// VARIABLES //\n\nvar GENERIC_DTYPE = 'generic';\n\n\n// MAIN //\n\n/**\n* Constructor for applying a unary function to each element in an input array.\n*\n* @constructor\n* @param {Function} fcn - unary function to apply\n* @param {StringArray} idtypes - list of supported input data types\n* @param {StringArray} odtypes - list of supported output data types\n* @param {string} policy - output data type policy\n* @throws {TypeError} first argument must be a function\n* @throws {TypeError} second argument must be an array of supported data types\n* @throws {TypeError} third argument must be an array of supported data types\n* @throws {TypeError} fourth argument must be a supported output data type policy\n* @returns {Unary} instance\n*\n* @example\n* var base = require( '@stdlib/math-base-special-abs' );\n* var dtypes = require( '@stdlib/array-dtypes' );\n*\n* var idt = dtypes( 'real_and_generic' );\n* var odt = idt;\n* var policy = 'same';\n*\n* var abs = new Unary( base, idt, odt, policy );\n*\n* var x = [ -1.0, -2.0, -3.0 ];\n*\n* var y = abs.apply( x );\n* // returns [ 1.0, 2.0, 3.0 ]\n*/\nfunction Unary( fcn, idtypes, odtypes, policy ) {\n\tif ( !( this instanceof Unary ) ) {\n\t\treturn new Unary( fcn, idtypes, odtypes, policy );\n\t}\n\tif ( !isFunction( fcn ) ) {\n\t\tthrow new TypeError( format( 'invalid argument. First argument must be a function. Value: `%s`.', fcn ) );\n\t}\n\tif (\n\t\t!isCollection( idtypes ) ||\n\t\tidtypes.length < 1 ||\n\t\t!everyBy( idtypes, isDataType )\n\t) {\n\t\tthrow new TypeError( format( 'invalid argument. Second argument must be an array of data types. Value: `%s`.', idtypes ) );\n\t}\n\tif (\n\t\t!isCollection( odtypes ) ||\n\t\todtypes.length < 1 ||\n\t\t!everyBy( odtypes, isDataType )\n\t) {\n\t\tthrow new TypeError( format( 'invalid argument. Third argument must be an array of data types. Value: `%s`.', odtypes ) );\n\t}\n\tif ( !isOutputDataTypePolicy( policy ) ) {\n\t\tthrow new TypeError( format( 'invalid argument. Fourth argument must be a supported output data type policy. Value: `%s`.', policy ) );\n\t}\n\tthis._fcn = fcn;\n\tthis._idtypes = copy( idtypes );\n\tthis._odtypes = copy( odtypes );\n\tthis._policy = policy;\n\treturn this;\n}\n\n/**\n* Applies a unary function to each element in a provided input array.\n*\n* @name apply\n* @memberof Unary.prototype\n* @type {Function}\n* @param {Collection} x - input array\n* @param {Options} [options] - function options\n* @param {string} [options.dtype] - output array data type\n* @throws {TypeError} first argument must be a collection\n* @throws {TypeError} first argument must have a supported data type\n* @throws {TypeError} options argument must be an object\n* @throws {TypeError} must provide valid options\n* @returns {Collection} output array\n*\n* @example\n* var base = require( '@stdlib/math-base-special-abs' );\n* var dtypes = require( '@stdlib/array-dtypes' );\n*\n* var idt = dtypes( 'real_and_generic' );\n* var odt = idt;\n* var policy = 'same';\n*\n* var abs = new Unary( base, idt, odt, policy );\n*\n* var x = [ -1.0, -2.0, -3.0 ];\n*\n* var y = abs.apply( x );\n* // returns [ 1.0, 2.0, 3.0 ]\n*/\nsetReadOnly( Unary.prototype, 'apply', function apply( x, options ) {\n\tvar opts;\n\tvar err;\n\tvar out;\n\tvar dt;\n\tif ( !isCollection( x ) ) {\n\t\tthrow new TypeError( format( 'invalid argument. First argument must be an array-like object. Value: `%s`.', x ) );\n\t}\n\tdt = dtype( x ) || GENERIC_DTYPE;\n\tif ( !contains( this._idtypes, dt ) ) {\n\t\tthrow new TypeError( format( 'invalid argument. First argument must have one of the following data types: \"%s\". Data type: `%s`.', this._idtypes.join( '\", \"' ), dt ) );\n\t}\n\topts = {};\n\tif ( arguments.length > 1 ) {\n\t\terr = validate( opts, this._odtypes, options );\n\t\tif ( err ) {\n\t\t\tthrow err;\n\t\t}\n\t}\n\tdt = opts.dtype || odtype( dt, this._policy );\n\tout = empty( x.length, dt );\n\tunary( [ x, out ], [ x.length ], [ 1, 1 ], this._fcn );\n\treturn out;\n});\n\n/**\n* Applies a unary function to each element in a provided input array and assigns results to a provided output array.\n*\n* @name assign\n* @memberof Unary.prototype\n* @type {Function}\n* @param {Collection} x - input array\n* @param {Collection} out - output array\n* @throws {TypeError} first argument must be a collection\n* @throws {TypeError} first argument must have a supported data type\n* @throws {TypeError} second argument must be a collection\n* @returns {Collection} output array\n*\n* @example\n* var base = require( '@stdlib/math-base-special-abs' );\n* var dtypes = require( '@stdlib/array-dtypes' );\n*\n* var idt = dtypes( 'real_and_generic' );\n* var odt = idt;\n* var policy = 'same';\n*\n* var abs = new Unary( base, idt, odt, policy );\n*\n* var x = [ -1.0, -2.0, -3.0 ];\n* var y = [ 0.0, 0.0, 0.0 ];\n*\n* var out = abs.assign( x, y );\n* // returns [ 1.0, 2.0, 3.0 ]\n*\n* var bool = ( out === y );\n* // returns true\n*/\nsetReadOnly( Unary.prototype, 'assign', function assign( x, out ) {\n\tvar dt;\n\tif ( !isCollection( x ) ) {\n\t\tthrow new TypeError( format( 'invalid argument. First argument must be an array-like object. Value: `%s`.', x ) );\n\t}\n\tif ( !isCollection( out ) ) {\n\t\tthrow new TypeError( format( 'invalid argument. Second argument must be an array-like object. Value: `%s`.', out ) );\n\t}\n\t// Validate the input array data type in order to maintain similar behavior to `apply` above...\n\tdt = dtype( x ) || GENERIC_DTYPE;\n\tif ( !contains( this._idtypes, dt ) ) {\n\t\tthrow new TypeError( format( 'invalid argument. First argument must have one of the following data types: \"%s\". Data type: `%s`.', this._idtypes.join( '\", \"' ), dt ) );\n\t}\n\t// Note: we intentionally don't validate the output array dtype because (1) maintains parity with `@stdlib/random/array/tools/unary` and (2) allowing any dtype provides an escape hatch for a user wanting to have an output array having a specific dtype that `apply` does not support\n\n\tunary( [ x, out ], [ x.length ], [ 1, 1 ], this._fcn );\n\treturn out;\n});\n\n\n// EXPORTS //\n\nmodule.exports = Unary;\n", "/**\n* @license Apache-2.0\n*\n* Copyright (c) 2025 The Stdlib Authors.\n*\n* Licensed under the Apache License, Version 2.0 (the \"License\");\n* you may not use this file except in compliance with the License.\n* You may obtain a copy of the License at\n*\n* http://www.apache.org/licenses/LICENSE-2.0\n*\n* Unless required by applicable law or agreed to in writing, software\n* distributed under the License is distributed on an \"AS IS\" BASIS,\n* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.\n* See the License for the specific language governing permissions and\n* limitations under the License.\n*/\n\n'use strict';\n\n/**\n* Constructor for applying a unary function to each element in an input array.\n*\n* @module @stdlib/math-array-tools-unary\n*\n* @example\n* var base = require( '@stdlib/math-base-special-abs' );\n* var dtypes = require( '@stdlib/array-dtypes' );\n* var Unary = require( '@stdlib/math-array-tools-unary' );\n*\n* var idt = dtypes( 'real_and_generic' );\n* var odt = idt;\n* var policy = 'same';\n*\n* var abs = new Unary( base, idt, odt, policy );\n*\n* var x = [ -1.0, -2.0, -3.0 ];\n*\n* var y = abs.apply( x );\n* // returns [ 1.0, 2.0, 3.0 ]\n*/\n\n// MODULES //\n\nvar main = require( './main.js' );\n\n\n// EXPORTS //\n\nmodule.exports = main;\n"],
|
|
5
|
+
"mappings": "uGAAA,IAAAA,EAAAC,EAAA,SAAAC,EAAAC,EAAA,cAsBA,IAAIC,EAAW,QAAS,gCAAiC,EACrDC,EAAa,QAAS,iCAAkC,EACxDC,EAAW,QAAS,oCAAqC,EACzDC,EAAS,QAAS,uBAAwB,EA0B9C,SAASC,EAAUC,EAAMC,EAAQC,EAAU,CAC1C,OAAMP,EAAUO,CAAQ,EAGnBN,EAAYM,EAAS,OAAQ,IACjCF,EAAK,MAAQE,EAAQ,MAChB,CAACL,EAAUI,EAAQD,EAAK,KAAM,GAC3B,IAAI,UAAWF,EAAQ,gFAAiF,QAASG,EAAO,KAAM,MAAO,EAAGD,EAAK,KAAM,CAAE,EAGvJ,KARC,IAAI,UAAWF,EAAQ,qEAAsEI,CAAQ,CAAE,CAShH,CAKAR,EAAO,QAAUK,ICnEjB,IAAAI,EAAAC,EAAA,SAAAC,EAAAC,EAAA,cAwBA,IAAIC,EAAc,QAAS,uDAAwD,EAC/EC,EAAa,QAAS,4BAA6B,EACnDC,EAAe,QAAS,8BAA+B,EACvDC,EAAa,QAAS,wCAAyC,EAC/DC,EAAyB,QAAS,wDAAyD,EAC3FC,EAAW,QAAS,oCAAqC,EACzDC,EAAQ,QAAS,4BAA6B,EAC9CC,EAAS,QAAS,yCAA0C,EAC5DC,EAAQ,QAAS,qBAAsB,EACvCC,EAAQ,QAAS,qBAAsB,EACvCC,EAAU,QAAS,6BAA8B,EACjDC,EAAO,QAAS,yBAA0B,EAC1CC,EAAS,QAAS,uBAAwB,EAC1CC,EAAW,IAKXC,EAAgB,UAkCpB,SAASC,EAAOC,EAAKC,EAASC,EAASC,EAAS,CAC/C,GAAK,EAAG,gBAAgBJ,GACvB,OAAO,IAAIA,EAAOC,EAAKC,EAASC,EAASC,CAAO,EAEjD,GAAK,CAAClB,EAAYe,CAAI,EACrB,MAAM,IAAI,UAAWJ,EAAQ,oEAAqEI,CAAI,CAAE,EAEzG,GACC,CAACd,EAAce,CAAQ,GACvBA,EAAQ,OAAS,GACjB,CAACP,EAASO,EAASd,CAAW,EAE9B,MAAM,IAAI,UAAWS,EAAQ,iFAAkFK,CAAQ,CAAE,EAE1H,GACC,CAACf,EAAcgB,CAAQ,GACvBA,EAAQ,OAAS,GACjB,CAACR,EAASQ,EAASf,CAAW,EAE9B,MAAM,IAAI,UAAWS,EAAQ,gFAAiFM,CAAQ,CAAE,EAEzH,GAAK,CAACd,EAAwBe,CAAO,EACpC,MAAM,IAAI,UAAWP,EAAQ,8FAA+FO,CAAO,CAAE,EAEtI,YAAK,KAAOH,EACZ,KAAK,SAAWL,EAAMM,CAAQ,EAC9B,KAAK,SAAWN,EAAMO,CAAQ,EAC9B,KAAK,QAAUC,EACR,IACR,CAgCAnB,EAAae,EAAM,UAAW,QAAS,SAAgBK,EAAGC,EAAU,CACnE,IAAIC,EACAC,EACAC,EACAC,EACJ,GAAK,CAACvB,EAAckB,CAAE,EACrB,MAAM,IAAI,UAAWR,EAAQ,8EAA+EQ,CAAE,CAAE,EAGjH,GADAK,EAAKhB,EAAOW,CAAE,GAAKN,EACd,CAACT,EAAU,KAAK,SAAUoB,CAAG,EACjC,MAAM,IAAI,UAAWb,EAAQ,qGAAsG,KAAK,SAAS,KAAM,MAAO,EAAGa,CAAG,CAAE,EAGvK,GADAH,EAAO,CAAC,EACH,UAAU,OAAS,IACvBC,EAAMV,EAAUS,EAAM,KAAK,SAAUD,CAAQ,EACxCE,GACJ,MAAMA,EAGR,OAAAE,EAAKH,EAAK,OAASf,EAAQkB,EAAI,KAAK,OAAQ,EAC5CD,EAAMhB,EAAOY,EAAE,OAAQK,CAAG,EAC1BnB,EAAO,CAAEc,EAAGI,CAAI,EAAG,CAAEJ,EAAE,MAAO,EAAG,CAAE,EAAG,CAAE,EAAG,KAAK,IAAK,EAC9CI,CACR,CAAC,EAkCDxB,EAAae,EAAM,UAAW,SAAU,SAAiBK,EAAGI,EAAM,CACjE,IAAIC,EACJ,GAAK,CAACvB,EAAckB,CAAE,EACrB,MAAM,IAAI,UAAWR,EAAQ,8EAA+EQ,CAAE,CAAE,EAEjH,GAAK,CAAClB,EAAcsB,CAAI,EACvB,MAAM,IAAI,UAAWZ,EAAQ,+EAAgFY,CAAI,CAAE,EAIpH,GADAC,EAAKhB,EAAOW,CAAE,GAAKN,EACd,CAACT,EAAU,KAAK,SAAUoB,CAAG,EACjC,MAAM,IAAI,UAAWb,EAAQ,qGAAsG,KAAK,SAAS,KAAM,MAAO,EAAGa,CAAG,CAAE,EAIvK,OAAAnB,EAAO,CAAEc,EAAGI,CAAI,EAAG,CAAEJ,EAAE,MAAO,EAAG,CAAE,EAAG,CAAE,EAAG,KAAK,IAAK,EAC9CI,CACR,CAAC,EAKDzB,EAAO,QAAUgB,IC5KjB,IAAIW,EAAO,IAKX,OAAO,QAAUA",
|
|
6
|
+
"names": ["require_validate", "__commonJSMin", "exports", "module", "isObject", "hasOwnProp", "contains", "format", "validate", "opts", "dtypes", "options", "require_main", "__commonJSMin", "exports", "module", "setReadOnly", "isFunction", "isCollection", "isDataType", "isOutputDataTypePolicy", "contains", "unary", "odtype", "empty", "dtype", "everyBy", "copy", "format", "validate", "GENERIC_DTYPE", "Unary", "fcn", "idtypes", "odtypes", "policy", "x", "options", "opts", "err", "out", "dt", "main"]
|
|
7
|
+
}
|
|
@@ -0,0 +1,223 @@
|
|
|
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
|
+
// TypeScript Version: 4.1
|
|
20
|
+
|
|
21
|
+
/// <reference types="@stdlib/types"/>
|
|
22
|
+
|
|
23
|
+
import { DataType, ArrayLike, AccessorArrayLike } from '@stdlib/types/array';
|
|
24
|
+
import { OutputPolicy } from '@stdlib/types/ndarray';
|
|
25
|
+
|
|
26
|
+
/**
|
|
27
|
+
* Input array.
|
|
28
|
+
*/
|
|
29
|
+
type InputArray<T> = ArrayLike<T> | AccessorArrayLike<T>;
|
|
30
|
+
|
|
31
|
+
/**
|
|
32
|
+
* Output array.
|
|
33
|
+
*/
|
|
34
|
+
type OutputArray<U> = ArrayLike<U> | AccessorArrayLike<U>;
|
|
35
|
+
|
|
36
|
+
/**
|
|
37
|
+
* Interface defining options.
|
|
38
|
+
*/
|
|
39
|
+
interface Options {
|
|
40
|
+
/**
|
|
41
|
+
* Output array data type.
|
|
42
|
+
*/
|
|
43
|
+
dtype?: DataType;
|
|
44
|
+
}
|
|
45
|
+
|
|
46
|
+
/**
|
|
47
|
+
* Unary function.
|
|
48
|
+
*
|
|
49
|
+
* @param value - input value
|
|
50
|
+
* @returns result
|
|
51
|
+
*/
|
|
52
|
+
type Unary<T, U> = ( x: T ) => U;
|
|
53
|
+
|
|
54
|
+
/**
|
|
55
|
+
* Class for applying a unary function to each element in an input array.
|
|
56
|
+
*/
|
|
57
|
+
declare class UnaryFunction<T, U> {
|
|
58
|
+
/**
|
|
59
|
+
* Constructor for applying a unary function to each element in an input array.
|
|
60
|
+
*
|
|
61
|
+
* @param fcn - unary function to apply
|
|
62
|
+
* @param idtypes - list of supported input data types
|
|
63
|
+
* @param odtypes - list of supported output data types
|
|
64
|
+
* @param policy - output data type policy
|
|
65
|
+
* @returns instance
|
|
66
|
+
*
|
|
67
|
+
* @example
|
|
68
|
+
* var base = require( '@stdlib/math-base-special-abs' );
|
|
69
|
+
* var dtypes = require( '@stdlib/array-dtypes' );
|
|
70
|
+
*
|
|
71
|
+
* var idt = dtypes( 'real_and_generic' );
|
|
72
|
+
* var odt = idt;
|
|
73
|
+
* var policy = 'same';
|
|
74
|
+
*
|
|
75
|
+
* var abs = new Unary( base, idt, odt, policy );
|
|
76
|
+
*
|
|
77
|
+
* var x = [ -1.0, -2.0, -3.0 ];
|
|
78
|
+
*
|
|
79
|
+
* var y = abs.apply( x );
|
|
80
|
+
* // returns [ 1.0, 2.0, 3.0 ]
|
|
81
|
+
*/
|
|
82
|
+
constructor( fcn: Unary<T, U>, idtypes: ArrayLike<DataType>, odtypes: ArrayLike<DataType>, policy: OutputPolicy );
|
|
83
|
+
|
|
84
|
+
/**
|
|
85
|
+
* Applies a unary function to each element in a provided input array.
|
|
86
|
+
*
|
|
87
|
+
* @param x - input array
|
|
88
|
+
* @param options - function options
|
|
89
|
+
* @returns output array
|
|
90
|
+
*
|
|
91
|
+
* @example
|
|
92
|
+
* var base = require( '@stdlib/math-base-special-abs' );
|
|
93
|
+
* var dtypes = require( '@stdlib/array-dtypes' );
|
|
94
|
+
*
|
|
95
|
+
* var idt = dtypes( 'real_and_generic' );
|
|
96
|
+
* var odt = idt;
|
|
97
|
+
* var policy = 'same';
|
|
98
|
+
*
|
|
99
|
+
* var abs = new Unary( base, idt, odt, policy );
|
|
100
|
+
*
|
|
101
|
+
* var x = [ -1.0, -2.0, -3.0 ];
|
|
102
|
+
*
|
|
103
|
+
* var y = abs.apply( x );
|
|
104
|
+
* // returns [ 1.0, 2.0, 3.0 ]
|
|
105
|
+
*/
|
|
106
|
+
apply( x: InputArray<T>, options?: Options ): OutputArray<U>; // NOTE: we lose type specificity here, but retaining specificity would likely be difficult and/or tedious to completely enumerate, as the output array data type is dependent on how `x` interacts with output data type policy and whether that policy has been overridden by `options.dtype`. In principle, as well, based on the policy, it is possible to know more exactly which `InputArray` types are actually allowed.
|
|
107
|
+
|
|
108
|
+
/**
|
|
109
|
+
* Applies a unary function to each element in a provided input array and assigns results to a provided output array.
|
|
110
|
+
*
|
|
111
|
+
* @param x - input array
|
|
112
|
+
* @param out - output array
|
|
113
|
+
* @returns output array
|
|
114
|
+
*
|
|
115
|
+
* @example
|
|
116
|
+
* var base = require( '@stdlib/math-base-special-abs' );
|
|
117
|
+
* var dtypes = require( '@stdlib/array-dtypes' );
|
|
118
|
+
*
|
|
119
|
+
* var idt = dtypes( 'real_and_generic' );
|
|
120
|
+
* var odt = idt;
|
|
121
|
+
* var policy = 'same';
|
|
122
|
+
*
|
|
123
|
+
* var abs = new Unary( base, idt, odt, policy );
|
|
124
|
+
*
|
|
125
|
+
* var x = [ -1.0, -2.0, -3.0 ];
|
|
126
|
+
* var y = [ 0.0, 0.0, 0.0 ];
|
|
127
|
+
*
|
|
128
|
+
* var out = abs.assign( x, y );
|
|
129
|
+
* // returns [ 1.0, 2.0, 3.0 ]
|
|
130
|
+
*
|
|
131
|
+
* var bool = ( out === y );
|
|
132
|
+
* // returns true
|
|
133
|
+
*/
|
|
134
|
+
assign<V extends OutputArray<U> = OutputArray<U>>( x: InputArray<T>, out: V ): V;
|
|
135
|
+
}
|
|
136
|
+
|
|
137
|
+
/**
|
|
138
|
+
* Interface defining a constructor which is both "newable" and "callable".
|
|
139
|
+
*/
|
|
140
|
+
interface UnaryFunctionConstructor {
|
|
141
|
+
/**
|
|
142
|
+
* Constructor for applying a unary function to each element in a provided array.
|
|
143
|
+
*
|
|
144
|
+
* @param fcn - unary function to apply
|
|
145
|
+
* @param idtypes - list of supported input data types
|
|
146
|
+
* @param odtypes - list of supported output data types
|
|
147
|
+
* @param policy - output data type policy
|
|
148
|
+
* @returns instance
|
|
149
|
+
*
|
|
150
|
+
* @example
|
|
151
|
+
* var base = require( '@stdlib/math-base-special-abs' );
|
|
152
|
+
* var dtypes = require( '@stdlib/array-dtypes' );
|
|
153
|
+
*
|
|
154
|
+
* var idt = dtypes( 'real_and_generic' );
|
|
155
|
+
* var odt = idt;
|
|
156
|
+
* var policy = 'same';
|
|
157
|
+
*
|
|
158
|
+
* var abs = new Unary( base, idt, odt, policy );
|
|
159
|
+
*
|
|
160
|
+
* var x = [ -1.0, -2.0, -3.0 ];
|
|
161
|
+
*
|
|
162
|
+
* var y = abs.apply( x );
|
|
163
|
+
* // returns [ 1.0, 2.0, 3.0 ]
|
|
164
|
+
*/
|
|
165
|
+
new<T = unknown, U = unknown>( fcn: Unary<T, U>, idtypes: ArrayLike<DataType>, odtypes: ArrayLike<DataType>, policy: OutputPolicy ): UnaryFunction<T, U>;
|
|
166
|
+
|
|
167
|
+
/**
|
|
168
|
+
* Constructor for applying a unary function to each element in a provided array.
|
|
169
|
+
*
|
|
170
|
+
* @param fcn - unary function to apply
|
|
171
|
+
* @param idtypes - list of supported input data types
|
|
172
|
+
* @param odtypes - list of supported output data types
|
|
173
|
+
* @param policy - output data type policy
|
|
174
|
+
* @returns instance
|
|
175
|
+
*
|
|
176
|
+
* @example
|
|
177
|
+
* var base = require( '@stdlib/math-base-special-abs' );
|
|
178
|
+
* var dtypes = require( '@stdlib/array-dtypes' );
|
|
179
|
+
*
|
|
180
|
+
* var idt = dtypes( 'real_and_generic' );
|
|
181
|
+
* var odt = idt;
|
|
182
|
+
* var policy = 'same';
|
|
183
|
+
*
|
|
184
|
+
* var abs = new Unary( base, idt, odt, policy );
|
|
185
|
+
*
|
|
186
|
+
* var x = [ -1.0, -2.0, -3.0 ];
|
|
187
|
+
*
|
|
188
|
+
* var y = abs.apply( x );
|
|
189
|
+
* // returns [ 1.0, 2.0, 3.0 ]
|
|
190
|
+
*/
|
|
191
|
+
<T = unknown, U = unknown>( fcn: Unary<T, U>, idtypes: ArrayLike<DataType>, odtypes: ArrayLike<DataType>, policy: OutputPolicy ): UnaryFunction<T, U>;
|
|
192
|
+
}
|
|
193
|
+
|
|
194
|
+
/**
|
|
195
|
+
* Constructor for applying a unary function to each element in a provided array.
|
|
196
|
+
*
|
|
197
|
+
* @param fcn - unary function to apply
|
|
198
|
+
* @param idtypes - list of supported input data types
|
|
199
|
+
* @param odtypes - list of supported output data types
|
|
200
|
+
* @param policy - output data type policy
|
|
201
|
+
* @returns instance
|
|
202
|
+
*
|
|
203
|
+
* @example
|
|
204
|
+
* var base = require( '@stdlib/math-base-special-abs' );
|
|
205
|
+
* var dtypes = require( '@stdlib/array-dtypes' );
|
|
206
|
+
*
|
|
207
|
+
* var idt = dtypes( 'real_and_generic' );
|
|
208
|
+
* var odt = idt;
|
|
209
|
+
* var policy = 'same';
|
|
210
|
+
*
|
|
211
|
+
* var abs = new Unary( base, idt, odt, policy );
|
|
212
|
+
*
|
|
213
|
+
* var x = [ -1.0, -2.0, -3.0 ];
|
|
214
|
+
*
|
|
215
|
+
* var y = abs.apply( x );
|
|
216
|
+
* // returns [ 1.0, 2.0, 3.0 ]
|
|
217
|
+
*/
|
|
218
|
+
declare var ctor: UnaryFunctionConstructor;
|
|
219
|
+
|
|
220
|
+
|
|
221
|
+
// EXPORTS //
|
|
222
|
+
|
|
223
|
+
export = ctor;
|
package/lib/index.js
ADDED
|
@@ -0,0 +1,50 @@
|
|
|
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
|
+
/**
|
|
22
|
+
* Constructor for applying a unary function to each element in an input array.
|
|
23
|
+
*
|
|
24
|
+
* @module @stdlib/math-array-tools-unary
|
|
25
|
+
*
|
|
26
|
+
* @example
|
|
27
|
+
* var base = require( '@stdlib/math-base-special-abs' );
|
|
28
|
+
* var dtypes = require( '@stdlib/array-dtypes' );
|
|
29
|
+
* var Unary = require( '@stdlib/math-array-tools-unary' );
|
|
30
|
+
*
|
|
31
|
+
* var idt = dtypes( 'real_and_generic' );
|
|
32
|
+
* var odt = idt;
|
|
33
|
+
* var policy = 'same';
|
|
34
|
+
*
|
|
35
|
+
* var abs = new Unary( base, idt, odt, policy );
|
|
36
|
+
*
|
|
37
|
+
* var x = [ -1.0, -2.0, -3.0 ];
|
|
38
|
+
*
|
|
39
|
+
* var y = abs.apply( x );
|
|
40
|
+
* // returns [ 1.0, 2.0, 3.0 ]
|
|
41
|
+
*/
|
|
42
|
+
|
|
43
|
+
// MODULES //
|
|
44
|
+
|
|
45
|
+
var main = require( './main.js' );
|
|
46
|
+
|
|
47
|
+
|
|
48
|
+
// EXPORTS //
|
|
49
|
+
|
|
50
|
+
module.exports = main;
|
package/lib/main.js
ADDED
|
@@ -0,0 +1,217 @@
|
|
|
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
|
+
/* eslint-disable no-restricted-syntax, no-invalid-this */
|
|
20
|
+
|
|
21
|
+
'use strict';
|
|
22
|
+
|
|
23
|
+
// MODULES //
|
|
24
|
+
|
|
25
|
+
var setReadOnly = require( '@stdlib/utils-define-nonenumerable-read-only-property' );
|
|
26
|
+
var isFunction = require( '@stdlib/assert-is-function' );
|
|
27
|
+
var isCollection = require( '@stdlib/assert-is-collection' );
|
|
28
|
+
var isDataType = require( '@stdlib/array-base-assert-is-data-type' );
|
|
29
|
+
var isOutputDataTypePolicy = require( '@stdlib/ndarray-base-assert-is-output-data-type-policy' );
|
|
30
|
+
var contains = require( '@stdlib/array-base-assert-contains' );
|
|
31
|
+
var unary = require( '@stdlib/strided-base-unary' );
|
|
32
|
+
var odtype = require( '@stdlib/ndarray-base-unary-output-dtype' );
|
|
33
|
+
var empty = require( '@stdlib/array-empty' );
|
|
34
|
+
var dtype = require( '@stdlib/array-dtype' );
|
|
35
|
+
var everyBy = require( '@stdlib/array-base-every-by' );
|
|
36
|
+
var copy = require( '@stdlib/array-base-copy' );
|
|
37
|
+
var format = require( '@stdlib/string-format' );
|
|
38
|
+
var validate = require( './validate.js' );
|
|
39
|
+
|
|
40
|
+
|
|
41
|
+
// VARIABLES //
|
|
42
|
+
|
|
43
|
+
var GENERIC_DTYPE = 'generic';
|
|
44
|
+
|
|
45
|
+
|
|
46
|
+
// MAIN //
|
|
47
|
+
|
|
48
|
+
/**
|
|
49
|
+
* Constructor for applying a unary function to each element in an input array.
|
|
50
|
+
*
|
|
51
|
+
* @constructor
|
|
52
|
+
* @param {Function} fcn - unary function to apply
|
|
53
|
+
* @param {StringArray} idtypes - list of supported input data types
|
|
54
|
+
* @param {StringArray} odtypes - list of supported output data types
|
|
55
|
+
* @param {string} policy - output data type policy
|
|
56
|
+
* @throws {TypeError} first argument must be a function
|
|
57
|
+
* @throws {TypeError} second argument must be an array of supported data types
|
|
58
|
+
* @throws {TypeError} third argument must be an array of supported data types
|
|
59
|
+
* @throws {TypeError} fourth argument must be a supported output data type policy
|
|
60
|
+
* @returns {Unary} instance
|
|
61
|
+
*
|
|
62
|
+
* @example
|
|
63
|
+
* var base = require( '@stdlib/math-base-special-abs' );
|
|
64
|
+
* var dtypes = require( '@stdlib/array-dtypes' );
|
|
65
|
+
*
|
|
66
|
+
* var idt = dtypes( 'real_and_generic' );
|
|
67
|
+
* var odt = idt;
|
|
68
|
+
* var policy = 'same';
|
|
69
|
+
*
|
|
70
|
+
* var abs = new Unary( base, idt, odt, policy );
|
|
71
|
+
*
|
|
72
|
+
* var x = [ -1.0, -2.0, -3.0 ];
|
|
73
|
+
*
|
|
74
|
+
* var y = abs.apply( x );
|
|
75
|
+
* // returns [ 1.0, 2.0, 3.0 ]
|
|
76
|
+
*/
|
|
77
|
+
function Unary( fcn, idtypes, odtypes, policy ) {
|
|
78
|
+
if ( !( this instanceof Unary ) ) {
|
|
79
|
+
return new Unary( fcn, idtypes, odtypes, policy );
|
|
80
|
+
}
|
|
81
|
+
if ( !isFunction( fcn ) ) {
|
|
82
|
+
throw new TypeError( format( 'invalid argument. First argument must be a function. Value: `%s`.', fcn ) );
|
|
83
|
+
}
|
|
84
|
+
if (
|
|
85
|
+
!isCollection( idtypes ) ||
|
|
86
|
+
idtypes.length < 1 ||
|
|
87
|
+
!everyBy( idtypes, isDataType )
|
|
88
|
+
) {
|
|
89
|
+
throw new TypeError( format( 'invalid argument. Second argument must be an array of data types. Value: `%s`.', idtypes ) );
|
|
90
|
+
}
|
|
91
|
+
if (
|
|
92
|
+
!isCollection( odtypes ) ||
|
|
93
|
+
odtypes.length < 1 ||
|
|
94
|
+
!everyBy( odtypes, isDataType )
|
|
95
|
+
) {
|
|
96
|
+
throw new TypeError( format( 'invalid argument. Third argument must be an array of data types. Value: `%s`.', odtypes ) );
|
|
97
|
+
}
|
|
98
|
+
if ( !isOutputDataTypePolicy( policy ) ) {
|
|
99
|
+
throw new TypeError( format( 'invalid argument. Fourth argument must be a supported output data type policy. Value: `%s`.', policy ) );
|
|
100
|
+
}
|
|
101
|
+
this._fcn = fcn;
|
|
102
|
+
this._idtypes = copy( idtypes );
|
|
103
|
+
this._odtypes = copy( odtypes );
|
|
104
|
+
this._policy = policy;
|
|
105
|
+
return this;
|
|
106
|
+
}
|
|
107
|
+
|
|
108
|
+
/**
|
|
109
|
+
* Applies a unary function to each element in a provided input array.
|
|
110
|
+
*
|
|
111
|
+
* @name apply
|
|
112
|
+
* @memberof Unary.prototype
|
|
113
|
+
* @type {Function}
|
|
114
|
+
* @param {Collection} x - input array
|
|
115
|
+
* @param {Options} [options] - function options
|
|
116
|
+
* @param {string} [options.dtype] - output array data type
|
|
117
|
+
* @throws {TypeError} first argument must be a collection
|
|
118
|
+
* @throws {TypeError} first argument must have a supported data type
|
|
119
|
+
* @throws {TypeError} options argument must be an object
|
|
120
|
+
* @throws {TypeError} must provide valid options
|
|
121
|
+
* @returns {Collection} output array
|
|
122
|
+
*
|
|
123
|
+
* @example
|
|
124
|
+
* var base = require( '@stdlib/math-base-special-abs' );
|
|
125
|
+
* var dtypes = require( '@stdlib/array-dtypes' );
|
|
126
|
+
*
|
|
127
|
+
* var idt = dtypes( 'real_and_generic' );
|
|
128
|
+
* var odt = idt;
|
|
129
|
+
* var policy = 'same';
|
|
130
|
+
*
|
|
131
|
+
* var abs = new Unary( base, idt, odt, policy );
|
|
132
|
+
*
|
|
133
|
+
* var x = [ -1.0, -2.0, -3.0 ];
|
|
134
|
+
*
|
|
135
|
+
* var y = abs.apply( x );
|
|
136
|
+
* // returns [ 1.0, 2.0, 3.0 ]
|
|
137
|
+
*/
|
|
138
|
+
setReadOnly( Unary.prototype, 'apply', function apply( x, options ) {
|
|
139
|
+
var opts;
|
|
140
|
+
var err;
|
|
141
|
+
var out;
|
|
142
|
+
var dt;
|
|
143
|
+
if ( !isCollection( x ) ) {
|
|
144
|
+
throw new TypeError( format( 'invalid argument. First argument must be an array-like object. Value: `%s`.', x ) );
|
|
145
|
+
}
|
|
146
|
+
dt = dtype( x ) || GENERIC_DTYPE;
|
|
147
|
+
if ( !contains( this._idtypes, dt ) ) {
|
|
148
|
+
throw new TypeError( format( 'invalid argument. First argument must have one of the following data types: "%s". Data type: `%s`.', this._idtypes.join( '", "' ), dt ) );
|
|
149
|
+
}
|
|
150
|
+
opts = {};
|
|
151
|
+
if ( arguments.length > 1 ) {
|
|
152
|
+
err = validate( opts, this._odtypes, options );
|
|
153
|
+
if ( err ) {
|
|
154
|
+
throw err;
|
|
155
|
+
}
|
|
156
|
+
}
|
|
157
|
+
dt = opts.dtype || odtype( dt, this._policy );
|
|
158
|
+
out = empty( x.length, dt );
|
|
159
|
+
unary( [ x, out ], [ x.length ], [ 1, 1 ], this._fcn );
|
|
160
|
+
return out;
|
|
161
|
+
});
|
|
162
|
+
|
|
163
|
+
/**
|
|
164
|
+
* Applies a unary function to each element in a provided input array and assigns results to a provided output array.
|
|
165
|
+
*
|
|
166
|
+
* @name assign
|
|
167
|
+
* @memberof Unary.prototype
|
|
168
|
+
* @type {Function}
|
|
169
|
+
* @param {Collection} x - input array
|
|
170
|
+
* @param {Collection} out - output array
|
|
171
|
+
* @throws {TypeError} first argument must be a collection
|
|
172
|
+
* @throws {TypeError} first argument must have a supported data type
|
|
173
|
+
* @throws {TypeError} second argument must be a collection
|
|
174
|
+
* @returns {Collection} output array
|
|
175
|
+
*
|
|
176
|
+
* @example
|
|
177
|
+
* var base = require( '@stdlib/math-base-special-abs' );
|
|
178
|
+
* var dtypes = require( '@stdlib/array-dtypes' );
|
|
179
|
+
*
|
|
180
|
+
* var idt = dtypes( 'real_and_generic' );
|
|
181
|
+
* var odt = idt;
|
|
182
|
+
* var policy = 'same';
|
|
183
|
+
*
|
|
184
|
+
* var abs = new Unary( base, idt, odt, policy );
|
|
185
|
+
*
|
|
186
|
+
* var x = [ -1.0, -2.0, -3.0 ];
|
|
187
|
+
* var y = [ 0.0, 0.0, 0.0 ];
|
|
188
|
+
*
|
|
189
|
+
* var out = abs.assign( x, y );
|
|
190
|
+
* // returns [ 1.0, 2.0, 3.0 ]
|
|
191
|
+
*
|
|
192
|
+
* var bool = ( out === y );
|
|
193
|
+
* // returns true
|
|
194
|
+
*/
|
|
195
|
+
setReadOnly( Unary.prototype, 'assign', function assign( x, out ) {
|
|
196
|
+
var dt;
|
|
197
|
+
if ( !isCollection( x ) ) {
|
|
198
|
+
throw new TypeError( format( 'invalid argument. First argument must be an array-like object. Value: `%s`.', x ) );
|
|
199
|
+
}
|
|
200
|
+
if ( !isCollection( out ) ) {
|
|
201
|
+
throw new TypeError( format( 'invalid argument. Second argument must be an array-like object. Value: `%s`.', out ) );
|
|
202
|
+
}
|
|
203
|
+
// Validate the input array data type in order to maintain similar behavior to `apply` above...
|
|
204
|
+
dt = dtype( x ) || GENERIC_DTYPE;
|
|
205
|
+
if ( !contains( this._idtypes, dt ) ) {
|
|
206
|
+
throw new TypeError( format( 'invalid argument. First argument must have one of the following data types: "%s". Data type: `%s`.', this._idtypes.join( '", "' ), dt ) );
|
|
207
|
+
}
|
|
208
|
+
// Note: we intentionally don't validate the output array dtype because (1) maintains parity with `@stdlib/random/array/tools/unary` and (2) allowing any dtype provides an escape hatch for a user wanting to have an output array having a specific dtype that `apply` does not support
|
|
209
|
+
|
|
210
|
+
unary( [ x, out ], [ x.length ], [ 1, 1 ], this._fcn );
|
|
211
|
+
return out;
|
|
212
|
+
});
|
|
213
|
+
|
|
214
|
+
|
|
215
|
+
// EXPORTS //
|
|
216
|
+
|
|
217
|
+
module.exports = Unary;
|
package/lib/validate.js
ADDED
|
@@ -0,0 +1,68 @@
|
|
|
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 isObject = require( '@stdlib/assert-is-plain-object' );
|
|
24
|
+
var hasOwnProp = require( '@stdlib/assert-has-own-property' );
|
|
25
|
+
var contains = require( '@stdlib/array-base-assert-contains' );
|
|
26
|
+
var format = require( '@stdlib/string-format' );
|
|
27
|
+
|
|
28
|
+
|
|
29
|
+
// MAIN //
|
|
30
|
+
|
|
31
|
+
/**
|
|
32
|
+
* Validates function options.
|
|
33
|
+
*
|
|
34
|
+
* @private
|
|
35
|
+
* @param {Object} opts - destination object
|
|
36
|
+
* @param {Array} dtypes - list of supported output data types
|
|
37
|
+
* @param {Options} options - function options
|
|
38
|
+
* @param {string} [options.dtype] - output array data type
|
|
39
|
+
* @returns {(Error|null)} null or an error object
|
|
40
|
+
*
|
|
41
|
+
* @example
|
|
42
|
+
* var opts = {};
|
|
43
|
+
* var dtypes = [ 'float64', 'float32', 'generic' ];
|
|
44
|
+
* var options = {
|
|
45
|
+
* 'dtype': 'float64'
|
|
46
|
+
* };
|
|
47
|
+
* var err = validate( opts, dtypes, options );
|
|
48
|
+
* if ( err ) {
|
|
49
|
+
* throw err;
|
|
50
|
+
* }
|
|
51
|
+
*/
|
|
52
|
+
function validate( opts, dtypes, options ) {
|
|
53
|
+
if ( !isObject( options ) ) {
|
|
54
|
+
return new TypeError( format( 'invalid argument. Options argument must be an object. Value: `%s`.', options ) );
|
|
55
|
+
}
|
|
56
|
+
if ( hasOwnProp( options, 'dtype' ) ) {
|
|
57
|
+
opts.dtype = options.dtype;
|
|
58
|
+
if ( !contains( dtypes, opts.dtype ) ) {
|
|
59
|
+
return new TypeError( format( 'invalid option. `%s` option must be one of the following: "%s". Option: `%s`.', 'dtype', dtypes.join( '", "' ), opts.dtype ) );
|
|
60
|
+
}
|
|
61
|
+
}
|
|
62
|
+
return null;
|
|
63
|
+
}
|
|
64
|
+
|
|
65
|
+
|
|
66
|
+
// EXPORTS //
|
|
67
|
+
|
|
68
|
+
module.exports = validate;
|
package/package.json
ADDED
|
@@ -0,0 +1,84 @@
|
|
|
1
|
+
{
|
|
2
|
+
"name": "@stdlib/math-array-tools-unary",
|
|
3
|
+
"version": "0.1.0",
|
|
4
|
+
"description": "Constructor for applying a unary function to each element in an input array.",
|
|
5
|
+
"license": "Apache-2.0",
|
|
6
|
+
"author": {
|
|
7
|
+
"name": "The Stdlib Authors",
|
|
8
|
+
"url": "https://github.com/stdlib-js/stdlib/graphs/contributors"
|
|
9
|
+
},
|
|
10
|
+
"contributors": [
|
|
11
|
+
{
|
|
12
|
+
"name": "The Stdlib Authors",
|
|
13
|
+
"url": "https://github.com/stdlib-js/stdlib/graphs/contributors"
|
|
14
|
+
}
|
|
15
|
+
],
|
|
16
|
+
"main": "./lib",
|
|
17
|
+
"directories": {
|
|
18
|
+
"doc": "./docs",
|
|
19
|
+
"lib": "./lib",
|
|
20
|
+
"dist": "./dist"
|
|
21
|
+
},
|
|
22
|
+
"types": "./docs/types",
|
|
23
|
+
"scripts": {},
|
|
24
|
+
"homepage": "https://stdlib.io",
|
|
25
|
+
"repository": {
|
|
26
|
+
"type": "git",
|
|
27
|
+
"url": "git://github.com/stdlib-js/math-array-tools-unary.git"
|
|
28
|
+
},
|
|
29
|
+
"bugs": {
|
|
30
|
+
"url": "https://github.com/stdlib-js/stdlib/issues"
|
|
31
|
+
},
|
|
32
|
+
"dependencies": {
|
|
33
|
+
"@stdlib/array-base-assert-contains": "^0.2.2",
|
|
34
|
+
"@stdlib/array-base-assert-is-data-type": "^0.2.1",
|
|
35
|
+
"@stdlib/array-base-copy": "^0.2.2",
|
|
36
|
+
"@stdlib/array-base-every-by": "^0.2.2",
|
|
37
|
+
"@stdlib/array-dtype": "^0.3.0",
|
|
38
|
+
"@stdlib/array-empty": "^0.3.0",
|
|
39
|
+
"@stdlib/assert-has-own-property": "^0.2.2",
|
|
40
|
+
"@stdlib/assert-is-collection": "^0.2.2",
|
|
41
|
+
"@stdlib/assert-is-function": "^0.2.2",
|
|
42
|
+
"@stdlib/assert-is-plain-object": "^0.2.2",
|
|
43
|
+
"@stdlib/ndarray-base-assert-is-output-data-type-policy": "^0.1.0",
|
|
44
|
+
"@stdlib/ndarray-base-unary-output-dtype": "^0.3.0",
|
|
45
|
+
"@stdlib/strided-base-unary": "^0.3.0",
|
|
46
|
+
"@stdlib/string-format": "^0.2.2",
|
|
47
|
+
"@stdlib/utils-define-nonenumerable-read-only-property": "^0.2.2",
|
|
48
|
+
"@stdlib/error-tools-fmtprodmsg": "^0.2.2"
|
|
49
|
+
},
|
|
50
|
+
"devDependencies": {},
|
|
51
|
+
"engines": {
|
|
52
|
+
"node": ">=0.10.0",
|
|
53
|
+
"npm": ">2.7.0"
|
|
54
|
+
},
|
|
55
|
+
"os": [
|
|
56
|
+
"aix",
|
|
57
|
+
"darwin",
|
|
58
|
+
"freebsd",
|
|
59
|
+
"linux",
|
|
60
|
+
"macos",
|
|
61
|
+
"openbsd",
|
|
62
|
+
"sunos",
|
|
63
|
+
"win32",
|
|
64
|
+
"windows"
|
|
65
|
+
],
|
|
66
|
+
"keywords": [
|
|
67
|
+
"stdlib",
|
|
68
|
+
"stdmath",
|
|
69
|
+
"mathematics",
|
|
70
|
+
"math",
|
|
71
|
+
"tools",
|
|
72
|
+
"array",
|
|
73
|
+
"vector",
|
|
74
|
+
"element-wise",
|
|
75
|
+
"vectorized",
|
|
76
|
+
"vectorization",
|
|
77
|
+
"apply",
|
|
78
|
+
"call"
|
|
79
|
+
],
|
|
80
|
+
"funding": {
|
|
81
|
+
"type": "opencollective",
|
|
82
|
+
"url": "https://opencollective.com/stdlib"
|
|
83
|
+
}
|
|
84
|
+
}
|