Skip to content

Commit 6788899

Browse files
committed
feat: add math/array/special/abs
--- 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 07f3085 commit 6788899

File tree

11 files changed

+1163
-0
lines changed

11 files changed

+1163
-0
lines changed
Lines changed: 135 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,135 @@
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+
# abs
22+
23+
> Compute the [absolute value][@stdlib/math/base/special/abs] for each element in an input array.
24+
25+
<section class="usage">
26+
27+
## Usage
28+
29+
```javascript
30+
var abs = require( '@stdlib/math/array/special/abs' );
31+
```
32+
33+
#### abs( x\[, options] )
34+
35+
Computes the [absolute value][@stdlib/math/base/special/abs] for each element in an input array.
36+
37+
```javascript
38+
var v = abs( [ -1.0, 2.0, -3.0, 4.0 ] );
39+
// returns [ 1.0, 2.0, 3.0, 4.0 ]
40+
```
41+
42+
The function has the following parameters:
43+
44+
- **x**: input array.
45+
- **options**: function options.
46+
47+
The function accepts the following options:
48+
49+
- **dtype**: output array data type.
50+
51+
To specify the output array data type, set the `dtype` option.
52+
53+
```javascript
54+
var v = abs( [ -1.0, 2.0, -3.0, 4.0 ], {
55+
'dtype': 'float64'
56+
});
57+
// returns <Float64Array>[ 1.0, 2.0, 3.0, 4.0 ]
58+
```
59+
60+
#### abs.assign( x, out )
61+
62+
Computes the [absolute value][@stdlib/math/base/special/abs] for each element in an input array and assigns results to a provided output array.
63+
64+
```javascript
65+
var zeros = require( '@stdlib/array/zeros' );
66+
67+
var out = zeros( 4, 'float64' );
68+
// returns <Float64Array>[ 0.0, 0.0, 0.0, 0.0 ]
69+
70+
var v = abs.assign( [ -1.0, 2.0, -3.0, 4.0 ], out );
71+
// returns <Float64Array>[ 1.0, 2.0, 3.0, 4.0 ]
72+
73+
var bool = ( v === out );
74+
// returns true
75+
```
76+
77+
The method has the following parameters:
78+
79+
- **x**: input array.
80+
- **out**: output array.
81+
82+
</section>
83+
84+
<!-- /.usage -->
85+
86+
<section class="notes">
87+
88+
</section>
89+
90+
<!-- /.notes -->
91+
92+
<section class="examples">
93+
94+
## Examples
95+
96+
<!-- eslint no-undef: "error" -->
97+
98+
```javascript
99+
var uniform = require( '@stdlib/random/array/uniform' );
100+
var logEach = require( '@stdlib/console/log-each' );
101+
var abs = require( '@stdlib/math/array/special/abs' );
102+
103+
// Generate an array of random numbers:
104+
var x = uniform( 10, -1.0, 1.0, {
105+
'dtype': 'generic'
106+
});
107+
108+
// Perform element-wise computation:
109+
var y = abs( x );
110+
111+
// Print the results:
112+
logEach( 'abs(%f) = %f', x, y );
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+
[@stdlib/math/base/special/abs]: https://github.com/stdlib-js/stdlib/tree/develop/lib/node_modules/%40stdlib/math/base/special/abs
132+
133+
</section>
134+
135+
<!-- /.links -->
Lines changed: 107 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,107 @@
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 uniform = require( '@stdlib/random/array/uniform' );
27+
var zeros = require( '@stdlib/array/zeros' );
28+
var pkg = require( './../package.json' ).name;
29+
var abs = require( './../lib' );
30+
31+
32+
// FUNCTIONS //
33+
34+
/**
35+
* Creates a benchmark function.
36+
*
37+
* @private
38+
* @param {PositiveInteger} len - array length
39+
* @returns {Function} benchmark function
40+
*/
41+
function createBenchmark( len ) {
42+
var x = [
43+
uniform( len, -50.0, 50.0, {
44+
'dtype': 'float64'
45+
}),
46+
uniform( len, -50.0, 50.0, {
47+
'dtype': 'float64'
48+
})
49+
];
50+
51+
return benchmark;
52+
53+
/**
54+
* Benchmark function.
55+
*
56+
* @private
57+
* @param {Benchmark} b - benchmark instance
58+
*/
59+
function benchmark( b ) {
60+
var out;
61+
var o;
62+
var i;
63+
64+
out = zeros( len, 'float64' );
65+
66+
b.tic();
67+
for ( i = 0; i < b.iterations; i++ ) {
68+
o = abs.assign( x[ i%x.length ], out );
69+
if ( isnan( o[ i%len ] ) ) {
70+
b.fail( 'should not return NaN' );
71+
}
72+
}
73+
b.toc();
74+
if ( isnan( o[ i%len ] ) ) {
75+
b.fail( 'should not return NaN' );
76+
}
77+
b.pass( 'benchmark finished' );
78+
b.end();
79+
}
80+
}
81+
82+
83+
// MAIN //
84+
85+
/**
86+
* Main execution sequence.
87+
*
88+
* @private
89+
*/
90+
function main() {
91+
var len;
92+
var min;
93+
var max;
94+
var f;
95+
var i;
96+
97+
min = 1; // 10^min
98+
max = 6; // 10^max
99+
100+
for ( i = min; i <= max; i++ ) {
101+
len = pow( 10, i );
102+
f = createBenchmark( len );
103+
bench( pkg+':assign:len='+len, f );
104+
}
105+
}
106+
107+
main();
Lines changed: 102 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,102 @@
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 uniform = require( '@stdlib/random/array/uniform' );
27+
var pkg = require( './../package.json' ).name;
28+
var abs = require( './../lib' );
29+
30+
31+
// FUNCTIONS //
32+
33+
/**
34+
* Creates a benchmark function.
35+
*
36+
* @private
37+
* @param {PositiveInteger} len - array length
38+
* @returns {Function} benchmark function
39+
*/
40+
function createBenchmark( len ) {
41+
var x = [
42+
uniform( len, -50.0, 50.0, {
43+
'dtype': 'float64'
44+
}),
45+
uniform( len, -50.0, 50.0, {
46+
'dtype': 'float64'
47+
})
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 o;
59+
var i;
60+
61+
b.tic();
62+
for ( i = 0; i < b.iterations; i++ ) {
63+
o = abs( x[ i%x.length ] );
64+
if ( isnan( o[ i%len ] ) ) {
65+
b.fail( 'should not return NaN' );
66+
}
67+
}
68+
b.toc();
69+
if ( isnan( o[ i%len ] ) ) {
70+
b.fail( 'should not return NaN' );
71+
}
72+
b.pass( 'benchmark finished' );
73+
b.end();
74+
}
75+
}
76+
77+
78+
// MAIN //
79+
80+
/**
81+
* Main execution sequence.
82+
*
83+
* @private
84+
*/
85+
function main() {
86+
var len;
87+
var min;
88+
var max;
89+
var f;
90+
var i;
91+
92+
min = 1; // 10^min
93+
max = 6; // 10^max
94+
95+
for ( i = min; i <= max; i++ ) {
96+
len = pow( 10, i );
97+
f = createBenchmark( len );
98+
bench( pkg+':len='+len, f );
99+
}
100+
}
101+
102+
main();

0 commit comments

Comments
 (0)