GNU Octave Manual Version 3by John W. Eaton, David Bateman, Søren Hauberg Paperback (6"x9"), 568 pages ISBN 095461206X RRP £24.95 ($39.95) |

## 4.6 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 behaviour 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
magnitude 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 nonzero if
`x`is a numeric object.

__Built-in Function:__**isreal***(*`x`)- Return true if
`x`is a real-valued numeric object.

__Built-in Function:__**iscomplex***(*`x`)- Return true if
`x`is a complex-valued numeric object.

__Built-in Function:__**ismatrix***(*`a`)- Return 1 if
`a`is a matrix. Otherwise, return 0.

__Function File:__**isvector***(*`a`)- Return 1 if
`a`is a vector. Otherwise, return 0.See also size, rows, columns, length, isscalar, ismatrix

__Function File:__**isscalar***(*`a`)- Return 1 if
`a`is a scalar. Otherwise, return 0.See also size, rows, columns, length, isscalar, ismatrix

__Function File:__**issquare***(*`x`)- If
`x`is a square matrix, then return the dimension of`x`. Otherwise, return 0.See also size, rows, columns, length, ismatrix, isscalar, isvector

__Function File:__**issymmetric***(*`x`,`tol`)- If
`x`is symmetric within the tolerance specified by`tol`, then return the dimension of`x`. Otherwise, return 0. If`tol`is omitted, use a tolerance equal to the machine precision. Matrix`x`is considered symmetric if`norm (`

.`x`-`x`.', inf) / norm (`x`, inf) <`tol`See also size, rows, columns, length, ishermitian, ismatrix, isscalar, issquare, isvector

__Function File:__**isdefinite***(*`x`,`tol`)- Return 1 if
`x`is symmetric positive definite within the tolerance specified by`tol`or 0 if`x`is symmetric positive semidefinite. Otherwise, return -1. If`tol`is omitted, use a tolerance equal to 100 times the machine precision.See also issymmetric

__Built-in Function:__**islogical***(*`x`)- Return true if
`x`is a logical object.

__Function File:__**isprime***(*`n`)-
Return true if

`n`is a prime number, false otherwise.Something like the following is much faster if you need to test a lot of small numbers:

`t`= ismember (`n`, primes (max (`n`(:))));If max(n) is very large, then you should be using special purpose factorization code.

See also primes, factor, gcd, lcm

ISBN 095461206X | GNU Octave Manual Version 3 | See the print edition |