-
Notifications
You must be signed in to change notification settings - Fork 0
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
[UPDATE] split into separate files. [FIX] README. missing dtype key.
- Loading branch information
Showing
11 changed files
with
310 additions
and
67 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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; |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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; |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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; |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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; |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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 ); | ||
} | ||
}); | ||
|
||
}); |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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 ); | ||
}); | ||
|
||
}); |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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 ); | ||
}); | ||
|
||
}); |
Oops, something went wrong.