README.md (2637B)
1 <!-- 2 3 @license Apache-2.0 4 5 Copyright (c) 2018 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 # Linspace 22 23 > Generate a linearly spaced numeric array. 24 25 <section class="usage"> 26 27 ## Usage 28 29 ```javascript 30 var linspace = require( '@stdlib/array/linspace' ); 31 ``` 32 33 #### linspace( start, stop\[, length] ) 34 35 Generates a linearly spaced numeric `array`. If a `length` is not provided, the default output `array` length is `100`. 36 37 ```javascript 38 var arr = linspace( 0, 100, 6 ); 39 // returns [ 0, 20, 40, 60, 80, 100 ] 40 ``` 41 42 </section> 43 44 <!-- /.usage --> 45 46 <section class="notes"> 47 48 ## Notes 49 50 - The output `array` is guaranteed to include the `start` and `stop` values. Beware, however, that values between the `start` and `stop` are subject to floating-point errors. Hence, 51 52 ```javascript 53 var arr = linspace( 0, 1, 3 ); 54 // returns [ 0, ~0.5, 1 ] 55 ``` 56 57 where `arr[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]: 58 59 ```javascript 60 var roundn = require( '@stdlib/math/base/special/roundn' ); 61 62 // Create an array subject to floating-point errors: 63 var arr = linspace( 0, 1, 21 ); 64 65 // Round each value to the nearest hundredth: 66 var out = []; 67 var i; 68 for ( i = 0; i < arr.length; i++ ) { 69 out.push( roundn( arr[ i ], -2 ) ); 70 } 71 72 console.log( out.join( '\n' ) ); 73 ``` 74 75 </section> 76 77 <!-- /.notes --> 78 79 <section class="examples"> 80 81 ## Examples 82 83 <!-- eslint no-undef: "error" --> 84 85 ```javascript 86 var linspace = require( '@stdlib/array/linspace' ); 87 var out; 88 89 // Default behavior: 90 out = linspace( 0, 10 ); 91 console.log( out.join( '\n' ) ); 92 93 // Specify length: 94 out = linspace( 0, 10, 10 ); 95 console.log( out.join( '\n' ) ); 96 97 out = linspace( 0, 10, 11 ); 98 console.log( out.join( '\n' ) ); 99 100 // Create an array with decremented values: 101 out = linspace( 10, 0, 11 ); 102 console.log( out.join( '\n' ) ); 103 ``` 104 105 </section> 106 107 <!-- /.examples --> 108 109 <section class="links"> 110 111 [@stdlib/math/base/special/roundn]: https://www.npmjs.com/package/@stdlib/math-base-special-roundn 112 113 </section> 114 115 <!-- /.links -->