← ppnm

Exercise "complex"

Introduction

In the C# programming language the complex numbers and the corresponding complex functions are defined in the stuct "System.Numerics.Complex" [manual]. The source code can be examined [at github]. In mono you have to link this library explicitly with the option "-reference:System.Numerics.dll".

Alternatively, you can use a simpler, home-made, implementation of complex numbers, complex.cs, and the corresponding complex functions, cmath.cs. You can build the library, say "cmath.dll", like this:

cmath.dll : cmath.cs complex.cs
	mcs -target:library -out:./cmath.dll $^
You can then link your "cmath.dll" library when compiling your main.cs like this,
main.exe : main.cs cmath.dll
	mcs -reference:cmath.dll -target:exe -out:main.exe main.cs

Tasks

  1. Calculate -1, i, ei, e, ii, ln(i), sin() and compare (using our "approx" function) with manually calculated results (check them please before using):

    -1 = ±i,
    ln(i) = ln(eiπ/2) = iπ/2,
    i = e½ln(i) = eiπ/4 = cos(π/4)+i sin(π/4) = 1/√2+i/√2 (and where is the second branch you think?)
    ii = ei ln(i) = e-π/2 ≈ 0.208, (ii is actually real – magic, isn't it?)
    
  2. Extra: add to our cmath-class the hyperbolic functions sinh, cosh of complex argument—and calculate sinh(i), cosh(i). Check that the results are correct.