Source: functions_complex.js

"use strict";


/**
* Generate a complex number, give the real and imaginary parts.
* @return complex number
* @param {number} Re real part of a complex
* @param {number} Im imaginary part of a complex number,
* @customfunction
*/
function complex(Re,Im) {    
  return cmplx(Re,Im);  
} 

/**
* Add two complex numbers (a+b).
* @return complex number (the sum)
* @param {complex} first number
* @param {complex} second number
* @customfunction
*/
function complexAdd(a,b){ 
  var json =  httpGetCall("utilities/complex/add/" + (complexRe(a)+0) + "," + (complexIm(a)+0) + "/" + (complexRe(b)+0) + "," + (complexIm(b)+0));
  var data = JSON.parse(json);      
  return cmplx(data.m_real, data.m_imaginary);
}

/**
* Find the conjugate of a complex number.
* @return complex number (the conjugate)
* @param {complex} complex number
* @customfunction
*/
function complexConjugate(complexNumber){
  var json =  httpGetCall("utilities/complex/conjugate/" + (complexRe(complexNumber)+0) + "," + (complexIm(complexNumber)+0) );
  var data = JSON.parse(json);      
  return cmplx(data.m_real, data.m_imaginary);
}

/**
* Get the complex representation of current, from the magnitude and power factor.
* For d.c. power factor = 1.  +ve power factor indicates lagging, -ve leading.
* @return complex number representing the current
* @param {number} current magnitude of the current 
* @param {number} powerFactor the power factor of the current (set = 1 for d.c.).
* @customfunction
*/
function complexCurrentPF(current,powerFactor){
  var json =  httpGetCall("electrical/current/complex/" + current + "/" + powerFactor);
  var data = JSON.parse(json);    
  return cmplx(data.Value.m_real, data.Value.m_imaginary);
}


/**
* Divide two complex numbers (a/b).
* @return complex number (the division)
* @param {complex} first number
* @param {complex} second number
* @customfunction
*/
function complexDivide(a,b){
  
  var json =  httpGetCall("utilities/complex/divide/" + (complexRe(a)+0) + "," + (complexIm(a)+0) + "/" + (complexRe(b)+0) + "," + (complexIm(b)+0));
  var data = JSON.parse(json);    
  
  return cmplx(data.m_real, data.m_imaginary);
}

/**
* Get the imaginary part of a complex number, give the full number (both real and imaginary parts).
* @return imaginary part
* @param {complex} complex number
* @customfunction
*/
function complexIm(complexNumber){
  return cmplxIm(complexNumber);
}

/**
* Get the magnitude part of a complex number, give the full number (both real and imaginary parts).
* @return magnitude of the number
* @param {complex} complex number
* @customfunction
*/
function complexMag(complexNumber){
  var json =  httpGetCall("utilities/complex/" + (complexRe(complexNumber)+0) + "," + (complexIm(complexNumber)+0));
  var data = JSON.parse(json);    
  return data.m_magnitude;
}

/**
* Generate a complex number, give the magnitude and phase angle, radians.
* @return complex number
* @param {number} magnitude magnitude of the complex number
* @param {number} phase phase angle of the complex number, radians
* @customfunction
*/
function complexMagPhase(magnitude,phase) {    
  var json =  httpGetCall("utilities/complex/polar/" + magnitude + "," + phase);
  var data = JSON.parse(json);      
  return cmplx(data.m_real, data.m_imaginary);
} 

/**
* Generate a complex number, give the magnitude and phase angle, degrees.
* @return complex number
* @param {number} magnitude magnitude of the complex number
* @param {number} phase phase angle of the complex number, degrees
* @customfunction
*/
function complexMagPhaseDeg(magnitude,phase) {    
  var json =  httpGetCall("utilities/complex/polar/" + magnitude + "," + phase + "/degrees");
  var data = JSON.parse(json);      
  return cmplx(data.m_real, data.m_imaginary);
}


/**
* Multiple two complex numbers (a*b).
* @return complex number (the product)
* @param {complex} first number
* @param {complex} second number
* @customfunction
*/
function complexMultiply(a,b){ 
  var json =  httpGetCall("utilities/complex/multiply/" + (complexRe(a)+0) + "," + (complexIm(a)+0) + "/" + (complexRe(b)+0) + "," + (complexIm(b)+0));
  var data = JSON.parse(json);      
  return cmplx(data.m_real, data.m_imaginary);
}

/**
* Get the power factor (cos(phase)) part of a complex number, give the full number (both real and imaginary parts).
* Normally used with electrical current.
* @return power factor
* @param {complex} complex number
* @customfunction
*/
function complexPF(complexNumber){
  var json =  httpGetCall("utilities/complex/" + (complexRe(complexNumber)+0) + "," + (complexIm(complexNumber)+0));
  var data = JSON.parse(json);    
  return data.m_cosPhase;
}


/**
* Get the phase (radians) part of a complex number, give the full number (both real and imaginary parts).
* @return phase of the number, radians
* @param {complex} complex number
* @customfunction
*/
function complexPhase(complexNumber){
  var json =  httpGetCall("utilities/complex/" + (complexRe(complexNumber)+0) + "," + (complexIm(complexNumber)+0));
  var data = JSON.parse(json);    
  return data.m_phase;
}


/**
* Get the phase (degrees) part of a complex number, give the full number (both real and imaginary parts).
* @return phase of the number, degrees
* @param {complex} complex number
* @customfunction
*/
function complexPhaseDeg(complexNumber){
  var json =  httpGetCall("utilities/complex/" + (complexRe(complexNumber)+0) + "," + (complexIm(complexNumber)+0));
  var data = JSON.parse(json);    
  return data.m_phase_degree;
}

/**
* Get the real part of a complex number, give the full number (both real and imaginary parts).
* @return real part
* @param {complex} complex number
* @customfunction
*/
function complexRe(complexNumber){
  return cmplxRe(complexNumber);
}

/**
* Find the reciprocal of a complex number.
* @return complex number (the reciprocal)
* @param {complex} complex number
* @customfunction
*/
function complexReciprocal(complexNumber){
  var json =  httpGetCall("utilities/complex/reciprocal/" + (complexRe(complexNumber)+0) + "," + (complexIm(complexNumber)+0) );
  var data = JSON.parse(json);     
  return cmplx(data.m_real, data.m_imaginary);
}


/**
* Generate a complex number, give the real and imaginary parts.
* @return complex number
* @param {number} Re real part of a complex
* @param {number} Im imaginary part of a complex number,
* @customfunction
*/
function complexReIm(Re,Im) {    
  return cmplx(Re,Im);  
} 

/**
* Round a complex number to a given number of decimal places.
* @return complex number
* @param {complex} complex number
* @param {number} dp number of decimal places to round to
* @customfunction
*/
function complexRound(complexNumber,dp){
  var Re = Number(cmplxRe(complexNumber)).toFixed(dp);
  var Im = Number(cmplxIm(complexNumber)).toFixed(dp);
  return cmplx(Re,Im);
}


/**
* Subtract one complex number from another (a-b).
* @return complex number (the difference)
* @param {complex} first number
* @param {complex} second number
* @customfunction
*/
function complexSubtract(a,b){
  var json =  httpGetCall("utilities/complex/subtract/" + (complexRe(a)+0) + "," + (complexIm(a)+0) + "/" + (complexRe(b)+0) + "," + (complexIm(b)+0));
  var data = JSON.parse(json);     
  return cmplx(data.m_real, data.m_imaginary);
}