# 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, and given a matrix, they work on an element by element basis.

`ceil (x)`
Return the smallest integer not less than x. If x is complex, return `ceil (real (x)) + ceil (imag (x)) * I`.
`floor (x)`
Return the largest integer not greater than x. If x is complex, return `floor (real (x)) + floor (imag (x)) * I`.
`fix (x)`
Truncate x toward zero. If x is complex, return `fix (real (x)) + fix (imag (x)) * I`.
`round (x)`
Return the integer nearest to x. If x is complex, return `round (real (x)) + round (imag (x)) * I`.
`sign (x)`
Compute the signum function, which is defined as For complex arguments, `sign` returns `x ./ abs (x)`.
`exp (x)`
Compute the exponential of x. To compute the matrix exponential, see section Linear Algebra.
`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)
```
`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]).
```
`log (x)`
Compute the natural logarithm of x. To compute the matrix logarithm, see section Linear Algebra.
`log2 (x)`
Compute the base-2 logarithm of x.
`log10 (x)`
Compute the base-10 logarithm of x.
`sqrt (x)`
Compute the square root of x. To compute the matrix square root, see section Linear Algebra.
`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.
`min (x)`
Like `max`, but return the minimum value.
`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.

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

`abs (x)`
Compute the magnitude of x.
`angle (x)`
`arg (x)`
Compute the argument of x.
`conj (x)`
Return the complex conjugate of x.
`imag (x)`
Return the imaginary part of x.
`real (x)`
Return the real part of x.

## Trigonometry

Octave provides the following trigonometric functions:

```sin    asin    sinh    asinh
cos    acos    cosh    acosh
tan    atan    tanh    atanh

sec    asec    sech    asech
csc    acsc    csch    acsch
cot    acot    coth    acoth
```

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

```sin ([1, 2; 3, 4])
```

produces

```ans =

0.84147   0.90930
0.14112  -0.75680
```

`atan2 (y, x)`

## Sums and Products

`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).
`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).
`cumsum (x)`
Return the cumulative sum of each column of x. For example,
```cumsum ([1, 2; 3, 4])
```
produces
```ans =

1  2
4  6
```
`cumprod (x)`
Return the cumulative product of each column of x. For example,
```cumprod ([1, 2; 3, 4])
```
produces
```ans =

1  2
3  8
```
`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

`beta`
Returns the beta function,
`betai (a, b, x)`
Returns 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.
`erf`
Computes the error function,
`erfc (z)`
Computes the complementary error function, `1 - erf (z)`.
`erfinv`
Computes the inverse of the error function.
`gamma (z)`
Computes the gamma 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.
`lgamma`
Returns the natural logarithm of the gamma function.