Basic Operations of Complex numbers

This demonstrates basic operations with complex numbers.

Complete Code

Download file: ComplexOperations.pg

PG problem file

Explanation

DOCUMENT();

loadMacros('PGstandard.pl', 'PGML.pl', 'PGcourse.pl');

Preamble

These standard macros need to be loaded.
Context('Complex');

$z0 = Complex(non_zero_random(-5, 4), non_zero_random(-5, 5));
$z1 = Complex([ -1, 4 ]);
$z2 = Complex("2-4i");
$z3 = 3 - 4 * i;

$ans1 = $z0 + $z1;
$a0   = non_zero_random(-4, 4);
$a1   = random(1, 5);
$ans2 = Compute("$a0*$z1-$a1*$z2");

Setup

To use complex numbers, we need to switch context with Context('Complex'). There are many ways to create a complex number. Notice on the 4th one i is defined and can be used naturally.

Also, the standard operations go through as expected. Notice that for the first two questions, we give the store the answer in a variable.

BEGIN_PGML
Let [`z_0=[$z0]`], [`z_1=[$z1]`], [`z_2=[$z2]`] and [`z_3=[$z3]`]. Find

[`z_0+z_1=`] [___]{$ans1}

[`[$a0]z_1-[$a1]z_2=`] [_____]{$ans2}

[`z_1z_2=`] [___]{$z1*$z2}

[``\frac{z_3}{z_0}= ``] [___]{$z3/$z0}

[`` z_2^2=``] [__]{$z2**2}
END_PGML

Statement

Note that in the last three answer blanks, the correct answer is in the {} instead of stored as a variable, like the first two. Either method is correct and it varies on which to use. Recall that the perl power ** is used in the last one.

BEGIN_PGML_SOLUTION
Solution explanation goes here.
END_PGML_SOLUTION

ENDDOCUMENT();

Solution

A solution should be provided here.