← ppnm

Exercise "complex"

Tasks

  1. Read the source code complex.cs and cmath.cs and find out how the complex numbers and complex function are defined.

  2. Get the two files to your box, for example,

    1. sudo apt install wget
    2. mkdir -p ~/ppnm/matlib/complex   # or whatever name you deem indicative
      
    3. cd ~/ppnm/matlib/complex
    4. wget http://172.28.64.24:8080/prog/matlib/complex/complex.cs
      wget http://172.28.64.24:8080/prog/matlib/complex/cmath.cs
      
    Alternatively, right-click on the files above and choose "Save link as..." (or simething like that).

  3. Add the following lines to your Makefile to build the cmath.dll library,
    DIR = $(HOME)/ppnm/matlib/complex          # or whatever your directory is
    cmath.dll : $(DIR)/cmath.cs $(DIR)/complex.cs
    	mcs -target:library -out:./cmath.dll $^
    
    Note "./" (the current directory) in front of "cmath.dll".

    You then link your "cmath.dll" library when compiling your main.cs like this,

    main.exe : main.cs cmath.dll
    	mcs -reference:cmath.dll main.cs
    
  4. 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?)
    
  5. 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.