Skip to content

stdlib-js/math-special-abs

About stdlib...

We believe in a future in which the web is a preferred environment for numerical computation. To help realize this future, we've built stdlib. stdlib is a standard library, with an emphasis on numerical and scientific computation, written in JavaScript (and C) for execution in browsers and in Node.js.

The library is fully decomposable, being architected in such a way that you can swap out and mix and match APIs and functionality to cater to your exact preferences and use cases.

When you use stdlib, you can be absolutely certain that you are using the most thorough, rigorous, well-written, studied, documented, tested, measured, and high-quality code out there.

To join us in bringing numerical computing to the web, get started by checking us out on GitHub, and please consider financially supporting stdlib. We greatly appreciate your continued support!

abs

NPM version Build Status Coverage Status

Compute the absolute value.

The absolute value is defined as

$$|x| = \begin{cases} x & \textrm{if}\ x \geq 0 \\ -x & \textrm{if}\ x < 0\end{cases}$$

Installation

npm install @stdlib/math-special-abs

Alternatively,

  • To load the package in a website via a script tag without installation and bundlers, use the ES Module available on the esm branch (see README).
  • If you are using Deno, visit the deno branch (see README for usage intructions).
  • For use in Observable, or in browser/node environments, use the Universal Module Definition (UMD) build available on the umd branch (see README).

The branches.md file summarizes the available branches and displays a diagram illustrating their relationships.

To view installation and usage instructions specific to each branch build, be sure to explicitly navigate to the respective README files on each branch, as linked to above.

Usage

var abs = require( '@stdlib/math-special-abs' );

abs( x[, options] )

Computes the absolute value.

var y = abs( -1.0 );
// returns 1.0

The function accepts the following arguments:

  • x: input ndarray, array-like object, or number. If provided an ndarray or array-like object, the function performs element-wise computation.
  • options: function options.

If provided an ndarray, the function returns an ndarray having the same shape and data type as x.

var array = require( '@stdlib/ndarray-array' );

var x = array( [ [ -1.0, -2.0 ], [ -3.0, -4.0 ] ] ); // 2x2
var y = abs( x );
// returns <ndarray>

var v = y.get( 0, 1 );
// returns 2.0

If provided an array-like object, the function returns an array-like object having the same length and data type as x.

var Float64Array = require( '@stdlib/array-float64' );

var x = new Float64Array( [ -1.0, -2.0 ] );
var y = abs( x );
// returns <Float64Array>[ 1.0, 2.0 ]

x = [ -1.0, -2.0 ];
y = abs( x );
// returns [ 1.0, 2.0 ]

The function accepts the following options:

  • dtype: output array data type. Only applicable when x is either an ndarray or array-like object. By default, the output array data type is inferred from the input array.
  • order: output array order. Only applicable when x is an ndarray. By default, the output array order is inferred from the input array.

By default, when provided either an ndarray or an array-like object, the function returns an object of the same "kind" (either an ndarray or array-like object, respectively) having the same underlying data type. To specify a different output array data type, set the dtype option.

var Float32Array = require( '@stdlib/array-float32' );

var x = new Float32Array( [ -1.0, -2.0 ] );
var y = abs( x );
// returns <Float32Array>[ 1.0, 2.0 ]

x = new Float32Array( [ -1.0, -2.0 ] );
y = abs( x, {
    'dtype': 'float64'
});
// returns <Float64Array>[ 1.0, 2.0 ]

abs.assign( x, y )

Computes the absolute value and assigns results to a provided output array.

var array = require( '@stdlib/ndarray-array' );

var x = array( [ [ -1.0, -2.0 ], [ -3.0, -4.0 ] ] ); // 2x2
var y = array( [ [ 0.0, 0.0 ], [ 0.0, 0.0 ] ] ); // 2x2
var out = abs.assign( x, y );
// returns <ndarray>

var bool = ( out === y );
// returns true

var v = y.get( 0, 1 );
// returns 2.0

The output array must be the same data "kind" (i.e., ndarray or array-like object) as the input array. For example, if x is an ndarray, y must also be an ndarray. Similarly, if x is an array-like object, y must also be an array-like object.

TODO: broadcasting discussion and example(s).

Examples

var Float64Array = require( '@stdlib/array-float64' );
var array = require( '@stdlib/ndarray-array' );
var ind2sub = require( '@stdlib/ndarray-ind2sub' );
var abs = require( '@stdlib/math-special-abs' );

// Provide a number...
var v = abs( -1.0 );
console.log( 'x = %d => abs(x) = %d', -1.0, v );

// Provide an array-like object...
var x = new Float64Array( [ -1.0, -2.0, -3.0 ] );
var y = abs( x );

var i;
for ( i = 0; i < x.length; i++ ) {
    console.log( 'x_%d = %d => abs(x_%d) = %d', i, x[ i ], i, y[ i ] );
}

// Provide an ndarray...
x = array( [ [ -1.0, -2.0 ], [ -3.0, -4.0 ] ] );
y = abs( x );

var sh = x.shape;
var sub;
for ( i = 0; i < x.length; i++ ) {
    sub = ind2sub( sh, i );
    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 ) );
}

Notice

This package is part of stdlib, a standard library for JavaScript and Node.js, with an emphasis on numerical and scientific computing. The library provides a collection of robust, high performance libraries for mathematics, statistics, streams, utilities, and more.

For more information on the project, filing bug reports and feature requests, and guidance on how to develop stdlib, see the main project repository.

Community

Chat


License

See LICENSE.

Copyright

Copyright © 2016-2024. The Stdlib Authors.