Previous: , Up: Numeric Data Types   [Contents][Index]

### 4.8 Predicates for Numeric Objects

Since the type of a variable may change during the execution of a program, it can be necessary to do type checking at run-time. Doing this also allows you to change the behavior of a function depending on the type of the input. As an example, this naive implementation of `abs` returns the absolute value of the input if it is a real number, and the length of the input if it is a complex number.

```function a = abs (x)
if (isreal (x))
a = sign (x) .* x;
elseif (iscomplex (x))
a = sqrt (real(x).^2 + imag(x).^2);
endif
endfunction
```

The following functions are available for determining the type of a variable.

Built-in Function: isnumeric (x)

Return true if x is a numeric object, i.e., an integer, real, or complex array.

Logical and character arrays are not considered to be numeric.

Built-in Function: islogical (x)
Built-in Function: isbool (x)

Return true if x is a logical object.

Built-in Function: isfloat (x)

Return true if x is a floating-point numeric object.

Objects of class double or single are floating-point objects.

Built-in Function: isreal (x)

Return true if x is a non-complex matrix or scalar.

For compatibility with MATLAB, this includes logical and character matrices.

Built-in Function: iscomplex (x)

Return true if x is a complex-valued numeric object.

Built-in Function: ismatrix (a)

Return true if a is a 2-D array.

Function File: isvector (x)

Return true if x is a vector.

A vector is a 2-D array where one of the dimensions is equal to 1. As a consequence a 1x1 array, or scalar, is also a vector.

Function File: isrow (x)

Return true if x is a row vector 1xN with non-negative N.

Function File: iscolumn (x)

Return true if x is a column vector Nx1 with non-negative N.

Built-in Function: isscalar (x)

Return true if x is a scalar.

Function File: issquare (x)

Return true if x is a square matrix.

Function File: issymmetric (A)
Function File: issymmetric (A, tol)

Return true if A is a symmetric matrix within the tolerance specified by tol.

The default tolerance is zero (uses faster code).

Matrix A is considered symmetric if `norm (A - A.', Inf) / norm (A, Inf) < tol`.

Function File: ishermitian (A)
Function File: ishermitian (A, tol)

Return true if A is Hermitian within the tolerance specified by tol.

The default tolerance is zero (uses faster code).

Matrix A is considered symmetric if `norm (A - A', Inf) / norm (A, Inf) < tol`.

Function File: isdefinite (A)
Function File: isdefinite (A, tol)

Return 1 if A is symmetric positive definite within the tolerance specified by tol or 0 if A is symmetric positive semidefinite. Otherwise, return -1.

If tol is omitted, use a tolerance of `100 * eps * norm (A, "fro")`

Function File: isbanded (A, lower, upper)

Return true if A is a matrix with entries confined between lower diagonals below the main diagonal and upper diagonals above the main diagonal.

lower and upper must be non-negative integers.

Function File: isdiag (A)

Return true if A is a diagonal matrix.

Function File: istril (A)

Return true if A is a lower triangular matrix.

A lower triangular matrix has nonzero entries only on the main diagonal and below.

Function File: istriu (A)

Return true if A is an upper triangular matrix.

An upper triangular matrix has nonzero entries only on the main diagonal and above.

Function File: isprime (x)

Return a logical array which is true where the elements of x are prime numbers and false where they are not.

A prime number is conventionally defined as a positive integer greater than 1 (e.g., 2, 3, …) which is divisible only by itself and 1. Octave extends this definition to include both negative integers and complex values. A negative integer is prime if its positive counterpart is prime. This is equivalent to `isprime (abs (x))`.

If `class (x)` is complex, then primality is tested in the domain of Gaussian integers (http://en.wikipedia.org/wiki/Gaussian_integer). Some non-complex integers are prime in the ordinary sense, but not in the domain of Gaussian integers. For example, 5 = (1+2i)*(1-2i) shows that 5 is not prime because it has a factor other than itself and 1. Exercise caution when testing complex and real values together in the same matrix.

Examples:

```isprime (1:6)
⇒ [0, 1, 1, 0, 1, 0]
```
```isprime ([i, 2, 3, 5])
⇒ [0, 0, 1, 0]
```

Programming Note: `isprime` is appropriate if the maximum value in x is not too large (< 1e15). For larger values special purpose factorization code should be used.

Compatibility Note: matlab does not extend the definition of prime numbers and will produce an error if given negative or complex inputs.