Skip to content

Commit 73b0b00

Browse files
committed
feat: add array/base/linspace2d
--- type: pre_push_report description: Results of running various checks prior to pushing changes. report: - task: run_javascript_examples status: na - task: run_c_examples status: na - task: run_cpp_examples status: na - task: run_javascript_readme_examples status: na - task: run_c_benchmarks status: na - task: run_cpp_benchmarks status: na - task: run_fortran_benchmarks status: na - task: run_javascript_benchmarks status: na - task: run_julia_benchmarks status: na - task: run_python_benchmarks status: na - task: run_r_benchmarks status: na - task: run_javascript_tests status: na ---
1 parent b6624d0 commit 73b0b00

File tree

11 files changed

+758
-0
lines changed

11 files changed

+758
-0
lines changed
Lines changed: 128 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,128 @@
1+
<!--
2+
3+
@license Apache-2.0
4+
5+
Copyright (c) 2025 The Stdlib Authors.
6+
7+
Licensed under the Apache License, Version 2.0 (the "License");
8+
you may not use this file except in compliance with the License.
9+
You may obtain a copy of the License at
10+
11+
http://www.apache.org/licenses/LICENSE-2.0
12+
13+
Unless required by applicable law or agreed to in writing, software
14+
distributed under the License is distributed on an "AS IS" BASIS,
15+
WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
16+
See the License for the specific language governing permissions and
17+
limitations under the License.
18+
19+
-->
20+
21+
# linspace2d
22+
23+
> Generate a linearly spaced two-dimensional nested numeric array.
24+
25+
<section class="usage">
26+
27+
## Usage
28+
29+
```javascript
30+
var linspace2d = require( '@stdlib/array/base/linspace2d' );
31+
```
32+
33+
#### linspace2d( start, stop, shape, colexicographic )
34+
35+
Generates a linearly spaced two-dimensional nested numeric array.
36+
37+
```javascript
38+
var x = linspace2d( 0, 100, [ 2, 3 ], false );
39+
// returns [ [ 0, 20, 40 ], [ 60, 80, 100 ] ]
40+
41+
x = linspace2d( 0, 100, [ 2, 3 ], true );
42+
// returns [ [ 0, 40, 80 ], [ 20, 60, 100 ] ]
43+
```
44+
45+
</section>
46+
47+
<!-- /.usage -->
48+
49+
<section class="notes">
50+
51+
## Notes
52+
53+
- The function assumes that the product of number of rows and columns is greater than or equal to `2`.
54+
55+
- 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,
56+
57+
```javascript
58+
var arr = linspace2d( 0, 10, [ 2, 2 ], false );
59+
// returns [ [ 0, 3.3333333333333335 ], [ 6.666666666666667, 10 ] ]
60+
```
61+
62+
If you desire more control over element precision, consider using [`roundn`][@stdlib/math/base/special/roundn]:
63+
64+
```javascript
65+
var roundn = require( '@stdlib/math/base/special/roundn' );
66+
67+
// Create an array subject to floating-point rounding errors:
68+
var arr = linspace( 0, 10, [ 3, 3 ], false );
69+
70+
// Round each value to the nearest hundredth:
71+
var i;
72+
var j;
73+
for ( i = 0; i < arr.length; i++ ) {
74+
for ( j = 0; j < arr[i].length; j++ ) {
75+
arr[ i ][ j ] = roundn( arr[ i ][ j ], -2 );
76+
}
77+
}
78+
console.log( arr.join( '\n' ) );
79+
```
80+
81+
</section>
82+
83+
<!-- /.notes -->
84+
85+
<section class="examples">
86+
87+
## Examples
88+
89+
<!-- eslint no-undef: "error" -->
90+
91+
```javascript
92+
var linspace2d = require( '@stdlib/array/base/linspace2d' );
93+
94+
var out = linspace2d( 0, 10, [ 2, 5 ], false );
95+
console.log( out );
96+
97+
out = linspace2d( 0, 10, [ 2, 3 ], true );
98+
console.log( out );
99+
100+
out = linspace2d( 0, 10, [ 4, 2 ], true );
101+
console.log( out );
102+
103+
// Create an array of arrays with decremented values:
104+
out = linspace2d( 10, 0, [ 2, 5 ], false );
105+
console.log( out );
106+
```
107+
108+
</section>
109+
110+
<!-- /.examples -->
111+
112+
<!-- Section for related `stdlib` packages. Do not manually edit this section, as it is automatically populated. -->
113+
114+
<section class="related">
115+
116+
</section>
117+
118+
<!-- /.related -->
119+
120+
<!-- Section for all links. Make sure to keep an empty line after the `section` element and another before the `/section` close. -->
121+
122+
<section class="links">
123+
124+
[@stdlib/math/base/special/roundn]: https://github.com/stdlib-js/stdlib/tree/develop/lib/node_modules/%40stdlib/math/base/special/roundn
125+
126+
</section>
127+
128+
<!-- /.links -->
Lines changed: 48 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,48 @@
1+
/**
2+
* @license Apache-2.0
3+
*
4+
* Copyright (c) 2025 The Stdlib Authors.
5+
*
6+
* Licensed under the Apache License, Version 2.0 (the "License");
7+
* you may not use this file except in compliance with the License.
8+
* You may obtain a copy of the License at
9+
*
10+
* http://www.apache.org/licenses/LICENSE-2.0
11+
*
12+
* Unless required by applicable law or agreed to in writing, software
13+
* distributed under the License is distributed on an "AS IS" BASIS,
14+
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
15+
* See the License for the specific language governing permissions and
16+
* limitations under the License.
17+
*/
18+
19+
'use strict';
20+
21+
// MODULES //
22+
23+
var bench = require( '@stdlib/bench' );
24+
var isArrayArray = require( '@stdlib/assert/is-array-array' );
25+
var pkg = require( './../package.json' ).name;
26+
var linspace2d = require( './../lib' );
27+
28+
29+
// MAIN //
30+
31+
bench( pkg, function benchmark( b ) {
32+
var i;
33+
var v;
34+
35+
b.tic();
36+
for ( i = 0; i < b.iterations; i++ ) {
37+
v = linspace2d( 0.0, 100.0, [ 2, 3 ], false );
38+
if ( typeof v !== 'object' ) {
39+
b.fail( 'should return an array of arrays' );
40+
}
41+
}
42+
b.toc();
43+
if ( !isArrayArray( v ) ) {
44+
b.fail( 'should return an array of arrays' );
45+
}
46+
b.pass( 'benchmark finished' );
47+
b.end();
48+
});
Lines changed: 95 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,95 @@
1+
/**
2+
* @license Apache-2.0
3+
*
4+
* Copyright (c) 2025 The Stdlib Authors.
5+
*
6+
* Licensed under the Apache License, Version 2.0 (the "License");
7+
* you may not use this file except in compliance with the License.
8+
* You may obtain a copy of the License at
9+
*
10+
* http://www.apache.org/licenses/LICENSE-2.0
11+
*
12+
* Unless required by applicable law or agreed to in writing, software
13+
* distributed under the License is distributed on an "AS IS" BASIS,
14+
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
15+
* See the License for the specific language governing permissions and
16+
* limitations under the License.
17+
*/
18+
19+
'use strict';
20+
21+
// MODULES //
22+
23+
var bench = require( '@stdlib/bench' );
24+
var pow = require( '@stdlib/math/base/special/pow' );
25+
var floor = require( '@stdlib/math/base/special/floor' );
26+
var sqrt = require( '@stdlib/math/base/special/sqrt' );
27+
var isArrayArray = require( '@stdlib/assert/is-array-array' );
28+
var pkg = require( './../package.json' ).name;
29+
var linspace2d = require( './../lib' );
30+
31+
32+
// FUNCTIONS //
33+
34+
/**
35+
* Creates a benchmark function.
36+
*
37+
* @private
38+
* @param {PositiveInteger} N - array lengths
39+
* @returns {Function} benchmark function
40+
*/
41+
function createBenchmark( N ) {
42+
return benchmark;
43+
44+
/**
45+
* Benchmark function.
46+
*
47+
* @private
48+
* @param {Benchmark} b - benchmark instance
49+
*/
50+
function benchmark( b ) {
51+
var v;
52+
var i;
53+
54+
b.tic();
55+
for ( i = 0; i < b.iterations; i++ ) {
56+
v = linspace2d( 0.0, 100.0, [ N, N ], false );
57+
if ( typeof v !== 'object' ) {
58+
b.fail( 'should return an array of arrays' );
59+
}
60+
}
61+
b.toc();
62+
if ( !isArrayArray( v ) ) {
63+
b.fail( 'should return an array of arrays' );
64+
}
65+
b.pass( 'benchmark finished' );
66+
b.end();
67+
}
68+
}
69+
70+
71+
// MAIN //
72+
73+
/**
74+
* Main execution sequence.
75+
*
76+
* @private
77+
*/
78+
function main() {
79+
var N;
80+
var min;
81+
var max;
82+
var f;
83+
var i;
84+
85+
min = 1; // 10^min
86+
max = 6; // 10^max
87+
88+
for ( i = min; i <= max; i++ ) {
89+
N = floor( sqrt( pow( 10, i ) ) );
90+
f = createBenchmark( N );
91+
bench( pkg+'::square_matrix:size='+(N*N), f );
92+
}
93+
}
94+
95+
main();
Lines changed: 32 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,32 @@
1+
2+
{{alias}}( start, stop, shape, colexicographic )
3+
Generates a linearly spaced two-dimensional nested numeric array.
4+
5+
The output array is guaranteed to include the `start` and `stop` values.
6+
7+
Parameters
8+
----------
9+
start: number
10+
First array value.
11+
12+
stop: number
13+
Last array value.
14+
15+
shape: Array<integer>
16+
Array shape.
17+
18+
colexicographic: boolean
19+
specifies whether the array values should be in colexicographic order
20+
21+
Returns
22+
-------
23+
out: Array
24+
Linearly spaced two-dimensional nested numeric array.
25+
26+
Examples
27+
--------
28+
> var arr = {{alias}}( 0, 10, [ 2, 3 ], false )
29+
[ [ 0, 2, 4 ], [ 6, 8, 10 ] ]
30+
31+
See Also
32+
--------
Lines changed: 51 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,51 @@
1+
/*
2+
* @license Apache-2.0
3+
*
4+
* Copyright (c) 2025 The Stdlib Authors.
5+
*
6+
* Licensed under the Apache License, Version 2.0 (the "License");
7+
* you may not use this file except in compliance with the License.
8+
* You may obtain a copy of the License at
9+
*
10+
* http://www.apache.org/licenses/LICENSE-2.0
11+
*
12+
* Unless required by applicable law or agreed to in writing, software
13+
* distributed under the License is distributed on an "AS IS" BASIS,
14+
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
15+
* See the License for the specific language governing permissions and
16+
* limitations under the License.
17+
*/
18+
19+
// TypeScript Version: 4.1
20+
21+
import { Shape2D } from '@stdlib/types/ndarray';
22+
23+
/**
24+
* Two-dimensional nested array.
25+
*/
26+
type Array2D<T> = Array<Array<T>>;
27+
28+
/**
29+
* Generates a linearly spaced two-dimensional nested numeric array.
30+
*
31+
* @param start - first array value
32+
* @param stop - last array value
33+
* @param {NonNegativeIntegerArray} shape - array shape
34+
* @param {boolean} colexicographic - specifies whether the array values should be in colexicographic order
35+
* @returns {Array} linearly spaced numeric array
36+
*
37+
* @example
38+
* var linspace2d = require( '@stdlib/array/base/linspace2d' );
39+
*
40+
* var x = linspace2d( 0, 100, [ 2, 3 ], false );
41+
* // returns [ [ 0, 20, 40 ], [ 60, 80, 100 ] ]
42+
43+
* x = linspace2d( 0, 100, [ 2, 3 ], true );
44+
* // returns [ [ 0, 40, 80 ], [ 20, 60, 100 ] ]
45+
*/
46+
declare function linspace2d( start: number, end: number, shape: Shape2D, colexicographic: boolean ): Array2D<number>;
47+
48+
49+
// EXPORTS //
50+
51+
export = linspace2d;

0 commit comments

Comments
 (0)