Mercurial > hg > octave-nkf
annotate scripts/specfun/isprime.m @ 20828:a3b9ee5c040a
Replace bsxfun with broadcasting for performance with complex inputs (bug #38628).
cumtrapz.m, quadgk.m, trapz.m, center.m, zscore.m: Replace bsxfun with
broadcasting for performance where inputs might be complex.
author | Rik <rik@octave.org> |
---|---|
date | Mon, 12 Oct 2015 21:28:32 -0700 |
parents | 2645f9ef8c88 |
children |
rev | line source |
---|---|
19898
4197fc428c7d
maint: Update copyright notices for 2015.
John W. Eaton <jwe@octave.org>
parents:
19794
diff
changeset
|
1 ## Copyright (C) 2000-2015 Paul Kienzle |
10664
faff5367cc05
second isprime rewrite
Jaroslav Hajek <highegg@gmail.com>
parents:
10657
diff
changeset
|
2 ## Copyright (C) 2010 VZLU Prague |
5827 | 3 ## |
4 ## This file is part of Octave. | |
5 ## | |
6 ## Octave is free software; you can redistribute it and/or modify it | |
7 ## under the terms of the GNU General Public License as published by | |
7016 | 8 ## the Free Software Foundation; either version 3 of the License, or (at |
9 ## your option) any later version. | |
5827 | 10 ## |
11 ## Octave is distributed in the hope that it will be useful, but | |
12 ## WITHOUT ANY WARRANTY; without even the implied warranty of | |
13 ## MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU | |
14 ## General Public License for more details. | |
15 ## | |
16 ## You should have received a copy of the GNU General Public License | |
7016 | 17 ## along with Octave; see the file COPYING. If not, see |
18 ## <http://www.gnu.org/licenses/>. | |
5827 | 19 |
20 ## -*- texinfo -*- | |
11431
0d9640d755b1
Improve docstrings for all isXXX functions.
Rik <octave@nomad.inbox5.com>
parents:
10664
diff
changeset
|
21 ## @deftypefn {Function File} {} isprime (@var{x}) |
20372
2645f9ef8c88
doc: Update more docstrings to have one sentence summary as first line.
Rik <rik@octave.org>
parents:
20038
diff
changeset
|
22 ## Return a logical array which is true where the elements of @var{x} are prime |
2645f9ef8c88
doc: Update more docstrings to have one sentence summary as first line.
Rik <rik@octave.org>
parents:
20038
diff
changeset
|
23 ## numbers and false where they are not. |
11431
0d9640d755b1
Improve docstrings for all isXXX functions.
Rik <octave@nomad.inbox5.com>
parents:
10664
diff
changeset
|
24 ## |
19244
bb0c5e182c12
isprime.m: Update docstring to note that 1 is not prime.
Rik <rik@octave.org>
parents:
19242
diff
changeset
|
25 ## A prime number is conventionally defined as a positive integer greater than |
bb0c5e182c12
isprime.m: Update docstring to note that 1 is not prime.
Rik <rik@octave.org>
parents:
19242
diff
changeset
|
26 ## 1 (e.g., 2, 3, @dots{}) which is divisible only by itself and 1. Octave |
bb0c5e182c12
isprime.m: Update docstring to note that 1 is not prime.
Rik <rik@octave.org>
parents:
19242
diff
changeset
|
27 ## extends this definition to include both negative integers and complex |
bb0c5e182c12
isprime.m: Update docstring to note that 1 is not prime.
Rik <rik@octave.org>
parents:
19242
diff
changeset
|
28 ## values. A negative integer is prime if its positive counterpart is prime. |
bb0c5e182c12
isprime.m: Update docstring to note that 1 is not prime.
Rik <rik@octave.org>
parents:
19242
diff
changeset
|
29 ## This is equivalent to @code{isprime (abs (x))}. |
19794
db92e7e28e1f
strip trailing whitespace from most source files
John W. Eaton <jwe@octave.org>
parents:
19244
diff
changeset
|
30 ## |
19242
2f117c4b5cb0
isprime.m: Document negative and complex inputs.
Rik <rik@octave.org>
parents:
19238
diff
changeset
|
31 ## If @code{class (@var{x})} is complex, then primality is tested in the domain |
2f117c4b5cb0
isprime.m: Document negative and complex inputs.
Rik <rik@octave.org>
parents:
19238
diff
changeset
|
32 ## of Gaussian integers (@url{http://en.wikipedia.org/wiki/Gaussian_integer}). |
2f117c4b5cb0
isprime.m: Document negative and complex inputs.
Rik <rik@octave.org>
parents:
19238
diff
changeset
|
33 ## Some non-complex integers are prime in the ordinary sense, but not in the |
2f117c4b5cb0
isprime.m: Document negative and complex inputs.
Rik <rik@octave.org>
parents:
19238
diff
changeset
|
34 ## domain of Gaussian integers. For example, @math{5 = (1+2i)*(1-2i)} shows |
2f117c4b5cb0
isprime.m: Document negative and complex inputs.
Rik <rik@octave.org>
parents:
19238
diff
changeset
|
35 ## that 5 is not prime because it has a factor other than itself and 1. |
2f117c4b5cb0
isprime.m: Document negative and complex inputs.
Rik <rik@octave.org>
parents:
19238
diff
changeset
|
36 ## Exercise caution when testing complex and real values together in the same |
2f117c4b5cb0
isprime.m: Document negative and complex inputs.
Rik <rik@octave.org>
parents:
19238
diff
changeset
|
37 ## matrix. |
2f117c4b5cb0
isprime.m: Document negative and complex inputs.
Rik <rik@octave.org>
parents:
19238
diff
changeset
|
38 ## |
2f117c4b5cb0
isprime.m: Document negative and complex inputs.
Rik <rik@octave.org>
parents:
19238
diff
changeset
|
39 ## Examples: |
5827 | 40 ## |
11431
0d9640d755b1
Improve docstrings for all isXXX functions.
Rik <octave@nomad.inbox5.com>
parents:
10664
diff
changeset
|
41 ## @example |
0d9640d755b1
Improve docstrings for all isXXX functions.
Rik <octave@nomad.inbox5.com>
parents:
10664
diff
changeset
|
42 ## @group |
0d9640d755b1
Improve docstrings for all isXXX functions.
Rik <octave@nomad.inbox5.com>
parents:
10664
diff
changeset
|
43 ## isprime (1:6) |
0d9640d755b1
Improve docstrings for all isXXX functions.
Rik <octave@nomad.inbox5.com>
parents:
10664
diff
changeset
|
44 ## @result{} [0, 1, 1, 0, 1, 0] |
0d9640d755b1
Improve docstrings for all isXXX functions.
Rik <octave@nomad.inbox5.com>
parents:
10664
diff
changeset
|
45 ## @end group |
0d9640d755b1
Improve docstrings for all isXXX functions.
Rik <octave@nomad.inbox5.com>
parents:
10664
diff
changeset
|
46 ## @end example |
19238
89e275a4f6f6
Allow isprime to handle complex numbers (bug #43041)
Jordi Gutiérrez Hermoso <jordigh@octave.org>
parents:
19237
diff
changeset
|
47 ## |
89e275a4f6f6
Allow isprime to handle complex numbers (bug #43041)
Jordi Gutiérrez Hermoso <jordigh@octave.org>
parents:
19237
diff
changeset
|
48 ## @example |
89e275a4f6f6
Allow isprime to handle complex numbers (bug #43041)
Jordi Gutiérrez Hermoso <jordigh@octave.org>
parents:
19237
diff
changeset
|
49 ## @group |
89e275a4f6f6
Allow isprime to handle complex numbers (bug #43041)
Jordi Gutiérrez Hermoso <jordigh@octave.org>
parents:
19237
diff
changeset
|
50 ## isprime ([i, 2, 3, 5]) |
89e275a4f6f6
Allow isprime to handle complex numbers (bug #43041)
Jordi Gutiérrez Hermoso <jordigh@octave.org>
parents:
19237
diff
changeset
|
51 ## @result{} [0, 0, 1, 0] |
89e275a4f6f6
Allow isprime to handle complex numbers (bug #43041)
Jordi Gutiérrez Hermoso <jordigh@octave.org>
parents:
19237
diff
changeset
|
52 ## @end group |
89e275a4f6f6
Allow isprime to handle complex numbers (bug #43041)
Jordi Gutiérrez Hermoso <jordigh@octave.org>
parents:
19237
diff
changeset
|
53 ## @end example |
19242
2f117c4b5cb0
isprime.m: Document negative and complex inputs.
Rik <rik@octave.org>
parents:
19238
diff
changeset
|
54 ## |
2f117c4b5cb0
isprime.m: Document negative and complex inputs.
Rik <rik@octave.org>
parents:
19238
diff
changeset
|
55 ## Programming Note: @code{isprime} is appropriate if the maximum value in |
2f117c4b5cb0
isprime.m: Document negative and complex inputs.
Rik <rik@octave.org>
parents:
19238
diff
changeset
|
56 ## @var{x} is not too large (< 1e15). For larger values special purpose |
2f117c4b5cb0
isprime.m: Document negative and complex inputs.
Rik <rik@octave.org>
parents:
19238
diff
changeset
|
57 ## factorization code should be used. |
2f117c4b5cb0
isprime.m: Document negative and complex inputs.
Rik <rik@octave.org>
parents:
19238
diff
changeset
|
58 ## |
2f117c4b5cb0
isprime.m: Document negative and complex inputs.
Rik <rik@octave.org>
parents:
19238
diff
changeset
|
59 ## Compatibility Note: @var{matlab} does not extend the definition of prime |
2f117c4b5cb0
isprime.m: Document negative and complex inputs.
Rik <rik@octave.org>
parents:
19238
diff
changeset
|
60 ## numbers and will produce an error if given negative or complex inputs. |
5827 | 61 ## @seealso{primes, factor, gcd, lcm} |
62 ## @end deftypefn | |
63 | |
11469
c776f063fefe
Overhaul m-script files to use common variable name between code and documentation.
Rik <octave@nomad.inbox5.com>
parents:
11431
diff
changeset
|
64 function t = isprime (x) |
7125 | 65 |
19233
0976f9fccbbd
isprime.m: Verify that input is a real number (bug #43041)
Rik <rik@octave.org>
parents:
17744
diff
changeset
|
66 if (nargin != 1) |
0976f9fccbbd
isprime.m: Verify that input is a real number (bug #43041)
Rik <rik@octave.org>
parents:
17744
diff
changeset
|
67 print_usage (); |
19238
89e275a4f6f6
Allow isprime to handle complex numbers (bug #43041)
Jordi Gutiérrez Hermoso <jordigh@octave.org>
parents:
19237
diff
changeset
|
68 elseif (any (fix (x) != x)) |
89e275a4f6f6
Allow isprime to handle complex numbers (bug #43041)
Jordi Gutiérrez Hermoso <jordigh@octave.org>
parents:
19237
diff
changeset
|
69 error ("isprime: X contains non-integer entries"); |
19233
0976f9fccbbd
isprime.m: Verify that input is a real number (bug #43041)
Rik <rik@octave.org>
parents:
17744
diff
changeset
|
70 endif |
0976f9fccbbd
isprime.m: Verify that input is a real number (bug #43041)
Rik <rik@octave.org>
parents:
17744
diff
changeset
|
71 |
19237
920a400929ca
* isprime.m: Return an empty array for empty input
Jordi Gutiérrez Hermoso <jordigh@octave.org>
parents:
19233
diff
changeset
|
72 if (isempty (x)) |
920a400929ca
* isprime.m: Return an empty array for empty input
Jordi Gutiérrez Hermoso <jordigh@octave.org>
parents:
19233
diff
changeset
|
73 t = x; |
920a400929ca
* isprime.m: Return an empty array for empty input
Jordi Gutiérrez Hermoso <jordigh@octave.org>
parents:
19233
diff
changeset
|
74 return; |
920a400929ca
* isprime.m: Return an empty array for empty input
Jordi Gutiérrez Hermoso <jordigh@octave.org>
parents:
19233
diff
changeset
|
75 endif |
920a400929ca
* isprime.m: Return an empty array for empty input
Jordi Gutiérrez Hermoso <jordigh@octave.org>
parents:
19233
diff
changeset
|
76 |
19238
89e275a4f6f6
Allow isprime to handle complex numbers (bug #43041)
Jordi Gutiérrez Hermoso <jordigh@octave.org>
parents:
19237
diff
changeset
|
77 if (iscomplex (x)) |
89e275a4f6f6
Allow isprime to handle complex numbers (bug #43041)
Jordi Gutiérrez Hermoso <jordigh@octave.org>
parents:
19237
diff
changeset
|
78 t = isgaussianprime (x); |
89e275a4f6f6
Allow isprime to handle complex numbers (bug #43041)
Jordi Gutiérrez Hermoso <jordigh@octave.org>
parents:
19237
diff
changeset
|
79 return; |
89e275a4f6f6
Allow isprime to handle complex numbers (bug #43041)
Jordi Gutiérrez Hermoso <jordigh@octave.org>
parents:
19237
diff
changeset
|
80 endif |
89e275a4f6f6
Allow isprime to handle complex numbers (bug #43041)
Jordi Gutiérrez Hermoso <jordigh@octave.org>
parents:
19237
diff
changeset
|
81 |
19242
2f117c4b5cb0
isprime.m: Document negative and complex inputs.
Rik <rik@octave.org>
parents:
19238
diff
changeset
|
82 ## Code strategy is to build a table with the list of possible primes |
2f117c4b5cb0
isprime.m: Document negative and complex inputs.
Rik <rik@octave.org>
parents:
19238
diff
changeset
|
83 ## and then quickly compare entries in x with the table of primes using |
2f117c4b5cb0
isprime.m: Document negative and complex inputs.
Rik <rik@octave.org>
parents:
19238
diff
changeset
|
84 ## lookup(). The table size is limited to save memory and computation |
2f117c4b5cb0
isprime.m: Document negative and complex inputs.
Rik <rik@octave.org>
parents:
19238
diff
changeset
|
85 ## time during its creation. All entries larger than the maximum in the |
2f117c4b5cb0
isprime.m: Document negative and complex inputs.
Rik <rik@octave.org>
parents:
19238
diff
changeset
|
86 ## table are checked by straightforward division. |
19238
89e275a4f6f6
Allow isprime to handle complex numbers (bug #43041)
Jordi Gutiérrez Hermoso <jordigh@octave.org>
parents:
19237
diff
changeset
|
87 |
19242
2f117c4b5cb0
isprime.m: Document negative and complex inputs.
Rik <rik@octave.org>
parents:
19238
diff
changeset
|
88 x = abs (x); # handle negative entries |
19233
0976f9fccbbd
isprime.m: Verify that input is a real number (bug #43041)
Rik <rik@octave.org>
parents:
17744
diff
changeset
|
89 maxn = max (x(:)); |
0976f9fccbbd
isprime.m: Verify that input is a real number (bug #43041)
Rik <rik@octave.org>
parents:
17744
diff
changeset
|
90 ## generate prime table of suitable length. |
19242
2f117c4b5cb0
isprime.m: Document negative and complex inputs.
Rik <rik@octave.org>
parents:
19238
diff
changeset
|
91 ## 1e7 threshold requires ~0.15 seconds of computation, 1e8 requires 1.8. |
2f117c4b5cb0
isprime.m: Document negative and complex inputs.
Rik <rik@octave.org>
parents:
19238
diff
changeset
|
92 maxp = min (maxn, max (sqrt (maxn), 1e7)); |
19233
0976f9fccbbd
isprime.m: Verify that input is a real number (bug #43041)
Rik <rik@octave.org>
parents:
17744
diff
changeset
|
93 pr = primes (maxp); |
19242
2f117c4b5cb0
isprime.m: Document negative and complex inputs.
Rik <rik@octave.org>
parents:
19238
diff
changeset
|
94 t = lookup (pr, x, "b"); # quick search for table matches. |
2f117c4b5cb0
isprime.m: Document negative and complex inputs.
Rik <rik@octave.org>
parents:
19238
diff
changeset
|
95 |
2f117c4b5cb0
isprime.m: Document negative and complex inputs.
Rik <rik@octave.org>
parents:
19238
diff
changeset
|
96 ## process any remaining large entries |
19233
0976f9fccbbd
isprime.m: Verify that input is a real number (bug #43041)
Rik <rik@octave.org>
parents:
17744
diff
changeset
|
97 m = x(x > maxp); |
0976f9fccbbd
isprime.m: Verify that input is a real number (bug #43041)
Rik <rik@octave.org>
parents:
17744
diff
changeset
|
98 if (! isempty (m)) |
0976f9fccbbd
isprime.m: Verify that input is a real number (bug #43041)
Rik <rik@octave.org>
parents:
17744
diff
changeset
|
99 if (maxn <= intmax ("uint32")) |
0976f9fccbbd
isprime.m: Verify that input is a real number (bug #43041)
Rik <rik@octave.org>
parents:
17744
diff
changeset
|
100 m = uint32 (m); |
0976f9fccbbd
isprime.m: Verify that input is a real number (bug #43041)
Rik <rik@octave.org>
parents:
17744
diff
changeset
|
101 elseif (maxn <= intmax ("uint64")) |
0976f9fccbbd
isprime.m: Verify that input is a real number (bug #43041)
Rik <rik@octave.org>
parents:
17744
diff
changeset
|
102 m = uint64 (m); |
0976f9fccbbd
isprime.m: Verify that input is a real number (bug #43041)
Rik <rik@octave.org>
parents:
17744
diff
changeset
|
103 else |
0976f9fccbbd
isprime.m: Verify that input is a real number (bug #43041)
Rik <rik@octave.org>
parents:
17744
diff
changeset
|
104 warning ("isprime: X contains integers too large to be tested"); |
10664
faff5367cc05
second isprime rewrite
Jaroslav Hajek <highegg@gmail.com>
parents:
10657
diff
changeset
|
105 endif |
19242
2f117c4b5cb0
isprime.m: Document negative and complex inputs.
Rik <rik@octave.org>
parents:
19238
diff
changeset
|
106 |
2f117c4b5cb0
isprime.m: Document negative and complex inputs.
Rik <rik@octave.org>
parents:
19238
diff
changeset
|
107 ## Start by dividing through by the small primes until the remaining |
2f117c4b5cb0
isprime.m: Document negative and complex inputs.
Rik <rik@octave.org>
parents:
19238
diff
changeset
|
108 ## list of entries is small (and most likely prime themselves). |
19233
0976f9fccbbd
isprime.m: Verify that input is a real number (bug #43041)
Rik <rik@octave.org>
parents:
17744
diff
changeset
|
109 pr = cast (pr(pr <= sqrt (maxn)), class (m)); |
0976f9fccbbd
isprime.m: Verify that input is a real number (bug #43041)
Rik <rik@octave.org>
parents:
17744
diff
changeset
|
110 for p = pr |
0976f9fccbbd
isprime.m: Verify that input is a real number (bug #43041)
Rik <rik@octave.org>
parents:
17744
diff
changeset
|
111 m = m(rem (m, p) != 0); |
19242
2f117c4b5cb0
isprime.m: Document negative and complex inputs.
Rik <rik@octave.org>
parents:
19238
diff
changeset
|
112 if (numel (m) < numel (pr) / 10) |
19233
0976f9fccbbd
isprime.m: Verify that input is a real number (bug #43041)
Rik <rik@octave.org>
parents:
17744
diff
changeset
|
113 break; |
0976f9fccbbd
isprime.m: Verify that input is a real number (bug #43041)
Rik <rik@octave.org>
parents:
17744
diff
changeset
|
114 endif |
0976f9fccbbd
isprime.m: Verify that input is a real number (bug #43041)
Rik <rik@octave.org>
parents:
17744
diff
changeset
|
115 endfor |
19242
2f117c4b5cb0
isprime.m: Document negative and complex inputs.
Rik <rik@octave.org>
parents:
19238
diff
changeset
|
116 |
2f117c4b5cb0
isprime.m: Document negative and complex inputs.
Rik <rik@octave.org>
parents:
19238
diff
changeset
|
117 ## Check the remaining list of possible primes against the |
2f117c4b5cb0
isprime.m: Document negative and complex inputs.
Rik <rik@octave.org>
parents:
19238
diff
changeset
|
118 ## remaining prime factors which were not tested in the for loop. |
2f117c4b5cb0
isprime.m: Document negative and complex inputs.
Rik <rik@octave.org>
parents:
19238
diff
changeset
|
119 ## This is just an optimization to use arrayfun over for loo |
19233
0976f9fccbbd
isprime.m: Verify that input is a real number (bug #43041)
Rik <rik@octave.org>
parents:
17744
diff
changeset
|
120 pr = pr(pr > p); |
0976f9fccbbd
isprime.m: Verify that input is a real number (bug #43041)
Rik <rik@octave.org>
parents:
17744
diff
changeset
|
121 mm = arrayfun (@(x) all (rem (x, pr)), m); |
0976f9fccbbd
isprime.m: Verify that input is a real number (bug #43041)
Rik <rik@octave.org>
parents:
17744
diff
changeset
|
122 m = m(mm); |
19242
2f117c4b5cb0
isprime.m: Document negative and complex inputs.
Rik <rik@octave.org>
parents:
19238
diff
changeset
|
123 |
2f117c4b5cb0
isprime.m: Document negative and complex inputs.
Rik <rik@octave.org>
parents:
19238
diff
changeset
|
124 ## Add any remaining entries, which are truly prime, to the results. |
10664
faff5367cc05
second isprime rewrite
Jaroslav Hajek <highegg@gmail.com>
parents:
10657
diff
changeset
|
125 if (! isempty (m)) |
19233
0976f9fccbbd
isprime.m: Verify that input is a real number (bug #43041)
Rik <rik@octave.org>
parents:
17744
diff
changeset
|
126 m = cast (sort (m), class (x)); |
0976f9fccbbd
isprime.m: Verify that input is a real number (bug #43041)
Rik <rik@octave.org>
parents:
17744
diff
changeset
|
127 t |= lookup (m, x, "b"); |
10664
faff5367cc05
second isprime rewrite
Jaroslav Hajek <highegg@gmail.com>
parents:
10657
diff
changeset
|
128 endif |
7125 | 129 endif |
130 | |
5827 | 131 endfunction |
9984
d1cc2e0ddf55
isprime: produce logical result
John W. Eaton <jwe@octave.org>
parents:
7125
diff
changeset
|
132 |
19238
89e275a4f6f6
Allow isprime to handle complex numbers (bug #43041)
Jordi Gutiérrez Hermoso <jordigh@octave.org>
parents:
19237
diff
changeset
|
133 function t = isgaussianprime (z) |
19242
2f117c4b5cb0
isprime.m: Document negative and complex inputs.
Rik <rik@octave.org>
parents:
19238
diff
changeset
|
134 ## Assume prime unless proven otherwise |
19238
89e275a4f6f6
Allow isprime to handle complex numbers (bug #43041)
Jordi Gutiérrez Hermoso <jordigh@octave.org>
parents:
19237
diff
changeset
|
135 t = true (size (z)); |
89e275a4f6f6
Allow isprime to handle complex numbers (bug #43041)
Jordi Gutiérrez Hermoso <jordigh@octave.org>
parents:
19237
diff
changeset
|
136 |
89e275a4f6f6
Allow isprime to handle complex numbers (bug #43041)
Jordi Gutiérrez Hermoso <jordigh@octave.org>
parents:
19237
diff
changeset
|
137 x = real (z); |
89e275a4f6f6
Allow isprime to handle complex numbers (bug #43041)
Jordi Gutiérrez Hermoso <jordigh@octave.org>
parents:
19237
diff
changeset
|
138 y = imag (z); |
89e275a4f6f6
Allow isprime to handle complex numbers (bug #43041)
Jordi Gutiérrez Hermoso <jordigh@octave.org>
parents:
19237
diff
changeset
|
139 |
89e275a4f6f6
Allow isprime to handle complex numbers (bug #43041)
Jordi Gutiérrez Hermoso <jordigh@octave.org>
parents:
19237
diff
changeset
|
140 ## If purely real or purely imaginary, ordinary prime test for |
89e275a4f6f6
Allow isprime to handle complex numbers (bug #43041)
Jordi Gutiérrez Hermoso <jordigh@octave.org>
parents:
19237
diff
changeset
|
141 ## that complex part if that part is 3 mod 4. |
89e275a4f6f6
Allow isprime to handle complex numbers (bug #43041)
Jordi Gutiérrez Hermoso <jordigh@octave.org>
parents:
19237
diff
changeset
|
142 xidx = y==0 & mod (x, 4) == 3; |
89e275a4f6f6
Allow isprime to handle complex numbers (bug #43041)
Jordi Gutiérrez Hermoso <jordigh@octave.org>
parents:
19237
diff
changeset
|
143 yidx = x==0 & mod (y, 4) == 3; |
89e275a4f6f6
Allow isprime to handle complex numbers (bug #43041)
Jordi Gutiérrez Hermoso <jordigh@octave.org>
parents:
19237
diff
changeset
|
144 |
89e275a4f6f6
Allow isprime to handle complex numbers (bug #43041)
Jordi Gutiérrez Hermoso <jordigh@octave.org>
parents:
19237
diff
changeset
|
145 t(xidx) &= isprime (x(xidx)); |
89e275a4f6f6
Allow isprime to handle complex numbers (bug #43041)
Jordi Gutiérrez Hermoso <jordigh@octave.org>
parents:
19237
diff
changeset
|
146 t(yidx) &= isprime (y(yidx)); |
89e275a4f6f6
Allow isprime to handle complex numbers (bug #43041)
Jordi Gutiérrez Hermoso <jordigh@octave.org>
parents:
19237
diff
changeset
|
147 |
19242
2f117c4b5cb0
isprime.m: Document negative and complex inputs.
Rik <rik@octave.org>
parents:
19238
diff
changeset
|
148 ## Otherwise, prime if x^2 + y^2 is prime |
2f117c4b5cb0
isprime.m: Document negative and complex inputs.
Rik <rik@octave.org>
parents:
19238
diff
changeset
|
149 zidx = ! (xidx | yidx); # Skip entries that were already evaluated |
2f117c4b5cb0
isprime.m: Document negative and complex inputs.
Rik <rik@octave.org>
parents:
19238
diff
changeset
|
150 zabs = x(zidx).^2 + y(zidx).^2; |
2f117c4b5cb0
isprime.m: Document negative and complex inputs.
Rik <rik@octave.org>
parents:
19238
diff
changeset
|
151 t(zidx) &= isprime (zabs); |
19238
89e275a4f6f6
Allow isprime to handle complex numbers (bug #43041)
Jordi Gutiérrez Hermoso <jordigh@octave.org>
parents:
19237
diff
changeset
|
152 endfunction |
89e275a4f6f6
Allow isprime to handle complex numbers (bug #43041)
Jordi Gutiérrez Hermoso <jordigh@octave.org>
parents:
19237
diff
changeset
|
153 |
89e275a4f6f6
Allow isprime to handle complex numbers (bug #43041)
Jordi Gutiérrez Hermoso <jordigh@octave.org>
parents:
19237
diff
changeset
|
154 |
14363
f3d52523cde1
Use Octave coding conventions in all m-file %!test blocks
Rik <octave@nomad.inbox5.com>
parents:
14138
diff
changeset
|
155 %!assert (isprime (3), true) |
f3d52523cde1
Use Octave coding conventions in all m-file %!test blocks
Rik <octave@nomad.inbox5.com>
parents:
14138
diff
changeset
|
156 %!assert (isprime (4), false) |
19238
89e275a4f6f6
Allow isprime to handle complex numbers (bug #43041)
Jordi Gutiérrez Hermoso <jordigh@octave.org>
parents:
19237
diff
changeset
|
157 %!assert (isprime (5i), false) |
89e275a4f6f6
Allow isprime to handle complex numbers (bug #43041)
Jordi Gutiérrez Hermoso <jordigh@octave.org>
parents:
19237
diff
changeset
|
158 %!assert (isprime (7i), true) |
89e275a4f6f6
Allow isprime to handle complex numbers (bug #43041)
Jordi Gutiérrez Hermoso <jordigh@octave.org>
parents:
19237
diff
changeset
|
159 %!assert (isprime ([1+2i, (2+3i)*(-1+2i)]), [true, false]) |
89e275a4f6f6
Allow isprime to handle complex numbers (bug #43041)
Jordi Gutiérrez Hermoso <jordigh@octave.org>
parents:
19237
diff
changeset
|
160 %!assert (isprime (-2), true) |
89e275a4f6f6
Allow isprime to handle complex numbers (bug #43041)
Jordi Gutiérrez Hermoso <jordigh@octave.org>
parents:
19237
diff
changeset
|
161 %!assert (isprime (complex (-2)), false) |
89e275a4f6f6
Allow isprime to handle complex numbers (bug #43041)
Jordi Gutiérrez Hermoso <jordigh@octave.org>
parents:
19237
diff
changeset
|
162 %!assert (isprime (2i), false) |
89e275a4f6f6
Allow isprime to handle complex numbers (bug #43041)
Jordi Gutiérrez Hermoso <jordigh@octave.org>
parents:
19237
diff
changeset
|
163 %!assert (isprime ([i, 2, 3, 5]), [false, false, true, false]) |
89e275a4f6f6
Allow isprime to handle complex numbers (bug #43041)
Jordi Gutiérrez Hermoso <jordigh@octave.org>
parents:
19237
diff
changeset
|
164 %!assert (isprime (0), false) |
14363
f3d52523cde1
Use Octave coding conventions in all m-file %!test blocks
Rik <octave@nomad.inbox5.com>
parents:
14138
diff
changeset
|
165 %!assert (isprime (magic (3)), logical ([0, 0, 0; 1, 1, 1; 0, 0, 1])) |
f3d52523cde1
Use Octave coding conventions in all m-file %!test blocks
Rik <octave@nomad.inbox5.com>
parents:
14138
diff
changeset
|
166 |
20038
9fc020886ae9
maint: Clean up m-files to follow Octave coding conventions.
Rik <rik@octave.org>
parents:
19898
diff
changeset
|
167 ## Test input validation |
9984
d1cc2e0ddf55
isprime: produce logical result
John W. Eaton <jwe@octave.org>
parents:
7125
diff
changeset
|
168 %!error isprime () |
d1cc2e0ddf55
isprime: produce logical result
John W. Eaton <jwe@octave.org>
parents:
7125
diff
changeset
|
169 %!error isprime (1, 2) |
19238
89e275a4f6f6
Allow isprime to handle complex numbers (bug #43041)
Jordi Gutiérrez Hermoso <jordigh@octave.org>
parents:
19237
diff
changeset
|
170 %!error <X contains non-integer entries> isprime (0.5i) |
89e275a4f6f6
Allow isprime to handle complex numbers (bug #43041)
Jordi Gutiérrez Hermoso <jordigh@octave.org>
parents:
19237
diff
changeset
|
171 %!error <X contains non-integer entries> isprime (0.5) |
19242
2f117c4b5cb0
isprime.m: Document negative and complex inputs.
Rik <rik@octave.org>
parents:
19238
diff
changeset
|
172 |