Skip to content

Commit

Permalink
[UPDATE] split into separate files. [FIX] README. missing dtype key.
Browse files Browse the repository at this point in the history
  • Loading branch information
kgryte committed Jul 20, 2015
1 parent 91e6cb4 commit 763bb54
Show file tree
Hide file tree
Showing 11 changed files with 310 additions and 67 deletions.
6 changes: 3 additions & 3 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -44,12 +44,12 @@ By default, the output data structure is a generic [`array`](https://developer.m
var out;

out = zeros( 5, {
'float32'
'dtype': 'float32'
});
// returns Float32Array( [0,0,0,0,0] );

out = zeros( [3,2], {
'int32'
'dtype': 'int32'
});
/*
[ 0 0
Expand All @@ -63,7 +63,7 @@ __Notes__:

``` javascript
var out = zeros( [2,1,3], {
'float32'
'dtype': 'float32'
});
// returns [ [ [0,0,0] ], [ [0,0,0] ] ]
```
Expand Down
32 changes: 32 additions & 0 deletions lib/array.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,32 @@
'use strict';

/**
* FUNCTION: zeros( len )
* Creates a zero-filled array.
*
* @param {Number} len - array length
* @returns {Number[]} zero-filled array
*/
function zeros( len ) {
var out,
i;

// Ensure fast elements...
if ( len < 64000 ) {
out = new Array( len );
for ( i = 0; i < len; i++ ) {
out[ i ] = 0;
}
} else {
out = [];
for ( i = 0; i < len; i++ ) {
out.push( 0 );
}
}
return out;
} // end FUNCTION zeros()


// EXPORTS //

module.exports = zeros;
24 changes: 24 additions & 0 deletions lib/arrayarray.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,24 @@
'use strict';

// MODULES //

var recurse = require( './recurse.js' );


// ZEROS //

/**
* FUNCTION: zeros( dims )
* Creates a zero-filled multidimensional array.
*
* @param {Number[]} dims - dimensions
* @returns {Matrix} zero-filled multidimensional array
*/
function zeros( dims ) {
return recurse( dims, 0 );
} // end FUNCTION zeros()


// EXPORTS //

module.exports = zeros;
42 changes: 12 additions & 30 deletions lib/index.js
Original file line number Diff line number Diff line change
Expand Up @@ -4,13 +4,18 @@

var isPositiveIntegerArray = require( 'validate.io-positive-integer-array' ),
isPositiveInteger = require( 'validate.io-positive-integer' ),
ctors = require( 'compute-array-constructors' ),
matrix = require( 'dstructs-matrix' ),
validate = require( './validate.js' ),
recurse = require( './recurse.js' ),
compile = require( './compile.js' );


// FUNCTIONS //

var array = require( './array.js' ),
typedarray = require( './typedarray.js' ),
arrayarray = require( './arrayarray.js' ),
matrix = require( './matrix.js' );


// ZEROS //

/**
Expand All @@ -23,16 +28,12 @@ var isPositiveIntegerArray = require( 'validate.io-positive-integer-array' ),
* @returns {Array|Number[]|Int8Array|Uint8Array|Uint8ClampedArray|Int16Array|Uint16Array|Int32Array|Uint32Array|Float32Array|Float64Array|Matrix} zeros
*/
function zeros( dims, options ) {
/* jshint newcap:false */
var opts = {},
isArray,
ndims,
ctor,
out,
err,
len,
dt,
i;
dt;

isArray = isPositiveIntegerArray( dims );
if ( !isArray && !isPositiveInteger( dims ) ) {
Expand All @@ -56,38 +57,19 @@ function zeros( dims, options ) {
}
// 1-dimensional data structures...
if ( ndims === 1 ) {
ctor = ctors( dt );
if ( ctor === null ) {
throw new Error( 'zeros()::invalid option. Data type option does not have a corresponding array constructor. Option: `' + dt + '`.' );
}
// Need to fill a generic array...
if ( dt === 'generic' ) {
// Ensure fast elements...
if ( len < 64000 ) {
out = new ctor( len );
for ( i = 0; i < len; i++ ) {
out[ i ] = 0;
}
} else {
out = [];
for ( i = 0; i < len; i++ ) {
out.push( 0 );
}
}
return out;
return array( len );
}
// Typed-arrays are already zero-filled...
return new ctor( len );
return typedarray( len, dt );
}
// Multidimensional data structures...
if ( dt !== 'generic' ) {
if ( ndims === 2 ) {
return matrix( dims, dt );
}
// TODO: dstructs-ndarray support goes here. Until then, fall through to plain arrays...
// return ndarray();
}
return recurse( dims, 0 );
return arrayarray( dims );
} // end FUNCTION zeros()


Expand Down
25 changes: 25 additions & 0 deletions lib/matrix.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,25 @@
'use strict';

// MODULES //

var matrix = require( 'dstructs-matrix' );


// ZEROS //

/**
* FUNCTION: zeros( dims, dt )
* Creates a zero-filled matrix.
*
* @param {Number[]} dims - dimensions
* @param {String} dt - data type
* @returns {Matrix} zero-filled matrix
*/
function zeros( dims, dt ) {
return matrix( dims, dt );
} // end FUNCTION zeros()


// EXPORTS //

module.exports = zeros;
30 changes: 30 additions & 0 deletions lib/typedarray.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,30 @@
'use strict';

// MODULES //

var ctors = require( 'compute-array-constructors' );


// ZEROS //

/**
* FUNCTION: zeros( len, dt )
* Creates a zero-filled typed array.
*
* @param {Number} len - array length
* @param {String} dt - data type
* @returns {Int8Array|Uint8Array|Uint8ClampedArray|Int16Array|Uint16Array|Int32Array|Uint32Array|Float32Array|Float64Array} zero-filled typed array
*/
function zeros( len, dt ) {
/* jshint newcap:false */
var ctor = ctors( dt );
if ( ctor === null ) {
throw new Error( 'zeros()::invalid value. Data type does not have a corresponding array constructor. Value: `' + dt + '`.' );
}
return new ctor( len );
} // end FUNCTION zeros()


// EXPORTS //

module.exports = zeros;
47 changes: 47 additions & 0 deletions test/test.array.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,47 @@
/* global describe, it, require */
'use strict';

// MODULES //

var // Expectation library:
chai = require( 'chai' ),

// Module to be tested:
zeros = require( './../lib/array.js' );


// VARIABLES //

var expect = chai.expect,
assert = chai.assert;


// TESTS //

describe( 'zero-filled array', function tests() {

it( 'should export a function', function test() {
expect( zeros ).to.be.a( 'function' );
});

it( 'should return a zero-filled array', function test() {
var actual, expected;

actual = zeros( 5 );
expected = [ 0, 0, 0, 0, 0 ];

assert.deepEqual( actual, expected );
});

it( 'should support fast elements', function test() {
var actual, i;

this.timeout( 0 );

actual = zeros( 100000 );
for ( i = 0; i < actual.length; i++ ) {
assert.strictEqual( actual[ i ], 0 );
}
});

});
41 changes: 41 additions & 0 deletions test/test.arrayarray.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,41 @@
/* global describe, it, require */
'use strict';

// MODULES //

var // Expectation library:
chai = require( 'chai' ),

// Module to be tested:
zeros = require( './../lib/arrayarray.js' );


// VARIABLES //

var expect = chai.expect,
assert = chai.assert;


// TESTS //

describe( 'zero-filled multidimensional array', function tests() {

it( 'should export a function', function test() {
expect( zeros ).to.be.a( 'function' );
});

it( 'should return a zero-filled array of arrays', function test() {
var actual, expected;

actual = zeros( [2,1] );
expected = [ [0], [0] ];

assert.deepEqual( actual, expected );

actual = zeros( [2,1,3] );
expected = [ [[0,0,0]], [[0,0,0]] ];

assert.deepEqual( actual, expected );
});

});
34 changes: 0 additions & 34 deletions test/test.js
Original file line number Diff line number Diff line change
Expand Up @@ -80,24 +80,6 @@ describe( 'compute-zeros', function tests() {
}
});

it( 'should throw an error if provided an unrecognized/unsupported data type option', function test() {
var values = [
'beep',
'boop'
];

for ( var i = 0; i < values.length; i++ ) {
expect( badValue( values[i] ) ).to.throw( Error );
}
function badValue( value ) {
return function() {
zeros( [10], {
'dtype': value
});
};
}
});

it( 'should return a zero-filled matrix', function test() {
var matrix = zeros( [2,2], {
'dtype': 'int32'
Expand Down Expand Up @@ -150,22 +132,6 @@ describe( 'compute-zeros', function tests() {
assert.deepEqual( actual, expected );
});

it( 'should support fast elements', function test() {
var actual, i;

this.timeout( 0 );

actual = zeros( [100000] );
for ( i = 0; i < actual.length; i++ ) {
assert.strictEqual( actual[ i ], 0 );
}

actual = zeros( [100000,2] );
for ( i = 0; i < actual.length; i++ ) {
assert.deepEqual( actual[ i ], [0,0] );
}
});

it( 'should, until ndarrays are supported, ignore the `dtype` option and return a generic multidimensional array for >2 dimensions', function test() {
var actual, expected;

Expand Down
39 changes: 39 additions & 0 deletions test/test.matrix.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,39 @@
/* global describe, it, require */
'use strict';

// MODULES //

var // Expectation library:
chai = require( 'chai' ),

// Module to be tested:
zeros = require( './../lib/matrix.js' );


// VARIABLES //

var expect = chai.expect,
assert = chai.assert;


// TESTS //

describe( 'zero-filled matrix', function tests() {

it( 'should export a function', function test() {
expect( zeros ).to.be.a( 'function' );
});

it( 'should return a zero-filled matrix', function test() {
var actual, expected;

actual = zeros( [2,2], 'int32' );

expected = new Int32Array( 4 );

assert.deepEqual( actual.shape, [2,2] );
assert.strictEqual( actual.dtype, 'int32' );
assert.deepEqual( actual.data, expected );
});

});
Loading

0 comments on commit 763bb54

Please sign in to comment.