Skip to content
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
113 changes: 113 additions & 0 deletions lib/node_modules/@stdlib/ndarray/base/atleastnd/README.md
Original file line number Diff line number Diff line change
@@ -0,0 +1,113 @@
<!--

@license Apache-2.0

Copyright (c) 2026 The Stdlib Authors.

Licensed under the Apache License, Version 2.0 (the "License");
you may not use this file except in compliance with the License.
You may obtain a copy of the License at

http://www.apache.org/licenses/LICENSE-2.0

Unless required by applicable law or agreed to in writing, software
distributed under the License is distributed on an "AS IS" BASIS,
WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
See the License for the specific language governing permissions and
limitations under the License.

-->

# atleastnd

> Convert the inputs to ndarrays having at least the provided number of dimensions.

<section class="intro">

</section>

<!-- /.intro -->

<section class="usage">

## Usage

```javascript
var atleastnd = require( '@stdlib/ndarray/base/atleastnd' );
```

#### atleastnd( ndims, arrays )

Converts the inputs to ndarrays having at least the provided number of dimensions.

<!-- eslint-disable max-len -->

```javascript
var array = require( '@stdlib/ndarray/array' );

var x = array( [ [ [ 1.0, 2.0 ] ], [ [ 3.0, 4.0 ] ] ] );
// returns <ndarray>[ [ [ 1.0, 2.0 ] ], [ [ 3.0, 4.0 ] ] ]

var y = array( [ [ 5.0, 6.0 ], [ 7.0, 8.0 ] ] );
// returns <ndarray>[ [ 5.0, 6.0 ], [ 7.0, 8.0 ] ]

var out = atleastnd( 3, [ x, y ] );
// returns [ <ndarray>, <ndarray> ]
```

The function accepts the following arguments:

- **ndims**: minimum number of dimensions.
- **arrays**: array-like object containing a list of arrays.

</section>

<!-- /.usage -->

<section class="notes">

</section>

<!-- /.notes -->

<section class="examples">

## Examples

<!-- eslint no-undef: "error" -->

```javascript
var discreteUniform = require( '@stdlib/random/discrete-uniform' );
var ndarray2array = require( '@stdlib/ndarray/to-array' );
var atleastnd = require( '@stdlib/ndarray/base/atleastnd' );

var x = discreteUniform( [ 2, 2, 2 ], -100, 100 );
// returns <ndarray>

var y = discreteUniform( [ 5, 2 ], -100, 100 );
// returns <ndarray>

var out = atleastnd( 3, [ x, y ] );
// returns [ <ndarray>, <ndarray> ]

console.log( ndarray2array( out[ 0 ] ) );
console.log( ndarray2array( out[ 1 ] ) );
```

</section>

<!-- /.examples -->

<!-- Section for related `stdlib` packages. Do not manually edit this section, as it is automatically populated. -->

<section class="related">

</section>

<!-- /.related -->

<section class="links">

</section>

<!-- /.links -->
103 changes: 103 additions & 0 deletions lib/node_modules/@stdlib/ndarray/base/atleastnd/benchmark/benchmark.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,103 @@
/**
* @license Apache-2.0
*
* Copyright (c) 2026 The Stdlib Authors.
*
* Licensed under the Apache License, Version 2.0 (the "License");
* you may not use this file except in compliance with the License.
* You may obtain a copy of the License at
*
* http://www.apache.org/licenses/LICENSE-2.0
*
* Unless required by applicable law or agreed to in writing, software
* distributed under the License is distributed on an "AS IS" BASIS,
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
* See the License for the specific language governing permissions and
* limitations under the License.
*/

'use strict';

// MODULES //

var bench = require( '@stdlib/bench' );
var Float64Array = require( '@stdlib/array/float64' );
var ndarrayBase = require( '@stdlib/ndarray/base/ctor' );
var ndarray = require( '@stdlib/ndarray/ctor' );
var isndarrayLike = require( '@stdlib/assert/is-ndarray-like' );
var format = require( '@stdlib/string/format' );
var pkg = require( './../package.json' ).name;
var atleastnd = require( './../lib' );


// MAIN //

bench( format( '%s::base_ndarray:ndims=2', pkg ), function benchmark( b ) {
var values;
var buffer;
var offset;
var dtype;
var order;
var out;
var i;

dtype = 'float64';
buffer = new Float64Array( 12 );
offset = 0;
order = 'row-major';

values = [
ndarrayBase( dtype, buffer, [ 3, 2, 2 ], [ 4, 2, 1 ], offset, order ),
ndarrayBase( dtype, buffer, [ 4, 3 ], [ 3, 1 ], offset, order ),
ndarrayBase( dtype, buffer, [ 12 ], [ 1 ], offset, order )
];

b.tic();
for ( i = 0; i < b.iterations; i++ ) {
out = atleastnd( 2, [ 10.0, values[ i%values.length ] ] );
if ( typeof out !== 'object' ) {
b.fail( 'should return an object' );
}
}
b.toc();
if ( !isndarrayLike( out[ 0 ] ) || !isndarrayLike( out[ 1 ] ) ) {
b.fail( 'should return an array of ndarrays' );
}
b.pass( 'benchmark finished' );
b.end();
});

bench( format( '%s::ndarray:ndims=2', pkg ), function benchmark( b ) {
var values;
var buffer;
var offset;
var dtype;
var order;
var out;
var i;

dtype = 'float64';
buffer = new Float64Array( 12 );
offset = 0;
order = 'row-major';

values = [
ndarray( dtype, buffer, [ 3, 2, 2 ], [ 4, 2, 1 ], offset, order ),
ndarray( dtype, buffer, [ 4, 3 ], [ 3, 1 ], offset, order ),
ndarray( dtype, buffer, [ 12 ], [ 1 ], offset, order )
];

b.tic();
for ( i = 0; i < b.iterations; i++ ) {
out = atleastnd( 2, [ 10.0, values[ i%values.length ] ] );
if ( typeof out !== 'object' ) {
b.fail( 'should return an object' );
}
}
b.toc();
if ( !isndarrayLike( out[ 0 ] ) || !isndarrayLike( out[ 1 ] ) ) {
b.fail( 'should return an array of ndarrays' );
}
b.pass( 'benchmark finished' );
b.end();
});
29 changes: 29 additions & 0 deletions lib/node_modules/@stdlib/ndarray/base/atleastnd/docs/repl.txt
Original file line number Diff line number Diff line change
@@ -0,0 +1,29 @@

{{alias}}( ndims, arrays )
Converts the inputs to ndarrays having at least the provided number of
dimensions.

Parameters
----------
ndims: number
Minimum number of dimensions.

arrays: ArrayLikeObject
List of arrays.

Returns
-------
out: Array<ndarray>
List of arrays.

Examples
--------
> var x = {{alias:@stdlib/ndarray/array}}( [ [ 1, 2 ], [ 3, 4 ] ] )
<ndarray>[ [ 1, 2 ], [ 3, 4 ] ]
> var y = {{alias:@stdlib/ndarray/array}}( [ 5, 6, 7, 8 ] )
<ndarray>[ 5, 6, 7, 8 ]
> var out = {{alias}}( 3, [ x, y ] )
[ <ndarray>, <ndarray> ]

See Also
--------
Original file line number Diff line number Diff line change
@@ -0,0 +1,49 @@
/*
* @license Apache-2.0
*
* Copyright (c) 2026 The Stdlib Authors.
*
* Licensed under the Apache License, Version 2.0 (the "License");
* you may not use this file except in compliance with the License.
* You may obtain a copy of the License at
*
* http://www.apache.org/licenses/LICENSE-2.0
*
* Unless required by applicable law or agreed to in writing, software
* distributed under the License is distributed on an "AS IS" BASIS,
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
* See the License for the specific language governing permissions and
* limitations under the License.
*/

// TypeScript Version: 4.1

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

import { ndarray } from '@stdlib/types/ndarray';

/**
* Converts the inputs to ndarrays having at least the provided number of dimensions.
*
* @param ndims - minimum number of dimensions
* @param arrays - array-like object containing a list of arrays
* @returns output arrays
*
* @example
* var array = require( '@stdlib/ndarray/array' );
*
* var x = array( [ [ [ 1.0, 2.0 ] ], [ [ 3.0, 4.0 ] ] ] );
* // returns <ndarray>[ [ [ 1.0, 2.0 ] ], [ [ 3.0, 4.0 ] ] ]
*
* var y = array( [ [ 5.0, 6.0 ], [ 7.0, 8.0 ] ] );
* // returns <ndarray>[ [ 5.0, 6.0 ], [ 7.0, 8.0 ] ]
*
* var out = atleastnd( 3, [ x, y ] );
* // returns [ <ndarray>, <ndarray> ]
*/
declare function atleastnd( ndims: number, arrays: Array<any> ): Array<ndarray>;

Check warning on line 44 in lib/node_modules/@stdlib/ndarray/base/atleastnd/docs/types/index.d.ts

View workflow job for this annotation

GitHub Actions / Lint Changed Files

Unexpected any. Specify a different type


// EXPORTS //

export = atleastnd;
68 changes: 68 additions & 0 deletions lib/node_modules/@stdlib/ndarray/base/atleastnd/docs/types/test.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,68 @@
/*
* @license Apache-2.0
*
* Copyright (c) 2026 The Stdlib Authors.
*
* Licensed under the Apache License, Version 2.0 (the "License");
* you may not use this file except in compliance with the License.
* You may obtain a copy of the License at
*
* http://www.apache.org/licenses/LICENSE-2.0
*
* Unless required by applicable law or agreed to in writing, software
* distributed under the License is distributed on an "AS IS" BASIS,
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
* See the License for the specific language governing permissions and
* limitations under the License.
*/

import zeros = require( '@stdlib/ndarray/zeros' );
import atleastnd = require( './index' );


// TESTS //

// The function returns an array of ndarrays...
{
const x = zeros( [ 2, 2 ] );
const y = zeros( [ 2, 2, 2 ] );

atleastnd( 3, [ x ] ); // $ExpectType ndarray[]
atleastnd( 3, [ x, y ] ); // $ExpectType ndarray[]
atleastnd( 3, [ x, y, x ] ); // $ExpectType ndarray[]
}

// The compiler throws an error if the function is not provided a first argument which is a number...
{
const x = zeros( [ 2, 2 ] );
const y = zeros( [ 2, 2, 2 ] );

atleastnd( '5', [ x, y ] ); // $ExpectError
atleastnd( true, [ x, y ] ); // $ExpectError
atleastnd( false, [ x, y ] ); // $ExpectError
atleastnd( null, [ x, y ] ); // $ExpectError
atleastnd( {}, [ x, y ] ); // $ExpectError
atleastnd( [ '5' ], [ x, y ] ); // $ExpectError
atleastnd( ( x: number ): number => x, [ x, y ] ); // $ExpectError
}

// The compiler throws an error if the function is not provided a second argument which is an array ...
{
atleastnd( 3, '5' ); // $ExpectError
atleastnd( 3, 5 ); // $ExpectError
atleastnd( 3, true ); // $ExpectError
atleastnd( 3, false ); // $ExpectError
atleastnd( 3, null ); // $ExpectError
atleastnd( 3, {} ); // $ExpectError
atleastnd( 3, ( x: number ): number => x ); // $ExpectError
}

// The compiler throws an error if the function is provided an unsupported number of arguments...
{
const x = zeros( [ 2, 2 ] );
const y = zeros( [ 2, 2, 2 ] );

atleastnd(); // $ExpectError
atleastnd( 3 ); // $ExpectError
atleastnd( 3, [ x, y ], {} ); // $ExpectError
}
Loading