|
| 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 | +The function accepts the following arguments: |
| 46 | + |
| 47 | +- **start**: first array value. |
| 48 | +- **stop**: last array value. |
| 49 | +- **shape**: array shape. |
| 50 | +- **colexicographic**: specifies whether generated array values should be stored in colexicographic order. |
| 51 | + |
| 52 | +</section> |
| 53 | + |
| 54 | +<!-- /.usage --> |
| 55 | + |
| 56 | +<section class="notes"> |
| 57 | + |
| 58 | +## Notes |
| 59 | + |
| 60 | +- 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, |
| 61 | + |
| 62 | + ```javascript |
| 63 | + var arr = linspace2d( 0, 1, [ 1, 3 ], false ); |
| 64 | + // returns [ [ 0, ~0.5, 1 ] ] |
| 65 | + ``` |
| 66 | + |
| 67 | + 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]: |
| 68 | + |
| 69 | + ```javascript |
| 70 | + var roundn = require( '@stdlib/math/base/special/roundn' ); |
| 71 | + var map2d = require( '@stdlib/array/base/map2d' ); |
| 72 | +
|
| 73 | + var arr = linspace2d( 0, 1, [ 1, 3 ], false ); |
| 74 | + // returns [ [ 0, ~0.5, 1 ] ] |
| 75 | +
|
| 76 | + // Round each value to the nearest tenth: |
| 77 | + var out = map2d( arr, [ 1, 3 ], clbk ); |
| 78 | + // returns [ [ 0, 0.5, 1 ] ] |
| 79 | +
|
| 80 | + function clbk( v ) { |
| 81 | + return roundn( v, -1 ); |
| 82 | + } |
| 83 | + ``` |
| 84 | + |
| 85 | +</section> |
| 86 | + |
| 87 | +<!-- /.notes --> |
| 88 | + |
| 89 | +<section class="examples"> |
| 90 | + |
| 91 | +## Examples |
| 92 | + |
| 93 | +<!-- eslint no-undef: "error" --> |
| 94 | + |
| 95 | +```javascript |
| 96 | +var linspace2d = require( '@stdlib/array/base/linspace2d' ); |
| 97 | +
|
| 98 | +var out = linspace2d( 0, 10, [ 2, 5 ], false ); |
| 99 | +console.log( out ); |
| 100 | +
|
| 101 | +out = linspace2d( 0, 10, [ 2, 3 ], true ); |
| 102 | +console.log( out ); |
| 103 | +
|
| 104 | +out = linspace2d( 0, 10, [ 4, 2 ], true ); |
| 105 | +console.log( out ); |
| 106 | +
|
| 107 | +// Create an array of arrays with decremented values: |
| 108 | +out = linspace2d( 10, 0, [ 2, 5 ], false ); |
| 109 | +console.log( out ); |
| 110 | +``` |
| 111 | + |
| 112 | +</section> |
| 113 | + |
| 114 | +<!-- /.examples --> |
| 115 | + |
| 116 | +<!-- Section for related `stdlib` packages. Do not manually edit this section, as it is automatically populated. --> |
| 117 | + |
| 118 | +<section class="related"> |
| 119 | + |
| 120 | +</section> |
| 121 | + |
| 122 | +<!-- /.related --> |
| 123 | + |
| 124 | +<!-- Section for all links. Make sure to keep an empty line after the `section` element and another before the `/section` close. --> |
| 125 | + |
| 126 | +<section class="links"> |
| 127 | + |
| 128 | +[@stdlib/math/base/special/roundn]: https://github.com/stdlib-js/math-base-special-roundn |
| 129 | + |
| 130 | +</section> |
| 131 | + |
| 132 | +<!-- /.links --> |
0 commit comments