Skip to content
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension


Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
9 changes: 5 additions & 4 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -2,7 +2,7 @@ L2 norm
===
[![NPM version][npm-image]][npm-url] [![Build Status][travis-image]][travis-url] [![Coverage Status][coveralls-image]][coveralls-url] [![Dependencies][dependencies-image]][dependencies-url]

> Computes the L2 norm ([Euclidean norm](http://en.wikipedia.org/wiki/Norm_(mathematics))) of an array of values.
> Computes the L2 norm ([Euclidean norm](http://en.wikipedia.org/wiki/Norm_(mathematics))) of an array.


## Installation
Expand All @@ -16,14 +16,13 @@ For use in the browser, use [browserify](https://github.com/substack/node-browse

## Usage


``` javascript
var l2norm = require( 'compute-l2norm' );
```

#### l2norm( arr[, accessor] )

Computes the _L2_ norm (Euclidean norm) of an `array` of values.
Computes the _L2_ norm (Euclidean norm) of an `array`.

``` javascript
var data = [ 2, 7, 3, -3, 9 ];
Expand All @@ -44,10 +43,12 @@ function getValue( d, i ) {
return d[ 1 ];
}

var res = l2norm( data, getValue );
var norm = l2norm( data, getValue );
// returns 5
```

If provided an empty `array`, the function returns `null`.


## Examples

Expand Down
4 changes: 3 additions & 1 deletion examples/index.js
Original file line number Diff line number Diff line change
@@ -1,8 +1,10 @@
'use strict';

var l2norm = require( './../lib' );

var data = new Array( 1000 );
for ( var i = 0; i < data.length; i++ ) {
data[ i ] = Math.random() * 100;
}

console.log( l2norm( data ) );
console.log( l2norm( data ) );
27 changes: 12 additions & 15 deletions lib/index.js
Original file line number Diff line number Diff line change
@@ -1,30 +1,30 @@
'use strict';

// L2NORM //
// MODULES //

var isArray = require( 'validate.io-array' ),
isFunction = require( 'validate.io-function' );


// L2NORM //

/**
* FUNCTION: l2norm( arr )
* Calculates the L2 norm (Euclidean norm) of an array of values.
* FUNCTION: l2norm( arr[, accessor] )
* Calculates the L2 norm (Euclidean norm) of an array.
*
* @param {Array} arr - array of values
* @param {Array} arr - input array
* @param {Function} [accessor] - accessor function for accessing array values
* @returns {Number} L2 norm
* @returns {Number|Null} L2 norm or null
*/
function l2norm( arr, clbk ) {
if ( !isArray( arr ) ) {
throw new TypeError( 'l2norm()::invalid input argument. Must provide an array. Value: `' + arr + '`.' );
}

if ( arguments.length > 1 ) {
if ( !isFunction( clbk ) ) {
throw new TypeError( 'l2norm()::invalid input argument. Accessor must be a function. Value: `' + clbk + '`.' );
}
}

var len = arr.length,
t = 0,
s = 1,
Expand All @@ -33,13 +33,13 @@ function l2norm( arr, clbk ) {
abs,
i;

if ( !len ) {
return null;
}
if ( clbk ) {
for ( i = 0; i < len; i++ ) {
val = clbk( arr[ i ], i );
abs = val;
if ( abs < 0 ) {
abs = -abs;
}
abs = ( val < 0 ) ? -val : val;
if ( abs > 0 ) {
if ( abs > t ) {
r = t / val;
Expand All @@ -54,10 +54,7 @@ function l2norm( arr, clbk ) {
} else {
for ( i = 0; i < len; i++ ) {
val = arr[ i ];
abs = val;
if ( abs < 0 ) {
abs = -abs;
}
abs = ( val < 0 ) ? -val : val;
if ( abs > 0 ) {
if ( abs > t ) {
r = t / val;
Expand Down
8 changes: 7 additions & 1 deletion package.json
Original file line number Diff line number Diff line change
Expand Up @@ -35,7 +35,13 @@
"linear algebra",
"norm",
"euclidean norm",
"l2norm"
"l2norm",
"l2 distance",
"euclidean length",
"length",
"distance",
"array",
"vector"
],
"bugs": {
"url": "https://github.com/compute-io/l2norm/issues"
Expand Down
23 changes: 14 additions & 9 deletions test/test.js
Original file line number Diff line number Diff line change
@@ -1,3 +1,4 @@
/* global require, describe, it */
'use strict';

// MODULES //
Expand Down Expand Up @@ -25,15 +26,15 @@ describe( 'compute-l2norm', function tests() {

it( 'should throw an error if not provided an array', function test() {
var values = [
'5',
5,
null,
undefined,
NaN,
true,
{},
function(){}
];
'5',
5,
null,
undefined,
NaN,
true,
{},
function(){}
];

for ( var i = 0; i < values.length; i++ ) {
expect( badValue( values[i] ) ).to.throw( TypeError );
Expand Down Expand Up @@ -68,6 +69,10 @@ describe( 'compute-l2norm', function tests() {
}
});

it( 'should return null if provided an empty array', function test() {
assert.isNull( l2norm( [] ) );
});

it( 'should return the L2 norm', function test() {
var data, expected;

Expand Down