GNU Octave - Arithmetic

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.

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

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

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

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

Mapping Function: log (x)
  Compute the natural logarithm of x. To compute the matrix logarithm, see section Linear Algebra.

Mapping Function: log10 (x)
  Compute the base-10 logarithm of x.

Mapping Function: y = log2 (x)
Mapping Function: [f, e] log2 (x)
  Compute the base-2 logarithm of x. With two outputs, returns f and e such that

Loadable Function: 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. Thus,

      max (max (x))

  returns the largest element of x.

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

Loadable Function: min (x)
  Like `max`, but return the minimum value.

Function File: nextpow2 (x)
  If x is a scalar, returns the first integer n such that

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

Mapping Function: pow2 (x)
Mapping Function: pow2 (f, e)
  With one argument, computes for each element of x. With two arguments, returns

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.

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

Mapping Function: sign (x)
  Compute the signum function, which is defined as

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

Mapping Function: xor (x, y)
  Return the `exclusive or' of the entries of x and y. For boolean expressions x and y, `xor (x, y)` is true if and only if x or y is true, but not if both x and y are true.

## 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,

Mapping Function: abs (z)
  Compute the magnitude of z, defined as

  For example,

      abs (3 + 4i)
           => 5

Mapping Function: arg (z)
Mapping Function: angle (z)
  Compute the argument of z, defined as

  in radians.

  For example,

      arg (3 + 4i)
           => 0.92730

Mapping Function: conj (z)
  Return the complex conjugate of z, defined as

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

Mapping Function: real (z)
  Return the real part of z.

## Trigonometry

Octave provides the following trigonometric functions:

Mapping Function: sin (z)
Mapping Function: cos (z)
Mapping Function: tan (z)
Mapping Function: sec (z)
Mapping Function: csc (z)
Mapping Function: cot (z)
  The ordinary trigonometric functions.

Mapping Function: asin (z)
Mapping Function: acos (z)
Mapping Function: atan (z)
Mapping Function: asec (z)
Mapping Function: acsc (z)
Mapping Function: acot (z)
  The ordinary inverse trigonometric functions.

Mapping Function: sinh (z)
Mapping Function: cosh (z)
Mapping Function: tanh (z)
Mapping Function: sech (z)
Mapping Function: csch (z)
Mapping Function: coth (z)
  Hyperbolic trigonometric functions.

Mapping Function: asinh (z)
Mapping Function: acosh (z)
Mapping Function: atanh (z)
Mapping Function: asech (z)
Mapping Function: acsch (z)
Mapping Function: acoth (z)
  Inverse hyperbolic trigonometric functions.

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

Mapping Function: atan2 (y, x)
  Return the arctangent of y/x. The signs of the arguments are used to determine the quadrant of the result, which is in the range

## Sums and Products

Built-in Function: sum (x)
  For a vector argument, return the sum of all the elements. For a matrix argument, return the sum of the elements in each column, as a row vector. The sum of an empty matrix is 0 if it has no columns, or a vector of zeros if it has no rows (see section Empty Matrices).

Built-in Function: prod (x)
  For a vector argument, return the product of all the elements. For a matrix argument, return the product of the elements in each column, as a row vector. The product of an empty matrix is 1 if it has no columns, or a vector of ones if it has no rows (see section Empty Matrices).

Built-in Function: cumsum (x)
  Return the cumulative sum of each column of x. For example,

      cumsum ([1, 2; 3, 4])
           =>  1  2
               4  6

Built-in Function: cumprod (x)
  Return the cumulative product of each column of x. For example,

      cumprod ([1, 2; 3, 4])
           =>  1  2
               3  8

Built-in Function: sumsq (x)
  For a vector argument, return the sum of the squares of all the elements. For a matrix argument, return the sum of the squares of the elements in each column, as a row vector.

## Special Functions

Mapping Function: besseli (alpha, x)
Mapping Function: besselj (alpha, x)
Mapping Function: besselk (alpha, x)
Mapping Function: bessely (alpha, x)
  Compute Bessel functions of the following types:
  `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.

  The second argument, x, must be a real matrix, vector, or scalar.

  The first argument, alpha, must be greater than or equal to zero. If alpha is a range, it must have an increment equal to one.

  If alpha is a scalar, the result is the same size as x.

  If alpha is a range, x must be a vector or scalar, and the result is a matrix with `length(x)` rows and `length(alpha)` columns.

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

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

  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.

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

  For example,

      bincoeff (5, 2)
           => 10

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

Mapping Function: erfc (z)
  Computes the complementary error function,

Mapping Function: erfinv (z)
  Computes the inverse of the error function,

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

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

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

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

Mapping Function: lgamma (a, x)
Mapping Function: gammaln (a, x)
  Return the natural logarithm of the gamma function.

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

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

Function File: commutation_matrix (m, n)
  Return the commutation matrix which is the unique matrix such that for all matrices

  If only one argument m is given, is returned.

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

Function File: duplication_matrix (n)
  Return the duplication matrix which is the unique matrix such that for all symmetric matrices

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

## Mathematical Constants

Built-in Variable: I
Built-in Variable: J
Built-in Variable: i
Built-in Variable: j
  A pure imaginary number, defined as 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.

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.

Built-in Variable: NaN
Built-in Variable: nan
  Not a number. This is the result of an operation like 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.

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

Built-in Variable: e
  The base of natural logarithms. The constant satisfies the equation

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

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

Built-in Variable: realmin
  The smallest 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

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