Thanks for the library. I have updated it a bit and added a couple extensions. Here is the code:
complex.h:
#pragma once
using namespace System;
#include <complex>
namespace ComplexMath
{
public value class Complex
{
private:
double _real;
double _imag;
static Complex Convert(std::complex<double> c);
public:
Complex(double real, double imag);
Complex(double real):_real(real),_imag(0)
{
}
Complex(std::complex<double>& Other);
virtual System::String^ ToString() override;
virtual System::String^ ToString(System::String^ Format);
static Complex operator +(Complex cLeft);
static Complex operator (Complex cLeft);
static Complex Plus(Complex cLeft)
{return operator +(cLeft);}
static Complex Negative(Complex cLeft)
{return operator (cLeft);}
static bool operator ==( Complex cLeft, Complex cRight);
static bool operator ==( Complex cLeft, double right);
static bool operator ==( double left, Complex cRight);
bool Equals( double number)
{return operator ==( *this,number);}
bool Equals( Complex c)
{return operator ==( *this,c);}
static bool Equals( Complex cLeft, Complex cRight)
{return operator ==( cLeft, cRight);}
static bool Equals( Complex cLeft, double right)
{return operator ==( cLeft, right);}
static bool Equals( double left, Complex cRight)
{return operator ==( left, cRight);}
static bool operator != ( Complex cLeft, Complex cRight)
{ return !operator ==(cLeft,cRight); }
static bool operator !=( Complex cLeft, double right)
{ return !operator ==(cLeft,right); }
static bool operator !=( double left, Complex cRight)
{ return !operator ==(left,cRight); }
static bool NotEqual( Complex cLeft, Complex cRight)
{ return !operator ==(cLeft,cRight); }
static bool NotEqual( Complex cLeft, double right)
{ return !operator ==(cLeft,right); }
static bool NotEqual( double left, Complex cRight)
{ return !operator ==(left,cRight); }
static Complex operator *( Complex cLeft, Complex cRight);
static Complex operator *( Complex cLeft, double right);
static Complex operator *( double left , Complex cRight);
static Complex Multiply( Complex cLeft, Complex cRight)
{ return operator *( cLeft, cRight); }
static Complex Multiply( Complex cLeft, double right)
{ return operator *( cLeft, right);}
static Complex Multiply( double left , Complex cRight)
{ return operator *( left , cRight); }
static Complex operator /( Complex cLeft , Complex cRight);
static Complex operator /( Complex cLeft , double right);
static Complex operator /( double left , Complex cRight);
static Complex Divide( Complex cLeft, Complex cRight)
{ return operator /( cLeft, cRight); }
static Complex Divide( Complex cLeft, double right)
{ return operator /( cLeft, right);}
static Complex Divide( double left , Complex cRight)
{ return operator /( left , cRight); }
static Complex operator +( Complex cLeft, Complex cRight);
static Complex operator +( Complex cLeft, double right);
static Complex operator +( double left, Complex cRight);
static Complex Add( Complex cLeft, Complex cRight)
{ return operator +( cLeft, cRight); }
static Complex Add( Complex cLeft, double right)
{ return operator +( cLeft, right);}
static Complex Add( double left , Complex cRight)
{ return operator +( left , cRight); }
static Complex operator ( Complex cLeft, Complex cRight);
static Complex operator ( Complex cLeft, double right);
static Complex operator ( double left, Complex cRight);
static Complex Subtract( Complex cLeft, Complex cRight)
{ return operator ( cLeft, cRight); }
static Complex Subtract( Complex cLeft, double right)
{ return operator ( cLeft, right);}
static Complex Subtract( double left , Complex cRight)
{ return operator ( left , cRight); }
property double x
{
double get()
{
return _real;
}
void set(double value)
{
_real=value;
}
}
property double y
{
double get()
{
return _imag;
}
void set(double value)
{
_imag=value;
}
}
property double Magnitude
{
double get()
{
return _real*_real+_imag*_imag;
}
void set(double value)
{
double arg = Complex::Arg(*this);
std::complex<double> c = std::polar(value,arg);
_real=c.real();
_imag=c.imag();
}
}
static double Abs(Complex c);
static double Arg(Complex c);
static Complex Conj(Complex c);
static double Imag(Complex c)
{ return c._imag;}
static double Real(Complex c)
{ return c._real;}
static Complex Polar(double r, double theta);
static Complex Polar(double r);
static Complex Cos(Complex c);
static Complex Cosh(Complex c);
static Complex Exp(Complex c);
static Complex Log(Complex c);
static Complex Log10(Complex c);
static double Norm(Complex c);
static Complex Pow(Complex base, double power);
static Complex Pow(Complex base, Complex power);
static Complex Pow(double base, Complex power);
static Complex Sin(Complex c);
static Complex Sinh(Complex c);
static Complex Sqrt(Complex c);
static Complex Tan(Complex c);
static Complex Tanh(Complex c);
static Complex Parse(String^ Text);
};
}
complex.cpp:
#include "stdafx.h"
#include "complex.h"
using namespace System;
using namespace std;
namespace ComplexMath
{
Complex::Complex(double real, double imag):_real(real),_imag(imag)
{
}
Complex::Complex(std::complex<double>& Other):_real(Other.real()),_imag(Other.imag())
{
}
System::String^ Complex::ToString()
{
return String::Format("({0:g6},{1:g6})",_real,_imag);
}
System::String^ Complex::ToString(System::String^ Format)
{
return String::Format(Format,_real,_imag);
}
Complex Complex::operator +(Complex cLeft)
{
return cLeft;
}
Complex Complex::operator (Complex cLeft)
{
cLeft._imag=cLeft._imag;
cLeft._real=cLeft._real;
return cLeft;
}
bool Complex::operator ==( Complex cLeft, Complex cRight)
{
return (cLeft._imag == cRight._imag) && (cLeft._real == cRight._real);
}
bool Complex::operator ==( Complex cLeft, double right)
{
return (cLeft._real == right) && (cLeft._imag == 0);
}
bool Complex::operator ==( double left, Complex cRight)
{
return (left == cRight._real) && (cRight._imag == 0);
}
Complex Complex::operator +(Complex cLeft, Complex cRight)
{
cLeft._real+=cRight._real;
cLeft._imag+=cRight._imag;
return cLeft;
}
Complex Complex::operator +( Complex cLeft, double right)
{
cLeft._real+=right;
return cLeft;
}
Complex Complex::operator +( double left, Complex cRight)
{
cRight._real+=left;
return cRight;
}
Complex Complex::operator ( Complex cLeft, Complex cRight)
{
cLeft._real=cRight._real;
cLeft._imag=cRight._imag;
return cLeft;
}
Complex Complex::operator ( Complex cLeft, double right)
{
cLeft._real=right;
return cLeft;
}
Complex Complex::operator ( double left, Complex cRight)
{
return cRight+left;
}
Complex Complex::operator *(Complex c1, Complex c2)
{
return Complex(c1._real*c2._realc1._imag*c2._imag,c1._real*c2._imag+c2._real*c1._imag);
}
Complex Complex::operator *(Complex cLeft, double right)
{
return Complex(right*cLeft._real,right*cLeft._imag);
}
Complex Complex::operator *( double left , Complex cRight)
{
complex<double> C1(cRight._real,cRight._imag);
return Complex(left*C1);
}
Complex Complex::operator /(Complex c1, Complex c2)
{
complex<double> C1(c1._real,c1._imag);
complex<double> C2(c2._real,c2._imag);
return Convert(C1/C2);
}
Complex Complex::operator /( Complex cLeft, double right)
{
complex<double> C1(cLeft._real,cLeft._imag);
return Convert(C1/right);
}
Complex Complex::operator /( double left , Complex cRight)
{
complex<double> C1(cRight._real,cRight._imag);
return Convert(left/C1);
}
double Complex::Abs(Complex c)
{
std::complex<double> c1(c._real,c._imag);
double rval = abs(c1);
return rval;
}
double Complex::Arg(Complex c)
{
std::complex<double> c1(c._real,c._imag);
double rval = arg(c1);
return rval;
}
Complex Complex::Conj(Complex c)
{
std::complex<double> c1(c._real,c._imag);
std::complex<double> c2 = conj(c1);
return Convert(c2);
}
Complex Complex::Cos(Complex c)
{
std::complex<double> c1(c._real,c._imag);
std::complex<double> c2 = cos(c1);
return Convert(c2);
}
Complex Complex::Cosh(Complex c)
{
std::complex<double> c1(c._real,c._imag);
std::complex<double> c2 = cosh(c1);
return Convert(c2);
}
Complex Complex::Exp(Complex c)
{
std::complex<double> c1(c._real,c._imag);
std::complex<double> c2 = exp(c1);
return Convert(c2);
}
Complex Complex::Log(Complex c)
{
std::complex<double> c1(c._real,c._imag);
std::complex<double> c2 = log(c1);
return Convert(c2);
}
Complex Complex::Log10(Complex c)
{
std::complex<double> c1(c._real,c._imag);
std::complex<double> c2 = std::log10(c1);
return Convert(c2);
}
double Complex::Norm(Complex c)
{
std::complex<double> c1(c._real,c._imag);
return std::norm(c1);
}
Complex Complex::Polar(double r)
{
return Polar(r,0);
}
Complex Complex::Polar(double r, double theta)
{
std::complex <double> c1 ( polar (r,theta ) );
return Convert(c1);
}
Complex Complex::Pow(Complex base, double power)
{
std::complex<double> c1(base._real,base._imag);
return Convert(std::pow(c1,power));
}
Complex Complex::Pow(Complex base, Complex power)
{
std::complex<double> c1(base._real,base._imag);
std::complex<double> c2(power._real,power._imag);
return Convert(std::pow(c1,c2));
}
Complex Complex::Pow(double base, Complex power)
{
std::complex<double> c1(power._real,power._imag);
return Convert(std::pow(base,c1));
}
Complex Complex::Sin(Complex c)
{
std::complex<double> c1(c._real,c._imag);
std::complex<double> c2 = sin(c1);
return Convert(c2);
}
Complex Complex::Sinh(Complex c)
{
std::complex<double> c1(c._real,c._imag);
std::complex<double> c2 = sinh(c1);
return Convert(c2);
}
Complex Complex::Sqrt(Complex c)
{
std::complex<double> c1(c._real,c._imag);
std::complex<double> c2 = sqrt(c1);
return Convert(c2);
}
Complex Complex::Tan(Complex c)
{
std::complex<double> c1(c._real,c._imag);
std::complex<double> c2 = tan(c1);
return Convert(c2);
}
Complex Complex::Tanh(Complex c)
{
std::complex<double> c1(c._real,c._imag);
std::complex<double> c2 = tanh(c1);
return Convert(c2);
}
Complex Complex::Parse(String^ Text)
{
String^ txt = Text>Replace("(","");
txt=txt>Replace(")","");
array<String^>^ parts = txt>Split(',');
return Complex(Double::Parse(parts[0]),Double::Parse(parts[1]));
}
Complex Complex::Convert(complex<double> c)
{
Complex rval = Complex(real(c),imag(c));
return rval;
}
}
