12,944,991 members (50,421 online)
alternative version

#### Stats

29.4K views
13 bookmarked
Posted 3 Jun 2007

# A C# class for complex numbers

, 3 Jul 2007 CPOL
 Rate this:
Implementation of the most common functions of complex numbers.

## Introduction

Here you go: a simple but mathematically rigorous implementation of complex numbers in one small C# library. No problem in square rooting negative numbers anymore!

## Functions

• Absolute value
• Argument
• Conjugation
• Cosine
• Exponential function
• Exponentiation
• Division
• Hyperbolic functions (Sinh, Cosh, Tanh, Coth, Sech, Csch)
• Logarithm
• Multiplication
• Sine
• Square root
• Subtraction

## Using the code

Either add a reference to CompLib.dll to your project, or directly use the class Complex.cs within your project.

The actual usage is intuitive:

```Complex I = Complex.I; // imaginary unit
Complex a = new Complex(1, 3); // inits a = 1+3i
Complex a2 = 1 + 3 * I; // a equals a2

Complex z = Complex.Pow((Complex.Sin(1/(1+I))), 1/3);```

## Points of interest

One more thing: Complex logarithm is not a unique operation; the main value is computed as is common in the CAS world. E.g., the equation z^4 = -1 has four complex solutions, but only one is returned when trying "`z = Complex.Sqrt(Complex.Sqrt(-1));`" (as does Maple, for instance). This is due to the computation of the exponentiation:

`Pow(a,b) := Exp(b * Log(a))`

## History

### Coming soon

• init complex number with format string such as "3+4i" using regex.

### Update July 3, 2007 #2

• Major bug in Arg() fixed (thanks Petr Stanislav!); this affects `Log()`, `Pow()`, and `Sqrt()`.

### Update June 10, 2007

• Replaced ^-operator with "`public static Complex Pow`", similar to `Math.Pow`.

### Update June 7, 2007

• Added `Zero` and `One` as constants (e.g., use "`Complex z = Complex.One;`" instead of "`Complex z = new Complex(1)`").
• Major bug of division operation removed (using `a/b = a*Conj(b)*(1/(Abs(b)*Abs(b))` now).
• `ToString` method bug fixed.

## Share

 Germany
No Biography provided

## You may also be interested in...

 Pro

 First Prev Next
 code licence enrico.deg10-Jun-10 22:53 enrico.deg 10-Jun-10 22:53
 struct instead of class Member 190663510-Jun-08 1:17 Member 1906635 10-Jun-08 1:17
 Re: struct instead of class PIEBALDconsult2-Apr-09 12:56 PIEBALDconsult 2-Apr-09 12:56
 7-Zip will extract Dom Rositas20-Jun-07 16:22 Dom Rositas 20-Jun-07 16:22
 Re: 7-Zip will extract [modified] hanzzoid3-Jul-07 2:19 hanzzoid 3-Jul-07 2:19
 WinZip unknown compression type Dom Rositas20-Jun-07 7:49 Dom Rositas 20-Jun-07 7:49
 Cannot extract files from ZIP archive Tom14-Jun-07 3:54 Tom1 4-Jun-07 3:54
 Re: Cannot extract files from ZIP archive Tom118-Jun-07 5:39 Tom1 18-Jun-07 5:39
 Exponentiation ALWAYS binds more strongly than multiplication. sherifffruitfly3-Jun-07 11:08 sherifffruitfly 3-Jun-07 11:08
 Re: Exponentiation ALWAYS binds more strongly than multiplication. Keith Rule3-Jun-07 20:16 Keith Rule 3-Jun-07 20:16
 sherifffruitfly wrote:That shouldn't even be a question up for discussion. I think you are being a bit harsh here. I think this is a legitimate question. Unfortunately when you use C# operator overloading you don't have an opportunity to change the operator precedence. Though it's nice to use operators for functions like exponents, it's not really feasible when operator precedence is an issue. That's why C# has a Math.Pow() method. I'd recommend creating a Pow() method instead of using operator overloading. Good Luck Keith Rule
 Re: Exponentiation ALWAYS binds more strongly than multiplication. peterchen3-Jun-07 22:20 peterchen 3-Jun-07 22:20
 Re: Exponentiation ALWAYS binds more strongly than multiplication. hanzzoid3-Jul-07 2:15 hanzzoid 3-Jul-07 2:15
 Re: Exponentiation ALWAYS binds more strongly than multiplication. PIEBALDconsult2-Apr-09 12:59 PIEBALDconsult 2-Apr-09 12:59
 Re: Exponentiation ALWAYS binds more strongly than multiplication. sherifffruitfly2-Apr-09 13:22 sherifffruitfly 2-Apr-09 13:22
 Re: Exponentiation ALWAYS binds more strongly than multiplication. PIEBALDconsult2-Apr-09 16:28 PIEBALDconsult 2-Apr-09 16:28
 Last Visit: 31-Dec-99 18:00     Last Update: 23-May-17 4:45 Refresh 1