Go to the first, previous, next, last section, table of contents.


Arithmetic

Unless otherwise noted, all of the functions described in this chapter will work for real and complex scalar or matrix arguments.

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.

@anchor{doc-ceil}

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

@anchor{doc-exp}

Mapping Function: exp (x)
Compute the exponential of x. To compute the matrix exponential, see section Linear Algebra.

@anchor{doc-fix}

Mapping Function: fix (x)
Truncate x toward zero. If x is complex, return fix (real (x)) + fix (imag (x)) * I.

@anchor{doc-floor}

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

@anchor{doc-gcd}

Mapping Function: gcd (x, ...)
Compute the greatest common divisor of the elements of x, or the list of all the arguments. For example,

gcd (a1, ..., ak)

is the same as

gcd ([a1, ..., ak])

An optional second return value, v contains an integer vector such that

g = v(1) * a(k) + ... + v(k) * a(k)

@seealso{lcm, min, max, ceil, and floor}

@anchor{doc-lcm}

Mapping Function: lcm (x, ...)
Compute the least common multiple of the elements elements of x, or the list of all the arguments. For example,

lcm (a1, ..., ak)

is the same as

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

@seealso{gcd, min, max, ceil, and floor}

@anchor{doc-log}

Mapping Function: log (x)
Compute the natural logarithm for each element of x. To compute the matrix logarithm, see section Linear Algebra.
@seealso{log2, log10, logspace, and exp}

@anchor{doc-log10}

Mapping Function: log10 (x)
Compute the base-10 logarithm for each element of x.
@seealso{log, log2, logspace, and exp}

@anchor{doc-log2}

Mapping Function: log2 (x)
Mapping Function: [f, e] log2 (x)
Compute the base-2 logarithm of x. With two outputs, returns f and e such that 1/2 <= abs(f) < 1 and x = f * 2^e.
@seealso{log, log10, logspace, and exp}

@anchor{doc-max} For a vector argument, return the maximum value. For a matrix argument, return the maximum value from each column, as a row vector. Thus,

max (max (x))

returns the largest element of x.

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

If called with two output arguments, also returns the index of the maximum value(s). Thus,

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

returns x = 5 and ix = 3.

@anchor{doc-min} For a vector argument, return the minimum value. For a matrix argument, return the minimum value from each column, as a row vector. Thus,

min (min (x))

returns the smallest element of x.

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

If called with two output arguments, also returns the index of the minimum value(s). Thus,

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

returns x = 0 and ix = 3.

@anchor{doc-mod}

Mapping Function: mod (x, y)
Compute modulo function, using

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

Note that this handles negative numbers correctly: 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.

@seealso{rem, round}

@anchor{doc-nextpow2}

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)).

@seealso{pow2}

@anchor{doc-pow2}

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).
@seealso{nextpow2}

@anchor{doc-rem}

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.

@seealso{mod, round}

@anchor{doc-round}

Mapping Function: round (x)
Return the integer nearest to x. If x is complex, return round (real (x)) + round (imag (x)) * I.
@seealso{rem}

@anchor{doc-sign}

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).

@anchor{doc-sqrt}

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 section Linear Algebra.

Complex Arithmetic

The following functions are available for working with complex numbers. Each expects a single argument. Given a matrix they work on an element by element basis. In the descriptions of the following functions, z is the complex number x + iy, where i is defined as sqrt (-1).

@anchor{doc-abs}

Mapping Function: abs (z)
Compute the magnitude of z, defined as |z| = sqrt (x^2 + y^2).

For example,

abs (3 + 4i)
     => 5

@anchor{doc-arg}

Mapping Function: arg (z)
Mapping Function: angle (z)
Compute the argument of z, defined as theta = atan (y/x).

in radians.

For example,

arg (3 + 4i)
     => 0.92730

@anchor{doc-conj}

Mapping Function: conj (z)
Return the complex conjugate of z, defined as conj (z) = x - iy.
@seealso{real and imag}

@anchor{doc-imag}

Mapping Function: imag (z)
Return the imaginary part of z as a real number.

@seealso{real and conj}

@anchor{doc-real}

Mapping Function: real (z)
Return the real part of z.
@seealso{imag and conj}

Trigonometry

Octave provides the following trigonometric functions. Angles are specified in radians. To convert from degrees to radians multipy by pi/180 (e.g. sin (30 * pi/180) returns the sine of 30 degrees).

@anchor{doc-sin}

Mapping Function: sin (x)
Compute the sin of each element of x.

@anchor{doc-cos}

Mapping Function: cos (x)
Compute the cosine of each element of x.

@anchor{doc-tan}

Mapping Function: tan (z)
Compute tanget of each element of x.

@anchor{doc-sec}

Mapping Function: sec (x)
Compute the secant of each element of x.

@anchor{doc-csc}

Mapping Function: csc (x)
Compute the cosecant of each element of x.

@anchor{doc-cot}

Mapping Function: cot (x)
Compute the cotangent of each element of x.

@anchor{doc-asin}

Mapping Function: asin (x)
Compute the inverse sine of each element of x.

@anchor{doc-acos}

Mapping Function: acos (x)
Compute the inverse cosine of each element of x.

@anchor{doc-atan}

Mapping Function: atan (x)
Compute the inverse tangent of each element of x.

@anchor{doc-asec}

Mapping Function: asec (x)
Compute the inverse secant of each element of x.

@anchor{doc-acsc}

Mapping Function: acsc (x)
Compute the inverse cosecant of each element of x.

@anchor{doc-acot}

Mapping Function: acot (x)
Compute the inverse cotangent of each element of x.

@anchor{doc-sinh}

Mapping Function: sinh (x)
Compute the inverse hyperbolic sin of each element of x.

@anchor{doc-cosh}

Mapping Function: cosh (x)
Compute the hyperbolic cosine of each element of x.

@anchor{doc-tanh}

Mapping Function: tanh (x)
Compute hyperbolic tangent of each element of x.

@anchor{doc-sech}

Mapping Function: sech (x)
Compute the hyperbolic secant of each element of x.

@anchor{doc-csch}

Mapping Function: csch (x)
Compute the hyperbolic cosecant of each element of x.

@anchor{doc-coth}

Mapping Function: coth (x)
Compute the hyperbolic cotangent of each element of x.

@anchor{doc-asinh}

Mapping Function: asinh (x)
Ompute the inverse hyperbolic sine of each element of x.

@anchor{doc-acosh}

Mapping Function: acosh (x)
Compute the inverse hyperbolic cosine of each element of x.

@anchor{doc-atanh}

Mapping Function: atanh (x)
Compute the inverse hyperbolic tanget of each element of x.

@anchor{doc-asech}

Mapping Function: asech (x)
Compute the inverse hyperbolic secant of each element of x.

@anchor{doc-acsch}

Mapping Function: acsch (x)
Compute the inverse hyperbolic cosecant of each element of x.

@anchor{doc-acoth}

Mapping Function: acoth (x)
Compute the inverse hyperbolic cotangent of each element of x.

Each of these functions expect a single argument. For matrix arguments, they work on an element by element basis. For example,

sin ([1, 2; 3, 4])
     =>  0.84147   0.90930
         0.14112  -0.75680

@anchor{doc-atan2}

Mapping Function: atan2 (y, x)
Compute atan (y / x) for corresponding elements of y and x. The result is in range -pi to pi.

Sums and Products

@anchor{doc-sum}

Built-in Function: sum (x, dim)
Sum of elements along dimension dim. If dim is omitted, it defaults to 1 (column-wise sum).

@anchor{doc-prod}

Built-in Function: prod (x, dim)
Product of elements along dimension dim. If dim is omitted, it defaults to 1 (column-wise products).

@anchor{doc-cumsum}

Built-in Function: cumsum (x, dim)
Cumulative sum of elements along dimension dim. If dim is omitted, it defaults to 1 (column-wise cumulative sums).

@anchor{doc-cumprod}

Built-in Function: cumprod (x, dim)
Cumulative product of elements along dimension dim. If dim is omitted, it defaults to 1 (column-wise cumulative products).

@anchor{doc-sumsq}

Built-in Function: sumsq (x, dim)
Sum of squares of elements along dimension dim. If dim is omitted, it defaults to 1 (column-wise sum of squares).

This function is equivalent to computing

sum (x .* conj (x), dim)

but it uses less memory and avoids calling conj if x is real.

Special Functions

@anchor{doc-besselj}

Loadable Function: [j, ierr] = besselj (alpha, x, opt)
Loadable Function: [y, ierr] = bessely (alpha, x, opt)
Loadable Function: [i, ierr] = besseli (alpha, x, opt)
Loadable Function: [k, ierr] = besselk (alpha, x, opt)
Loadable Function: [h, ierr] = besselh (alpha, k, x, opt)
Compute Bessel or Hankel functions of various kinds:

besselj
Bessel functions of the first kind.
bessely
Bessel functions of the second kind.
besseli
Modified Bessel functions of the first kind.
besselk
Modified Bessel functions of the second kind.
besselh
Compute Hankel functions of the first (k = 1) or second (k = 2) kind.

If the argument opt is supplied, the result is scaled by the exp (-I*x) for k = 1 or exp (I*x) for k = 2.

If alpha is a scalar, the result is the same size as x. If x is a scalar, the result is the same size as alpha. If alpha is a row vector and x is a column vector, the result is a matrix with length (x) rows and length (alpha) columns. Otherwise, alpha and x must conform and the result will be the same size.

The value of alpha must be real. The value of x may be complex.

If requested, ierr contains the following status information and is the same size as the result.

  1. Normal return.
  2. Input error, return NaN.
  3. Overflow, return Inf.
  4. Loss of significance by argument reduction results in less than half of machine accuracy.
  5. Complete loss of significance by argument reduction, return NaN.
  6. Error--no computation, algorithm termination condition not met, return NaN.

@anchor{doc-airy}

Loadable Function: [a, ierr] = airy (k, z, opt)
Compute Airy functions of the first and second kind, and their derivatives.

  K   Function   Scale factor (if a third argument is supplied)
 --  --------   ----------------------------------------------
  0   Ai (Z)     exp ((2/3) * Z * sqrt (Z))
  1   dAi(Z)/dZ  exp ((2/3) * Z * sqrt (Z))
  2   Bi (Z)     exp (-abs (real ((2/3) * Z *sqrt (Z))))
  3   dBi(Z)/dZ  exp (-abs (real ((2/3) * Z *sqrt (Z))))

The function call airy (z) is equivalent to airy (0, z).

The result is the same size as z.

If requested, ierr contains the following status information and is the same size as the result.

  1. Normal return.
  2. Input error, return NaN.
  3. Overflow, return Inf.
  4. Loss of significance by argument reduction results in less than half of machine accuracy.
  5. Complete loss of significance by argument reduction, return NaN.
  6. Error--no computation, algorithm termination condition not met, return NaN

@anchor{doc-beta}

Mapping Function: beta (a, b)
Return the Beta function,

beta (a, b) = gamma (a) * gamma (b) / gamma (a + b).

@anchor{doc-betainc}

Mapping Function: betainc (x, a, b)
Return the incomplete Beta function,

                                    x
                                   /
betai (a, b, x) = beta (a, b)^(-1) | t^(a-1) (1-t)^(b-1) dt.
                                   /
                                t=0

If x has more than one component, both a and b must be scalars. If x is a scalar, a and b must be of compatible dimensions.

@anchor{doc-bincoeff}

Mapping Function: bincoeff (n, k)
Return the binomial coefficient of n and k, defined as

 /   \
 | n |    n (n-1) (n-2) ... (n-k+1)
 |   |  = -------------------------
 | k |               k!
 \   /

For example,

bincoeff (5, 2)
=> 10

@anchor{doc-erf}

Mapping Function: erf (z)
Computes the error function,

                         z
                        /
erf (z) = (2/sqrt (pi)) | e^(-t^2) dt
                        /
                     t=0

@seealso{erfc and erfinv}

@anchor{doc-erfc}

Mapping Function: erfc (z)
Computes the complementary error function, 1 - erf (z).

@seealso{erf and erfinv}

@anchor{doc-erfinv}

Mapping Function: erfinv (z)
Computes the inverse of the error function.
@seealso{erf and erfc}

@anchor{doc-gamma}

Mapping Function: gamma (z)
Computes the Gamma function,

            infinity
            /
gamma (z) = | t^(z-1) exp (-t) dt.
            /
         t=0

@seealso{gammai and lgamma}

@anchor{doc-gammainc}

Mapping Function: gammainc (x, a)
Computes the incomplete gamma function,

                                x
                      1        /
gammainc (x, a) = ---------    | exp (-t) t^(a-1) dt
                  gamma (a)    /
                            t=0

If a is scalar, then gammainc (x, a) is returned for each element of x and vice versa.

If neither x nor a is scalar, the sizes of x and a must agree, and gammainc is applied element-by-element.

@seealso{gamma and lgamma}

@anchor{doc-lgamma}

Mapping Function: lgamma (a, x)
Mapping Function: gammaln (a, x)
Return the natural logarithm of the gamma function.
@seealso{gamma and gammai}

@anchor{doc-cross}

Function File: cross (x, y)
Computes the vector cross product of the two 3-dimensional vectors x and y.

cross ([1,1,0], [0,1,1])
=> [ 1; -1; 1 ]

If x and y are two - dimensional matrices the cross product is applied along the first dimension with 3 elements.

@anchor{doc-commutation_matrix}

Function File: commutation_matrix (m, n)
Return the commutation matrix K(m,n) which is the unique m*n by m*n matrix such that @math{K(m,n) * vec(A) = vec(A')} for all @math{m} by @math{n} matrices @math{A}.

If only one argument m is given, @math{K(m,m)} is returned.

See Magnus and Neudecker (1988), Matrix differential calculus with applications in statistics and econometrics.

@anchor{doc-duplication_matrix}

Function File: duplication_matrix (n)
Return the duplication matrix @math{Dn} which is the unique @math{n^2} by @math{n*(n+1)/2} matrix such that @math{Dn vech (A) = vec (A)} for all symmetric @math{n} by @math{n} matrices @math{A}.

See Magnus and Neudecker (1988), Matrix differential calculus with applications in statistics and econometrics.

Coordinate Transformations

@anchor{doc-cart2pol}

Function File: [theta, r] = cart2pol (x, y)
Function File: [theta, r, z] = cart2pol (x, y, z)
Transform cartesian to polar or cylindrical coordinates. x, y (and z) must be of same shape. theta describes the angle relative to the x - axis. r is the distance to the z - axis (0, 0, z).
@seealso{pol2cart, cart2sph, sph2cart}

@anchor{doc-pol2cart}

Function File: [x, y] = pol2cart (theta, r)
Function File: [x, y, z] = pol2cart (theta, r, z)
Transform polar or cylindrical to cartesian coordinates. theta, r (and z) must be of same shape. theta describes the angle relative to the x - axis. r is the distance to the z - axis (0, 0, z).
@seealso{cart2pol, cart2sph, sph2cart}

@anchor{doc-cart2sph}

Function File: [theta, phi, r] = cart2sph (x, y, z)
Transform cartesian to spherical coordinates. x, y and z must be of same shape. theta describes the angle relative to the x - axis. phi is the angle relative to the xy - plane. r is the distance to the origin (0, 0, 0).
@seealso{pol2cart, cart2pol, sph2cart}

@anchor{doc-sph2cart}

Function File: [x, y, z] = sph2cart (theta, phi, r)
Transform spherical to cartesian coordinates. x, y and z must be of same shape. theta describes the angle relative to the x-axis. phi is the angle relative to the xy-plane. r is the distance to the origin (0, 0, 0).
@seealso{pol2cart, cart2pol, cart2sph}

Mathematical Constants

@anchor{doc-I}

Built-in Variable: I
Built-in Variable: J
Built-in Variable: i
Built-in Variable: j
A pure imaginary number, defined as sqrt (-1). The I and J forms are true constants, and cannot be modified. The i and j forms are like ordinary variables, and may be used for other purposes. However, unlike other variables, they once again assume their special predefined values if they are cleared See section Status of Variables.

@anchor{doc-Inf}

Built-in Variable: Inf
Built-in Variable: inf
Infinity. This is the result of an operation like 1/0, or an operation that results in a floating point overflow.

@anchor{doc-NaN}

Built-in Variable: NaN
Built-in Variable: nan
Not a number. This is the result of an operation like 0/0, or `Inf - Inf', or any operation with a NaN.

Note that NaN always compares not equal to NaN. This behavior is specified by the IEEE standard for floating point arithmetic. To find NaN values, you must use the isnan function.

@anchor{doc-pi}

Built-in Variable: pi
The ratio of the circumference of a circle to its diameter. Internally, pi is computed as `4.0 * atan (1.0)'.

@anchor{doc-e}

Built-in Variable: e
The base of natural logarithms. The constant e satisfies the equation log (e) = 1.

@anchor{doc-eps}

Built-in Variable: eps
The machine precision. More precisely, eps is the largest relative spacing between any two adjacent numbers in the machine's floating point system. This number is obviously system-dependent. On machines that support 64 bit IEEE floating point arithmetic, eps is approximately 2.2204e-16.

@anchor{doc-realmax}

Built-in Variable: realmax
The largest floating point number that is representable. The actual value is system-dependent. On machines that support 64-bit IEEE floating point arithmetic, realmax is approximately 1.7977e+308

@anchor{doc-realmin}

Built-in Variable: realmin
The smallest normalized floating point number that is representable. The actual value is system-dependent. On machines that support 64-bit IEEE floating point arithmetic, realmin is approximately 2.2251e-308


Go to the first, previous, next, last section, table of contents.