Next: Complex Arithmetic, Up: Arithmetic

### 17.1 Utility Functions

The following functions are available for working with complex numbers. Each expects a single argument. They are called mapping functions because when given a matrix argument, they apply the given function to each element of the matrix.

— Mapping Function: ceil (x)

> Return the smallest integer not less than x. If x is complex, return `ceil (real (`x`)) + ceil (imag (`x`)) * I`.

— Function File: cplxpair (z, tol, dim)

> Sort the numbers z into complex conjugate pairs ordered by increasing real part. With identical real parts, order by increasing imaginary magnitude. Place the negative imaginary complex number first within each pair. Place all the real numbers after all the complex pairs (those with `abs (imag (`z`) /` z`) <` tol`)`, where the default value of tol is `100 *` eps.

> By default the complex pairs are sorted along the first non-singleton dimension of z. If dim is specified, then the complex pairs are sorted along this dimension.

> Signal an error if some complex numbers could not be paired. Requires all complex numbers to be exact conjugates within tol, or signals an error. Note that there are no guarantees on the order of the returned pairs with identical real parts but differing imaginary parts.

>               cplxpair (exp(2i*pi*[0:4]'/5)) == exp(2i*pi*[3; 2; 4; 1; 0]/5)

— Function File: d = del2 (m)
— Function File: d = del2 (m, h)
— Function File: d = del2 (m, dx, dy, ...)

> Calculates the discrete Laplace operator. If m is a matrix this is defined as

>                     1    / d^2            d^2         \
>               D  = --- * | ---  M(x,y) +  ---  M(x,y) |
>                     4    \ dx^2           dy^2        /

> The above to continued to N-dimensional arrays calculating the second derivative over the higher dimensions.

> The spacing between evaluation points may be defined by h, which is a scalar defining the spacing in all dimensions. Or alternatively, the spacing in each dimension may be defined separately by dx, dy, etc. Scalar spacing values give equidistant spacing, whereas vector spacing values can be used to specify variable spacing. The length of the vectors must match the respective dimension of m. The default spacing value is 1.

> You need at least 3 data points for each dimension. Boundary points are calculated as the linear extrapolation of the interior points.

> See also: gradient, diff.

— Mapping Function: exp (x)

> Compute the exponential of x. To compute the matrix exponential, see Linear Algebra.

— Mapping Function: expm1 (x)

> Compute exp (x) - 1 accurately in neighbourhood of zero.

— Function File: p = factor (q)
— Function File: [p, n] = factor (q)

> Return prime factorization of q. That is `prod (`p`) ==` q. If q`== 1`, returns 1.

> With two output arguments, returns the unique primes p and their multiplicities. That is `prod (`p`.^` n`) ==` q.

— Function File: factorial (n)

> Return the factorial of n. If n is scalar, this is equivalent to `prod (1:`n`)`. If n is an array, the factorial of the elements of the array are returned.

— Mapping Function: fix (x)

> Truncate x toward zero. If x is complex, return `fix (real (`x`)) + fix (imag (`x`)) * I`.

— Mapping Function: floor (x)

> Return the largest integer not greater than x. If x is complex, return `floor (real (`x`)) + floor (imag (`x`)) * I`.

— Mapping Function: fmod (x, y)

> Compute the floating point remainder of dividing x by y using the C library function `fmod`. The result has the same sign as x. If y is zero, the result is implementation-defined.

— Loadable Function: g = gcd (a1, ...)
— Loadable Function: [g, v1, ...] = gcd (a1, ...)

> If a single argument is given then compute the greatest common divisor of the elements of this argument. Otherwise if more than one argument is given all arguments must be the same size or scalar. In this case the greatest common divisor is calculated for element individually. All elements must be integers. For example,

>               gcd ([15, 20])
>                     5

> and

>               gcd ([15, 9], [20 18])
>                     5  9

> Optional return arguments v1, etc, contain integer vectors such that,

>               g = v1 .* a1 + v2 .* a2 + ...

> For backward compatibility with previous versions of this function, when all arguments are scalar, a single return argument v1 containing all of the values of v1, ... is acceptable.

> See also: lcm, min, max, ceil, floor.

— Function File: x = gradient (m)
— Function File: [x, y, ...] = gradient (m)
— Function File: [...] = gradient (m, s)
— Function File: [...] = gradient (m, dx, dy, ...)
— Function File: [...] = gradient (f, x0)
— Function File: [...] = gradient (f, x0, s)
— Function File: [...] = gradient (f, x0, dx, dy, ...)

> Calculate the gradient of sampled data, or of a function. If m is a vector, calculate the one dimensional gradient of m. If m is a matrix the gradient is calculated for each row.

> `[`x`,` y`] = gradient (`m`)` calculates the one dimensional gradient for each direction if m if m is a matrix. Additional return arguments can be use for multi-dimensional matrices.

> Spacing values between two points can be provided by the dx, dy or h parameters. If h is supplied it is assumed to be the spacing in all directions. Otherwise, separate values of the spacing can be supplied by the dx, etc variables. A scalar value specifies an equidistant spacing, while a vector value can be used to specify a variable spacing. The length must match their respective dimension of m.

> At boundary points a linear extrapolation is applied. Interior points are calculated with the first approximation of the numerical gradient

>               y'(i) = 1/(x(i+1)-x(i-1)) *(y(i-1)-y(i+1)).

> If the first argument f is a function handle, the gradient of the function at the points in x0 is approximated using central difference. For example, `gradient (@cos, 0)` approximates the gradient of the cosine function in the point x0 = 0. As with sampled data, the spacing values between the points from which the gradient is estimated can be set via the s or dx, dy, ... arguments. By default a spacing of 1 is used.

— Mapping Function: hypot (x, y)

> Compute square-root of the squares of x and y element-by-element. This equivalent to `sqrt (`x`.^ 2 +` y`.^ 2)`, but calculated in a manner that avoids overflows for large values of x or y.

— Mapping Function: lcm (x, ...)

> Compute the least common multiple of the elements of x, or the list of all the arguments. For example,

>               lcm (a1, ..., ak)

> is the same as

>               lcm ([a1, ..., ak]).

> All elements must be the same size or scalar.

> See also: gcd, min, max, ceil, floor.

— Mapping Function: log (x)

> Compute the natural logarithm for each element of x. To compute the matrix logarithm, see Linear Algebra.

> See also: log2, log10, logspace, exp.

— Mapping Function: log1p (x)

> Compute log (1 + x) accurately in neighbourhood of zero.

— Mapping Function: log10 (x)

> Compute the base-10 logarithm for each element of x.

> See also: log, log2, logspace, exp.

— Mapping Function: log2 (x)
— Mapping Function: [f, e] = log2 (x)

> Compute the base-2 logarithm for each element of x. If called with two output arguments, split x to binary mantissa and exponent so that `1/2 <= abs(f) < 1` and e is an integer. If `x = 0`, `f = e = 0`.

> See also: log, log10, log2, exp.

— Loadable Function: max (x, y, dim)
— Loadable Function: [w, iw] = max (x)

> For a vector argument, return the maximum value. For a matrix argument, return the maximum value from each column, as a row vector, or over the dimension dim if defined. For two matrices (or a matrix and scalar), return the pair-wise maximum. Thus,

>               max (max (x))

> returns the largest element of x, and

>               max (2:5, pi)
>                     3.1416  3.1416  4.0000  5.0000

> compares each element of the range `2:5` with `pi`, and returns a row vector of the maximum values.

> For complex arguments, the magnitude of the elements are used for comparison.

> If called with one input and two output arguments, `max` also returns the first index of the maximum value(s). Thus,

>               [x, ix] = max ([1, 3, 5, 2, 5])
>                     x = 5
>                       ix = 3

— Loadable Function: min (x, y, dim)
— Loadable Function: [w, iw] = min (x)

> For a vector argument, return the minimum value. For a matrix argument, return the minimum value from each column, as a row vector, or over the dimension dim if defined. For two matrices (or a matrix and scalar), return the pair-wise minimum. Thus,

>               min (min (x))

> returns the smallest element of x, and

>               min (2:5, pi)
>                     2.0000  3.0000  3.1416  3.1416

> compares each element of the range `2:5` with `pi`, and returns a row vector of the minimum values.

> For complex arguments, the magnitude of the elements are used for comparison.

> If called with one input and two output arguments, `min` also returns the first index of the minimum value(s). Thus,

>               [x, ix] = min ([1, 3, 0, 2, 5])
>                     x = 0
>                       ix = 3

— Loadable Function: cummax (x, dim)
— Loadable Function: [w, iw] = cummax (x)

> Return the cumulative maximum values. That means, the call

>                 [w, iw] = cummax (x, dim)

> is equivalent to the following code:

>                 for i = 1:size (x, dim)
>                   [w(:,...,i,:,...), iw(:,...,i,:,...)] = max(x(:,...,1:i,:,...), dim);
>                 endfor

> but computed in a much faster manner. The behaviour if dim or iw is unspecified is analogous to `max`.

— Loadable Function: cummin (x, dim)
— Loadable Function: [w, iw] = cummin (x)

> Return the cumulative minimum values. That means, the call

>                 [w, iw] = cummin (x, dim)

> is equivalent to the following code:

>                 for i = 1:size (x, dim)
>                   [w(:,...,i,:,...), iw(:,...,i,:,...)] = min(x(:,...,1:i,:,...), dim);
>                 endfor

> but computed in a much faster manner. The behaviour if dim or iw is unspecified is analogous to `min`.

— Mapping Function: mod (x, y)

> Compute modulo function. Conceptually this is given by

>               x - y .* floor (x ./ y)

> and is written in a manner that the correct modulus is returned for integer types. This function handles negative values correctly. That is `mod (-1, 3)` is 2, not -1 as `rem (-1, 3)` returns. Also, `mod (`x`, 0)` returns x.

> An error message is printed if the dimensions of the arguments do not agree, or if either of the arguments is complex.

> See also: rem, round.

— Function File: nextpow2 (x)

> If x is a scalar, returns the first integer n such that 2^n >= abs (x).

> If x is a vector, return `nextpow2 (length (`x`))`.

> See also: pow2.

— Function File: nthroot (x, n)

> Compute the n-th root of x, returning real results for real components of x. For example

>               nthroot (-1, 3)
>                -1
>               (-1) ^ (1 / 3)
>                0.50000 - 0.86603i

— Mapping Function: pow2 (x)
— Mapping Function: pow2 (f, e)

> With one argument, computes 2 .^ x for each element of x. With two arguments, returns f .* (2 .^ e).

> See also: nextpow2.

— Function File: primes (n)

> Return all primes up to n.

> Note that if you need a specific number of primes, you can use the fact the distance from one prime to the next is on average proportional to the logarithm of the prime. Integrating, you find that there are about k primes less than k \log (5 k).

> The algorithm used is called the Sieve of Erastothenes.

— Function File: reallog (x)

> Return the real natural logarithm of x. If any element results in the return value being complex `reallog` produces an error.

> See also: log, realsqrt, realpow.

— Function File: realpow (x, y)

> Return the element by element power operator. This is equivalent to x`.^` y, except that if the return value is complex `realpow` produces an error.

> See also: reallog, realsqrt.

— Function File: realsqrt (x)

> Return the real sqrt of x. If any element results in the return value being complex `realsqrt` produces an error.

> See also: sqrt, reallog, realpow.

— Mapping Function: rem (x, y)

> Return the remainder of x`/` y, computed using the expression

>               x - y .* fix (x ./ y)

> An error message is printed if the dimensions of the arguments do not agree, or if either of the arguments is complex.

> See also: mod, round.

— Mapping Function: round (x)

> Return the integer nearest to x. If x is complex, return `round (real (`x`)) + round (imag (`x`)) * I`.

> See also: rem.

— Mapping Function: roundb (x)

> Return the integer nearest to x. If there are two nearest integers, return the even one (banker's rounding). If x is complex, return `roundb (real (`x`)) + roundb (imag (`x`)) * I`.

> See also: rem.

— Mapping Function: sign (x)

> Compute the signum function, which is defined as

>                          -1, x < 0;
>               sign (x) =  0, x = 0;
>                           1, x > 0.

> For complex arguments, `sign` returns `x ./ abs (`x`)`.

— Mapping Function: sqrt (x)

> Compute the square root of x. If x is negative, a complex result is returned. To compute the matrix square root, see Linear Algebra.