Huge News!Announcing our $40M Series B led by Abstract Ventures.Learn More
Socket
Sign inDemoInstall
Socket

dstructs-matrix

Package Overview
Dependencies
Maintainers
1
Versions
8
Alerts
File Explorer

Advanced tools

Socket logo

Install Socket

Detect and block malicious and high-risk dependencies

Install

dstructs-matrix - npm Package Compare versions

Comparing version 1.0.0 to 2.0.0

34

lib/ctor.js

@@ -11,24 +11,12 @@ 'use strict';

* @param {Int8Array|Uint8Array|Uint8ClampedArray|Int16Array|Uint16Array|Int32Array|Uint32Array|Float32Array|Float64Array} data - input typed array
* @param {String} dtype - matrix data type
* @param {Number[]} shape - matrix dimensions/shape
* @param {String} dtype - matrix data type
* @param {Number} offset - matrix offset
* @param {Number[]} strides - matrix strides
* @returns {Matrix} Matrix instance
*/
function Matrix( data, shape, dtype ) {
var strides,
ndims,
s, i;
function Matrix( data, dtype, shape, offset, strides ) {
if ( !( this instanceof Matrix ) ) {
return new Matrix( data, shape, dtype );
return new Matrix( data, dtype, shape, offset, strides );
}
ndims = shape.length;
// Determine the matrix strides...
strides = new Array( ndims );
s = 1;
for ( i = ndims-1; i >= 0; i-- ) {
strides[ i ] = s;
s *= shape[ i ];
}
// Underlying data type:

@@ -50,3 +38,3 @@ Object.defineProperty( this, 'dtype', {

// Matrix strides (non-enumerable):
// Matrix strides:
Object.defineProperty( this, 'strides', {

@@ -59,5 +47,13 @@ 'value': strides,

// Matrix offset:
Object.defineProperty( this, 'offset', {
'value': offset,
'configurable': false,
'enumerable': true,
'writable': true
});
// Number of matrix dimensions:
Object.defineProperty( this, 'ndims', {
'value': ndims,
'value': shape.length,
'configurable': false,

@@ -64,0 +60,0 @@ 'enumerable': true,

@@ -11,32 +11,20 @@ 'use strict';

* @param {Int8Array|Uint8Array|Uint8ClampedArray|Int16Array|Uint16Array|Int32Array|Uint32Array|Float32Array|Float64Array} data - input typed array
* @param {String} dtype - matrix data type
* @param {Number[]} shape - matrix dimensions/shape
* @param {String} dtype - matrix data type
* @param {Number} offset - matrix offset
* @param {Number[]} strides - matrix strides
* @returns {Matrix} Matrix instance
*/
function Matrix( data, shape, dtype ) {
var strides,
ndims,
s, i;
function Matrix( data, dtype, shape, offset, strides ) {
if ( !( this instanceof Matrix ) ) {
return new Matrix( data, shape, dtype );
return new Matrix( data, dtype, shape, offset, strides );
}
ndims = shape.length;
// Determine the matrix strides...
strides = new Array( ndims );
s = 1;
for ( i = ndims-1; i >= 0; i-- ) {
strides[ i ] = s;
s *= shape[ i ];
}
this.dtype = dtype;
this.shape = shape;
this.strides = strides;
this.ndims = ndims;
this.offset = offset;
this.ndims = shape.length;
this.length = data.length;
this.nbytes = data.byteLength;
this.data = data;
return this;

@@ -43,0 +31,0 @@ } // end FUNCTION Matrix()

@@ -23,3 +23,3 @@ 'use strict';

}
return this.data[ i*this.strides[0] + j*this.strides[1] ];
return this.data[ this.offset + i*this.strides[0] + j*this.strides[1] ];
} // end FUNCTION get()

@@ -26,0 +26,0 @@

@@ -13,3 +13,3 @@ 'use strict';

/*jshint validthis:true */
return this.data[ i*this.strides[0] + j*this.strides[1] ];
return this.data[ this.offset + i*this.strides[0] + j*this.strides[1] ];
} // end FUNCTION get()

@@ -16,0 +16,0 @@

@@ -19,2 +19,3 @@ 'use strict';

/*jshint validthis:true */
var r, j;
if ( !isInteger( idx ) ) {

@@ -25,4 +26,12 @@ throw new TypeError( 'iget()::invalid input argument. Must provide a integer. Value: `' + idx + '`.' );

idx += this.length;
if ( idx < 0 ) {
return;
}
}
return this.data[ idx ];
j = idx % this.strides[ 0 ];
r = idx - j;
if ( this.strides[ 0 ] < 0 ) {
r = -r;
}
return this.data[ this.offset + r + j*this.strides[1] ];
} // end FUNCTION iget()

@@ -29,0 +38,0 @@

@@ -12,6 +12,15 @@ 'use strict';

/*jshint validthis:true */
var r, j;
if ( idx < 0 ) {
idx += this.length;
if ( idx < 0 ) {
return;
}
}
return this.data[ idx ];
j = idx % this.strides[ 0 ];
r = idx - j;
if ( this.strides[ 0 ] < 0 ) {
r = -r;
}
return this.data[ this.offset + r + j*this.strides[1] ];
} // end FUNCTION iget()

@@ -18,0 +27,0 @@

@@ -21,2 +21,3 @@ 'use strict';

/* jshint validthis: true */
var r, j;
if ( !isInteger( idx ) ) {

@@ -34,3 +35,8 @@ throw new TypeError( 'iset()::invalid input argument. An index must be an integer. Value: `' + idx + '`.' );

}
this.data[ idx ] = v;
j = idx % this.strides[ 0 ];
r = idx - j;
if ( this.strides[ 0 ] < 0 ) {
r = -r;
}
this.data[ this.offset + r + j*this.strides[1] ] = v;
return this;

@@ -37,0 +43,0 @@ } // end FUNCTION iset()

@@ -13,2 +13,3 @@ 'use strict';

/* jshint validthis: true */
var r, j;
if ( idx < 0 ) {

@@ -20,3 +21,8 @@ idx += this.length;

}
this.data[ idx ] = v;
j = idx % this.strides[ 0 ];
r = idx - j;
if ( this.strides[ 0 ] < 0 ) {
r = -r;
}
this.data[ this.offset + r + j*this.strides[1] ] = v;
return this;

@@ -23,0 +29,0 @@ } // end FUNCTION iset()

@@ -103,3 +103,3 @@ 'use strict';

// Return a new Matrix instance:
return new Matrix( data, shape, dtype );
return new Matrix( data, dtype, shape, 0, [shape[1],1] );
} // end FUNCTION matrix()

@@ -106,0 +106,0 @@

@@ -80,3 +80,3 @@ 'use strict';

// Return a new Matrix instance:
return new Matrix( data, shape, dtype );
return new Matrix( data, dtype, shape, 0, [shape[1],1] );
} // end FUNCTION matrix()

@@ -83,0 +83,0 @@

@@ -28,7 +28,13 @@ 'use strict';

out,
sgn,
d,
s0, s1, s2, s3,
o,
r, dr,
i, j, m, n;
s0 = this.strides[ 0 ];
s1 = this.strides[ 1 ];
o = this.offset;
if ( arguments.length < 2 ) {

@@ -45,10 +51,13 @@ if ( !isNonNegativeIntegerArray( rows ) ) {

}
j = i.length;
m = i.length;
// Create a row vector (matrix):
d = new BTYPES[ this.dtype ]( j );
out = new this.constructor( d, [1,j], this.dtype );
d = new BTYPES[ this.dtype ]( m );
out = new this.constructor( d, this.dtype, [1,m], 0, [m,1] );
for ( n = 0; n < j; n++ ) {
d[ n ] = this.data[ i[n] ];
sgn = ( s0 < 0 ) ? -1 : 1;
for ( n = 0; n < m; n++ ) {
j = i[ n ] % s0;
r = sgn * ( i[n] - j );
d[ n ] = this.data[ o + r + j*s1 ];
}

@@ -97,10 +106,8 @@ } else {

d = new BTYPES[ this.dtype ]( nRows*nCols );
out = new this.constructor( d, [nRows,nCols], this.dtype );
out = new this.constructor( d, this.dtype, [nRows,nCols], 0, [nCols,1]);
s0 = this.strides[ 0 ];
s1 = this.strides[ 1 ];
s2 = out.strides[ 0 ];
s3 = out.strides[ 1 ];
for ( m = 0; m < nRows; m++ ) {
r = i[ m ] * s0;
r = o + i[m]*s0;
dr = m * s2;

@@ -107,0 +114,0 @@ for ( n = 0; n < nCols; n++ ) {

@@ -23,17 +23,26 @@ 'use strict';

out,
sgn,
d,
s0, s1, s2, s3,
o,
r, dr,
i, j, m, n;
s0 = this.strides[ 0 ];
s1 = this.strides[ 1 ];
o = this.offset;
if ( arguments.length < 2 ) {
i = rows;
j = i.length;
m = i.length;
// Create a row vector (matrix):
d = new BTYPES[ this.dtype ]( j );
out = new this.constructor( d, [1,j], this.dtype );
d = new BTYPES[ this.dtype ]( m );
out = new this.constructor( d, this.dtype, [1,m], 0, [m,1] );
for ( n = 0; n < j; n++ ) {
d[ n ] = this.data[ i[n] ];
sgn = ( s0 < 0 ) ? -1 : 1;
for ( n = 0; n < m; n++ ) {
j = i[ n ] % s0;
r = sgn * ( i[n] - j );
d[ n ] = this.data[ o + r + j*s1 ];
}

@@ -64,10 +73,8 @@ } else {

d = new BTYPES[ this.dtype ]( nRows*nCols );
out = new this.constructor( d, [nRows,nCols], this.dtype );
out = new this.constructor( d, this.dtype, [nRows,nCols], 0, [nCols,1] );
s0 = this.strides[ 0 ];
s1 = this.strides[ 1 ];
s2 = out.strides[ 0 ];
s3 = out.strides[ 1 ];
for ( m = 0; m < nRows; m++ ) {
r = i[ m ] * s0;
r = o + i[m]*s0;
dr = m * s2;

@@ -74,0 +81,0 @@ for ( n = 0; n < nCols; n++ ) {

@@ -13,7 +13,14 @@ 'use strict';

function mset1( mat, idx, v ) {
var len = idx.length,
i;
var s0 = mat.strides[ 0 ],
s1 = mat.strides[ 1 ],
len = idx.length,
o = mat.offset,
sgn,
r, j, n;
for ( i = 0; i < len; i++ ) {
mat.data[ idx[ i ] ] = v;
sgn = ( s0 < 0 ) ? -1 : 1;
for ( n = 0; n < len; n++ ) {
j = idx[ n ] % s0;
r = sgn * ( idx[n] - j );
mat.data[ o + r + j*s1 ] = v;
}

@@ -20,0 +27,0 @@ } // end FUNCTION mset1()

@@ -14,15 +14,26 @@ 'use strict';

function mset2( mat, idx, clbk, ctx ) {
var nRows = mat.shape[ 0 ],
var s0 = mat.strides[ 0 ],
s1 = mat.strides[ 1 ],
len = idx.length,
o = mat.offset,
sgn,
r, c,
i, j;
i, k, n;
for ( i = 0; i < len; i++ ) {
j = idx[ i ];
sgn = ( s0 < 0 ) ? -1 : 1;
for ( n = 0; n < len; n++ ) {
// Get the column number:
c = idx[ n ] % s0;
// Determine the row and column indices...
c = j % nRows; // remainder
r = ( j - c ) / nRows;
// Determine the row offset:
i = sgn * ( idx[n] - c );
mat.data[ j ] = clbk.call( ctx, mat.data[ j ], r, c, j );
// Get the row number:
r = i / s0;
// Calculate the index:
k = o + i + c*s1;
// Set the value:
mat.data[ k ] = clbk.call( ctx, mat.data[ k ], r, c, k );
}

@@ -29,0 +40,0 @@ } // end FUNCTION mset2()

@@ -13,4 +13,13 @@ 'use strict';

function mset3( mat, idx, m ) {
var len = idx.length,
i;
var s0 = mat.strides[ 0 ],
s1 = mat.strides[ 1 ],
s2 = m.strides[ 0 ],
s3 = m.strides[ 1 ],
len = idx.length,
o0 = mat.offset,
o1 = m.offset,
sgn0, sgn1,
r0, r1,
j0, j1,
n;

@@ -20,4 +29,14 @@ if ( m.length !== len ) {

}
for ( i = 0; i < len; i++ ) {
mat.data[ idx[ i ] ] = m.data[ i ];
sgn0 = ( s0 < 0 ) ? -1 : 1;
sgn1 = ( s2 < 0 ) ? -1 : 1;
for ( n = 0; n < len; n++ ) {
// Get the column number and row offset for the first matrix:
j0 = idx[ n ] % s0;
r0 = sgn0 * ( idx[n] - j0 );
// Get the column number and row offset for the value matrix:
j1 = n % s2;
r1 = sgn1 * ( n - j1 );
mat.data[ o0 + r0 + j0*s1 ] = m.data[ o1 + r1 + j1*s3 ];
}

@@ -24,0 +43,0 @@ } // end FUNCTION mset3()

@@ -15,16 +15,15 @@ 'use strict';

function mset4( mat, rows, cols, clbk, ctx ) {
var nRows = rows.length,
var s0 = mat.strides[ 0 ],
s1 = mat.strides[ 1 ],
nRows = rows.length,
nCols = cols.length,
idx,
s0, s1,
o = mat.offset,
r,
i, j;
i, j, k;
s0 = mat.strides[ 0 ];
s1 = mat.strides[ 1 ];
for ( i = 0; i < nRows; i++ ) {
r = rows[ i ] * s0;
r = o + rows[i]*s0;
for ( j = 0; j < nCols; j++ ) {
idx = r + cols[j]*s1;
mat.data[ idx ] = clbk.call( ctx, mat.data[ idx ], rows[ i ], cols[ j ], idx );
k = r + cols[j]*s1;
mat.data[ k ] = clbk.call( ctx, mat.data[ k ], rows[ i ], cols[ j ], k );
}

@@ -31,0 +30,0 @@ }

@@ -14,12 +14,12 @@ 'use strict';

function mset5( mat, rows, cols, v ) {
var nRows = rows.length,
var s0 = mat.strides[ 0 ],
s1 = mat.strides[ 1 ],
nRows = rows.length,
nCols = cols.length,
s0, s1,
o = mat.offset,
r,
i, j;
s0 = mat.strides[ 0 ];
s1 = mat.strides[ 1 ];
for ( i = 0; i < nRows; i++ ) {
r = rows[ i ] * s0;
r = o + rows[i]*s0;
for ( j = 0; j < nCols; j++ ) {

@@ -26,0 +26,0 @@ mat.data[ r + cols[j]*s1 ] = v;

@@ -14,6 +14,11 @@ 'use strict';

function mset6( mat, rows, cols, m ) {
var nRows = rows.length,
var s0 = mat.strides[ 0 ],
s1 = mat.strides[ 1 ],
s2 = m.strides[ 0 ],
s3 = m.strides[ 1 ],
nRows = rows.length,
nCols = cols.length,
s0, s1, s2, s3,
r, mr,
o0 = mat.offset,
o1 = m.offset,
r0, r1,
i, j;

@@ -24,11 +29,7 @@

}
s0 = mat.strides[ 0 ];
s1 = mat.strides[ 1 ];
s2 = m.strides[ 0 ];
s3 = m.strides[ 1 ];
for ( i = 0; i < nRows; i++ ) {
r = rows[ i ] * s0;
mr = i * s2;
r0 = o0 + rows[i]*s0;
r1 = o1 + i*s2;
for ( j = 0; j < nCols; j++ ) {
mat.data[ r + cols[j]*s1 ] = m.data[ mr + j*s3 ];
mat.data[ r0 + cols[j]*s1 ] = m.data[ r1 + j*s3 ];
}

@@ -35,0 +36,0 @@ }

@@ -28,3 +28,6 @@ 'use strict';

}
this.data[ i*this.strides[0] + j*this.strides[1] ] = v;
i = this.offset + i*this.strides[0] + j*this.strides[1];
if ( i >= 0 ) {
this.data[ i ] = v;
}
return this;

@@ -31,0 +34,0 @@ } // end FUNCTION set()

@@ -14,3 +14,6 @@ 'use strict';

/* jshint validthis: true */
this.data[ i*this.strides[0] + j*this.strides[1] ] = v;
i = this.offset + i*this.strides[0] + j*this.strides[1];
if ( i >= 0 ) {
this.data[ i ] = v;
}
return this;

@@ -17,0 +20,0 @@ } // end FUNCTION set()

@@ -32,3 +32,4 @@ 'use strict';

len,
s0, s1, s2, s3,
s0, s1,
o,
d,

@@ -53,3 +54,3 @@ r, dr,

d = new BTYPES[ this.dtype ]( len );
mat = new this.constructor( d, [nRows,nCols], this.dtype );
mat = new this.constructor( d, this.dtype, [nRows,nCols], 0, [nCols,1] );

@@ -59,9 +60,8 @@ if ( len ) {

s1 = this.strides[ 1 ];
s2 = mat.strides[ 0 ];
s3 = mat.strides[ 1 ];
o = this.offset;
for ( i = 0; i < nRows; i++ ) {
r = rows[ i ] * s0;
dr = i * s2;
r = o + rows[i]*s0;
dr = i * nCols;
for ( j = 0; j < nCols; j++ ) {
d[ dr + j*s3 ] = this.data[ r + cols[j]*s1 ];
d[ dr + j ] = this.data[ r + cols[j]*s1 ];
}

@@ -68,0 +68,0 @@ }

@@ -31,3 +31,4 @@ 'use strict';

len,
s0, s1, s2, s3,
s0, s1,
o,
d,

@@ -46,3 +47,3 @@ r, dr,

d = new BTYPES[ this.dtype ]( len );
mat = new this.constructor( d, [nRows,nCols], this.dtype );
mat = new this.constructor( d, this.dtype, [nRows,nCols], 0, [nCols,1] );

@@ -52,9 +53,8 @@ if ( len ) {

s1 = this.strides[ 1 ];
s2 = mat.strides[ 0 ];
s3 = mat.strides[ 1 ];
o = this.offset;
for ( i = 0; i < nRows; i++ ) {
r = rows[ i ] * s0;
dr = i * s2;
r = o + rows[i]*s0;
dr = i * nCols;
for ( j = 0; j < nCols; j++ ) {
d[ dr + j*s3 ] = this.data[ r + cols[j]*s1 ];
d[ dr + j ] = this.data[ r + cols[j]*s1 ];
}

@@ -61,0 +61,0 @@ }

@@ -33,5 +33,5 @@ 'use strict';

s0, s1, s2, s3,
idx,
r, mr,
i, j;
o0, o1,
r0, r1,
i, j, k;

@@ -62,2 +62,3 @@ if ( !isString( seq ) ) {

s1 = this.strides[ 1 ];
o0 = this.offset;

@@ -72,6 +73,6 @@ // Callback...

for ( i = 0; i < nRows; i++ ) {
r = rows[ i ] * s0;
r0 = o0 + rows[i]*s0;
for ( j = 0; j < nCols; j++ ) {
idx = r + cols[j]*s1;
this.data[ idx ] = clbk.call( ctx, this.data[ idx ], rows[i], cols[j], idx );
k = r0 + cols[j]*s1;
this.data[ k ] = clbk.call( ctx, this.data[ k ], rows[i], cols[j], k );
}

@@ -90,7 +91,8 @@ }

s3 = mat.strides[ 1 ];
o1 = mat.offset;
for ( i = 0; i < nRows; i++ ) {
r = rows[ i ] * s0;
mr = i * s2;
r0 = o0 + rows[i]*s0;
r1 = o1 + i*s2;
for ( j = 0; j < nCols; j++ ) {
this.data[ r + cols[j]*s1 ] = mat.data[ mr + j*s3 ];
this.data[ r0 + cols[j]*s1 ] = mat.data[ r1 + j*s3 ];
}

@@ -102,5 +104,5 @@ }

for ( i = 0; i < nRows; i++ ) {
r = rows[ i ] * s0;
r0 = o0 + rows[i]*s0;
for ( j = 0; j < nCols; j++ ) {
this.data[ r + cols[j]*s1 ] = val;
this.data[ r0 + cols[j]*s1 ] = val;
}

@@ -107,0 +109,0 @@ }

@@ -30,5 +30,5 @@ 'use strict';

s0, s1, s2, s3,
idx,
r, mr,
i, j;
o0, o1,
r0, r1,
i, j, k;

@@ -53,2 +53,3 @@ seqs = seq.split( ',' );

s1 = this.strides[ 1 ];
o0 = this.offset;

@@ -63,6 +64,6 @@ // Callback...

for ( i = 0; i < nRows; i++ ) {
r = rows[ i ] * s0;
r0 = o0 + rows[i]*s0;
for ( j = 0; j < nCols; j++ ) {
idx = r + cols[j]*s1;
this.data[ idx ] = clbk.call( ctx, this.data[ idx ], rows[i], cols[j], idx );
k = r0 + cols[j]*s1;
this.data[ k ] = clbk.call( ctx, this.data[ k ], rows[i], cols[j], k );
}

@@ -81,7 +82,8 @@ }

s3 = mat.strides[ 1 ];
o1 = mat.offset;
for ( i = 0; i < nRows; i++ ) {
r = rows[ i ] * s0;
mr = i * s2;
r0 = o0 + rows[i]*s0;
r1 = o1 + i*s2;
for ( j = 0; j < nCols; j++ ) {
this.data[ r + cols[j]*s1 ] = mat.data[ mr + j*s3 ];
this.data[ r0 + cols[j]*s1 ] = mat.data[ r1 + j*s3 ];
}

@@ -93,5 +95,5 @@ }

for ( i = 0; i < nRows; i++ ) {
r = rows[ i ] * s0;
r0 = o0 + rows[i]*s0;
for ( j = 0; j < nCols; j++ ) {
this.data[ r + cols[j]*s1 ] = val;
this.data[ r0 + cols[j]*s1 ] = val;
}

@@ -98,0 +100,0 @@ }

@@ -18,9 +18,9 @@ 'use strict';

str = '',
r,
o,
i, j;
for ( i = 0; i < nRows; i++ ) {
r = i * s0;
o = this.offset + i*s0;
for ( j = 0; j < nCols; j++ ) {
str += this.data[ r + j*s1 ];
str += this.data[ o + j*s1 ];
if ( j < n ) {

@@ -27,0 +27,0 @@ str += ',';

{
"name": "dstructs-matrix",
"version": "1.0.0",
"version": "2.0.0",
"description": "Matrices.",

@@ -5,0 +5,0 @@ "author": {

@@ -139,2 +139,15 @@ Matrix

<a name="matrix-offset"></a>
#### offset
A property returning the `offset` used to index into the underlying data store.
``` javascript
var offset = mat.offset;
// returns 0
```
By default, the `offset` is `0`. While not __read-only__, most consumers should treat the `offset` as a __read-only__ property.
<a name="matrix-strides" class="read-only-property"></a>

@@ -150,2 +163,5 @@ #### strides

While not __frozen__, most consumers should treat the `strides` elements as __read-only__ elements.
<a name="matrix-length" class="read-only-property"></a>

@@ -644,5 +660,5 @@ #### length

#### mat.constructor( data, shape, dtype )
#### mat.constructor( data, dtype, shape, offset, strides )
Creates a new `Matrix` having a specified `shape`, `dtype`, and underlying typed `data` store.
Creates a new `Matrix` having a specified `shape`, `offset`, `strides`, `dtype`, and underlying typed `data` store.

@@ -661,3 +677,3 @@ ``` javascript

var mat2 = new mat1.constructor( data, [2,5], 'float32' );
var mat2 = new mat1.constructor( data, mat1.dtype, [2,5], 0, [5,1] );
/*

@@ -664,0 +680,0 @@ [ 0 0 0 0 0

SocketSocket SOC 2 Logo

Product

  • Package Alerts
  • Integrations
  • Docs
  • Pricing
  • FAQ
  • Roadmap
  • Changelog

Packages

npm

Stay in touch

Get open source security insights delivered straight into your inbox.


  • Terms
  • Privacy
  • Security

Made with ⚡️ by Socket Inc