compute-sqrt

3.0.1 • Public • Published

Square Root

NPM version Build Status Coverage Status Dependencies

Computes an element-wise principal square root.

The principal square root is defined as

Equation for the principal square root.

For negative numbers, the principal square root is not defined.

Installation

$ npm install compute-sqrt

For use in the browser, use browserify.

Usage

var sqrt = require( 'compute-sqrt' );

sqrt( x[, opts] )

Computes an element-wise principal square root. x may be either a number, an array, a typed array, or a matrix.

var matrix = require( 'dstructs-matrix' ),
    data,
    mat,
    out,
    i;
 
out = sqrt( 9 );
// returns 3
 
out = sqrt( -9 );
// returns NaN
 
data = [ 4, 9, 16 ];
out = sqrt( data );
// returns [ 2, 3, 4 ]
 
data = new Int8Array( data );
out = sqrt( data );
// returns Float64Array( [2,3,4] )
 
data = new Int16Array( 6 );
for ( i = 0; i < 6; i++ ) {
    data[ i ] = i*i;
}
mat = matrix( data, [3,2], 'int16' );
/*
    [  0  1
       4  9
      16 25 ]
*/
 
out = sqrt( mat );
/*
    [ 0 1
      2 3
      4 5 ]
*/

The function accepts the following options:

  • accessor: accessor function for accessing array values.
  • dtype: output typed array or matrix data type. Default: float64.
  • copy: boolean indicating if the function should return a new data structure. Default: true.
  • path: deepget/deepset key path.
  • sep: deepget/deepset key path separator. Default: '.'.

For non-numeric arrays, provide an accessor function for accessing array values.

var data = [
    [0,4],
    [1,9],
    [2,16],
    [3,25],
    [4,36]
];
 
function getValue( d, i ) {
    return d[ 1 ];
}
 
var out = sqrt( data, {
    'accessor': getValue
});
// returns [ 2, 3, 4, 5, 6 ]

To deepset an object array, provide a key path and, optionally, a key path separator.

var data = [
    {'x':[0,4]},
    {'x':[1,9]},
    {'x':[2,16]},
    {'x':[3,25]},
    {'x':[4,36]}
];
 
var out = sqrt( data, 'x|1', '|' );
/*
    [
        {'x':[0,2]},
        {'x':[1,3]},
        {'x':[2,4]},
        {'x':[3,5]},
        {'x':[4,6]}
    ]
*/
 
var bool = ( data === out );
// returns true

By default, when provided a typed array or matrix, the output data structure is float64 in order to preserve precision. To specify a different data type, set the dtype option (see matrix for a list of acceptable data types).

var data, out;
 
data = new Int8Array( [4,9,16] );
 
out = sqrt( data, {
    'dtype': 'int32'
});
// returns Int32Array( [2,3,4] )
 
// Works for plain arrays, as well...
out = sqrt( [4,9,16], {
    'dtype': 'uint8'
});
// returns Uint8Array( [2,3,4] )

By default, the function returns a new data structure. To mutate the input data structure (e.g., when input values can be discarded or when optimizing memory usage), set the copy option to false.

var data,
    bool,
    mat,
    out,
    i;
 
data = [ 4, 9, 16 ];
 
out = sqrt( data, {
    'copy': false
});
// returns [ 2, 3, 4 ]
 
bool = ( data === out );
// returns true
 
data = new Int16Array( 6 );
for ( i = 0; i < 6; i++ ) {
    data[ i ] = i*i;
}
mat = matrix( data, [3,2], 'int16' );
/*
    [  0  1
       4  9
      16 25 ]
*/
 
out = sqrt( mat, {
    'copy': false
});
/*
    [ 0 1
      2 3
      4 5 ]
*/
 
bool = ( mat === out );
// returns true

Notes

  • If an element is not a numeric value, the evaluated principal square root is NaN.

    var data, out;
     
    out = sqrt( null );
    // returns NaN
     
    out = sqrt( true );
    // returns NaN
     
    out = sqrt( {'a':'b'} );
    // returns NaN
     
    out = sqrt( [ true, null, [] ] );
    // returns [ NaN, NaN, NaN ]
     
    function getValue( d, i ) {
        return d.x;
    }
    data = [
        {'x':true},
        {'x':[]},
        {'x':{}},
        {'x':null}
    ];
     
    out = sqrt( data, {
        'accessor': getValue
    });
    // returns [ NaN, NaN, NaN, NaN ]
     
    out = sqrt( data, {
        'path': 'x'
    });
    /*
        [
            {'x':NaN},
            {'x':NaN},
            {'x':NaN,
            {'x':NaN}
        ]
    */
  • Be careful when providing a data structure which contains non-numeric elements and specifying an integer output data type, as NaN values are cast to 0.

    var out = sqrt( [ true, null, [] ], {
        'dtype': 'int8'
    });
    // returns Int8Array( [0,0,0] );

Examples

var matrix = require( 'dstructs-matrix' ),
    sqrt = require( 'compute-sqrt' );
 
var data,
    mat,
    out,
    tmp,
    i;
 
// Plain arrays...
data = new Array( 10 );
for ( i = 0; i < data.length; i++ ) {
    data[ i ] = Math.round( Math.random()*1000 );
}
out = sqrt( data );
 
// Object arrays (accessors)...
function getValue( d ) {
    return d.x;
}
for ( i = 0; i < data.length; i++ ) {
    data[ i ] = {
        'x': data[ i ]
    };
}
out = sqrt( data, {
    'accessor': getValue
});
 
// Deep set arrays...
for ( i = 0; i < data.length; i++ ) {
    data[ i ] = {
        'x': [ i, data[ i ].x ]
    };
}
out = sqrt( data, {
    'path': 'x/1',
    'sep': '/'
});
 
// Typed arrays...
data = new Int32Array( 10 );
for ( i = 0; i < data.length; i++ ) {
    data[ i ] = Math.random() * 100;
}
out = sqrt( data );
 
// Matrices...
mat = matrix( data, [5,2], 'int32' );
out = sqrt( mat );
 
// Matrices (custom output data type)...
out = sqrt( mat, {
    'dtype': 'uint8'
});

To run the example code from the top-level application directory,

$ node ./examples/index.js

Tests

Unit

Unit tests use the Mocha test framework with Chai assertions. To run the tests, execute the following command in the top-level application directory:

$ make test

All new feature development should have corresponding unit tests to validate correct functionality.

Test Coverage

This repository uses Istanbul as its code coverage tool. To generate a test coverage report, execute the following command in the top-level application directory:

$ make test-cov

Istanbul creates a ./reports/coverage directory. To access an HTML version of the report,

$ make view-cov

License

MIT license.

Copyright

Copyright © 2014-2015. The Compute.io Authors.

Package Sidebar

Install

npm i compute-sqrt

Weekly Downloads

143

Version

3.0.1

License

MIT

Last publish

Collaborators

  • kgryte