# Complex Numbers

## Enabling complex numbers

Complex numbers have a real part and an imaginary part e.g. `2 + 3j`

, where `j`

is the square root of -1.
Normally, if you attempt to evaluate `Sqrt(-1)`

, you'll get a warning,
and if you ignore warnings the result will be NaN. So first you must enable complex numbers.

To enable complex numbers, first make sure you do not have a node selected and that you are not in the middle of editing a definition, and make sure you are in edit mode. Then on the menus select **Definition → System Variables → EnableComplexNumbers**. The object window for *EnableComplexNumbers* will appear. Change the definition from 0 to 1.

When EnableComplexNumbers is 0, built-in math functions operating on real numbers will warn and return NaN when the result would not be a real number. Built-in math functions will evaluate and return complex results without warnings when the parameter passed to them is complex. So, for example, `Sqrt(4j)`

returns `1.414+1.414j`

even when EnableComplexNumbers is 0.

These functions are affected by this system variable: ArcCos, ArcCosH, ArcSin, Ln, Logit, LogTen, Sqrt, as well as the x^y operator.

## Typing complex literals

A complex number has a *real part* and an *imaginary part* and is written like `3.4 + 2.3j`

, where 3.4 is the real part and 2.3 is the imaginary part. If there is no imaginary part, then it is a real number.

To type the imaginary part, append a lower case `i`

or lower case `j`

to the number. For example, `1i`

or `1j`

is the square root of -1. You must have a digit before the `i`

or `j`

, so you can't type just `i`

-- that would refer to a variable named `i`

. You also cannot put a space between the numbers and the `i`

or `j`

. Numeric suffixes come before the `i`

or `j`

, so you would write `3.4Kj`

, `1.2mj`

or `1.2e-3j`

.

If you have a variable, `x`

, that holds a complex number, and you want to use its value for the imaginary part, it does not work to type `xj`

-- that would refer to a variable named `xj`

. You need to type `x*1j`

.

Complex numbers display in result tables as the real part plus or minus the complex part, and using a small `j`

after the complex part, e.g., `-5 - 3j, 7.2 + 0.1j`

. The real and complex parts each use the prevailing number format.

When complex numbers appear on graphs, the real part is graphed. See Graphing below.

## Precision and range

The real and imaginary parts of a complex number each have approximately 7 decimal digits of precision and can range from -3.4e+38 to 3.4e+38 and as small as 3.4e-38. This is less precision than is used for a real number in Analytica, which has roughly 15 decimal digits of precision and a range from -1.7e+308 to 1.7e+308, and down to 1.7e-308. One complex number takes up the same amount of memory as one real number (the complex number stores two 32-bit floats, whereas the a real number stores one 64-bit float).

## Accessing the parts of a complex number

To extract the real or imaginary part of a complex number, as a real number, use RealPart(x) or ImPart(x). For example

`RealPart(-3+4j) → -3`

`ImPart(-3+4j) → 4`

A complex number can also be written in polar form as [math]\displaystyle{ A e^{\theta j} }[/math], where «A» is the *magnitude* and [math]\displaystyle{ \theta }[/math] is the *phase*. To retrieve the magnitude use Abs(x). The phase can be extracted either in degrees or radians using ComplexDegrees(x) or ComplexRadians(x):.

`Abs(-3+4j) → 5`

`ComplexDegrees(-3+4j) → 126.9`

`ComplexRadians(-3+4j) → 2.214`

To construct a complex number from its magnitude, «A», and phase in radians, «r», use the Exp function in `A*Exp(r*1j)`

. Given a phase in degrees, «d», use `A*Exp(Radians(d)*1j)`

.

`5*Exp(2.214j) → -3 + 4j`

## Complex Conjugate

The complex conjugate of a number is obtained by Conj as

`Conj(-3+4j) → -3-4j`

The conjugate transpose of a matrix is obtained by Transpose using

`Transpose(Conj(A), I, J)`

## Arithmetic operations

The operations of +, -, * and / are standard from any textbook:

- [math]\displaystyle{ (a + b j) + (c + d j) = (a+c) + (b+d) j }[/math]
- [math]\displaystyle{ (a + b j) - (c + d j) = (a-c) + (b-d) j }[/math]
- [math]\displaystyle{ (a + b j) * (c + d j) = (ac - bd) + (bc + ad) j }[/math]
- [math]\displaystyle{ (a + b j) / (c + d j) = {{ac + bd}\over{c^2+d^2}} + {{bc-ad}\over{c^2+d^2}} j }[/math]

The conjugate of a complex number is `RealPart(x) - 1j*ImPart(x)`

.

For exponentiation (`x^y`

), involving two complex numbers, [math]\displaystyle{ x^y = \exp(y*\ln(x)) }[/math].

## Degrees or radians?

Trigonometric functions in Analytica (Sin, Cos, ArcSin, etc) use degrees by convention. However, the functions Exp, Ln, and LogTen, when applied to a complex number, use radians. The convention that [math]\displaystyle{ e^{\pi j}=-1 }[/math] is highly standardized (if a degrees convention were used we would instead have [math]\displaystyle{ e^{180j}=-1 }[/math], which could get confusing fast). Thus, we have this identity for Analytica expressions.

`Exp(1j*x) = Cos(Degrees(x)) + 1j * Sin(Degrees(x))`

## Graphing

When you graph a value, `x`

, which contains complex numbers, RealPart(x) is graphed and labeled as `x`

. To get the imaginary part on the same graph, click the **XY** button at the top right, check **Use another variable** and press **Add...**. Select *Library = Advanced Math* then scroll down and select ImPart. In the parameter box, enter the name of the variable, which is `x`

in this example and press **OK** twice. You might also want to go into **Graph Setup...** and check **Swap horizontal and vertical** and then select *ImPart(x)* as the vertical axis.

## Comparisons and ordering

The comparison operators = and <> can be used to detect whether two complex numbers are equal or not-equal.

Ordering comparisons (<, >, <=, >=) cannot be applied to non-equal complex numbers. If you attempt to do so, a warning is issued and the result (if you ignore warnings) is NaN. Any system that imposes even a partial order on complex numbers runs into logical contradictions. For example, if you were to assert that `0 < 1j`

, then by squaring each side you get `0 < -1`

, a contradiction.

You can apply the sorting functions (SortIndex, Rank and Sort) to arrays of complex numbers. For this purpose and arbitrary total ordering is imposed (RealPart compared first, in the event of a tie, ImPart is used) so that the sort can be carried out. Functions Max and Min return the first value among incomparable items.

## History

New in Analytica 4.5.

## See Also

Accessing the parts of a complex number:

Non-matrix functions that compute or return complex numbers:

Matrix functions that handle complex matrices:

Enable comment auto-refresher