Mercurial > octave
annotate libinterp/corefcn/gcd.cc @ 33617:ec2635a02328 bytecode-interpreter tip
maint: Merge default to bytecode-interpreter.
author | Markus Mützel <markus.muetzel@gmx.de> |
---|---|
date | Tue, 21 May 2024 18:29:03 +0200 |
parents | f53ac65ffba6 |
children |
rev | line source |
---|---|
27923
bd51beb6205e
update formatting of copyright notices
John W. Eaton <jwe@octave.org>
parents:
27919
diff
changeset
|
1 //////////////////////////////////////////////////////////////////////// |
bd51beb6205e
update formatting of copyright notices
John W. Eaton <jwe@octave.org>
parents:
27919
diff
changeset
|
2 // |
32632
2e484f9f1f18
maint: update Octave Project Developers copyright for the new year
John W. Eaton <jwe@octave.org>
parents:
32271
diff
changeset
|
3 // Copyright (C) 2004-2024 The Octave Project Developers |
27923
bd51beb6205e
update formatting of copyright notices
John W. Eaton <jwe@octave.org>
parents:
27919
diff
changeset
|
4 // |
bd51beb6205e
update formatting of copyright notices
John W. Eaton <jwe@octave.org>
parents:
27919
diff
changeset
|
5 // See the file COPYRIGHT.md in the top-level directory of this |
bd51beb6205e
update formatting of copyright notices
John W. Eaton <jwe@octave.org>
parents:
27919
diff
changeset
|
6 // distribution or <https://octave.org/copyright/>. |
bd51beb6205e
update formatting of copyright notices
John W. Eaton <jwe@octave.org>
parents:
27919
diff
changeset
|
7 // |
bd51beb6205e
update formatting of copyright notices
John W. Eaton <jwe@octave.org>
parents:
27919
diff
changeset
|
8 // This file is part of Octave. |
bd51beb6205e
update formatting of copyright notices
John W. Eaton <jwe@octave.org>
parents:
27919
diff
changeset
|
9 // |
bd51beb6205e
update formatting of copyright notices
John W. Eaton <jwe@octave.org>
parents:
27919
diff
changeset
|
10 // Octave is free software: you can redistribute it and/or modify it |
bd51beb6205e
update formatting of copyright notices
John W. Eaton <jwe@octave.org>
parents:
27919
diff
changeset
|
11 // under the terms of the GNU General Public License as published by |
bd51beb6205e
update formatting of copyright notices
John W. Eaton <jwe@octave.org>
parents:
27919
diff
changeset
|
12 // the Free Software Foundation, either version 3 of the License, or |
bd51beb6205e
update formatting of copyright notices
John W. Eaton <jwe@octave.org>
parents:
27919
diff
changeset
|
13 // (at your option) any later version. |
bd51beb6205e
update formatting of copyright notices
John W. Eaton <jwe@octave.org>
parents:
27919
diff
changeset
|
14 // |
bd51beb6205e
update formatting of copyright notices
John W. Eaton <jwe@octave.org>
parents:
27919
diff
changeset
|
15 // Octave is distributed in the hope that it will be useful, but |
bd51beb6205e
update formatting of copyright notices
John W. Eaton <jwe@octave.org>
parents:
27919
diff
changeset
|
16 // WITHOUT ANY WARRANTY; without even the implied warranty of |
bd51beb6205e
update formatting of copyright notices
John W. Eaton <jwe@octave.org>
parents:
27919
diff
changeset
|
17 // MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the |
bd51beb6205e
update formatting of copyright notices
John W. Eaton <jwe@octave.org>
parents:
27919
diff
changeset
|
18 // GNU General Public License for more details. |
bd51beb6205e
update formatting of copyright notices
John W. Eaton <jwe@octave.org>
parents:
27919
diff
changeset
|
19 // |
bd51beb6205e
update formatting of copyright notices
John W. Eaton <jwe@octave.org>
parents:
27919
diff
changeset
|
20 // You should have received a copy of the GNU General Public License |
bd51beb6205e
update formatting of copyright notices
John W. Eaton <jwe@octave.org>
parents:
27919
diff
changeset
|
21 // along with Octave; see the file COPYING. If not, see |
bd51beb6205e
update formatting of copyright notices
John W. Eaton <jwe@octave.org>
parents:
27919
diff
changeset
|
22 // <https://www.gnu.org/licenses/>. |
bd51beb6205e
update formatting of copyright notices
John W. Eaton <jwe@octave.org>
parents:
27919
diff
changeset
|
23 // |
bd51beb6205e
update formatting of copyright notices
John W. Eaton <jwe@octave.org>
parents:
27919
diff
changeset
|
24 //////////////////////////////////////////////////////////////////////// |
4864 | 25 |
21724
aba2e6293dd8
use "#if ..." consistently instead of "#ifdef" and "#ifndef"
John W. Eaton <jwe@octave.org>
parents:
21581
diff
changeset
|
26 #if defined (HAVE_CONFIG_H) |
21301
40de9f8f23a6
Use '#include "config.h"' rather than <config.h>.
Rik <rik@octave.org>
parents:
21200
diff
changeset
|
27 # include "config.h" |
4864 | 28 #endif |
29 | |
30 #include "dNDArray.h" | |
31 #include "CNDArray.h" | |
7815
a41df65f3f00
Add some single precision test code and fix resulting bugs
David Bateman <dbateman@free.fr>
parents:
7017
diff
changeset
|
32 #include "fNDArray.h" |
a41df65f3f00
Add some single precision test code and fix resulting bugs
David Bateman <dbateman@free.fr>
parents:
7017
diff
changeset
|
33 #include "fCNDArray.h" |
4864 | 34 #include "lo-mappers.h" |
11021
0ee74d581c00
optimize & rewrite gcd
Jaroslav Hajek <highegg@gmail.com>
parents:
10840
diff
changeset
|
35 #include "oct-binmap.h" |
4864 | 36 |
15039
e753177cde93
maint: Move non-dynamically linked functions from DLD-FUNCTIONS/ to corefcn/ directory
Rik <rik@octave.org>
parents:
14854
diff
changeset
|
37 #include "defun.h" |
4864 | 38 #include "error.h" |
20940
48b2ad5ee801
maint: Rename oct-obj.[cc|h] to ovl.[cc|h] for clarity.
Rik <rik@octave.org>
parents:
20939
diff
changeset
|
39 #include "ovl.h" |
4864 | 40 |
31605
e88a07dec498
maint: Use macros to begin/end C++ namespaces.
Rik <rik@octave.org>
parents:
30991
diff
changeset
|
41 OCTAVE_BEGIN_NAMESPACE(octave) |
29958
32c3a5805893
move DEFUN and DEFMETHOD functions inside octave namespace
John W. Eaton <jwe@octave.org>
parents:
29931
diff
changeset
|
42 |
11062
88d78ee12ee8
Implement gcd for Gaussian (complex) integers
Jordi Gutiérrez Hermoso <jordigh@gmail.com>
parents:
11061
diff
changeset
|
43 static double |
11021
0ee74d581c00
optimize & rewrite gcd
Jaroslav Hajek <highegg@gmail.com>
parents:
10840
diff
changeset
|
44 simple_gcd (double a, double b) |
0ee74d581c00
optimize & rewrite gcd
Jaroslav Hajek <highegg@gmail.com>
parents:
10840
diff
changeset
|
45 { |
29961
7d6709900da7
eliminate octave:: namespace tags in DEFUN and DEFMETHOD and more
John W. Eaton <jwe@octave.org>
parents:
29958
diff
changeset
|
46 if (! math::isinteger (a) || ! math::isinteger (b)) |
20650
a6a452346c42
use error and error_with_id directly in libinterp code
John W. Eaton <jwe@octave.org>
parents:
20555
diff
changeset
|
47 error ("gcd: all values must be integers"); |
11021
0ee74d581c00
optimize & rewrite gcd
Jaroslav Hajek <highegg@gmail.com>
parents:
10840
diff
changeset
|
48 |
11064 | 49 double aa = fabs (a); |
50 double bb = fabs (b); | |
51 | |
11021
0ee74d581c00
optimize & rewrite gcd
Jaroslav Hajek <highegg@gmail.com>
parents:
10840
diff
changeset
|
52 while (bb != 0) |
0ee74d581c00
optimize & rewrite gcd
Jaroslav Hajek <highegg@gmail.com>
parents:
10840
diff
changeset
|
53 { |
0ee74d581c00
optimize & rewrite gcd
Jaroslav Hajek <highegg@gmail.com>
parents:
10840
diff
changeset
|
54 double tt = fmod (aa, bb); |
11064 | 55 aa = bb; |
56 bb = tt; | |
11021
0ee74d581c00
optimize & rewrite gcd
Jaroslav Hajek <highegg@gmail.com>
parents:
10840
diff
changeset
|
57 } |
0ee74d581c00
optimize & rewrite gcd
Jaroslav Hajek <highegg@gmail.com>
parents:
10840
diff
changeset
|
58 |
0ee74d581c00
optimize & rewrite gcd
Jaroslav Hajek <highegg@gmail.com>
parents:
10840
diff
changeset
|
59 return aa; |
0ee74d581c00
optimize & rewrite gcd
Jaroslav Hajek <highegg@gmail.com>
parents:
10840
diff
changeset
|
60 } |
0ee74d581c00
optimize & rewrite gcd
Jaroslav Hajek <highegg@gmail.com>
parents:
10840
diff
changeset
|
61 |
11062
88d78ee12ee8
Implement gcd for Gaussian (complex) integers
Jordi Gutiérrez Hermoso <jordigh@gmail.com>
parents:
11061
diff
changeset
|
62 template <typename FP> |
88d78ee12ee8
Implement gcd for Gaussian (complex) integers
Jordi Gutiérrez Hermoso <jordigh@gmail.com>
parents:
11061
diff
changeset
|
63 static void |
11450
5eb10763069f
substitute and use LAPACK_LIBS in mkoctfile script
John W. Eaton <jwe@octave.org>
parents:
11064
diff
changeset
|
64 divide (const std::complex<FP>& a, const std::complex<FP>& b, |
5eb10763069f
substitute and use LAPACK_LIBS in mkoctfile script
John W. Eaton <jwe@octave.org>
parents:
11064
diff
changeset
|
65 std::complex<FP>& q, std::complex<FP>& r) |
11062
88d78ee12ee8
Implement gcd for Gaussian (complex) integers
Jordi Gutiérrez Hermoso <jordigh@gmail.com>
parents:
11061
diff
changeset
|
66 { |
21942
aab79a1885cc
limit gnulib headers to liboctave/wrappers directory
John W. Eaton <jwe@octave.org>
parents:
21782
diff
changeset
|
67 FP qr = std::floor ((a/b).real () + 0.5); |
aab79a1885cc
limit gnulib headers to liboctave/wrappers directory
John W. Eaton <jwe@octave.org>
parents:
21782
diff
changeset
|
68 FP qi = std::floor ((a/b).imag () + 0.5); |
11064 | 69 |
70 q = std::complex<FP> (qr, qi); | |
71 | |
11062
88d78ee12ee8
Implement gcd for Gaussian (complex) integers
Jordi Gutiérrez Hermoso <jordigh@gmail.com>
parents:
11061
diff
changeset
|
72 r = a - q*b; |
88d78ee12ee8
Implement gcd for Gaussian (complex) integers
Jordi Gutiérrez Hermoso <jordigh@gmail.com>
parents:
11061
diff
changeset
|
73 } |
88d78ee12ee8
Implement gcd for Gaussian (complex) integers
Jordi Gutiérrez Hermoso <jordigh@gmail.com>
parents:
11061
diff
changeset
|
74 |
88d78ee12ee8
Implement gcd for Gaussian (complex) integers
Jordi Gutiérrez Hermoso <jordigh@gmail.com>
parents:
11061
diff
changeset
|
75 template <typename FP> |
88d78ee12ee8
Implement gcd for Gaussian (complex) integers
Jordi Gutiérrez Hermoso <jordigh@gmail.com>
parents:
11061
diff
changeset
|
76 static std::complex<FP> |
88d78ee12ee8
Implement gcd for Gaussian (complex) integers
Jordi Gutiérrez Hermoso <jordigh@gmail.com>
parents:
11061
diff
changeset
|
77 simple_gcd (const std::complex<FP>& a, const std::complex<FP>& b) |
88d78ee12ee8
Implement gcd for Gaussian (complex) integers
Jordi Gutiérrez Hermoso <jordigh@gmail.com>
parents:
11061
diff
changeset
|
78 { |
29961
7d6709900da7
eliminate octave:: namespace tags in DEFUN and DEFMETHOD and more
John W. Eaton <jwe@octave.org>
parents:
29958
diff
changeset
|
79 if (! math::isinteger (a.real ()) |
7d6709900da7
eliminate octave:: namespace tags in DEFUN and DEFMETHOD and more
John W. Eaton <jwe@octave.org>
parents:
29958
diff
changeset
|
80 || ! math::isinteger (a.imag ()) |
7d6709900da7
eliminate octave:: namespace tags in DEFUN and DEFMETHOD and more
John W. Eaton <jwe@octave.org>
parents:
29958
diff
changeset
|
81 || ! math::isinteger (b.real ()) |
7d6709900da7
eliminate octave:: namespace tags in DEFUN and DEFMETHOD and more
John W. Eaton <jwe@octave.org>
parents:
29958
diff
changeset
|
82 || ! math::isinteger (b.imag ())) |
20650
a6a452346c42
use error and error_with_id directly in libinterp code
John W. Eaton <jwe@octave.org>
parents:
20555
diff
changeset
|
83 error ("gcd: all complex parts must be integers"); |
11062
88d78ee12ee8
Implement gcd for Gaussian (complex) integers
Jordi Gutiérrez Hermoso <jordigh@gmail.com>
parents:
11061
diff
changeset
|
84 |
11064 | 85 std::complex<FP> aa = a; |
86 std::complex<FP> bb = b; | |
11062
88d78ee12ee8
Implement gcd for Gaussian (complex) integers
Jordi Gutiérrez Hermoso <jordigh@gmail.com>
parents:
11061
diff
changeset
|
87 |
11064 | 88 if (abs (aa) < abs (bb)) |
89 std::swap (aa, bb); | |
11062
88d78ee12ee8
Implement gcd for Gaussian (complex) integers
Jordi Gutiérrez Hermoso <jordigh@gmail.com>
parents:
11061
diff
changeset
|
90 |
11064 | 91 while (abs (bb) != 0) |
11062
88d78ee12ee8
Implement gcd for Gaussian (complex) integers
Jordi Gutiérrez Hermoso <jordigh@gmail.com>
parents:
11061
diff
changeset
|
92 { |
88d78ee12ee8
Implement gcd for Gaussian (complex) integers
Jordi Gutiérrez Hermoso <jordigh@gmail.com>
parents:
11061
diff
changeset
|
93 std::complex<FP> qq, rr; |
88d78ee12ee8
Implement gcd for Gaussian (complex) integers
Jordi Gutiérrez Hermoso <jordigh@gmail.com>
parents:
11061
diff
changeset
|
94 divide (aa, bb, qq, rr); |
88d78ee12ee8
Implement gcd for Gaussian (complex) integers
Jordi Gutiérrez Hermoso <jordigh@gmail.com>
parents:
11061
diff
changeset
|
95 aa = bb; |
88d78ee12ee8
Implement gcd for Gaussian (complex) integers
Jordi Gutiérrez Hermoso <jordigh@gmail.com>
parents:
11061
diff
changeset
|
96 bb = rr; |
88d78ee12ee8
Implement gcd for Gaussian (complex) integers
Jordi Gutiérrez Hermoso <jordigh@gmail.com>
parents:
11061
diff
changeset
|
97 } |
88d78ee12ee8
Implement gcd for Gaussian (complex) integers
Jordi Gutiérrez Hermoso <jordigh@gmail.com>
parents:
11061
diff
changeset
|
98 |
88d78ee12ee8
Implement gcd for Gaussian (complex) integers
Jordi Gutiérrez Hermoso <jordigh@gmail.com>
parents:
11061
diff
changeset
|
99 return aa; |
88d78ee12ee8
Implement gcd for Gaussian (complex) integers
Jordi Gutiérrez Hermoso <jordigh@gmail.com>
parents:
11061
diff
changeset
|
100 } |
88d78ee12ee8
Implement gcd for Gaussian (complex) integers
Jordi Gutiérrez Hermoso <jordigh@gmail.com>
parents:
11061
diff
changeset
|
101 |
21139
538b57866b90
consistently use "typename" intead of "class" in template declarations
John W. Eaton <jwe@octave.org>
parents:
21100
diff
changeset
|
102 template <typename T> |
11021
0ee74d581c00
optimize & rewrite gcd
Jaroslav Hajek <highegg@gmail.com>
parents:
10840
diff
changeset
|
103 static octave_int<T> |
0ee74d581c00
optimize & rewrite gcd
Jaroslav Hajek <highegg@gmail.com>
parents:
10840
diff
changeset
|
104 simple_gcd (const octave_int<T>& a, const octave_int<T>& b) |
0ee74d581c00
optimize & rewrite gcd
Jaroslav Hajek <highegg@gmail.com>
parents:
10840
diff
changeset
|
105 { |
11064 | 106 T aa = a.abs ().value (); |
107 T bb = b.abs ().value (); | |
108 | |
11021
0ee74d581c00
optimize & rewrite gcd
Jaroslav Hajek <highegg@gmail.com>
parents:
10840
diff
changeset
|
109 while (bb != 0) |
0ee74d581c00
optimize & rewrite gcd
Jaroslav Hajek <highegg@gmail.com>
parents:
10840
diff
changeset
|
110 { |
0ee74d581c00
optimize & rewrite gcd
Jaroslav Hajek <highegg@gmail.com>
parents:
10840
diff
changeset
|
111 T tt = aa % bb; |
11064 | 112 aa = bb; |
113 bb = tt; | |
11021
0ee74d581c00
optimize & rewrite gcd
Jaroslav Hajek <highegg@gmail.com>
parents:
10840
diff
changeset
|
114 } |
0ee74d581c00
optimize & rewrite gcd
Jaroslav Hajek <highegg@gmail.com>
parents:
10840
diff
changeset
|
115 |
0ee74d581c00
optimize & rewrite gcd
Jaroslav Hajek <highegg@gmail.com>
parents:
10840
diff
changeset
|
116 return aa; |
0ee74d581c00
optimize & rewrite gcd
Jaroslav Hajek <highegg@gmail.com>
parents:
10840
diff
changeset
|
117 } |
0ee74d581c00
optimize & rewrite gcd
Jaroslav Hajek <highegg@gmail.com>
parents:
10840
diff
changeset
|
118 |
11062
88d78ee12ee8
Implement gcd for Gaussian (complex) integers
Jordi Gutiérrez Hermoso <jordigh@gmail.com>
parents:
11061
diff
changeset
|
119 static double |
11021
0ee74d581c00
optimize & rewrite gcd
Jaroslav Hajek <highegg@gmail.com>
parents:
10840
diff
changeset
|
120 extended_gcd (double a, double b, double& x, double& y) |
0ee74d581c00
optimize & rewrite gcd
Jaroslav Hajek <highegg@gmail.com>
parents:
10840
diff
changeset
|
121 { |
29961
7d6709900da7
eliminate octave:: namespace tags in DEFUN and DEFMETHOD and more
John W. Eaton <jwe@octave.org>
parents:
29958
diff
changeset
|
122 if (! math::isinteger (a) || ! math::isinteger (b)) |
20650
a6a452346c42
use error and error_with_id directly in libinterp code
John W. Eaton <jwe@octave.org>
parents:
20555
diff
changeset
|
123 error ("gcd: all values must be integers"); |
11021
0ee74d581c00
optimize & rewrite gcd
Jaroslav Hajek <highegg@gmail.com>
parents:
10840
diff
changeset
|
124 |
11064 | 125 double aa = fabs (a); |
126 double bb = fabs (b); | |
127 | |
18100
6a71e5030df5
Follow coding convention of defining and initializing only 1 variable per line in liboctinterp.
Rik <rik@octave.org>
parents:
17787
diff
changeset
|
128 double xx, lx, yy, ly; |
6a71e5030df5
Follow coding convention of defining and initializing only 1 variable per line in liboctinterp.
Rik <rik@octave.org>
parents:
17787
diff
changeset
|
129 xx = 0, lx = 1; |
6a71e5030df5
Follow coding convention of defining and initializing only 1 variable per line in liboctinterp.
Rik <rik@octave.org>
parents:
17787
diff
changeset
|
130 yy = 1, ly = 0; |
11064 | 131 |
11021
0ee74d581c00
optimize & rewrite gcd
Jaroslav Hajek <highegg@gmail.com>
parents:
10840
diff
changeset
|
132 while (bb != 0) |
0ee74d581c00
optimize & rewrite gcd
Jaroslav Hajek <highegg@gmail.com>
parents:
10840
diff
changeset
|
133 { |
21942
aab79a1885cc
limit gnulib headers to liboctave/wrappers directory
John W. Eaton <jwe@octave.org>
parents:
21782
diff
changeset
|
134 double qq = std::floor (aa / bb); |
11021
0ee74d581c00
optimize & rewrite gcd
Jaroslav Hajek <highegg@gmail.com>
parents:
10840
diff
changeset
|
135 double tt = fmod (aa, bb); |
11064 | 136 |
137 aa = bb; | |
138 bb = tt; | |
11021
0ee74d581c00
optimize & rewrite gcd
Jaroslav Hajek <highegg@gmail.com>
parents:
10840
diff
changeset
|
139 |
0ee74d581c00
optimize & rewrite gcd
Jaroslav Hajek <highegg@gmail.com>
parents:
10840
diff
changeset
|
140 double tx = lx - qq*xx; |
11064 | 141 lx = xx; |
142 xx = tx; | |
11021
0ee74d581c00
optimize & rewrite gcd
Jaroslav Hajek <highegg@gmail.com>
parents:
10840
diff
changeset
|
143 |
0ee74d581c00
optimize & rewrite gcd
Jaroslav Hajek <highegg@gmail.com>
parents:
10840
diff
changeset
|
144 double ty = ly - qq*yy; |
11064 | 145 ly = yy; |
146 yy = ty; | |
11021
0ee74d581c00
optimize & rewrite gcd
Jaroslav Hajek <highegg@gmail.com>
parents:
10840
diff
changeset
|
147 } |
4864 | 148 |
23450
855122b993da
maint: Wrap tertiary operator in parentheses "(COND ? x : y)".
Rik <rik@octave.org>
parents:
23220
diff
changeset
|
149 x = (a >= 0 ? lx : -lx); |
855122b993da
maint: Wrap tertiary operator in parentheses "(COND ? x : y)".
Rik <rik@octave.org>
parents:
23220
diff
changeset
|
150 y = (b >= 0 ? ly : -ly); |
11021
0ee74d581c00
optimize & rewrite gcd
Jaroslav Hajek <highegg@gmail.com>
parents:
10840
diff
changeset
|
151 |
0ee74d581c00
optimize & rewrite gcd
Jaroslav Hajek <highegg@gmail.com>
parents:
10840
diff
changeset
|
152 return aa; |
0ee74d581c00
optimize & rewrite gcd
Jaroslav Hajek <highegg@gmail.com>
parents:
10840
diff
changeset
|
153 } |
0ee74d581c00
optimize & rewrite gcd
Jaroslav Hajek <highegg@gmail.com>
parents:
10840
diff
changeset
|
154 |
11062
88d78ee12ee8
Implement gcd for Gaussian (complex) integers
Jordi Gutiérrez Hermoso <jordigh@gmail.com>
parents:
11061
diff
changeset
|
155 template <typename FP> |
88d78ee12ee8
Implement gcd for Gaussian (complex) integers
Jordi Gutiérrez Hermoso <jordigh@gmail.com>
parents:
11061
diff
changeset
|
156 static std::complex<FP> |
88d78ee12ee8
Implement gcd for Gaussian (complex) integers
Jordi Gutiérrez Hermoso <jordigh@gmail.com>
parents:
11061
diff
changeset
|
157 extended_gcd (const std::complex<FP>& a, const std::complex<FP>& b, |
88d78ee12ee8
Implement gcd for Gaussian (complex) integers
Jordi Gutiérrez Hermoso <jordigh@gmail.com>
parents:
11061
diff
changeset
|
158 std::complex<FP>& x, std::complex<FP>& y) |
88d78ee12ee8
Implement gcd for Gaussian (complex) integers
Jordi Gutiérrez Hermoso <jordigh@gmail.com>
parents:
11061
diff
changeset
|
159 { |
29961
7d6709900da7
eliminate octave:: namespace tags in DEFUN and DEFMETHOD and more
John W. Eaton <jwe@octave.org>
parents:
29958
diff
changeset
|
160 if (! math::isinteger (a.real ()) |
7d6709900da7
eliminate octave:: namespace tags in DEFUN and DEFMETHOD and more
John W. Eaton <jwe@octave.org>
parents:
29958
diff
changeset
|
161 || ! math::isinteger (a.imag ()) |
7d6709900da7
eliminate octave:: namespace tags in DEFUN and DEFMETHOD and more
John W. Eaton <jwe@octave.org>
parents:
29958
diff
changeset
|
162 || ! math::isinteger (b.real ()) |
7d6709900da7
eliminate octave:: namespace tags in DEFUN and DEFMETHOD and more
John W. Eaton <jwe@octave.org>
parents:
29958
diff
changeset
|
163 || ! math::isinteger (b.imag ())) |
20650
a6a452346c42
use error and error_with_id directly in libinterp code
John W. Eaton <jwe@octave.org>
parents:
20555
diff
changeset
|
164 error ("gcd: all complex parts must be integers"); |
11062
88d78ee12ee8
Implement gcd for Gaussian (complex) integers
Jordi Gutiérrez Hermoso <jordigh@gmail.com>
parents:
11061
diff
changeset
|
165 |
18100
6a71e5030df5
Follow coding convention of defining and initializing only 1 variable per line in liboctinterp.
Rik <rik@octave.org>
parents:
17787
diff
changeset
|
166 std::complex<FP> aa = a; |
6a71e5030df5
Follow coding convention of defining and initializing only 1 variable per line in liboctinterp.
Rik <rik@octave.org>
parents:
17787
diff
changeset
|
167 std::complex<FP> bb = b; |
11062
88d78ee12ee8
Implement gcd for Gaussian (complex) integers
Jordi Gutiérrez Hermoso <jordigh@gmail.com>
parents:
11061
diff
changeset
|
168 bool swapped = false; |
11064 | 169 if (abs (aa) < abs (bb)) |
11062
88d78ee12ee8
Implement gcd for Gaussian (complex) integers
Jordi Gutiérrez Hermoso <jordigh@gmail.com>
parents:
11061
diff
changeset
|
170 { |
11064 | 171 std::swap (aa, bb); |
11062
88d78ee12ee8
Implement gcd for Gaussian (complex) integers
Jordi Gutiérrez Hermoso <jordigh@gmail.com>
parents:
11061
diff
changeset
|
172 swapped = true; |
88d78ee12ee8
Implement gcd for Gaussian (complex) integers
Jordi Gutiérrez Hermoso <jordigh@gmail.com>
parents:
11061
diff
changeset
|
173 } |
88d78ee12ee8
Implement gcd for Gaussian (complex) integers
Jordi Gutiérrez Hermoso <jordigh@gmail.com>
parents:
11061
diff
changeset
|
174 |
18100
6a71e5030df5
Follow coding convention of defining and initializing only 1 variable per line in liboctinterp.
Rik <rik@octave.org>
parents:
17787
diff
changeset
|
175 std::complex<FP> xx, lx, yy, ly; |
6a71e5030df5
Follow coding convention of defining and initializing only 1 variable per line in liboctinterp.
Rik <rik@octave.org>
parents:
17787
diff
changeset
|
176 xx = 0, lx = 1; |
6a71e5030df5
Follow coding convention of defining and initializing only 1 variable per line in liboctinterp.
Rik <rik@octave.org>
parents:
17787
diff
changeset
|
177 yy = 1, ly = 0; |
11064 | 178 |
179 while (abs(bb) != 0) | |
11062
88d78ee12ee8
Implement gcd for Gaussian (complex) integers
Jordi Gutiérrez Hermoso <jordigh@gmail.com>
parents:
11061
diff
changeset
|
180 { |
88d78ee12ee8
Implement gcd for Gaussian (complex) integers
Jordi Gutiérrez Hermoso <jordigh@gmail.com>
parents:
11061
diff
changeset
|
181 std::complex<FP> qq, rr; |
88d78ee12ee8
Implement gcd for Gaussian (complex) integers
Jordi Gutiérrez Hermoso <jordigh@gmail.com>
parents:
11061
diff
changeset
|
182 divide (aa, bb, qq, rr); |
11064 | 183 aa = bb; |
184 bb = rr; | |
11062
88d78ee12ee8
Implement gcd for Gaussian (complex) integers
Jordi Gutiérrez Hermoso <jordigh@gmail.com>
parents:
11061
diff
changeset
|
185 |
88d78ee12ee8
Implement gcd for Gaussian (complex) integers
Jordi Gutiérrez Hermoso <jordigh@gmail.com>
parents:
11061
diff
changeset
|
186 std::complex<FP> tx = lx - qq*xx; |
11064 | 187 lx = xx; |
188 xx = tx; | |
11062
88d78ee12ee8
Implement gcd for Gaussian (complex) integers
Jordi Gutiérrez Hermoso <jordigh@gmail.com>
parents:
11061
diff
changeset
|
189 |
88d78ee12ee8
Implement gcd for Gaussian (complex) integers
Jordi Gutiérrez Hermoso <jordigh@gmail.com>
parents:
11061
diff
changeset
|
190 std::complex<FP> ty = ly - qq*yy; |
11064 | 191 ly = yy; |
192 yy = ty; | |
11062
88d78ee12ee8
Implement gcd for Gaussian (complex) integers
Jordi Gutiérrez Hermoso <jordigh@gmail.com>
parents:
11061
diff
changeset
|
193 } |
88d78ee12ee8
Implement gcd for Gaussian (complex) integers
Jordi Gutiérrez Hermoso <jordigh@gmail.com>
parents:
11061
diff
changeset
|
194 |
88d78ee12ee8
Implement gcd for Gaussian (complex) integers
Jordi Gutiérrez Hermoso <jordigh@gmail.com>
parents:
11061
diff
changeset
|
195 x = lx; |
88d78ee12ee8
Implement gcd for Gaussian (complex) integers
Jordi Gutiérrez Hermoso <jordigh@gmail.com>
parents:
11061
diff
changeset
|
196 y = ly; |
11064 | 197 |
11062
88d78ee12ee8
Implement gcd for Gaussian (complex) integers
Jordi Gutiérrez Hermoso <jordigh@gmail.com>
parents:
11061
diff
changeset
|
198 if (swapped) |
11064 | 199 std::swap (x, y); |
11062
88d78ee12ee8
Implement gcd for Gaussian (complex) integers
Jordi Gutiérrez Hermoso <jordigh@gmail.com>
parents:
11061
diff
changeset
|
200 |
88d78ee12ee8
Implement gcd for Gaussian (complex) integers
Jordi Gutiérrez Hermoso <jordigh@gmail.com>
parents:
11061
diff
changeset
|
201 return aa; |
88d78ee12ee8
Implement gcd for Gaussian (complex) integers
Jordi Gutiérrez Hermoso <jordigh@gmail.com>
parents:
11061
diff
changeset
|
202 } |
88d78ee12ee8
Implement gcd for Gaussian (complex) integers
Jordi Gutiérrez Hermoso <jordigh@gmail.com>
parents:
11061
diff
changeset
|
203 |
21139
538b57866b90
consistently use "typename" intead of "class" in template declarations
John W. Eaton <jwe@octave.org>
parents:
21100
diff
changeset
|
204 template <typename T> |
11021
0ee74d581c00
optimize & rewrite gcd
Jaroslav Hajek <highegg@gmail.com>
parents:
10840
diff
changeset
|
205 static octave_int<T> |
0ee74d581c00
optimize & rewrite gcd
Jaroslav Hajek <highegg@gmail.com>
parents:
10840
diff
changeset
|
206 extended_gcd (const octave_int<T>& a, const octave_int<T>& b, |
0ee74d581c00
optimize & rewrite gcd
Jaroslav Hajek <highegg@gmail.com>
parents:
10840
diff
changeset
|
207 octave_int<T>& x, octave_int<T>& y) |
4864 | 208 { |
11064 | 209 T aa = a.abs ().value (); |
210 T bb = b.abs ().value (); | |
18100
6a71e5030df5
Follow coding convention of defining and initializing only 1 variable per line in liboctinterp.
Rik <rik@octave.org>
parents:
17787
diff
changeset
|
211 T xx, lx, yy, ly; |
6a71e5030df5
Follow coding convention of defining and initializing only 1 variable per line in liboctinterp.
Rik <rik@octave.org>
parents:
17787
diff
changeset
|
212 xx = 0, lx = 1; |
6a71e5030df5
Follow coding convention of defining and initializing only 1 variable per line in liboctinterp.
Rik <rik@octave.org>
parents:
17787
diff
changeset
|
213 yy = 1, ly = 0; |
11064 | 214 |
11021
0ee74d581c00
optimize & rewrite gcd
Jaroslav Hajek <highegg@gmail.com>
parents:
10840
diff
changeset
|
215 while (bb != 0) |
0ee74d581c00
optimize & rewrite gcd
Jaroslav Hajek <highegg@gmail.com>
parents:
10840
diff
changeset
|
216 { |
0ee74d581c00
optimize & rewrite gcd
Jaroslav Hajek <highegg@gmail.com>
parents:
10840
diff
changeset
|
217 T qq = aa / bb; |
0ee74d581c00
optimize & rewrite gcd
Jaroslav Hajek <highegg@gmail.com>
parents:
10840
diff
changeset
|
218 T tt = aa % bb; |
11064 | 219 aa = bb; |
220 bb = tt; | |
11021
0ee74d581c00
optimize & rewrite gcd
Jaroslav Hajek <highegg@gmail.com>
parents:
10840
diff
changeset
|
221 |
0ee74d581c00
optimize & rewrite gcd
Jaroslav Hajek <highegg@gmail.com>
parents:
10840
diff
changeset
|
222 T tx = lx - qq*xx; |
11064 | 223 lx = xx; |
224 xx = tx; | |
11021
0ee74d581c00
optimize & rewrite gcd
Jaroslav Hajek <highegg@gmail.com>
parents:
10840
diff
changeset
|
225 |
0ee74d581c00
optimize & rewrite gcd
Jaroslav Hajek <highegg@gmail.com>
parents:
10840
diff
changeset
|
226 T ty = ly - qq*yy; |
11064 | 227 ly = yy; |
228 yy = ty; | |
11021
0ee74d581c00
optimize & rewrite gcd
Jaroslav Hajek <highegg@gmail.com>
parents:
10840
diff
changeset
|
229 } |
0ee74d581c00
optimize & rewrite gcd
Jaroslav Hajek <highegg@gmail.com>
parents:
10840
diff
changeset
|
230 |
0ee74d581c00
optimize & rewrite gcd
Jaroslav Hajek <highegg@gmail.com>
parents:
10840
diff
changeset
|
231 x = octave_int<T> (lx) * a.signum (); |
0ee74d581c00
optimize & rewrite gcd
Jaroslav Hajek <highegg@gmail.com>
parents:
10840
diff
changeset
|
232 y = octave_int<T> (ly) * b.signum (); |
0ee74d581c00
optimize & rewrite gcd
Jaroslav Hajek <highegg@gmail.com>
parents:
10840
diff
changeset
|
233 |
0ee74d581c00
optimize & rewrite gcd
Jaroslav Hajek <highegg@gmail.com>
parents:
10840
diff
changeset
|
234 return aa; |
0ee74d581c00
optimize & rewrite gcd
Jaroslav Hajek <highegg@gmail.com>
parents:
10840
diff
changeset
|
235 } |
0ee74d581c00
optimize & rewrite gcd
Jaroslav Hajek <highegg@gmail.com>
parents:
10840
diff
changeset
|
236 |
21139
538b57866b90
consistently use "typename" intead of "class" in template declarations
John W. Eaton <jwe@octave.org>
parents:
21100
diff
changeset
|
237 template <typename NDA> |
11021
0ee74d581c00
optimize & rewrite gcd
Jaroslav Hajek <highegg@gmail.com>
parents:
10840
diff
changeset
|
238 static octave_value |
0ee74d581c00
optimize & rewrite gcd
Jaroslav Hajek <highegg@gmail.com>
parents:
10840
diff
changeset
|
239 do_simple_gcd (const octave_value& a, const octave_value& b) |
0ee74d581c00
optimize & rewrite gcd
Jaroslav Hajek <highegg@gmail.com>
parents:
10840
diff
changeset
|
240 { |
0ee74d581c00
optimize & rewrite gcd
Jaroslav Hajek <highegg@gmail.com>
parents:
10840
diff
changeset
|
241 typedef typename NDA::element_type T; |
0ee74d581c00
optimize & rewrite gcd
Jaroslav Hajek <highegg@gmail.com>
parents:
10840
diff
changeset
|
242 octave_value retval; |
0ee74d581c00
optimize & rewrite gcd
Jaroslav Hajek <highegg@gmail.com>
parents:
10840
diff
changeset
|
243 |
0ee74d581c00
optimize & rewrite gcd
Jaroslav Hajek <highegg@gmail.com>
parents:
10840
diff
changeset
|
244 if (a.is_scalar_type () && b.is_scalar_type ()) |
0ee74d581c00
optimize & rewrite gcd
Jaroslav Hajek <highegg@gmail.com>
parents:
10840
diff
changeset
|
245 { |
0ee74d581c00
optimize & rewrite gcd
Jaroslav Hajek <highegg@gmail.com>
parents:
10840
diff
changeset
|
246 // Optimize scalar case. |
0ee74d581c00
optimize & rewrite gcd
Jaroslav Hajek <highegg@gmail.com>
parents:
10840
diff
changeset
|
247 T aa = octave_value_extract<T> (a); |
0ee74d581c00
optimize & rewrite gcd
Jaroslav Hajek <highegg@gmail.com>
parents:
10840
diff
changeset
|
248 T bb = octave_value_extract<T> (b); |
0ee74d581c00
optimize & rewrite gcd
Jaroslav Hajek <highegg@gmail.com>
parents:
10840
diff
changeset
|
249 retval = simple_gcd (aa, bb); |
0ee74d581c00
optimize & rewrite gcd
Jaroslav Hajek <highegg@gmail.com>
parents:
10840
diff
changeset
|
250 } |
0ee74d581c00
optimize & rewrite gcd
Jaroslav Hajek <highegg@gmail.com>
parents:
10840
diff
changeset
|
251 else |
0ee74d581c00
optimize & rewrite gcd
Jaroslav Hajek <highegg@gmail.com>
parents:
10840
diff
changeset
|
252 { |
0ee74d581c00
optimize & rewrite gcd
Jaroslav Hajek <highegg@gmail.com>
parents:
10840
diff
changeset
|
253 NDA aa = octave_value_extract<NDA> (a); |
0ee74d581c00
optimize & rewrite gcd
Jaroslav Hajek <highegg@gmail.com>
parents:
10840
diff
changeset
|
254 NDA bb = octave_value_extract<NDA> (b); |
0ee74d581c00
optimize & rewrite gcd
Jaroslav Hajek <highegg@gmail.com>
parents:
10840
diff
changeset
|
255 retval = binmap<T> (aa, bb, simple_gcd, "gcd"); |
0ee74d581c00
optimize & rewrite gcd
Jaroslav Hajek <highegg@gmail.com>
parents:
10840
diff
changeset
|
256 } |
0ee74d581c00
optimize & rewrite gcd
Jaroslav Hajek <highegg@gmail.com>
parents:
10840
diff
changeset
|
257 |
0ee74d581c00
optimize & rewrite gcd
Jaroslav Hajek <highegg@gmail.com>
parents:
10840
diff
changeset
|
258 return retval; |
4864 | 259 } |
260 | |
11021
0ee74d581c00
optimize & rewrite gcd
Jaroslav Hajek <highegg@gmail.com>
parents:
10840
diff
changeset
|
261 // Dispatcher |
0ee74d581c00
optimize & rewrite gcd
Jaroslav Hajek <highegg@gmail.com>
parents:
10840
diff
changeset
|
262 static octave_value |
0ee74d581c00
optimize & rewrite gcd
Jaroslav Hajek <highegg@gmail.com>
parents:
10840
diff
changeset
|
263 do_simple_gcd (const octave_value& a, const octave_value& b) |
0ee74d581c00
optimize & rewrite gcd
Jaroslav Hajek <highegg@gmail.com>
parents:
10840
diff
changeset
|
264 { |
0ee74d581c00
optimize & rewrite gcd
Jaroslav Hajek <highegg@gmail.com>
parents:
10840
diff
changeset
|
265 octave_value retval; |
0ee74d581c00
optimize & rewrite gcd
Jaroslav Hajek <highegg@gmail.com>
parents:
10840
diff
changeset
|
266 builtin_type_t btyp = btyp_mixed_numeric (a.builtin_type (), |
31607
aac27ad79be6
maint: Re-indent code after switch to using namespace macros.
Rik <rik@octave.org>
parents:
31605
diff
changeset
|
267 b.builtin_type ()); |
11021
0ee74d581c00
optimize & rewrite gcd
Jaroslav Hajek <highegg@gmail.com>
parents:
10840
diff
changeset
|
268 switch (btyp) |
0ee74d581c00
optimize & rewrite gcd
Jaroslav Hajek <highegg@gmail.com>
parents:
10840
diff
changeset
|
269 { |
0ee74d581c00
optimize & rewrite gcd
Jaroslav Hajek <highegg@gmail.com>
parents:
10840
diff
changeset
|
270 case btyp_double: |
23583
b7747a2c88b2
maint: Deprecate is_sparse_type and replace with issparse.
Rik <rik@octave.org>
parents:
23450
diff
changeset
|
271 if (a.issparse () && b.issparse ()) |
11021
0ee74d581c00
optimize & rewrite gcd
Jaroslav Hajek <highegg@gmail.com>
parents:
10840
diff
changeset
|
272 { |
0ee74d581c00
optimize & rewrite gcd
Jaroslav Hajek <highegg@gmail.com>
parents:
10840
diff
changeset
|
273 retval = do_simple_gcd<SparseMatrix> (a, b); |
0ee74d581c00
optimize & rewrite gcd
Jaroslav Hajek <highegg@gmail.com>
parents:
10840
diff
changeset
|
274 break; |
0ee74d581c00
optimize & rewrite gcd
Jaroslav Hajek <highegg@gmail.com>
parents:
10840
diff
changeset
|
275 } |
23826
d69021d58a61
avoid fallthrough warnings
John W. Eaton <jwe@octave.org>
parents:
23583
diff
changeset
|
276 OCTAVE_FALLTHROUGH; |
11064 | 277 |
11021
0ee74d581c00
optimize & rewrite gcd
Jaroslav Hajek <highegg@gmail.com>
parents:
10840
diff
changeset
|
278 case btyp_float: |
0ee74d581c00
optimize & rewrite gcd
Jaroslav Hajek <highegg@gmail.com>
parents:
10840
diff
changeset
|
279 retval = do_simple_gcd<NDArray> (a, b); |
0ee74d581c00
optimize & rewrite gcd
Jaroslav Hajek <highegg@gmail.com>
parents:
10840
diff
changeset
|
280 break; |
11064 | 281 |
22407
34ce5be04942
maint: Style check C++ code in libinterp/.
Rik <rik@octave.org>
parents:
22327
diff
changeset
|
282 #define MAKE_INT_BRANCH(X) \ |
34ce5be04942
maint: Style check C++ code in libinterp/.
Rik <rik@octave.org>
parents:
22327
diff
changeset
|
283 case btyp_ ## X: \ |
34ce5be04942
maint: Style check C++ code in libinterp/.
Rik <rik@octave.org>
parents:
22327
diff
changeset
|
284 retval = do_simple_gcd<X ## NDArray> (a, b); \ |
34ce5be04942
maint: Style check C++ code in libinterp/.
Rik <rik@octave.org>
parents:
22327
diff
changeset
|
285 break |
11064 | 286 |
31607
aac27ad79be6
maint: Re-indent code after switch to using namespace macros.
Rik <rik@octave.org>
parents:
31605
diff
changeset
|
287 MAKE_INT_BRANCH (int8); |
aac27ad79be6
maint: Re-indent code after switch to using namespace macros.
Rik <rik@octave.org>
parents:
31605
diff
changeset
|
288 MAKE_INT_BRANCH (int16); |
aac27ad79be6
maint: Re-indent code after switch to using namespace macros.
Rik <rik@octave.org>
parents:
31605
diff
changeset
|
289 MAKE_INT_BRANCH (int32); |
aac27ad79be6
maint: Re-indent code after switch to using namespace macros.
Rik <rik@octave.org>
parents:
31605
diff
changeset
|
290 MAKE_INT_BRANCH (int64); |
aac27ad79be6
maint: Re-indent code after switch to using namespace macros.
Rik <rik@octave.org>
parents:
31605
diff
changeset
|
291 MAKE_INT_BRANCH (uint8); |
aac27ad79be6
maint: Re-indent code after switch to using namespace macros.
Rik <rik@octave.org>
parents:
31605
diff
changeset
|
292 MAKE_INT_BRANCH (uint16); |
aac27ad79be6
maint: Re-indent code after switch to using namespace macros.
Rik <rik@octave.org>
parents:
31605
diff
changeset
|
293 MAKE_INT_BRANCH (uint32); |
aac27ad79be6
maint: Re-indent code after switch to using namespace macros.
Rik <rik@octave.org>
parents:
31605
diff
changeset
|
294 MAKE_INT_BRANCH (uint64); |
11064 | 295 |
11021
0ee74d581c00
optimize & rewrite gcd
Jaroslav Hajek <highegg@gmail.com>
parents:
10840
diff
changeset
|
296 #undef MAKE_INT_BRANCH |
11064 | 297 |
11021
0ee74d581c00
optimize & rewrite gcd
Jaroslav Hajek <highegg@gmail.com>
parents:
10840
diff
changeset
|
298 case btyp_complex: |
11064 | 299 retval = do_simple_gcd<ComplexNDArray> (a, b); |
11062
88d78ee12ee8
Implement gcd for Gaussian (complex) integers
Jordi Gutiérrez Hermoso <jordigh@gmail.com>
parents:
11061
diff
changeset
|
300 break; |
11064 | 301 |
11021
0ee74d581c00
optimize & rewrite gcd
Jaroslav Hajek <highegg@gmail.com>
parents:
10840
diff
changeset
|
302 case btyp_float_complex: |
11064 | 303 retval = do_simple_gcd<FloatComplexNDArray> (a, b); |
11062
88d78ee12ee8
Implement gcd for Gaussian (complex) integers
Jordi Gutiérrez Hermoso <jordigh@gmail.com>
parents:
11061
diff
changeset
|
304 break; |
11064 | 305 |
11021
0ee74d581c00
optimize & rewrite gcd
Jaroslav Hajek <highegg@gmail.com>
parents:
10840
diff
changeset
|
306 default: |
0ee74d581c00
optimize & rewrite gcd
Jaroslav Hajek <highegg@gmail.com>
parents:
10840
diff
changeset
|
307 error ("gcd: invalid class combination for gcd: %s and %s\n", |
0ee74d581c00
optimize & rewrite gcd
Jaroslav Hajek <highegg@gmail.com>
parents:
10840
diff
changeset
|
308 a.class_name ().c_str (), b.class_name ().c_str ()); |
0ee74d581c00
optimize & rewrite gcd
Jaroslav Hajek <highegg@gmail.com>
parents:
10840
diff
changeset
|
309 } |
0ee74d581c00
optimize & rewrite gcd
Jaroslav Hajek <highegg@gmail.com>
parents:
10840
diff
changeset
|
310 |
0ee74d581c00
optimize & rewrite gcd
Jaroslav Hajek <highegg@gmail.com>
parents:
10840
diff
changeset
|
311 if (btyp == btyp_float) |
0ee74d581c00
optimize & rewrite gcd
Jaroslav Hajek <highegg@gmail.com>
parents:
10840
diff
changeset
|
312 retval = retval.float_array_value (); |
0ee74d581c00
optimize & rewrite gcd
Jaroslav Hajek <highegg@gmail.com>
parents:
10840
diff
changeset
|
313 |
0ee74d581c00
optimize & rewrite gcd
Jaroslav Hajek <highegg@gmail.com>
parents:
10840
diff
changeset
|
314 return retval; |
0ee74d581c00
optimize & rewrite gcd
Jaroslav Hajek <highegg@gmail.com>
parents:
10840
diff
changeset
|
315 } |
0ee74d581c00
optimize & rewrite gcd
Jaroslav Hajek <highegg@gmail.com>
parents:
10840
diff
changeset
|
316 |
21139
538b57866b90
consistently use "typename" intead of "class" in template declarations
John W. Eaton <jwe@octave.org>
parents:
21100
diff
changeset
|
317 template <typename NDA> |
11021
0ee74d581c00
optimize & rewrite gcd
Jaroslav Hajek <highegg@gmail.com>
parents:
10840
diff
changeset
|
318 static octave_value |
0ee74d581c00
optimize & rewrite gcd
Jaroslav Hajek <highegg@gmail.com>
parents:
10840
diff
changeset
|
319 do_extended_gcd (const octave_value& a, const octave_value& b, |
0ee74d581c00
optimize & rewrite gcd
Jaroslav Hajek <highegg@gmail.com>
parents:
10840
diff
changeset
|
320 octave_value& x, octave_value& y) |
7815
a41df65f3f00
Add some single precision test code and fix resulting bugs
David Bateman <dbateman@free.fr>
parents:
7017
diff
changeset
|
321 { |
11021
0ee74d581c00
optimize & rewrite gcd
Jaroslav Hajek <highegg@gmail.com>
parents:
10840
diff
changeset
|
322 typedef typename NDA::element_type T; |
0ee74d581c00
optimize & rewrite gcd
Jaroslav Hajek <highegg@gmail.com>
parents:
10840
diff
changeset
|
323 octave_value retval; |
0ee74d581c00
optimize & rewrite gcd
Jaroslav Hajek <highegg@gmail.com>
parents:
10840
diff
changeset
|
324 |
0ee74d581c00
optimize & rewrite gcd
Jaroslav Hajek <highegg@gmail.com>
parents:
10840
diff
changeset
|
325 if (a.is_scalar_type () && b.is_scalar_type ()) |
0ee74d581c00
optimize & rewrite gcd
Jaroslav Hajek <highegg@gmail.com>
parents:
10840
diff
changeset
|
326 { |
0ee74d581c00
optimize & rewrite gcd
Jaroslav Hajek <highegg@gmail.com>
parents:
10840
diff
changeset
|
327 // Optimize scalar case. |
0ee74d581c00
optimize & rewrite gcd
Jaroslav Hajek <highegg@gmail.com>
parents:
10840
diff
changeset
|
328 T aa = octave_value_extract<T> (a); |
0ee74d581c00
optimize & rewrite gcd
Jaroslav Hajek <highegg@gmail.com>
parents:
10840
diff
changeset
|
329 T bb = octave_value_extract<T> (b); |
0ee74d581c00
optimize & rewrite gcd
Jaroslav Hajek <highegg@gmail.com>
parents:
10840
diff
changeset
|
330 T xx, yy; |
0ee74d581c00
optimize & rewrite gcd
Jaroslav Hajek <highegg@gmail.com>
parents:
10840
diff
changeset
|
331 retval = extended_gcd (aa, bb, xx, yy); |
0ee74d581c00
optimize & rewrite gcd
Jaroslav Hajek <highegg@gmail.com>
parents:
10840
diff
changeset
|
332 x = xx; |
0ee74d581c00
optimize & rewrite gcd
Jaroslav Hajek <highegg@gmail.com>
parents:
10840
diff
changeset
|
333 y = yy; |
0ee74d581c00
optimize & rewrite gcd
Jaroslav Hajek <highegg@gmail.com>
parents:
10840
diff
changeset
|
334 } |
0ee74d581c00
optimize & rewrite gcd
Jaroslav Hajek <highegg@gmail.com>
parents:
10840
diff
changeset
|
335 else |
0ee74d581c00
optimize & rewrite gcd
Jaroslav Hajek <highegg@gmail.com>
parents:
10840
diff
changeset
|
336 { |
0ee74d581c00
optimize & rewrite gcd
Jaroslav Hajek <highegg@gmail.com>
parents:
10840
diff
changeset
|
337 NDA aa = octave_value_extract<NDA> (a); |
0ee74d581c00
optimize & rewrite gcd
Jaroslav Hajek <highegg@gmail.com>
parents:
10840
diff
changeset
|
338 NDA bb = octave_value_extract<NDA> (b); |
0ee74d581c00
optimize & rewrite gcd
Jaroslav Hajek <highegg@gmail.com>
parents:
10840
diff
changeset
|
339 |
0ee74d581c00
optimize & rewrite gcd
Jaroslav Hajek <highegg@gmail.com>
parents:
10840
diff
changeset
|
340 dim_vector dv = aa.dims (); |
0ee74d581c00
optimize & rewrite gcd
Jaroslav Hajek <highegg@gmail.com>
parents:
10840
diff
changeset
|
341 if (aa.numel () == 1) |
0ee74d581c00
optimize & rewrite gcd
Jaroslav Hajek <highegg@gmail.com>
parents:
10840
diff
changeset
|
342 dv = bb.dims (); |
0ee74d581c00
optimize & rewrite gcd
Jaroslav Hajek <highegg@gmail.com>
parents:
10840
diff
changeset
|
343 else if (bb.numel () != 1 && bb.dims () != dv) |
29961
7d6709900da7
eliminate octave:: namespace tags in DEFUN and DEFMETHOD and more
John W. Eaton <jwe@octave.org>
parents:
29958
diff
changeset
|
344 err_nonconformant ("gcd", a.dims (), b.dims ()); |
11021
0ee74d581c00
optimize & rewrite gcd
Jaroslav Hajek <highegg@gmail.com>
parents:
10840
diff
changeset
|
345 |
0ee74d581c00
optimize & rewrite gcd
Jaroslav Hajek <highegg@gmail.com>
parents:
10840
diff
changeset
|
346 NDA gg (dv), xx (dv), yy (dv); |
0ee74d581c00
optimize & rewrite gcd
Jaroslav Hajek <highegg@gmail.com>
parents:
10840
diff
changeset
|
347 |
29931
7faff48840eb
prefer data over fortran_vec for read-only access to data
John W. Eaton <jwe@octave.org>
parents:
29359
diff
changeset
|
348 const T *aptr = aa.data (); |
7faff48840eb
prefer data over fortran_vec for read-only access to data
John W. Eaton <jwe@octave.org>
parents:
29359
diff
changeset
|
349 const T *bptr = bb.data (); |
11021
0ee74d581c00
optimize & rewrite gcd
Jaroslav Hajek <highegg@gmail.com>
parents:
10840
diff
changeset
|
350 |
11064 | 351 bool inca = aa.numel () != 1; |
352 bool incb = bb.numel () != 1; | |
11021
0ee74d581c00
optimize & rewrite gcd
Jaroslav Hajek <highegg@gmail.com>
parents:
10840
diff
changeset
|
353 |
32660
f53ac65ffba6
maint: New method rwdata() as clearer alternative to fortran_vec().
Rik <rik@octave.org>
parents:
32632
diff
changeset
|
354 T *gptr = gg.rwdata (); |
f53ac65ffba6
maint: New method rwdata() as clearer alternative to fortran_vec().
Rik <rik@octave.org>
parents:
32632
diff
changeset
|
355 T *xptr = xx.rwdata (); |
f53ac65ffba6
maint: New method rwdata() as clearer alternative to fortran_vec().
Rik <rik@octave.org>
parents:
32632
diff
changeset
|
356 T *yptr = yy.rwdata (); |
11021
0ee74d581c00
optimize & rewrite gcd
Jaroslav Hajek <highegg@gmail.com>
parents:
10840
diff
changeset
|
357 |
0ee74d581c00
optimize & rewrite gcd
Jaroslav Hajek <highegg@gmail.com>
parents:
10840
diff
changeset
|
358 octave_idx_type n = gg.numel (); |
0ee74d581c00
optimize & rewrite gcd
Jaroslav Hajek <highegg@gmail.com>
parents:
10840
diff
changeset
|
359 for (octave_idx_type i = 0; i < n; i++) |
0ee74d581c00
optimize & rewrite gcd
Jaroslav Hajek <highegg@gmail.com>
parents:
10840
diff
changeset
|
360 { |
0ee74d581c00
optimize & rewrite gcd
Jaroslav Hajek <highegg@gmail.com>
parents:
10840
diff
changeset
|
361 octave_quit (); |
0ee74d581c00
optimize & rewrite gcd
Jaroslav Hajek <highegg@gmail.com>
parents:
10840
diff
changeset
|
362 |
0ee74d581c00
optimize & rewrite gcd
Jaroslav Hajek <highegg@gmail.com>
parents:
10840
diff
changeset
|
363 *gptr++ = extended_gcd (*aptr, *bptr, *xptr++, *yptr++); |
11064 | 364 |
11021
0ee74d581c00
optimize & rewrite gcd
Jaroslav Hajek <highegg@gmail.com>
parents:
10840
diff
changeset
|
365 aptr += inca; |
0ee74d581c00
optimize & rewrite gcd
Jaroslav Hajek <highegg@gmail.com>
parents:
10840
diff
changeset
|
366 bptr += incb; |
0ee74d581c00
optimize & rewrite gcd
Jaroslav Hajek <highegg@gmail.com>
parents:
10840
diff
changeset
|
367 } |
0ee74d581c00
optimize & rewrite gcd
Jaroslav Hajek <highegg@gmail.com>
parents:
10840
diff
changeset
|
368 |
0ee74d581c00
optimize & rewrite gcd
Jaroslav Hajek <highegg@gmail.com>
parents:
10840
diff
changeset
|
369 x = xx; |
0ee74d581c00
optimize & rewrite gcd
Jaroslav Hajek <highegg@gmail.com>
parents:
10840
diff
changeset
|
370 y = yy; |
11064 | 371 |
11021
0ee74d581c00
optimize & rewrite gcd
Jaroslav Hajek <highegg@gmail.com>
parents:
10840
diff
changeset
|
372 retval = gg; |
0ee74d581c00
optimize & rewrite gcd
Jaroslav Hajek <highegg@gmail.com>
parents:
10840
diff
changeset
|
373 } |
0ee74d581c00
optimize & rewrite gcd
Jaroslav Hajek <highegg@gmail.com>
parents:
10840
diff
changeset
|
374 |
0ee74d581c00
optimize & rewrite gcd
Jaroslav Hajek <highegg@gmail.com>
parents:
10840
diff
changeset
|
375 return retval; |
7815
a41df65f3f00
Add some single precision test code and fix resulting bugs
David Bateman <dbateman@free.fr>
parents:
7017
diff
changeset
|
376 } |
a41df65f3f00
Add some single precision test code and fix resulting bugs
David Bateman <dbateman@free.fr>
parents:
7017
diff
changeset
|
377 |
11021
0ee74d581c00
optimize & rewrite gcd
Jaroslav Hajek <highegg@gmail.com>
parents:
10840
diff
changeset
|
378 // Dispatcher |
0ee74d581c00
optimize & rewrite gcd
Jaroslav Hajek <highegg@gmail.com>
parents:
10840
diff
changeset
|
379 static octave_value |
0ee74d581c00
optimize & rewrite gcd
Jaroslav Hajek <highegg@gmail.com>
parents:
10840
diff
changeset
|
380 do_extended_gcd (const octave_value& a, const octave_value& b, |
0ee74d581c00
optimize & rewrite gcd
Jaroslav Hajek <highegg@gmail.com>
parents:
10840
diff
changeset
|
381 octave_value& x, octave_value& y) |
0ee74d581c00
optimize & rewrite gcd
Jaroslav Hajek <highegg@gmail.com>
parents:
10840
diff
changeset
|
382 { |
0ee74d581c00
optimize & rewrite gcd
Jaroslav Hajek <highegg@gmail.com>
parents:
10840
diff
changeset
|
383 octave_value retval; |
11064 | 384 |
11021
0ee74d581c00
optimize & rewrite gcd
Jaroslav Hajek <highegg@gmail.com>
parents:
10840
diff
changeset
|
385 builtin_type_t btyp = btyp_mixed_numeric (a.builtin_type (), |
31607
aac27ad79be6
maint: Re-indent code after switch to using namespace macros.
Rik <rik@octave.org>
parents:
31605
diff
changeset
|
386 b.builtin_type ()); |
11021
0ee74d581c00
optimize & rewrite gcd
Jaroslav Hajek <highegg@gmail.com>
parents:
10840
diff
changeset
|
387 switch (btyp) |
0ee74d581c00
optimize & rewrite gcd
Jaroslav Hajek <highegg@gmail.com>
parents:
10840
diff
changeset
|
388 { |
0ee74d581c00
optimize & rewrite gcd
Jaroslav Hajek <highegg@gmail.com>
parents:
10840
diff
changeset
|
389 case btyp_double: |
0ee74d581c00
optimize & rewrite gcd
Jaroslav Hajek <highegg@gmail.com>
parents:
10840
diff
changeset
|
390 case btyp_float: |
0ee74d581c00
optimize & rewrite gcd
Jaroslav Hajek <highegg@gmail.com>
parents:
10840
diff
changeset
|
391 retval = do_extended_gcd<NDArray> (a, b, x, y); |
0ee74d581c00
optimize & rewrite gcd
Jaroslav Hajek <highegg@gmail.com>
parents:
10840
diff
changeset
|
392 break; |
11064 | 393 |
22407
34ce5be04942
maint: Style check C++ code in libinterp/.
Rik <rik@octave.org>
parents:
22327
diff
changeset
|
394 #define MAKE_INT_BRANCH(X) \ |
34ce5be04942
maint: Style check C++ code in libinterp/.
Rik <rik@octave.org>
parents:
22327
diff
changeset
|
395 case btyp_ ## X: \ |
34ce5be04942
maint: Style check C++ code in libinterp/.
Rik <rik@octave.org>
parents:
22327
diff
changeset
|
396 retval = do_extended_gcd<X ## NDArray> (a, b, x, y); \ |
34ce5be04942
maint: Style check C++ code in libinterp/.
Rik <rik@octave.org>
parents:
22327
diff
changeset
|
397 break |
11064 | 398 |
31607
aac27ad79be6
maint: Re-indent code after switch to using namespace macros.
Rik <rik@octave.org>
parents:
31605
diff
changeset
|
399 MAKE_INT_BRANCH (int8); |
aac27ad79be6
maint: Re-indent code after switch to using namespace macros.
Rik <rik@octave.org>
parents:
31605
diff
changeset
|
400 MAKE_INT_BRANCH (int16); |
aac27ad79be6
maint: Re-indent code after switch to using namespace macros.
Rik <rik@octave.org>
parents:
31605
diff
changeset
|
401 MAKE_INT_BRANCH (int32); |
aac27ad79be6
maint: Re-indent code after switch to using namespace macros.
Rik <rik@octave.org>
parents:
31605
diff
changeset
|
402 MAKE_INT_BRANCH (int64); |
aac27ad79be6
maint: Re-indent code after switch to using namespace macros.
Rik <rik@octave.org>
parents:
31605
diff
changeset
|
403 MAKE_INT_BRANCH (uint8); |
aac27ad79be6
maint: Re-indent code after switch to using namespace macros.
Rik <rik@octave.org>
parents:
31605
diff
changeset
|
404 MAKE_INT_BRANCH (uint16); |
aac27ad79be6
maint: Re-indent code after switch to using namespace macros.
Rik <rik@octave.org>
parents:
31605
diff
changeset
|
405 MAKE_INT_BRANCH (uint32); |
aac27ad79be6
maint: Re-indent code after switch to using namespace macros.
Rik <rik@octave.org>
parents:
31605
diff
changeset
|
406 MAKE_INT_BRANCH (uint64); |
11064 | 407 |
11021
0ee74d581c00
optimize & rewrite gcd
Jaroslav Hajek <highegg@gmail.com>
parents:
10840
diff
changeset
|
408 #undef MAKE_INT_BRANCH |
11064 | 409 |
11021
0ee74d581c00
optimize & rewrite gcd
Jaroslav Hajek <highegg@gmail.com>
parents:
10840
diff
changeset
|
410 case btyp_complex: |
11062
88d78ee12ee8
Implement gcd for Gaussian (complex) integers
Jordi Gutiérrez Hermoso <jordigh@gmail.com>
parents:
11061
diff
changeset
|
411 retval = do_extended_gcd<ComplexNDArray> (a, b, x, y); |
88d78ee12ee8
Implement gcd for Gaussian (complex) integers
Jordi Gutiérrez Hermoso <jordigh@gmail.com>
parents:
11061
diff
changeset
|
412 break; |
11064 | 413 |
11021
0ee74d581c00
optimize & rewrite gcd
Jaroslav Hajek <highegg@gmail.com>
parents:
10840
diff
changeset
|
414 case btyp_float_complex: |
11062
88d78ee12ee8
Implement gcd for Gaussian (complex) integers
Jordi Gutiérrez Hermoso <jordigh@gmail.com>
parents:
11061
diff
changeset
|
415 retval = do_extended_gcd<FloatComplexNDArray> (a, b, x, y); |
88d78ee12ee8
Implement gcd for Gaussian (complex) integers
Jordi Gutiérrez Hermoso <jordigh@gmail.com>
parents:
11061
diff
changeset
|
416 break; |
11064 | 417 |
11021
0ee74d581c00
optimize & rewrite gcd
Jaroslav Hajek <highegg@gmail.com>
parents:
10840
diff
changeset
|
418 default: |
0ee74d581c00
optimize & rewrite gcd
Jaroslav Hajek <highegg@gmail.com>
parents:
10840
diff
changeset
|
419 error ("gcd: invalid class combination for gcd: %s and %s\n", |
0ee74d581c00
optimize & rewrite gcd
Jaroslav Hajek <highegg@gmail.com>
parents:
10840
diff
changeset
|
420 a.class_name ().c_str (), b.class_name ().c_str ()); |
0ee74d581c00
optimize & rewrite gcd
Jaroslav Hajek <highegg@gmail.com>
parents:
10840
diff
changeset
|
421 } |
0ee74d581c00
optimize & rewrite gcd
Jaroslav Hajek <highegg@gmail.com>
parents:
10840
diff
changeset
|
422 |
0ee74d581c00
optimize & rewrite gcd
Jaroslav Hajek <highegg@gmail.com>
parents:
10840
diff
changeset
|
423 // For consistency. |
23583
b7747a2c88b2
maint: Deprecate is_sparse_type and replace with issparse.
Rik <rik@octave.org>
parents:
23450
diff
changeset
|
424 if (a.issparse () && b.issparse ()) |
11021
0ee74d581c00
optimize & rewrite gcd
Jaroslav Hajek <highegg@gmail.com>
parents:
10840
diff
changeset
|
425 { |
0ee74d581c00
optimize & rewrite gcd
Jaroslav Hajek <highegg@gmail.com>
parents:
10840
diff
changeset
|
426 retval = retval.sparse_matrix_value (); |
0ee74d581c00
optimize & rewrite gcd
Jaroslav Hajek <highegg@gmail.com>
parents:
10840
diff
changeset
|
427 x = x.sparse_matrix_value (); |
0ee74d581c00
optimize & rewrite gcd
Jaroslav Hajek <highegg@gmail.com>
parents:
10840
diff
changeset
|
428 y = y.sparse_matrix_value (); |
0ee74d581c00
optimize & rewrite gcd
Jaroslav Hajek <highegg@gmail.com>
parents:
10840
diff
changeset
|
429 } |
0ee74d581c00
optimize & rewrite gcd
Jaroslav Hajek <highegg@gmail.com>
parents:
10840
diff
changeset
|
430 |
0ee74d581c00
optimize & rewrite gcd
Jaroslav Hajek <highegg@gmail.com>
parents:
10840
diff
changeset
|
431 if (btyp == btyp_float) |
0ee74d581c00
optimize & rewrite gcd
Jaroslav Hajek <highegg@gmail.com>
parents:
10840
diff
changeset
|
432 { |
0ee74d581c00
optimize & rewrite gcd
Jaroslav Hajek <highegg@gmail.com>
parents:
10840
diff
changeset
|
433 retval = retval.float_array_value (); |
0ee74d581c00
optimize & rewrite gcd
Jaroslav Hajek <highegg@gmail.com>
parents:
10840
diff
changeset
|
434 x = x.float_array_value (); |
0ee74d581c00
optimize & rewrite gcd
Jaroslav Hajek <highegg@gmail.com>
parents:
10840
diff
changeset
|
435 y = y.float_array_value (); |
0ee74d581c00
optimize & rewrite gcd
Jaroslav Hajek <highegg@gmail.com>
parents:
10840
diff
changeset
|
436 } |
0ee74d581c00
optimize & rewrite gcd
Jaroslav Hajek <highegg@gmail.com>
parents:
10840
diff
changeset
|
437 |
0ee74d581c00
optimize & rewrite gcd
Jaroslav Hajek <highegg@gmail.com>
parents:
10840
diff
changeset
|
438 return retval; |
0ee74d581c00
optimize & rewrite gcd
Jaroslav Hajek <highegg@gmail.com>
parents:
10840
diff
changeset
|
439 } |
0ee74d581c00
optimize & rewrite gcd
Jaroslav Hajek <highegg@gmail.com>
parents:
10840
diff
changeset
|
440 |
15039
e753177cde93
maint: Move non-dynamically linked functions from DLD-FUNCTIONS/ to corefcn/ directory
Rik <rik@octave.org>
parents:
14854
diff
changeset
|
441 DEFUN (gcd, args, nargout, |
21966
112b20240c87
move docstrings in C++ files out of C strings and into comments
John W. Eaton <jwe@octave.org>
parents:
21942
diff
changeset
|
442 doc: /* -*- texinfo -*- |
112b20240c87
move docstrings in C++ files out of C strings and into comments
John W. Eaton <jwe@octave.org>
parents:
21942
diff
changeset
|
443 @deftypefn {} {@var{g} =} gcd (@var{a1}, @var{a2}, @dots{}) |
112b20240c87
move docstrings in C++ files out of C strings and into comments
John W. Eaton <jwe@octave.org>
parents:
21942
diff
changeset
|
444 @deftypefnx {} {[@var{g}, @var{v1}, @dots{}] =} gcd (@var{a1}, @var{a2}, @dots{}) |
112b20240c87
move docstrings in C++ files out of C strings and into comments
John W. Eaton <jwe@octave.org>
parents:
21942
diff
changeset
|
445 Compute the greatest common divisor of @var{a1}, @var{a2}, @dots{}. |
112b20240c87
move docstrings in C++ files out of C strings and into comments
John W. Eaton <jwe@octave.org>
parents:
21942
diff
changeset
|
446 |
27954
cbabcd7bb6c3
doc: Clarify documentation of inputs for gcd (bug #57609).
Rik <rik@octave.org>
parents:
27923
diff
changeset
|
447 All arguments must be the same size or scalar. For arrays, the greatest common |
cbabcd7bb6c3
doc: Clarify documentation of inputs for gcd (bug #57609).
Rik <rik@octave.org>
parents:
27923
diff
changeset
|
448 divisor is calculated for each element individually. All elements must be |
cbabcd7bb6c3
doc: Clarify documentation of inputs for gcd (bug #57609).
Rik <rik@octave.org>
parents:
27923
diff
changeset
|
449 ordinary or Gaussian (complex) integers. Note that for Gaussian integers, the |
cbabcd7bb6c3
doc: Clarify documentation of inputs for gcd (bug #57609).
Rik <rik@octave.org>
parents:
27923
diff
changeset
|
450 gcd is only unique up to a phase factor (multiplication by 1, -1, i, or -i), so |
cbabcd7bb6c3
doc: Clarify documentation of inputs for gcd (bug #57609).
Rik <rik@octave.org>
parents:
27923
diff
changeset
|
451 an arbitrary greatest common divisor among the four possible is returned. |
21966
112b20240c87
move docstrings in C++ files out of C strings and into comments
John W. Eaton <jwe@octave.org>
parents:
21942
diff
changeset
|
452 |
112b20240c87
move docstrings in C++ files out of C strings and into comments
John W. Eaton <jwe@octave.org>
parents:
21942
diff
changeset
|
453 Optional return arguments @var{v1}, @dots{}, contain integer vectors such |
112b20240c87
move docstrings in C++ files out of C strings and into comments
John W. Eaton <jwe@octave.org>
parents:
21942
diff
changeset
|
454 that, |
112b20240c87
move docstrings in C++ files out of C strings and into comments
John W. Eaton <jwe@octave.org>
parents:
21942
diff
changeset
|
455 |
112b20240c87
move docstrings in C++ files out of C strings and into comments
John W. Eaton <jwe@octave.org>
parents:
21942
diff
changeset
|
456 @tex |
112b20240c87
move docstrings in C++ files out of C strings and into comments
John W. Eaton <jwe@octave.org>
parents:
21942
diff
changeset
|
457 $g = v_1 a_1 + v_2 a_2 + \cdots$ |
112b20240c87
move docstrings in C++ files out of C strings and into comments
John W. Eaton <jwe@octave.org>
parents:
21942
diff
changeset
|
458 @end tex |
112b20240c87
move docstrings in C++ files out of C strings and into comments
John W. Eaton <jwe@octave.org>
parents:
21942
diff
changeset
|
459 @ifnottex |
112b20240c87
move docstrings in C++ files out of C strings and into comments
John W. Eaton <jwe@octave.org>
parents:
21942
diff
changeset
|
460 |
112b20240c87
move docstrings in C++ files out of C strings and into comments
John W. Eaton <jwe@octave.org>
parents:
21942
diff
changeset
|
461 @example |
112b20240c87
move docstrings in C++ files out of C strings and into comments
John W. Eaton <jwe@octave.org>
parents:
21942
diff
changeset
|
462 @var{g} = @var{v1} .* @var{a1} + @var{v2} .* @var{a2} + @dots{} |
112b20240c87
move docstrings in C++ files out of C strings and into comments
John W. Eaton <jwe@octave.org>
parents:
21942
diff
changeset
|
463 @end example |
112b20240c87
move docstrings in C++ files out of C strings and into comments
John W. Eaton <jwe@octave.org>
parents:
21942
diff
changeset
|
464 |
112b20240c87
move docstrings in C++ files out of C strings and into comments
John W. Eaton <jwe@octave.org>
parents:
21942
diff
changeset
|
465 @end ifnottex |
112b20240c87
move docstrings in C++ files out of C strings and into comments
John W. Eaton <jwe@octave.org>
parents:
21942
diff
changeset
|
466 |
112b20240c87
move docstrings in C++ files out of C strings and into comments
John W. Eaton <jwe@octave.org>
parents:
21942
diff
changeset
|
467 Example code: |
112b20240c87
move docstrings in C++ files out of C strings and into comments
John W. Eaton <jwe@octave.org>
parents:
21942
diff
changeset
|
468 |
112b20240c87
move docstrings in C++ files out of C strings and into comments
John W. Eaton <jwe@octave.org>
parents:
21942
diff
changeset
|
469 @example |
112b20240c87
move docstrings in C++ files out of C strings and into comments
John W. Eaton <jwe@octave.org>
parents:
21942
diff
changeset
|
470 @group |
112b20240c87
move docstrings in C++ files out of C strings and into comments
John W. Eaton <jwe@octave.org>
parents:
21942
diff
changeset
|
471 gcd ([15, 9], [20, 18]) |
112b20240c87
move docstrings in C++ files out of C strings and into comments
John W. Eaton <jwe@octave.org>
parents:
21942
diff
changeset
|
472 @result{} 5 9 |
112b20240c87
move docstrings in C++ files out of C strings and into comments
John W. Eaton <jwe@octave.org>
parents:
21942
diff
changeset
|
473 @end group |
112b20240c87
move docstrings in C++ files out of C strings and into comments
John W. Eaton <jwe@octave.org>
parents:
21942
diff
changeset
|
474 @end example |
112b20240c87
move docstrings in C++ files out of C strings and into comments
John W. Eaton <jwe@octave.org>
parents:
21942
diff
changeset
|
475 |
32271
e81b372d1203
doc: Expand documentation for gcd() and lcm()
Arun Giridhar <arungiridhar@gmail.com>
parents:
31706
diff
changeset
|
476 Programming tip: To find the GCD of all the elements of a single array, use |
e81b372d1203
doc: Expand documentation for gcd() and lcm()
Arun Giridhar <arungiridhar@gmail.com>
parents:
31706
diff
changeset
|
477 @code{num2cell} instead of nested calls or a loop: |
e81b372d1203
doc: Expand documentation for gcd() and lcm()
Arun Giridhar <arungiridhar@gmail.com>
parents:
31706
diff
changeset
|
478 |
e81b372d1203
doc: Expand documentation for gcd() and lcm()
Arun Giridhar <arungiridhar@gmail.com>
parents:
31706
diff
changeset
|
479 @example |
e81b372d1203
doc: Expand documentation for gcd() and lcm()
Arun Giridhar <arungiridhar@gmail.com>
parents:
31706
diff
changeset
|
480 @group |
e81b372d1203
doc: Expand documentation for gcd() and lcm()
Arun Giridhar <arungiridhar@gmail.com>
parents:
31706
diff
changeset
|
481 x = [30 42 70 105]; # vector or array of inputs |
e81b372d1203
doc: Expand documentation for gcd() and lcm()
Arun Giridhar <arungiridhar@gmail.com>
parents:
31706
diff
changeset
|
482 gcd (num2cell (x) @{:@}) |
e81b372d1203
doc: Expand documentation for gcd() and lcm()
Arun Giridhar <arungiridhar@gmail.com>
parents:
31706
diff
changeset
|
483 @result{} 1 |
e81b372d1203
doc: Expand documentation for gcd() and lcm()
Arun Giridhar <arungiridhar@gmail.com>
parents:
31706
diff
changeset
|
484 @end group |
e81b372d1203
doc: Expand documentation for gcd() and lcm()
Arun Giridhar <arungiridhar@gmail.com>
parents:
31706
diff
changeset
|
485 @end example |
e81b372d1203
doc: Expand documentation for gcd() and lcm()
Arun Giridhar <arungiridhar@gmail.com>
parents:
31706
diff
changeset
|
486 |
21966
112b20240c87
move docstrings in C++ files out of C strings and into comments
John W. Eaton <jwe@octave.org>
parents:
21942
diff
changeset
|
487 @seealso{lcm, factor, isprime} |
112b20240c87
move docstrings in C++ files out of C strings and into comments
John W. Eaton <jwe@octave.org>
parents:
21942
diff
changeset
|
488 @end deftypefn */) |
4864 | 489 { |
490 int nargin = args.length (); | |
491 | |
20802
8bb38ba1bad6
eliminate return statements after calls to print_usage
John W. Eaton <jwe@octave.org>
parents:
20650
diff
changeset
|
492 if (nargin < 2) |
8bb38ba1bad6
eliminate return statements after calls to print_usage
John W. Eaton <jwe@octave.org>
parents:
20650
diff
changeset
|
493 print_usage (); |
8bb38ba1bad6
eliminate return statements after calls to print_usage
John W. Eaton <jwe@octave.org>
parents:
20650
diff
changeset
|
494 |
20939
b17fda023ca6
maint: Use new C++ archetype in more files.
Rik <rik@octave.org>
parents:
20853
diff
changeset
|
495 octave_value_list retval; |
b17fda023ca6
maint: Use new C++ archetype in more files.
Rik <rik@octave.org>
parents:
20853
diff
changeset
|
496 |
20802
8bb38ba1bad6
eliminate return statements after calls to print_usage
John W. Eaton <jwe@octave.org>
parents:
20650
diff
changeset
|
497 if (nargout > 1) |
4864 | 498 { |
20802
8bb38ba1bad6
eliminate return statements after calls to print_usage
John W. Eaton <jwe@octave.org>
parents:
20650
diff
changeset
|
499 retval.resize (nargin + 1); |
11064 | 500 |
20802
8bb38ba1bad6
eliminate return statements after calls to print_usage
John W. Eaton <jwe@octave.org>
parents:
20650
diff
changeset
|
501 retval(0) = do_extended_gcd (args(0), args(1), retval(1), retval(2)); |
8bb38ba1bad6
eliminate return statements after calls to print_usage
John W. Eaton <jwe@octave.org>
parents:
20650
diff
changeset
|
502 |
8bb38ba1bad6
eliminate return statements after calls to print_usage
John W. Eaton <jwe@octave.org>
parents:
20650
diff
changeset
|
503 for (int j = 2; j < nargin; j++) |
10154
40dfc0c99116
DLD-FUNCTIONS/*.cc: untabify
John W. Eaton <jwe@octave.org>
parents:
9167
diff
changeset
|
504 { |
20802
8bb38ba1bad6
eliminate return statements after calls to print_usage
John W. Eaton <jwe@octave.org>
parents:
20650
diff
changeset
|
505 octave_value x; |
20939
b17fda023ca6
maint: Use new C++ archetype in more files.
Rik <rik@octave.org>
parents:
20853
diff
changeset
|
506 retval(0) = do_extended_gcd (retval(0), args(j), x, retval(j+1)); |
20802
8bb38ba1bad6
eliminate return statements after calls to print_usage
John W. Eaton <jwe@octave.org>
parents:
20650
diff
changeset
|
507 for (int i = 0; i < j; i++) |
8bb38ba1bad6
eliminate return statements after calls to print_usage
John W. Eaton <jwe@octave.org>
parents:
20650
diff
changeset
|
508 retval(i+1).assign (octave_value::op_el_mul_eq, x); |
10154
40dfc0c99116
DLD-FUNCTIONS/*.cc: untabify
John W. Eaton <jwe@octave.org>
parents:
9167
diff
changeset
|
509 } |
7815
a41df65f3f00
Add some single precision test code and fix resulting bugs
David Bateman <dbateman@free.fr>
parents:
7017
diff
changeset
|
510 } |
4864 | 511 else |
20802
8bb38ba1bad6
eliminate return statements after calls to print_usage
John W. Eaton <jwe@octave.org>
parents:
20650
diff
changeset
|
512 { |
8bb38ba1bad6
eliminate return statements after calls to print_usage
John W. Eaton <jwe@octave.org>
parents:
20650
diff
changeset
|
513 retval(0) = do_simple_gcd (args(0), args(1)); |
8bb38ba1bad6
eliminate return statements after calls to print_usage
John W. Eaton <jwe@octave.org>
parents:
20650
diff
changeset
|
514 |
8bb38ba1bad6
eliminate return statements after calls to print_usage
John W. Eaton <jwe@octave.org>
parents:
20650
diff
changeset
|
515 for (int j = 2; j < nargin; j++) |
8bb38ba1bad6
eliminate return statements after calls to print_usage
John W. Eaton <jwe@octave.org>
parents:
20650
diff
changeset
|
516 retval(0) = do_simple_gcd (retval(0), args(j)); |
8bb38ba1bad6
eliminate return statements after calls to print_usage
John W. Eaton <jwe@octave.org>
parents:
20650
diff
changeset
|
517 } |
4864 | 518 |
519 return retval; | |
520 } | |
521 | |
522 /* | |
14501
60e5cf354d80
Update %!tests in DLD-FUNCTIONS/ directory with Octave coding conventions.
Rik <octave@nomad.inbox5.com>
parents:
14360
diff
changeset
|
523 %!assert (gcd (200, 300, 50, 35), 5) |
60e5cf354d80
Update %!tests in DLD-FUNCTIONS/ directory with Octave coding conventions.
Rik <octave@nomad.inbox5.com>
parents:
14360
diff
changeset
|
524 %!assert (gcd (int16 (200), int16 (300), int16 (50), int16 (35)), int16 (5)) |
30346
91c6288781ba
maint: Shorten some long lines in libinterp to <= 80 characters (bug #57599)
Rik <rik@octave.org>
parents:
29961
diff
changeset
|
525 %!assert (gcd (uint64 (200), uint64 (300), uint64 (50), uint64 (35)), |
91c6288781ba
maint: Shorten some long lines in libinterp to <= 80 characters (bug #57599)
Rik <rik@octave.org>
parents:
29961
diff
changeset
|
526 %! uint64 (5)) |
14501
60e5cf354d80
Update %!tests in DLD-FUNCTIONS/ directory with Octave coding conventions.
Rik <octave@nomad.inbox5.com>
parents:
14360
diff
changeset
|
527 %!assert (gcd (18-i, -29+3i), -3-4i) |
7815
a41df65f3f00
Add some single precision test code and fix resulting bugs
David Bateman <dbateman@free.fr>
parents:
7017
diff
changeset
|
528 |
19132
c3bdd640a4dd
* gcd.cc: add more tests
Jordi Gutiérrez Hermoso <jordigh@octave.org>
parents:
18100
diff
changeset
|
529 %!test |
c3bdd640a4dd
* gcd.cc: add more tests
Jordi Gutiérrez Hermoso <jordigh@octave.org>
parents:
18100
diff
changeset
|
530 %! p = [953 967]; |
c3bdd640a4dd
* gcd.cc: add more tests
Jordi Gutiérrez Hermoso <jordigh@octave.org>
parents:
18100
diff
changeset
|
531 %! u = [953 + i*971, 967 + i*977]; |
c3bdd640a4dd
* gcd.cc: add more tests
Jordi Gutiérrez Hermoso <jordigh@octave.org>
parents:
18100
diff
changeset
|
532 %! [d, k(1), k(2)] = gcd (p(1), p(2)); |
c3bdd640a4dd
* gcd.cc: add more tests
Jordi Gutiérrez Hermoso <jordigh@octave.org>
parents:
18100
diff
changeset
|
533 %! [z, w(1), w(2)] = gcd (u(1), u(2)); |
21580
ecce63c99c3f
maint: Add semicolons to terminate code in %! blocks.
Rik <rik@octave.org>
parents:
21301
diff
changeset
|
534 %! assert (d, 1); |
ecce63c99c3f
maint: Add semicolons to terminate code in %! blocks.
Rik <rik@octave.org>
parents:
21301
diff
changeset
|
535 %! assert (sum (p.*k), d); |
ecce63c99c3f
maint: Add semicolons to terminate code in %! blocks.
Rik <rik@octave.org>
parents:
21301
diff
changeset
|
536 %! assert (abs (z), sqrt (2)); |
ecce63c99c3f
maint: Add semicolons to terminate code in %! blocks.
Rik <rik@octave.org>
parents:
21301
diff
changeset
|
537 %! assert (abs (sum (u.*w)), sqrt (2)); |
19132
c3bdd640a4dd
* gcd.cc: add more tests
Jordi Gutiérrez Hermoso <jordigh@octave.org>
parents:
18100
diff
changeset
|
538 |
21581
6fab85c1538f
maint: Follow Octave conventions for use of semicolon in BIST tests.
Rik <rik@octave.org>
parents:
21580
diff
changeset
|
539 %!error <all values must be integers> gcd (1/2, 2) |
6fab85c1538f
maint: Follow Octave conventions for use of semicolon in BIST tests.
Rik <rik@octave.org>
parents:
21580
diff
changeset
|
540 %!error <all complex parts must be integers> gcd (e + i*pi, 1) |
19132
c3bdd640a4dd
* gcd.cc: add more tests
Jordi Gutiérrez Hermoso <jordigh@octave.org>
parents:
18100
diff
changeset
|
541 |
14501
60e5cf354d80
Update %!tests in DLD-FUNCTIONS/ directory with Octave coding conventions.
Rik <octave@nomad.inbox5.com>
parents:
14360
diff
changeset
|
542 %!error gcd () |
7815
a41df65f3f00
Add some single precision test code and fix resulting bugs
David Bateman <dbateman@free.fr>
parents:
7017
diff
changeset
|
543 |
a41df65f3f00
Add some single precision test code and fix resulting bugs
David Bateman <dbateman@free.fr>
parents:
7017
diff
changeset
|
544 %!test |
a41df65f3f00
Add some single precision test code and fix resulting bugs
David Bateman <dbateman@free.fr>
parents:
7017
diff
changeset
|
545 %! s.a = 1; |
14501
60e5cf354d80
Update %!tests in DLD-FUNCTIONS/ directory with Octave coding conventions.
Rik <octave@nomad.inbox5.com>
parents:
14360
diff
changeset
|
546 %! fail ("gcd (s)"); |
4864 | 547 */ |
29958
32c3a5805893
move DEFUN and DEFMETHOD functions inside octave namespace
John W. Eaton <jwe@octave.org>
parents:
29931
diff
changeset
|
548 |
31605
e88a07dec498
maint: Use macros to begin/end C++ namespaces.
Rik <rik@octave.org>
parents:
30991
diff
changeset
|
549 OCTAVE_END_NAMESPACE(octave) |