Mercurial > octave
annotate libinterp/corefcn/gcd.cc @ 29958:32c3a5805893
move DEFUN and DEFMETHOD functions inside octave namespace
* mk-opts.pl: Surround emitted function definitions with
OCTAVE_NAMESPACE_BEGIN and OCTAVE_NAMESPACE_END tags.
* mk-builtins.pl: Surround emitted function declarations with
OCTAVE_NAMESPACE_BEGIN and OCTAVE_NAMESPACE_END tags. Emit deprecated
global inline functions.
* __betainc__.cc, __contourc__.cc, __dsearchn__.cc, __eigs__.cc,
__expint__.cc, __ftp__.cc, __gammainc__.cc, __ichol__.cc, __ilu__.cc,
__lin_interpn__.cc, __magick_read__.cc, __pchip_deriv__.cc, __qp__.cc,
amd.cc, balance.cc, besselj.cc, bitfcns.cc, bsxfun.cc, call-stack.cc,
ccolamd.cc, cellfun.cc, chol.cc, colamd.cc, colloc.cc, conv2.cc,
daspk.cc, dasrt.cc, dassl.cc, data.cc, debug.cc, defaults.cc,
defun.cc, det.cc, dirfns.cc, display.cc, dlmread.cc, dmperm.cc,
dot.cc, eig.cc, ellipj.cc, environment.cc, error.cc, event-manager.cc,
fcn-info.cc, fft.cc, fft2.cc, fftn.cc, file-io.cc, filter.cc, find.cc,
gcd.cc, getgrent.cc, getpwent.cc, getrusage.cc, givens.cc,
graphics.cc, gsvd.cc, hash.cc, help.cc, hess.cc, hex2num.cc, input.cc,
interpreter.cc, inv.cc, jsondecode.cc, jsonencode.cc, kron.cc,
load-path.cc, load-save.cc, lookup.cc, ls-oct-text.cc, lsode.cc,
lu.cc, mappers.cc, matrix_type.cc, max.cc, mgorth.cc, nproc.cc,
oct-hist.cc, ordqz.cc, ordschur.cc, pager.cc, pinv.cc, pr-flt-fmt.cc,
pr-output.cc, psi.cc, qr.cc, quad.cc, quadcc.cc, qz.cc, rand.cc,
rcond.cc, regexp.cc, schur.cc, settings.cc, sighandlers.cc, sparse.cc,
spparms.cc, sqrtm.cc, stream-euler.cc, strfind.cc, strfns.cc,
sub2ind.cc, svd.cc, sylvester.cc, symbfact.cc, symrcm.cc, symtab.cc,
syscalls.cc, sysdep.cc, time.cc, toplev.cc, tril.cc, tsearch.cc,
typecast.cc, urlwrite.cc, utils.cc, variables.cc, __delaunayn__.cc,
__fltk_uigetfile__.cc, __glpk__.cc, __init_gnuplot__.cc, __ode15__.cc,
__voronoi__.cc, audiodevinfo.cc, audioread.cc, convhulln.cc, fftw.cc,
gzip.cc, ov-base.cc, ov-bool-mat.cc, ov-cell.cc, ov-class.cc,
ov-classdef.cc, ov-fcn-handle.cc, ov-java.cc, ov-null-mat.cc,
ov-oncleanup.cc, ov-struct.cc, ov-typeinfo.cc, ov-usr-fcn.cc, ov.cc,
octave.cc, lex.ll, oct-parse.yy, profiler.cc, pt-eval.cc: Surround
DEFUN and DEFMETHOD function defnitions with OCTAVE_NAMESPACE_BEGIN
and OCTAVE_NAMESPACE_END tags.
author | John W. Eaton <jwe@octave.org> |
---|---|
date | Fri, 13 Aug 2021 21:53:51 -0400 |
parents | 7faff48840eb |
children | 7d6709900da7 |
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 // |
29358
0a5b15007766
update Octave Project Developers copyright for the new year
John W. Eaton <jwe@octave.org>
parents:
27954
diff
changeset
|
3 // Copyright (C) 2004-2021 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 |
29958
32c3a5805893
move DEFUN and DEFMETHOD functions inside octave namespace
John W. Eaton <jwe@octave.org>
parents:
29931
diff
changeset
|
41 OCTAVE_NAMESPACE_BEGIN |
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 { |
21782
2aef506f3fec
use namespace for lo-mappers.h functions
John W. Eaton <jwe@octave.org>
parents:
21724
diff
changeset
|
46 if (! octave::math::isinteger (a) || ! octave::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 // Don't use the Complex and FloatComplex typedefs because we need to |
88d78ee12ee8
Implement gcd for Gaussian (complex) integers
Jordi Gutiérrez Hermoso <jordigh@gmail.com>
parents:
11061
diff
changeset
|
63 // refer to the actual float precision FP in the body (and when gcc |
88d78ee12ee8
Implement gcd for Gaussian (complex) integers
Jordi Gutiérrez Hermoso <jordigh@gmail.com>
parents:
11061
diff
changeset
|
64 // implements template aliases from C++0x, can do a small fix here). |
88d78ee12ee8
Implement gcd for Gaussian (complex) integers
Jordi Gutiérrez Hermoso <jordigh@gmail.com>
parents:
11061
diff
changeset
|
65 template <typename FP> |
88d78ee12ee8
Implement gcd for Gaussian (complex) integers
Jordi Gutiérrez Hermoso <jordigh@gmail.com>
parents:
11061
diff
changeset
|
66 static void |
11450
5eb10763069f
substitute and use LAPACK_LIBS in mkoctfile script
John W. Eaton <jwe@octave.org>
parents:
11064
diff
changeset
|
67 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
|
68 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
|
69 { |
21942
aab79a1885cc
limit gnulib headers to liboctave/wrappers directory
John W. Eaton <jwe@octave.org>
parents:
21782
diff
changeset
|
70 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
|
71 FP qi = std::floor ((a/b).imag () + 0.5); |
11064 | 72 |
73 q = std::complex<FP> (qr, qi); | |
74 | |
11062
88d78ee12ee8
Implement gcd for Gaussian (complex) integers
Jordi Gutiérrez Hermoso <jordigh@gmail.com>
parents:
11061
diff
changeset
|
75 r = a - q*b; |
88d78ee12ee8
Implement gcd for Gaussian (complex) integers
Jordi Gutiérrez Hermoso <jordigh@gmail.com>
parents:
11061
diff
changeset
|
76 } |
88d78ee12ee8
Implement gcd for Gaussian (complex) integers
Jordi Gutiérrez Hermoso <jordigh@gmail.com>
parents:
11061
diff
changeset
|
77 |
88d78ee12ee8
Implement gcd for Gaussian (complex) integers
Jordi Gutiérrez Hermoso <jordigh@gmail.com>
parents:
11061
diff
changeset
|
78 template <typename FP> |
88d78ee12ee8
Implement gcd for Gaussian (complex) integers
Jordi Gutiérrez Hermoso <jordigh@gmail.com>
parents:
11061
diff
changeset
|
79 static std::complex<FP> |
88d78ee12ee8
Implement gcd for Gaussian (complex) integers
Jordi Gutiérrez Hermoso <jordigh@gmail.com>
parents:
11061
diff
changeset
|
80 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
|
81 { |
22407
34ce5be04942
maint: Style check C++ code in libinterp/.
Rik <rik@octave.org>
parents:
22327
diff
changeset
|
82 if (! octave::math::isinteger (a.real ()) |
34ce5be04942
maint: Style check C++ code in libinterp/.
Rik <rik@octave.org>
parents:
22327
diff
changeset
|
83 || ! octave::math::isinteger (a.imag ()) |
34ce5be04942
maint: Style check C++ code in libinterp/.
Rik <rik@octave.org>
parents:
22327
diff
changeset
|
84 || ! octave::math::isinteger (b.real ()) |
34ce5be04942
maint: Style check C++ code in libinterp/.
Rik <rik@octave.org>
parents:
22327
diff
changeset
|
85 || ! octave::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
|
86 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
|
87 |
11064 | 88 std::complex<FP> aa = a; |
89 std::complex<FP> bb = b; | |
11062
88d78ee12ee8
Implement gcd for Gaussian (complex) integers
Jordi Gutiérrez Hermoso <jordigh@gmail.com>
parents:
11061
diff
changeset
|
90 |
11064 | 91 if (abs (aa) < abs (bb)) |
92 std::swap (aa, bb); | |
11062
88d78ee12ee8
Implement gcd for Gaussian (complex) integers
Jordi Gutiérrez Hermoso <jordigh@gmail.com>
parents:
11061
diff
changeset
|
93 |
11064 | 94 while (abs (bb) != 0) |
11062
88d78ee12ee8
Implement gcd for Gaussian (complex) integers
Jordi Gutiérrez Hermoso <jordigh@gmail.com>
parents:
11061
diff
changeset
|
95 { |
88d78ee12ee8
Implement gcd for Gaussian (complex) integers
Jordi Gutiérrez Hermoso <jordigh@gmail.com>
parents:
11061
diff
changeset
|
96 std::complex<FP> qq, rr; |
88d78ee12ee8
Implement gcd for Gaussian (complex) integers
Jordi Gutiérrez Hermoso <jordigh@gmail.com>
parents:
11061
diff
changeset
|
97 divide (aa, bb, qq, rr); |
88d78ee12ee8
Implement gcd for Gaussian (complex) integers
Jordi Gutiérrez Hermoso <jordigh@gmail.com>
parents:
11061
diff
changeset
|
98 aa = bb; |
88d78ee12ee8
Implement gcd for Gaussian (complex) integers
Jordi Gutiérrez Hermoso <jordigh@gmail.com>
parents:
11061
diff
changeset
|
99 bb = rr; |
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 |
88d78ee12ee8
Implement gcd for Gaussian (complex) integers
Jordi Gutiérrez Hermoso <jordigh@gmail.com>
parents:
11061
diff
changeset
|
102 return aa; |
88d78ee12ee8
Implement gcd for Gaussian (complex) integers
Jordi Gutiérrez Hermoso <jordigh@gmail.com>
parents:
11061
diff
changeset
|
103 } |
88d78ee12ee8
Implement gcd for Gaussian (complex) integers
Jordi Gutiérrez Hermoso <jordigh@gmail.com>
parents:
11061
diff
changeset
|
104 |
21139
538b57866b90
consistently use "typename" intead of "class" in template declarations
John W. Eaton <jwe@octave.org>
parents:
21100
diff
changeset
|
105 template <typename T> |
11021
0ee74d581c00
optimize & rewrite gcd
Jaroslav Hajek <highegg@gmail.com>
parents:
10840
diff
changeset
|
106 static octave_int<T> |
0ee74d581c00
optimize & rewrite gcd
Jaroslav Hajek <highegg@gmail.com>
parents:
10840
diff
changeset
|
107 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
|
108 { |
11064 | 109 T aa = a.abs ().value (); |
110 T bb = b.abs ().value (); | |
111 | |
11021
0ee74d581c00
optimize & rewrite gcd
Jaroslav Hajek <highegg@gmail.com>
parents:
10840
diff
changeset
|
112 while (bb != 0) |
0ee74d581c00
optimize & rewrite gcd
Jaroslav Hajek <highegg@gmail.com>
parents:
10840
diff
changeset
|
113 { |
0ee74d581c00
optimize & rewrite gcd
Jaroslav Hajek <highegg@gmail.com>
parents:
10840
diff
changeset
|
114 T tt = aa % bb; |
11064 | 115 aa = bb; |
116 bb = tt; | |
11021
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 |
0ee74d581c00
optimize & rewrite gcd
Jaroslav Hajek <highegg@gmail.com>
parents:
10840
diff
changeset
|
119 return aa; |
0ee74d581c00
optimize & rewrite gcd
Jaroslav Hajek <highegg@gmail.com>
parents:
10840
diff
changeset
|
120 } |
0ee74d581c00
optimize & rewrite gcd
Jaroslav Hajek <highegg@gmail.com>
parents:
10840
diff
changeset
|
121 |
11062
88d78ee12ee8
Implement gcd for Gaussian (complex) integers
Jordi Gutiérrez Hermoso <jordigh@gmail.com>
parents:
11061
diff
changeset
|
122 static double |
11021
0ee74d581c00
optimize & rewrite gcd
Jaroslav Hajek <highegg@gmail.com>
parents:
10840
diff
changeset
|
123 extended_gcd (double a, double b, double& x, double& y) |
0ee74d581c00
optimize & rewrite gcd
Jaroslav Hajek <highegg@gmail.com>
parents:
10840
diff
changeset
|
124 { |
21782
2aef506f3fec
use namespace for lo-mappers.h functions
John W. Eaton <jwe@octave.org>
parents:
21724
diff
changeset
|
125 if (! octave::math::isinteger (a) || ! octave::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
|
126 error ("gcd: all values must be integers"); |
11021
0ee74d581c00
optimize & rewrite gcd
Jaroslav Hajek <highegg@gmail.com>
parents:
10840
diff
changeset
|
127 |
11064 | 128 double aa = fabs (a); |
129 double bb = fabs (b); | |
130 | |
18100
6a71e5030df5
Follow coding convention of defining and initializing only 1 variable per line in liboctinterp.
Rik <rik@octave.org>
parents:
17787
diff
changeset
|
131 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
|
132 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
|
133 yy = 1, ly = 0; |
11064 | 134 |
11021
0ee74d581c00
optimize & rewrite gcd
Jaroslav Hajek <highegg@gmail.com>
parents:
10840
diff
changeset
|
135 while (bb != 0) |
0ee74d581c00
optimize & rewrite gcd
Jaroslav Hajek <highegg@gmail.com>
parents:
10840
diff
changeset
|
136 { |
21942
aab79a1885cc
limit gnulib headers to liboctave/wrappers directory
John W. Eaton <jwe@octave.org>
parents:
21782
diff
changeset
|
137 double qq = std::floor (aa / bb); |
11021
0ee74d581c00
optimize & rewrite gcd
Jaroslav Hajek <highegg@gmail.com>
parents:
10840
diff
changeset
|
138 double tt = fmod (aa, bb); |
11064 | 139 |
140 aa = bb; | |
141 bb = tt; | |
11021
0ee74d581c00
optimize & rewrite gcd
Jaroslav Hajek <highegg@gmail.com>
parents:
10840
diff
changeset
|
142 |
0ee74d581c00
optimize & rewrite gcd
Jaroslav Hajek <highegg@gmail.com>
parents:
10840
diff
changeset
|
143 double tx = lx - qq*xx; |
11064 | 144 lx = xx; |
145 xx = tx; | |
11021
0ee74d581c00
optimize & rewrite gcd
Jaroslav Hajek <highegg@gmail.com>
parents:
10840
diff
changeset
|
146 |
0ee74d581c00
optimize & rewrite gcd
Jaroslav Hajek <highegg@gmail.com>
parents:
10840
diff
changeset
|
147 double ty = ly - qq*yy; |
11064 | 148 ly = yy; |
149 yy = ty; | |
11021
0ee74d581c00
optimize & rewrite gcd
Jaroslav Hajek <highegg@gmail.com>
parents:
10840
diff
changeset
|
150 } |
4864 | 151 |
23450
855122b993da
maint: Wrap tertiary operator in parentheses "(COND ? x : y)".
Rik <rik@octave.org>
parents:
23220
diff
changeset
|
152 x = (a >= 0 ? lx : -lx); |
855122b993da
maint: Wrap tertiary operator in parentheses "(COND ? x : y)".
Rik <rik@octave.org>
parents:
23220
diff
changeset
|
153 y = (b >= 0 ? ly : -ly); |
11021
0ee74d581c00
optimize & rewrite gcd
Jaroslav Hajek <highegg@gmail.com>
parents:
10840
diff
changeset
|
154 |
0ee74d581c00
optimize & rewrite gcd
Jaroslav Hajek <highegg@gmail.com>
parents:
10840
diff
changeset
|
155 return aa; |
0ee74d581c00
optimize & rewrite gcd
Jaroslav Hajek <highegg@gmail.com>
parents:
10840
diff
changeset
|
156 } |
0ee74d581c00
optimize & rewrite gcd
Jaroslav Hajek <highegg@gmail.com>
parents:
10840
diff
changeset
|
157 |
11062
88d78ee12ee8
Implement gcd for Gaussian (complex) integers
Jordi Gutiérrez Hermoso <jordigh@gmail.com>
parents:
11061
diff
changeset
|
158 template <typename FP> |
88d78ee12ee8
Implement gcd for Gaussian (complex) integers
Jordi Gutiérrez Hermoso <jordigh@gmail.com>
parents:
11061
diff
changeset
|
159 static std::complex<FP> |
88d78ee12ee8
Implement gcd for Gaussian (complex) integers
Jordi Gutiérrez Hermoso <jordigh@gmail.com>
parents:
11061
diff
changeset
|
160 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
|
161 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
|
162 { |
22407
34ce5be04942
maint: Style check C++ code in libinterp/.
Rik <rik@octave.org>
parents:
22327
diff
changeset
|
163 if (! octave::math::isinteger (a.real ()) |
34ce5be04942
maint: Style check C++ code in libinterp/.
Rik <rik@octave.org>
parents:
22327
diff
changeset
|
164 || ! octave::math::isinteger (a.imag ()) |
34ce5be04942
maint: Style check C++ code in libinterp/.
Rik <rik@octave.org>
parents:
22327
diff
changeset
|
165 || ! octave::math::isinteger (b.real ()) |
34ce5be04942
maint: Style check C++ code in libinterp/.
Rik <rik@octave.org>
parents:
22327
diff
changeset
|
166 || ! octave::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
|
167 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
|
168 |
18100
6a71e5030df5
Follow coding convention of defining and initializing only 1 variable per line in liboctinterp.
Rik <rik@octave.org>
parents:
17787
diff
changeset
|
169 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
|
170 std::complex<FP> bb = b; |
11062
88d78ee12ee8
Implement gcd for Gaussian (complex) integers
Jordi Gutiérrez Hermoso <jordigh@gmail.com>
parents:
11061
diff
changeset
|
171 bool swapped = false; |
11064 | 172 if (abs (aa) < abs (bb)) |
11062
88d78ee12ee8
Implement gcd for Gaussian (complex) integers
Jordi Gutiérrez Hermoso <jordigh@gmail.com>
parents:
11061
diff
changeset
|
173 { |
11064 | 174 std::swap (aa, bb); |
11062
88d78ee12ee8
Implement gcd for Gaussian (complex) integers
Jordi Gutiérrez Hermoso <jordigh@gmail.com>
parents:
11061
diff
changeset
|
175 swapped = true; |
88d78ee12ee8
Implement gcd for Gaussian (complex) integers
Jordi Gutiérrez Hermoso <jordigh@gmail.com>
parents:
11061
diff
changeset
|
176 } |
88d78ee12ee8
Implement gcd for Gaussian (complex) integers
Jordi Gutiérrez Hermoso <jordigh@gmail.com>
parents:
11061
diff
changeset
|
177 |
18100
6a71e5030df5
Follow coding convention of defining and initializing only 1 variable per line in liboctinterp.
Rik <rik@octave.org>
parents:
17787
diff
changeset
|
178 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
|
179 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
|
180 yy = 1, ly = 0; |
11064 | 181 |
182 while (abs(bb) != 0) | |
11062
88d78ee12ee8
Implement gcd for Gaussian (complex) integers
Jordi Gutiérrez Hermoso <jordigh@gmail.com>
parents:
11061
diff
changeset
|
183 { |
88d78ee12ee8
Implement gcd for Gaussian (complex) integers
Jordi Gutiérrez Hermoso <jordigh@gmail.com>
parents:
11061
diff
changeset
|
184 std::complex<FP> qq, rr; |
88d78ee12ee8
Implement gcd for Gaussian (complex) integers
Jordi Gutiérrez Hermoso <jordigh@gmail.com>
parents:
11061
diff
changeset
|
185 divide (aa, bb, qq, rr); |
11064 | 186 aa = bb; |
187 bb = rr; | |
11062
88d78ee12ee8
Implement gcd for Gaussian (complex) integers
Jordi Gutiérrez Hermoso <jordigh@gmail.com>
parents:
11061
diff
changeset
|
188 |
88d78ee12ee8
Implement gcd for Gaussian (complex) integers
Jordi Gutiérrez Hermoso <jordigh@gmail.com>
parents:
11061
diff
changeset
|
189 std::complex<FP> tx = lx - qq*xx; |
11064 | 190 lx = xx; |
191 xx = tx; | |
11062
88d78ee12ee8
Implement gcd for Gaussian (complex) integers
Jordi Gutiérrez Hermoso <jordigh@gmail.com>
parents:
11061
diff
changeset
|
192 |
88d78ee12ee8
Implement gcd for Gaussian (complex) integers
Jordi Gutiérrez Hermoso <jordigh@gmail.com>
parents:
11061
diff
changeset
|
193 std::complex<FP> ty = ly - qq*yy; |
11064 | 194 ly = yy; |
195 yy = ty; | |
11062
88d78ee12ee8
Implement gcd for Gaussian (complex) integers
Jordi Gutiérrez Hermoso <jordigh@gmail.com>
parents:
11061
diff
changeset
|
196 } |
88d78ee12ee8
Implement gcd for Gaussian (complex) integers
Jordi Gutiérrez Hermoso <jordigh@gmail.com>
parents:
11061
diff
changeset
|
197 |
88d78ee12ee8
Implement gcd for Gaussian (complex) integers
Jordi Gutiérrez Hermoso <jordigh@gmail.com>
parents:
11061
diff
changeset
|
198 x = lx; |
88d78ee12ee8
Implement gcd for Gaussian (complex) integers
Jordi Gutiérrez Hermoso <jordigh@gmail.com>
parents:
11061
diff
changeset
|
199 y = ly; |
11064 | 200 |
11062
88d78ee12ee8
Implement gcd for Gaussian (complex) integers
Jordi Gutiérrez Hermoso <jordigh@gmail.com>
parents:
11061
diff
changeset
|
201 if (swapped) |
11064 | 202 std::swap (x, y); |
11062
88d78ee12ee8
Implement gcd for Gaussian (complex) integers
Jordi Gutiérrez Hermoso <jordigh@gmail.com>
parents:
11061
diff
changeset
|
203 |
88d78ee12ee8
Implement gcd for Gaussian (complex) integers
Jordi Gutiérrez Hermoso <jordigh@gmail.com>
parents:
11061
diff
changeset
|
204 return aa; |
88d78ee12ee8
Implement gcd for Gaussian (complex) integers
Jordi Gutiérrez Hermoso <jordigh@gmail.com>
parents:
11061
diff
changeset
|
205 } |
88d78ee12ee8
Implement gcd for Gaussian (complex) integers
Jordi Gutiérrez Hermoso <jordigh@gmail.com>
parents:
11061
diff
changeset
|
206 |
21139
538b57866b90
consistently use "typename" intead of "class" in template declarations
John W. Eaton <jwe@octave.org>
parents:
21100
diff
changeset
|
207 template <typename T> |
11021
0ee74d581c00
optimize & rewrite gcd
Jaroslav Hajek <highegg@gmail.com>
parents:
10840
diff
changeset
|
208 static octave_int<T> |
0ee74d581c00
optimize & rewrite gcd
Jaroslav Hajek <highegg@gmail.com>
parents:
10840
diff
changeset
|
209 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
|
210 octave_int<T>& x, octave_int<T>& y) |
4864 | 211 { |
11064 | 212 T aa = a.abs ().value (); |
213 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
|
214 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
|
215 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
|
216 yy = 1, ly = 0; |
11064 | 217 |
11021
0ee74d581c00
optimize & rewrite gcd
Jaroslav Hajek <highegg@gmail.com>
parents:
10840
diff
changeset
|
218 while (bb != 0) |
0ee74d581c00
optimize & rewrite gcd
Jaroslav Hajek <highegg@gmail.com>
parents:
10840
diff
changeset
|
219 { |
0ee74d581c00
optimize & rewrite gcd
Jaroslav Hajek <highegg@gmail.com>
parents:
10840
diff
changeset
|
220 T qq = aa / bb; |
0ee74d581c00
optimize & rewrite gcd
Jaroslav Hajek <highegg@gmail.com>
parents:
10840
diff
changeset
|
221 T tt = aa % bb; |
11064 | 222 aa = bb; |
223 bb = tt; | |
11021
0ee74d581c00
optimize & rewrite gcd
Jaroslav Hajek <highegg@gmail.com>
parents:
10840
diff
changeset
|
224 |
0ee74d581c00
optimize & rewrite gcd
Jaroslav Hajek <highegg@gmail.com>
parents:
10840
diff
changeset
|
225 T tx = lx - qq*xx; |
11064 | 226 lx = xx; |
227 xx = tx; | |
11021
0ee74d581c00
optimize & rewrite gcd
Jaroslav Hajek <highegg@gmail.com>
parents:
10840
diff
changeset
|
228 |
0ee74d581c00
optimize & rewrite gcd
Jaroslav Hajek <highegg@gmail.com>
parents:
10840
diff
changeset
|
229 T ty = ly - qq*yy; |
11064 | 230 ly = yy; |
231 yy = ty; | |
11021
0ee74d581c00
optimize & rewrite gcd
Jaroslav Hajek <highegg@gmail.com>
parents:
10840
diff
changeset
|
232 } |
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 x = octave_int<T> (lx) * a.signum (); |
0ee74d581c00
optimize & rewrite gcd
Jaroslav Hajek <highegg@gmail.com>
parents:
10840
diff
changeset
|
235 y = octave_int<T> (ly) * b.signum (); |
0ee74d581c00
optimize & rewrite gcd
Jaroslav Hajek <highegg@gmail.com>
parents:
10840
diff
changeset
|
236 |
0ee74d581c00
optimize & rewrite gcd
Jaroslav Hajek <highegg@gmail.com>
parents:
10840
diff
changeset
|
237 return aa; |
0ee74d581c00
optimize & rewrite gcd
Jaroslav Hajek <highegg@gmail.com>
parents:
10840
diff
changeset
|
238 } |
0ee74d581c00
optimize & rewrite gcd
Jaroslav Hajek <highegg@gmail.com>
parents:
10840
diff
changeset
|
239 |
21139
538b57866b90
consistently use "typename" intead of "class" in template declarations
John W. Eaton <jwe@octave.org>
parents:
21100
diff
changeset
|
240 template <typename NDA> |
11021
0ee74d581c00
optimize & rewrite gcd
Jaroslav Hajek <highegg@gmail.com>
parents:
10840
diff
changeset
|
241 static octave_value |
0ee74d581c00
optimize & rewrite gcd
Jaroslav Hajek <highegg@gmail.com>
parents:
10840
diff
changeset
|
242 do_simple_gcd (const octave_value& a, const octave_value& b) |
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 typedef typename NDA::element_type T; |
0ee74d581c00
optimize & rewrite gcd
Jaroslav Hajek <highegg@gmail.com>
parents:
10840
diff
changeset
|
245 octave_value retval; |
0ee74d581c00
optimize & rewrite gcd
Jaroslav Hajek <highegg@gmail.com>
parents:
10840
diff
changeset
|
246 |
0ee74d581c00
optimize & rewrite gcd
Jaroslav Hajek <highegg@gmail.com>
parents:
10840
diff
changeset
|
247 if (a.is_scalar_type () && b.is_scalar_type ()) |
0ee74d581c00
optimize & rewrite gcd
Jaroslav Hajek <highegg@gmail.com>
parents:
10840
diff
changeset
|
248 { |
0ee74d581c00
optimize & rewrite gcd
Jaroslav Hajek <highegg@gmail.com>
parents:
10840
diff
changeset
|
249 // Optimize scalar case. |
0ee74d581c00
optimize & rewrite gcd
Jaroslav Hajek <highegg@gmail.com>
parents:
10840
diff
changeset
|
250 T aa = octave_value_extract<T> (a); |
0ee74d581c00
optimize & rewrite gcd
Jaroslav Hajek <highegg@gmail.com>
parents:
10840
diff
changeset
|
251 T bb = octave_value_extract<T> (b); |
0ee74d581c00
optimize & rewrite gcd
Jaroslav Hajek <highegg@gmail.com>
parents:
10840
diff
changeset
|
252 retval = simple_gcd (aa, bb); |
0ee74d581c00
optimize & rewrite gcd
Jaroslav Hajek <highegg@gmail.com>
parents:
10840
diff
changeset
|
253 } |
0ee74d581c00
optimize & rewrite gcd
Jaroslav Hajek <highegg@gmail.com>
parents:
10840
diff
changeset
|
254 else |
0ee74d581c00
optimize & rewrite gcd
Jaroslav Hajek <highegg@gmail.com>
parents:
10840
diff
changeset
|
255 { |
0ee74d581c00
optimize & rewrite gcd
Jaroslav Hajek <highegg@gmail.com>
parents:
10840
diff
changeset
|
256 NDA aa = octave_value_extract<NDA> (a); |
0ee74d581c00
optimize & rewrite gcd
Jaroslav Hajek <highegg@gmail.com>
parents:
10840
diff
changeset
|
257 NDA bb = octave_value_extract<NDA> (b); |
0ee74d581c00
optimize & rewrite gcd
Jaroslav Hajek <highegg@gmail.com>
parents:
10840
diff
changeset
|
258 retval = binmap<T> (aa, bb, simple_gcd, "gcd"); |
0ee74d581c00
optimize & rewrite gcd
Jaroslav Hajek <highegg@gmail.com>
parents:
10840
diff
changeset
|
259 } |
0ee74d581c00
optimize & rewrite gcd
Jaroslav Hajek <highegg@gmail.com>
parents:
10840
diff
changeset
|
260 |
0ee74d581c00
optimize & rewrite gcd
Jaroslav Hajek <highegg@gmail.com>
parents:
10840
diff
changeset
|
261 return retval; |
4864 | 262 } |
263 | |
11021
0ee74d581c00
optimize & rewrite gcd
Jaroslav Hajek <highegg@gmail.com>
parents:
10840
diff
changeset
|
264 // Dispatcher |
0ee74d581c00
optimize & rewrite gcd
Jaroslav Hajek <highegg@gmail.com>
parents:
10840
diff
changeset
|
265 static octave_value |
0ee74d581c00
optimize & rewrite gcd
Jaroslav Hajek <highegg@gmail.com>
parents:
10840
diff
changeset
|
266 do_simple_gcd (const octave_value& a, const octave_value& b) |
0ee74d581c00
optimize & rewrite gcd
Jaroslav Hajek <highegg@gmail.com>
parents:
10840
diff
changeset
|
267 { |
0ee74d581c00
optimize & rewrite gcd
Jaroslav Hajek <highegg@gmail.com>
parents:
10840
diff
changeset
|
268 octave_value retval; |
0ee74d581c00
optimize & rewrite gcd
Jaroslav Hajek <highegg@gmail.com>
parents:
10840
diff
changeset
|
269 builtin_type_t btyp = btyp_mixed_numeric (a.builtin_type (), |
0ee74d581c00
optimize & rewrite gcd
Jaroslav Hajek <highegg@gmail.com>
parents:
10840
diff
changeset
|
270 b.builtin_type ()); |
0ee74d581c00
optimize & rewrite gcd
Jaroslav Hajek <highegg@gmail.com>
parents:
10840
diff
changeset
|
271 switch (btyp) |
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 case btyp_double: |
23583
b7747a2c88b2
maint: Deprecate is_sparse_type and replace with issparse.
Rik <rik@octave.org>
parents:
23450
diff
changeset
|
274 if (a.issparse () && b.issparse ()) |
11021
0ee74d581c00
optimize & rewrite gcd
Jaroslav Hajek <highegg@gmail.com>
parents:
10840
diff
changeset
|
275 { |
0ee74d581c00
optimize & rewrite gcd
Jaroslav Hajek <highegg@gmail.com>
parents:
10840
diff
changeset
|
276 retval = do_simple_gcd<SparseMatrix> (a, b); |
0ee74d581c00
optimize & rewrite gcd
Jaroslav Hajek <highegg@gmail.com>
parents:
10840
diff
changeset
|
277 break; |
0ee74d581c00
optimize & rewrite gcd
Jaroslav Hajek <highegg@gmail.com>
parents:
10840
diff
changeset
|
278 } |
23826
d69021d58a61
avoid fallthrough warnings
John W. Eaton <jwe@octave.org>
parents:
23583
diff
changeset
|
279 OCTAVE_FALLTHROUGH; |
11064 | 280 |
11021
0ee74d581c00
optimize & rewrite gcd
Jaroslav Hajek <highegg@gmail.com>
parents:
10840
diff
changeset
|
281 case btyp_float: |
0ee74d581c00
optimize & rewrite gcd
Jaroslav Hajek <highegg@gmail.com>
parents:
10840
diff
changeset
|
282 retval = do_simple_gcd<NDArray> (a, b); |
0ee74d581c00
optimize & rewrite gcd
Jaroslav Hajek <highegg@gmail.com>
parents:
10840
diff
changeset
|
283 break; |
11064 | 284 |
22407
34ce5be04942
maint: Style check C++ code in libinterp/.
Rik <rik@octave.org>
parents:
22327
diff
changeset
|
285 #define MAKE_INT_BRANCH(X) \ |
34ce5be04942
maint: Style check C++ code in libinterp/.
Rik <rik@octave.org>
parents:
22327
diff
changeset
|
286 case btyp_ ## X: \ |
34ce5be04942
maint: Style check C++ code in libinterp/.
Rik <rik@octave.org>
parents:
22327
diff
changeset
|
287 retval = do_simple_gcd<X ## NDArray> (a, b); \ |
34ce5be04942
maint: Style check C++ code in libinterp/.
Rik <rik@octave.org>
parents:
22327
diff
changeset
|
288 break |
11064 | 289 |
11021
0ee74d581c00
optimize & rewrite gcd
Jaroslav Hajek <highegg@gmail.com>
parents:
10840
diff
changeset
|
290 MAKE_INT_BRANCH (int8); |
0ee74d581c00
optimize & rewrite gcd
Jaroslav Hajek <highegg@gmail.com>
parents:
10840
diff
changeset
|
291 MAKE_INT_BRANCH (int16); |
0ee74d581c00
optimize & rewrite gcd
Jaroslav Hajek <highegg@gmail.com>
parents:
10840
diff
changeset
|
292 MAKE_INT_BRANCH (int32); |
0ee74d581c00
optimize & rewrite gcd
Jaroslav Hajek <highegg@gmail.com>
parents:
10840
diff
changeset
|
293 MAKE_INT_BRANCH (int64); |
0ee74d581c00
optimize & rewrite gcd
Jaroslav Hajek <highegg@gmail.com>
parents:
10840
diff
changeset
|
294 MAKE_INT_BRANCH (uint8); |
0ee74d581c00
optimize & rewrite gcd
Jaroslav Hajek <highegg@gmail.com>
parents:
10840
diff
changeset
|
295 MAKE_INT_BRANCH (uint16); |
0ee74d581c00
optimize & rewrite gcd
Jaroslav Hajek <highegg@gmail.com>
parents:
10840
diff
changeset
|
296 MAKE_INT_BRANCH (uint32); |
0ee74d581c00
optimize & rewrite gcd
Jaroslav Hajek <highegg@gmail.com>
parents:
10840
diff
changeset
|
297 MAKE_INT_BRANCH (uint64); |
11064 | 298 |
11021
0ee74d581c00
optimize & rewrite gcd
Jaroslav Hajek <highegg@gmail.com>
parents:
10840
diff
changeset
|
299 #undef MAKE_INT_BRANCH |
11064 | 300 |
11021
0ee74d581c00
optimize & rewrite gcd
Jaroslav Hajek <highegg@gmail.com>
parents:
10840
diff
changeset
|
301 case btyp_complex: |
11064 | 302 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
|
303 break; |
11064 | 304 |
11021
0ee74d581c00
optimize & rewrite gcd
Jaroslav Hajek <highegg@gmail.com>
parents:
10840
diff
changeset
|
305 case btyp_float_complex: |
11064 | 306 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
|
307 break; |
11064 | 308 |
11021
0ee74d581c00
optimize & rewrite gcd
Jaroslav Hajek <highegg@gmail.com>
parents:
10840
diff
changeset
|
309 default: |
0ee74d581c00
optimize & rewrite gcd
Jaroslav Hajek <highegg@gmail.com>
parents:
10840
diff
changeset
|
310 error ("gcd: invalid class combination for gcd: %s and %s\n", |
0ee74d581c00
optimize & rewrite gcd
Jaroslav Hajek <highegg@gmail.com>
parents:
10840
diff
changeset
|
311 a.class_name ().c_str (), b.class_name ().c_str ()); |
0ee74d581c00
optimize & rewrite gcd
Jaroslav Hajek <highegg@gmail.com>
parents:
10840
diff
changeset
|
312 } |
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 if (btyp == btyp_float) |
0ee74d581c00
optimize & rewrite gcd
Jaroslav Hajek <highegg@gmail.com>
parents:
10840
diff
changeset
|
315 retval = retval.float_array_value (); |
0ee74d581c00
optimize & rewrite gcd
Jaroslav Hajek <highegg@gmail.com>
parents:
10840
diff
changeset
|
316 |
0ee74d581c00
optimize & rewrite gcd
Jaroslav Hajek <highegg@gmail.com>
parents:
10840
diff
changeset
|
317 return retval; |
0ee74d581c00
optimize & rewrite gcd
Jaroslav Hajek <highegg@gmail.com>
parents:
10840
diff
changeset
|
318 } |
0ee74d581c00
optimize & rewrite gcd
Jaroslav Hajek <highegg@gmail.com>
parents:
10840
diff
changeset
|
319 |
21139
538b57866b90
consistently use "typename" intead of "class" in template declarations
John W. Eaton <jwe@octave.org>
parents:
21100
diff
changeset
|
320 template <typename NDA> |
11021
0ee74d581c00
optimize & rewrite gcd
Jaroslav Hajek <highegg@gmail.com>
parents:
10840
diff
changeset
|
321 static octave_value |
0ee74d581c00
optimize & rewrite gcd
Jaroslav Hajek <highegg@gmail.com>
parents:
10840
diff
changeset
|
322 do_extended_gcd (const octave_value& a, const octave_value& b, |
0ee74d581c00
optimize & rewrite gcd
Jaroslav Hajek <highegg@gmail.com>
parents:
10840
diff
changeset
|
323 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
|
324 { |
11021
0ee74d581c00
optimize & rewrite gcd
Jaroslav Hajek <highegg@gmail.com>
parents:
10840
diff
changeset
|
325 typedef typename NDA::element_type T; |
0ee74d581c00
optimize & rewrite gcd
Jaroslav Hajek <highegg@gmail.com>
parents:
10840
diff
changeset
|
326 octave_value retval; |
0ee74d581c00
optimize & rewrite gcd
Jaroslav Hajek <highegg@gmail.com>
parents:
10840
diff
changeset
|
327 |
0ee74d581c00
optimize & rewrite gcd
Jaroslav Hajek <highegg@gmail.com>
parents:
10840
diff
changeset
|
328 if (a.is_scalar_type () && b.is_scalar_type ()) |
0ee74d581c00
optimize & rewrite gcd
Jaroslav Hajek <highegg@gmail.com>
parents:
10840
diff
changeset
|
329 { |
0ee74d581c00
optimize & rewrite gcd
Jaroslav Hajek <highegg@gmail.com>
parents:
10840
diff
changeset
|
330 // Optimize scalar case. |
0ee74d581c00
optimize & rewrite gcd
Jaroslav Hajek <highegg@gmail.com>
parents:
10840
diff
changeset
|
331 T aa = octave_value_extract<T> (a); |
0ee74d581c00
optimize & rewrite gcd
Jaroslav Hajek <highegg@gmail.com>
parents:
10840
diff
changeset
|
332 T bb = octave_value_extract<T> (b); |
0ee74d581c00
optimize & rewrite gcd
Jaroslav Hajek <highegg@gmail.com>
parents:
10840
diff
changeset
|
333 T xx, yy; |
0ee74d581c00
optimize & rewrite gcd
Jaroslav Hajek <highegg@gmail.com>
parents:
10840
diff
changeset
|
334 retval = extended_gcd (aa, bb, xx, yy); |
0ee74d581c00
optimize & rewrite gcd
Jaroslav Hajek <highegg@gmail.com>
parents:
10840
diff
changeset
|
335 x = xx; |
0ee74d581c00
optimize & rewrite gcd
Jaroslav Hajek <highegg@gmail.com>
parents:
10840
diff
changeset
|
336 y = yy; |
0ee74d581c00
optimize & rewrite gcd
Jaroslav Hajek <highegg@gmail.com>
parents:
10840
diff
changeset
|
337 } |
0ee74d581c00
optimize & rewrite gcd
Jaroslav Hajek <highegg@gmail.com>
parents:
10840
diff
changeset
|
338 else |
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 NDA aa = octave_value_extract<NDA> (a); |
0ee74d581c00
optimize & rewrite gcd
Jaroslav Hajek <highegg@gmail.com>
parents:
10840
diff
changeset
|
341 NDA bb = octave_value_extract<NDA> (b); |
0ee74d581c00
optimize & rewrite gcd
Jaroslav Hajek <highegg@gmail.com>
parents:
10840
diff
changeset
|
342 |
0ee74d581c00
optimize & rewrite gcd
Jaroslav Hajek <highegg@gmail.com>
parents:
10840
diff
changeset
|
343 dim_vector dv = aa.dims (); |
0ee74d581c00
optimize & rewrite gcd
Jaroslav Hajek <highegg@gmail.com>
parents:
10840
diff
changeset
|
344 if (aa.numel () == 1) |
0ee74d581c00
optimize & rewrite gcd
Jaroslav Hajek <highegg@gmail.com>
parents:
10840
diff
changeset
|
345 dv = bb.dims (); |
0ee74d581c00
optimize & rewrite gcd
Jaroslav Hajek <highegg@gmail.com>
parents:
10840
diff
changeset
|
346 else if (bb.numel () != 1 && bb.dims () != dv) |
22327
d0562b3159c7
move more classes inside octave namespace
John W. Eaton <jwe@octave.org>
parents:
22323
diff
changeset
|
347 octave::err_nonconformant ("gcd", a.dims (), b.dims ()); |
11021
0ee74d581c00
optimize & rewrite gcd
Jaroslav Hajek <highegg@gmail.com>
parents:
10840
diff
changeset
|
348 |
0ee74d581c00
optimize & rewrite gcd
Jaroslav Hajek <highegg@gmail.com>
parents:
10840
diff
changeset
|
349 NDA gg (dv), xx (dv), yy (dv); |
0ee74d581c00
optimize & rewrite gcd
Jaroslav Hajek <highegg@gmail.com>
parents:
10840
diff
changeset
|
350 |
29931
7faff48840eb
prefer data over fortran_vec for read-only access to data
John W. Eaton <jwe@octave.org>
parents:
29359
diff
changeset
|
351 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
|
352 const T *bptr = bb.data (); |
11021
0ee74d581c00
optimize & rewrite gcd
Jaroslav Hajek <highegg@gmail.com>
parents:
10840
diff
changeset
|
353 |
11064 | 354 bool inca = aa.numel () != 1; |
355 bool incb = bb.numel () != 1; | |
11021
0ee74d581c00
optimize & rewrite gcd
Jaroslav Hajek <highegg@gmail.com>
parents:
10840
diff
changeset
|
356 |
0ee74d581c00
optimize & rewrite gcd
Jaroslav Hajek <highegg@gmail.com>
parents:
10840
diff
changeset
|
357 T *gptr = gg.fortran_vec (); |
18100
6a71e5030df5
Follow coding convention of defining and initializing only 1 variable per line in liboctinterp.
Rik <rik@octave.org>
parents:
17787
diff
changeset
|
358 T *xptr = xx.fortran_vec (); |
6a71e5030df5
Follow coding convention of defining and initializing only 1 variable per line in liboctinterp.
Rik <rik@octave.org>
parents:
17787
diff
changeset
|
359 T *yptr = yy.fortran_vec (); |
11021
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_idx_type n = gg.numel (); |
0ee74d581c00
optimize & rewrite gcd
Jaroslav Hajek <highegg@gmail.com>
parents:
10840
diff
changeset
|
362 for (octave_idx_type i = 0; i < n; i++) |
0ee74d581c00
optimize & rewrite gcd
Jaroslav Hajek <highegg@gmail.com>
parents:
10840
diff
changeset
|
363 { |
0ee74d581c00
optimize & rewrite gcd
Jaroslav Hajek <highegg@gmail.com>
parents:
10840
diff
changeset
|
364 octave_quit (); |
0ee74d581c00
optimize & rewrite gcd
Jaroslav Hajek <highegg@gmail.com>
parents:
10840
diff
changeset
|
365 |
0ee74d581c00
optimize & rewrite gcd
Jaroslav Hajek <highegg@gmail.com>
parents:
10840
diff
changeset
|
366 *gptr++ = extended_gcd (*aptr, *bptr, *xptr++, *yptr++); |
11064 | 367 |
11021
0ee74d581c00
optimize & rewrite gcd
Jaroslav Hajek <highegg@gmail.com>
parents:
10840
diff
changeset
|
368 aptr += inca; |
0ee74d581c00
optimize & rewrite gcd
Jaroslav Hajek <highegg@gmail.com>
parents:
10840
diff
changeset
|
369 bptr += incb; |
0ee74d581c00
optimize & rewrite gcd
Jaroslav Hajek <highegg@gmail.com>
parents:
10840
diff
changeset
|
370 } |
0ee74d581c00
optimize & rewrite gcd
Jaroslav Hajek <highegg@gmail.com>
parents:
10840
diff
changeset
|
371 |
0ee74d581c00
optimize & rewrite gcd
Jaroslav Hajek <highegg@gmail.com>
parents:
10840
diff
changeset
|
372 x = xx; |
0ee74d581c00
optimize & rewrite gcd
Jaroslav Hajek <highegg@gmail.com>
parents:
10840
diff
changeset
|
373 y = yy; |
11064 | 374 |
11021
0ee74d581c00
optimize & rewrite gcd
Jaroslav Hajek <highegg@gmail.com>
parents:
10840
diff
changeset
|
375 retval = gg; |
0ee74d581c00
optimize & rewrite gcd
Jaroslav Hajek <highegg@gmail.com>
parents:
10840
diff
changeset
|
376 } |
0ee74d581c00
optimize & rewrite gcd
Jaroslav Hajek <highegg@gmail.com>
parents:
10840
diff
changeset
|
377 |
0ee74d581c00
optimize & rewrite gcd
Jaroslav Hajek <highegg@gmail.com>
parents:
10840
diff
changeset
|
378 return retval; |
7815
a41df65f3f00
Add some single precision test code and fix resulting bugs
David Bateman <dbateman@free.fr>
parents:
7017
diff
changeset
|
379 } |
a41df65f3f00
Add some single precision test code and fix resulting bugs
David Bateman <dbateman@free.fr>
parents:
7017
diff
changeset
|
380 |
11021
0ee74d581c00
optimize & rewrite gcd
Jaroslav Hajek <highegg@gmail.com>
parents:
10840
diff
changeset
|
381 // Dispatcher |
0ee74d581c00
optimize & rewrite gcd
Jaroslav Hajek <highegg@gmail.com>
parents:
10840
diff
changeset
|
382 static octave_value |
0ee74d581c00
optimize & rewrite gcd
Jaroslav Hajek <highegg@gmail.com>
parents:
10840
diff
changeset
|
383 do_extended_gcd (const octave_value& a, const octave_value& b, |
0ee74d581c00
optimize & rewrite gcd
Jaroslav Hajek <highegg@gmail.com>
parents:
10840
diff
changeset
|
384 octave_value& x, octave_value& y) |
0ee74d581c00
optimize & rewrite gcd
Jaroslav Hajek <highegg@gmail.com>
parents:
10840
diff
changeset
|
385 { |
0ee74d581c00
optimize & rewrite gcd
Jaroslav Hajek <highegg@gmail.com>
parents:
10840
diff
changeset
|
386 octave_value retval; |
11064 | 387 |
11021
0ee74d581c00
optimize & rewrite gcd
Jaroslav Hajek <highegg@gmail.com>
parents:
10840
diff
changeset
|
388 builtin_type_t btyp = btyp_mixed_numeric (a.builtin_type (), |
0ee74d581c00
optimize & rewrite gcd
Jaroslav Hajek <highegg@gmail.com>
parents:
10840
diff
changeset
|
389 b.builtin_type ()); |
0ee74d581c00
optimize & rewrite gcd
Jaroslav Hajek <highegg@gmail.com>
parents:
10840
diff
changeset
|
390 switch (btyp) |
0ee74d581c00
optimize & rewrite gcd
Jaroslav Hajek <highegg@gmail.com>
parents:
10840
diff
changeset
|
391 { |
0ee74d581c00
optimize & rewrite gcd
Jaroslav Hajek <highegg@gmail.com>
parents:
10840
diff
changeset
|
392 case btyp_double: |
0ee74d581c00
optimize & rewrite gcd
Jaroslav Hajek <highegg@gmail.com>
parents:
10840
diff
changeset
|
393 case btyp_float: |
0ee74d581c00
optimize & rewrite gcd
Jaroslav Hajek <highegg@gmail.com>
parents:
10840
diff
changeset
|
394 retval = do_extended_gcd<NDArray> (a, b, x, y); |
0ee74d581c00
optimize & rewrite gcd
Jaroslav Hajek <highegg@gmail.com>
parents:
10840
diff
changeset
|
395 break; |
11064 | 396 |
22407
34ce5be04942
maint: Style check C++ code in libinterp/.
Rik <rik@octave.org>
parents:
22327
diff
changeset
|
397 #define MAKE_INT_BRANCH(X) \ |
34ce5be04942
maint: Style check C++ code in libinterp/.
Rik <rik@octave.org>
parents:
22327
diff
changeset
|
398 case btyp_ ## X: \ |
34ce5be04942
maint: Style check C++ code in libinterp/.
Rik <rik@octave.org>
parents:
22327
diff
changeset
|
399 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
|
400 break |
11064 | 401 |
11021
0ee74d581c00
optimize & rewrite gcd
Jaroslav Hajek <highegg@gmail.com>
parents:
10840
diff
changeset
|
402 MAKE_INT_BRANCH (int8); |
0ee74d581c00
optimize & rewrite gcd
Jaroslav Hajek <highegg@gmail.com>
parents:
10840
diff
changeset
|
403 MAKE_INT_BRANCH (int16); |
0ee74d581c00
optimize & rewrite gcd
Jaroslav Hajek <highegg@gmail.com>
parents:
10840
diff
changeset
|
404 MAKE_INT_BRANCH (int32); |
0ee74d581c00
optimize & rewrite gcd
Jaroslav Hajek <highegg@gmail.com>
parents:
10840
diff
changeset
|
405 MAKE_INT_BRANCH (int64); |
0ee74d581c00
optimize & rewrite gcd
Jaroslav Hajek <highegg@gmail.com>
parents:
10840
diff
changeset
|
406 MAKE_INT_BRANCH (uint8); |
0ee74d581c00
optimize & rewrite gcd
Jaroslav Hajek <highegg@gmail.com>
parents:
10840
diff
changeset
|
407 MAKE_INT_BRANCH (uint16); |
0ee74d581c00
optimize & rewrite gcd
Jaroslav Hajek <highegg@gmail.com>
parents:
10840
diff
changeset
|
408 MAKE_INT_BRANCH (uint32); |
0ee74d581c00
optimize & rewrite gcd
Jaroslav Hajek <highegg@gmail.com>
parents:
10840
diff
changeset
|
409 MAKE_INT_BRANCH (uint64); |
11064 | 410 |
11021
0ee74d581c00
optimize & rewrite gcd
Jaroslav Hajek <highegg@gmail.com>
parents:
10840
diff
changeset
|
411 #undef MAKE_INT_BRANCH |
11064 | 412 |
11021
0ee74d581c00
optimize & rewrite gcd
Jaroslav Hajek <highegg@gmail.com>
parents:
10840
diff
changeset
|
413 case btyp_complex: |
11062
88d78ee12ee8
Implement gcd for Gaussian (complex) integers
Jordi Gutiérrez Hermoso <jordigh@gmail.com>
parents:
11061
diff
changeset
|
414 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
|
415 break; |
11064 | 416 |
11021
0ee74d581c00
optimize & rewrite gcd
Jaroslav Hajek <highegg@gmail.com>
parents:
10840
diff
changeset
|
417 case btyp_float_complex: |
11062
88d78ee12ee8
Implement gcd for Gaussian (complex) integers
Jordi Gutiérrez Hermoso <jordigh@gmail.com>
parents:
11061
diff
changeset
|
418 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
|
419 break; |
11064 | 420 |
11021
0ee74d581c00
optimize & rewrite gcd
Jaroslav Hajek <highegg@gmail.com>
parents:
10840
diff
changeset
|
421 default: |
0ee74d581c00
optimize & rewrite gcd
Jaroslav Hajek <highegg@gmail.com>
parents:
10840
diff
changeset
|
422 error ("gcd: invalid class combination for gcd: %s and %s\n", |
0ee74d581c00
optimize & rewrite gcd
Jaroslav Hajek <highegg@gmail.com>
parents:
10840
diff
changeset
|
423 a.class_name ().c_str (), b.class_name ().c_str ()); |
0ee74d581c00
optimize & rewrite gcd
Jaroslav Hajek <highegg@gmail.com>
parents:
10840
diff
changeset
|
424 } |
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 // For consistency. |
23583
b7747a2c88b2
maint: Deprecate is_sparse_type and replace with issparse.
Rik <rik@octave.org>
parents:
23450
diff
changeset
|
427 if (a.issparse () && b.issparse ()) |
11021
0ee74d581c00
optimize & rewrite gcd
Jaroslav Hajek <highegg@gmail.com>
parents:
10840
diff
changeset
|
428 { |
0ee74d581c00
optimize & rewrite gcd
Jaroslav Hajek <highegg@gmail.com>
parents:
10840
diff
changeset
|
429 retval = retval.sparse_matrix_value (); |
0ee74d581c00
optimize & rewrite gcd
Jaroslav Hajek <highegg@gmail.com>
parents:
10840
diff
changeset
|
430 x = x.sparse_matrix_value (); |
0ee74d581c00
optimize & rewrite gcd
Jaroslav Hajek <highegg@gmail.com>
parents:
10840
diff
changeset
|
431 y = y.sparse_matrix_value (); |
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 |
0ee74d581c00
optimize & rewrite gcd
Jaroslav Hajek <highegg@gmail.com>
parents:
10840
diff
changeset
|
434 if (btyp == btyp_float) |
0ee74d581c00
optimize & rewrite gcd
Jaroslav Hajek <highegg@gmail.com>
parents:
10840
diff
changeset
|
435 { |
0ee74d581c00
optimize & rewrite gcd
Jaroslav Hajek <highegg@gmail.com>
parents:
10840
diff
changeset
|
436 retval = retval.float_array_value (); |
0ee74d581c00
optimize & rewrite gcd
Jaroslav Hajek <highegg@gmail.com>
parents:
10840
diff
changeset
|
437 x = x.float_array_value (); |
0ee74d581c00
optimize & rewrite gcd
Jaroslav Hajek <highegg@gmail.com>
parents:
10840
diff
changeset
|
438 y = y.float_array_value (); |
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 |
0ee74d581c00
optimize & rewrite gcd
Jaroslav Hajek <highegg@gmail.com>
parents:
10840
diff
changeset
|
441 return retval; |
0ee74d581c00
optimize & rewrite gcd
Jaroslav Hajek <highegg@gmail.com>
parents:
10840
diff
changeset
|
442 } |
0ee74d581c00
optimize & rewrite gcd
Jaroslav Hajek <highegg@gmail.com>
parents:
10840
diff
changeset
|
443 |
15039
e753177cde93
maint: Move non-dynamically linked functions from DLD-FUNCTIONS/ to corefcn/ directory
Rik <rik@octave.org>
parents:
14854
diff
changeset
|
444 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
|
445 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
|
446 @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
|
447 @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
|
448 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
|
449 |
27954
cbabcd7bb6c3
doc: Clarify documentation of inputs for gcd (bug #57609).
Rik <rik@octave.org>
parents:
27923
diff
changeset
|
450 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
|
451 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
|
452 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
|
453 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
|
454 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
|
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 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
|
457 that, |
112b20240c87
move docstrings in C++ files out of C strings and into comments
John W. Eaton <jwe@octave.org>
parents:
21942
diff
changeset
|
458 |
112b20240c87
move docstrings in C++ files out of C strings and into comments
John W. Eaton <jwe@octave.org>
parents:
21942
diff
changeset
|
459 @tex |
112b20240c87
move docstrings in C++ files out of C strings and into comments
John W. Eaton <jwe@octave.org>
parents:
21942
diff
changeset
|
460 $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
|
461 @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
|
462 @ifnottex |
112b20240c87
move docstrings in C++ files out of C strings and into comments
John W. Eaton <jwe@octave.org>
parents:
21942
diff
changeset
|
463 |
112b20240c87
move docstrings in C++ files out of C strings and into comments
John W. Eaton <jwe@octave.org>
parents:
21942
diff
changeset
|
464 @example |
112b20240c87
move docstrings in C++ files out of C strings and into comments
John W. Eaton <jwe@octave.org>
parents:
21942
diff
changeset
|
465 @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
|
466 @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
|
467 |
112b20240c87
move docstrings in C++ files out of C strings and into comments
John W. Eaton <jwe@octave.org>
parents:
21942
diff
changeset
|
468 @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
|
469 |
112b20240c87
move docstrings in C++ files out of C strings and into comments
John W. Eaton <jwe@octave.org>
parents:
21942
diff
changeset
|
470 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
|
471 |
112b20240c87
move docstrings in C++ files out of C strings and into comments
John W. Eaton <jwe@octave.org>
parents:
21942
diff
changeset
|
472 @example |
112b20240c87
move docstrings in C++ files out of C strings and into comments
John W. Eaton <jwe@octave.org>
parents:
21942
diff
changeset
|
473 @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 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
|
475 @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
|
476 @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
|
477 @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
|
478 |
112b20240c87
move docstrings in C++ files out of C strings and into comments
John W. Eaton <jwe@octave.org>
parents:
21942
diff
changeset
|
479 @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
|
480 @end deftypefn */) |
4864 | 481 { |
482 int nargin = args.length (); | |
483 | |
20802
8bb38ba1bad6
eliminate return statements after calls to print_usage
John W. Eaton <jwe@octave.org>
parents:
20650
diff
changeset
|
484 if (nargin < 2) |
8bb38ba1bad6
eliminate return statements after calls to print_usage
John W. Eaton <jwe@octave.org>
parents:
20650
diff
changeset
|
485 print_usage (); |
8bb38ba1bad6
eliminate return statements after calls to print_usage
John W. Eaton <jwe@octave.org>
parents:
20650
diff
changeset
|
486 |
20939
b17fda023ca6
maint: Use new C++ archetype in more files.
Rik <rik@octave.org>
parents:
20853
diff
changeset
|
487 octave_value_list retval; |
b17fda023ca6
maint: Use new C++ archetype in more files.
Rik <rik@octave.org>
parents:
20853
diff
changeset
|
488 |
20802
8bb38ba1bad6
eliminate return statements after calls to print_usage
John W. Eaton <jwe@octave.org>
parents:
20650
diff
changeset
|
489 if (nargout > 1) |
4864 | 490 { |
20802
8bb38ba1bad6
eliminate return statements after calls to print_usage
John W. Eaton <jwe@octave.org>
parents:
20650
diff
changeset
|
491 retval.resize (nargin + 1); |
11064 | 492 |
20802
8bb38ba1bad6
eliminate return statements after calls to print_usage
John W. Eaton <jwe@octave.org>
parents:
20650
diff
changeset
|
493 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
|
494 |
8bb38ba1bad6
eliminate return statements after calls to print_usage
John W. Eaton <jwe@octave.org>
parents:
20650
diff
changeset
|
495 for (int j = 2; j < nargin; j++) |
10154
40dfc0c99116
DLD-FUNCTIONS/*.cc: untabify
John W. Eaton <jwe@octave.org>
parents:
9167
diff
changeset
|
496 { |
20802
8bb38ba1bad6
eliminate return statements after calls to print_usage
John W. Eaton <jwe@octave.org>
parents:
20650
diff
changeset
|
497 octave_value x; |
20939
b17fda023ca6
maint: Use new C++ archetype in more files.
Rik <rik@octave.org>
parents:
20853
diff
changeset
|
498 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
|
499 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
|
500 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
|
501 } |
7815
a41df65f3f00
Add some single precision test code and fix resulting bugs
David Bateman <dbateman@free.fr>
parents:
7017
diff
changeset
|
502 } |
4864 | 503 else |
20802
8bb38ba1bad6
eliminate return statements after calls to print_usage
John W. Eaton <jwe@octave.org>
parents:
20650
diff
changeset
|
504 { |
8bb38ba1bad6
eliminate return statements after calls to print_usage
John W. Eaton <jwe@octave.org>
parents:
20650
diff
changeset
|
505 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
|
506 |
8bb38ba1bad6
eliminate return statements after calls to print_usage
John W. Eaton <jwe@octave.org>
parents:
20650
diff
changeset
|
507 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
|
508 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
|
509 } |
4864 | 510 |
511 return retval; | |
512 } | |
513 | |
514 /* | |
14501
60e5cf354d80
Update %!tests in DLD-FUNCTIONS/ directory with Octave coding conventions.
Rik <octave@nomad.inbox5.com>
parents:
14360
diff
changeset
|
515 %!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
|
516 %!assert (gcd (int16 (200), int16 (300), int16 (50), int16 (35)), int16 (5)) |
60e5cf354d80
Update %!tests in DLD-FUNCTIONS/ directory with Octave coding conventions.
Rik <octave@nomad.inbox5.com>
parents:
14360
diff
changeset
|
517 %!assert (gcd (uint64 (200), uint64 (300), uint64 (50), uint64 (35)), uint64 (5)) |
60e5cf354d80
Update %!tests in DLD-FUNCTIONS/ directory with Octave coding conventions.
Rik <octave@nomad.inbox5.com>
parents:
14360
diff
changeset
|
518 %!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
|
519 |
19132
c3bdd640a4dd
* gcd.cc: add more tests
Jordi Gutiérrez Hermoso <jordigh@octave.org>
parents:
18100
diff
changeset
|
520 %!test |
c3bdd640a4dd
* gcd.cc: add more tests
Jordi Gutiérrez Hermoso <jordigh@octave.org>
parents:
18100
diff
changeset
|
521 %! p = [953 967]; |
c3bdd640a4dd
* gcd.cc: add more tests
Jordi Gutiérrez Hermoso <jordigh@octave.org>
parents:
18100
diff
changeset
|
522 %! u = [953 + i*971, 967 + i*977]; |
c3bdd640a4dd
* gcd.cc: add more tests
Jordi Gutiérrez Hermoso <jordigh@octave.org>
parents:
18100
diff
changeset
|
523 %! [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
|
524 %! [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
|
525 %! assert (d, 1); |
ecce63c99c3f
maint: Add semicolons to terminate code in %! blocks.
Rik <rik@octave.org>
parents:
21301
diff
changeset
|
526 %! assert (sum (p.*k), d); |
ecce63c99c3f
maint: Add semicolons to terminate code in %! blocks.
Rik <rik@octave.org>
parents:
21301
diff
changeset
|
527 %! assert (abs (z), sqrt (2)); |
ecce63c99c3f
maint: Add semicolons to terminate code in %! blocks.
Rik <rik@octave.org>
parents:
21301
diff
changeset
|
528 %! 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
|
529 |
21581
6fab85c1538f
maint: Follow Octave conventions for use of semicolon in BIST tests.
Rik <rik@octave.org>
parents:
21580
diff
changeset
|
530 %!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
|
531 %!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
|
532 |
14501
60e5cf354d80
Update %!tests in DLD-FUNCTIONS/ directory with Octave coding conventions.
Rik <octave@nomad.inbox5.com>
parents:
14360
diff
changeset
|
533 %!error gcd () |
7815
a41df65f3f00
Add some single precision test code and fix resulting bugs
David Bateman <dbateman@free.fr>
parents:
7017
diff
changeset
|
534 |
a41df65f3f00
Add some single precision test code and fix resulting bugs
David Bateman <dbateman@free.fr>
parents:
7017
diff
changeset
|
535 %!test |
a41df65f3f00
Add some single precision test code and fix resulting bugs
David Bateman <dbateman@free.fr>
parents:
7017
diff
changeset
|
536 %! s.a = 1; |
14501
60e5cf354d80
Update %!tests in DLD-FUNCTIONS/ directory with Octave coding conventions.
Rik <octave@nomad.inbox5.com>
parents:
14360
diff
changeset
|
537 %! fail ("gcd (s)"); |
4864 | 538 */ |
29958
32c3a5805893
move DEFUN and DEFMETHOD functions inside octave namespace
John W. Eaton <jwe@octave.org>
parents:
29931
diff
changeset
|
539 |
32c3a5805893
move DEFUN and DEFMETHOD functions inside octave namespace
John W. Eaton <jwe@octave.org>
parents:
29931
diff
changeset
|
540 OCTAVE_NAMESPACE_END |