Macro Library complex

Library name: complex

Functions to handle operations for complex numbers.  Version 1.  October 2020

Author:  Amir Tavangar



To import the library, include loadlibrary("complex") in the question's common control.

The functions in this library can be used to handle operations for complex numbers such as

List of functions:

  • cx_arg: Argument (theta)
  • cx_modul: Modulus
  • cx_conj: Conjugate
  • cx_std2pol: Standard to polar form
  • cx_pol2std: Polar to standard form
  • cx_polEu: Polar-Euler notation
  • cx_add: Addition
  • cx_sub: Subtraction
  • cx_div: Division
  • cx_mul: Multiplication
  • cx_pow: Power
  • cx_root: nth Roots
  • cx_quadRoot: Quadratic roots
  • cx_cubicRoot: Cubic roots
  • cx_prettyquadRoot: Formatted quadratic roots
  • cx_format2std: Display in standard form
  • cx_format2pol: Display in polar form
  • cx_plot: Plot z
  • cx_matrixreduce: RREF of complex matrices
  •  

     Function output and question types:

    The output of most of the functions in this library is an array. Other than formatting functions, the element(s) of arrays are paired values in square brackets - for single-number output: [Re, Im], and for multi-number output: array([Re1, Im1], [Re2, Im2], ...). The values can be accessed by indexing the array. Arrays are zero-indexed so, for example, the first element in array $A = array([2, 3], [-4, 0]) can be accessed by $A[0] = [2, 3]. N-tuple question-type accepts paired values as input for answers: $answer = $A[0]. Alternatively, Number question-type can be used if the values in the bracket are the answer(s). For example, the value in the array's first element can be accessed by $A[0][0] = 2, and the second value by $A[0][1] = 3. So if the answer is the first value in the first bracket, then $answer = $A[0][0].

    Formatting functions format2pol and format2std can be used for displaying answers ($showanswer) as well as the answer itself. If they are to be used as the answer, calculated complex question-type accepts both formats with $answerformat = "sloppycomplex" for the polar form.

     

     Functions:

    cx_arg(num, [argin = "rad", roundto = 12])

    Function: cx_arg returns the argument (angle theta) of a complex number.

     Parameters:

     Returns:

    The agument of a complex number in radian (or degree) as a float.

     

    cx_modul(num, [roundto = 12])

    Function cx_modul returns the modulus (absolute value) of a complex number, which is sqrt(Re^2 + Im^2).

    Parameters:

    Returns:

    The modulus of a complex number as a float.

     

    cx_conj(num, [roundto = 12])

    Function cx_conj returns the conjugate of a complex number; the conjugate of (a+bi) is (a-bi).

    Parameters:

     Returns:

    The conjugate of a complex number in an array (same format as the input).

     

    cx_std2pol(num, [argin = "rad", roundto = 12])

    Function cx_std2pol converts the standard form to polar form and returns the modulus and the argument as a paired value.

    Parameters:

     Returns:

    The modulus and the argument of the complex number as a paired value in an array: [mod, arg].

     

    cx_pol2std(num, [argin = "rad", roundto = 12])

    Function cx_pol2std converts the polar form to standard form and returns the real and imaginary parts as a paired value.

    Parameters:

     Returns:

    The real and imaginary parts as a paired value in an array: [Re, Im].

     

    cx_polEu(num, [argin = "rad", roundto = 12])

    Function cx_polEu returns the polar form of a complex number with the Euler's formula notation: z = r e^i*theta as a string for displaying answer.

    Parameters:

     Returns:

    The polar form with the Euler's formula notation as a string.If num has more than one complex number, the function returns an array of strings: array("r1*e^i*theta1", "r2*e^i*theta2", ...).

     

    cx_add(num, [roundto = 12])

    Function cx_add returns the sum of input complex numbers in standard form as a paired value in an array.

     Parameters:

     Returns:

    The real and imaginary parts of the sum as a paired value in an array: [Re, Im].

     

    cx_sub(num, [roundto = 12])

    Function cx_sub returns the difference of input complex numbers in standard form as a paired value in an array.

     Parameters:

     Returns:

    The real and imaginary parts of the difference as a paired value in an array: [Re, Im].

     

    cx_div(num, [roundto = 12])

    Function cx_div returns the quotient of two complex numbers in standard form as a paired values in an array.

     Parameters:

     Returns:

    The quotient of two complex numbers in standard form as a paired values in an array: [Re, Im].

     

    cx_mul(num, [roundto = 12])

    Function cx_mul returns the product of input complex numbers in standard form as a paired value in an array.

     Parameters:

     Returns:

    The real and imaginary parts of the product as a paired value in an array: [Re, Im].

     

    cx_pow(num, exp, [roundto = 12])

    Function cx_pow returns the power of a complex number in standard form as a paired value in an array.

    Parameters:

     Returns:

    The real and imaginary parts of the power as a paired value in an array: [Re, Im].

     

    cx_root(num, n, [roundto = 12])

    Function cx_root returns an array of a paired value of real and imaginary parts of the nth roots.

    Parameters:

     Returns:

    An array of a paired value of real and imaginary parts of the nth roots: array([Re1, Im1], [Re2, Im2], ...)

     

    cx_quadRoot(a, b, c, [roundto = 12])

    Function: cx_quadRoot returns an array of roots of the quadratic equation f(x) = ax^2 + bx + c. Real roots are returned as an array A = array([r1],[r2]) ordered from the smallest to largest, and the complex roots are returned as an array A = array([Re1,Im1], [Re2,Im2]). Question type Complex handles both types of solutions.

     Parameters:

     Returns:

    An array of the two roots (either real or complex) of the quadratic equation.

     

    cx_cubicRoot(coeff, [disp = False, roundto = 12])

    Function: cx_cubicRoot Returns an array of roots of the cubic equation f(x) = ax^3 + bx^2 + cx + d. Real roots are returned as an array([r1],[r2],[r3]) and complex roots are return as an array([r1],[Re1,Im1], [Re1,Im1]).

     Parameters:

     Returns:

    An array of the three roots (either real or complex) of the cubic equation.

     

    cx_prettyquadRoot(a, b, c)

    Function: cx_prettyquadRoot returns an array of the formatted roots of the quadratic equation f(x) = ax^2 + bx + c as string. Real roots are returned as an array A = array("r1", "r2") ordered from the smallest to largest, and the complex roots are returned as an array A = array("a+bi", "a-bi") This function is suitable for displaying answer (i.e., $showanswer).

     Parameters:

     Returns:

    An array of formatted string of roots (either real or complex) of the quadratic equation.

     

    cx_format2std(num, [roundto = 3])

    Function cx_format2std returns a string form of a complex number in standard form: a + b i.

    Parameters:

     Returns:

    The complex number in standard form a + bi as a string. If num has more than one complex number, the function returns an array of the standard form of the complex numbers as strings and output = array("a1+b1 i", "a2+b2 i", ...).

     

    cx_format2pol(num, [argin="rad" , roundto = 3])

    Function cx_format2pol returns a string form of a complex number in polar form: r (cos(t) + i sin(t)).

    Parameters:

     Returns:

    The complex number in polar form as a string. If num has more than one complex number, the function returns an array of the polar forms as strings and output = array("r1 (cos(t1) + i sin(t1))", "r2 (cos(t2) + i sin(t2))", ...).

     

    cx_plot(num, [argin = "deg" , roundto = 3, showlabels = true])

    Function cx_plot returns the plot of a complex number.

    Parameters:

     Returns:

    The plot of a complex number.

     

    cx_matrixreduce(A, [rref = False, disp = False, roundto = 4])

    Function cx_matrixreduce returns the row echelon (ref) or reduced row echelon form (rref) of the matrix A; it handles matrices with both real and complex entries.

    Parameters:

     Returns:

    Returns the row echelon (ref) or reduced row echelon form (rref) of the matrix A in the same format as A. The display format returns the formatted matrix. See question 674661 on MOM for an example.

     

    Back to top of page