time-to-botec

Benchmark sampling in different programming languages
Log | Files | Refs | README

matrix.js (2739B)


      1 /**
      2 * @license Apache-2.0
      3 *
      4 * Copyright (c) 2018 The Stdlib Authors.
      5 *
      6 * Licensed under the Apache License, Version 2.0 (the "License");
      7 * you may not use this file except in compliance with the License.
      8 * You may obtain a copy of the License at
      9 *
     10 *    http://www.apache.org/licenses/LICENSE-2.0
     11 *
     12 * Unless required by applicable law or agreed to in writing, software
     13 * distributed under the License is distributed on an "AS IS" BASIS,
     14 * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
     15 * See the License for the specific language governing permissions and
     16 * limitations under the License.
     17 */
     18 
     19 'use strict';
     20 
     21 // MODULES //
     22 
     23 var Int32Array = require( '@stdlib/array/int32' );
     24 var Float64Array = require( '@stdlib/array/float64' );
     25 var setReadOnly = require( '@stdlib/utils/define-nonenumerable-read-only-property' );
     26 
     27 
     28 // MAIN //
     29 
     30 /**
     31 * Returns a Matrix instance.
     32 *
     33 * @private
     34 * @constructor
     35 * @param {NumberArray} shape - matrix dimensions/shape
     36 * @param {string} [dtype="float64"] - matrix data type
     37 * @returns {Matrix} Matrix instance
     38 */
     39 function matrix() {
     40 	var dtype;
     41 	var ndims;
     42 	var shape;
     43 	var data;
     44 	var len;
     45 	var mat;
     46 	var i;
     47 
     48 	if ( arguments.length === 1 ) {
     49 		shape = arguments[ 0 ];
     50 	}
     51 	else if ( arguments.length === 2 ) {
     52 		shape = arguments[ 0 ];
     53 		dtype = arguments[ 1 ];
     54 	}
     55 	ndims = shape.length;
     56 	len = 1;
     57 	for ( i = 0; i < ndims; i++ ) {
     58 		len *= shape[ i ];
     59 	}
     60 	// Initialize a zero-filled typed array...
     61 	if ( dtype === 'int32' ) {
     62 		data = new Int32Array( len );
     63 	}
     64 	else {
     65 		data = new Float64Array( len );
     66 	}
     67 
     68 	// Return a new Matrix instance:
     69 	mat = {};
     70 	mat.dtype = dtype;
     71 	mat.shape = shape;
     72 	mat.strides = [ shape[1], 1 ];
     73 	mat.offset = 0;
     74 	mat.ndims = shape.length;
     75 	mat.length = data.length;
     76 	mat.nbytes = data.byteLength;
     77 	mat.data = data;
     78 
     79 	setReadOnly( mat, 'get', get );
     80 	setReadOnly( mat, 'set', set );
     81 	return mat;
     82 
     83 	/**
     84 	* Returns a matrix element based on the provided row and column indices.
     85 	*
     86 	* @private
     87 	* @param {integer} i - row index
     88 	* @param {integer} j - column index
     89 	* @returns {(number|undefined)} matrix element
     90 	*/
     91 	function get( i, j ) {
     92 		/* eslint-disable no-invalid-this */
     93 		var idx = this.offset + ( i*this.strides[0] ) + ( j*this.strides[1] );
     94 		return this.data[ idx ];
     95 	}
     96 
     97 	/**
     98 	* Sets a matrix element based on the provided row and column indices.
     99 	*
    100 	* @private
    101 	* @param {integer} i - row index
    102 	* @param {integer} j - column index
    103 	* @param {number} v - value to set
    104 	* @returns {Matrix} Matrix instance
    105 	*/
    106 	function set( i, j, v ) {
    107 		/* eslint-disable no-invalid-this */
    108 		i = this.offset + ( i*this.strides[0] ) + ( j*this.strides[1] );
    109 		if ( i >= 0 ) {
    110 			this.data[ i ] = v;
    111 		}
    112 		return this;
    113 	}
    114 }
    115 
    116 
    117 // EXPORTS //
    118 
    119 module.exports = matrix;