Not Yet Answered # C# Complex Numbers

jonsca 1,059 ddanbe 2,467 Hey, so I wanna ask how I need to create a method who will remove word if in that word is 2 same chars. Example: "Potato" in this word there is a 2 "o" chars so this word will need to be removed. "Forum" in this word there is no ...

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 article has been dead for over six months. Start a new discussion instead.

Recommended Articles

Help! I want to create a java program that finds the highest even integer among the values entered by the user. Stop asking values when a value less than 1 have been entered. If no even integer is entered, display "No Even Integer"

Here is the sample output that I ...

I am writing a java program that needs to execute shell commands, so I wrote a function that would take the command to execute as a string (ie: "mkdir ~/Folder1") and execute that command with the shell. Here is the function:

```
try
{
Runtime run = Runtime.getRuntime();
Process pr = ...
```