Skip to content

Commit a2e3f27

Browse files
committed
refactor!: remove support for non-ndarray arguments
This commit aligns the API with reductions (e.g., `stats/max`) where top-level APIs only accept ndarrays. BREAKING CHANGE: remove support for non-ndarray arguments To migrate, users should wrap non-ndarray arguments using `@stdlib/ndarray/array` or `@stdlib/ndarray/from-scalar` which will convert non-ndarray arguments to ndarrays. In general, if a user is providing a number, a user is likely to want a number in return. Similarly for built-in array types. So always returning an ndarray is likely not what a user wants. This change essentially requires users to be explicit about what implementation and behavior they desire. If a user still wants to retain previous behavior, they can write a thin wrapper a top the new API which performs delegation based on whether an input argument is a scalar, array, or an ndarray. --- 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: passed - 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 1b46342 commit a2e3f27

File tree

62 files changed

+1432
-4570
lines changed

Some content is hidden

Large Commits have some content hidden by default. Use the searchbox below for content that may be hidden.

62 files changed

+1432
-4570
lines changed

lib/node_modules/@stdlib/math/special/abs/README.md

Lines changed: 87 additions & 86 deletions
Original file line numberDiff line numberDiff line change
@@ -20,27 +20,12 @@ limitations under the License.
2020

2121
# abs
2222

23-
> Compute the [absolute value][absolute-value].
23+
> Compute the [absolute value][@stdlib/math/base/special/abs] for each element in an [ndarray][@stdlib/ndarray/ctor].
2424
2525
<!-- Section to include introductory text. Make sure to keep an empty line after the intro `section` element and another before the `/section` close. -->
2626

2727
<section class="intro">
2828

29-
The [absolute value][absolute-value] is defined as
30-
31-
<!-- <equation class="equation" label="eq:absolute_value" align="center" raw="|x| = \begin{cases} x & \textrm{if}\ x \geq 0 \\ -x & \textrm{if}\ x < 0\end{cases}" alt="Absolute value"> -->
32-
33-
```math
34-
|x| = \begin{cases} x & \textrm{if}\ x \geq 0 \\ -x & \textrm{if}\ x < 0\end{cases}
35-
```
36-
37-
<!-- <div class="equation" align="center" data-raw-text="|x| = \begin{cases} x &amp; \textrm{if}\ x \geq 0 \\ -x &amp; \textrm{if}\ x &lt; 0\end{cases}" data-equation="eq:absolute_value">
38-
<img src="https://cdn.jsdelivr.net/gh/stdlib-js/stdlib@61c5f878886bd5b3b98976501c974bf69f575238/lib/node_modules/@stdlib/math/special/abs/docs/img/equation_absolute_value.svg" alt="Absolute value">
39-
<br>
40-
</div> -->
41-
42-
<!-- </equation> -->
43-
4429
</section>
4530

4631
<!-- /.intro -->
@@ -57,88 +42,115 @@ var abs = require( '@stdlib/math/special/abs' );
5742

5843
#### abs( x\[, options] )
5944

60-
Computes the [absolute value][absolute-value].
45+
Computes the [absolute value][@stdlib/math/base/special/abs] for each element in an [ndarray][@stdlib/ndarray/ctor].
6146

6247
```javascript
63-
var y = abs( -1.0 );
64-
// returns 1.0
48+
var ndarray2array = require( '@stdlib/ndarray/to-array' );
49+
var array = require( '@stdlib/ndarray/array' );
50+
51+
var x = array( [ [ -1.0, -2.0 ], [ -3.0, -4.0 ] ] );
52+
var y = abs( x );
53+
// returns <ndarray>
54+
55+
var arr = ndarray2array( y );
56+
// returns [ [ 1.0, 2.0 ], [ 3.0, 4.0 ] ]
6557
```
6658

6759
The function accepts the following arguments:
6860

69-
- **x**: input [`ndarray`][@stdlib/ndarray/ctor], array-like object, or number. If provided an [`ndarray`][@stdlib/ndarray/ctor] or array-like object, the function performs element-wise computation.
70-
- **options**: function options.
61+
- **x**: input [ndarray][@stdlib/ndarray/ctor].
62+
- **options**: function options (_optional_).
63+
64+
The function accepts the following options:
7165

72-
If provided an [`ndarray`][@stdlib/ndarray/ctor], the function returns an [`ndarray`][@stdlib/ndarray/ctor] having the same shape and data type as `x`.
66+
- **dtype**: output ndarray [data type][@stdlib/ndarray/dtypes]. Must be a real-valued or generic [data type][@stdlib/ndarray/dtypes].
67+
- **order**: output ndarray [order][@stdlib/ndarray/orders] (i.e., memory layout).
68+
69+
By default, the function returns an [ndarray][@stdlib/ndarray/ctor] having a [data type][@stdlib/ndarray/dtypes] determined by the function's output data type [policy][@stdlib/ndarray/output-dtype-policies]. To override the default behavior, set the `dtype` option.
7370

7471
```javascript
72+
var ndarray2array = require( '@stdlib/ndarray/to-array' );
7573
var array = require( '@stdlib/ndarray/array' );
74+
var getDType = require( '@stdlib/ndarray/dtype' );
7675

77-
var x = array( [ [ -1.0, -2.0 ], [ -3.0, -4.0 ] ] ); // 2x2
78-
var y = abs( x );
76+
var x = array( [ [ -1.0, -2.0 ], [ -3.0, -4.0 ] ] );
77+
var y = abs( x, {
78+
'dtype': 'generic'
79+
});
7980
// returns <ndarray>
8081

81-
var v = y.get( 0, 1 );
82-
// returns 2.0
83-
```
84-
85-
If provided an array-like object, the function returns an array-like object having the same length and data type as `x`.
86-
87-
```javascript
88-
var Float64Array = require( '@stdlib/array/float64' );
89-
90-
var x = new Float64Array( [ -1.0, -2.0 ] );
91-
var y = abs( x );
92-
// returns <Float64Array>[ 1.0, 2.0 ]
82+
var dt = getDType( y );
83+
// returns 'generic'
9384

94-
x = [ -1.0, -2.0 ];
95-
y = abs( x );
96-
// returns [ 1.0, 2.0 ]
85+
var arr = ndarray2array( y );
86+
// returns [ [ 1.0, 2.0 ], [ 3.0, 4.0 ] ]
9787
```
9888

99-
The function accepts the following `options`:
100-
101-
- **dtype**: output array [data type][@stdlib/ndarray/dtypes]. Only applicable when `x` is either an [`ndarray`][@stdlib/ndarray/ctor] or array-like object. By default, the output array data type is inferred from the input array.
102-
- **order**: output array [order][@stdlib/ndarray/orders]. Only applicable when `x` is an [`ndarray`][@stdlib/ndarray/ctor]. By default, the output array order is inferred from the input array.
103-
104-
By default, when provided either an [`ndarray`][@stdlib/ndarray/ctor] or an array-like object, the function returns an object of the same "kind" (either an [`ndarray`][@stdlib/ndarray/ctor] or array-like object, respectively) having the same underlying [data type][@stdlib/ndarray/dtypes]. To specify a different output array [data type][@stdlib/ndarray/dtypes], set the `dtype` option.
89+
By default, the function returns an [ndarray][@stdlib/ndarray/ctor] having the same [order][@stdlib/ndarray/orders] as the input [ndarray][@stdlib/ndarray/ctor]. To return an [ndarray][@stdlib/ndarray/ctor] having a specific memory layout irrespective of the memory layout of the input [ndarray][@stdlib/ndarray/ctor], set the `order` option.
10590

10691
```javascript
107-
var Float32Array = require( '@stdlib/array/float32' );
108-
109-
var x = new Float32Array( [ -1.0, -2.0 ] );
110-
var y = abs( x );
111-
// returns <Float32Array>[ 1.0, 2.0 ]
92+
var ndarray2array = require( '@stdlib/ndarray/to-array' );
93+
var array = require( '@stdlib/ndarray/array' );
94+
var getOrder = require( '@stdlib/ndarray/order' );
11295

113-
x = new Float32Array( [ -1.0, -2.0 ] );
114-
y = abs( x, {
115-
'dtype': 'float64'
96+
var x = array( [ [ -1.0, -2.0 ], [ -3.0, -4.0 ] ] );
97+
var y = abs( x, {
98+
'order': 'column-major'
11699
});
117-
// returns <Float64Array>[ 1.0, 2.0 ]
100+
// returns <ndarray>
101+
102+
var ord = getOrder( y );
103+
// returns 'column-major'
104+
105+
var arr = ndarray2array( y );
106+
// returns [ [ 1.0, 2.0 ], [ 3.0, 4.0 ] ]
118107
```
119108

120109
#### abs.assign( x, y )
121110

122-
Computes the [absolute value][absolute-value] and assigns results to a provided output array.
111+
Computes the [absolute value][@stdlib/math/base/special/abs] for each element in an [ndarray][@stdlib/ndarray/ctor] and assigns results to a provided output [ndarray][@stdlib/ndarray/ctor].
123112

124113
```javascript
114+
var ndarray2array = require( '@stdlib/ndarray/to-array' );
125115
var array = require( '@stdlib/ndarray/array' );
126116

127-
var x = array( [ [ -1.0, -2.0 ], [ -3.0, -4.0 ] ] ); // 2x2
128-
var y = array( [ [ 0.0, 0.0 ], [ 0.0, 0.0 ] ] ); // 2x2
117+
var x = array( [ [ -1.0, -2.0 ], [ -3.0, -4.0 ] ] );
118+
var y = array( [ [ 0.0, 0.0 ], [ 0.0, 0.0 ] ] );
119+
129120
var out = abs.assign( x, y );
130121
// returns <ndarray>
131122

132123
var bool = ( out === y );
133124
// returns true
134125

135-
var v = y.get( 0, 1 );
136-
// returns 2.0
126+
var arr = ndarray2array( out );
127+
// returns [ [ 1.0, 2.0 ], [ 3.0, 4.0 ] ]
137128
```
138129

139-
The output array must be the same data "kind" (i.e., [`ndarray`][@stdlib/ndarray/ctor] or array-like object) as the input array. For example, if `x` is an [`ndarray`][@stdlib/ndarray/ctor], `y` must also be an [`ndarray`][@stdlib/ndarray/ctor]. Similarly, if `x` is an array-like object, `y` must also be an array-like object.
130+
The function accepts the following arguments:
131+
132+
- **x**: input [ndarray][@stdlib/ndarray/ctor]. Must have a shape which is [broadcast-compatible][@stdlib/ndarray/base/broadcast-shapes] with the shape of the output [ndarray][@stdlib/ndarray/ctor].
133+
- **y**: output [ndarray][@stdlib/ndarray/ctor].
134+
135+
The function supports broadcasting an input [ndarray][@stdlib/ndarray/ctor] to the shape of the output [ndarray][@stdlib/ndarray/ctor] without performing a physical copy of the input [ndarray][@stdlib/ndarray/ctor]'s underlying data.
136+
137+
```javascript
138+
var ndarray2array = require( '@stdlib/ndarray/to-array' );
139+
var zeros = require( '@stdlib/ndarray/zeros' );
140+
var array = require( '@stdlib/ndarray/array' );
141+
142+
// Create a 2x2 input ndarray:
143+
var x = array( [ [ -1.0, -2.0 ], [ -3.0, -4.0 ] ] );
144+
145+
// Create a 2x2x2 output ndarray:
146+
var y = zeros( [ 2, 2, 2 ] );
147+
148+
var out = abs.assign( x, y );
149+
// returns <ndarray>
140150

141-
TODO: broadcasting discussion and example(s).
151+
var arr = ndarray2array( out );
152+
// returns [ [ [ 1.0, 2.0 ], [ 3.0, 4.0 ] ], [ [ 1.0, 2.0 ], [ 3.0, 4.0 ] ] ]
153+
```
142154

143155
</section>
144156

@@ -148,6 +160,10 @@ TODO: broadcasting discussion and example(s).
148160

149161
<section class="notes">
150162

163+
## Notes
164+
165+
- The output data type [policy][@stdlib/ndarray/output-dtype-policies] only applies to the main function and specifies that, by default, the function must return an [ndarray][@stdlib/ndarray/ctor] having a real-valued or "generic" [data type][@stdlib/ndarray/dtypes]. For the `assign` method, the output [ndarray][@stdlib/ndarray/ctor] is allowed to have any supported output [data type][@stdlib/ndarray/dtypes].
166+
151167
</section>
152168

153169
<!-- /.notes -->
@@ -161,34 +177,15 @@ TODO: broadcasting discussion and example(s).
161177
<!-- eslint no-undef: "error" -->
162178

163179
```javascript
164-
var Float64Array = require( '@stdlib/array/float64' );
165-
var array = require( '@stdlib/ndarray/array' );
166-
var ind2sub = require( '@stdlib/ndarray/ind2sub' );
180+
var uniform = require( '@stdlib/random/uniform' );
181+
var ndarray2array = require( '@stdlib/ndarray/to-array' );
167182
var abs = require( '@stdlib/math/special/abs' );
168183

169-
// Provide a number...
170-
var v = abs( -1.0 );
171-
console.log( 'x = %d => abs(x) = %d', -1.0, v );
184+
var x = uniform( [ 5, 5 ], -10.0, 10.0 );
185+
console.log( ndarray2array( x ) );
172186

173-
// Provide an array-like object...
174-
var x = new Float64Array( [ -1.0, -2.0, -3.0 ] );
175187
var y = abs( x );
176-
177-
var i;
178-
for ( i = 0; i < x.length; i++ ) {
179-
console.log( 'x_%d = %d => abs(x_%d) = %d', i, x[ i ], i, y[ i ] );
180-
}
181-
182-
// Provide an ndarray...
183-
x = array( [ [ -1.0, -2.0 ], [ -3.0, -4.0 ] ] );
184-
y = abs( x );
185-
186-
var sh = x.shape;
187-
var sub;
188-
for ( i = 0; i < x.length; i++ ) {
189-
sub = ind2sub( sh, i );
190-
console.log( 'x_%d%d = %d => abs(x_%d%d) = %d', sub[ 0 ], sub[ 1 ], x.iget( i ), sub[ 0 ], sub[ 1 ], y.iget( i ) );
191-
}
188+
console.log( ndarray2array( y ) );
192189
```
193190

194191
</section>
@@ -215,14 +212,18 @@ for ( i = 0; i < x.length; i++ ) {
215212

216213
<section class="links">
217214

218-
[absolute-value]: https://en.wikipedia.org/wiki/Absolute_value
215+
[@stdlib/math/base/special/abs]: https://github.com/stdlib-js/stdlib/tree/develop/lib/node_modules/%40stdlib/math/base/special/abs
219216

220217
[@stdlib/ndarray/ctor]: https://github.com/stdlib-js/stdlib/tree/develop/lib/node_modules/%40stdlib/ndarray/ctor
221218

222219
[@stdlib/ndarray/orders]: https://github.com/stdlib-js/stdlib/tree/develop/lib/node_modules/%40stdlib/ndarray/orders
223220

224221
[@stdlib/ndarray/dtypes]: https://github.com/stdlib-js/stdlib/tree/develop/lib/node_modules/%40stdlib/ndarray/dtypes
225222

223+
[@stdlib/ndarray/output-dtype-policies]: https://github.com/stdlib-js/stdlib/tree/develop/lib/node_modules/%40stdlib/ndarray/output-dtype-policies
224+
225+
[@stdlib/ndarray/base/broadcast-shapes]: https://github.com/stdlib-js/stdlib/tree/develop/lib/node_modules/%40stdlib/ndarray/base/broadcast-shapes
226+
226227
</section>
227228

228229
<!-- /.links -->
Lines changed: 27 additions & 20 deletions
Original file line numberDiff line numberDiff line change
@@ -21,17 +21,22 @@
2121
// MODULES //
2222

2323
var bench = require( '@stdlib/bench' );
24-
var uniform = require( '@stdlib/random/base/uniform' ).factory;
24+
var uniform = require( '@stdlib/random/uniform' );
2525
var isnan = require( '@stdlib/math/base/assert/is-nan' );
2626
var pow = require( '@stdlib/math/base/special/pow' );
27-
var Float32Array = require( '@stdlib/array/float32' );
27+
var getData = require( '@stdlib/ndarray/data-buffer' );
28+
var format = require( '@stdlib/string/format' );
2829
var pkg = require( './../package.json' ).name;
2930
var abs = require( './../lib/main.js' );
3031

3132

3233
// VARIABLES //
3334

34-
var rand = uniform( -100.0, 100.0 );
35+
var DTYPES = [
36+
'float64',
37+
'float32',
38+
'generic'
39+
];
3540

3641

3742
// FUNCTIONS //
@@ -40,17 +45,14 @@ var rand = uniform( -100.0, 100.0 );
4045
* Creates a benchmark function.
4146
*
4247
* @private
43-
* @param {PositiveInteger} len - array length
48+
* @param {PositiveInteger} size - array size
49+
* @param {string} dtype - data type
4450
* @returns {Function} benchmark function
4551
*/
46-
function createBenchmark( len ) {
47-
var x;
48-
var i;
49-
50-
x = new Float32Array( len );
51-
for ( i = 0; i < len; i++ ) {
52-
x[ i ] = rand();
53-
}
52+
function createBenchmark( size, dtype ) {
53+
var x = uniform( [ size ], -10.0, 10.0, {
54+
'dtype': dtype
55+
});
5456
return benchmark;
5557

5658
/**
@@ -66,12 +68,12 @@ function createBenchmark( len ) {
6668
b.tic();
6769
for ( i = 0; i < b.iterations; i++ ) {
6870
y = abs( x );
69-
if ( isnan( y[ i%len ] ) ) {
70-
b.fail( 'should not return NaN' );
71+
if ( typeof y !== 'object' ) {
72+
b.fail( 'should return an ndarray' );
7173
}
7274
}
7375
b.toc();
74-
if ( isnan( y[ i%len ] ) ) {
76+
if ( isnan( getData( y )[ i%size ] ) ) {
7577
b.fail( 'should not return NaN' );
7678
}
7779
b.pass( 'benchmark finished' );
@@ -88,19 +90,24 @@ function createBenchmark( len ) {
8890
* @private
8991
*/
9092
function main() {
91-
var len;
93+
var size;
9294
var min;
9395
var max;
96+
var dt;
9497
var f;
9598
var i;
99+
var j;
96100

97101
min = 1; // 10^min
98102
max = 6; // 10^max
99103

100-
for ( i = min; i <= max; i++ ) {
101-
len = pow( 10, i );
102-
f = createBenchmark( len );
103-
bench( pkg+'::array_like_object:contiguous=true,ndims=1,dtype=float32,len='+len, f );
104+
for ( j = 0; j < DTYPES.length; j++ ) {
105+
dt = DTYPES[ j ];
106+
for ( i = min; i <= max; i++ ) {
107+
size = pow( 10, i );
108+
f = createBenchmark( size, dt );
109+
bench( format( '%s:contiguous=true,ndims=1,dtype=%s,size=%d', pkg, dt, size ), f );
110+
}
104111
}
105112
}
106113

0 commit comments

Comments
 (0)