# zgraph help

This is the documentation for **zgraph**,
an interactive program for domain coloring of complex functions.

This program was created in December 2016 to extend the
**complex polynomial toy**.

## 1. Basic usage

In the text box in the upper left corner of the window, enter an
expression that is a function of the variable $z$ using standard
computer math notation, and then press **Enter** on your
keyboard. The program will then interpret the function, and, if
there are no errors, update the left and right views.

The left view is a domain coloring of the complex plane, and the right view is a domain coloring of the complex plane mapped onto the Riemann sphere (the complex plane closed up with an additional point at infinity, forming a sphere). In this particular implementation, the color represents the argument of the value of the complex function at that particular point, and the gray contours indicate curves where the function has constant magnitude. The contours are logarithmically spaced, meaning the magnitude of the function changes by a constant multiplicative factor between consecutive contours.

**Click and drag**- Rotate the Riemann sphere.
**Left arrow**and**Right arrow**- Adjust the
animation speed of the argument coloring.
**Right arrow**makes the rainbow rotate counterclockwise around zeros and clockwise around poles. **Down arrow**and**Up arrow**- Adjust the
animation speed of the contour lines.
**Up arrow**makes the contours move away from zeros and towards poles.

**Known quirk:** If these commands do not work, click on the
graph to ensure the focus is not on the expression editor or the
Load/Save buttons.

## 2. Loading and Saving

Click the **Load** button in the upper right to see examples of
functions that people have saved. If you find an interesting
function, click the **Save** button to upload the function for
others to see. (The author reserves the right to prune this list of
functions!)

## 3. Families of functions

The complex function may additionally be functions of the real-valued parameters $s$ and $t$.

**Shift + Left arrow**and**Shift + Right arrow**- Adjust the rate that the $t$ variable changes as a function of time.
**Shift + Down arrow**and**Shift + Up arrow**- Adjust the rate that the $s$ variable changes as a function of time.

## 4. Language reference

The program accepts a number of standard complex functions and
operators. There is also some non-standard notation for function
composition. In the following, let `a` and `b` represent
valid expressions. The usual order of operations holds.

`z`- The variable $z$ that expressions are a function of.
`s`and`t`- Real-valued variables $s$ and $t$; see Families of functions.
`a + b`- Addition.
`a - b`- Subtraction.
`-a`- Negation.
`+a`- Unary plus (no effect).
`a b`or`a * b`- Multiplication.
`a / b`- Division.
`a ** b`or`a ^ b`- Exponentiation. The implementation is ${e}^{b\mathrm{ln}a}$, where the logarithm has a branch cut on the negative real ray.
`(a)`- Parenthetical grouping.
`exp(a)`- The analytic function ${e}^{a}$.
`sin(a)`- The analytic function $\mathrm{sin}a$.
`cos(a)`- The analytic function $\mathrm{cos}a$.
`tan(a)`- The function $\frac{\mathrm{sin}a}{\mathrm{cos}a}$.
`ln(a)`- The function $\mathrm{ln}a$, with a branch cut along the negative real ray.
`conj(a)`- The complex conjugate $\overline{a}$.
`re(a)`- The real part of $a$.
`im(a)`- The imaginary part of $a$.
`abs(a)`- The absolute value $|a|$, which for complex numbers we define to be the magnitude of $a$.
`arg(a)`- The argument of $a$, as a number between $-\pi $ and $\pi $.
`i`- The imaginary unit $i=\sqrt{-1}$, which we imagine to be ninety degrees counterclockwise from $1$.
`pi`- The circular circumference-to-diameter ratio $\pi $.
`e`- The natural exponential base $e$.
- a sequence of digits
`0`–`9`with at most one decimal point - Interpreted as the corresponding floating-point number.
`a then b`- If $f(z)=a$ and $g(z)=b$, this expression
represents the composition $g\circ f$. In particular, the value
of
`z`in`b`is the value of`a`. `a iterate n`- If $f(z)=a$, then this represents
${f}^{n}(z)=f(f(\cdots f(z)\cdots ))$. It must be the case that
`n`is a positive integer constant. This has higher precedence than`then`. `a iterateprod n`or`a iteratesum n`- This is
like
`iterate`, but the result is the product or sum of the iterates rather than the last iterate. `c`- The original value of
`z`, even in the face of function composition. Useful for`iterate`.

Formal grammar:

compose = iterate {"then" iterate}. iterate = sum {("iterate" | "iterateprod" | "iteratesum") "num"}. sum = ["+"|"-"] prod {("+"|"-") prod}. prod = exp {["*"|"/"] exp}. exp = term {("^"|"**") ["+"|"-"] term}. term = fun | "(" compose ")" | "num" | "z" | "s" | "t" | "e" | "i" | "pi" | "const". fun = elem "(" compose {"," compose} ")". elem = "exp" | "sin" | "cos" | "tan" | "ln" | "conj" | "re" | "im" | "abs" | "arg".

## 5. Implementation details

Expressions are compiled to the OpenGL Shader Language (GLSL), and this program is executed by GPU. The process begins by converting the complex functions to a list of real-valued functions of two real variables, and these are somewhat optimized using global value numbering. The program logs the resulting shader program to the console in your browser’s developer tools, for the curious.

A consequence of this is that the accuracy of the graph is up to the
graphics driver and its particular implementation of
`mediump` floating-point numbers for your particular graphics
card. For instance, floating-point numbers with too great of a
magnitude might yield any number of colors, but in practice it seems
most drivers default to black.

It would be nice to have the circle from the complex polynomials toy, however this would involve needing to sample the points on the circle properly. There, I cheated by adding enough points that most people wouldn’t notice this limitation.