Flex/ActionScript Sample
This class defines a complex number and can perform operations on it, such as mulitplication and addition.
CComplexNumber.as Sample:
package ComplexNumber
{
public class CComplexNumber
{
public var _real:Number;
public var _img:Number;
public function CComplexNumber(real:Number=0,img:Number=0)
{
_real = real;
_img = img;
}
public function r() : Number
{
return _real;
}
public function i() : Number
{
return _img;
}
public function multc(n1:CComplexNumber,n2:CComplexNumber) : CComplexNumber
{
var n3:CComplexNumber = new CComplexNumber(0,0);
n3._real = n1._real*n2._real - n1._img*n2._img;
n3._img = n2._real*n1._img + n1._real*n2._img;
return n3;
}
public function divn(c:CComplexNumber,n:Number) : void
{
c._img /= n;
c._real /= n;
}
public function subc(n1:CComplexNumber,n2:CComplexNumber) : CComplexNumber
{
var n3:CComplexNumber = new CComplexNumber(0,0);
n3._real = n1._real - n2._real;
n3._img = n1._img - n2._img;
return n3;
}
public function addc(n1:CComplexNumber,n2:CComplexNumber) : CComplexNumber
{
var n3:CComplexNumber = new CComplexNumber(0,0);
n3._real = n1._real + n2._real;
n3._img = n1._img + n2._img;
return n3;
}
public function mag() : Number
{
var val:Number;
val = _real*_real + _img*_img;
val = Math.sqrt(val);
return val;
}
//return phase in radians
public function phase() : Number
{
var hyp:Number;
var theta:Number;
hyp = mag();
theta = Math.acos(_real/hyp);
if(_img < 0){
theta = 6.283185 - theta;
}
return theta;
}
}
}