I'm trying to create a program in C# that allows for arithmetic of Complex Numbers. Just adding, subtracting, multiplying and dividing. Anyone got any suggestions?

1

Create your own class with + - * / overloaded.

This is an example for matrices but it's similar (complex will be less complicated).

Give it a go and post back when you get stuck (or if you have code already post it and someone can look it over).

1

This is how I did it some months ago :

```
using System;
namespace MarMath
{
/// <summary>
///
///
/// struct for complex numbers
///
/// See also [url]http://en.wikipedia.org/wiki/Complex_number[/url]
/// A collection of as much functions and issues I could find about it on the web.
/// Special thanks to Scott Knake for his assistance on IFormattable and IConvertible.
/// © Euler, DeMoivre and others...
///
/// Version 1.00 8 augustus 2009
///
/// Danny Marivoet
///
/// </summary>
struct Complex : IFormattable, IConvertible
{
#region Fields.
/// <summary>
/// The real and imaginary parts of this struct
/// If the imaginary part is zero then a complex number is a real number.
/// The imaginary part is always multiplied with a constant mostly called i.
/// i^2 = -1
/// </summary>
private double _real, _imag;
// A static random field which uses a timeseed
private static Random rnd = new Random();
#endregion
#region Complex contructors.
/// <summary>
/// Initialize a complex number with a real and imaginary part
/// </summary>
/// <param name="real"></param>
/// <param name="imag"></param>
public Complex(double real, double imag)
{
this._real = real; this._imag = imag;
}
/// <summary>
/// Create a new complex number out of an existing one
/// </summary>
/// <param name="z"></param>
public Complex(Complex z)
{
this._real = z.realpart; this._imag = z.imaginarypart;
}
#endregion
#region Properties.
/// <summary>
/// Get the real part of a complex number.
/// </summary>
public double realpart
{
get { return _real; }
set { _real = value; }
}
/// <summary>
/// Get the imaginary part of a complex number.
/// </summary>
public double imaginarypart
{
get { return _imag; }
set { _imag = value; }
}
/// <summary>
/// I^2 is equal to -1.
/// </summary>
public static Complex I
{
get { return new Complex(0.0, 1.0); }
}
/// <summary>
/// Return a zero comlex number
/// </summary>
public static Complex Zero
{
get { return new Complex(0.0, 0.0); }
}
/// <summary>
/// Get the complex conjugate of a complex number.
/// It is obtained by changing the sign of the imaginary part.
/// </summary>
public Complex Conjugate
{
get { return new Complex(_real, -_imag); }
}
/// <summary>
/// Also called Norm or absolute value.
/// </summary>
public double Modulus
{
get { return System.Math.Sqrt(_real * _real + _imag * _imag); }
}
/// <summary>
/// Returns the square of the Modulus
/// </summary>
public double AbsoluteSquare
{
get { return _real * _real + _imag * _imag; }
}
/// <summary>
/// Also called angle.
/// </summary>
public double Argument
{
get
{
if (_imag == 0 && _real < 0)
return Math.PI;
if (_imag == 0 && _real >= 0)
return 0;
return System.Math.Atan2(_imag, _real);
}
}
/// <summary>
/// Returns one divided by the current value.
/// </summary>
public Complex Reciprocal
{
get
{
if (_real == 0d && _imag == 0d)
throw new DivideByZeroException();
double div = _real * _real + _imag * _imag;
return new Complex(_real / div, -_imag / div);
}
}
#endregion
```

This is just the start and not the complete code. I leave it to you to implement the operator overloading. You will also have to override the ToString method, because of the special format of complex numbers when written.

This topic has been dead for over six months. Start a new discussion instead.

Recommended Topics

Hi guys!

i think this is usefull, a lot of the users have problems with .Net Framework installation,

Error code: HRESULT 0xc8000222

Do these to solve:

a: Click Start > ...

There are a number of very old threads on CUDA so I'm starting a new one rather than resurrecting an old one.

Does anyone here have any experience setting up ...

ive been develop a project known as ordering system

i need to delete data from db by clicking the button

ive follow a tutorial from utube and to the exact ...