Mercurial > octave
annotate libinterp/corefcn/gcd.cc @ 22197:e43d83253e28
refill multi-line macro definitions
Use the Emacs C++ mode style for line continuation markers in
multi-line macro definitions.
* make_int.cc, __dsearchn__.cc, __magick_read__.cc, besselj.cc,
bitfcns.cc, bsxfun.cc, cellfun.cc, data.cc, defun-dld.h, defun-int.h,
defun.h, det.cc, error.h, find.cc, gcd.cc, graphics.cc, interpreter.h,
jit-ir.h, jit-typeinfo.h, lookup.cc, ls-mat5.cc, max.cc, mexproto.h,
mxarray.in.h, oct-stream.cc, ordschur.cc, pr-output.cc, profiler.h,
psi.cc, regexp.cc, sparse-xdiv.cc, sparse-xpow.cc, tril.cc, txt-eng.h,
utils.cc, variables.cc, variables.h, xdiv.cc, xpow.cc, __glpk__.cc,
ov-base.cc, ov-base.h, ov-cell.cc, ov-ch-mat.cc, ov-classdef.cc,
ov-complex.cc, ov-cx-mat.cc, ov-cx-sparse.cc, ov-float.cc, ov-float.h,
ov-flt-complex.cc, ov-flt-cx-mat.cc, ov-flt-re-mat.cc,
ov-int-traits.h, ov-lazy-idx.h, ov-perm.cc, ov-re-mat.cc,
ov-re-sparse.cc, ov-scalar.cc, ov-scalar.h, ov-str-mat.cc,
ov-type-conv.h, ov.cc, ov.h, op-class.cc, op-int-conv.cc, op-int.h,
op-str-str.cc, ops.h, lex.ll, Array.cc, CMatrix.cc, CSparse.cc,
MArray.cc, MArray.h, MDiagArray2.cc, MDiagArray2.h, MSparse.h,
Sparse.cc, dMatrix.cc, dSparse.cc, fCMatrix.cc, fMatrix.cc,
idx-vector.cc, f77-fcn.h, quit.h, bsxfun-decl.h, bsxfun-defs.cc,
lo-specfun.cc, oct-convn.cc, oct-convn.h, oct-norm.cc, oct-norm.h,
oct-rand.cc, Sparse-op-decls.h, Sparse-op-defs.h, mx-inlines.cc,
mx-op-decl.h, mx-op-defs.h, mach-info.cc, oct-group.cc, oct-passwd.cc,
oct-syscalls.cc, oct-time.cc, data-conv.cc, kpse.cc, lo-ieee.h,
lo-macros.h, oct-cmplx.h, oct-glob.cc, oct-inttypes.cc,
oct-inttypes.h, oct-locbuf.h, oct-sparse.h, url-transfer.cc,
oct-conf-post.in.h, shared-fcns.h: Refill macro definitions.
author | John W. Eaton <jwe@octave.org> |
---|---|
date | Mon, 01 Aug 2016 12:40:18 -0400 |
parents | 112b20240c87 |
children | bac0d6f07a3e |
rev | line source |
---|---|
4864 | 1 /* |
2 | |
19697
4197fc428c7d
maint: Update copyright notices for 2015.
John W. Eaton <jwe@octave.org>
parents:
19149
diff
changeset
|
3 Copyright (C) 2004-2015 David Bateman |
11062
88d78ee12ee8
Implement gcd for Gaussian (complex) integers
Jordi Gutiérrez Hermoso <jordigh@gmail.com>
parents:
11061
diff
changeset
|
4 Copyright (C) 2010 Jaroslav Hajek, Jordi GutiƩrrez Hermoso |
4864 | 5 |
7016 | 6 This file is part of Octave. |
4864 | 7 |
7016 | 8 Octave is free software; you can redistribute it and/or modify it |
9 under the terms of the GNU General Public License as published by the | |
10 Free Software Foundation; either version 3 of the License, or (at your | |
11 option) any later version. | |
12 | |
13 Octave is distributed in the hope that it will be useful, but WITHOUT | |
14 ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or | |
15 FITNESS FOR A PARTICULAR PURPOSE. See the GNU General Public License | |
16 for more details. | |
4864 | 17 |
18 You should have received a copy of the GNU General Public License | |
7016 | 19 along with Octave; see the file COPYING. If not, see |
20 <http://www.gnu.org/licenses/>. | |
4864 | 21 |
22 */ | |
23 | |
21724
aba2e6293dd8
use "#if ..." consistently instead of "#ifdef" and "#ifndef"
John W. Eaton <jwe@octave.org>
parents:
21581
diff
changeset
|
24 #if defined (HAVE_CONFIG_H) |
21301
40de9f8f23a6
Use '#include "config.h"' rather than <config.h>.
Rik <rik@octave.org>
parents:
21200
diff
changeset
|
25 # include "config.h" |
4864 | 26 #endif |
27 | |
28 #include "dNDArray.h" | |
29 #include "CNDArray.h" | |
7815
a41df65f3f00
Add some single precision test code and fix resulting bugs
David Bateman <dbateman@free.fr>
parents:
7017
diff
changeset
|
30 #include "fNDArray.h" |
a41df65f3f00
Add some single precision test code and fix resulting bugs
David Bateman <dbateman@free.fr>
parents:
7017
diff
changeset
|
31 #include "fCNDArray.h" |
4864 | 32 #include "lo-mappers.h" |
11021
0ee74d581c00
optimize & rewrite gcd
Jaroslav Hajek <highegg@gmail.com>
parents:
10840
diff
changeset
|
33 #include "oct-binmap.h" |
4864 | 34 |
15039
e753177cde93
maint: Move non-dynamically linked functions from DLD-FUNCTIONS/ to corefcn/ directory
Rik <rik@octave.org>
parents:
14854
diff
changeset
|
35 #include "defun.h" |
4864 | 36 #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
|
37 #include "ovl.h" |
4864 | 38 |
11062
88d78ee12ee8
Implement gcd for Gaussian (complex) integers
Jordi Gutiérrez Hermoso <jordigh@gmail.com>
parents:
11061
diff
changeset
|
39 static double |
11021
0ee74d581c00
optimize & rewrite gcd
Jaroslav Hajek <highegg@gmail.com>
parents:
10840
diff
changeset
|
40 simple_gcd (double a, double b) |
0ee74d581c00
optimize & rewrite gcd
Jaroslav Hajek <highegg@gmail.com>
parents:
10840
diff
changeset
|
41 { |
21782
2aef506f3fec
use namespace for lo-mappers.h functions
John W. Eaton <jwe@octave.org>
parents:
21724
diff
changeset
|
42 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
|
43 error ("gcd: all values must be integers"); |
11021
0ee74d581c00
optimize & rewrite gcd
Jaroslav Hajek <highegg@gmail.com>
parents:
10840
diff
changeset
|
44 |
11064 | 45 double aa = fabs (a); |
46 double bb = fabs (b); | |
47 | |
11021
0ee74d581c00
optimize & rewrite gcd
Jaroslav Hajek <highegg@gmail.com>
parents:
10840
diff
changeset
|
48 while (bb != 0) |
0ee74d581c00
optimize & rewrite gcd
Jaroslav Hajek <highegg@gmail.com>
parents:
10840
diff
changeset
|
49 { |
0ee74d581c00
optimize & rewrite gcd
Jaroslav Hajek <highegg@gmail.com>
parents:
10840
diff
changeset
|
50 double tt = fmod (aa, bb); |
11064 | 51 aa = bb; |
52 bb = tt; | |
11021
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 |
0ee74d581c00
optimize & rewrite gcd
Jaroslav Hajek <highegg@gmail.com>
parents:
10840
diff
changeset
|
55 return aa; |
0ee74d581c00
optimize & rewrite gcd
Jaroslav Hajek <highegg@gmail.com>
parents:
10840
diff
changeset
|
56 } |
0ee74d581c00
optimize & rewrite gcd
Jaroslav Hajek <highegg@gmail.com>
parents:
10840
diff
changeset
|
57 |
11062
88d78ee12ee8
Implement gcd for Gaussian (complex) integers
Jordi Gutiérrez Hermoso <jordigh@gmail.com>
parents:
11061
diff
changeset
|
58 // 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
|
59 // 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
|
60 // 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
|
61 template <typename FP> |
88d78ee12ee8
Implement gcd for Gaussian (complex) integers
Jordi Gutiérrez Hermoso <jordigh@gmail.com>
parents:
11061
diff
changeset
|
62 static void |
11450
5eb10763069f
substitute and use LAPACK_LIBS in mkoctfile script
John W. Eaton <jwe@octave.org>
parents:
11064
diff
changeset
|
63 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
|
64 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
|
65 { |
21942
aab79a1885cc
limit gnulib headers to liboctave/wrappers directory
John W. Eaton <jwe@octave.org>
parents:
21782
diff
changeset
|
66 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
|
67 FP qi = std::floor ((a/b).imag () + 0.5); |
11064 | 68 |
69 q = std::complex<FP> (qr, qi); | |
70 | |
11062
88d78ee12ee8
Implement gcd for Gaussian (complex) integers
Jordi Gutiérrez Hermoso <jordigh@gmail.com>
parents:
11061
diff
changeset
|
71 r = a - q*b; |
88d78ee12ee8
Implement gcd for Gaussian (complex) integers
Jordi Gutiérrez Hermoso <jordigh@gmail.com>
parents:
11061
diff
changeset
|
72 } |
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 template <typename FP> |
88d78ee12ee8
Implement gcd for Gaussian (complex) integers
Jordi Gutiérrez Hermoso <jordigh@gmail.com>
parents:
11061
diff
changeset
|
75 static std::complex<FP> |
88d78ee12ee8
Implement gcd for Gaussian (complex) integers
Jordi Gutiérrez Hermoso <jordigh@gmail.com>
parents:
11061
diff
changeset
|
76 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
|
77 { |
21782
2aef506f3fec
use namespace for lo-mappers.h functions
John W. Eaton <jwe@octave.org>
parents:
21724
diff
changeset
|
78 if (! octave::math::isinteger (a.real ()) || ! octave::math::isinteger (a.imag ()) |
2aef506f3fec
use namespace for lo-mappers.h functions
John W. Eaton <jwe@octave.org>
parents:
21724
diff
changeset
|
79 || ! octave::math::isinteger (b.real ()) || ! 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
|
80 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
|
81 |
11064 | 82 std::complex<FP> aa = a; |
83 std::complex<FP> bb = b; | |
11062
88d78ee12ee8
Implement gcd for Gaussian (complex) integers
Jordi Gutiérrez Hermoso <jordigh@gmail.com>
parents:
11061
diff
changeset
|
84 |
11064 | 85 if (abs (aa) < abs (bb)) |
86 std::swap (aa, bb); | |
11062
88d78ee12ee8
Implement gcd for Gaussian (complex) integers
Jordi Gutiérrez Hermoso <jordigh@gmail.com>
parents:
11061
diff
changeset
|
87 |
11064 | 88 while (abs (bb) != 0) |
11062
88d78ee12ee8
Implement gcd for Gaussian (complex) integers
Jordi Gutiérrez Hermoso <jordigh@gmail.com>
parents:
11061
diff
changeset
|
89 { |
88d78ee12ee8
Implement gcd for Gaussian (complex) integers
Jordi Gutiérrez Hermoso <jordigh@gmail.com>
parents:
11061
diff
changeset
|
90 std::complex<FP> qq, rr; |
88d78ee12ee8
Implement gcd for Gaussian (complex) integers
Jordi Gutiérrez Hermoso <jordigh@gmail.com>
parents:
11061
diff
changeset
|
91 divide (aa, bb, qq, rr); |
88d78ee12ee8
Implement gcd for Gaussian (complex) integers
Jordi Gutiérrez Hermoso <jordigh@gmail.com>
parents:
11061
diff
changeset
|
92 aa = bb; |
88d78ee12ee8
Implement gcd for Gaussian (complex) integers
Jordi Gutiérrez Hermoso <jordigh@gmail.com>
parents:
11061
diff
changeset
|
93 bb = rr; |
88d78ee12ee8
Implement gcd for Gaussian (complex) integers
Jordi Gutiérrez Hermoso <jordigh@gmail.com>
parents:
11061
diff
changeset
|
94 } |
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 return aa; |
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 |
21139
538b57866b90
consistently use "typename" intead of "class" in template declarations
John W. Eaton <jwe@octave.org>
parents:
21100
diff
changeset
|
99 template <typename T> |
11021
0ee74d581c00
optimize & rewrite gcd
Jaroslav Hajek <highegg@gmail.com>
parents:
10840
diff
changeset
|
100 static octave_int<T> |
0ee74d581c00
optimize & rewrite gcd
Jaroslav Hajek <highegg@gmail.com>
parents:
10840
diff
changeset
|
101 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
|
102 { |
11064 | 103 T aa = a.abs ().value (); |
104 T bb = b.abs ().value (); | |
105 | |
11021
0ee74d581c00
optimize & rewrite gcd
Jaroslav Hajek <highegg@gmail.com>
parents:
10840
diff
changeset
|
106 while (bb != 0) |
0ee74d581c00
optimize & rewrite gcd
Jaroslav Hajek <highegg@gmail.com>
parents:
10840
diff
changeset
|
107 { |
0ee74d581c00
optimize & rewrite gcd
Jaroslav Hajek <highegg@gmail.com>
parents:
10840
diff
changeset
|
108 T tt = aa % bb; |
11064 | 109 aa = bb; |
110 bb = tt; | |
11021
0ee74d581c00
optimize & rewrite gcd
Jaroslav Hajek <highegg@gmail.com>
parents:
10840
diff
changeset
|
111 } |
0ee74d581c00
optimize & rewrite gcd
Jaroslav Hajek <highegg@gmail.com>
parents:
10840
diff
changeset
|
112 |
0ee74d581c00
optimize & rewrite gcd
Jaroslav Hajek <highegg@gmail.com>
parents:
10840
diff
changeset
|
113 return aa; |
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 |
11062
88d78ee12ee8
Implement gcd for Gaussian (complex) integers
Jordi Gutiérrez Hermoso <jordigh@gmail.com>
parents:
11061
diff
changeset
|
116 static double |
11021
0ee74d581c00
optimize & rewrite gcd
Jaroslav Hajek <highegg@gmail.com>
parents:
10840
diff
changeset
|
117 extended_gcd (double a, double b, double& x, double& y) |
0ee74d581c00
optimize & rewrite gcd
Jaroslav Hajek <highegg@gmail.com>
parents:
10840
diff
changeset
|
118 { |
21782
2aef506f3fec
use namespace for lo-mappers.h functions
John W. Eaton <jwe@octave.org>
parents:
21724
diff
changeset
|
119 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
|
120 error ("gcd: all values must be integers"); |
11021
0ee74d581c00
optimize & rewrite gcd
Jaroslav Hajek <highegg@gmail.com>
parents:
10840
diff
changeset
|
121 |
11064 | 122 double aa = fabs (a); |
123 double bb = fabs (b); | |
124 | |
18100
6a71e5030df5
Follow coding convention of defining and initializing only 1 variable per line in liboctinterp.
Rik <rik@octave.org>
parents:
17787
diff
changeset
|
125 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
|
126 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
|
127 yy = 1, ly = 0; |
11064 | 128 |
11021
0ee74d581c00
optimize & rewrite gcd
Jaroslav Hajek <highegg@gmail.com>
parents:
10840
diff
changeset
|
129 while (bb != 0) |
0ee74d581c00
optimize & rewrite gcd
Jaroslav Hajek <highegg@gmail.com>
parents:
10840
diff
changeset
|
130 { |
21942
aab79a1885cc
limit gnulib headers to liboctave/wrappers directory
John W. Eaton <jwe@octave.org>
parents:
21782
diff
changeset
|
131 double qq = std::floor (aa / bb); |
11021
0ee74d581c00
optimize & rewrite gcd
Jaroslav Hajek <highegg@gmail.com>
parents:
10840
diff
changeset
|
132 double tt = fmod (aa, bb); |
11064 | 133 |
134 aa = bb; | |
135 bb = tt; | |
11021
0ee74d581c00
optimize & rewrite gcd
Jaroslav Hajek <highegg@gmail.com>
parents:
10840
diff
changeset
|
136 |
0ee74d581c00
optimize & rewrite gcd
Jaroslav Hajek <highegg@gmail.com>
parents:
10840
diff
changeset
|
137 double tx = lx - qq*xx; |
11064 | 138 lx = xx; |
139 xx = tx; | |
11021
0ee74d581c00
optimize & rewrite gcd
Jaroslav Hajek <highegg@gmail.com>
parents:
10840
diff
changeset
|
140 |
0ee74d581c00
optimize & rewrite gcd
Jaroslav Hajek <highegg@gmail.com>
parents:
10840
diff
changeset
|
141 double ty = ly - qq*yy; |
11064 | 142 ly = yy; |
143 yy = ty; | |
11021
0ee74d581c00
optimize & rewrite gcd
Jaroslav Hajek <highegg@gmail.com>
parents:
10840
diff
changeset
|
144 } |
4864 | 145 |
11062
88d78ee12ee8
Implement gcd for Gaussian (complex) integers
Jordi Gutiérrez Hermoso <jordigh@gmail.com>
parents:
11061
diff
changeset
|
146 x = a >= 0 ? lx : -lx; |
11021
0ee74d581c00
optimize & rewrite gcd
Jaroslav Hajek <highegg@gmail.com>
parents:
10840
diff
changeset
|
147 y = b >= 0 ? ly : -ly; |
0ee74d581c00
optimize & rewrite gcd
Jaroslav Hajek <highegg@gmail.com>
parents:
10840
diff
changeset
|
148 |
0ee74d581c00
optimize & rewrite gcd
Jaroslav Hajek <highegg@gmail.com>
parents:
10840
diff
changeset
|
149 return aa; |
0ee74d581c00
optimize & rewrite gcd
Jaroslav Hajek <highegg@gmail.com>
parents:
10840
diff
changeset
|
150 } |
0ee74d581c00
optimize & rewrite gcd
Jaroslav Hajek <highegg@gmail.com>
parents:
10840
diff
changeset
|
151 |
11062
88d78ee12ee8
Implement gcd for Gaussian (complex) integers
Jordi Gutiérrez Hermoso <jordigh@gmail.com>
parents:
11061
diff
changeset
|
152 template <typename FP> |
88d78ee12ee8
Implement gcd for Gaussian (complex) integers
Jordi Gutiérrez Hermoso <jordigh@gmail.com>
parents:
11061
diff
changeset
|
153 static std::complex<FP> |
88d78ee12ee8
Implement gcd for Gaussian (complex) integers
Jordi Gutiérrez Hermoso <jordigh@gmail.com>
parents:
11061
diff
changeset
|
154 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
|
155 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
|
156 { |
21782
2aef506f3fec
use namespace for lo-mappers.h functions
John W. Eaton <jwe@octave.org>
parents:
21724
diff
changeset
|
157 if (! octave::math::isinteger (a.real ()) || ! octave::math::isinteger (a.imag ()) |
2aef506f3fec
use namespace for lo-mappers.h functions
John W. Eaton <jwe@octave.org>
parents:
21724
diff
changeset
|
158 || ! octave::math::isinteger (b.real ()) || ! 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
|
159 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
|
160 |
18100
6a71e5030df5
Follow coding convention of defining and initializing only 1 variable per line in liboctinterp.
Rik <rik@octave.org>
parents:
17787
diff
changeset
|
161 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
|
162 std::complex<FP> bb = b; |
11062
88d78ee12ee8
Implement gcd for Gaussian (complex) integers
Jordi Gutiérrez Hermoso <jordigh@gmail.com>
parents:
11061
diff
changeset
|
163 bool swapped = false; |
11064 | 164 if (abs (aa) < abs (bb)) |
11062
88d78ee12ee8
Implement gcd for Gaussian (complex) integers
Jordi Gutiérrez Hermoso <jordigh@gmail.com>
parents:
11061
diff
changeset
|
165 { |
11064 | 166 std::swap (aa, bb); |
11062
88d78ee12ee8
Implement gcd for Gaussian (complex) integers
Jordi Gutiérrez Hermoso <jordigh@gmail.com>
parents:
11061
diff
changeset
|
167 swapped = true; |
88d78ee12ee8
Implement gcd for Gaussian (complex) integers
Jordi Gutiérrez Hermoso <jordigh@gmail.com>
parents:
11061
diff
changeset
|
168 } |
88d78ee12ee8
Implement gcd for Gaussian (complex) integers
Jordi Gutiérrez Hermoso <jordigh@gmail.com>
parents:
11061
diff
changeset
|
169 |
18100
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> 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
|
171 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
|
172 yy = 1, ly = 0; |
11064 | 173 |
174 while (abs(bb) != 0) | |
11062
88d78ee12ee8
Implement gcd for Gaussian (complex) integers
Jordi Gutiérrez Hermoso <jordigh@gmail.com>
parents:
11061
diff
changeset
|
175 { |
88d78ee12ee8
Implement gcd for Gaussian (complex) integers
Jordi Gutiérrez Hermoso <jordigh@gmail.com>
parents:
11061
diff
changeset
|
176 std::complex<FP> qq, rr; |
88d78ee12ee8
Implement gcd for Gaussian (complex) integers
Jordi Gutiérrez Hermoso <jordigh@gmail.com>
parents:
11061
diff
changeset
|
177 divide (aa, bb, qq, rr); |
11064 | 178 aa = bb; |
179 bb = rr; | |
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> tx = lx - qq*xx; |
11064 | 182 lx = xx; |
183 xx = tx; | |
11062
88d78ee12ee8
Implement gcd for Gaussian (complex) integers
Jordi Gutiérrez Hermoso <jordigh@gmail.com>
parents:
11061
diff
changeset
|
184 |
88d78ee12ee8
Implement gcd for Gaussian (complex) integers
Jordi Gutiérrez Hermoso <jordigh@gmail.com>
parents:
11061
diff
changeset
|
185 std::complex<FP> ty = ly - qq*yy; |
11064 | 186 ly = yy; |
187 yy = ty; | |
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 |
88d78ee12ee8
Implement gcd for Gaussian (complex) integers
Jordi Gutiérrez Hermoso <jordigh@gmail.com>
parents:
11061
diff
changeset
|
190 x = lx; |
88d78ee12ee8
Implement gcd for Gaussian (complex) integers
Jordi Gutiérrez Hermoso <jordigh@gmail.com>
parents:
11061
diff
changeset
|
191 y = ly; |
11064 | 192 |
11062
88d78ee12ee8
Implement gcd for Gaussian (complex) integers
Jordi Gutiérrez Hermoso <jordigh@gmail.com>
parents:
11061
diff
changeset
|
193 if (swapped) |
11064 | 194 std::swap (x, y); |
11062
88d78ee12ee8
Implement gcd for Gaussian (complex) integers
Jordi Gutiérrez Hermoso <jordigh@gmail.com>
parents:
11061
diff
changeset
|
195 |
88d78ee12ee8
Implement gcd for Gaussian (complex) integers
Jordi Gutiérrez Hermoso <jordigh@gmail.com>
parents:
11061
diff
changeset
|
196 return aa; |
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 |
21139
538b57866b90
consistently use "typename" intead of "class" in template declarations
John W. Eaton <jwe@octave.org>
parents:
21100
diff
changeset
|
199 template <typename T> |
11021
0ee74d581c00
optimize & rewrite gcd
Jaroslav Hajek <highegg@gmail.com>
parents:
10840
diff
changeset
|
200 static octave_int<T> |
0ee74d581c00
optimize & rewrite gcd
Jaroslav Hajek <highegg@gmail.com>
parents:
10840
diff
changeset
|
201 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
|
202 octave_int<T>& x, octave_int<T>& y) |
4864 | 203 { |
11064 | 204 T aa = a.abs ().value (); |
205 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
|
206 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
|
207 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
|
208 yy = 1, ly = 0; |
11064 | 209 |
11021
0ee74d581c00
optimize & rewrite gcd
Jaroslav Hajek <highegg@gmail.com>
parents:
10840
diff
changeset
|
210 while (bb != 0) |
0ee74d581c00
optimize & rewrite gcd
Jaroslav Hajek <highegg@gmail.com>
parents:
10840
diff
changeset
|
211 { |
0ee74d581c00
optimize & rewrite gcd
Jaroslav Hajek <highegg@gmail.com>
parents:
10840
diff
changeset
|
212 T qq = aa / bb; |
0ee74d581c00
optimize & rewrite gcd
Jaroslav Hajek <highegg@gmail.com>
parents:
10840
diff
changeset
|
213 T tt = aa % bb; |
11064 | 214 aa = bb; |
215 bb = tt; | |
11021
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 tx = lx - qq*xx; |
11064 | 218 lx = xx; |
219 xx = tx; | |
11021
0ee74d581c00
optimize & rewrite gcd
Jaroslav Hajek <highegg@gmail.com>
parents:
10840
diff
changeset
|
220 |
0ee74d581c00
optimize & rewrite gcd
Jaroslav Hajek <highegg@gmail.com>
parents:
10840
diff
changeset
|
221 T ty = ly - qq*yy; |
11064 | 222 ly = yy; |
223 yy = ty; | |
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 |
0ee74d581c00
optimize & rewrite gcd
Jaroslav Hajek <highegg@gmail.com>
parents:
10840
diff
changeset
|
226 x = octave_int<T> (lx) * a.signum (); |
0ee74d581c00
optimize & rewrite gcd
Jaroslav Hajek <highegg@gmail.com>
parents:
10840
diff
changeset
|
227 y = octave_int<T> (ly) * b.signum (); |
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 return aa; |
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 |
21139
538b57866b90
consistently use "typename" intead of "class" in template declarations
John W. Eaton <jwe@octave.org>
parents:
21100
diff
changeset
|
232 template <typename NDA> |
11021
0ee74d581c00
optimize & rewrite gcd
Jaroslav Hajek <highegg@gmail.com>
parents:
10840
diff
changeset
|
233 static octave_value |
0ee74d581c00
optimize & rewrite gcd
Jaroslav Hajek <highegg@gmail.com>
parents:
10840
diff
changeset
|
234 do_simple_gcd (const octave_value& a, const octave_value& b) |
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 typedef typename NDA::element_type T; |
0ee74d581c00
optimize & rewrite gcd
Jaroslav Hajek <highegg@gmail.com>
parents:
10840
diff
changeset
|
237 octave_value retval; |
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 if (a.is_scalar_type () && b.is_scalar_type ()) |
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 // Optimize scalar case. |
0ee74d581c00
optimize & rewrite gcd
Jaroslav Hajek <highegg@gmail.com>
parents:
10840
diff
changeset
|
242 T aa = octave_value_extract<T> (a); |
0ee74d581c00
optimize & rewrite gcd
Jaroslav Hajek <highegg@gmail.com>
parents:
10840
diff
changeset
|
243 T bb = octave_value_extract<T> (b); |
0ee74d581c00
optimize & rewrite gcd
Jaroslav Hajek <highegg@gmail.com>
parents:
10840
diff
changeset
|
244 retval = simple_gcd (aa, bb); |
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 else |
0ee74d581c00
optimize & rewrite gcd
Jaroslav Hajek <highegg@gmail.com>
parents:
10840
diff
changeset
|
247 { |
0ee74d581c00
optimize & rewrite gcd
Jaroslav Hajek <highegg@gmail.com>
parents:
10840
diff
changeset
|
248 NDA aa = octave_value_extract<NDA> (a); |
0ee74d581c00
optimize & rewrite gcd
Jaroslav Hajek <highegg@gmail.com>
parents:
10840
diff
changeset
|
249 NDA bb = octave_value_extract<NDA> (b); |
0ee74d581c00
optimize & rewrite gcd
Jaroslav Hajek <highegg@gmail.com>
parents:
10840
diff
changeset
|
250 retval = binmap<T> (aa, bb, simple_gcd, "gcd"); |
0ee74d581c00
optimize & rewrite gcd
Jaroslav Hajek <highegg@gmail.com>
parents:
10840
diff
changeset
|
251 } |
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 return retval; |
4864 | 254 } |
255 | |
11021
0ee74d581c00
optimize & rewrite gcd
Jaroslav Hajek <highegg@gmail.com>
parents:
10840
diff
changeset
|
256 // Dispatcher |
0ee74d581c00
optimize & rewrite gcd
Jaroslav Hajek <highegg@gmail.com>
parents:
10840
diff
changeset
|
257 static octave_value |
0ee74d581c00
optimize & rewrite gcd
Jaroslav Hajek <highegg@gmail.com>
parents:
10840
diff
changeset
|
258 do_simple_gcd (const octave_value& a, const octave_value& b) |
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 octave_value retval; |
0ee74d581c00
optimize & rewrite gcd
Jaroslav Hajek <highegg@gmail.com>
parents:
10840
diff
changeset
|
261 builtin_type_t btyp = btyp_mixed_numeric (a.builtin_type (), |
0ee74d581c00
optimize & rewrite gcd
Jaroslav Hajek <highegg@gmail.com>
parents:
10840
diff
changeset
|
262 b.builtin_type ()); |
0ee74d581c00
optimize & rewrite gcd
Jaroslav Hajek <highegg@gmail.com>
parents:
10840
diff
changeset
|
263 switch (btyp) |
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 case btyp_double: |
0ee74d581c00
optimize & rewrite gcd
Jaroslav Hajek <highegg@gmail.com>
parents:
10840
diff
changeset
|
266 if (a.is_sparse_type () && b.is_sparse_type ()) |
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 retval = do_simple_gcd<SparseMatrix> (a, b); |
0ee74d581c00
optimize & rewrite gcd
Jaroslav Hajek <highegg@gmail.com>
parents:
10840
diff
changeset
|
269 break; |
0ee74d581c00
optimize & rewrite gcd
Jaroslav Hajek <highegg@gmail.com>
parents:
10840
diff
changeset
|
270 } |
11064 | 271 // fall through! |
272 | |
11021
0ee74d581c00
optimize & rewrite gcd
Jaroslav Hajek <highegg@gmail.com>
parents:
10840
diff
changeset
|
273 case btyp_float: |
0ee74d581c00
optimize & rewrite gcd
Jaroslav Hajek <highegg@gmail.com>
parents:
10840
diff
changeset
|
274 retval = do_simple_gcd<NDArray> (a, b); |
0ee74d581c00
optimize & rewrite gcd
Jaroslav Hajek <highegg@gmail.com>
parents:
10840
diff
changeset
|
275 break; |
11064 | 276 |
22197
e43d83253e28
refill multi-line macro definitions
John W. Eaton <jwe@octave.org>
parents:
21966
diff
changeset
|
277 #define MAKE_INT_BRANCH(X) \ |
e43d83253e28
refill multi-line macro definitions
John W. Eaton <jwe@octave.org>
parents:
21966
diff
changeset
|
278 case btyp_ ## X: \ |
e43d83253e28
refill multi-line macro definitions
John W. Eaton <jwe@octave.org>
parents:
21966
diff
changeset
|
279 retval = do_simple_gcd<X ## NDArray> (a, b); \ |
e43d83253e28
refill multi-line macro definitions
John W. Eaton <jwe@octave.org>
parents:
21966
diff
changeset
|
280 break |
11064 | 281 |
11021
0ee74d581c00
optimize & rewrite gcd
Jaroslav Hajek <highegg@gmail.com>
parents:
10840
diff
changeset
|
282 MAKE_INT_BRANCH (int8); |
0ee74d581c00
optimize & rewrite gcd
Jaroslav Hajek <highegg@gmail.com>
parents:
10840
diff
changeset
|
283 MAKE_INT_BRANCH (int16); |
0ee74d581c00
optimize & rewrite gcd
Jaroslav Hajek <highegg@gmail.com>
parents:
10840
diff
changeset
|
284 MAKE_INT_BRANCH (int32); |
0ee74d581c00
optimize & rewrite gcd
Jaroslav Hajek <highegg@gmail.com>
parents:
10840
diff
changeset
|
285 MAKE_INT_BRANCH (int64); |
0ee74d581c00
optimize & rewrite gcd
Jaroslav Hajek <highegg@gmail.com>
parents:
10840
diff
changeset
|
286 MAKE_INT_BRANCH (uint8); |
0ee74d581c00
optimize & rewrite gcd
Jaroslav Hajek <highegg@gmail.com>
parents:
10840
diff
changeset
|
287 MAKE_INT_BRANCH (uint16); |
0ee74d581c00
optimize & rewrite gcd
Jaroslav Hajek <highegg@gmail.com>
parents:
10840
diff
changeset
|
288 MAKE_INT_BRANCH (uint32); |
0ee74d581c00
optimize & rewrite gcd
Jaroslav Hajek <highegg@gmail.com>
parents:
10840
diff
changeset
|
289 MAKE_INT_BRANCH (uint64); |
11064 | 290 |
11021
0ee74d581c00
optimize & rewrite gcd
Jaroslav Hajek <highegg@gmail.com>
parents:
10840
diff
changeset
|
291 #undef MAKE_INT_BRANCH |
11064 | 292 |
11021
0ee74d581c00
optimize & rewrite gcd
Jaroslav Hajek <highegg@gmail.com>
parents:
10840
diff
changeset
|
293 case btyp_complex: |
11064 | 294 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
|
295 break; |
11064 | 296 |
11021
0ee74d581c00
optimize & rewrite gcd
Jaroslav Hajek <highegg@gmail.com>
parents:
10840
diff
changeset
|
297 case btyp_float_complex: |
11064 | 298 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
|
299 break; |
11064 | 300 |
11021
0ee74d581c00
optimize & rewrite gcd
Jaroslav Hajek <highegg@gmail.com>
parents:
10840
diff
changeset
|
301 default: |
0ee74d581c00
optimize & rewrite gcd
Jaroslav Hajek <highegg@gmail.com>
parents:
10840
diff
changeset
|
302 error ("gcd: invalid class combination for gcd: %s and %s\n", |
0ee74d581c00
optimize & rewrite gcd
Jaroslav Hajek <highegg@gmail.com>
parents:
10840
diff
changeset
|
303 a.class_name ().c_str (), b.class_name ().c_str ()); |
0ee74d581c00
optimize & rewrite gcd
Jaroslav Hajek <highegg@gmail.com>
parents:
10840
diff
changeset
|
304 } |
0ee74d581c00
optimize & rewrite gcd
Jaroslav Hajek <highegg@gmail.com>
parents:
10840
diff
changeset
|
305 |
0ee74d581c00
optimize & rewrite gcd
Jaroslav Hajek <highegg@gmail.com>
parents:
10840
diff
changeset
|
306 if (btyp == btyp_float) |
0ee74d581c00
optimize & rewrite gcd
Jaroslav Hajek <highegg@gmail.com>
parents:
10840
diff
changeset
|
307 retval = retval.float_array_value (); |
0ee74d581c00
optimize & rewrite gcd
Jaroslav Hajek <highegg@gmail.com>
parents:
10840
diff
changeset
|
308 |
0ee74d581c00
optimize & rewrite gcd
Jaroslav Hajek <highegg@gmail.com>
parents:
10840
diff
changeset
|
309 return retval; |
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 |
21139
538b57866b90
consistently use "typename" intead of "class" in template declarations
John W. Eaton <jwe@octave.org>
parents:
21100
diff
changeset
|
312 template <typename NDA> |
11021
0ee74d581c00
optimize & rewrite gcd
Jaroslav Hajek <highegg@gmail.com>
parents:
10840
diff
changeset
|
313 static octave_value |
0ee74d581c00
optimize & rewrite gcd
Jaroslav Hajek <highegg@gmail.com>
parents:
10840
diff
changeset
|
314 do_extended_gcd (const octave_value& a, const octave_value& b, |
0ee74d581c00
optimize & rewrite gcd
Jaroslav Hajek <highegg@gmail.com>
parents:
10840
diff
changeset
|
315 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
|
316 { |
11021
0ee74d581c00
optimize & rewrite gcd
Jaroslav Hajek <highegg@gmail.com>
parents:
10840
diff
changeset
|
317 typedef typename NDA::element_type T; |
0ee74d581c00
optimize & rewrite gcd
Jaroslav Hajek <highegg@gmail.com>
parents:
10840
diff
changeset
|
318 octave_value retval; |
0ee74d581c00
optimize & rewrite gcd
Jaroslav Hajek <highegg@gmail.com>
parents:
10840
diff
changeset
|
319 |
0ee74d581c00
optimize & rewrite gcd
Jaroslav Hajek <highegg@gmail.com>
parents:
10840
diff
changeset
|
320 if (a.is_scalar_type () && b.is_scalar_type ()) |
0ee74d581c00
optimize & rewrite gcd
Jaroslav Hajek <highegg@gmail.com>
parents:
10840
diff
changeset
|
321 { |
0ee74d581c00
optimize & rewrite gcd
Jaroslav Hajek <highegg@gmail.com>
parents:
10840
diff
changeset
|
322 // Optimize scalar case. |
0ee74d581c00
optimize & rewrite gcd
Jaroslav Hajek <highegg@gmail.com>
parents:
10840
diff
changeset
|
323 T aa = octave_value_extract<T> (a); |
0ee74d581c00
optimize & rewrite gcd
Jaroslav Hajek <highegg@gmail.com>
parents:
10840
diff
changeset
|
324 T bb = octave_value_extract<T> (b); |
0ee74d581c00
optimize & rewrite gcd
Jaroslav Hajek <highegg@gmail.com>
parents:
10840
diff
changeset
|
325 T xx, yy; |
0ee74d581c00
optimize & rewrite gcd
Jaroslav Hajek <highegg@gmail.com>
parents:
10840
diff
changeset
|
326 retval = extended_gcd (aa, bb, xx, yy); |
0ee74d581c00
optimize & rewrite gcd
Jaroslav Hajek <highegg@gmail.com>
parents:
10840
diff
changeset
|
327 x = xx; |
0ee74d581c00
optimize & rewrite gcd
Jaroslav Hajek <highegg@gmail.com>
parents:
10840
diff
changeset
|
328 y = yy; |
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 else |
0ee74d581c00
optimize & rewrite gcd
Jaroslav Hajek <highegg@gmail.com>
parents:
10840
diff
changeset
|
331 { |
0ee74d581c00
optimize & rewrite gcd
Jaroslav Hajek <highegg@gmail.com>
parents:
10840
diff
changeset
|
332 NDA aa = octave_value_extract<NDA> (a); |
0ee74d581c00
optimize & rewrite gcd
Jaroslav Hajek <highegg@gmail.com>
parents:
10840
diff
changeset
|
333 NDA bb = octave_value_extract<NDA> (b); |
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 dim_vector dv = aa.dims (); |
0ee74d581c00
optimize & rewrite gcd
Jaroslav Hajek <highegg@gmail.com>
parents:
10840
diff
changeset
|
336 if (aa.numel () == 1) |
0ee74d581c00
optimize & rewrite gcd
Jaroslav Hajek <highegg@gmail.com>
parents:
10840
diff
changeset
|
337 dv = bb.dims (); |
0ee74d581c00
optimize & rewrite gcd
Jaroslav Hajek <highegg@gmail.com>
parents:
10840
diff
changeset
|
338 else if (bb.numel () != 1 && bb.dims () != dv) |
21100
e39e05d90788
Switch gripe_XXX to either err_XXX or warn_XXX naming scheme.
Rik <rik@octave.org>
parents:
20940
diff
changeset
|
339 err_nonconformant ("gcd", a.dims (), b.dims ()); |
11021
0ee74d581c00
optimize & rewrite gcd
Jaroslav Hajek <highegg@gmail.com>
parents:
10840
diff
changeset
|
340 |
0ee74d581c00
optimize & rewrite gcd
Jaroslav Hajek <highegg@gmail.com>
parents:
10840
diff
changeset
|
341 NDA gg (dv), xx (dv), yy (dv); |
0ee74d581c00
optimize & rewrite gcd
Jaroslav Hajek <highegg@gmail.com>
parents:
10840
diff
changeset
|
342 |
11064 | 343 const T *aptr = aa.fortran_vec (); |
344 const T *bptr = bb.fortran_vec (); | |
11021
0ee74d581c00
optimize & rewrite gcd
Jaroslav Hajek <highegg@gmail.com>
parents:
10840
diff
changeset
|
345 |
11064 | 346 bool inca = aa.numel () != 1; |
347 bool incb = bb.numel () != 1; | |
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 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
|
350 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
|
351 T *yptr = yy.fortran_vec (); |
11021
0ee74d581c00
optimize & rewrite gcd
Jaroslav Hajek <highegg@gmail.com>
parents:
10840
diff
changeset
|
352 |
0ee74d581c00
optimize & rewrite gcd
Jaroslav Hajek <highegg@gmail.com>
parents:
10840
diff
changeset
|
353 octave_idx_type n = gg.numel (); |
0ee74d581c00
optimize & rewrite gcd
Jaroslav Hajek <highegg@gmail.com>
parents:
10840
diff
changeset
|
354 for (octave_idx_type i = 0; i < n; i++) |
0ee74d581c00
optimize & rewrite gcd
Jaroslav Hajek <highegg@gmail.com>
parents:
10840
diff
changeset
|
355 { |
0ee74d581c00
optimize & rewrite gcd
Jaroslav Hajek <highegg@gmail.com>
parents:
10840
diff
changeset
|
356 octave_quit (); |
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 *gptr++ = extended_gcd (*aptr, *bptr, *xptr++, *yptr++); |
11064 | 359 |
11021
0ee74d581c00
optimize & rewrite gcd
Jaroslav Hajek <highegg@gmail.com>
parents:
10840
diff
changeset
|
360 aptr += inca; |
0ee74d581c00
optimize & rewrite gcd
Jaroslav Hajek <highegg@gmail.com>
parents:
10840
diff
changeset
|
361 bptr += incb; |
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 |
0ee74d581c00
optimize & rewrite gcd
Jaroslav Hajek <highegg@gmail.com>
parents:
10840
diff
changeset
|
364 x = xx; |
0ee74d581c00
optimize & rewrite gcd
Jaroslav Hajek <highegg@gmail.com>
parents:
10840
diff
changeset
|
365 y = yy; |
11064 | 366 |
11021
0ee74d581c00
optimize & rewrite gcd
Jaroslav Hajek <highegg@gmail.com>
parents:
10840
diff
changeset
|
367 retval = gg; |
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 |
0ee74d581c00
optimize & rewrite gcd
Jaroslav Hajek <highegg@gmail.com>
parents:
10840
diff
changeset
|
370 return retval; |
7815
a41df65f3f00
Add some single precision test code and fix resulting bugs
David Bateman <dbateman@free.fr>
parents:
7017
diff
changeset
|
371 } |
a41df65f3f00
Add some single precision test code and fix resulting bugs
David Bateman <dbateman@free.fr>
parents:
7017
diff
changeset
|
372 |
11021
0ee74d581c00
optimize & rewrite gcd
Jaroslav Hajek <highegg@gmail.com>
parents:
10840
diff
changeset
|
373 // Dispatcher |
0ee74d581c00
optimize & rewrite gcd
Jaroslav Hajek <highegg@gmail.com>
parents:
10840
diff
changeset
|
374 static octave_value |
0ee74d581c00
optimize & rewrite gcd
Jaroslav Hajek <highegg@gmail.com>
parents:
10840
diff
changeset
|
375 do_extended_gcd (const octave_value& a, const octave_value& b, |
0ee74d581c00
optimize & rewrite gcd
Jaroslav Hajek <highegg@gmail.com>
parents:
10840
diff
changeset
|
376 octave_value& x, octave_value& y) |
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 octave_value retval; |
11064 | 379 |
11021
0ee74d581c00
optimize & rewrite gcd
Jaroslav Hajek <highegg@gmail.com>
parents:
10840
diff
changeset
|
380 builtin_type_t btyp = btyp_mixed_numeric (a.builtin_type (), |
0ee74d581c00
optimize & rewrite gcd
Jaroslav Hajek <highegg@gmail.com>
parents:
10840
diff
changeset
|
381 b.builtin_type ()); |
0ee74d581c00
optimize & rewrite gcd
Jaroslav Hajek <highegg@gmail.com>
parents:
10840
diff
changeset
|
382 switch (btyp) |
0ee74d581c00
optimize & rewrite gcd
Jaroslav Hajek <highegg@gmail.com>
parents:
10840
diff
changeset
|
383 { |
0ee74d581c00
optimize & rewrite gcd
Jaroslav Hajek <highegg@gmail.com>
parents:
10840
diff
changeset
|
384 case btyp_double: |
0ee74d581c00
optimize & rewrite gcd
Jaroslav Hajek <highegg@gmail.com>
parents:
10840
diff
changeset
|
385 case btyp_float: |
0ee74d581c00
optimize & rewrite gcd
Jaroslav Hajek <highegg@gmail.com>
parents:
10840
diff
changeset
|
386 retval = do_extended_gcd<NDArray> (a, b, x, y); |
0ee74d581c00
optimize & rewrite gcd
Jaroslav Hajek <highegg@gmail.com>
parents:
10840
diff
changeset
|
387 break; |
11064 | 388 |
22197
e43d83253e28
refill multi-line macro definitions
John W. Eaton <jwe@octave.org>
parents:
21966
diff
changeset
|
389 #define MAKE_INT_BRANCH(X) \ |
e43d83253e28
refill multi-line macro definitions
John W. Eaton <jwe@octave.org>
parents:
21966
diff
changeset
|
390 case btyp_ ## X: \ |
e43d83253e28
refill multi-line macro definitions
John W. Eaton <jwe@octave.org>
parents:
21966
diff
changeset
|
391 retval = do_extended_gcd<X ## NDArray> (a, b, x, y); \ |
e43d83253e28
refill multi-line macro definitions
John W. Eaton <jwe@octave.org>
parents:
21966
diff
changeset
|
392 break |
11064 | 393 |
11021
0ee74d581c00
optimize & rewrite gcd
Jaroslav Hajek <highegg@gmail.com>
parents:
10840
diff
changeset
|
394 MAKE_INT_BRANCH (int8); |
0ee74d581c00
optimize & rewrite gcd
Jaroslav Hajek <highegg@gmail.com>
parents:
10840
diff
changeset
|
395 MAKE_INT_BRANCH (int16); |
0ee74d581c00
optimize & rewrite gcd
Jaroslav Hajek <highegg@gmail.com>
parents:
10840
diff
changeset
|
396 MAKE_INT_BRANCH (int32); |
0ee74d581c00
optimize & rewrite gcd
Jaroslav Hajek <highegg@gmail.com>
parents:
10840
diff
changeset
|
397 MAKE_INT_BRANCH (int64); |
0ee74d581c00
optimize & rewrite gcd
Jaroslav Hajek <highegg@gmail.com>
parents:
10840
diff
changeset
|
398 MAKE_INT_BRANCH (uint8); |
0ee74d581c00
optimize & rewrite gcd
Jaroslav Hajek <highegg@gmail.com>
parents:
10840
diff
changeset
|
399 MAKE_INT_BRANCH (uint16); |
0ee74d581c00
optimize & rewrite gcd
Jaroslav Hajek <highegg@gmail.com>
parents:
10840
diff
changeset
|
400 MAKE_INT_BRANCH (uint32); |
0ee74d581c00
optimize & rewrite gcd
Jaroslav Hajek <highegg@gmail.com>
parents:
10840
diff
changeset
|
401 MAKE_INT_BRANCH (uint64); |
11064 | 402 |
11021
0ee74d581c00
optimize & rewrite gcd
Jaroslav Hajek <highegg@gmail.com>
parents:
10840
diff
changeset
|
403 #undef MAKE_INT_BRANCH |
11064 | 404 |
11021
0ee74d581c00
optimize & rewrite gcd
Jaroslav Hajek <highegg@gmail.com>
parents:
10840
diff
changeset
|
405 case btyp_complex: |
11062
88d78ee12ee8
Implement gcd for Gaussian (complex) integers
Jordi Gutiérrez Hermoso <jordigh@gmail.com>
parents:
11061
diff
changeset
|
406 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
|
407 break; |
11064 | 408 |
11021
0ee74d581c00
optimize & rewrite gcd
Jaroslav Hajek <highegg@gmail.com>
parents:
10840
diff
changeset
|
409 case btyp_float_complex: |
11062
88d78ee12ee8
Implement gcd for Gaussian (complex) integers
Jordi Gutiérrez Hermoso <jordigh@gmail.com>
parents:
11061
diff
changeset
|
410 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
|
411 break; |
11064 | 412 |
11021
0ee74d581c00
optimize & rewrite gcd
Jaroslav Hajek <highegg@gmail.com>
parents:
10840
diff
changeset
|
413 default: |
0ee74d581c00
optimize & rewrite gcd
Jaroslav Hajek <highegg@gmail.com>
parents:
10840
diff
changeset
|
414 error ("gcd: invalid class combination for gcd: %s and %s\n", |
0ee74d581c00
optimize & rewrite gcd
Jaroslav Hajek <highegg@gmail.com>
parents:
10840
diff
changeset
|
415 a.class_name ().c_str (), b.class_name ().c_str ()); |
0ee74d581c00
optimize & rewrite gcd
Jaroslav Hajek <highegg@gmail.com>
parents:
10840
diff
changeset
|
416 } |
0ee74d581c00
optimize & rewrite gcd
Jaroslav Hajek <highegg@gmail.com>
parents:
10840
diff
changeset
|
417 |
0ee74d581c00
optimize & rewrite gcd
Jaroslav Hajek <highegg@gmail.com>
parents:
10840
diff
changeset
|
418 // For consistency. |
20555
f90c8372b7ba
eliminate many more simple uses of error_state
John W. Eaton <jwe@octave.org>
parents:
20172
diff
changeset
|
419 if (a.is_sparse_type () && b.is_sparse_type ()) |
11021
0ee74d581c00
optimize & rewrite gcd
Jaroslav Hajek <highegg@gmail.com>
parents:
10840
diff
changeset
|
420 { |
0ee74d581c00
optimize & rewrite gcd
Jaroslav Hajek <highegg@gmail.com>
parents:
10840
diff
changeset
|
421 retval = retval.sparse_matrix_value (); |
0ee74d581c00
optimize & rewrite gcd
Jaroslav Hajek <highegg@gmail.com>
parents:
10840
diff
changeset
|
422 x = x.sparse_matrix_value (); |
0ee74d581c00
optimize & rewrite gcd
Jaroslav Hajek <highegg@gmail.com>
parents:
10840
diff
changeset
|
423 y = y.sparse_matrix_value (); |
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 if (btyp == btyp_float) |
0ee74d581c00
optimize & rewrite gcd
Jaroslav Hajek <highegg@gmail.com>
parents:
10840
diff
changeset
|
427 { |
0ee74d581c00
optimize & rewrite gcd
Jaroslav Hajek <highegg@gmail.com>
parents:
10840
diff
changeset
|
428 retval = retval.float_array_value (); |
0ee74d581c00
optimize & rewrite gcd
Jaroslav Hajek <highegg@gmail.com>
parents:
10840
diff
changeset
|
429 x = x.float_array_value (); |
0ee74d581c00
optimize & rewrite gcd
Jaroslav Hajek <highegg@gmail.com>
parents:
10840
diff
changeset
|
430 y = y.float_array_value (); |
0ee74d581c00
optimize & rewrite gcd
Jaroslav Hajek <highegg@gmail.com>
parents:
10840
diff
changeset
|
431 } |
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 return retval; |
0ee74d581c00
optimize & rewrite gcd
Jaroslav Hajek <highegg@gmail.com>
parents:
10840
diff
changeset
|
434 } |
0ee74d581c00
optimize & rewrite gcd
Jaroslav Hajek <highegg@gmail.com>
parents:
10840
diff
changeset
|
435 |
15039
e753177cde93
maint: Move non-dynamically linked functions from DLD-FUNCTIONS/ to corefcn/ directory
Rik <rik@octave.org>
parents:
14854
diff
changeset
|
436 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
|
437 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
|
438 @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
|
439 @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
|
440 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
|
441 |
112b20240c87
move docstrings in C++ files out of C strings and into comments
John W. Eaton <jwe@octave.org>
parents:
21942
diff
changeset
|
442 If more than one argument is given then all arguments must be the same size |
112b20240c87
move docstrings in C++ files out of C strings and into comments
John W. Eaton <jwe@octave.org>
parents:
21942
diff
changeset
|
443 or scalar. In this case the greatest common divisor is calculated for each |
112b20240c87
move docstrings in C++ files out of C strings and into comments
John W. Eaton <jwe@octave.org>
parents:
21942
diff
changeset
|
444 element individually. All elements must be ordinary or Gaussian (complex) |
112b20240c87
move docstrings in C++ files out of C strings and into comments
John W. Eaton <jwe@octave.org>
parents:
21942
diff
changeset
|
445 integers. Note that for Gaussian integers, the gcd is only unique up to a |
112b20240c87
move docstrings in C++ files out of C strings and into comments
John W. Eaton <jwe@octave.org>
parents:
21942
diff
changeset
|
446 phase factor (multiplication by 1, -1, i, or -i), so an arbitrary greatest |
112b20240c87
move docstrings in C++ files out of C strings and into comments
John W. Eaton <jwe@octave.org>
parents:
21942
diff
changeset
|
447 common divisor among the four possible is returned. |
112b20240c87
move docstrings in C++ files out of C strings and into comments
John W. Eaton <jwe@octave.org>
parents:
21942
diff
changeset
|
448 |
112b20240c87
move docstrings in C++ files out of C strings and into comments
John W. Eaton <jwe@octave.org>
parents:
21942
diff
changeset
|
449 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
|
450 that, |
112b20240c87
move docstrings in C++ files out of C strings and into comments
John W. Eaton <jwe@octave.org>
parents:
21942
diff
changeset
|
451 |
112b20240c87
move docstrings in C++ files out of C strings and into comments
John W. Eaton <jwe@octave.org>
parents:
21942
diff
changeset
|
452 @tex |
112b20240c87
move docstrings in C++ files out of C strings and into comments
John W. Eaton <jwe@octave.org>
parents:
21942
diff
changeset
|
453 $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
|
454 @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
|
455 @ifnottex |
112b20240c87
move docstrings in C++ files out of C strings and into comments
John W. Eaton <jwe@octave.org>
parents:
21942
diff
changeset
|
456 |
112b20240c87
move docstrings in C++ files out of C strings and into comments
John W. Eaton <jwe@octave.org>
parents:
21942
diff
changeset
|
457 @example |
112b20240c87
move docstrings in C++ files out of C strings and into comments
John W. Eaton <jwe@octave.org>
parents:
21942
diff
changeset
|
458 @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
|
459 @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
|
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 @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
|
462 |
112b20240c87
move docstrings in C++ files out of C strings and into comments
John W. Eaton <jwe@octave.org>
parents:
21942
diff
changeset
|
463 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
|
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 @example |
112b20240c87
move docstrings in C++ files out of C strings and into comments
John W. Eaton <jwe@octave.org>
parents:
21942
diff
changeset
|
466 @group |
112b20240c87
move docstrings in C++ files out of C strings and into comments
John W. Eaton <jwe@octave.org>
parents:
21942
diff
changeset
|
467 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
|
468 @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
|
469 @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
|
470 @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
|
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 @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
|
473 @end deftypefn */) |
4864 | 474 { |
475 int nargin = args.length (); | |
476 | |
20802
8bb38ba1bad6
eliminate return statements after calls to print_usage
John W. Eaton <jwe@octave.org>
parents:
20650
diff
changeset
|
477 if (nargin < 2) |
8bb38ba1bad6
eliminate return statements after calls to print_usage
John W. Eaton <jwe@octave.org>
parents:
20650
diff
changeset
|
478 print_usage (); |
8bb38ba1bad6
eliminate return statements after calls to print_usage
John W. Eaton <jwe@octave.org>
parents:
20650
diff
changeset
|
479 |
20939
b17fda023ca6
maint: Use new C++ archetype in more files.
Rik <rik@octave.org>
parents:
20853
diff
changeset
|
480 octave_value_list retval; |
b17fda023ca6
maint: Use new C++ archetype in more files.
Rik <rik@octave.org>
parents:
20853
diff
changeset
|
481 |
20802
8bb38ba1bad6
eliminate return statements after calls to print_usage
John W. Eaton <jwe@octave.org>
parents:
20650
diff
changeset
|
482 if (nargout > 1) |
4864 | 483 { |
20802
8bb38ba1bad6
eliminate return statements after calls to print_usage
John W. Eaton <jwe@octave.org>
parents:
20650
diff
changeset
|
484 retval.resize (nargin + 1); |
11064 | 485 |
20802
8bb38ba1bad6
eliminate return statements after calls to print_usage
John W. Eaton <jwe@octave.org>
parents:
20650
diff
changeset
|
486 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
|
487 |
8bb38ba1bad6
eliminate return statements after calls to print_usage
John W. Eaton <jwe@octave.org>
parents:
20650
diff
changeset
|
488 for (int j = 2; j < nargin; j++) |
10154
40dfc0c99116
DLD-FUNCTIONS/*.cc: untabify
John W. Eaton <jwe@octave.org>
parents:
9167
diff
changeset
|
489 { |
20802
8bb38ba1bad6
eliminate return statements after calls to print_usage
John W. Eaton <jwe@octave.org>
parents:
20650
diff
changeset
|
490 octave_value x; |
20939
b17fda023ca6
maint: Use new C++ archetype in more files.
Rik <rik@octave.org>
parents:
20853
diff
changeset
|
491 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
|
492 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
|
493 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
|
494 } |
7815
a41df65f3f00
Add some single precision test code and fix resulting bugs
David Bateman <dbateman@free.fr>
parents:
7017
diff
changeset
|
495 } |
4864 | 496 else |
20802
8bb38ba1bad6
eliminate return statements after calls to print_usage
John W. Eaton <jwe@octave.org>
parents:
20650
diff
changeset
|
497 { |
8bb38ba1bad6
eliminate return statements after calls to print_usage
John W. Eaton <jwe@octave.org>
parents:
20650
diff
changeset
|
498 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
|
499 |
8bb38ba1bad6
eliminate return statements after calls to print_usage
John W. Eaton <jwe@octave.org>
parents:
20650
diff
changeset
|
500 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
|
501 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
|
502 } |
4864 | 503 |
504 return retval; | |
505 } | |
506 | |
507 /* | |
14501
60e5cf354d80
Update %!tests in DLD-FUNCTIONS/ directory with Octave coding conventions.
Rik <octave@nomad.inbox5.com>
parents:
14360
diff
changeset
|
508 %!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
|
509 %!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
|
510 %!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
|
511 %!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
|
512 |
19132
c3bdd640a4dd
* gcd.cc: add more tests
Jordi Gutiérrez Hermoso <jordigh@octave.org>
parents:
18100
diff
changeset
|
513 %!test |
c3bdd640a4dd
* gcd.cc: add more tests
Jordi Gutiérrez Hermoso <jordigh@octave.org>
parents:
18100
diff
changeset
|
514 %! p = [953 967]; |
c3bdd640a4dd
* gcd.cc: add more tests
Jordi Gutiérrez Hermoso <jordigh@octave.org>
parents:
18100
diff
changeset
|
515 %! u = [953 + i*971, 967 + i*977]; |
c3bdd640a4dd
* gcd.cc: add more tests
Jordi Gutiérrez Hermoso <jordigh@octave.org>
parents:
18100
diff
changeset
|
516 %! [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
|
517 %! [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
|
518 %! assert (d, 1); |
ecce63c99c3f
maint: Add semicolons to terminate code in %! blocks.
Rik <rik@octave.org>
parents:
21301
diff
changeset
|
519 %! assert (sum (p.*k), d); |
ecce63c99c3f
maint: Add semicolons to terminate code in %! blocks.
Rik <rik@octave.org>
parents:
21301
diff
changeset
|
520 %! assert (abs (z), sqrt (2)); |
ecce63c99c3f
maint: Add semicolons to terminate code in %! blocks.
Rik <rik@octave.org>
parents:
21301
diff
changeset
|
521 %! 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
|
522 |
21581
6fab85c1538f
maint: Follow Octave conventions for use of semicolon in BIST tests.
Rik <rik@octave.org>
parents:
21580
diff
changeset
|
523 %!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
|
524 %!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
|
525 |
14501
60e5cf354d80
Update %!tests in DLD-FUNCTIONS/ directory with Octave coding conventions.
Rik <octave@nomad.inbox5.com>
parents:
14360
diff
changeset
|
526 %!error gcd () |
7815
a41df65f3f00
Add some single precision test code and fix resulting bugs
David Bateman <dbateman@free.fr>
parents:
7017
diff
changeset
|
527 |
a41df65f3f00
Add some single precision test code and fix resulting bugs
David Bateman <dbateman@free.fr>
parents:
7017
diff
changeset
|
528 %!test |
a41df65f3f00
Add some single precision test code and fix resulting bugs
David Bateman <dbateman@free.fr>
parents:
7017
diff
changeset
|
529 %! s.a = 1; |
14501
60e5cf354d80
Update %!tests in DLD-FUNCTIONS/ directory with Octave coding conventions.
Rik <octave@nomad.inbox5.com>
parents:
14360
diff
changeset
|
530 %! fail ("gcd (s)"); |
4864 | 531 */ |