Difference between revisions of "Complex (MathObject Class)"

From WeBWorK_wiki
Jump to navigation Jump to search
(Adjust spacing in tables)
m (Corrected spelling error in "conj" method. Was "$c->cong".)
 
(3 intermediate revisions by one other user not shown)
Line 34: Line 34:
 
=== Methods ===
 
=== Methods ===
   
The Complex class supports the [[MathObject_Methods_and_Properties| common MathObject methods]], and the following additional methods:
+
The Complex class supports the [[Common MathObject Methods]], and the following additional methods:
   
 
{| class="wikitable"
 
{| class="wikitable"
! Option !! Description
+
! Method !! Description
  +
 
|- style="vertical-align: top"
 
|- style="vertical-align: top"
 
| style="padding: 5px; white-space: nowrap" | <code>Re($c)&nbsp;&nbsp;</code> &nbsp; or &nbsp; <code>$c->Re</code>
 
| style="padding: 5px; white-space: nowrap" | <code>Re($c)&nbsp;&nbsp;</code> &nbsp; or &nbsp; <code>$c->Re</code>
 
| style="padding: 5px" | Returns the real part of <code>$c</code> as a Real MathObject.
 
| style="padding: 5px" | Returns the real part of <code>$c</code> as a Real MathObject.
  +
 
|- style="vertical-align: top"
 
|- style="vertical-align: top"
 
| style="padding: 5px; white-space: nowrap" | <code>Im($c)&nbsp;&nbsp;</code> &nbsp; or &nbsp; <code>$c->Im</code>
 
| style="padding: 5px; white-space: nowrap" | <code>Im($c)&nbsp;&nbsp;</code> &nbsp; or &nbsp; <code>$c->Im</code>
 
| style="padding: 5px" | Returns the imaginary part <code>$c</code> as a Real MathObject.
 
| style="padding: 5px" | Returns the imaginary part <code>$c</code> as a Real MathObject.
  +
 
|- style="vertical-align: top"
 
|- style="vertical-align: top"
 
| style="padding: 5px; white-space: nowrap" | <code>arg($c)&nbsp;</code> &nbsp; or &nbsp; <code>$c->arg</code>
 
| style="padding: 5px; white-space: nowrap" | <code>arg($c)&nbsp;</code> &nbsp; or &nbsp; <code>$c->arg</code>
 
| style="padding: 5px" | Returns the argument of <code>$c</code> (i.e., the angle in radians counterclockwise from the <math>x</math>-axis) as a Real MathObject.
 
| style="padding: 5px" | Returns the argument of <code>$c</code> (i.e., the angle in radians counterclockwise from the <math>x</math>-axis) as a Real MathObject.
  +
 
|- style="vertical-align: top"
 
|- style="vertical-align: top"
 
| style="padding: 5px; white-space: nowrap" | <code>mod($c)&nbsp;</code> &nbsp; or &nbsp; <code>$c->mod</code>
 
| style="padding: 5px; white-space: nowrap" | <code>mod($c)&nbsp;</code> &nbsp; or &nbsp; <code>$c->mod</code>
 
| style="padding: 5px" | Returns the modulus of <code>$c</code> (i.e., its distance from the origin) as a Real MathObject. Also available as <code>abs</code> or <code>norm</code>.
 
| style="padding: 5px" | Returns the modulus of <code>$c</code> (i.e., its distance from the origin) as a Real MathObject. Also available as <code>abs</code> or <code>norm</code>.
  +
 
|- style="vertical-align: top"
 
|- style="vertical-align: top"
| style="padding: 5px; white-space: nowrap" | <code>conj($c)</code> &nbsp; or &nbsp; <code>$c->cong</code>
+
| style="padding: 5px; white-space: nowrap" | <code>conj($c)</code> &nbsp; or &nbsp; <code>$c->conj</code>
 
| style="padding: 5px" | Returns the complex conjugate of <code>$c</code> as a Complex MathObject.
 
| style="padding: 5px" | Returns the complex conjugate of <code>$c</code> as a Complex MathObject.
  +
 
|}
 
|}
   
Line 58: Line 63:
 
=== Properties ===
 
=== Properties ===
   
The Complex class supports the [[MathObject_Methods_and_Properties| common MathObject properties]], and the following additional ones:
+
The Complex class supports the [[Common MathObject Properties]], and the following additional ones:
   
 
{| class="wikitable"
 
{| class="wikitable"
Line 67: Line 72:
 
| style="padding: 5px" | When set, this value indicates that the complex number is periodic, with period given by this value.
 
| style="padding: 5px" | When set, this value indicates that the complex number is periodic, with period given by this value.
 
| style="text-align: center" | <code>undef</code>
 
| style="text-align: center" | <code>undef</code>
  +
 
|- style="vertical-align: baseline"
 
|- style="vertical-align: baseline"
 
| style="padding: 5px; white-space: nowrap" | <code>$c->{logPeriodic}</code>
 
| style="padding: 5px; white-space: nowrap" | <code>$c->{logPeriodic}</code>
 
| style="padding: 5px" | When <code>period</code> is defined, and <code>logPeriodic</code> is set to <code>1</code> this indicates that the periodicity is logarithmic (i.e., the period refers to the log of the value, not the value itself).
 
| style="padding: 5px" | When <code>period</code> is defined, and <code>logPeriodic</code> is set to <code>1</code> this indicates that the periodicity is logarithmic (i.e., the period refers to the log of the value, not the value itself).
 
| style="text-align: center" | <code>0</code>
 
| style="text-align: center" | <code>0</code>
  +
 
|}
 
|}
   

Latest revision as of 15:04, 28 June 2017

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