annotate libinterp/corefcn/gcd.cc @ 22327:d0562b3159c7

move more classes inside octave namespace * ov-complex.cc, quit.h, lo-array-errwarn.h, lo-array-errwarn.cc, lo-array-gripes.cc: Move classes inside octave namespace. * NEWS, file-editor-tab.cc, Cell.cc, __qp__.cc, cellfun.cc, daspk.cc, dasrt.cc, dassl.cc, data.cc, error.cc, error.h, errwarn.cc, errwarn.h, file-io.cc, gcd.cc, graphics.cc, graphics.in.h, gripes.cc, gripes.h, input.cc, interpreter.cc, interpreter.h, inv.cc, jit-typeinfo.cc, load-path.cc, ls-mat-ascii.cc, ls-mat5.cc, lsode.cc, mex.cc, oct-handle.h, oct-map.cc, oct-stream.cc, quad.cc, rand.cc, sparse-xdiv.cc, sparse-xpow.cc, sparse.cc, sub2ind.cc, toplev.cc, utils.cc, variables.cc, xdiv.cc, xpow.cc, __eigs__.cc, __init_gnuplot__.cc, ov-base-diag.cc, ov-base-mat.cc, ov-base-scalar.cc, ov-base-sparse.cc, ov-base.cc, ov-class.cc, ov-classdef.cc, ov-complex.h, ov-complex.cc, ov-cx-mat.cc, ov-cx-sparse.cc, ov-fcn-handle.cc, ov-float.cc, ov-float.h, ov-flt-complex.h, ov-flt-cx-mat.cc, ov-flt-re-mat.cc, ov-java.cc, ov-oncleanup.cc, ov-perm.cc, ov-range.cc, ov-re-diag.cc, ov-re-mat.cc, ov-re-sparse.cc, ov-scalar.cc, ov-scalar.h, ov-str-mat.cc, ov.cc, op-cs-cs.cc, op-fcs-fcs.cc, op-fs-fs.cc, op-int.h, op-s-s.cc, ops.h, oct-parse.in.yy, pt-assign.cc, pt-eval.cc, pt-idx.cc, pt.cc, Array-util.cc, Array.cc, CColVector.cc, CDiagMatrix.cc, CMatrix.cc, CNDArray.cc, CRowVector.cc, CSparse.cc, DiagArray2.cc, MDiagArray2.cc, MSparse.cc, PermMatrix.cc, Range.cc, Sparse.cc, dColVector.cc, dDiagMatrix.cc, dMatrix.cc, dNDArray.cc, dRowVector.cc, dSparse.cc, fCColVector.cc, fCDiagMatrix.cc, fCMatrix.cc, fCNDArray.cc, fCRowVector.cc, fColVector.cc, fDiagMatrix.cc, fMatrix.cc, fNDArray.cc, fRowVector.cc, idx-vector.cc, quit.cc, quit.h, gepbalance.cc, Sparse-diag-op-defs.h, Sparse-op-defs.h, Sparse-perm-op-defs.h, mx-inlines.cc, mx-op-defs.h, cmd-edit.cc, lo-array-errwarn.cc, lo-array-errwarn.h, lo-array-gripes.cc, lo-array-gripes.h, oct-binmap.h: Update to use namespace.
author John W. Eaton <jwe@octave.org>
date Wed, 17 Aug 2016 03:41:42 -0400
parents bac0d6f07a3e
children 34ce5be04942
Ignore whitespace changes - Everywhere: Within whitespace: At end of lines:
rev   line source
4864
d49d761c8c93 [project @ 2004-04-16 16:01:11 by jwe]
jwe
parents:
diff changeset
1 /*
d49d761c8c93 [project @ 2004-04-16 16:01:11 by jwe]
jwe
parents:
diff changeset
2
22323
bac0d6f07a3e maint: Update copyright notices for 2016.
John W. Eaton <jwe@octave.org>
parents: 22197
diff changeset
3 Copyright (C) 2004-2016 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
d49d761c8c93 [project @ 2004-04-16 16:01:11 by jwe]
jwe
parents:
diff changeset
5
7016
93c65f2a5668 [project @ 2007-10-12 06:40:56 by jwe]
jwe
parents: 7001
diff changeset
6 This file is part of Octave.
4864
d49d761c8c93 [project @ 2004-04-16 16:01:11 by jwe]
jwe
parents:
diff changeset
7
7016
93c65f2a5668 [project @ 2007-10-12 06:40:56 by jwe]
jwe
parents: 7001
diff changeset
8 Octave is free software; you can redistribute it and/or modify it
93c65f2a5668 [project @ 2007-10-12 06:40:56 by jwe]
jwe
parents: 7001
diff changeset
9 under the terms of the GNU General Public License as published by the
93c65f2a5668 [project @ 2007-10-12 06:40:56 by jwe]
jwe
parents: 7001
diff changeset
10 Free Software Foundation; either version 3 of the License, or (at your
93c65f2a5668 [project @ 2007-10-12 06:40:56 by jwe]
jwe
parents: 7001
diff changeset
11 option) any later version.
93c65f2a5668 [project @ 2007-10-12 06:40:56 by jwe]
jwe
parents: 7001
diff changeset
12
93c65f2a5668 [project @ 2007-10-12 06:40:56 by jwe]
jwe
parents: 7001
diff changeset
13 Octave is distributed in the hope that it will be useful, but WITHOUT
93c65f2a5668 [project @ 2007-10-12 06:40:56 by jwe]
jwe
parents: 7001
diff changeset
14 ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or
93c65f2a5668 [project @ 2007-10-12 06:40:56 by jwe]
jwe
parents: 7001
diff changeset
15 FITNESS FOR A PARTICULAR PURPOSE. See the GNU General Public License
93c65f2a5668 [project @ 2007-10-12 06:40:56 by jwe]
jwe
parents: 7001
diff changeset
16 for more details.
4864
d49d761c8c93 [project @ 2004-04-16 16:01:11 by jwe]
jwe
parents:
diff changeset
17
d49d761c8c93 [project @ 2004-04-16 16:01:11 by jwe]
jwe
parents:
diff changeset
18 You should have received a copy of the GNU General Public License
7016
93c65f2a5668 [project @ 2007-10-12 06:40:56 by jwe]
jwe
parents: 7001
diff changeset
19 along with Octave; see the file COPYING. If not, see
93c65f2a5668 [project @ 2007-10-12 06:40:56 by jwe]
jwe
parents: 7001
diff changeset
20 <http://www.gnu.org/licenses/>.
4864
d49d761c8c93 [project @ 2004-04-16 16:01:11 by jwe]
jwe
parents:
diff changeset
21
d49d761c8c93 [project @ 2004-04-16 16:01:11 by jwe]
jwe
parents:
diff changeset
22 */
d49d761c8c93 [project @ 2004-04-16 16:01:11 by jwe]
jwe
parents:
diff changeset
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
d49d761c8c93 [project @ 2004-04-16 16:01:11 by jwe]
jwe
parents:
diff changeset
26 #endif
d49d761c8c93 [project @ 2004-04-16 16:01:11 by jwe]
jwe
parents:
diff changeset
27
d49d761c8c93 [project @ 2004-04-16 16:01:11 by jwe]
jwe
parents:
diff changeset
28 #include "dNDArray.h"
d49d761c8c93 [project @ 2004-04-16 16:01:11 by jwe]
jwe
parents:
diff changeset
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
d49d761c8c93 [project @ 2004-04-16 16:01:11 by jwe]
jwe
parents:
diff changeset
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
d49d761c8c93 [project @ 2004-04-16 16:01:11 by jwe]
jwe
parents:
diff changeset
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
d49d761c8c93 [project @ 2004-04-16 16:01:11 by jwe]
jwe
parents:
diff changeset
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
d49d761c8c93 [project @ 2004-04-16 16:01:11 by jwe]
jwe
parents:
diff changeset
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
61a6c99d8ff7 gcd.cc: style fixes
John W. Eaton <jwe@octave.org>
parents: 11062
diff changeset
45 double aa = fabs (a);
61a6c99d8ff7 gcd.cc: style fixes
John W. Eaton <jwe@octave.org>
parents: 11062
diff changeset
46 double bb = fabs (b);
61a6c99d8ff7 gcd.cc: style fixes
John W. Eaton <jwe@octave.org>
parents: 11062
diff changeset
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
61a6c99d8ff7 gcd.cc: style fixes
John W. Eaton <jwe@octave.org>
parents: 11062
diff changeset
51 aa = bb;
61a6c99d8ff7 gcd.cc: style fixes
John W. Eaton <jwe@octave.org>
parents: 11062
diff changeset
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
61a6c99d8ff7 gcd.cc: style fixes
John W. Eaton <jwe@octave.org>
parents: 11062
diff changeset
68
61a6c99d8ff7 gcd.cc: style fixes
John W. Eaton <jwe@octave.org>
parents: 11062
diff changeset
69 q = std::complex<FP> (qr, qi);
61a6c99d8ff7 gcd.cc: style fixes
John W. Eaton <jwe@octave.org>
parents: 11062
diff changeset
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
61a6c99d8ff7 gcd.cc: style fixes
John W. Eaton <jwe@octave.org>
parents: 11062
diff changeset
82 std::complex<FP> aa = a;
61a6c99d8ff7 gcd.cc: style fixes
John W. Eaton <jwe@octave.org>
parents: 11062
diff changeset
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
61a6c99d8ff7 gcd.cc: style fixes
John W. Eaton <jwe@octave.org>
parents: 11062
diff changeset
85 if (abs (aa) < abs (bb))
61a6c99d8ff7 gcd.cc: style fixes
John W. Eaton <jwe@octave.org>
parents: 11062
diff changeset
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
61a6c99d8ff7 gcd.cc: style fixes
John W. Eaton <jwe@octave.org>
parents: 11062
diff changeset
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
61a6c99d8ff7 gcd.cc: style fixes
John W. Eaton <jwe@octave.org>
parents: 11062
diff changeset
103 T aa = a.abs ().value ();
61a6c99d8ff7 gcd.cc: style fixes
John W. Eaton <jwe@octave.org>
parents: 11062
diff changeset
104 T bb = b.abs ().value ();
61a6c99d8ff7 gcd.cc: style fixes
John W. Eaton <jwe@octave.org>
parents: 11062
diff changeset
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
61a6c99d8ff7 gcd.cc: style fixes
John W. Eaton <jwe@octave.org>
parents: 11062
diff changeset
109 aa = bb;
61a6c99d8ff7 gcd.cc: style fixes
John W. Eaton <jwe@octave.org>
parents: 11062
diff changeset
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
61a6c99d8ff7 gcd.cc: style fixes
John W. Eaton <jwe@octave.org>
parents: 11062
diff changeset
122 double aa = fabs (a);
61a6c99d8ff7 gcd.cc: style fixes
John W. Eaton <jwe@octave.org>
parents: 11062
diff changeset
123 double bb = fabs (b);
61a6c99d8ff7 gcd.cc: style fixes
John W. Eaton <jwe@octave.org>
parents: 11062
diff changeset
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
61a6c99d8ff7 gcd.cc: style fixes
John W. Eaton <jwe@octave.org>
parents: 11062
diff changeset
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
61a6c99d8ff7 gcd.cc: style fixes
John W. Eaton <jwe@octave.org>
parents: 11062
diff changeset
133
61a6c99d8ff7 gcd.cc: style fixes
John W. Eaton <jwe@octave.org>
parents: 11062
diff changeset
134 aa = bb;
61a6c99d8ff7 gcd.cc: style fixes
John W. Eaton <jwe@octave.org>
parents: 11062
diff changeset
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
61a6c99d8ff7 gcd.cc: style fixes
John W. Eaton <jwe@octave.org>
parents: 11062
diff changeset
138 lx = xx;
61a6c99d8ff7 gcd.cc: style fixes
John W. Eaton <jwe@octave.org>
parents: 11062
diff changeset
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
61a6c99d8ff7 gcd.cc: style fixes
John W. Eaton <jwe@octave.org>
parents: 11062
diff changeset
142 ly = yy;
61a6c99d8ff7 gcd.cc: style fixes
John W. Eaton <jwe@octave.org>
parents: 11062
diff changeset
143 yy = ty;
11021
0ee74d581c00 optimize & rewrite gcd
Jaroslav Hajek <highegg@gmail.com>
parents: 10840
diff changeset
144 }
4864
d49d761c8c93 [project @ 2004-04-16 16:01:11 by jwe]
jwe
parents:
diff changeset
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
61a6c99d8ff7 gcd.cc: style fixes
John W. Eaton <jwe@octave.org>
parents: 11062
diff changeset
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
61a6c99d8ff7 gcd.cc: style fixes
John W. Eaton <jwe@octave.org>
parents: 11062
diff changeset
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
61a6c99d8ff7 gcd.cc: style fixes
John W. Eaton <jwe@octave.org>
parents: 11062
diff changeset
173
61a6c99d8ff7 gcd.cc: style fixes
John W. Eaton <jwe@octave.org>
parents: 11062
diff changeset
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
61a6c99d8ff7 gcd.cc: style fixes
John W. Eaton <jwe@octave.org>
parents: 11062
diff changeset
178 aa = bb;
61a6c99d8ff7 gcd.cc: style fixes
John W. Eaton <jwe@octave.org>
parents: 11062
diff changeset
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
61a6c99d8ff7 gcd.cc: style fixes
John W. Eaton <jwe@octave.org>
parents: 11062
diff changeset
182 lx = xx;
61a6c99d8ff7 gcd.cc: style fixes
John W. Eaton <jwe@octave.org>
parents: 11062
diff changeset
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
61a6c99d8ff7 gcd.cc: style fixes
John W. Eaton <jwe@octave.org>
parents: 11062
diff changeset
186 ly = yy;
61a6c99d8ff7 gcd.cc: style fixes
John W. Eaton <jwe@octave.org>
parents: 11062
diff changeset
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
61a6c99d8ff7 gcd.cc: style fixes
John W. Eaton <jwe@octave.org>
parents: 11062
diff changeset
192
11062
88d78ee12ee8 Implement gcd for Gaussian (complex) integers
Jordi Gutiérrez Hermoso <jordigh@gmail.com>
parents: 11061
diff changeset
193 if (swapped)
11064
61a6c99d8ff7 gcd.cc: style fixes
John W. Eaton <jwe@octave.org>
parents: 11062
diff changeset
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
d49d761c8c93 [project @ 2004-04-16 16:01:11 by jwe]
jwe
parents:
diff changeset
203 {
11064
61a6c99d8ff7 gcd.cc: style fixes
John W. Eaton <jwe@octave.org>
parents: 11062
diff changeset
204 T aa = a.abs ().value ();
61a6c99d8ff7 gcd.cc: style fixes
John W. Eaton <jwe@octave.org>
parents: 11062
diff changeset
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
61a6c99d8ff7 gcd.cc: style fixes
John W. Eaton <jwe@octave.org>
parents: 11062
diff changeset
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
61a6c99d8ff7 gcd.cc: style fixes
John W. Eaton <jwe@octave.org>
parents: 11062
diff changeset
214 aa = bb;
61a6c99d8ff7 gcd.cc: style fixes
John W. Eaton <jwe@octave.org>
parents: 11062
diff changeset
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
61a6c99d8ff7 gcd.cc: style fixes
John W. Eaton <jwe@octave.org>
parents: 11062
diff changeset
218 lx = xx;
61a6c99d8ff7 gcd.cc: style fixes
John W. Eaton <jwe@octave.org>
parents: 11062
diff changeset
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
61a6c99d8ff7 gcd.cc: style fixes
John W. Eaton <jwe@octave.org>
parents: 11062
diff changeset
222 ly = yy;
61a6c99d8ff7 gcd.cc: style fixes
John W. Eaton <jwe@octave.org>
parents: 11062
diff changeset
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
d49d761c8c93 [project @ 2004-04-16 16:01:11 by jwe]
jwe
parents:
diff changeset
254 }
d49d761c8c93 [project @ 2004-04-16 16:01:11 by jwe]
jwe
parents:
diff changeset
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
61a6c99d8ff7 gcd.cc: style fixes
John W. Eaton <jwe@octave.org>
parents: 11062
diff changeset
271 // fall through!
61a6c99d8ff7 gcd.cc: style fixes
John W. Eaton <jwe@octave.org>
parents: 11062
diff changeset
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
61a6c99d8ff7 gcd.cc: style fixes
John W. Eaton <jwe@octave.org>
parents: 11062
diff changeset
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
61a6c99d8ff7 gcd.cc: style fixes
John W. Eaton <jwe@octave.org>
parents: 11062
diff changeset
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
61a6c99d8ff7 gcd.cc: style fixes
John W. Eaton <jwe@octave.org>
parents: 11062
diff changeset
290
11021
0ee74d581c00 optimize & rewrite gcd
Jaroslav Hajek <highegg@gmail.com>
parents: 10840
diff changeset
291 #undef MAKE_INT_BRANCH
11064
61a6c99d8ff7 gcd.cc: style fixes
John W. Eaton <jwe@octave.org>
parents: 11062
diff changeset
292
11021
0ee74d581c00 optimize & rewrite gcd
Jaroslav Hajek <highegg@gmail.com>
parents: 10840
diff changeset
293 case btyp_complex:
11064
61a6c99d8ff7 gcd.cc: style fixes
John W. Eaton <jwe@octave.org>
parents: 11062
diff changeset
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
61a6c99d8ff7 gcd.cc: style fixes
John W. Eaton <jwe@octave.org>
parents: 11062
diff changeset
296
11021
0ee74d581c00 optimize & rewrite gcd
Jaroslav Hajek <highegg@gmail.com>
parents: 10840
diff changeset
297 case btyp_float_complex:
11064
61a6c99d8ff7 gcd.cc: style fixes
John W. Eaton <jwe@octave.org>
parents: 11062
diff changeset
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
61a6c99d8ff7 gcd.cc: style fixes
John W. Eaton <jwe@octave.org>
parents: 11062
diff changeset
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)
22327
d0562b3159c7 move more classes inside octave namespace
John W. Eaton <jwe@octave.org>
parents: 22323
diff changeset
339 octave::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
61a6c99d8ff7 gcd.cc: style fixes
John W. Eaton <jwe@octave.org>
parents: 11062
diff changeset
343 const T *aptr = aa.fortran_vec ();
61a6c99d8ff7 gcd.cc: style fixes
John W. Eaton <jwe@octave.org>
parents: 11062
diff changeset
344 const T *bptr = bb.fortran_vec ();
11021
0ee74d581c00 optimize & rewrite gcd
Jaroslav Hajek <highegg@gmail.com>
parents: 10840
diff changeset
345
11064
61a6c99d8ff7 gcd.cc: style fixes
John W. Eaton <jwe@octave.org>
parents: 11062
diff changeset
346 bool inca = aa.numel () != 1;
61a6c99d8ff7 gcd.cc: style fixes
John W. Eaton <jwe@octave.org>
parents: 11062
diff changeset
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
61a6c99d8ff7 gcd.cc: style fixes
John W. Eaton <jwe@octave.org>
parents: 11062
diff changeset
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
61a6c99d8ff7 gcd.cc: style fixes
John W. Eaton <jwe@octave.org>
parents: 11062
diff changeset
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
61a6c99d8ff7 gcd.cc: style fixes
John W. Eaton <jwe@octave.org>
parents: 11062
diff changeset
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
61a6c99d8ff7 gcd.cc: style fixes
John W. Eaton <jwe@octave.org>
parents: 11062
diff changeset
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
61a6c99d8ff7 gcd.cc: style fixes
John W. Eaton <jwe@octave.org>
parents: 11062
diff changeset
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
61a6c99d8ff7 gcd.cc: style fixes
John W. Eaton <jwe@octave.org>
parents: 11062
diff changeset
402
11021
0ee74d581c00 optimize & rewrite gcd
Jaroslav Hajek <highegg@gmail.com>
parents: 10840
diff changeset
403 #undef MAKE_INT_BRANCH
11064
61a6c99d8ff7 gcd.cc: style fixes
John W. Eaton <jwe@octave.org>
parents: 11062
diff changeset
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
61a6c99d8ff7 gcd.cc: style fixes
John W. Eaton <jwe@octave.org>
parents: 11062
diff changeset
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
61a6c99d8ff7 gcd.cc: style fixes
John W. Eaton <jwe@octave.org>
parents: 11062
diff changeset
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
d49d761c8c93 [project @ 2004-04-16 16:01:11 by jwe]
jwe
parents:
diff changeset
474 {
d49d761c8c93 [project @ 2004-04-16 16:01:11 by jwe]
jwe
parents:
diff changeset
475 int nargin = args.length ();
d49d761c8c93 [project @ 2004-04-16 16:01:11 by jwe]
jwe
parents:
diff changeset
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
d49d761c8c93 [project @ 2004-04-16 16:01:11 by jwe]
jwe
parents:
diff changeset
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
61a6c99d8ff7 gcd.cc: style fixes
John W. Eaton <jwe@octave.org>
parents: 11062
diff changeset
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
d49d761c8c93 [project @ 2004-04-16 16:01:11 by jwe]
jwe
parents:
diff changeset
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
d49d761c8c93 [project @ 2004-04-16 16:01:11 by jwe]
jwe
parents:
diff changeset
503
d49d761c8c93 [project @ 2004-04-16 16:01:11 by jwe]
jwe
parents:
diff changeset
504 return retval;
d49d761c8c93 [project @ 2004-04-16 16:01:11 by jwe]
jwe
parents:
diff changeset
505 }
d49d761c8c93 [project @ 2004-04-16 16:01:11 by jwe]
jwe
parents:
diff changeset
506
d49d761c8c93 [project @ 2004-04-16 16:01:11 by jwe]
jwe
parents:
diff changeset
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
d49d761c8c93 [project @ 2004-04-16 16:01:11 by jwe]
jwe
parents:
diff changeset
531 */