use strict;
use warnings;
use Math::Complex;

# Wir erzeugen erst einige komplexe Zahlen:
my $a = Math::Complex->new(3,5);              # 3 + 5i
my $b = Math::Complex->new(2,-2);             # 2 + -2i
my $c = Math::Complex->new();

my $z1 = Math::Complex->make(3, 4);           # Kartesisch -> 3+4i
my $z2 = -5.3 + 4.6*i;                        # Symbol i ist ueberladen
my $z3 = Math::Complex->emake(2, pi/2);       # Polar -> 2i

# Die Zahlen werden richtig angezeigt:
print '$a         --> ', $a, "\n";
print '$b         --> ', $b, "\n";
print '$c         --> ', $c, "\n";
print '$z1        --> ', $z1, "\n";
print '$z2        --> ', $z2, "\n";
print '$z3        --> ', $z3, "\n";

# Einige einfache arithmetische und mathematische Operationen:
$c = cplx(3,5) * cplx(2,-2);             # einfacher fürs Auge

print '$a + $b    --> ', $a + $b,   "\n";  # Addition
print '$a - $b    --> ', $a - $b,   "\n";  # Subtraktion
print '$a * $b    --> ', $a * $b,   "\n";  # Multiplikation
print '$c         --> ', $c,        "\n";  # Multiplikationserg.
print '$z1 / $z2  --> ', $z1/$z2,   "\n";  # Komplexe Division
print 'Re($z3)    --> ', $z3->Re(), "\n";  # Realteil
print 'Im($z3)    --> ', $z3->Im(), "\n";  # Imaginaerteil

print 'sin($z3)   --> ', sin($z3),  "\n";  # Sinus
print 'acos($z2)  --> ', acos($z2), "\n";  # acos(), komplex

print 'i * i      --> ', i * i,     "\n";  # -1 -- 1, pi

# mathematische Funktionen
print "sqrt(3+2*i)--> ", sqrt(3+2*i), "\n";
print "abs(2+i)   --> ", abs(2+i),    "\n";

# Quadratische Gleichung
my ($x1,$x2) = solveQuad(1,2,3);

print "\nLoesung fuer: x*x + 2x + 3 = 0:\n"; 
print "x1 = $x1, x2 = $x2\n";
 
sub solveQuad
  {
	my ($a,$b,$c) = @_;
	my $root = sqrt($b*$b - 4*$a*$c);
	return (-$b + $root)/(2*$a), (-$b - $root)/(2*$a);
  }
