time-to-botec

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

complex64.js (4309B)


      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 isNumber = require( '@stdlib/assert/is-number' ).isPrimitive;
     24 var defineProperty = require( '@stdlib/utils/define-property' );
     25 var setReadOnly = require( '@stdlib/utils/define-nonenumerable-read-only-property' );
     26 var Float32Array = require( '@stdlib/array/float32' );
     27 var toString = require( './tostring.js' ); // eslint-disable-line stdlib/no-redeclare
     28 var toJSON = require( './tojson.js' );
     29 
     30 
     31 // MAIN //
     32 
     33 /**
     34 * 64-bit complex number constructor.
     35 *
     36 * @constructor
     37 * @param {number} real - real component
     38 * @param {number} imag - imaginary component
     39 * @throws {TypeError} must invoke using the `new` keyword
     40 * @throws {TypeError} real component must be a number primitive
     41 * @throws {TypeError} imaginary component must be a number primitive
     42 * @returns {Complex64} 64-bit complex number
     43 *
     44 * @example
     45 * var z = new Complex64( 5.0, 3.0 );
     46 * // returns <Complex64>
     47 */
     48 function Complex64( real, imag ) {
     49 	var view;
     50 	if ( !( this instanceof Complex64 ) ) {
     51 		throw new TypeError( 'invalid invocation. Constructor must be called with the `new` keyword.' );
     52 	}
     53 	if ( !isNumber( real ) ) {
     54 		throw new TypeError( 'invalid argument. Real component must be a number primitive. Value: `'+real+'`.' );
     55 	}
     56 	if ( !isNumber( imag ) ) {
     57 		throw new TypeError( 'invalid argument. Imaginary component must be a number primitive. Value: `'+imag+'`.' );
     58 	}
     59 	defineProperty( this, 're', {
     60 		'configurable': false,
     61 		'enumerable': true,
     62 		'get': getReal
     63 	});
     64 
     65 	defineProperty( this, 'im', {
     66 		'configurable': false,
     67 		'enumerable': true,
     68 		'get': getImag
     69 	});
     70 	view = new Float32Array( 2 );
     71 	view[ 0 ] = real;
     72 	view[ 1 ] = imag;
     73 
     74 	return this;
     75 
     76 	/**
     77 	* Returns a real component.
     78 	*
     79 	* @private
     80 	* @returns {number} real component
     81 	*/
     82 	function getReal() {
     83 		return view[ 0 ];
     84 	}
     85 
     86 	/**
     87 	* Returns an imaginary component.
     88 	*
     89 	* @private
     90 	* @returns {number} imaginary component
     91 	*/
     92 	function getImag() {
     93 		return view[ 1 ];
     94 	}
     95 }
     96 
     97 /**
     98 * Size (in bytes) of each component.
     99 *
    100 * @name BYTES_PER_ELEMENT
    101 * @memberof Complex64
    102 * @type {integer}
    103 * @returns {integer} size of each component
    104 *
    105 * @example
    106 * var nbytes = Complex64.BYTES_PER_ELEMENT;
    107 * // returns 4
    108 */
    109 setReadOnly( Complex64, 'BYTES_PER_ELEMENT', 4 );
    110 
    111 /**
    112 * Size (in bytes) of each component.
    113 *
    114 * @name BYTES_PER_ELEMENT
    115 * @memberof Complex64.prototype
    116 * @type {integer}
    117 * @returns {integer} size of each component
    118 *
    119 * @example
    120 * var z = new Complex64( 5.0, 3.0 );
    121 *
    122 * var nbytes = z.BYTES_PER_ELEMENT;
    123 * // returns 4
    124 */
    125 setReadOnly( Complex64.prototype, 'BYTES_PER_ELEMENT', 4 );
    126 
    127 /**
    128 * Length (in bytes) of a complex number.
    129 *
    130 * @name byteLength
    131 * @memberof Complex64.prototype
    132 * @type {integer}
    133 * @returns {integer} byte length
    134 *
    135 * @example
    136 * var z = new Complex64( 5.0, 3.0 );
    137 *
    138 * var nbytes = z.byteLength;
    139 * // returns 8
    140 */
    141 setReadOnly( Complex64.prototype, 'byteLength', 8 );
    142 
    143 /**
    144 * Serializes a complex number as a string.
    145 *
    146 * @name toString
    147 * @memberof Complex64.prototype
    148 * @type {Function}
    149 * @returns {string} serialized complex number
    150 *
    151 * @example
    152 * var z = new Complex64( 5.0, 3.0 );
    153 *
    154 * var str = z.toString();
    155 * // returns '5 + 3i'
    156 */
    157 setReadOnly( Complex64.prototype, 'toString', toString );
    158 
    159 /**
    160 * Serializes a complex number as a JSON object.
    161 *
    162 * ## Notes
    163 *
    164 * -   `JSON.stringify()` implicitly calls this method when stringifying a `Complex64` instance.
    165 *
    166 *
    167 * @name toJSON
    168 * @memberof Complex64.prototype
    169 * @type {Function}
    170 * @returns {Object} serialized complex number
    171 *
    172 * @example
    173 * var z = new Complex64( 5.0, 3.0 );
    174 *
    175 * var obj = z.toJSON();
    176 * // returns { 'type': 'Complex64', 're': 5.0, 'im': 3.0 }
    177 */
    178 setReadOnly( Complex64.prototype, 'toJSON', toJSON );
    179 
    180 
    181 // EXPORTS //
    182 
    183 module.exports = Complex64;