File: Predicates-for-Numeric-Objects.html

package info (click to toggle)
octave 4.4.1-5
  • links: PTS, VCS
  • area: main
  • in suites: buster
  • size: 114,832 kB
  • sloc: cpp: 310,009; ansic: 54,616; fortran: 22,631; yacc: 8,706; sh: 8,231; objc: 7,972; lex: 3,897; perl: 1,540; java: 1,309; awk: 1,070; makefile: 415; xml: 59
file content (323 lines) | stat: -rw-r--r-- 16,623 bytes parent folder | download
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
<!DOCTYPE html PUBLIC "-//W3C//DTD HTML 4.01 Transitional//EN" "http://www.w3.org/TR/html4/loose.dtd">
<html>
<!-- Created by GNU Texinfo 6.5, http://www.gnu.org/software/texinfo/ -->
<head>
<meta http-equiv="Content-Type" content="text/html; charset=utf-8">
<title>Predicates for Numeric Objects (GNU Octave (version 4.4.1))</title>

<meta name="description" content="Predicates for Numeric Objects (GNU Octave (version 4.4.1))">
<meta name="keywords" content="Predicates for Numeric Objects (GNU Octave (version 4.4.1))">
<meta name="resource-type" content="document">
<meta name="distribution" content="global">
<meta name="Generator" content="makeinfo">
<link href="index.html#Top" rel="start" title="Top">
<link href="Concept-Index.html#Concept-Index" rel="index" title="Concept Index">
<link href="index.html#SEC_Contents" rel="contents" title="Table of Contents">
<link href="Numeric-Data-Types.html#Numeric-Data-Types" rel="up" title="Numeric Data Types">
<link href="Strings.html#Strings" rel="next" title="Strings">
<link href="Promotion-and-Demotion-of-Data-Types.html#Promotion-and-Demotion-of-Data-Types" rel="prev" title="Promotion and Demotion of Data Types">
<style type="text/css">
<!--
a.summary-letter {text-decoration: none}
blockquote.indentedblock {margin-right: 0em}
blockquote.smallindentedblock {margin-right: 0em; font-size: smaller}
blockquote.smallquotation {font-size: smaller}
div.display {margin-left: 3.2em}
div.example {margin-left: 3.2em}
div.lisp {margin-left: 3.2em}
div.smalldisplay {margin-left: 3.2em}
div.smallexample {margin-left: 3.2em}
div.smalllisp {margin-left: 3.2em}
kbd {font-style: oblique}
pre.display {font-family: inherit}
pre.format {font-family: inherit}
pre.menu-comment {font-family: serif}
pre.menu-preformatted {font-family: serif}
pre.smalldisplay {font-family: inherit; font-size: smaller}
pre.smallexample {font-size: smaller}
pre.smallformat {font-family: inherit; font-size: smaller}
pre.smalllisp {font-size: smaller}
span.nolinebreak {white-space: nowrap}
span.roman {font-family: initial; font-weight: normal}
span.sansserif {font-family: sans-serif; font-weight: normal}
ul.no-bullet {list-style: none}
-->
</style>
<link rel="stylesheet" type="text/css" href="octave.css">


</head>

<body lang="en">
<a name="Predicates-for-Numeric-Objects"></a>
<div class="header">
<p>
Previous: <a href="Promotion-and-Demotion-of-Data-Types.html#Promotion-and-Demotion-of-Data-Types" accesskey="p" rel="prev">Promotion and Demotion of Data Types</a>, Up: <a href="Numeric-Data-Types.html#Numeric-Data-Types" accesskey="u" rel="up">Numeric Data Types</a> &nbsp; [<a href="index.html#SEC_Contents" title="Table of contents" rel="contents">Contents</a>][<a href="Concept-Index.html#Concept-Index" title="Index" rel="index">Index</a>]</p>
</div>
<hr>
<a name="Predicates-for-Numeric-Objects-1"></a>
<h3 class="section">4.8 Predicates for Numeric Objects</h3>

<p>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 <code>abs</code>
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.
</p>
<div class="example">
<pre class="example">function a = abs (x)
  if (isreal (x))
    a = sign (x) .* x;
  elseif (iscomplex (x))
    a = sqrt (real(x).^2 + imag(x).^2);
  endif
endfunction
</pre></div>

<p>The following functions are available for determining the type of a
variable.
</p>
<a name="XREFisnumeric"></a><dl>
<dt><a name="index-isnumeric"></a><em></em> <strong>isnumeric</strong> <em>(<var>x</var>)</em></dt>
<dd><p>Return true if <var>x</var> is a numeric object, i.e., an integer, real, or
complex array.
</p>
<p>Logical and character arrays are not considered to be numeric.
</p>
<p><strong>See also:</strong> <a href="Integer-Data-Types.html#XREFisinteger">isinteger</a>, <a href="#XREFisfloat">isfloat</a>, <a href="#XREFisreal">isreal</a>, <a href="#XREFiscomplex">iscomplex</a>, <a href="Character-Arrays.html#XREFischar">ischar</a>, <a href="#XREFislogical">islogical</a>, <a href="Character-Arrays.html#XREFisstring">isstring</a>, <a href="Basic-Usage-of-Cell-Arrays.html#XREFiscell">iscell</a>, <a href="Creating-Structures.html#XREFisstruct">isstruct</a>, <a href="Built_002din-Data-Types.html#XREFisa">isa</a>.
</p></dd></dl>


<a name="XREFislogical"></a><dl>
<dt><a name="index-islogical"></a><em></em> <strong>islogical</strong> <em>(<var>x</var>)</em></dt>
<dt><a name="index-isbool"></a><em></em> <strong>isbool</strong> <em>(<var>x</var>)</em></dt>
<dd><p>Return true if <var>x</var> is a logical object.
</p>
<p><strong>See also:</strong> <a href="Character-Arrays.html#XREFischar">ischar</a>, <a href="#XREFisfloat">isfloat</a>, <a href="Integer-Data-Types.html#XREFisinteger">isinteger</a>, <a href="Character-Arrays.html#XREFisstring">isstring</a>, <a href="#XREFisnumeric">isnumeric</a>, <a href="Built_002din-Data-Types.html#XREFisa">isa</a>.
</p></dd></dl>


<a name="XREFisfloat"></a><dl>
<dt><a name="index-isfloat"></a><em></em> <strong>isfloat</strong> <em>(<var>x</var>)</em></dt>
<dd><p>Return true if <var>x</var> is a floating-point numeric object.
</p>
<p>Objects of class double or single are floating-point objects.
</p>
<p><strong>See also:</strong> <a href="Integer-Data-Types.html#XREFisinteger">isinteger</a>, <a href="Character-Arrays.html#XREFischar">ischar</a>, <a href="#XREFislogical">islogical</a>, <a href="#XREFisnumeric">isnumeric</a>, <a href="Character-Arrays.html#XREFisstring">isstring</a>, <a href="Built_002din-Data-Types.html#XREFisa">isa</a>.
</p></dd></dl>


<a name="XREFisreal"></a><dl>
<dt><a name="index-isreal"></a><em></em> <strong>isreal</strong> <em>(<var>x</var>)</em></dt>
<dd><p>Return true if <var>x</var> is a non-complex matrix or scalar.
</p>
<p>For compatibility with <small>MATLAB</small>, this includes logical and character
matrices.
</p>
<p><strong>See also:</strong> <a href="#XREFiscomplex">iscomplex</a>, <a href="#XREFisnumeric">isnumeric</a>, <a href="Built_002din-Data-Types.html#XREFisa">isa</a>.
</p></dd></dl>


<a name="XREFiscomplex"></a><dl>
<dt><a name="index-iscomplex"></a><em></em> <strong>iscomplex</strong> <em>(<var>x</var>)</em></dt>
<dd><p>Return true if <var>x</var> is a complex-valued numeric object.
</p>
<p><strong>See also:</strong> <a href="#XREFisreal">isreal</a>, <a href="#XREFisnumeric">isnumeric</a>, <a href="Character-Arrays.html#XREFischar">ischar</a>, <a href="#XREFisfloat">isfloat</a>, <a href="#XREFislogical">islogical</a>, <a href="Character-Arrays.html#XREFisstring">isstring</a>, <a href="Built_002din-Data-Types.html#XREFisa">isa</a>.
</p></dd></dl>


<a name="XREFismatrix"></a><dl>
<dt><a name="index-ismatrix"></a><em></em> <strong>ismatrix</strong> <em>(<var>a</var>)</em></dt>
<dd><p>Return true if <var>a</var> is a 2-D array.
</p>
<p><strong>See also:</strong> <a href="#XREFisscalar">isscalar</a>, <a href="#XREFisvector">isvector</a>, <a href="Basic-Usage-of-Cell-Arrays.html#XREFiscell">iscell</a>, <a href="Creating-Structures.html#XREFisstruct">isstruct</a>, <a href="Information.html#XREFissparse">issparse</a>, <a href="Built_002din-Data-Types.html#XREFisa">isa</a>.
</p></dd></dl>


<a name="XREFisvector"></a><dl>
<dt><a name="index-isvector"></a><em></em> <strong>isvector</strong> <em>(<var>x</var>)</em></dt>
<dd><p>Return true if <var>x</var> is a vector.
</p>
<p>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.
</p>
<p><strong>See also:</strong> <a href="#XREFisscalar">isscalar</a>, <a href="#XREFismatrix">ismatrix</a>, <a href="Object-Sizes.html#XREFsize">size</a>, <a href="Object-Sizes.html#XREFrows">rows</a>, <a href="Object-Sizes.html#XREFcolumns">columns</a>, <a href="Object-Sizes.html#XREFlength">length</a>.
</p></dd></dl>


<a name="XREFisrow"></a><dl>
<dt><a name="index-isrow"></a><em></em> <strong>isrow</strong> <em>(<var>x</var>)</em></dt>
<dd><p>Return true if <var>x</var> is a row vector 1xN with non-negative N.
</p>
<p><strong>See also:</strong> <a href="#XREFiscolumn">iscolumn</a>, <a href="#XREFisscalar">isscalar</a>, <a href="#XREFisvector">isvector</a>, <a href="#XREFismatrix">ismatrix</a>.
</p></dd></dl>


<a name="XREFiscolumn"></a><dl>
<dt><a name="index-iscolumn"></a><em></em> <strong>iscolumn</strong> <em>(<var>x</var>)</em></dt>
<dd><p>Return true if <var>x</var> is a column vector Nx1 with non-negative N.
</p>
<p><strong>See also:</strong> <a href="#XREFisrow">isrow</a>, <a href="#XREFisscalar">isscalar</a>, <a href="#XREFisvector">isvector</a>, <a href="#XREFismatrix">ismatrix</a>.
</p></dd></dl>


<a name="XREFisscalar"></a><dl>
<dt><a name="index-isscalar"></a><em></em> <strong>isscalar</strong> <em>(<var>x</var>)</em></dt>
<dd><p>Return true if <var>x</var> is a scalar.
</p>
<p><strong>See also:</strong> <a href="#XREFisvector">isvector</a>, <a href="#XREFismatrix">ismatrix</a>.
</p></dd></dl>


<a name="XREFissquare"></a><dl>
<dt><a name="index-issquare"></a><em></em> <strong>issquare</strong> <em>(<var>x</var>)</em></dt>
<dd><p>Return true if <var>x</var> is a square matrix.
</p>
<p><strong>See also:</strong> <a href="#XREFisscalar">isscalar</a>, <a href="#XREFisvector">isvector</a>, <a href="#XREFismatrix">ismatrix</a>, <a href="Object-Sizes.html#XREFsize">size</a>.
</p></dd></dl>


<a name="XREFissymmetric"></a><dl>
<dt><a name="index-issymmetric"></a><em></em> <strong>issymmetric</strong> <em>(<var>A</var>)</em></dt>
<dt><a name="index-issymmetric-1"></a><em></em> <strong>issymmetric</strong> <em>(<var>A</var>, <var>tol</var>)</em></dt>
<dd><p>Return true if <var>A</var> is a symmetric matrix within the tolerance specified
by <var>tol</var>.
</p>
<p>The default tolerance is zero (uses faster code).
</p>
<p>Matrix <var>A</var> is considered symmetric if
<code>norm (<var>A</var> - <var>A</var>.', Inf) / norm (<var>A</var>, Inf) &lt; <var>tol</var></code>.
</p>
<p><strong>See also:</strong> <a href="#XREFishermitian">ishermitian</a>, <a href="#XREFisdefinite">isdefinite</a>.
</p></dd></dl>


<a name="XREFishermitian"></a><dl>
<dt><a name="index-ishermitian"></a><em></em> <strong>ishermitian</strong> <em>(<var>A</var>)</em></dt>
<dt><a name="index-ishermitian-1"></a><em></em> <strong>ishermitian</strong> <em>(<var>A</var>, <var>tol</var>)</em></dt>
<dd><p>Return true if <var>A</var> is Hermitian within the tolerance specified by
<var>tol</var>.
</p>
<p>The default tolerance is zero (uses faster code).
</p>
<p>Matrix <var>A</var> is considered symmetric if
<code>norm (<var>A</var> - <var>A</var>', Inf) / norm (<var>A</var>, Inf) &lt; <var>tol</var></code>.
</p>
<p><strong>See also:</strong> <a href="#XREFissymmetric">issymmetric</a>, <a href="#XREFisdefinite">isdefinite</a>.
</p></dd></dl>


<a name="XREFisdefinite"></a><dl>
<dt><a name="index-isdefinite"></a><em></em> <strong>isdefinite</strong> <em>(<var>A</var>)</em></dt>
<dt><a name="index-isdefinite-1"></a><em></em> <strong>isdefinite</strong> <em>(<var>A</var>, <var>tol</var>)</em></dt>
<dd><p>Return 1 if <var>A</var> is symmetric positive definite within the
tolerance specified by <var>tol</var> or 0 if <var>A</var> is symmetric
positive semi-definite.  Otherwise, return -1.
</p>
<p>If <var>tol</var> is omitted, use a tolerance of
<code>100 * eps * norm (<var>A</var>, &quot;fro&quot;)</code>
</p>
<p><strong>See also:</strong> <a href="#XREFissymmetric">issymmetric</a>, <a href="#XREFishermitian">ishermitian</a>.
</p></dd></dl>


<a name="XREFisbanded"></a><dl>
<dt><a name="index-isbanded"></a><em></em> <strong>isbanded</strong> <em>(<var>A</var>, <var>lower</var>, <var>upper</var>)</em></dt>
<dd><p>Return true if <var>A</var> is a matrix with entries confined between
<var>lower</var> diagonals below the main diagonal and <var>upper</var> diagonals
above the main diagonal.
</p>
<p><var>lower</var> and <var>upper</var> must be non-negative integers.
</p>
<p><strong>See also:</strong> <a href="#XREFisdiag">isdiag</a>, <a href="#XREFistril">istril</a>, <a href="#XREFistriu">istriu</a>, <a href="Basic-Matrix-Functions.html#XREFbandwidth">bandwidth</a>.
</p></dd></dl>


<a name="XREFisdiag"></a><dl>
<dt><a name="index-isdiag"></a><em></em> <strong>isdiag</strong> <em>(<var>A</var>)</em></dt>
<dd><p>Return true if <var>A</var> is a diagonal matrix.
</p>
<p><strong>See also:</strong> <a href="#XREFisbanded">isbanded</a>, <a href="#XREFistril">istril</a>, <a href="#XREFistriu">istriu</a>, <a href="Rearranging-Matrices.html#XREFdiag">diag</a>, <a href="Basic-Matrix-Functions.html#XREFbandwidth">bandwidth</a>.
</p></dd></dl>


<a name="XREFistril"></a><dl>
<dt><a name="index-istril"></a><em></em> <strong>istril</strong> <em>(<var>A</var>)</em></dt>
<dd><p>Return true if <var>A</var> is a lower triangular matrix.
</p>
<p>A lower triangular matrix has nonzero entries only on the main diagonal and
below.
</p>
<p><strong>See also:</strong> <a href="#XREFistriu">istriu</a>, <a href="#XREFisbanded">isbanded</a>, <a href="#XREFisdiag">isdiag</a>, <a href="Rearranging-Matrices.html#XREFtril">tril</a>, <a href="Basic-Matrix-Functions.html#XREFbandwidth">bandwidth</a>.
</p></dd></dl>


<a name="XREFistriu"></a><dl>
<dt><a name="index-istriu"></a><em></em> <strong>istriu</strong> <em>(<var>A</var>)</em></dt>
<dd><p>Return true if <var>A</var> is an upper triangular matrix.
</p>
<p>An upper triangular matrix has nonzero entries only on the main diagonal and
above.
</p>
<p><strong>See also:</strong> <a href="#XREFisdiag">isdiag</a>, <a href="#XREFisbanded">isbanded</a>, <a href="#XREFistril">istril</a>, <a href="Rearranging-Matrices.html#XREFtriu">triu</a>, <a href="Basic-Matrix-Functions.html#XREFbandwidth">bandwidth</a>.
</p></dd></dl>


<a name="XREFisprime"></a><dl>
<dt><a name="index-isprime"></a><em></em> <strong>isprime</strong> <em>(<var>x</var>)</em></dt>
<dd><p>Return a logical array which is true where the elements of <var>x</var> are prime
numbers and false where they are not.
</p>
<p>A prime number is conventionally defined as a positive integer greater than
1 (e.g., 2, 3, &hellip;) 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 <code>isprime (abs (x))</code>.
</p>
<p>If <code>class (<var>x</var>)</code> is complex, then primality is tested in the domain
of Gaussian integers (<a href="https://en.wikipedia.org/wiki/Gaussian_integer">https://en.wikipedia.org/wiki/Gaussian_integer</a>).
Some non-complex integers are prime in the ordinary sense, but not in the
domain of Gaussian integers.  For example, <em>5 = (1+2i)*(1-2i)</em> 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.
</p>
<p>Examples:
</p>
<div class="example">
<pre class="example">isprime (1:6)
    &rArr; [0, 1, 1, 0, 1, 0]
</pre></div>

<div class="example">
<pre class="example">isprime ([i, 2, 3, 5])
    &rArr; [0, 0, 1, 0]
</pre></div>

<p>Programming Note: <code>isprime</code> is appropriate if the maximum value in
<var>x</var> is not too large (&lt; 1e15).  For larger values special purpose
factorization code should be used.
</p>
<p>Compatibility Note: <var>matlab</var> does not extend the definition of prime
numbers and will produce an error if given negative or complex inputs.
</p>
<p><strong>See also:</strong> <a href="Utility-Functions.html#XREFprimes">primes</a>, <a href="Utility-Functions.html#XREFfactor">factor</a>, <a href="Utility-Functions.html#XREFgcd">gcd</a>, <a href="Utility-Functions.html#XREFlcm">lcm</a>.
</p></dd></dl>


<p>If instead of knowing properties of variables, you wish to know which
variables are defined and to gather other information about the
workspace itself, see <a href="Status-of-Variables.html#Status-of-Variables">Status of Variables</a>.
</p>

<hr>
<div class="header">
<p>
Previous: <a href="Promotion-and-Demotion-of-Data-Types.html#Promotion-and-Demotion-of-Data-Types" accesskey="p" rel="prev">Promotion and Demotion of Data Types</a>, Up: <a href="Numeric-Data-Types.html#Numeric-Data-Types" accesskey="u" rel="up">Numeric Data Types</a> &nbsp; [<a href="index.html#SEC_Contents" title="Table of contents" rel="contents">Contents</a>][<a href="Concept-Index.html#Concept-Index" title="Index" rel="index">Index</a>]</p>
</div>



</body>
</html>