|
| 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="" 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 ) |
| 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. |
| 66 | + |
| 67 | +If provided an [`ndarray`][@stdlib/ndarray/ctor] or array-like object, the function computes the [absolute value][absolute-value] for each element and returns an [`ndarray`][@stdlib/ndarray/ctor] having the same shape as `x` and containing element-wise results. |
| 68 | + |
| 69 | +```javascript |
| 70 | +var Float64Array = require( '@stdlib/array/float64' ); |
| 71 | +var array = require( '@stdlib/ndarray/array' ); |
| 72 | + |
| 73 | +// Provide a typed array... |
| 74 | +var x = new Float64Array( [ -1.0, -2.0 ] ); |
| 75 | +var y = abs( x ); |
| 76 | +// returns <ndarray> |
| 77 | + |
| 78 | +var v = y.data; |
| 79 | +// returns <Float64Array>[ 1.0, 2.0 ] |
| 80 | + |
| 81 | +// Provide an ndarray... |
| 82 | +x = array( [ [ -1.0, -2.0 ], [ -3.0, -4.0 ] ] ); // 2x2 |
| 83 | +y = abs( x ); |
| 84 | +// returns <ndarray> |
| 85 | + |
| 86 | +v = y.get( 0, 1 ); |
| 87 | +// returns 2.0 |
| 88 | +``` |
| 89 | + |
| 90 | +</section> |
| 91 | + |
| 92 | +<!-- /.usage --> |
| 93 | + |
| 94 | +<!-- Package usage notes. Make sure to keep an empty line after the `section` element and another before the `/section` close. --> |
| 95 | + |
| 96 | +<section class="notes"> |
| 97 | + |
| 98 | +</section> |
| 99 | + |
| 100 | +<!-- /.notes --> |
| 101 | + |
| 102 | +<!-- Package usage examples. --> |
| 103 | + |
| 104 | +<section class="examples"> |
| 105 | + |
| 106 | +## Examples |
| 107 | + |
| 108 | +<!-- eslint no-undef: "error" --> |
| 109 | + |
| 110 | +```javascript |
| 111 | +var Float64Array = require( '@stdlib/array/float64' ); |
| 112 | +var array = require( '@stdlib/ndarray/array' ); |
| 113 | +var ind2sub = require( '@stdlib/ndarray/ind2sub' ); |
| 114 | +var abs = require( '@stdlib/math/special/abs' ); |
| 115 | + |
| 116 | +var sub; |
| 117 | +var sh; |
| 118 | +var x; |
| 119 | +var y; |
| 120 | +var i; |
| 121 | + |
| 122 | +// Provide a number... |
| 123 | +y = abs( -1.0 ); |
| 124 | +console.log( 'x = %d => abs(x) = %d', -1.0, y ); |
| 125 | + |
| 126 | +// Provide an array-like object... |
| 127 | +x = new Float64Array( [ -1.0, -2.0, -3.0 ] ); |
| 128 | +y = abs( x ); |
| 129 | +for ( i = 0; i < x.length; i++ ) { |
| 130 | + console.log( 'x_%d = %d => abs(x_%d) = %d', i, x[ i ], i, y.iget( i ) ); |
| 131 | +} |
| 132 | + |
| 133 | +// Provide an ndarray... |
| 134 | +x = array( [ [ -1.0, -2.0 ], [ -3.0, -4.0 ] ] ); |
| 135 | +sh = x.shape; |
| 136 | + |
| 137 | +y = abs( x ); |
| 138 | +for ( i = 0; i < x.length; i++ ) { |
| 139 | + sub = ind2sub( sh, i ); |
| 140 | + 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 ) ); |
| 141 | +} |
| 142 | +``` |
| 143 | + |
| 144 | +</section> |
| 145 | + |
| 146 | +<!-- /.examples --> |
| 147 | + |
| 148 | +<!-- 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. --> |
| 149 | + |
| 150 | +<section class="references"> |
| 151 | + |
| 152 | +</section> |
| 153 | + |
| 154 | +<!-- /.references --> |
| 155 | + |
| 156 | +<!-- Section for all links. Make sure to keep an empty line after the `section` element and another before the `/section` close. --> |
| 157 | + |
| 158 | +<section class="links"> |
| 159 | + |
| 160 | +[absolute-value]: https://en.wikipedia.org/wiki/Absolute_value |
| 161 | + |
| 162 | +[@stdlib/ndarray/ctor]: https://github.com/stdlib-js/stdlib |
| 163 | + |
| 164 | +</section> |
| 165 | + |
| 166 | +<!-- /.links --> |
0 commit comments