Skip to content
Merged
Show file tree
Hide file tree
Changes from 4 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
128 changes: 128 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,128 @@
<!--

@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 ] ]
```

</section>

<!-- /.usage -->

<section class="notes">

## Notes

- The function assumes that the product of the number of rows and columns is greater than or equal to `2`.

- 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, 10, [ 2, 2 ], false );
// returns [ [ 0, 3.3333333333333335 ], [ 6.666666666666667, 10 ] ]
```

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' );

// Create an array subject to floating-point rounding errors:
var arr = linspace( 0, 10, [ 3, 3 ], false );

// Round each value to the nearest hundredth:
var i;
var j;
for ( i = 0; i < arr.length; i++ ) {
for ( j = 0; j < arr[i].length; j++ ) {
arr[ i ][ j ] = roundn( arr[ i ][ j ], -2 );
}
}
console.log( arr.join( '\n' ) );
```

</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() {

Check failure on line 78 in lib/node_modules/@stdlib/array/base/linspace2d/benchmark/benchmark.size.js

View workflow job for this annotation

GitHub Actions / Lint Changed Files

Variable declarations inside of function are not ordered by length (in decreasing order)
var N;
var min;
var max;
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();
32 changes: 32 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,32 @@

{{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 the array values should be 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,51 @@
/*
* @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 the array values should be in colexicographic order
* @returns linearly spaced numeric array
*
* @example
* var linspace2d = require( '@stdlib/array/base/linspace2d' );
*
* 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
Loading