Click here to Skip to main content
6,934,113 members and growing! (9,371 online)
Email Password   helpLost your password?
Languages » C# » General     Intermediate

A C# class for complex numbers

By hanzzoid

Implementation of the most common functions of complex numbers.
C#2.0, Windows, .NET2.0, Visual-Studio, Dev, QA, Design
Posted:3 Jun 2007
Updated:3 Jul 2007
Views:14,928
Bookmarked:12 times
Unedited contribution
printPrint Friendly   add Share
      Discuss Discuss   Broken Article?Report  
13 votes for this article.
Popularity: 3.14 Rating: 2.82 out of 5
3 votes, 23.1%
1
2 votes, 15.4%
2
3 votes, 23.1%
3
2 votes, 15.4%
4
3 votes, 23.1%
5

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 reference to CompLib.dll to your project or directly use the class Complex.cs within your project.

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 solution, 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 affected 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 bugfixed

License

This article has no explicit license attached to it but may contain usage terms in the article text or the download files themselves. If in doubt please contact the author via the discussion board below.

A list of licenses authors might use can be found here

About the Author

hanzzoid


Member

Location: Germany Germany

Other popular C# articles:

 
Article Top
 
You must Sign In to use this message board.
FAQ FAQ 
 
Noise Tolerance  Layout  Per page   
 Msgs 1 to 14 of 14 (Total in Forum: 14) (Refresh)FirstPrevNext
Generalstruct instead of class PinmemberMember 19066352:17 10 Jun '08  
GeneralRe: struct instead of class PinmemberPIEBALDconsult13:56 2 Apr '09  
General7-Zip will extract PinmemberDom Rositas17:22 20 Jun '07  
GeneralRe: 7-Zip will extract [modified] Pinmemberhanzzoid3:19 3 Jul '07  
GeneralWinZip unknown compression type PinmemberDom Rositas8:49 20 Jun '07  
GeneralCannot extract files from ZIP archive PinmemberTom14:54 4 Jun '07  
GeneralRe: Cannot extract files from ZIP archive PinmemberTom16:39 18 Jun '07  
GeneralExponentiation ALWAYS binds more strongly than multiplication. Pinmembersherifffruitfly12:08 3 Jun '07  
GeneralRe: Exponentiation ALWAYS binds more strongly than multiplication. PinmemberKeith Rule21:16 3 Jun '07  
GeneralRe: Exponentiation ALWAYS binds more strongly than multiplication. Pinsupporterpeterchen23:20 3 Jun '07  
GeneralRe: Exponentiation ALWAYS binds more strongly than multiplication. Pinmemberhanzzoid3:15 3 Jul '07  
GeneralRe: Exponentiation ALWAYS binds more strongly than multiplication. PinmemberPIEBALDconsult13:59 2 Apr '09  
GeneralRe: Exponentiation ALWAYS binds more strongly than multiplication. Pinmembersherifffruitfly14:22 2 Apr '09  
GeneralRe: Exponentiation ALWAYS binds more strongly than multiplication. PinmemberPIEBALDconsult17:28 2 Apr '09  

General General    News News    Question Question    Answer Answer    Joke Joke    Rant Rant    Admin Admin   

Use Ctrl+Left/Right to switch messages, Ctrl+Up/Down to switch threads, Ctrl+PgUp/PgDown to switch pages.

PermaLink | Privacy | Terms of Use
Last Updated: 3 Jul 2007
Editor:
Copyright 2007 by hanzzoid
Everything else Copyright © CodeProject, 1999-2010
Web20 | Advertise on the Code Project