annotate libinterp/corefcn/gcd.cc @ 22407:34ce5be04942

maint: Style check C++ code in libinterp/. * build-env.h, build-env.in.cc, builtins.h, Cell.cc, Cell.h, __contourc__.cc, __dispatch__.cc, __dsearchn__.cc, __ilu__.cc, __lin_interpn__.cc, __luinc__.cc, __magick_read__.cc, __pchip_deriv__.cc, __qp__.cc, balance.cc, base-text-renderer.h, besselj.cc, bitfcns.cc, bsxfun.cc, c-file-ptr-stream.cc, c-file-ptr-stream.h, call-stack.cc, call-stack.h, cdisplay.c, cdisplay.h, cellfun.cc, coct-hdf5-types.c, comment-list.cc, comment-list.h, conv2.cc, daspk.cc, dasrt.cc, dassl.cc, data.cc, data.h, debug.cc, debug.h, defaults.cc, defaults.in.h, defun-dld.h, defun-int.h, defun.h, det.cc, dirfns.cc, dirfns.h, display.cc, display.h, dlmread.cc, dynamic-ld.cc, dynamic-ld.h, eig.cc, error.cc, error.h, errwarn.h, event-queue.cc, event-queue.h, fft.cc, fft2.cc, fftn.cc, file-io.cc, file-io.h, filter.cc, find.cc, ft-text-renderer.cc, ft-text-renderer.h, gammainc.cc, gcd.cc, getgrent.cc, getpwent.cc, givens.cc, gl-render.cc, gl-render.h, gl2ps-print.cc, gl2ps-print.h, graphics.cc, graphics.in.h, gripes.h, gsvd.cc, hash.cc, help.cc, help.h, hess.cc, hex2num.cc, hook-fcn.cc, hook-fcn.h, input.cc, input.h, interpreter.cc, interpreter.h, inv.cc, jit-ir.cc, jit-ir.h, jit-typeinfo.cc, jit-typeinfo.h, jit-util.cc, jit-util.h, kron.cc, load-path.cc, load-path.h, load-save.cc, load-save.h, lookup.cc, ls-ascii-helper.cc, ls-ascii-helper.h, ls-hdf5.cc, ls-hdf5.h, ls-mat-ascii.cc, ls-mat-ascii.h, ls-mat4.cc, ls-mat4.h, ls-mat5.h, ls-oct-binary.cc, ls-oct-binary.h, ls-oct-text.cc, ls-oct-text.h, ls-utils.cc, ls-utils.h, lsode.cc, lu.cc, matrix_type.cc, max.cc, mex.cc, mex.h, mexproto.h, mgorth.cc, mxarray.in.h, nproc.cc, oct-errno.h, oct-errno.in.cc, oct-fstrm.cc, oct-fstrm.h, oct-handle.h, oct-hdf5-types.cc, oct-hdf5-types.h, oct-hdf5.h, oct-hist.cc, oct-hist.h, oct-iostrm.cc, oct-iostrm.h, oct-lvalue.cc, oct-lvalue.h, oct-map.h, oct-obj.h, oct-opengl.h, oct-prcstrm.cc, oct-prcstrm.h, oct-procbuf.cc, oct-procbuf.h, oct-stdstrm.h, oct-stream.cc, oct-stream.h, oct-strstrm.cc, oct-strstrm.h, oct.h, octave-default-image.h, octave-link.h, octave-preserve-stream-state.h, ordschur.cc, pager.cc, pager.h, pinv.cc, pr-output.cc, pr-output.h, procstream.cc, procstream.h, profiler.h, psi.cc, pt-jit.cc, pt-jit.h, quad.cc, quadcc.cc, qz.cc, rand.cc, rcond.cc, regexp.cc, schur.cc, sighandlers.cc, sighandlers.h, sparse-xdiv.cc, sparse-xdiv.h, sparse-xpow.cc, sparse-xpow.h, sparse.cc, spparms.cc, sqrtm.cc, str2double.cc, strfind.cc, strfns.cc, sub2ind.cc, svd.cc, sylvester.cc, symtab.cc, symtab.h, syscalls.cc, sysdep.cc, sysdep.h, text-renderer.cc, text-renderer.h, time.cc, toplev.cc, toplev.h, tril.cc, tsearch.cc, txt-eng.cc, txt-eng.h, typecast.cc, urlwrite.cc, utils.cc, utils.h, variables.cc, variables.h, workspace-element.h, xdiv.cc, xdiv.h, xnorm.cc, xnorm.h, xpow.cc, xpow.h, zfstream.cc, zfstream.h, deprecated-config.h, __delaunayn__.cc, __eigs__.cc, __fltk_uigetfile__.cc, __glpk__.cc, __init_fltk__.cc, __init_gnuplot__.cc, __osmesa_print__.cc, __voronoi__.cc, amd.cc, audiodevinfo.cc, audioread.cc, ccolamd.cc, chol.cc, colamd.cc, convhulln.cc, dmperm.cc, fftw.cc, gzip.cc, oct-qhull.h, qr.cc, symbfact.cc, symrcm.cc, liboctinterp-build-info.h, liboctinterp-build-info.in.cc, ov-base-diag.h, ov-base-int.cc, ov-base-int.h, ov-base-mat.cc, ov-base-mat.h, ov-base-scalar.h, ov-base-sparse.cc, ov-base-sparse.h, ov-base.cc, ov-base.h, ov-bool-mat.cc, ov-bool-mat.h, ov-bool-sparse.cc, ov-bool-sparse.h, ov-bool.cc, ov-bool.h, ov-builtin.cc, ov-builtin.h, ov-cell.cc, ov-cell.h, ov-ch-mat.cc, ov-ch-mat.h, ov-class.cc, ov-class.h, ov-classdef.cc, ov-classdef.h, ov-colon.cc, ov-colon.h, ov-complex.cc, ov-complex.h, ov-cs-list.h, ov-cx-diag.cc, ov-cx-diag.h, ov-cx-mat.cc, ov-cx-mat.h, ov-cx-sparse.cc, ov-cx-sparse.h, ov-dld-fcn.cc, ov-dld-fcn.h, ov-fcn-handle.cc, ov-fcn-handle.h, ov-fcn-inline.cc, ov-fcn-inline.h, ov-fcn.cc, ov-fcn.h, ov-float.cc, ov-float.h, ov-flt-complex.cc, ov-flt-complex.h, ov-flt-cx-diag.cc, ov-flt-cx-diag.h, ov-flt-cx-mat.cc, ov-flt-cx-mat.h, ov-flt-re-diag.cc, ov-flt-re-diag.h, ov-flt-re-mat.cc, ov-flt-re-mat.h, ov-int-traits.h, ov-int16.cc, ov-int16.h, ov-int32.cc, ov-int32.h, ov-int64.cc, ov-int64.h, ov-int8.cc, ov-int8.h, ov-intx.h, ov-java.cc, ov-java.h, ov-lazy-idx.cc, ov-lazy-idx.h, ov-mex-fcn.cc, ov-mex-fcn.h, ov-null-mat.cc, ov-null-mat.h, ov-oncleanup.cc, ov-oncleanup.h, ov-perm.cc, ov-perm.h, ov-range.cc, ov-range.h, ov-re-diag.cc, ov-re-diag.h, ov-re-mat.cc, ov-re-mat.h, ov-re-sparse.cc, ov-re-sparse.h, ov-scalar.cc, ov-scalar.h, ov-str-mat.cc, ov-str-mat.h, ov-struct.cc, ov-struct.h, ov-typeinfo.cc, ov-typeinfo.h, ov-uint16.cc, ov-uint16.h, ov-uint32.cc, ov-uint32.h, ov-uint64.cc, ov-uint64.h, ov-uint8.cc, ov-uint8.h, ov-usr-fcn.cc, ov-usr-fcn.h, ov.h, ovl.cc, ovl.h, octave.cc, octave.h, op-b-b.cc, op-b-bm.cc, op-b-sbm.cc, op-bm-b.cc, op-bm-bm.cc, op-bm-sbm.cc, op-cdm-cdm.cc, op-cell.cc, op-chm.cc, op-class.cc, op-cm-cm.cc, op-cm-cs.cc, op-cm-m.cc, op-cm-pm.cc, op-cm-s.cc, op-cm-scm.cc, op-cm-sm.cc, op-cs-cm.cc, op-cs-cs.cc, op-cs-m.cc, op-cs-s.cc, op-cs-scm.cc, op-cs-sm.cc, op-dm-dm.cc, op-dm-scm.cc, op-dm-sm.cc, op-dm-template.cc, op-dms-template.cc, op-fcdm-fcdm.cc, op-fcm-fcm.cc, op-fcm-fcs.cc, op-fcm-fm.cc, op-fcm-fs.cc, op-fcm-pm.cc, op-fcn.cc, op-fcs-fcm.cc, op-fcs-fcs.cc, op-fcs-fm.cc, op-fcs-fs.cc, op-fdm-fdm.cc, op-fm-fcm.cc, op-fm-fcs.cc, op-fm-fm.cc, op-fm-fs.cc, op-fm-pm.cc, op-fs-fcm.cc, op-fs-fcs.cc, op-fs-fm.cc, op-fs-fs.cc, op-i16-i16.cc, op-i32-i32.cc, op-i64-i64.cc, op-i8-i8.cc, op-int-concat.cc, op-int.h, op-m-cm.cc, op-m-cs.cc, op-m-m.cc, op-m-pm.cc, op-m-s.cc, op-m-scm.cc, op-m-sm.cc, op-pm-cm.cc, op-pm-fcm.cc, op-pm-fm.cc, op-pm-m.cc, op-pm-pm.cc, op-pm-scm.cc, op-pm-sm.cc, op-pm-template.cc, op-range.cc, op-s-cm.cc, op-s-cs.cc, op-s-m.cc, op-s-s.cc, op-s-scm.cc, op-s-sm.cc, op-sbm-b.cc, op-sbm-bm.cc, op-sbm-sbm.cc, op-scm-cm.cc, op-scm-cs.cc, op-scm-m.cc, op-scm-s.cc, op-scm-scm.cc, op-scm-sm.cc, op-sm-cm.cc, op-sm-cs.cc, op-sm-m.cc, op-sm-s.cc, op-sm-scm.cc, op-sm-sm.cc, op-str-m.cc, op-str-s.cc, op-str-str.cc, op-struct.cc, op-ui16-ui16.cc, op-ui32-ui32.cc, op-ui64-ui64.cc, op-ui8-ui8.cc, ops.h, options-usage.h, lex.h, parse.h, pt-all.h, pt-arg-list.cc, pt-arg-list.h, pt-array-list.h, pt-assign.cc, pt-assign.h, pt-binop.cc, pt-binop.h, pt-bp.cc, pt-bp.h, pt-cbinop.cc, pt-cbinop.h, pt-cell.cc, pt-cell.h, pt-check.cc, pt-check.h, pt-classdef.cc, pt-classdef.h, pt-cmd.cc, pt-cmd.h, pt-colon.cc, pt-colon.h, pt-const.cc, pt-const.h, pt-decl.cc, pt-decl.h, pt-eval.cc, pt-eval.h, pt-except.cc, pt-except.h, pt-exp.cc, pt-exp.h, pt-fcn-handle.cc, pt-fcn-handle.h, pt-funcall.cc, pt-funcall.h, pt-id.cc, pt-id.h, pt-idx.cc, pt-idx.h, pt-jump.cc, pt-jump.h, pt-loop.cc, pt-loop.h, pt-mat.cc, pt-mat.h, pt-misc.cc, pt-misc.h, pt-pr-code.cc, pt-pr-code.h, pt-select.cc, pt-select.h, pt-stmt.cc, pt-stmt.h, pt-unop.cc, pt-unop.h, pt-walk.h, pt.cc, pt.h, token.cc, token.h, Array-jit.cc, Array-tc.cc, version.cc, version.in.h: Style check C++ code in libinterp/
author Rik <rik@octave.org>
date Tue, 30 Aug 2016 21:46:47 -0700
parents d0562b3159c7
children 3a2b891d0b33 e9a0469dedd9
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 {
22407
34ce5be04942 maint: Style check C++ code in libinterp/.
Rik <rik@octave.org>
parents: 22327
diff changeset
78 if (! octave::math::isinteger (a.real ())
34ce5be04942 maint: Style check C++ code in libinterp/.
Rik <rik@octave.org>
parents: 22327
diff changeset
79 || ! octave::math::isinteger (a.imag ())
34ce5be04942 maint: Style check C++ code in libinterp/.
Rik <rik@octave.org>
parents: 22327
diff changeset
80 || ! octave::math::isinteger (b.real ())
34ce5be04942 maint: Style check C++ code in libinterp/.
Rik <rik@octave.org>
parents: 22327
diff changeset
81 || ! 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
82 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
83
11064
61a6c99d8ff7 gcd.cc: style fixes
John W. Eaton <jwe@octave.org>
parents: 11062
diff changeset
84 std::complex<FP> aa = a;
61a6c99d8ff7 gcd.cc: style fixes
John W. Eaton <jwe@octave.org>
parents: 11062
diff changeset
85 std::complex<FP> bb = b;
11062
88d78ee12ee8 Implement gcd for Gaussian (complex) integers
Jordi Gutiérrez Hermoso <jordigh@gmail.com>
parents: 11061
diff changeset
86
11064
61a6c99d8ff7 gcd.cc: style fixes
John W. Eaton <jwe@octave.org>
parents: 11062
diff changeset
87 if (abs (aa) < abs (bb))
61a6c99d8ff7 gcd.cc: style fixes
John W. Eaton <jwe@octave.org>
parents: 11062
diff changeset
88 std::swap (aa, bb);
11062
88d78ee12ee8 Implement gcd for Gaussian (complex) integers
Jordi Gutiérrez Hermoso <jordigh@gmail.com>
parents: 11061
diff changeset
89
11064
61a6c99d8ff7 gcd.cc: style fixes
John W. Eaton <jwe@octave.org>
parents: 11062
diff changeset
90 while (abs (bb) != 0)
11062
88d78ee12ee8 Implement gcd for Gaussian (complex) integers
Jordi Gutiérrez Hermoso <jordigh@gmail.com>
parents: 11061
diff changeset
91 {
88d78ee12ee8 Implement gcd for Gaussian (complex) integers
Jordi Gutiérrez Hermoso <jordigh@gmail.com>
parents: 11061
diff changeset
92 std::complex<FP> qq, rr;
88d78ee12ee8 Implement gcd for Gaussian (complex) integers
Jordi Gutiérrez Hermoso <jordigh@gmail.com>
parents: 11061
diff changeset
93 divide (aa, bb, qq, rr);
88d78ee12ee8 Implement gcd for Gaussian (complex) integers
Jordi Gutiérrez Hermoso <jordigh@gmail.com>
parents: 11061
diff changeset
94 aa = bb;
88d78ee12ee8 Implement gcd for Gaussian (complex) integers
Jordi Gutiérrez Hermoso <jordigh@gmail.com>
parents: 11061
diff changeset
95 bb = rr;
88d78ee12ee8 Implement gcd for Gaussian (complex) integers
Jordi Gutiérrez Hermoso <jordigh@gmail.com>
parents: 11061
diff changeset
96 }
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 return aa;
88d78ee12ee8 Implement gcd for Gaussian (complex) integers
Jordi Gutiérrez Hermoso <jordigh@gmail.com>
parents: 11061
diff changeset
99 }
88d78ee12ee8 Implement gcd for Gaussian (complex) integers
Jordi Gutiérrez Hermoso <jordigh@gmail.com>
parents: 11061
diff changeset
100
21139
538b57866b90 consistently use "typename" intead of "class" in template declarations
John W. Eaton <jwe@octave.org>
parents: 21100
diff changeset
101 template <typename T>
11021
0ee74d581c00 optimize & rewrite gcd
Jaroslav Hajek <highegg@gmail.com>
parents: 10840
diff changeset
102 static octave_int<T>
0ee74d581c00 optimize & rewrite gcd
Jaroslav Hajek <highegg@gmail.com>
parents: 10840
diff changeset
103 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
104 {
11064
61a6c99d8ff7 gcd.cc: style fixes
John W. Eaton <jwe@octave.org>
parents: 11062
diff changeset
105 T aa = a.abs ().value ();
61a6c99d8ff7 gcd.cc: style fixes
John W. Eaton <jwe@octave.org>
parents: 11062
diff changeset
106 T bb = b.abs ().value ();
61a6c99d8ff7 gcd.cc: style fixes
John W. Eaton <jwe@octave.org>
parents: 11062
diff changeset
107
11021
0ee74d581c00 optimize & rewrite gcd
Jaroslav Hajek <highegg@gmail.com>
parents: 10840
diff changeset
108 while (bb != 0)
0ee74d581c00 optimize & rewrite gcd
Jaroslav Hajek <highegg@gmail.com>
parents: 10840
diff changeset
109 {
0ee74d581c00 optimize & rewrite gcd
Jaroslav Hajek <highegg@gmail.com>
parents: 10840
diff changeset
110 T tt = aa % bb;
11064
61a6c99d8ff7 gcd.cc: style fixes
John W. Eaton <jwe@octave.org>
parents: 11062
diff changeset
111 aa = bb;
61a6c99d8ff7 gcd.cc: style fixes
John W. Eaton <jwe@octave.org>
parents: 11062
diff changeset
112 bb = tt;
11021
0ee74d581c00 optimize & rewrite gcd
Jaroslav Hajek <highegg@gmail.com>
parents: 10840
diff changeset
113 }
0ee74d581c00 optimize & rewrite gcd
Jaroslav Hajek <highegg@gmail.com>
parents: 10840
diff changeset
114
0ee74d581c00 optimize & rewrite gcd
Jaroslav Hajek <highegg@gmail.com>
parents: 10840
diff changeset
115 return aa;
0ee74d581c00 optimize & rewrite gcd
Jaroslav Hajek <highegg@gmail.com>
parents: 10840
diff changeset
116 }
0ee74d581c00 optimize & rewrite gcd
Jaroslav Hajek <highegg@gmail.com>
parents: 10840
diff changeset
117
11062
88d78ee12ee8 Implement gcd for Gaussian (complex) integers
Jordi Gutiérrez Hermoso <jordigh@gmail.com>
parents: 11061
diff changeset
118 static double
11021
0ee74d581c00 optimize & rewrite gcd
Jaroslav Hajek <highegg@gmail.com>
parents: 10840
diff changeset
119 extended_gcd (double a, double b, double& x, double& y)
0ee74d581c00 optimize & rewrite gcd
Jaroslav Hajek <highegg@gmail.com>
parents: 10840
diff changeset
120 {
21782
2aef506f3fec use namespace for lo-mappers.h functions
John W. Eaton <jwe@octave.org>
parents: 21724
diff changeset
121 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
122 error ("gcd: all values must be integers");
11021
0ee74d581c00 optimize & rewrite gcd
Jaroslav Hajek <highegg@gmail.com>
parents: 10840
diff changeset
123
11064
61a6c99d8ff7 gcd.cc: style fixes
John W. Eaton <jwe@octave.org>
parents: 11062
diff changeset
124 double aa = fabs (a);
61a6c99d8ff7 gcd.cc: style fixes
John W. Eaton <jwe@octave.org>
parents: 11062
diff changeset
125 double bb = fabs (b);
61a6c99d8ff7 gcd.cc: style fixes
John W. Eaton <jwe@octave.org>
parents: 11062
diff changeset
126
18100
6a71e5030df5 Follow coding convention of defining and initializing only 1 variable per line in liboctinterp.
Rik <rik@octave.org>
parents: 17787
diff changeset
127 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
128 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
129 yy = 1, ly = 0;
11064
61a6c99d8ff7 gcd.cc: style fixes
John W. Eaton <jwe@octave.org>
parents: 11062
diff changeset
130
11021
0ee74d581c00 optimize & rewrite gcd
Jaroslav Hajek <highegg@gmail.com>
parents: 10840
diff changeset
131 while (bb != 0)
0ee74d581c00 optimize & rewrite gcd
Jaroslav Hajek <highegg@gmail.com>
parents: 10840
diff changeset
132 {
21942
aab79a1885cc limit gnulib headers to liboctave/wrappers directory
John W. Eaton <jwe@octave.org>
parents: 21782
diff changeset
133 double qq = std::floor (aa / bb);
11021
0ee74d581c00 optimize & rewrite gcd
Jaroslav Hajek <highegg@gmail.com>
parents: 10840
diff changeset
134 double tt = fmod (aa, bb);
11064
61a6c99d8ff7 gcd.cc: style fixes
John W. Eaton <jwe@octave.org>
parents: 11062
diff changeset
135
61a6c99d8ff7 gcd.cc: style fixes
John W. Eaton <jwe@octave.org>
parents: 11062
diff changeset
136 aa = bb;
61a6c99d8ff7 gcd.cc: style fixes
John W. Eaton <jwe@octave.org>
parents: 11062
diff changeset
137 bb = tt;
11021
0ee74d581c00 optimize & rewrite gcd
Jaroslav Hajek <highegg@gmail.com>
parents: 10840
diff changeset
138
0ee74d581c00 optimize & rewrite gcd
Jaroslav Hajek <highegg@gmail.com>
parents: 10840
diff changeset
139 double tx = lx - qq*xx;
11064
61a6c99d8ff7 gcd.cc: style fixes
John W. Eaton <jwe@octave.org>
parents: 11062
diff changeset
140 lx = xx;
61a6c99d8ff7 gcd.cc: style fixes
John W. Eaton <jwe@octave.org>
parents: 11062
diff changeset
141 xx = tx;
11021
0ee74d581c00 optimize & rewrite gcd
Jaroslav Hajek <highegg@gmail.com>
parents: 10840
diff changeset
142
0ee74d581c00 optimize & rewrite gcd
Jaroslav Hajek <highegg@gmail.com>
parents: 10840
diff changeset
143 double ty = ly - qq*yy;
11064
61a6c99d8ff7 gcd.cc: style fixes
John W. Eaton <jwe@octave.org>
parents: 11062
diff changeset
144 ly = yy;
61a6c99d8ff7 gcd.cc: style fixes
John W. Eaton <jwe@octave.org>
parents: 11062
diff changeset
145 yy = ty;
11021
0ee74d581c00 optimize & rewrite gcd
Jaroslav Hajek <highegg@gmail.com>
parents: 10840
diff changeset
146 }
4864
d49d761c8c93 [project @ 2004-04-16 16:01:11 by jwe]
jwe
parents:
diff changeset
147
11062
88d78ee12ee8 Implement gcd for Gaussian (complex) integers
Jordi Gutiérrez Hermoso <jordigh@gmail.com>
parents: 11061
diff changeset
148 x = a >= 0 ? lx : -lx;
11021
0ee74d581c00 optimize & rewrite gcd
Jaroslav Hajek <highegg@gmail.com>
parents: 10840
diff changeset
149 y = b >= 0 ? ly : -ly;
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 return aa;
0ee74d581c00 optimize & rewrite gcd
Jaroslav Hajek <highegg@gmail.com>
parents: 10840
diff changeset
152 }
0ee74d581c00 optimize & rewrite gcd
Jaroslav Hajek <highegg@gmail.com>
parents: 10840
diff changeset
153
11062
88d78ee12ee8 Implement gcd for Gaussian (complex) integers
Jordi Gutiérrez Hermoso <jordigh@gmail.com>
parents: 11061
diff changeset
154 template <typename FP>
88d78ee12ee8 Implement gcd for Gaussian (complex) integers
Jordi Gutiérrez Hermoso <jordigh@gmail.com>
parents: 11061
diff changeset
155 static std::complex<FP>
88d78ee12ee8 Implement gcd for Gaussian (complex) integers
Jordi Gutiérrez Hermoso <jordigh@gmail.com>
parents: 11061
diff changeset
156 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
157 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
158 {
22407
34ce5be04942 maint: Style check C++ code in libinterp/.
Rik <rik@octave.org>
parents: 22327
diff changeset
159 if (! octave::math::isinteger (a.real ())
34ce5be04942 maint: Style check C++ code in libinterp/.
Rik <rik@octave.org>
parents: 22327
diff changeset
160 || ! octave::math::isinteger (a.imag ())
34ce5be04942 maint: Style check C++ code in libinterp/.
Rik <rik@octave.org>
parents: 22327
diff changeset
161 || ! octave::math::isinteger (b.real ())
34ce5be04942 maint: Style check C++ code in libinterp/.
Rik <rik@octave.org>
parents: 22327
diff changeset
162 || ! 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
163 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
164
18100
6a71e5030df5 Follow coding convention of defining and initializing only 1 variable per line in liboctinterp.
Rik <rik@octave.org>
parents: 17787
diff changeset
165 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
166 std::complex<FP> bb = b;
11062
88d78ee12ee8 Implement gcd for Gaussian (complex) integers
Jordi Gutiérrez Hermoso <jordigh@gmail.com>
parents: 11061
diff changeset
167 bool swapped = false;
11064
61a6c99d8ff7 gcd.cc: style fixes
John W. Eaton <jwe@octave.org>
parents: 11062
diff changeset
168 if (abs (aa) < abs (bb))
11062
88d78ee12ee8 Implement gcd for Gaussian (complex) integers
Jordi Gutiérrez Hermoso <jordigh@gmail.com>
parents: 11061
diff changeset
169 {
11064
61a6c99d8ff7 gcd.cc: style fixes
John W. Eaton <jwe@octave.org>
parents: 11062
diff changeset
170 std::swap (aa, bb);
11062
88d78ee12ee8 Implement gcd for Gaussian (complex) integers
Jordi Gutiérrez Hermoso <jordigh@gmail.com>
parents: 11061
diff changeset
171 swapped = true;
88d78ee12ee8 Implement gcd for Gaussian (complex) integers
Jordi Gutiérrez Hermoso <jordigh@gmail.com>
parents: 11061
diff changeset
172 }
88d78ee12ee8 Implement gcd for Gaussian (complex) integers
Jordi Gutiérrez Hermoso <jordigh@gmail.com>
parents: 11061
diff changeset
173
18100
6a71e5030df5 Follow coding convention of defining and initializing only 1 variable per line in liboctinterp.
Rik <rik@octave.org>
parents: 17787
diff changeset
174 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
175 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
176 yy = 1, ly = 0;
11064
61a6c99d8ff7 gcd.cc: style fixes
John W. Eaton <jwe@octave.org>
parents: 11062
diff changeset
177
61a6c99d8ff7 gcd.cc: style fixes
John W. Eaton <jwe@octave.org>
parents: 11062
diff changeset
178 while (abs(bb) != 0)
11062
88d78ee12ee8 Implement gcd for Gaussian (complex) integers
Jordi Gutiérrez Hermoso <jordigh@gmail.com>
parents: 11061
diff changeset
179 {
88d78ee12ee8 Implement gcd for Gaussian (complex) integers
Jordi Gutiérrez Hermoso <jordigh@gmail.com>
parents: 11061
diff changeset
180 std::complex<FP> qq, rr;
88d78ee12ee8 Implement gcd for Gaussian (complex) integers
Jordi Gutiérrez Hermoso <jordigh@gmail.com>
parents: 11061
diff changeset
181 divide (aa, bb, qq, rr);
11064
61a6c99d8ff7 gcd.cc: style fixes
John W. Eaton <jwe@octave.org>
parents: 11062
diff changeset
182 aa = bb;
61a6c99d8ff7 gcd.cc: style fixes
John W. Eaton <jwe@octave.org>
parents: 11062
diff changeset
183 bb = rr;
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> tx = lx - qq*xx;
11064
61a6c99d8ff7 gcd.cc: style fixes
John W. Eaton <jwe@octave.org>
parents: 11062
diff changeset
186 lx = xx;
61a6c99d8ff7 gcd.cc: style fixes
John W. Eaton <jwe@octave.org>
parents: 11062
diff changeset
187 xx = tx;
11062
88d78ee12ee8 Implement gcd for Gaussian (complex) integers
Jordi Gutiérrez Hermoso <jordigh@gmail.com>
parents: 11061
diff changeset
188
88d78ee12ee8 Implement gcd for Gaussian (complex) integers
Jordi Gutiérrez Hermoso <jordigh@gmail.com>
parents: 11061
diff changeset
189 std::complex<FP> ty = ly - qq*yy;
11064
61a6c99d8ff7 gcd.cc: style fixes
John W. Eaton <jwe@octave.org>
parents: 11062
diff changeset
190 ly = yy;
61a6c99d8ff7 gcd.cc: style fixes
John W. Eaton <jwe@octave.org>
parents: 11062
diff changeset
191 yy = ty;
11062
88d78ee12ee8 Implement gcd for Gaussian (complex) integers
Jordi Gutiérrez Hermoso <jordigh@gmail.com>
parents: 11061
diff changeset
192 }
88d78ee12ee8 Implement gcd for Gaussian (complex) integers
Jordi Gutiérrez Hermoso <jordigh@gmail.com>
parents: 11061
diff changeset
193
88d78ee12ee8 Implement gcd for Gaussian (complex) integers
Jordi Gutiérrez Hermoso <jordigh@gmail.com>
parents: 11061
diff changeset
194 x = lx;
88d78ee12ee8 Implement gcd for Gaussian (complex) integers
Jordi Gutiérrez Hermoso <jordigh@gmail.com>
parents: 11061
diff changeset
195 y = ly;
11064
61a6c99d8ff7 gcd.cc: style fixes
John W. Eaton <jwe@octave.org>
parents: 11062
diff changeset
196
11062
88d78ee12ee8 Implement gcd for Gaussian (complex) integers
Jordi Gutiérrez Hermoso <jordigh@gmail.com>
parents: 11061
diff changeset
197 if (swapped)
11064
61a6c99d8ff7 gcd.cc: style fixes
John W. Eaton <jwe@octave.org>
parents: 11062
diff changeset
198 std::swap (x, y);
11062
88d78ee12ee8 Implement gcd for Gaussian (complex) integers
Jordi Gutiérrez Hermoso <jordigh@gmail.com>
parents: 11061
diff changeset
199
88d78ee12ee8 Implement gcd for Gaussian (complex) integers
Jordi Gutiérrez Hermoso <jordigh@gmail.com>
parents: 11061
diff changeset
200 return aa;
88d78ee12ee8 Implement gcd for Gaussian (complex) integers
Jordi Gutiérrez Hermoso <jordigh@gmail.com>
parents: 11061
diff changeset
201 }
88d78ee12ee8 Implement gcd for Gaussian (complex) integers
Jordi Gutiérrez Hermoso <jordigh@gmail.com>
parents: 11061
diff changeset
202
21139
538b57866b90 consistently use "typename" intead of "class" in template declarations
John W. Eaton <jwe@octave.org>
parents: 21100
diff changeset
203 template <typename T>
11021
0ee74d581c00 optimize & rewrite gcd
Jaroslav Hajek <highegg@gmail.com>
parents: 10840
diff changeset
204 static octave_int<T>
0ee74d581c00 optimize & rewrite gcd
Jaroslav Hajek <highegg@gmail.com>
parents: 10840
diff changeset
205 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
206 octave_int<T>& x, octave_int<T>& y)
4864
d49d761c8c93 [project @ 2004-04-16 16:01:11 by jwe]
jwe
parents:
diff changeset
207 {
11064
61a6c99d8ff7 gcd.cc: style fixes
John W. Eaton <jwe@octave.org>
parents: 11062
diff changeset
208 T aa = a.abs ().value ();
61a6c99d8ff7 gcd.cc: style fixes
John W. Eaton <jwe@octave.org>
parents: 11062
diff changeset
209 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
210 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
211 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
212 yy = 1, ly = 0;
11064
61a6c99d8ff7 gcd.cc: style fixes
John W. Eaton <jwe@octave.org>
parents: 11062
diff changeset
213
11021
0ee74d581c00 optimize & rewrite gcd
Jaroslav Hajek <highegg@gmail.com>
parents: 10840
diff changeset
214 while (bb != 0)
0ee74d581c00 optimize & rewrite gcd
Jaroslav Hajek <highegg@gmail.com>
parents: 10840
diff changeset
215 {
0ee74d581c00 optimize & rewrite gcd
Jaroslav Hajek <highegg@gmail.com>
parents: 10840
diff changeset
216 T qq = aa / bb;
0ee74d581c00 optimize & rewrite gcd
Jaroslav Hajek <highegg@gmail.com>
parents: 10840
diff changeset
217 T tt = aa % bb;
11064
61a6c99d8ff7 gcd.cc: style fixes
John W. Eaton <jwe@octave.org>
parents: 11062
diff changeset
218 aa = bb;
61a6c99d8ff7 gcd.cc: style fixes
John W. Eaton <jwe@octave.org>
parents: 11062
diff changeset
219 bb = tt;
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 tx = lx - qq*xx;
11064
61a6c99d8ff7 gcd.cc: style fixes
John W. Eaton <jwe@octave.org>
parents: 11062
diff changeset
222 lx = xx;
61a6c99d8ff7 gcd.cc: style fixes
John W. Eaton <jwe@octave.org>
parents: 11062
diff changeset
223 xx = tx;
11021
0ee74d581c00 optimize & rewrite gcd
Jaroslav Hajek <highegg@gmail.com>
parents: 10840
diff changeset
224
0ee74d581c00 optimize & rewrite gcd
Jaroslav Hajek <highegg@gmail.com>
parents: 10840
diff changeset
225 T ty = ly - qq*yy;
11064
61a6c99d8ff7 gcd.cc: style fixes
John W. Eaton <jwe@octave.org>
parents: 11062
diff changeset
226 ly = yy;
61a6c99d8ff7 gcd.cc: style fixes
John W. Eaton <jwe@octave.org>
parents: 11062
diff changeset
227 yy = ty;
11021
0ee74d581c00 optimize & rewrite gcd
Jaroslav Hajek <highegg@gmail.com>
parents: 10840
diff changeset
228 }
0ee74d581c00 optimize & rewrite gcd
Jaroslav Hajek <highegg@gmail.com>
parents: 10840
diff changeset
229
0ee74d581c00 optimize & rewrite gcd
Jaroslav Hajek <highegg@gmail.com>
parents: 10840
diff changeset
230 x = octave_int<T> (lx) * a.signum ();
0ee74d581c00 optimize & rewrite gcd
Jaroslav Hajek <highegg@gmail.com>
parents: 10840
diff changeset
231 y = octave_int<T> (ly) * b.signum ();
0ee74d581c00 optimize & rewrite gcd
Jaroslav Hajek <highegg@gmail.com>
parents: 10840
diff changeset
232
0ee74d581c00 optimize & rewrite gcd
Jaroslav Hajek <highegg@gmail.com>
parents: 10840
diff changeset
233 return aa;
0ee74d581c00 optimize & rewrite gcd
Jaroslav Hajek <highegg@gmail.com>
parents: 10840
diff changeset
234 }
0ee74d581c00 optimize & rewrite gcd
Jaroslav Hajek <highegg@gmail.com>
parents: 10840
diff changeset
235
21139
538b57866b90 consistently use "typename" intead of "class" in template declarations
John W. Eaton <jwe@octave.org>
parents: 21100
diff changeset
236 template <typename NDA>
11021
0ee74d581c00 optimize & rewrite gcd
Jaroslav Hajek <highegg@gmail.com>
parents: 10840
diff changeset
237 static octave_value
0ee74d581c00 optimize & rewrite gcd
Jaroslav Hajek <highegg@gmail.com>
parents: 10840
diff changeset
238 do_simple_gcd (const octave_value& a, const octave_value& b)
0ee74d581c00 optimize & rewrite gcd
Jaroslav Hajek <highegg@gmail.com>
parents: 10840
diff changeset
239 {
0ee74d581c00 optimize & rewrite gcd
Jaroslav Hajek <highegg@gmail.com>
parents: 10840
diff changeset
240 typedef typename NDA::element_type T;
0ee74d581c00 optimize & rewrite gcd
Jaroslav Hajek <highegg@gmail.com>
parents: 10840
diff changeset
241 octave_value retval;
0ee74d581c00 optimize & rewrite gcd
Jaroslav Hajek <highegg@gmail.com>
parents: 10840
diff changeset
242
0ee74d581c00 optimize & rewrite gcd
Jaroslav Hajek <highegg@gmail.com>
parents: 10840
diff changeset
243 if (a.is_scalar_type () && b.is_scalar_type ())
0ee74d581c00 optimize & rewrite gcd
Jaroslav Hajek <highegg@gmail.com>
parents: 10840
diff changeset
244 {
0ee74d581c00 optimize & rewrite gcd
Jaroslav Hajek <highegg@gmail.com>
parents: 10840
diff changeset
245 // Optimize scalar case.
0ee74d581c00 optimize & rewrite gcd
Jaroslav Hajek <highegg@gmail.com>
parents: 10840
diff changeset
246 T aa = octave_value_extract<T> (a);
0ee74d581c00 optimize & rewrite gcd
Jaroslav Hajek <highegg@gmail.com>
parents: 10840
diff changeset
247 T bb = octave_value_extract<T> (b);
0ee74d581c00 optimize & rewrite gcd
Jaroslav Hajek <highegg@gmail.com>
parents: 10840
diff changeset
248 retval = simple_gcd (aa, bb);
0ee74d581c00 optimize & rewrite gcd
Jaroslav Hajek <highegg@gmail.com>
parents: 10840
diff changeset
249 }
0ee74d581c00 optimize & rewrite gcd
Jaroslav Hajek <highegg@gmail.com>
parents: 10840
diff changeset
250 else
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 NDA aa = octave_value_extract<NDA> (a);
0ee74d581c00 optimize & rewrite gcd
Jaroslav Hajek <highegg@gmail.com>
parents: 10840
diff changeset
253 NDA bb = octave_value_extract<NDA> (b);
0ee74d581c00 optimize & rewrite gcd
Jaroslav Hajek <highegg@gmail.com>
parents: 10840
diff changeset
254 retval = binmap<T> (aa, bb, simple_gcd, "gcd");
0ee74d581c00 optimize & rewrite gcd
Jaroslav Hajek <highegg@gmail.com>
parents: 10840
diff changeset
255 }
0ee74d581c00 optimize & rewrite gcd
Jaroslav Hajek <highegg@gmail.com>
parents: 10840
diff changeset
256
0ee74d581c00 optimize & rewrite gcd
Jaroslav Hajek <highegg@gmail.com>
parents: 10840
diff changeset
257 return retval;
4864
d49d761c8c93 [project @ 2004-04-16 16:01:11 by jwe]
jwe
parents:
diff changeset
258 }
d49d761c8c93 [project @ 2004-04-16 16:01:11 by jwe]
jwe
parents:
diff changeset
259
11021
0ee74d581c00 optimize & rewrite gcd
Jaroslav Hajek <highegg@gmail.com>
parents: 10840
diff changeset
260 // Dispatcher
0ee74d581c00 optimize & rewrite gcd
Jaroslav Hajek <highegg@gmail.com>
parents: 10840
diff changeset
261 static octave_value
0ee74d581c00 optimize & rewrite gcd
Jaroslav Hajek <highegg@gmail.com>
parents: 10840
diff changeset
262 do_simple_gcd (const octave_value& a, const octave_value& b)
0ee74d581c00 optimize & rewrite gcd
Jaroslav Hajek <highegg@gmail.com>
parents: 10840
diff changeset
263 {
0ee74d581c00 optimize & rewrite gcd
Jaroslav Hajek <highegg@gmail.com>
parents: 10840
diff changeset
264 octave_value retval;
0ee74d581c00 optimize & rewrite gcd
Jaroslav Hajek <highegg@gmail.com>
parents: 10840
diff changeset
265 builtin_type_t btyp = btyp_mixed_numeric (a.builtin_type (),
0ee74d581c00 optimize & rewrite gcd
Jaroslav Hajek <highegg@gmail.com>
parents: 10840
diff changeset
266 b.builtin_type ());
0ee74d581c00 optimize & rewrite gcd
Jaroslav Hajek <highegg@gmail.com>
parents: 10840
diff changeset
267 switch (btyp)
0ee74d581c00 optimize & rewrite gcd
Jaroslav Hajek <highegg@gmail.com>
parents: 10840
diff changeset
268 {
0ee74d581c00 optimize & rewrite gcd
Jaroslav Hajek <highegg@gmail.com>
parents: 10840
diff changeset
269 case btyp_double:
0ee74d581c00 optimize & rewrite gcd
Jaroslav Hajek <highegg@gmail.com>
parents: 10840
diff changeset
270 if (a.is_sparse_type () && b.is_sparse_type ())
0ee74d581c00 optimize & rewrite gcd
Jaroslav Hajek <highegg@gmail.com>
parents: 10840
diff changeset
271 {
0ee74d581c00 optimize & rewrite gcd
Jaroslav Hajek <highegg@gmail.com>
parents: 10840
diff changeset
272 retval = do_simple_gcd<SparseMatrix> (a, b);
0ee74d581c00 optimize & rewrite gcd
Jaroslav Hajek <highegg@gmail.com>
parents: 10840
diff changeset
273 break;
0ee74d581c00 optimize & rewrite gcd
Jaroslav Hajek <highegg@gmail.com>
parents: 10840
diff changeset
274 }
11064
61a6c99d8ff7 gcd.cc: style fixes
John W. Eaton <jwe@octave.org>
parents: 11062
diff changeset
275 // fall through!
61a6c99d8ff7 gcd.cc: style fixes
John W. Eaton <jwe@octave.org>
parents: 11062
diff changeset
276
11021
0ee74d581c00 optimize & rewrite gcd
Jaroslav Hajek <highegg@gmail.com>
parents: 10840
diff changeset
277 case btyp_float:
0ee74d581c00 optimize & rewrite gcd
Jaroslav Hajek <highegg@gmail.com>
parents: 10840
diff changeset
278 retval = do_simple_gcd<NDArray> (a, b);
0ee74d581c00 optimize & rewrite gcd
Jaroslav Hajek <highegg@gmail.com>
parents: 10840
diff changeset
279 break;
11064
61a6c99d8ff7 gcd.cc: style fixes
John W. Eaton <jwe@octave.org>
parents: 11062
diff changeset
280
22407
34ce5be04942 maint: Style check C++ code in libinterp/.
Rik <rik@octave.org>
parents: 22327
diff changeset
281 #define MAKE_INT_BRANCH(X) \
34ce5be04942 maint: Style check C++ code in libinterp/.
Rik <rik@octave.org>
parents: 22327
diff changeset
282 case btyp_ ## X: \
34ce5be04942 maint: Style check C++ code in libinterp/.
Rik <rik@octave.org>
parents: 22327
diff changeset
283 retval = do_simple_gcd<X ## NDArray> (a, b); \
34ce5be04942 maint: Style check C++ code in libinterp/.
Rik <rik@octave.org>
parents: 22327
diff changeset
284 break
11064
61a6c99d8ff7 gcd.cc: style fixes
John W. Eaton <jwe@octave.org>
parents: 11062
diff changeset
285
11021
0ee74d581c00 optimize & rewrite gcd
Jaroslav Hajek <highegg@gmail.com>
parents: 10840
diff changeset
286 MAKE_INT_BRANCH (int8);
0ee74d581c00 optimize & rewrite gcd
Jaroslav Hajek <highegg@gmail.com>
parents: 10840
diff changeset
287 MAKE_INT_BRANCH (int16);
0ee74d581c00 optimize & rewrite gcd
Jaroslav Hajek <highegg@gmail.com>
parents: 10840
diff changeset
288 MAKE_INT_BRANCH (int32);
0ee74d581c00 optimize & rewrite gcd
Jaroslav Hajek <highegg@gmail.com>
parents: 10840
diff changeset
289 MAKE_INT_BRANCH (int64);
0ee74d581c00 optimize & rewrite gcd
Jaroslav Hajek <highegg@gmail.com>
parents: 10840
diff changeset
290 MAKE_INT_BRANCH (uint8);
0ee74d581c00 optimize & rewrite gcd
Jaroslav Hajek <highegg@gmail.com>
parents: 10840
diff changeset
291 MAKE_INT_BRANCH (uint16);
0ee74d581c00 optimize & rewrite gcd
Jaroslav Hajek <highegg@gmail.com>
parents: 10840
diff changeset
292 MAKE_INT_BRANCH (uint32);
0ee74d581c00 optimize & rewrite gcd
Jaroslav Hajek <highegg@gmail.com>
parents: 10840
diff changeset
293 MAKE_INT_BRANCH (uint64);
11064
61a6c99d8ff7 gcd.cc: style fixes
John W. Eaton <jwe@octave.org>
parents: 11062
diff changeset
294
11021
0ee74d581c00 optimize & rewrite gcd
Jaroslav Hajek <highegg@gmail.com>
parents: 10840
diff changeset
295 #undef MAKE_INT_BRANCH
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_complex:
11064
61a6c99d8ff7 gcd.cc: style fixes
John W. Eaton <jwe@octave.org>
parents: 11062
diff changeset
298 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
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 case btyp_float_complex:
11064
61a6c99d8ff7 gcd.cc: style fixes
John W. Eaton <jwe@octave.org>
parents: 11062
diff changeset
302 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
303 break;
11064
61a6c99d8ff7 gcd.cc: style fixes
John W. Eaton <jwe@octave.org>
parents: 11062
diff changeset
304
11021
0ee74d581c00 optimize & rewrite gcd
Jaroslav Hajek <highegg@gmail.com>
parents: 10840
diff changeset
305 default:
0ee74d581c00 optimize & rewrite gcd
Jaroslav Hajek <highegg@gmail.com>
parents: 10840
diff changeset
306 error ("gcd: invalid class combination for gcd: %s and %s\n",
0ee74d581c00 optimize & rewrite gcd
Jaroslav Hajek <highegg@gmail.com>
parents: 10840
diff changeset
307 a.class_name ().c_str (), b.class_name ().c_str ());
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
0ee74d581c00 optimize & rewrite gcd
Jaroslav Hajek <highegg@gmail.com>
parents: 10840
diff changeset
310 if (btyp == btyp_float)
0ee74d581c00 optimize & rewrite gcd
Jaroslav Hajek <highegg@gmail.com>
parents: 10840
diff changeset
311 retval = retval.float_array_value ();
0ee74d581c00 optimize & rewrite gcd
Jaroslav Hajek <highegg@gmail.com>
parents: 10840
diff changeset
312
0ee74d581c00 optimize & rewrite gcd
Jaroslav Hajek <highegg@gmail.com>
parents: 10840
diff changeset
313 return retval;
0ee74d581c00 optimize & rewrite gcd
Jaroslav Hajek <highegg@gmail.com>
parents: 10840
diff changeset
314 }
0ee74d581c00 optimize & rewrite gcd
Jaroslav Hajek <highegg@gmail.com>
parents: 10840
diff changeset
315
21139
538b57866b90 consistently use "typename" intead of "class" in template declarations
John W. Eaton <jwe@octave.org>
parents: 21100
diff changeset
316 template <typename NDA>
11021
0ee74d581c00 optimize & rewrite gcd
Jaroslav Hajek <highegg@gmail.com>
parents: 10840
diff changeset
317 static octave_value
0ee74d581c00 optimize & rewrite gcd
Jaroslav Hajek <highegg@gmail.com>
parents: 10840
diff changeset
318 do_extended_gcd (const octave_value& a, const octave_value& b,
0ee74d581c00 optimize & rewrite gcd
Jaroslav Hajek <highegg@gmail.com>
parents: 10840
diff changeset
319 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
320 {
11021
0ee74d581c00 optimize & rewrite gcd
Jaroslav Hajek <highegg@gmail.com>
parents: 10840
diff changeset
321 typedef typename NDA::element_type T;
0ee74d581c00 optimize & rewrite gcd
Jaroslav Hajek <highegg@gmail.com>
parents: 10840
diff changeset
322 octave_value retval;
0ee74d581c00 optimize & rewrite gcd
Jaroslav Hajek <highegg@gmail.com>
parents: 10840
diff changeset
323
0ee74d581c00 optimize & rewrite gcd
Jaroslav Hajek <highegg@gmail.com>
parents: 10840
diff changeset
324 if (a.is_scalar_type () && b.is_scalar_type ())
0ee74d581c00 optimize & rewrite gcd
Jaroslav Hajek <highegg@gmail.com>
parents: 10840
diff changeset
325 {
0ee74d581c00 optimize & rewrite gcd
Jaroslav Hajek <highegg@gmail.com>
parents: 10840
diff changeset
326 // Optimize scalar case.
0ee74d581c00 optimize & rewrite gcd
Jaroslav Hajek <highegg@gmail.com>
parents: 10840
diff changeset
327 T aa = octave_value_extract<T> (a);
0ee74d581c00 optimize & rewrite gcd
Jaroslav Hajek <highegg@gmail.com>
parents: 10840
diff changeset
328 T bb = octave_value_extract<T> (b);
0ee74d581c00 optimize & rewrite gcd
Jaroslav Hajek <highegg@gmail.com>
parents: 10840
diff changeset
329 T xx, yy;
0ee74d581c00 optimize & rewrite gcd
Jaroslav Hajek <highegg@gmail.com>
parents: 10840
diff changeset
330 retval = extended_gcd (aa, bb, xx, yy);
0ee74d581c00 optimize & rewrite gcd
Jaroslav Hajek <highegg@gmail.com>
parents: 10840
diff changeset
331 x = xx;
0ee74d581c00 optimize & rewrite gcd
Jaroslav Hajek <highegg@gmail.com>
parents: 10840
diff changeset
332 y = yy;
0ee74d581c00 optimize & rewrite gcd
Jaroslav Hajek <highegg@gmail.com>
parents: 10840
diff changeset
333 }
0ee74d581c00 optimize & rewrite gcd
Jaroslav Hajek <highegg@gmail.com>
parents: 10840
diff changeset
334 else
0ee74d581c00 optimize & rewrite gcd
Jaroslav Hajek <highegg@gmail.com>
parents: 10840
diff changeset
335 {
0ee74d581c00 optimize & rewrite gcd
Jaroslav Hajek <highegg@gmail.com>
parents: 10840
diff changeset
336 NDA aa = octave_value_extract<NDA> (a);
0ee74d581c00 optimize & rewrite gcd
Jaroslav Hajek <highegg@gmail.com>
parents: 10840
diff changeset
337 NDA bb = octave_value_extract<NDA> (b);
0ee74d581c00 optimize & rewrite gcd
Jaroslav Hajek <highegg@gmail.com>
parents: 10840
diff changeset
338
0ee74d581c00 optimize & rewrite gcd
Jaroslav Hajek <highegg@gmail.com>
parents: 10840
diff changeset
339 dim_vector dv = aa.dims ();
0ee74d581c00 optimize & rewrite gcd
Jaroslav Hajek <highegg@gmail.com>
parents: 10840
diff changeset
340 if (aa.numel () == 1)
0ee74d581c00 optimize & rewrite gcd
Jaroslav Hajek <highegg@gmail.com>
parents: 10840
diff changeset
341 dv = bb.dims ();
0ee74d581c00 optimize & rewrite gcd
Jaroslav Hajek <highegg@gmail.com>
parents: 10840
diff changeset
342 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
343 octave::err_nonconformant ("gcd", a.dims (), b.dims ());
11021
0ee74d581c00 optimize & rewrite gcd
Jaroslav Hajek <highegg@gmail.com>
parents: 10840
diff changeset
344
0ee74d581c00 optimize & rewrite gcd
Jaroslav Hajek <highegg@gmail.com>
parents: 10840
diff changeset
345 NDA gg (dv), xx (dv), yy (dv);
0ee74d581c00 optimize & rewrite gcd
Jaroslav Hajek <highegg@gmail.com>
parents: 10840
diff changeset
346
11064
61a6c99d8ff7 gcd.cc: style fixes
John W. Eaton <jwe@octave.org>
parents: 11062
diff changeset
347 const T *aptr = aa.fortran_vec ();
61a6c99d8ff7 gcd.cc: style fixes
John W. Eaton <jwe@octave.org>
parents: 11062
diff changeset
348 const T *bptr = bb.fortran_vec ();
11021
0ee74d581c00 optimize & rewrite gcd
Jaroslav Hajek <highegg@gmail.com>
parents: 10840
diff changeset
349
11064
61a6c99d8ff7 gcd.cc: style fixes
John W. Eaton <jwe@octave.org>
parents: 11062
diff changeset
350 bool inca = aa.numel () != 1;
61a6c99d8ff7 gcd.cc: style fixes
John W. Eaton <jwe@octave.org>
parents: 11062
diff changeset
351 bool incb = bb.numel () != 1;
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 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
354 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
355 T *yptr = yy.fortran_vec ();
11021
0ee74d581c00 optimize & rewrite gcd
Jaroslav Hajek <highegg@gmail.com>
parents: 10840
diff changeset
356
0ee74d581c00 optimize & rewrite gcd
Jaroslav Hajek <highegg@gmail.com>
parents: 10840
diff changeset
357 octave_idx_type n = gg.numel ();
0ee74d581c00 optimize & rewrite gcd
Jaroslav Hajek <highegg@gmail.com>
parents: 10840
diff changeset
358 for (octave_idx_type i = 0; i < n; i++)
0ee74d581c00 optimize & rewrite gcd
Jaroslav Hajek <highegg@gmail.com>
parents: 10840
diff changeset
359 {
0ee74d581c00 optimize & rewrite gcd
Jaroslav Hajek <highegg@gmail.com>
parents: 10840
diff changeset
360 octave_quit ();
0ee74d581c00 optimize & rewrite gcd
Jaroslav Hajek <highegg@gmail.com>
parents: 10840
diff changeset
361
0ee74d581c00 optimize & rewrite gcd
Jaroslav Hajek <highegg@gmail.com>
parents: 10840
diff changeset
362 *gptr++ = extended_gcd (*aptr, *bptr, *xptr++, *yptr++);
11064
61a6c99d8ff7 gcd.cc: style fixes
John W. Eaton <jwe@octave.org>
parents: 11062
diff changeset
363
11021
0ee74d581c00 optimize & rewrite gcd
Jaroslav Hajek <highegg@gmail.com>
parents: 10840
diff changeset
364 aptr += inca;
0ee74d581c00 optimize & rewrite gcd
Jaroslav Hajek <highegg@gmail.com>
parents: 10840
diff changeset
365 bptr += incb;
0ee74d581c00 optimize & rewrite gcd
Jaroslav Hajek <highegg@gmail.com>
parents: 10840
diff changeset
366 }
0ee74d581c00 optimize & rewrite gcd
Jaroslav Hajek <highegg@gmail.com>
parents: 10840
diff changeset
367
0ee74d581c00 optimize & rewrite gcd
Jaroslav Hajek <highegg@gmail.com>
parents: 10840
diff changeset
368 x = xx;
0ee74d581c00 optimize & rewrite gcd
Jaroslav Hajek <highegg@gmail.com>
parents: 10840
diff changeset
369 y = yy;
11064
61a6c99d8ff7 gcd.cc: style fixes
John W. Eaton <jwe@octave.org>
parents: 11062
diff changeset
370
11021
0ee74d581c00 optimize & rewrite gcd
Jaroslav Hajek <highegg@gmail.com>
parents: 10840
diff changeset
371 retval = gg;
0ee74d581c00 optimize & rewrite gcd
Jaroslav Hajek <highegg@gmail.com>
parents: 10840
diff changeset
372 }
0ee74d581c00 optimize & rewrite gcd
Jaroslav Hajek <highegg@gmail.com>
parents: 10840
diff changeset
373
0ee74d581c00 optimize & rewrite gcd
Jaroslav Hajek <highegg@gmail.com>
parents: 10840
diff changeset
374 return retval;
7815
a41df65f3f00 Add some single precision test code and fix resulting bugs
David Bateman <dbateman@free.fr>
parents: 7017
diff changeset
375 }
a41df65f3f00 Add some single precision test code and fix resulting bugs
David Bateman <dbateman@free.fr>
parents: 7017
diff changeset
376
11021
0ee74d581c00 optimize & rewrite gcd
Jaroslav Hajek <highegg@gmail.com>
parents: 10840
diff changeset
377 // Dispatcher
0ee74d581c00 optimize & rewrite gcd
Jaroslav Hajek <highegg@gmail.com>
parents: 10840
diff changeset
378 static octave_value
0ee74d581c00 optimize & rewrite gcd
Jaroslav Hajek <highegg@gmail.com>
parents: 10840
diff changeset
379 do_extended_gcd (const octave_value& a, const octave_value& b,
0ee74d581c00 optimize & rewrite gcd
Jaroslav Hajek <highegg@gmail.com>
parents: 10840
diff changeset
380 octave_value& x, octave_value& y)
0ee74d581c00 optimize & rewrite gcd
Jaroslav Hajek <highegg@gmail.com>
parents: 10840
diff changeset
381 {
0ee74d581c00 optimize & rewrite gcd
Jaroslav Hajek <highegg@gmail.com>
parents: 10840
diff changeset
382 octave_value retval;
11064
61a6c99d8ff7 gcd.cc: style fixes
John W. Eaton <jwe@octave.org>
parents: 11062
diff changeset
383
11021
0ee74d581c00 optimize & rewrite gcd
Jaroslav Hajek <highegg@gmail.com>
parents: 10840
diff changeset
384 builtin_type_t btyp = btyp_mixed_numeric (a.builtin_type (),
0ee74d581c00 optimize & rewrite gcd
Jaroslav Hajek <highegg@gmail.com>
parents: 10840
diff changeset
385 b.builtin_type ());
0ee74d581c00 optimize & rewrite gcd
Jaroslav Hajek <highegg@gmail.com>
parents: 10840
diff changeset
386 switch (btyp)
0ee74d581c00 optimize & rewrite gcd
Jaroslav Hajek <highegg@gmail.com>
parents: 10840
diff changeset
387 {
0ee74d581c00 optimize & rewrite gcd
Jaroslav Hajek <highegg@gmail.com>
parents: 10840
diff changeset
388 case btyp_double:
0ee74d581c00 optimize & rewrite gcd
Jaroslav Hajek <highegg@gmail.com>
parents: 10840
diff changeset
389 case btyp_float:
0ee74d581c00 optimize & rewrite gcd
Jaroslav Hajek <highegg@gmail.com>
parents: 10840
diff changeset
390 retval = do_extended_gcd<NDArray> (a, b, x, y);
0ee74d581c00 optimize & rewrite gcd
Jaroslav Hajek <highegg@gmail.com>
parents: 10840
diff changeset
391 break;
11064
61a6c99d8ff7 gcd.cc: style fixes
John W. Eaton <jwe@octave.org>
parents: 11062
diff changeset
392
22407
34ce5be04942 maint: Style check C++ code in libinterp/.
Rik <rik@octave.org>
parents: 22327
diff changeset
393 #define MAKE_INT_BRANCH(X) \
34ce5be04942 maint: Style check C++ code in libinterp/.
Rik <rik@octave.org>
parents: 22327
diff changeset
394 case btyp_ ## X: \
34ce5be04942 maint: Style check C++ code in libinterp/.
Rik <rik@octave.org>
parents: 22327
diff changeset
395 retval = do_extended_gcd<X ## NDArray> (a, b, x, y); \
34ce5be04942 maint: Style check C++ code in libinterp/.
Rik <rik@octave.org>
parents: 22327
diff changeset
396 break
11064
61a6c99d8ff7 gcd.cc: style fixes
John W. Eaton <jwe@octave.org>
parents: 11062
diff changeset
397
11021
0ee74d581c00 optimize & rewrite gcd
Jaroslav Hajek <highegg@gmail.com>
parents: 10840
diff changeset
398 MAKE_INT_BRANCH (int8);
0ee74d581c00 optimize & rewrite gcd
Jaroslav Hajek <highegg@gmail.com>
parents: 10840
diff changeset
399 MAKE_INT_BRANCH (int16);
0ee74d581c00 optimize & rewrite gcd
Jaroslav Hajek <highegg@gmail.com>
parents: 10840
diff changeset
400 MAKE_INT_BRANCH (int32);
0ee74d581c00 optimize & rewrite gcd
Jaroslav Hajek <highegg@gmail.com>
parents: 10840
diff changeset
401 MAKE_INT_BRANCH (int64);
0ee74d581c00 optimize & rewrite gcd
Jaroslav Hajek <highegg@gmail.com>
parents: 10840
diff changeset
402 MAKE_INT_BRANCH (uint8);
0ee74d581c00 optimize & rewrite gcd
Jaroslav Hajek <highegg@gmail.com>
parents: 10840
diff changeset
403 MAKE_INT_BRANCH (uint16);
0ee74d581c00 optimize & rewrite gcd
Jaroslav Hajek <highegg@gmail.com>
parents: 10840
diff changeset
404 MAKE_INT_BRANCH (uint32);
0ee74d581c00 optimize & rewrite gcd
Jaroslav Hajek <highegg@gmail.com>
parents: 10840
diff changeset
405 MAKE_INT_BRANCH (uint64);
11064
61a6c99d8ff7 gcd.cc: style fixes
John W. Eaton <jwe@octave.org>
parents: 11062
diff changeset
406
11021
0ee74d581c00 optimize & rewrite gcd
Jaroslav Hajek <highegg@gmail.com>
parents: 10840
diff changeset
407 #undef MAKE_INT_BRANCH
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_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<ComplexNDArray> (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 case btyp_float_complex:
11062
88d78ee12ee8 Implement gcd for Gaussian (complex) integers
Jordi Gutiérrez Hermoso <jordigh@gmail.com>
parents: 11061
diff changeset
414 retval = do_extended_gcd<FloatComplexNDArray> (a, b, x, y);
88d78ee12ee8 Implement gcd for Gaussian (complex) integers
Jordi Gutiérrez Hermoso <jordigh@gmail.com>
parents: 11061
diff changeset
415 break;
11064
61a6c99d8ff7 gcd.cc: style fixes
John W. Eaton <jwe@octave.org>
parents: 11062
diff changeset
416
11021
0ee74d581c00 optimize & rewrite gcd
Jaroslav Hajek <highegg@gmail.com>
parents: 10840
diff changeset
417 default:
0ee74d581c00 optimize & rewrite gcd
Jaroslav Hajek <highegg@gmail.com>
parents: 10840
diff changeset
418 error ("gcd: invalid class combination for gcd: %s and %s\n",
0ee74d581c00 optimize & rewrite gcd
Jaroslav Hajek <highegg@gmail.com>
parents: 10840
diff changeset
419 a.class_name ().c_str (), b.class_name ().c_str ());
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
0ee74d581c00 optimize & rewrite gcd
Jaroslav Hajek <highegg@gmail.com>
parents: 10840
diff changeset
422 // For consistency.
20555
f90c8372b7ba eliminate many more simple uses of error_state
John W. Eaton <jwe@octave.org>
parents: 20172
diff changeset
423 if (a.is_sparse_type () && b.is_sparse_type ())
11021
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 retval = retval.sparse_matrix_value ();
0ee74d581c00 optimize & rewrite gcd
Jaroslav Hajek <highegg@gmail.com>
parents: 10840
diff changeset
426 x = x.sparse_matrix_value ();
0ee74d581c00 optimize & rewrite gcd
Jaroslav Hajek <highegg@gmail.com>
parents: 10840
diff changeset
427 y = y.sparse_matrix_value ();
0ee74d581c00 optimize & rewrite gcd
Jaroslav Hajek <highegg@gmail.com>
parents: 10840
diff changeset
428 }
0ee74d581c00 optimize & rewrite gcd
Jaroslav Hajek <highegg@gmail.com>
parents: 10840
diff changeset
429
0ee74d581c00 optimize & rewrite gcd
Jaroslav Hajek <highegg@gmail.com>
parents: 10840
diff changeset
430 if (btyp == btyp_float)
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 retval = retval.float_array_value ();
0ee74d581c00 optimize & rewrite gcd
Jaroslav Hajek <highegg@gmail.com>
parents: 10840
diff changeset
433 x = x.float_array_value ();
0ee74d581c00 optimize & rewrite gcd
Jaroslav Hajek <highegg@gmail.com>
parents: 10840
diff changeset
434 y = y.float_array_value ();
0ee74d581c00 optimize & rewrite gcd
Jaroslav Hajek <highegg@gmail.com>
parents: 10840
diff changeset
435 }
0ee74d581c00 optimize & rewrite gcd
Jaroslav Hajek <highegg@gmail.com>
parents: 10840
diff changeset
436
0ee74d581c00 optimize & rewrite gcd
Jaroslav Hajek <highegg@gmail.com>
parents: 10840
diff changeset
437 return retval;
0ee74d581c00 optimize & rewrite gcd
Jaroslav Hajek <highegg@gmail.com>
parents: 10840
diff changeset
438 }
0ee74d581c00 optimize & rewrite gcd
Jaroslav Hajek <highegg@gmail.com>
parents: 10840
diff changeset
439
15039
e753177cde93 maint: Move non-dynamically linked functions from DLD-FUNCTIONS/ to corefcn/ directory
Rik <rik@octave.org>
parents: 14854
diff changeset
440 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
441 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
442 @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
443 @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
444 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
445
112b20240c87 move docstrings in C++ files out of C strings and into comments
John W. Eaton <jwe@octave.org>
parents: 21942
diff changeset
446 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
447 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
448 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
449 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
450 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
451 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
452
112b20240c87 move docstrings in C++ files out of C strings and into comments
John W. Eaton <jwe@octave.org>
parents: 21942
diff changeset
453 Optional return arguments @var{v1}, @dots{}, contain integer vectors such
112b20240c87 move docstrings in C++ files out of C strings and into comments
John W. Eaton <jwe@octave.org>
parents: 21942
diff changeset
454 that,
112b20240c87 move docstrings in C++ files out of C strings and into comments
John W. Eaton <jwe@octave.org>
parents: 21942
diff changeset
455
112b20240c87 move docstrings in C++ files out of C strings and into comments
John W. Eaton <jwe@octave.org>
parents: 21942
diff changeset
456 @tex
112b20240c87 move docstrings in C++ files out of C strings and into comments
John W. Eaton <jwe@octave.org>
parents: 21942
diff changeset
457 $g = v_1 a_1 + v_2 a_2 + \cdots$
112b20240c87 move docstrings in C++ files out of C strings and into comments
John W. Eaton <jwe@octave.org>
parents: 21942
diff changeset
458 @end tex
112b20240c87 move docstrings in C++ files out of C strings and into comments
John W. Eaton <jwe@octave.org>
parents: 21942
diff changeset
459 @ifnottex
112b20240c87 move docstrings in C++ files out of C strings and into comments
John W. Eaton <jwe@octave.org>
parents: 21942
diff changeset
460
112b20240c87 move docstrings in C++ files out of C strings and into comments
John W. Eaton <jwe@octave.org>
parents: 21942
diff changeset
461 @example
112b20240c87 move docstrings in C++ files out of C strings and into comments
John W. Eaton <jwe@octave.org>
parents: 21942
diff changeset
462 @var{g} = @var{v1} .* @var{a1} + @var{v2} .* @var{a2} + @dots{}
112b20240c87 move docstrings in C++ files out of C strings and into comments
John W. Eaton <jwe@octave.org>
parents: 21942
diff changeset
463 @end example
112b20240c87 move docstrings in C++ files out of C strings and into comments
John W. Eaton <jwe@octave.org>
parents: 21942
diff changeset
464
112b20240c87 move docstrings in C++ files out of C strings and into comments
John W. Eaton <jwe@octave.org>
parents: 21942
diff changeset
465 @end ifnottex
112b20240c87 move docstrings in C++ files out of C strings and into comments
John W. Eaton <jwe@octave.org>
parents: 21942
diff changeset
466
112b20240c87 move docstrings in C++ files out of C strings and into comments
John W. Eaton <jwe@octave.org>
parents: 21942
diff changeset
467 Example code:
112b20240c87 move docstrings in C++ files out of C strings and into comments
John W. Eaton <jwe@octave.org>
parents: 21942
diff changeset
468
112b20240c87 move docstrings in C++ files out of C strings and into comments
John W. Eaton <jwe@octave.org>
parents: 21942
diff changeset
469 @example
112b20240c87 move docstrings in C++ files out of C strings and into comments
John W. Eaton <jwe@octave.org>
parents: 21942
diff changeset
470 @group
112b20240c87 move docstrings in C++ files out of C strings and into comments
John W. Eaton <jwe@octave.org>
parents: 21942
diff changeset
471 gcd ([15, 9], [20, 18])
112b20240c87 move docstrings in C++ files out of C strings and into comments
John W. Eaton <jwe@octave.org>
parents: 21942
diff changeset
472 @result{} 5 9
112b20240c87 move docstrings in C++ files out of C strings and into comments
John W. Eaton <jwe@octave.org>
parents: 21942
diff changeset
473 @end group
112b20240c87 move docstrings in C++ files out of C strings and into comments
John W. Eaton <jwe@octave.org>
parents: 21942
diff changeset
474 @end example
112b20240c87 move docstrings in C++ files out of C strings and into comments
John W. Eaton <jwe@octave.org>
parents: 21942
diff changeset
475
112b20240c87 move docstrings in C++ files out of C strings and into comments
John W. Eaton <jwe@octave.org>
parents: 21942
diff changeset
476 @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
477 @end deftypefn */)
4864
d49d761c8c93 [project @ 2004-04-16 16:01:11 by jwe]
jwe
parents:
diff changeset
478 {
d49d761c8c93 [project @ 2004-04-16 16:01:11 by jwe]
jwe
parents:
diff changeset
479 int nargin = args.length ();
d49d761c8c93 [project @ 2004-04-16 16:01:11 by jwe]
jwe
parents:
diff changeset
480
20802
8bb38ba1bad6 eliminate return statements after calls to print_usage
John W. Eaton <jwe@octave.org>
parents: 20650
diff changeset
481 if (nargin < 2)
8bb38ba1bad6 eliminate return statements after calls to print_usage
John W. Eaton <jwe@octave.org>
parents: 20650
diff changeset
482 print_usage ();
8bb38ba1bad6 eliminate return statements after calls to print_usage
John W. Eaton <jwe@octave.org>
parents: 20650
diff changeset
483
20939
b17fda023ca6 maint: Use new C++ archetype in more files.
Rik <rik@octave.org>
parents: 20853
diff changeset
484 octave_value_list retval;
b17fda023ca6 maint: Use new C++ archetype in more files.
Rik <rik@octave.org>
parents: 20853
diff changeset
485
20802
8bb38ba1bad6 eliminate return statements after calls to print_usage
John W. Eaton <jwe@octave.org>
parents: 20650
diff changeset
486 if (nargout > 1)
4864
d49d761c8c93 [project @ 2004-04-16 16:01:11 by jwe]
jwe
parents:
diff changeset
487 {
20802
8bb38ba1bad6 eliminate return statements after calls to print_usage
John W. Eaton <jwe@octave.org>
parents: 20650
diff changeset
488 retval.resize (nargin + 1);
11064
61a6c99d8ff7 gcd.cc: style fixes
John W. Eaton <jwe@octave.org>
parents: 11062
diff changeset
489
20802
8bb38ba1bad6 eliminate return statements after calls to print_usage
John W. Eaton <jwe@octave.org>
parents: 20650
diff changeset
490 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
491
8bb38ba1bad6 eliminate return statements after calls to print_usage
John W. Eaton <jwe@octave.org>
parents: 20650
diff changeset
492 for (int j = 2; j < nargin; j++)
10154
40dfc0c99116 DLD-FUNCTIONS/*.cc: untabify
John W. Eaton <jwe@octave.org>
parents: 9167
diff changeset
493 {
20802
8bb38ba1bad6 eliminate return statements after calls to print_usage
John W. Eaton <jwe@octave.org>
parents: 20650
diff changeset
494 octave_value x;
20939
b17fda023ca6 maint: Use new C++ archetype in more files.
Rik <rik@octave.org>
parents: 20853
diff changeset
495 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
496 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
497 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
498 }
7815
a41df65f3f00 Add some single precision test code and fix resulting bugs
David Bateman <dbateman@free.fr>
parents: 7017
diff changeset
499 }
4864
d49d761c8c93 [project @ 2004-04-16 16:01:11 by jwe]
jwe
parents:
diff changeset
500 else
20802
8bb38ba1bad6 eliminate return statements after calls to print_usage
John W. Eaton <jwe@octave.org>
parents: 20650
diff changeset
501 {
8bb38ba1bad6 eliminate return statements after calls to print_usage
John W. Eaton <jwe@octave.org>
parents: 20650
diff changeset
502 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
503
8bb38ba1bad6 eliminate return statements after calls to print_usage
John W. Eaton <jwe@octave.org>
parents: 20650
diff changeset
504 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
505 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
506 }
4864
d49d761c8c93 [project @ 2004-04-16 16:01:11 by jwe]
jwe
parents:
diff changeset
507
d49d761c8c93 [project @ 2004-04-16 16:01:11 by jwe]
jwe
parents:
diff changeset
508 return retval;
d49d761c8c93 [project @ 2004-04-16 16:01:11 by jwe]
jwe
parents:
diff changeset
509 }
d49d761c8c93 [project @ 2004-04-16 16:01:11 by jwe]
jwe
parents:
diff changeset
510
d49d761c8c93 [project @ 2004-04-16 16:01:11 by jwe]
jwe
parents:
diff changeset
511 /*
14501
60e5cf354d80 Update %!tests in DLD-FUNCTIONS/ directory with Octave coding conventions.
Rik <octave@nomad.inbox5.com>
parents: 14360
diff changeset
512 %!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
513 %!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
514 %!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
515 %!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
516
19132
c3bdd640a4dd * gcd.cc: add more tests
Jordi Gutiérrez Hermoso <jordigh@octave.org>
parents: 18100
diff changeset
517 %!test
c3bdd640a4dd * gcd.cc: add more tests
Jordi Gutiérrez Hermoso <jordigh@octave.org>
parents: 18100
diff changeset
518 %! p = [953 967];
c3bdd640a4dd * gcd.cc: add more tests
Jordi Gutiérrez Hermoso <jordigh@octave.org>
parents: 18100
diff changeset
519 %! u = [953 + i*971, 967 + i*977];
c3bdd640a4dd * gcd.cc: add more tests
Jordi Gutiérrez Hermoso <jordigh@octave.org>
parents: 18100
diff changeset
520 %! [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
521 %! [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
522 %! assert (d, 1);
ecce63c99c3f maint: Add semicolons to terminate code in %! blocks.
Rik <rik@octave.org>
parents: 21301
diff changeset
523 %! assert (sum (p.*k), d);
ecce63c99c3f maint: Add semicolons to terminate code in %! blocks.
Rik <rik@octave.org>
parents: 21301
diff changeset
524 %! assert (abs (z), sqrt (2));
ecce63c99c3f maint: Add semicolons to terminate code in %! blocks.
Rik <rik@octave.org>
parents: 21301
diff changeset
525 %! 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
526
21581
6fab85c1538f maint: Follow Octave conventions for use of semicolon in BIST tests.
Rik <rik@octave.org>
parents: 21580
diff changeset
527 %!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
528 %!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
529
14501
60e5cf354d80 Update %!tests in DLD-FUNCTIONS/ directory with Octave coding conventions.
Rik <octave@nomad.inbox5.com>
parents: 14360
diff changeset
530 %!error gcd ()
7815
a41df65f3f00 Add some single precision test code and fix resulting bugs
David Bateman <dbateman@free.fr>
parents: 7017
diff changeset
531
a41df65f3f00 Add some single precision test code and fix resulting bugs
David Bateman <dbateman@free.fr>
parents: 7017
diff changeset
532 %!test
a41df65f3f00 Add some single precision test code and fix resulting bugs
David Bateman <dbateman@free.fr>
parents: 7017
diff changeset
533 %! s.a = 1;
14501
60e5cf354d80 Update %!tests in DLD-FUNCTIONS/ directory with Octave coding conventions.
Rik <octave@nomad.inbox5.com>
parents: 14360
diff changeset
534 %! fail ("gcd (s)");
4864
d49d761c8c93 [project @ 2004-04-16 16:01:11 by jwe]
jwe
parents:
diff changeset
535 */
22407
34ce5be04942 maint: Style check C++ code in libinterp/.
Rik <rik@octave.org>
parents: 22327
diff changeset
536