Skip to content

Commit c52b682

Browse files
committed
chore: clean-up
--- 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: na - task: lint_javascript_tests status: passed - task: lint_javascript_benchmarks status: na - 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 4ce22f7 commit c52b682

8 files changed

Lines changed: 117 additions & 51 deletions

File tree

lib/node_modules/@stdlib/ndarray/base/atleastnd/README.md

Lines changed: 23 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -20,7 +20,7 @@ limitations under the License.
2020

2121
# atleastnd
2222

23-
> Convert the inputs to ndarrays having at least the provided number of dimensions.
23+
> Convert a list of values (scalars and/or ndarrays) to ndarrays having at least a specified number of dimensions.
2424
2525
<section class="intro">
2626

@@ -38,9 +38,7 @@ var atleastnd = require( '@stdlib/ndarray/base/atleastnd' );
3838

3939
#### atleastnd( ndims, arrays )
4040

41-
Converts the inputs to ndarrays having at least the provided number of dimensions.
42-
43-
<!-- eslint-disable max-len -->
41+
Converts a list of values (scalars and/or ndarrays) to ndarrays having at least a specified number of dimensions.
4442

4543
```javascript
4644
var array = require( '@stdlib/ndarray/array' );
@@ -58,14 +56,28 @@ var out = atleastnd( 3, [ x, y ] );
5856
The function accepts the following arguments:
5957

6058
- **ndims**: minimum number of dimensions.
61-
- **arrays**: array-like object containing a list of arrays.
59+
- **arrays**: array-like object containing a list of scalars and/or ndarrays.
6260

6361
</section>
6462

6563
<!-- /.usage -->
6664

6765
<section class="notes">
6866

67+
## Notes
68+
69+
- If a provided ndarray has fewer dimensions than `ndims`, the returned ndarray is a view on the input ndarray data buffer. The view is typically **not** contiguous. As more than one element of a returned view may refer to the same memory location, writing to the view may affect multiple elements. If you need to write to the returned ndarray, copy the ndarray **before** performing operations which may mutate elements.
70+
71+
- The returned ndarray is a "base" [ndarray][@stdlib/ndarray/base/ctor], and, thus, the returned [ndarray][@stdlib/ndarray/base/ctor] does not perform bounds checking or afford any of the guarantees of the non-base [ndarray][@stdlib/ndarray/ctor] constructor. The primary intent of this function is to broadcast an ndarray-like object within internal implementations and to do so with minimal overhead.
72+
73+
- If provided a scalar value (i.e., a non-ndarray) and that value
74+
75+
- is a number, the returned ndarray will have the [default][@stdlib/ndarray/defaults] real-valued floating-point data type.
76+
- is a boolean, the returned ndarray will have the [default][@stdlib/ndarray/defaults] boolean data type.
77+
- is a complex number object of a known data type, the data type of the returned ndarray will be the same as the provided value.
78+
- is a complex number object of an unknown data type, the returned ndarray will have the [default][@stdlib/ndarray/defaults] complex-valued floating-point data type.
79+
- is any other value type, the returned ndarray will have a `'generic'` data type.
80+
6981
</section>
7082

7183
<!-- /.notes -->
@@ -108,6 +120,12 @@ console.log( ndarray2array( out[ 1 ] ) );
108120

109121
<section class="links">
110122

123+
[@stdlib/ndarray/ctor]: https://github.com/stdlib-js/stdlib/tree/develop/lib/node_modules/%40stdlib/ndarray/ctor
124+
125+
[@stdlib/ndarray/base/ctor]: https://github.com/stdlib-js/stdlib/tree/develop/lib/node_modules/%40stdlib/ndarray/base/ctor
126+
127+
[@stdlib/ndarray/defaults]: https://github.com/stdlib-js/stdlib/tree/develop/lib/node_modules/%40stdlib/ndarray/defaults
128+
111129
</section>
112130

113131
<!-- /.links -->

lib/node_modules/@stdlib/ndarray/base/atleastnd/docs/repl.txt

Lines changed: 4 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -1,20 +1,20 @@
11

22
{{alias}}( ndims, arrays )
3-
Converts the inputs to ndarrays having at least the provided number of
4-
dimensions.
3+
Converts a list of values (scalars and/or ndarrays) to ndarrays having at
4+
least a specified number of dimensions.
55

66
Parameters
77
----------
88
ndims: number
99
Minimum number of dimensions.
1010

1111
arrays: ArrayLikeObject
12-
List of arrays.
12+
List of scalars and/or ndarrays.
1313

1414
Returns
1515
-------
1616
out: Array<ndarray>
17-
List of arrays.
17+
List of ndarrays.
1818

1919
Examples
2020
--------

lib/node_modules/@stdlib/ndarray/base/atleastnd/docs/types/index.d.ts

Lines changed: 5 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -20,14 +20,15 @@
2020

2121
/// <reference types="@stdlib/types"/>
2222

23+
import { ArrayLike } from '@stdlib/types/array';
2324
import { ndarray } from '@stdlib/types/ndarray';
2425

2526
/**
26-
* Converts the inputs to ndarrays having at least the provided number of dimensions.
27+
* Converts a list of values (scalars and/or ndarrays) to ndarrays having at least a specified number of dimensions.
2728
*
2829
* @param ndims - minimum number of dimensions
29-
* @param arrays - array-like object containing a list of arrays
30-
* @returns output arrays
30+
* @param arrays - array-like object containing a list of scalars and/or ndarrays
31+
* @returns an array of ndarrays
3132
*
3233
* @example
3334
* var array = require( '@stdlib/ndarray/array' );
@@ -41,7 +42,7 @@ import { ndarray } from '@stdlib/types/ndarray';
4142
* var out = atleastnd( 3, [ x, y ] );
4243
* // returns [ <ndarray>, <ndarray> ]
4344
*/
44-
declare function atleastnd( ndims: number, arrays: Array<any> ): Array<ndarray>;
45+
declare function atleastnd( ndims: number, arrays: ArrayLike<any> ): Array<ndarray>;
4546

4647

4748
// EXPORTS //

lib/node_modules/@stdlib/ndarray/base/atleastnd/docs/types/test.ts

Lines changed: 2 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -32,7 +32,7 @@ import atleastnd = require( './index' );
3232
atleastnd( 3, [ x, y, x ] ); // $ExpectType ndarray[]
3333
}
3434

35-
// The compiler throws an error if the function is not provided a first argument which is a number...
35+
// The compiler throws an error if the function is provided a first argument which is not a number...
3636
{
3737
const x = zeros( [ 2, 2 ] );
3838
const y = zeros( [ 2, 2, 2 ] );
@@ -46,9 +46,8 @@ import atleastnd = require( './index' );
4646
atleastnd( ( x: number ): number => x, [ x, y ] ); // $ExpectError
4747
}
4848

49-
// The compiler throws an error if the function is not provided a second argument which is an array ...
49+
// The compiler throws an error if the function is provided a second argument which is not an array ...
5050
{
51-
atleastnd( 3, '5' ); // $ExpectError
5251
atleastnd( 3, 5 ); // $ExpectError
5352
atleastnd( 3, true ); // $ExpectError
5453
atleastnd( 3, false ); // $ExpectError

lib/node_modules/@stdlib/ndarray/base/atleastnd/lib/index.js

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -19,7 +19,7 @@
1919
'use strict';
2020

2121
/**
22-
* Convert the inputs to ndarrays having at least the provided number of dimensions.
22+
* Convert a list of values (scalars and/or ndarrays) to ndarrays having at least a specified number of dimensions.
2323
*
2424
* @module @stdlib/ndarray/base/atleastnd
2525
*

lib/node_modules/@stdlib/ndarray/base/atleastnd/lib/main.js

Lines changed: 48 additions & 24 deletions
Original file line numberDiff line numberDiff line change
@@ -21,6 +21,10 @@
2121
// MODULES //
2222

2323
var isndarrayLike = require( '@stdlib/assert/is-ndarray-like' );
24+
var isNumber = require( '@stdlib/assert/is-number' ).isPrimitive;
25+
var isComplexLike = require( '@stdlib/assert/is-complex-like' );
26+
var isBoolean = require( '@stdlib/assert/is-boolean' ).isPrimitive;
27+
var complexDataType = require( '@stdlib/complex/dtype' );
2428
var broadcastScalar = require( '@stdlib/ndarray/base/broadcast-scalar' );
2529
var dims = require( '@stdlib/ndarray/base/ndims' );
2630
var defaults = require( '@stdlib/ndarray/defaults' );
@@ -36,18 +40,20 @@ var ones = require( '@stdlib/array/base/ones' );
3640

3741
// VARIABLES //
3842

39-
var DTYPE = defaults.get( 'dtypes.default' );
43+
var DEFAULT_REAL = defaults.get( 'dtypes.real_floating_point' );
44+
var DEFAULT_CMPLX = defaults.get( 'dtypes.complex_floating_point' );
45+
var DEFAULT_BOOL = defaults.get( 'dtypes.boolean' );
4046
var ORDER = defaults.get( 'order' );
4147

4248

4349
// MAIN //
4450

4551
/**
46-
* Converts the inputs to ndarrays having at least the provided number of dimensions.
52+
* Converts a list of values (scalars and/or ndarrays) to ndarrays having at least a specified number of dimensions.
4753
*
4854
* @param {NonNegativeInteger} ndims - minimum number of dimensions
49-
* @param {ArrayLikeObject<Object>} arrays - array-like object containing a list of arrays
50-
* @returns {ArrayLikeObject<Object>} output arrays
55+
* @param {ArrayLikeObject<Object>} arrays - array-like object containing a list of scalars and/or ndarrays
56+
* @returns {Array<Object>} list of ndarrays
5157
*
5258
* @example
5359
* var array = require( '@stdlib/ndarray/array' );
@@ -67,36 +73,54 @@ function atleastnd( ndims, arrays ) {
6773
var out;
6874
var sh;
6975
var st;
76+
var dt;
77+
var v;
7078
var N;
71-
var n;
7279
var i;
7380
var j;
7481

7582
out = [];
7683
for ( i = 0; i < arrays.length; i++ ) {
77-
if ( isndarrayLike( arrays[ i ] ) ) {
78-
N = dims( arrays[ i ] );
84+
v = arrays[ i ];
85+
if ( isndarrayLike( v ) ) {
86+
N = dims( v );
7987
if ( N >= ndims ) {
80-
out.push( arrays[ i ] );
81-
} else {
82-
sh = getShape( arrays[ i ], false );
83-
n = ndims - N;
84-
st = getStrides( arrays[ i ], false );
85-
shape = [];
86-
strides = [];
87-
for ( j = 0; j < n; j++ ) {
88-
shape.push( 1 );
89-
strides.push( st[ 0 ] );
90-
}
91-
for ( j = 0; j < sh.length; j++ ) {
92-
shape.push( sh[ j ] );
93-
strides.push( st[ j ] );
94-
}
95-
out.push( ndarray( getDType( arrays[ i ] ), getData( arrays[ i ] ), shape, strides, getOffset( arrays[ i ] ), getOrder( arrays[ i ] ) ) ); // eslint-disable-line max-len
88+
out.push( v );
89+
continue;
90+
}
91+
sh = getShape( v, false );
92+
st = getStrides( v, false );
93+
94+
shape = [];
95+
strides = [];
96+
97+
// Prepend singleton dimensions...
98+
for ( j = 0; j < ndims-N; j++ ) {
99+
shape.push( 1 );
100+
strides.push( st[ 0 ] );
101+
}
102+
// Copy remaining dimensions...
103+
for ( j = 0; j < sh.length; j++ ) {
104+
shape.push( sh[ j ] );
105+
strides.push( st[ j ] );
106+
}
107+
out.push( new ndarray( getDType( v ), getData( v ), shape, strides, getOffset( v ), getOrder( v ) ) ); // eslint-disable-line max-len
108+
continue;
109+
}
110+
// For scalar values, resolve a corresponding ndarray data type...
111+
if ( isNumber( v ) ) { // TODO: consider abstracting this logic to an `ndarray/base/scalar-dtype` (???) package, as this logic is found elsewhere (e.g., `ndarray/from-scalar`) and it would be good to avoid duplication, especially as we add support for more ndarray data types
112+
dt = DEFAULT_REAL;
113+
} else if ( isBoolean( v ) ) {
114+
dt = DEFAULT_BOOL;
115+
} else if ( isComplexLike( v ) ) {
116+
dt = complexDataType( v );
117+
if ( dt === null ) {
118+
dt = DEFAULT_CMPLX;
96119
}
97120
} else {
98-
out.push( broadcastScalar( arrays[ i ], DTYPE, ones( ndims ), ORDER ) ); // eslint-disable-line max-len
121+
dt = 'generic';
99122
}
123+
out.push( broadcastScalar( v, dt, ones( ndims ), ORDER ) );
100124
}
101125
return out;
102126
}

lib/node_modules/@stdlib/ndarray/base/atleastnd/package.json

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,7 +1,7 @@
11
{
22
"name": "@stdlib/ndarray/base/atleastnd",
33
"version": "0.0.0",
4-
"description": "Coverts the inputs to ndarrays having at least the provided number of dimensions.",
4+
"description": "Convert a list of values (scalars and/or ndarrays) to ndarrays having at least a specified number of dimensions.",
55
"license": "Apache-2.0",
66
"author": {
77
"name": "The Stdlib Authors",

lib/node_modules/@stdlib/ndarray/base/atleastnd/test/test.js

Lines changed: 33 additions & 9 deletions
Original file line numberDiff line numberDiff line change
@@ -25,6 +25,7 @@ var isArray = require( '@stdlib/assert/is-array' );
2525
var getShape = require( '@stdlib/ndarray/base/shape' );
2626
var zeros = require( '@stdlib/ndarray/zeros' );
2727
var ndarray = require( '@stdlib/ndarray/base/ctor' );
28+
var Complex128 = require( '@stdlib/complex/float64/ctor' );
2829
var atleastnd = require( './../lib' );
2930

3031

@@ -36,33 +37,56 @@ tape( 'main export is a function', function test( t ) {
3637
t.end();
3738
});
3839

39-
tape( 'the function converts the inputs to ndarrays having at least the provided number of dimensions.', function test( t ) {
40+
tape( 'the function a list of values (scalars and/or ndarrays) to ndarrays having at least a specified number of dimensions', function test( t ) {
4041
var out;
4142
var x;
4243
var y;
4344
var z;
45+
var w;
46+
var u;
47+
var v;
48+
var s;
4449

4550
x = zeros( [ 2, 2 ] );
4651
y = zeros( [ 2 ] );
4752
z = 10.0;
53+
w = true;
54+
u = new Complex128( 1.0, 2.0 );
55+
v = {
56+
're': 3.0,
57+
'im': 4.0
58+
};
59+
s = 'beep';
4860

49-
out = atleastnd( 3, [ x, y, z ] );
61+
out = atleastnd( 3, [ x, y, z, w, u, v, s ] );
5062

5163
t.strictEqual( isArray( out ), true, 'returns expected value' );
5264
t.deepEqual( getShape( out[ 0 ] ), [ 1, 2, 2 ], 'returns expected value' );
5365
t.deepEqual( getShape( out[ 1 ] ), [ 1, 1, 2 ], 'returns expected value' );
5466
t.deepEqual( getShape( out[ 2 ] ), [ 1, 1, 1 ], 'returns expected value' );
55-
t.notEqual( out[ 0 ], x, 'returns new instance' );
56-
t.notEqual( out[ 1 ], y, 'returns new instance' );
57-
t.notEqual( out[ 2 ], z, 'returns new instance' );
67+
t.deepEqual( getShape( out[ 3 ] ), [ 1, 1, 1 ], 'returns expected value' );
68+
t.deepEqual( getShape( out[ 4 ] ), [ 1, 1, 1 ], 'returns expected value' );
69+
t.deepEqual( getShape( out[ 5 ] ), [ 1, 1, 1 ], 'returns expected value' );
70+
t.deepEqual( getShape( out[ 6 ] ), [ 1, 1, 1 ], 'returns expected value' );
71+
t.notEqual( out[ 0 ], x, 'returns expected value' );
72+
t.notEqual( out[ 1 ], y, 'returns expected value' );
73+
t.notEqual( out[ 2 ], z, 'returns expected value' );
74+
t.notEqual( out[ 3 ], w, 'returns expected value' );
75+
t.notEqual( out[ 4 ], u, 'returns expected value' );
76+
t.notEqual( out[ 5 ], v, 'returns expected value' );
77+
t.notEqual( out[ 6 ], s, 'returns expected value' );
5878
t.strictEqual( out[ 0 ] instanceof ndarray, true, 'returns expected value' );
5979
t.strictEqual( out[ 1 ] instanceof ndarray, true, 'returns expected value' );
6080
t.strictEqual( out[ 2 ] instanceof ndarray, true, 'returns expected value' );
81+
t.strictEqual( out[ 3 ] instanceof ndarray, true, 'returns expected value' );
82+
t.strictEqual( out[ 4 ] instanceof ndarray, true, 'returns expected value' );
83+
t.strictEqual( out[ 5 ] instanceof ndarray, true, 'returns expected value' );
84+
t.strictEqual( out[ 6 ] instanceof ndarray, true, 'returns expected value' );
6185

6286
t.end();
6387
});
6488

65-
tape( 'the function returns the same ndarray if provided an ndarray having dimensions more than or equal to `ndims`', function test( t ) {
89+
tape( 'the function returns an ndarray unchanged if an ndarray has at least a specified number of dimensions', function test( t ) {
6690
var out;
6791
var x;
6892
var y;
@@ -78,9 +102,9 @@ tape( 'the function returns the same ndarray if provided an ndarray having dimen
78102
t.deepEqual( getShape( out[ 0 ] ), [ 1, 2, 2 ], 'returns expected value' );
79103
t.deepEqual( getShape( out[ 1 ] ), [ 3, 2, 2 ], 'returns expected value' );
80104
t.deepEqual( getShape( out[ 2 ] ), [ 1, 3, 2, 2 ], 'returns expected value' );
81-
t.notEqual( out[ 0 ], x, 'returns new instance' );
82-
t.strictEqual( out[ 1 ], y, 'returns new instance' );
83-
t.strictEqual( out[ 2 ], z, 'returns new instance' );
105+
t.notEqual( out[ 0 ], x, 'returns expected value' );
106+
t.strictEqual( out[ 1 ], y, 'returns expected value' );
107+
t.strictEqual( out[ 2 ], z, 'returns expected value' );
84108
t.strictEqual( out[ 0 ] instanceof ndarray, true, 'returns expected value' );
85109
t.strictEqual( out[ 1 ] instanceof ndarray, true, 'returns expected value' );
86110
t.strictEqual( out[ 2 ] instanceof ndarray, true, 'returns expected value' );

0 commit comments

Comments
 (0)