README.md (6744B)
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 # abs 22 23 > Compute the [absolute value][absolute-value]. 24 25 <!-- Section to include introductory text. Make sure to keep an empty line after the intro `section` element and another before the `/section` close. --> 26 27 <section class="intro"> 28 29 The [absolute value][absolute-value] is defined as 30 31 <!-- <equation class="equation" label="eq:absolute_value" align="center" raw="|x| = \begin{cases} x & \textrm{if}\ x \geq 0 \\ -x & \textrm{if}\ x < 0\end{cases}" alt="Absolute value"> --> 32 33 <div class="equation" align="center" data-raw-text="|x| = \begin{cases} x & \textrm{if}\ x \geq 0 \\ -x & \textrm{if}\ x < 0\end{cases}" data-equation="eq:absolute_value"> 34 <img src="https://cdn.jsdelivr.net/gh/stdlib-js/stdlib@61c5f878886bd5b3b98976501c974bf69f575238/lib/node_modules/@stdlib/math/special/abs/docs/img/equation_absolute_value.svg" alt="Absolute value"> 35 <br> 36 </div> 37 38 <!-- </equation> --> 39 40 </section> 41 42 <!-- /.intro --> 43 44 <!-- Package usage documentation. --> 45 46 <section class="usage"> 47 48 ## Usage 49 50 ```javascript 51 var abs = require( '@stdlib/math/special/abs' ); 52 ``` 53 54 #### abs( x\[, options] ) 55 56 Computes the [absolute value][absolute-value]. 57 58 ```javascript 59 var y = abs( -1.0 ); 60 // returns 1.0 61 ``` 62 63 The function accepts the following arguments: 64 65 - **x**: input [`ndarray`][@stdlib/ndarray/ctor], array-like object, or number. If provided an [`ndarray`][@stdlib/ndarray/ctor] or array-like object, the function performs element-wise computation. 66 - **options**: function options. 67 68 If provided an [`ndarray`][@stdlib/ndarray/ctor], the function returns an [`ndarray`][@stdlib/ndarray/ctor] having the same shape and data type as `x`. 69 70 ```javascript 71 var array = require( '@stdlib/ndarray/array' ); 72 73 var x = array( [ [ -1.0, -2.0 ], [ -3.0, -4.0 ] ] ); // 2x2 74 var y = abs( x ); 75 // returns <ndarray> 76 77 var v = y.get( 0, 1 ); 78 // returns 2.0 79 ``` 80 81 If provided an array-like object, the function returns an array-like object having the same length and data type as `x`. 82 83 ```javascript 84 var Float64Array = require( '@stdlib/array/float64' ); 85 86 var x = new Float64Array( [ -1.0, -2.0 ] ); 87 var y = abs( x ); 88 // returns <Float64Array>[ 1.0, 2.0 ] 89 90 x = [ -1.0, -2.0 ]; 91 y = abs( x ); 92 // returns [ 1.0, 2.0 ] 93 ``` 94 95 The function accepts the following `options`: 96 97 - **dtype**: output array [data type][@stdlib/ndarray/dtypes]. Only applicable when `x` is either an [`ndarray`][@stdlib/ndarray/ctor] or array-like object. By default, the output array data type is inferred from the input array. 98 - **order**: output array [order][@stdlib/ndarray/orders]. Only applicable when `x` is an [`ndarray`][@stdlib/ndarray/ctor]. By default, the output array order is inferred from the input array. 99 100 By default, when provided either an [`ndarray`][@stdlib/ndarray/ctor] or an array-like object, the function returns an object of the same "kind" (either an [`ndarray`][@stdlib/ndarray/ctor] or array-like object, respectively) having the same underlying [data type][@stdlib/ndarray/dtypes]. To specify a different output array [data type][@stdlib/ndarray/dtypes], set the `dtype` option. 101 102 ```javascript 103 var Float32Array = require( '@stdlib/array/float32' ); 104 105 var x = new Float32Array( [ -1.0, -2.0 ] ); 106 var y = abs( x ); 107 // returns <Float32Array>[ 1.0, 2.0 ] 108 109 x = new Float32Array( [ -1.0, -2.0 ] ); 110 y = abs( x, { 111 'dtype': 'float64' 112 }); 113 // returns <Float64Array>[ 1.0, 2.0 ] 114 ``` 115 116 #### abs.assign( x, y ) 117 118 Computes the [absolute value][absolute-value] and assigns results to a provided output array. 119 120 ```javascript 121 var array = require( '@stdlib/ndarray/array' ); 122 123 var x = array( [ [ -1.0, -2.0 ], [ -3.0, -4.0 ] ] ); // 2x2 124 var y = array( [ [ 0.0, 0.0 ], [ 0.0, 0.0 ] ] ); // 2x2 125 var out = abs.assign( x, y ); 126 // returns <ndarray> 127 128 var bool = ( out === y ); 129 // returns true 130 131 var v = y.get( 0, 1 ); 132 // returns 2.0 133 ``` 134 135 The output array must be the same data "kind" (i.e., [`ndarray`][@stdlib/ndarray/ctor] or array-like object) as the input array. For example, if `x` is an [`ndarray`][@stdlib/ndarray/ctor], `y` must also be an [`ndarray`][@stdlib/ndarray/ctor]. Similarly, if `x` is an array-like object, `y` must also be an array-like object. 136 137 TODO: broadcasting discussion and example(s). 138 139 </section> 140 141 <!-- /.usage --> 142 143 <!-- Package usage notes. Make sure to keep an empty line after the `section` element and another before the `/section` close. --> 144 145 <section class="notes"> 146 147 </section> 148 149 <!-- /.notes --> 150 151 <!-- Package usage examples. --> 152 153 <section class="examples"> 154 155 ## Examples 156 157 <!-- eslint no-undef: "error" --> 158 159 ```javascript 160 var Float64Array = require( '@stdlib/array/float64' ); 161 var array = require( '@stdlib/ndarray/array' ); 162 var ind2sub = require( '@stdlib/ndarray/ind2sub' ); 163 var abs = require( '@stdlib/math/special/abs' ); 164 165 // Provide a number... 166 var v = abs( -1.0 ); 167 console.log( 'x = %d => abs(x) = %d', -1.0, v ); 168 169 // Provide an array-like object... 170 var x = new Float64Array( [ -1.0, -2.0, -3.0 ] ); 171 var y = abs( x ); 172 173 var i; 174 for ( i = 0; i < x.length; i++ ) { 175 console.log( 'x_%d = %d => abs(x_%d) = %d', i, x[ i ], i, y[ i ] ); 176 } 177 178 // Provide an ndarray... 179 x = array( [ [ -1.0, -2.0 ], [ -3.0, -4.0 ] ] ); 180 y = abs( x ); 181 182 var sh = x.shape; 183 var sub; 184 for ( i = 0; i < x.length; i++ ) { 185 sub = ind2sub( sh, i ); 186 console.log( 'x_%d%d = %d => abs(x_%d%d) = %d', sub[ 0 ], sub[ 1 ], x.iget( i ), sub[ 0 ], sub[ 1 ], y.iget( i ) ); 187 } 188 ``` 189 190 </section> 191 192 <!-- /.examples --> 193 194 <!-- Section to include cited references. If references are included, add a horizontal rule *before* the section. Make sure to keep an empty line after the `section` element and another before the `/section` close. --> 195 196 <section class="references"> 197 198 </section> 199 200 <!-- /.references --> 201 202 <!-- Section for all links. Make sure to keep an empty line after the `section` element and another before the `/section` close. --> 203 204 <section class="links"> 205 206 [absolute-value]: https://en.wikipedia.org/wiki/Absolute_value 207 208 [@stdlib/ndarray/ctor]: https://www.npmjs.com/package/@stdlib/ndarray-ctor 209 210 [@stdlib/ndarray/orders]: https://www.npmjs.com/package/@stdlib/ndarray-orders 211 212 [@stdlib/ndarray/dtypes]: https://www.npmjs.com/package/@stdlib/ndarray-dtypes 213 214 </section> 215 216 <!-- /.links -->