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

: `tf =` 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.

See also: isinteger, isfloat, isreal, iscomplex, ischar, islogical, isstring, iscell, isstruct, isa.

: `tf =` islogical `(x)`
: `tf =` isbool `(x)`

Return true if x is a logical object.

Programming Note: `isbool` is an alias for `islogical` and can be used interchangeably.

: `tf =` isfloat `(x)`

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

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

: `tf =` isreal `(x)`

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

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

: `tf =` iscomplex `(x)`

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

: `tf =` ismatrix `(x)`

Return true if x is a 2-D array.

A matrix is an array of any type where `ndims (x) == 2` and for which `size (x)` returns `[M, N]` with non-negative M and N.

: `tf =` isvector `(x)`

Return true if x is a vector.

A vector is a 2-D array of any type where one of the dimensions is equal to 1 (either 1xN or Nx1). As a consequence of this definition, a 1x1 object (a scalar) is also a vector.

: `tf =` isrow `(x)`

Return true if x is a row vector.

A row vector is a 2-D array of any type for which `size (x)` returns `[1, N]` with non-negative N.

: `tf =` iscolumn `(x)`

Return true if x is a column vector.

A column vector is a 2-D array of any type for which `size (x)` returns `[N, 1]` with non-negative N.

: `tf =` isscalar `(x)`

Return true if x is a scalar.

A scalar is a single-element object of any type for which `size (x)` returns `[1, 1]`.

: `tf =` issquare `(x)`

Return true if x is a 2-D square array.

A square array is a 2-D array of any type for which `size (x)` returns `[N, N]` where N is a non-negative integer.

: `tf =` issymmetric `(A)`
: `tf =` issymmetric `(A, tol)`
: `tf =` issymmetric `(A, "skew")`
: `tf =` issymmetric `(A, "skew", tol)`

Return true if A is a symmetric or skew-symmetric numeric matrix within the tolerance specified by tol.

The default tolerance is zero (uses faster code).

The type of symmetry to check may be specified with the additional input `"nonskew"` (default) for regular symmetry or `"skew"` for skew-symmetry.

Background: A matrix is symmetric if the transpose of the matrix is equal to the original matrix: `A == A.'`. If a tolerance is given then symmetry is determined by `norm (A - A.', Inf) / norm (A, Inf) < tol`.

A matrix is skew-symmetric if the transpose of the matrix is equal to the negative of the original matrix: `A == -A.'`. If a tolerance is given then skew-symmetry is determined by `norm (A + A.', Inf) / norm (A, Inf) < tol`.

: `tf =` ishermitian `(A)`
: `tf =` ishermitian `(A, tol)`
: `tf =` ishermitian `(A, "skew")`
: `tf =` ishermitian `(A, "skew", tol)`

Return true if A is a Hermitian or skew-Hermitian numeric matrix within the tolerance specified by tol.

The default tolerance is zero (uses faster code).

The type of symmetry to check may be specified with the additional input `"nonskew"` (default) for regular Hermitian or `"skew"` for skew-Hermitian.

Background: A matrix is Hermitian if the complex conjugate transpose of the matrix is equal to the original matrix: `A == A'`. If a tolerance is given then the calculation is `norm (A - A', Inf) / norm (A, Inf) < tol`.

A matrix is skew-Hermitian if the complex conjugate transpose of the matrix is equal to the negative of the original matrix: `A == -A'`. If a tolerance is given then the calculation is `norm (A + A', Inf) / norm (A, Inf) < tol`.

: `tf =` isdefinite `(A)`
: `tf =` isdefinite `(A, tol)`

Return true if A is symmetric positive definite numeric matrix within the tolerance specified by tol.

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

Background: A positive definite matrix has eigenvalues which are all greater than zero. A positive semi-definite matrix has eigenvalues which are all greater than or equal to zero. The matrix A is very likely to be positive semi-definite if the following two conditions hold for a suitably small tolerance tol.

```isdefinite (A) ⇒ 0
isdefinite (A + 5*tol, tol) ⇒ 1
```

: `tf =` isbanded `(A, lower, upper)`

Return true if A is a numeric 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.

: `tf =` isdiag `(A)`

Return true if A is a diagonal numeric matrix which is defined as a 2-D array where all elements above and below the main diagonal are zero.

: `tf =` istril `(A)`

Return true if A is a lower triangular numeric matrix.

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

: `tf =` istriu `(A)`

Return true if A is an upper triangular numeric matrix.

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

: `tf =` 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 (https://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 suitable for all x in the range abs(x) < 2^64. Cast inputs larger than `flintmax` to `uint64`.

For larger inputs, use ‘sym’ if you have the Symbolic package installed and loaded:

```isprime (sym ('58745389709258902525390450') + (0:4))
⇒  0  1  0  0  0
```

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

: `tf =` isuniform `(v)`
: `[tf, delta] =` isuniform `(v)`

Return true if the real vector v is uniformly spaced and false otherwise.

A vector is uniform if the mean difference (delta) between all elements is the same to within a tolerance of `4 * eps (max (abs (v)))`.

The optional output delta is the uniform difference between elements. If the vector is not uniform then delta is `NaN`. delta is of the same class as v for floating point inputs and of class double for integer, logical, and character inputs.

Programming Notes: The output is always false for the special cases of an empty input or a scalar input. If any element is `NaN` then the output is false. If delta is smaller than the calculated relative tolerance then an absolute tolerance of `eps` is used.