Complex (MathObject Class)

From WeBWorK_wiki
Jump to navigation Jump to search

Complex Class

The Complex class implements complex numbers with "fuzzy" comparison (due to the fact that the real and complex parts are handled using MathObject Reals). Complex numbers can be created most readily in the Complex Context.


Creation

Complex numbers are created via the Complex() function, or by Compute(), or through the use of the pre-defined complex number i. As with Real objects, you can add, subtract, multiply, and perform the other usual arithmetic operations on Complex objects, and can compute sin(), sqrt() and the other standard functions on Complex arguments. In addition, there are arg() and mod(), which return the argument or modulus of a Complex value, and Re() and Im() that return its real or imaginary part. The conj() function returns the complex conjugate of a Complex object. The value i represents [math]\sqrt{-1}[/math] and can be used in your Perl expressions to produce complex numbers. Note that you must use -(i) to obtain [math]-i[/math] in Perl expressions (but not in strings parsed by MathObjects, such as student answers or the arguments to Compute()).

   Context("Complex");
   
   $z = Complex(2,3);
   $z = Complex([2,3]);
   $z = 2 + 3 * i;
   $z = Complex("2 + 3i");
   $z = Compute("2 + 3i");
   
   $w = sin($z);      # same as Compute("sin(2+3i)");
   $w = conj($z);     # same as Complex("2 - 3i");
   $w = $z**2;        # same as Compute("(2+3i)^2");
   $w = $z + 5*i;     # same as Complex("2 + 8i");
   $w = $z - (i);     # parens needed in Perl expressions


Answer Checker

As with all MathObjects, you obtain an answer checker for a Complex object via the cmp() method:

   ANS(Compute("2+3i")->cmp);

The Complex class supports the common answer-checker options. There are no additional options.


Methods

The Complex class supports the Common MathObject Methods, and the following additional methods:

Method Description
Re($c)     or   $c->Re Returns the real part of $c as a Real MathObject.
Im($c)     or   $c->Im Returns the imaginary part $c as a Real MathObject.
arg($c)    or   $c->arg Returns the argument of $c (i.e., the angle in radians counterclockwise from the [math]x[/math]-axis) as a Real MathObject.
mod($c)    or   $c->mod Returns the modulus of $c (i.e., its distance from the origin) as a Real MathObject. Also available as abs or norm.
conj($c)   or   $c->conj Returns the complex conjugate of $c as a Complex MathObject.


Properties

The Complex class supports the Common MathObject Properties, and the following additional ones:

Property Description Default
$c->{period} When set, this value indicates that the complex number is periodic, with period given by this value. undef
$c->{logPeriodic} When period is defined, and logPeriodic is set to 1 this indicates that the periodicity is logarithmic (i.e., the period refers to the log of the value, not the value itself). 0