time-to-botec

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

cexp.js (2113B)


      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 copysign = require( './../../../../base/special/copysign' );
     24 var sincos = require( './../../../../base/special/sincos' );
     25 var isnan = require( './../../../../base/assert/is-nan' );
     26 var isInfinite = require( './../../../../base/assert/is-infinite' );
     27 var exp = require( './../../../../base/special/exp' );
     28 var PINF = require( '@stdlib/constants/float64/pinf' );
     29 var NINF = require( '@stdlib/constants/float64/ninf' );
     30 
     31 
     32 // MAIN //
     33 
     34 /**
     35 * Computes the exponential function of a complex number.
     36 *
     37 * @private
     38 * @param {(Array|TypedArray|Object)} out - output array
     39 * @param {number} re - real component
     40 * @param {number} im - imaginary component
     41 * @returns {(Array|TypedArray|Object)} output array
     42 *
     43 * @example
     44 * var out = [ 0.0, 0.0 ];
     45 *
     46 * var v = cexp( out, 0.0, 1.0 );
     47 * // returns [ ~0.540, ~0.841 ]
     48 *
     49 * var bool = ( v === out );
     50 * // returns true
     51 */
     52 function cexp( out, re, im ) {
     53 	var tmp;
     54 	var e;
     55 	if ( isnan( re ) ) {
     56 		out[ 0 ] = NaN;
     57 		out[ 1 ] = ( im === 0.0 ) ? im : re;
     58 	} else if ( isInfinite( im ) ) {
     59 		if ( re === PINF ) {
     60 			out[ 0 ] = -re;
     61 			out[ 1 ] = NaN;
     62 		} else if ( re === NINF ) {
     63 			out[ 0 ] = -0.0;
     64 			out[ 1 ] = copysign( 0.0, im );
     65 		} else {
     66 			out[ 0 ] = NaN;
     67 			out[ 1 ] = NaN;
     68 		}
     69 	} else {
     70 		e = exp( re );
     71 		if ( im === 0.0 ) {
     72 			out[ 0 ] = e;
     73 			out[ 1 ] = im;
     74 		} else {
     75 			sincos( out, im );
     76 			tmp = out[ 0 ];
     77 			out[ 0 ] = out[ 1 ] * e;
     78 			out[ 1 ] = tmp * e;
     79 		}
     80 	}
     81 	return out;
     82 }
     83 
     84 
     85 // EXPORTS //
     86 
     87 module.exports = cexp;