README.md (5742B)
1 <!-- 2 3 @license Apache-2.0 4 5 Copyright (c) 2020 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 # sqrt 22 23 > Compute the principal [square root][square-root] of each element in a strided array. 24 25 <section class="intro"> 26 27 The principal [square root][square-root] is defined as 28 29 <!-- <equation class="equation" label="eq:principal_square_root" align="center" raw="\sqrt{x^2} = \begin{matrix} x, & \textrm{if}\ x \geq 0\end{matrix}" alt="Principal square root"> --> 30 31 <div class="equation" align="center" data-raw-text="\sqrt{x^2} = \begin{matrix} x, & \textrm{if}\ x \geq 0\end{matrix}" data-equation="eq:principal_square_root"> 32 <img src="https://cdn.jsdelivr.net/gh/stdlib-js/stdlib@5de0e834d1b5b074d6df210b9cb492c75c9953a2/lib/node_modules/@stdlib/math/strided/special/sqrt/docs/img/equation_principal_square_root.svg" alt="Principal square root"> 33 <br> 34 </div> 35 36 <!-- </equation> --> 37 38 </section> 39 40 <!-- /.intro --> 41 42 <section class="usage"> 43 44 ## Usage 45 46 ```javascript 47 var sqrt = require( '@stdlib/math/strided/special/sqrt' ); 48 ``` 49 50 #### sqrt( N, x, strideX, y, strideY ) 51 52 Computes the principal [square root][square-root] of each element in a strided array `x` and assigns the results to elements in a strided array `y`. 53 54 ```javascript 55 var Float64Array = require( '@stdlib/array/float64' ); 56 57 var x = new Float64Array( [ 0.0, 4.0, 9.0, 12.0, 24.0 ] ); 58 59 // Perform operation in-place: 60 sqrt( x.length, x, 1, x, 1 ); 61 // x => <Float64Array>[ 0.0, 2.0, 3.0, ~3.464, ~4.899 ] 62 ``` 63 64 The function accepts the following arguments: 65 66 - **N**: number of indexed elements. 67 - **x**: input array-like object. 68 - **strideX**: index increment for `x`. 69 - **y**: output array-like object. 70 - **strideY**: index increment for `y`. 71 72 The `N` and `stride` parameters determine which elements in `x` and `y` are accessed at runtime. For example, to index every other value in `x` and the first `N` elements of `y` in reverse order, 73 74 ```javascript 75 var Float64Array = require( '@stdlib/array/float64' ); 76 77 var x = new Float64Array( [ 0.0, 4.0, 9.0, 12.0, 24.0, 64.0 ] ); 78 var y = new Float64Array( [ 0.0, 0.0, 0.0, 0.0, 0.0, 0.0 ] ); 79 80 sqrt( 3, x, 2, y, -1 ); 81 // y => <Float64Array>[ ~4.899, 3.0, 0.0, 0.0, 0.0, 0.0 ] 82 ``` 83 84 Note that indexing is relative to the first index. To introduce an offset, use [`typed array`][mdn-typed-array] views. 85 86 ```javascript 87 var Float64Array = require( '@stdlib/array/float64' ); 88 89 // Initial arrays... 90 var x0 = new Float64Array( [ 0.0, 4.0, 9.0, 12.0, 24.0, 64.0 ] ); 91 var y0 = new Float64Array( [ 0.0, 0.0, 0.0, 0.0, 0.0, 0.0 ] ); 92 93 // Create offset views... 94 var x1 = new Float64Array( x0.buffer, x0.BYTES_PER_ELEMENT*1 ); // start at 2nd element 95 var y1 = new Float64Array( y0.buffer, y0.BYTES_PER_ELEMENT*3 ); // start at 4th element 96 97 sqrt( 3, x1, -2, y1, 1 ); 98 // y0 => <Float64Array>[ 0.0, 0.0, 0.0, 8.0, ~3.464, 2.0 ] 99 ``` 100 101 #### sqrt.ndarray( N, x, strideX, offsetX, y, strideY, offsetY ) 102 103 Computes the principal [square root][square-root] of each element in a strided array `x` and assigns the results to elements in a strided array `y` using alternative indexing semantics. 104 105 ```javascript 106 var Float64Array = require( '@stdlib/array/float64' ); 107 108 var x = new Float64Array( [ 0.0, 4.0, 9.0, 12.0, 24.0 ] ); 109 var y = new Float64Array( [ 0.0, 0.0, 0.0, 0.0, 0.0 ] ); 110 111 sqrt.ndarray( x.length, x, 1, 0, y, 1, 0 ); 112 // y => <Float64Array>[ 0.0, 2.0, 3.0, ~3.464, ~4.899 ] 113 ``` 114 115 The function accepts the following additional arguments: 116 117 - **offsetX**: starting index for `x`. 118 - **offsetY**: starting index for `y`. 119 120 While [`typed array`][mdn-typed-array] views mandate a view offset based on the underlying `buffer`, the `offsetX` and `offsetY` parameters support indexing semantics based on starting indices. For example, to index every other value in `x` starting from the second value and to index the last `N` elements in `y`, 121 122 ```javascript 123 var Float64Array = require( '@stdlib/array/float64' ); 124 125 var x = new Float64Array( [ 0.0, 4.0, 9.0, 12.0, 24.0, 64.0 ] ); 126 var y = new Float64Array( [ 0.0, 0.0, 0.0, 0.0, 0.0, 0.0 ] ); 127 128 sqrt.ndarray( 3, x, 2, 1, y, -1, y.length-1 ); 129 // y => <Float64Array>[ 0.0, 0.0, 0.0, 8.0, ~3.464, 2.0 ] 130 ``` 131 132 </section> 133 134 <!-- /.usage --> 135 136 <section class="notes"> 137 138 </section> 139 140 <!-- /.notes --> 141 142 <section class="examples"> 143 144 ## Examples 145 146 <!-- eslint no-undef: "error" --> 147 148 ```javascript 149 var uniform = require( '@stdlib/random/base/uniform' ).factory; 150 var filledarray = require( '@stdlib/array/filled' ); 151 var dtypes = require( '@stdlib/array/dtypes' ); 152 var gfillBy = require( '@stdlib/blas/ext/base/gfill-by' ); 153 var sqrt = require( '@stdlib/math/strided/special/sqrt' ); 154 155 var dt; 156 var x; 157 var y; 158 var i; 159 160 dt = dtypes(); 161 for ( i = 0; i < dt.length; i++ ) { 162 x = filledarray( 0.0, 10, dt[ i ] ); 163 gfillBy( x.length, x, 1, uniform( 0.0, 100.0 ) ); 164 console.log( x ); 165 166 y = filledarray( 0.0, x.length, 'generic' ); 167 console.log( y ); 168 169 sqrt.ndarray( x.length, x, 1, 0, y, -1, y.length-1 ); 170 console.log( y ); 171 console.log( '' ); 172 } 173 ``` 174 175 </section> 176 177 <!-- /.examples --> 178 179 <section class="links"> 180 181 [mdn-typed-array]: https://developer.mozilla.org/en-US/docs/Web/JavaScript/Reference/Global_Objects/TypedArray 182 183 [square-root]: https://en.wikipedia.org/wiki/Square_root 184 185 </section> 186 187 <!-- /.links -->