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, switch to the Complex context with Context('Complex').

Several ways to create a complex number that are demonstrated. Notice for the 4th example that i is defined as a MathObject complex number that can be used directly in Perl computations.

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_1 z_2 =`] [___]{$z1*$z2}

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

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

Statement

For the last three answer rules, the correct answer is directly computed from previously defined variables in the answer rule option braces {...} instead of being stored in another variable, as in the first two answer rules. Either method is correct. Usually you would only need store the answer in a variable if it will be used in other places in the code as well which is not done even for the first two answers rules in this case.

Note that the ** in the last answer is the Perl exponent operator.

BEGIN_PGML_SOLUTION
Solution explanation goes here.
END_PGML_SOLUTION

ENDDOCUMENT();

Solution

A solution should be provided here.