# 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

- Converting from standard to polar form and vice versa
- Finding modulus, argument, and conjugate
- Multiplication, division, raising to a power, and taking the nth root
- Finding real or complex roots of quadratic equations

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:

- num: An array of real and imaginary parts of a complex number given in the form: num = [Re, Im] or num = array(Re, Im).
- argin: Optional - Unit for the argument; default is "rad" for radian. For argument in degree, argin should be set to "deg".
- roundto: Optional - number of decimal places to which values should be rounded off; default is 12 decimal places.

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:

- num: An array of real and imaginary parts of a complex number given in the form: num = [Re, Im] or num = array(Re, Im).
- roundto: Optional - number of decimal places to which values should be rounded off; default is 12 decimal places.

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:

- num: An array of real and imaginary parts of a complex number given in the form: num = [Re, Im] or num = array(Re, Im).
- roundto: Optional - number of decimal places to which values should be rounded off; default is 12 decimal places.

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:

- num: An array of real and imaginary parts of a complex number given in the form: num = [Re, Im] or num = array(Re, Im).
- argin: Optional - Unit for the argument; default is "rad" for radian. For argument in degree, argin should be set to "deg".
- roundto: Optional - number of decimal places to which values should be rounded off; default is 12 decimal places.

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:

- num: An array of modulus and argument of a complex number in polar form: num = [mod, arg] or array(mod, arg).
- argin: Optional - Unit for the argument; default is "rad" for radian. If the argument is in degree, argin should be set to "deg".
- roundto: Optional - number of decimal places to which values should be rounded off; default is 12 decimal places.

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:

- num: An array of real and imaginary parts of a complex number given in square brackets: num = array([Re, Im], [Re2, Im2], ...). A Single complex number can also be input in the form [Re, Im].
- argin: Optional - Unit for the argument; default is "rad" for radian. If the argument is in degree, argin should be set to "deg".
- roundto: Optional - number of decimal places to which values should be rounded off; default is 12 decimal places.

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:

- num: An array of real and imaginary parts of complex numbers given as paired values in square brackets. For example, to add 2-3i, -5i, and sqrt(2)-i, num = array([2,-3], [0,-5], [sqrt(2),-1]).
- roundto: Optional - number of decimal places to which values should be rounded off; default is 12 decimal places.

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:

- num: An array of real and imaginary parts of complex numbers given as paired values in square brackets. For example, to subtract -5i, and sqrt(2)-i from 2-3i, num = array([2,-3], [0,-5], [sqrt(2),-1]).
- roundto: Optional - number of decimal places to which values should be rounded off; default is 12 decimal places.

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:

- num: An array of real and imaginary parts of two complex numbers (dividend, divisor) given as paired values in square brackets. For example, to divide 2-3i by sqrt(2)-i, num = array([2,-3], [sqrt(2),-1]).
- roundto: Optional - number of decimal places to which values should be rounded off; default is 12 decimal places.

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:

- num: An array of real and imaginary parts of complex numbers given as paired values in square brackets. For example, to multiply 2-3i, -5i, and sqrt(2)-i, num = array([2,-3], [0,-5], [sqrt(2),-1]).
- roundto: Optional - number of decimal places to which values should be rounded off; default is 12 decimal places.

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:

- num: An array of real and imaginary parts of a complex number given in the form: num = [Re, Im] or num = array(Re, Im).
- exp: exponent to which the complex number is raised.
- roundto: Optional - number of decimal places to which values should be rounded off; default is 12 decimal places.

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:

- num: An array of real and imaginary parts of a complex number given in the form: num = [Re, Im] or num = array(Re, Im).
- n: The nth roots. For example, for the cubic roots n=3.
- roundto: Optional - number of decimal places to which values should be rounded off; default is 12 decimal places.

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:

- a: The numerical coefficient of x^2
- b: The numerical coefficient of x
- c: The constant
- roundto: Optional - number of decimal places to which values should be rounded off; default is 12 decimal places.

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:

- coeff: The array of numerical coefficients: [a,b,c,d]
- disp: If set to true, the function returns the string version of the roots for display in the form array("root1", "root2", "root3"), which should not be used in calculation.
- roundto: Optional - number of decimal places to which the roots should be rounded off; default is 12 decimal places.

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:

- a: The numerical coefficient of x^2
- b: The numerical coefficient of x
- c: The constant

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:

- num: An array of real and imaginary parts of a complex number given in square brackets: num = array([Re, Im], [Re2, Im2], ...). A Single complex number can also be input in the form [Re, Im].
- roundto: Optional - number of decimal places to which values should be rounded off; default is 3 decimal places.

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:

- num: An array of real and imaginary parts of a complex number given in square brackets: num = array([Re, Im], [Re2, Im2], ...). A Single complex number can also be input in the form [Re, Im].
- argin: Optional - unit for the argument; default is "rad" for degree. For argument in radian, argin = "deg".
- roundto: Optional - number of decimal places to which values should be rounded off; default is 3 decimal places.

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:

- num: An array of real and imaginary parts of a complex number given in square brackets: num = [Re, Im].
- argin: Optional - unit for the argument; default is "deg" for degree. For argument in radian, argin = "rad".
- roundto: Optional - number of decimal places to which values should be rounded off; default is 3 decimal places.
- showlabels: Optional - if true (default), labels for the real and imaginary parts, modulus, argument, and z are shown on the plot. If false, no label is displayed.

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:

- A: Matrix in the form array([row1],[row2],...) where each row can have real entries as a single value or complex entries in the form [Re, Im]; for example, the 2x2 matrix array([1, 2+3i], [-i, 5]) should be entered as A = array([1, [2,3]], [[0,-1], 5])
- rref: Optional - If true, it returns the reduce row echelon form; the default is false, which returns the row echelon form (ref).
- disp: Optional - If set to true, the function returns the formatted matrix (with string entries) for display, which should not be used for calculation.
- roundto: Optional - number of decimal places to which the entries should be rounded off in display format (i.e. when disp = true); default is 4 decimal places.

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