Click here to Skip to main content
Click here to Skip to main content

A C# class for complex numbers

By , 3 Jul 2007
 

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
  • Addition
  • 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 July 3, 2007

  • Added hyperbolic functions.

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.

License

This article, along with any associated source code and files, is licensed under The Code Project Open License (CPOL)

About the Author

hanzzoid
Germany Germany
Member
No Biography provided

Sign Up to vote   Poor Excellent
Add a reason or comment to your vote: x
Votes of 3 or less require a comment

Comments and Discussions

 
Hint: For improved responsiveness ensure Javascript is enabled and choose 'Normal' from the Layout dropdown and hit 'Update'.
You must Sign In to use this message board.
Search this forum  
    Spacing  Noise  Layout  Per page   
Questioncode licencememberenrico.deg10 Jun '10 - 22:53 
Generalstruct instead of classmemberMember 190663510 Jun '08 - 1:17 
GeneralRe: struct instead of classmemberPIEBALDconsult2 Apr '09 - 12:56 
General7-Zip will extractmemberDom Rositas20 Jun '07 - 16:22 
GeneralRe: 7-Zip will extract [modified]memberhanzzoid3 Jul '07 - 2:19 
GeneralWinZip unknown compression typememberDom Rositas20 Jun '07 - 7:49 
GeneralCannot extract files from ZIP archivememberTom14 Jun '07 - 3:54 
GeneralRe: Cannot extract files from ZIP archivememberTom118 Jun '07 - 5:39 
GeneralExponentiation ALWAYS binds more strongly than multiplication.membersherifffruitfly3 Jun '07 - 11:08 
GeneralRe: Exponentiation ALWAYS binds more strongly than multiplication.memberKeith Rule3 Jun '07 - 20:16 
GeneralRe: Exponentiation ALWAYS binds more strongly than multiplication.memberpeterchen3 Jun '07 - 22:20 
GeneralRe: Exponentiation ALWAYS binds more strongly than multiplication.memberhanzzoid3 Jul '07 - 2:15 
GeneralRe: Exponentiation ALWAYS binds more strongly than multiplication.memberPIEBALDconsult2 Apr '09 - 12:59 
GeneralRe: Exponentiation ALWAYS binds more strongly than multiplication.membersherifffruitfly2 Apr '09 - 13:22 
GeneralRe: Exponentiation ALWAYS binds more strongly than multiplication.memberPIEBALDconsult2 Apr '09 - 16:28 

General General    News News    Suggestion Suggestion    Question Question    Bug Bug    Answer Answer    Joke Joke    Rant Rant    Admin Admin   

Permalink | Advertise | Privacy | Mobile
Web01 | 2.6.130516.1 | Last Updated 3 Jul 2007
Article Copyright 2007 by hanzzoid
Everything else Copyright © CodeProject, 1999-2013
Terms of Use
Layout: fixed | fluid