Complex (MathObject Class)

From WeBWorK_wiki
Revision as of 16:34, 2 August 2012 by Dpvc (talk | contribs)
Jump to navigation Jump to search

The 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). 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