Click here to Skip to main content
Licence CPOL
First Posted 3 Jun 2007
Views 21,521
Downloads 247
Bookmarked 13 times

A C# class for complex numbers

By | 3 Jul 2007 | Article
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
  • 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



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

 
You must Sign In to use this message board. (secure sign-in)
 
Search this forum  
 FAQ
    Noise  Layout  Per page   
  Refresh
Questioncode licence Pinmemberenrico.deg22:53 10 Jun '10  
Generalstruct instead of class PinmemberMember 19066351:17 10 Jun '08  
GeneralRe: struct instead of class PinmemberPIEBALDconsult12:56 2 Apr '09  
General7-Zip will extract PinmemberDom Rositas16:22 20 Jun '07  
GeneralRe: 7-Zip will extract [modified] Pinmemberhanzzoid2:19 3 Jul '07  
GeneralWinZip unknown compression type PinmemberDom Rositas7:49 20 Jun '07  
GeneralCannot extract files from ZIP archive PinmemberTom13:54 4 Jun '07  
GeneralRe: Cannot extract files from ZIP archive PinmemberTom15:39 18 Jun '07  
GeneralExponentiation ALWAYS binds more strongly than multiplication. Pinmembersherifffruitfly11:08 3 Jun '07  
GeneralRe: Exponentiation ALWAYS binds more strongly than multiplication. PinmemberKeith Rule20:16 3 Jun '07  
GeneralRe: Exponentiation ALWAYS binds more strongly than multiplication. Pinmemberpeterchen22:20 3 Jun '07  
GeneralRe: Exponentiation ALWAYS binds more strongly than multiplication. Pinmemberhanzzoid2:15 3 Jul '07  
GeneralRe: Exponentiation ALWAYS binds more strongly than multiplication. PinmemberPIEBALDconsult12:59 2 Apr '09  
GeneralRe: Exponentiation ALWAYS binds more strongly than multiplication. Pinmembersherifffruitfly13:22 2 Apr '09  
GeneralRe: Exponentiation ALWAYS binds more strongly than multiplication. PinmemberPIEBALDconsult16:28 2 Apr '09  

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

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

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