Package Exports
- compute.io
This package does not declare an exports field, so the exports above have been automatically detected and optimized by JSPM instead. If any package subpath is missing, it is recommended to post an issue to the original package (compute.io) to support the "exports" field. If that is not possible, create a JSPM override to customize the exports field for this package.
Readme
Compute.io
Computation library.
Table of Contents
[Installation](#installation)
[Usage](#usage)
[Utilities](#utilities)
[Statistics](#statistics)
- Geometry
[Fluent Interface](#fluent-interface)
[Tests](#tests)
[Unit](#unit)
- Coverage
[License](#license)
Installation
$ npm install compute.io
Usage
To use compute,
var compute = require( 'compute.io' );
The compute module is comprised of several smaller modules. If you want to roll your own compute, follow the links and import the individual modules.
The compute module has the following methods...
Utilities
compute.roundn( x, n )
Round values to the nearest multiple of 10^n
. x
may be either a single numeric value or an array of values. n
must be an integer
.
console.log( compute.roundn( Math.PI, -2 ) );
// Returns 3.14
console.log( compute.roundn( 111, 2 ) );
// Returns 100
var data = [ 2.342, 4.943, 2.234, 7.992, 3.142 ];
console.log( compute.roundn( data, -2 ) );
// Returns [...] where each value is rounded to nearest hundredth
Statistics
compute.min( arr )
Computes the minimum value of an array.
var data = [ 2, 4, 2, 7, 3 ];
console.log( compute.min( data ) );
compute.max( arr )
Computes the maximum value of an array.
var data = [ 2, 4, 2, 7, 3 ];
console.log( compute.max( data ) );
compute.sum( arr )
Computes the sum of an array.
var data = [ 2, 4, 2, 7, 3 ];
console.log( compute.sum( data ) );
compute.mean( arr )
Computes the mean over an array of values.
var data = [ 2, 4, 2, 7, 3 ];
console.log( compute.mean( data ) );
compute.variance( arr )
Computes the sample variance over an array of values.
var data = [ 2, 4, 2, 7, 3 ];
console.log( compute.variance( data ) );
compute.stdev( arr )
Computes the sample standard deviation over an array of values.
var data = [ 2, 4, 2, 7, 3 ];
console.log( compute.stdev( data ) );
compute.mode( arr )
Computes the mode of an array.
var data = [ 2, 4, 2, 7, 3 ];
console.log( compute.mode( data ) );
Geometry
compute.hypot( a, b )
Computes the hypotenuse of a right triangle.
var a = 10,
b = 12;
console.log( compute.hypot( a, b ) );
Fluent Interface
For data pipelines, invoking serial methods can become verbose.
data = compute.roundn( data, -3 );
data = compute.mean( data );
data = compute.roundn( data, 0 );
...
Fluent interfaces can help alleviate this problem. Such interfaces have been popularized by libraries such as jQuery and D3 which utilize method chaining.
To create a fluent interface,
var flow = compute.flow();
A flow
pipeline should be initialized.
flow.value( data );
Once initialized, all compute methods are now available. The lone difference is that data should not be explicitly passed as an argument. For example,
flow
.value( data )
.roundn( -3 )
.mean()
.roundn( 0 );
To return the flow value
,
var mean = flow.value();
To help understand the transformations comprising a data pipeline, flow
exposes an inspect()
method, which logs the current value
to the console while maintaining the fluent interface.
flow.inspect();
The above flow
can be modified accordingly,
flow
.value( data )
.inspect()
.roundn( -3 )
.inspect()
.mean()
.inspect()
.roundn( 0 )
.inspect();
To summarize the flow
API...
flow.value( [value] )
This method is a setter/getter. If no value
is provided, returns the current flow value
. If a value
is provided, sets the flow value
.
flow.value( [ 4, 3, 6, 2 ] );
flow.inspect()
Logs the current flow value
to the console, while maintaining the fluent interface.
flow.inspect();
Notes
When creating flows, ensure that the output from one computation matches the input argument requirements for the next computation.
For large datasets, rather than loading datasets into memory, consider using file streams and utilize stream tools such as [Flow.io](https://github.com/flow-io/flow.io).
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,
$ open reports/coverage/lcov-report/index.html
License
Copyright
Copyright © 2014. Athan Reines.