-
-
Notifications
You must be signed in to change notification settings - Fork 903
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
Changes from 4 commits
Commits
Show all changes
16 commits
Select commit
Hold shift + click to select a range
73b0b00
feat: add array/base/linspace2d
headlessNode 19f11f5
fix: idx and docs
headlessNode 7c179b1
docs: apply suggestions from code review
headlessNode 159c7fb
docs: apply suggestions from code review
headlessNode cf48e17
fix: apply suggestions from code review
headlessNode 4358c3e
docs: apply suggestions from code review
headlessNode 5804066
refactor: remove the extra loop
headlessNode c72a036
refactor: optimize the function
headlessNode fdc9e73
Merge remote-tracking branch 'upstream/develop' into linspace2d-array…
stdlib-bot dd68407
docs: refactor readme
headlessNode 254a017
refactor: apply suggestions from code review
headlessNode 6fdf82f
refactor: make single pass
headlessNode 237c3d1
Apply suggestions from code review
kgryte 093808f
refactor: create the output array in a single pass
kgryte 6a8dac4
docs: demonstrate floating-point rounding error
kgryte 6cb2196
docs: remove note
kgryte File filter
Filter by extension
Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
There are no files selected for viewing
128 changes: 128 additions & 0 deletions
128
lib/node_modules/@stdlib/array/base/linspace2d/README.md
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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`. | ||
|
||
kgryte marked this conversation as resolved.
Outdated
Show resolved
Hide resolved
|
||
- 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, | ||
kgryte marked this conversation as resolved.
Outdated
Show resolved
Hide resolved
|
||
|
||
```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; | ||
headlessNode marked this conversation as resolved.
Outdated
Show resolved
Hide resolved
|
||
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 --> |
48 changes: 48 additions & 0 deletions
48
lib/node_modules/@stdlib/array/base/linspace2d/benchmark/benchmark.js
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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(); | ||
}); |
95 changes: 95 additions & 0 deletions
95
lib/node_modules/@stdlib/array/base/linspace2d/benchmark/benchmark.size.js
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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 N; | ||
var min; | ||
var max; | ||
var f; | ||
var i; | ||
headlessNode marked this conversation as resolved.
Outdated
Show resolved
Hide resolved
|
||
|
||
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
32
lib/node_modules/@stdlib/array/base/linspace2d/docs/repl.txt
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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 | ||
headlessNode marked this conversation as resolved.
Outdated
Show resolved
Hide resolved
|
||
|
||
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 | ||
-------- |
51 changes: 51 additions & 0 deletions
51
lib/node_modules/@stdlib/array/base/linspace2d/docs/types/index.d.ts
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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 | ||
headlessNode marked this conversation as resolved.
Outdated
Show resolved
Hide resolved
|
||
* | ||
* @example | ||
* var linspace2d = require( '@stdlib/array/base/linspace2d' ); | ||
* | ||
headlessNode marked this conversation as resolved.
Outdated
Show resolved
Hide resolved
|
||
* 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; |
Oops, something went wrong.
Oops, something went wrong.
Add this suggestion to a batch that can be applied as a single commit.
This suggestion is invalid because no changes were made to the code.
Suggestions cannot be applied while the pull request is closed.
Suggestions cannot be applied while viewing a subset of changes.
Only one suggestion per line can be applied in a batch.
Add this suggestion to a batch that can be applied as a single commit.
Applying suggestions on deleted lines is not supported.
You must change the existing code in this line in order to create a valid suggestion.
Outdated suggestions cannot be applied.
This suggestion has been applied or marked resolved.
Suggestions cannot be applied from pending reviews.
Suggestions cannot be applied on multi-line comments.
Suggestions cannot be applied while the pull request is queued to merge.
Suggestion cannot be applied right now. Please check back later.
Uh oh!
There was an error while loading. Please reload this page.