Skip to content

feat: add array/base/linspace2d #5464

New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Merged
merged 16 commits into from
Apr 9, 2025
Merged
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
132 changes: 132 additions & 0 deletions lib/node_modules/@stdlib/array/base/linspace2d/README.md
Original file line number Diff line number Diff line change
@@ -0,0 +1,132 @@
<!--

@license Apache-2.0

Copyright (c) 2025 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.

-->

# linspace2d

> Generate a linearly spaced two-dimensional nested numeric array.

<section class="usage">

## Usage

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

#### linspace2d( start, stop, shape, colexicographic )

Generates a linearly spaced two-dimensional nested numeric array.

```javascript
var x = linspace2d( 0, 100, [ 2, 3 ], false );
// returns [ [ 0, 20, 40 ], [ 60, 80, 100 ] ]

x = linspace2d( 0, 100, [ 2, 3 ], true );
// returns [ [ 0, 40, 80 ], [ 20, 60, 100 ] ]
```

The function accepts the following arguments:

- **start**: first array value.
- **stop**: last array value.
- **shape**: array shape.
- **colexicographic**: specifies whether generated array values should be stored in colexicographic order.

</section>

<!-- /.usage -->

<section class="notes">

## Notes

- The output array is guaranteed to include the `start` and `stop` values. Beware, however, that values between `start` and `stop` are subject to floating-point rounding errors. Hence,

```javascript
var arr = linspace2d( 0, 1, [ 1, 3 ], false );
// returns [ [ 0, ~0.5, 1 ] ]
```

where `arr[0][1]` is only guaranteed to be approximately equal to `0.5`. If you desire more control over element precision, consider using [`roundn`][@stdlib/math/base/special/roundn]:

```javascript
var roundn = require( '@stdlib/math/base/special/roundn' );
var map2d = require( '@stdlib/array/base/map2d' );

var arr = linspace2d( 0, 1, [ 1, 3 ], false );
// returns [ [ 0, ~0.5, 1 ] ]

// Round each value to the nearest tenth:
var out = map2d( arr, [ 1, 3 ], clbk );
// returns [ [ 0, 0.5, 1 ] ]

function clbk( v ) {
return roundn( v, -1 );
}
```

</section>

<!-- /.notes -->

<section class="examples">

## Examples

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

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

var out = linspace2d( 0, 10, [ 2, 5 ], false );
console.log( out );

out = linspace2d( 0, 10, [ 2, 3 ], true );
console.log( out );

out = linspace2d( 0, 10, [ 4, 2 ], true );
console.log( out );

// Create an array of arrays with decremented values:
out = linspace2d( 10, 0, [ 2, 5 ], false );
console.log( out );
```

</section>

<!-- /.examples -->

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

<section class="related">

</section>

<!-- /.related -->

<!-- Section for all links. Make sure to keep an empty line after the `section` element and another before the `/section` close. -->

<section class="links">

[@stdlib/math/base/special/roundn]: https://github.com/stdlib-js/stdlib/tree/develop/lib/node_modules/%40stdlib/math/base/special/roundn

</section>

<!-- /.links -->
Original file line number Diff line number Diff line change
@@ -0,0 +1,48 @@
/**
* @license Apache-2.0
*
* Copyright (c) 2025 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 isArrayArray = require( '@stdlib/assert/is-array-array' );
var pkg = require( './../package.json' ).name;
var linspace2d = require( './../lib' );


// MAIN //

bench( pkg, function benchmark( b ) {
var i;
var v;

b.tic();
for ( i = 0; i < b.iterations; i++ ) {
v = linspace2d( 0.0, 100.0, [ 2, 3 ], false );
if ( typeof v !== 'object' ) {
b.fail( 'should return an array of arrays' );
}
}
b.toc();
if ( !isArrayArray( v ) ) {
b.fail( 'should return an array of arrays' );
}
b.pass( 'benchmark finished' );
b.end();
});
Original file line number Diff line number Diff line change
@@ -0,0 +1,95 @@
/**
* @license Apache-2.0
*
* Copyright (c) 2025 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 pow = require( '@stdlib/math/base/special/pow' );
var floor = require( '@stdlib/math/base/special/floor' );
var sqrt = require( '@stdlib/math/base/special/sqrt' );
var isArrayArray = require( '@stdlib/assert/is-array-array' );
var pkg = require( './../package.json' ).name;
var linspace2d = require( './../lib' );


// FUNCTIONS //

/**
* Creates a benchmark function.
*
* @private
* @param {PositiveInteger} N - array lengths
* @returns {Function} benchmark function
*/
function createBenchmark( N ) {
return benchmark;

/**
* Benchmark function.
*
* @private
* @param {Benchmark} b - benchmark instance
*/
function benchmark( b ) {
var v;
var i;

b.tic();
for ( i = 0; i < b.iterations; i++ ) {
v = linspace2d( 0.0, 100.0, [ N, N ], false );
if ( typeof v !== 'object' ) {
b.fail( 'should return an array of arrays' );
}
}
b.toc();
if ( !isArrayArray( v ) ) {
b.fail( 'should return an array of arrays' );
}
b.pass( 'benchmark finished' );
b.end();
}
}


// MAIN //

/**
* Main execution sequence.
*
* @private
*/
function main() {
var min;
var max;
var N;
var f;
var i;

min = 1; // 10^min
max = 6; // 10^max

for ( i = min; i <= max; i++ ) {
N = floor( sqrt( pow( 10, i ) ) );
f = createBenchmark( N );
bench( pkg+'::square_matrix:size='+(N*N), f );
}
}

main();
33 changes: 33 additions & 0 deletions lib/node_modules/@stdlib/array/base/linspace2d/docs/repl.txt
Original file line number Diff line number Diff line change
@@ -0,0 +1,33 @@

{{alias}}( start, stop, shape, colexicographic )
Generates a linearly spaced two-dimensional nested numeric array.

The output array is guaranteed to include the `start` and `stop` values.

Parameters
----------
start: number
First array value.

stop: number
Last array value.

shape: Array<integer>
Array shape.

colexicographic: boolean
Specifies whether generated array values should be stored in
colexicographic order.

Returns
-------
out: Array
Linearly spaced two-dimensional nested numeric array.

Examples
--------
> var arr = {{alias}}( 0, 10, [ 2, 3 ], false )
[ [ 0, 2, 4 ], [ 6, 8, 10 ] ]

See Also
--------
Original file line number Diff line number Diff line change
@@ -0,0 +1,49 @@
/*
* @license Apache-2.0
*
* Copyright (c) 2025 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

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

/**
* Two-dimensional nested array.
*/
type Array2D<T> = Array<Array<T>>;

/**
* Generates a linearly spaced two-dimensional nested numeric array.
*
* @param start - first array value
* @param stop - last array value
* @param shape - array shape
* @param colexicographic - specifies whether generated array values should be stored in colexicographic order
* @returns linearly spaced two-dimensional nested numeric array
*
* @example
* var x = linspace2d( 0, 100, [ 2, 3 ], false );
* // returns [ [ 0, 20, 40 ], [ 60, 80, 100 ] ]
*
* x = linspace2d( 0, 100, [ 2, 3 ], true );
* // returns [ [ 0, 40, 80 ], [ 20, 60, 100 ] ]
*/
declare function linspace2d( start: number, stop: number, shape: Shape2D, colexicographic: boolean ): Array2D<number>;


// EXPORTS //

export = linspace2d;
Loading