annotate libinterp/corefcn/gcd.cc @ 31605:e88a07dec498 stable

maint: Use macros to begin/end C++ namespaces. * oct-conf-post-public.in.h: Define two macros (OCTAVE_BEGIN_NAMESPACE, OCTAVE_END_NAMESPACE) that can be used to start/end a namespace. * mk-opts.pl, build-env.h, build-env.in.cc, __betainc__.cc, __contourc__.cc, __dsearchn__.cc, __eigs__.cc, __expint__.cc, __ftp__.cc, __gammainc__.cc, __ichol__.cc, __ilu__.cc, __isprimelarge__.cc, __lin_interpn__.cc, __magick_read__.cc, __pchip_deriv__.cc, __qp__.cc, amd.cc, auto-shlib.cc, auto-shlib.h, balance.cc, base-text-renderer.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, ccolamd.cc, cellfun.cc, chol.cc, colamd.cc, colloc.cc, conv2.cc, daspk.cc, dasrt.cc, dassl.cc, data.cc, data.h, debug.cc, defaults.cc, defaults.h, defun-int.h, defun.cc, det.cc, dirfns.cc, display.cc, display.h, dlmread.cc, dmperm.cc, dot.cc, dynamic-ld.cc, dynamic-ld.h, eig.cc, ellipj.cc, environment.cc, environment.h, error.cc, error.h, errwarn.h, event-manager.cc, event-manager.h, event-queue.cc, event-queue.h, fcn-info.cc, fcn-info.h, fft.cc, fft2.cc, fftn.cc, file-io.cc, filter.cc, find.cc, ft-text-renderer.cc, ft-text-renderer.h, gcd.cc, getgrent.cc, getpwent.cc, getrusage.cc, givens.cc, gl-render.cc, gl-render.h, gl2ps-print.cc, gl2ps-print.h, graphics-toolkit.cc, graphics-toolkit.h, graphics.cc, graphics.in.h, gsvd.cc, gtk-manager.cc, gtk-manager.h, hash.cc, help.cc, help.h, hess.cc, hex2num.cc, hook-fcn.cc, hook-fcn.h, input.cc, input.h, interpreter-private.cc, interpreter-private.h, interpreter.cc, interpreter.h, inv.cc, jsondecode.cc, jsonencode.cc, kron.cc, latex-text-renderer.cc, latex-text-renderer.h, load-path.cc, load-path.h, load-save.cc, load-save.h, lookup.cc, ls-ascii-helper.cc, ls-ascii-helper.h, ls-oct-text.cc, ls-utils.cc, ls-utils.h, lsode.cc, lu.cc, mappers.cc, matrix_type.cc, max.cc, mex-private.h, mex.cc, mgorth.cc, nproc.cc, oct-fstrm.cc, oct-fstrm.h, oct-hdf5-types.cc, oct-hdf5-types.h, oct-hist.cc, oct-hist.h, oct-iostrm.cc, oct-iostrm.h, oct-opengl.h, oct-prcstrm.cc, oct-prcstrm.h, oct-procbuf.cc, oct-procbuf.h, oct-process.cc, oct-process.h, oct-stdstrm.h, oct-stream.cc, oct-stream.h, oct-strstrm.cc, oct-strstrm.h, oct-tex-lexer.in.ll, oct-tex-parser.yy, ordqz.cc, ordschur.cc, pager.cc, pager.h, pinv.cc, pow2.cc, pr-flt-fmt.cc, pr-output.cc, procstream.cc, procstream.h, psi.cc, qr.cc, quad.cc, quadcc.cc, qz.cc, rand.cc, rcond.cc, regexp.cc, schur.cc, settings.cc, settings.h, sighandlers.cc, sighandlers.h, sparse-xdiv.cc, sparse-xdiv.h, sparse-xpow.cc, sparse-xpow.h, sparse.cc, spparms.cc, sqrtm.cc, stack-frame.cc, stack-frame.h, stream-euler.cc, strfind.cc, strfns.cc, sub2ind.cc, svd.cc, sylvester.cc, symbfact.cc, syminfo.cc, syminfo.h, symrcm.cc, symrec.cc, symrec.h, symscope.cc, symscope.h, symtab.cc, symtab.h, syscalls.cc, sysdep.cc, sysdep.h, text-engine.cc, text-engine.h, text-renderer.cc, text-renderer.h, time.cc, toplev.cc, tril.cc, tsearch.cc, typecast.cc, url-handle-manager.cc, url-handle-manager.h, urlwrite.cc, utils.cc, utils.h, variables.cc, variables.h, xdiv.cc, xdiv.h, xnorm.cc, xnorm.h, xpow.cc, xpow.h, __delaunayn__.cc, __fltk_uigetfile__.cc, __glpk__.cc, __init_fltk__.cc, __init_gnuplot__.cc, __ode15__.cc, __voronoi__.cc, audiodevinfo.cc, audioread.cc, convhulln.cc, fftw.cc, gzip.cc, mk-build-env-features.sh, mk-builtins.pl, cdef-class.cc, cdef-class.h, cdef-fwd.h, cdef-manager.cc, cdef-manager.h, cdef-method.cc, cdef-method.h, cdef-object.cc, cdef-object.h, cdef-package.cc, cdef-package.h, cdef-property.cc, cdef-property.h, cdef-utils.cc, cdef-utils.h, ov-base.cc, ov-base.h, ov-bool-mat.cc, ov-builtin.h, ov-cell.cc, ov-class.cc, ov-class.h, ov-classdef.cc, ov-classdef.h, ov-complex.cc, ov-fcn-handle.cc, ov-fcn-handle.h, ov-fcn.h, ov-java.cc, ov-java.h, ov-mex-fcn.h, ov-null-mat.cc, ov-oncleanup.cc, ov-struct.cc, ov-typeinfo.cc, ov-typeinfo.h, ov-usr-fcn.cc, ov-usr-fcn.h, ov.cc, ov.h, octave.cc, octave.h, mk-ops.sh, 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-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-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-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-m-cm.cc, op-m-cs.cc, op-m-m.cc, op-m-s.cc, op-m-scm.cc, op-m-sm.cc, op-mi.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, anon-fcn-validator.cc, anon-fcn-validator.h, bp-table.cc, bp-table.h, comment-list.cc, comment-list.h, filepos.h, lex.h, lex.ll, oct-lvalue.cc, oct-lvalue.h, oct-parse.yy, parse.h, profiler.cc, profiler.h, pt-anon-scopes.cc, pt-anon-scopes.h, pt-arg-list.cc, pt-arg-list.h, pt-args-block.cc, pt-args-block.h, pt-array-list.cc, 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.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-id.cc, pt-id.h, pt-idx.cc, pt-idx.h, 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-spmd.cc, pt-spmd.h, pt-stmt.cc, pt-stmt.h, pt-tm-const.cc, pt-tm-const.h, pt-unop.cc, pt-unop.h, pt-vm-eval.cc, pt-walk.cc, pt-walk.h, pt.cc, pt.h, token.cc, token.h, Range.cc, Range.h, idx-vector.cc, idx-vector.h, range-fwd.h, CollocWt.cc, CollocWt.h, aepbalance.cc, aepbalance.h, chol.cc, chol.h, gepbalance.cc, gepbalance.h, gsvd.cc, gsvd.h, hess.cc, hess.h, lo-mappers.cc, lo-mappers.h, lo-specfun.cc, lo-specfun.h, lu.cc, lu.h, oct-convn.cc, oct-convn.h, oct-fftw.cc, oct-fftw.h, oct-norm.cc, oct-norm.h, oct-rand.cc, oct-rand.h, oct-spparms.cc, oct-spparms.h, qr.cc, qr.h, qrp.cc, qrp.h, randgamma.cc, randgamma.h, randmtzig.cc, randmtzig.h, randpoisson.cc, randpoisson.h, schur.cc, schur.h, sparse-chol.cc, sparse-chol.h, sparse-lu.cc, sparse-lu.h, sparse-qr.cc, sparse-qr.h, svd.cc, svd.h, child-list.cc, child-list.h, dir-ops.cc, dir-ops.h, file-ops.cc, file-ops.h, file-stat.cc, file-stat.h, lo-sysdep.cc, lo-sysdep.h, lo-sysinfo.cc, lo-sysinfo.h, mach-info.cc, mach-info.h, oct-env.cc, oct-env.h, oct-group.cc, oct-group.h, oct-password.cc, oct-password.h, oct-syscalls.cc, oct-syscalls.h, oct-time.cc, oct-time.h, oct-uname.cc, oct-uname.h, action-container.cc, action-container.h, base-list.h, cmd-edit.cc, cmd-edit.h, cmd-hist.cc, cmd-hist.h, f77-fcn.h, file-info.cc, file-info.h, lo-array-errwarn.cc, lo-array-errwarn.h, lo-hash.cc, lo-hash.h, lo-ieee.h, lo-regexp.cc, lo-regexp.h, lo-utils.cc, lo-utils.h, oct-base64.cc, oct-base64.h, oct-glob.cc, oct-glob.h, oct-inttypes.h, oct-mutex.cc, oct-mutex.h, oct-refcount.h, oct-shlib.cc, oct-shlib.h, oct-sparse.cc, oct-sparse.h, oct-string.h, octave-preserve-stream-state.h, pathsearch.cc, pathsearch.h, quit.cc, quit.h, unwind-prot.cc, unwind-prot.h, url-transfer.cc, url-transfer.h : Use new macros to begin/end C++ namespaces.
author Rik <rik@octave.org>
date Thu, 01 Dec 2022 14:23:45 -0800
parents 2e39b8ff1860
children aac27ad79be6
Ignore whitespace changes - Everywhere: Within whitespace: At end of lines:
rev   line source
27923
bd51beb6205e update formatting of copyright notices
John W. Eaton <jwe@octave.org>
parents: 27919
diff changeset
1 ////////////////////////////////////////////////////////////////////////
bd51beb6205e update formatting of copyright notices
John W. Eaton <jwe@octave.org>
parents: 27919
diff changeset
2 //
30564
796f54d4ddbf update Octave Project Developers copyright for the new year
John W. Eaton <jwe@octave.org>
parents: 29961
diff changeset
3 // Copyright (C) 2004-2022 The Octave Project Developers
27923
bd51beb6205e update formatting of copyright notices
John W. Eaton <jwe@octave.org>
parents: 27919
diff changeset
4 //
bd51beb6205e update formatting of copyright notices
John W. Eaton <jwe@octave.org>
parents: 27919
diff changeset
5 // See the file COPYRIGHT.md in the top-level directory of this
bd51beb6205e update formatting of copyright notices
John W. Eaton <jwe@octave.org>
parents: 27919
diff changeset
6 // distribution or <https://octave.org/copyright/>.
bd51beb6205e update formatting of copyright notices
John W. Eaton <jwe@octave.org>
parents: 27919
diff changeset
7 //
bd51beb6205e update formatting of copyright notices
John W. Eaton <jwe@octave.org>
parents: 27919
diff changeset
8 // This file is part of Octave.
bd51beb6205e update formatting of copyright notices
John W. Eaton <jwe@octave.org>
parents: 27919
diff changeset
9 //
bd51beb6205e update formatting of copyright notices
John W. Eaton <jwe@octave.org>
parents: 27919
diff changeset
10 // Octave is free software: you can redistribute it and/or modify it
bd51beb6205e update formatting of copyright notices
John W. Eaton <jwe@octave.org>
parents: 27919
diff changeset
11 // under the terms of the GNU General Public License as published by
bd51beb6205e update formatting of copyright notices
John W. Eaton <jwe@octave.org>
parents: 27919
diff changeset
12 // the Free Software Foundation, either version 3 of the License, or
bd51beb6205e update formatting of copyright notices
John W. Eaton <jwe@octave.org>
parents: 27919
diff changeset
13 // (at your option) any later version.
bd51beb6205e update formatting of copyright notices
John W. Eaton <jwe@octave.org>
parents: 27919
diff changeset
14 //
bd51beb6205e update formatting of copyright notices
John W. Eaton <jwe@octave.org>
parents: 27919
diff changeset
15 // Octave is distributed in the hope that it will be useful, but
bd51beb6205e update formatting of copyright notices
John W. Eaton <jwe@octave.org>
parents: 27919
diff changeset
16 // WITHOUT ANY WARRANTY; without even the implied warranty of
bd51beb6205e update formatting of copyright notices
John W. Eaton <jwe@octave.org>
parents: 27919
diff changeset
17 // MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
bd51beb6205e update formatting of copyright notices
John W. Eaton <jwe@octave.org>
parents: 27919
diff changeset
18 // GNU General Public License for more details.
bd51beb6205e update formatting of copyright notices
John W. Eaton <jwe@octave.org>
parents: 27919
diff changeset
19 //
bd51beb6205e update formatting of copyright notices
John W. Eaton <jwe@octave.org>
parents: 27919
diff changeset
20 // You should have received a copy of the GNU General Public License
bd51beb6205e update formatting of copyright notices
John W. Eaton <jwe@octave.org>
parents: 27919
diff changeset
21 // along with Octave; see the file COPYING. If not, see
bd51beb6205e update formatting of copyright notices
John W. Eaton <jwe@octave.org>
parents: 27919
diff changeset
22 // <https://www.gnu.org/licenses/>.
bd51beb6205e update formatting of copyright notices
John W. Eaton <jwe@octave.org>
parents: 27919
diff changeset
23 //
bd51beb6205e update formatting of copyright notices
John W. Eaton <jwe@octave.org>
parents: 27919
diff changeset
24 ////////////////////////////////////////////////////////////////////////
4864
d49d761c8c93 [project @ 2004-04-16 16:01:11 by jwe]
jwe
parents:
diff changeset
25
21724
aba2e6293dd8 use "#if ..." consistently instead of "#ifdef" and "#ifndef"
John W. Eaton <jwe@octave.org>
parents: 21581
diff changeset
26 #if defined (HAVE_CONFIG_H)
21301
40de9f8f23a6 Use '#include "config.h"' rather than <config.h>.
Rik <rik@octave.org>
parents: 21200
diff changeset
27 # include "config.h"
4864
d49d761c8c93 [project @ 2004-04-16 16:01:11 by jwe]
jwe
parents:
diff changeset
28 #endif
d49d761c8c93 [project @ 2004-04-16 16:01:11 by jwe]
jwe
parents:
diff changeset
29
d49d761c8c93 [project @ 2004-04-16 16:01:11 by jwe]
jwe
parents:
diff changeset
30 #include "dNDArray.h"
d49d761c8c93 [project @ 2004-04-16 16:01:11 by jwe]
jwe
parents:
diff changeset
31 #include "CNDArray.h"
7815
a41df65f3f00 Add some single precision test code and fix resulting bugs
David Bateman <dbateman@free.fr>
parents: 7017
diff changeset
32 #include "fNDArray.h"
a41df65f3f00 Add some single precision test code and fix resulting bugs
David Bateman <dbateman@free.fr>
parents: 7017
diff changeset
33 #include "fCNDArray.h"
4864
d49d761c8c93 [project @ 2004-04-16 16:01:11 by jwe]
jwe
parents:
diff changeset
34 #include "lo-mappers.h"
11021
0ee74d581c00 optimize & rewrite gcd
Jaroslav Hajek <highegg@gmail.com>
parents: 10840
diff changeset
35 #include "oct-binmap.h"
4864
d49d761c8c93 [project @ 2004-04-16 16:01:11 by jwe]
jwe
parents:
diff changeset
36
15039
e753177cde93 maint: Move non-dynamically linked functions from DLD-FUNCTIONS/ to corefcn/ directory
Rik <rik@octave.org>
parents: 14854
diff changeset
37 #include "defun.h"
4864
d49d761c8c93 [project @ 2004-04-16 16:01:11 by jwe]
jwe
parents:
diff changeset
38 #include "error.h"
20940
48b2ad5ee801 maint: Rename oct-obj.[cc|h] to ovl.[cc|h] for clarity.
Rik <rik@octave.org>
parents: 20939
diff changeset
39 #include "ovl.h"
4864
d49d761c8c93 [project @ 2004-04-16 16:01:11 by jwe]
jwe
parents:
diff changeset
40
31605
e88a07dec498 maint: Use macros to begin/end C++ namespaces.
Rik <rik@octave.org>
parents: 30991
diff changeset
41 OCTAVE_BEGIN_NAMESPACE(octave)
29958
32c3a5805893 move DEFUN and DEFMETHOD functions inside octave namespace
John W. Eaton <jwe@octave.org>
parents: 29931
diff changeset
42
11062
88d78ee12ee8 Implement gcd for Gaussian (complex) integers
Jordi Gutiérrez Hermoso <jordigh@gmail.com>
parents: 11061
diff changeset
43 static double
11021
0ee74d581c00 optimize & rewrite gcd
Jaroslav Hajek <highegg@gmail.com>
parents: 10840
diff changeset
44 simple_gcd (double a, double b)
0ee74d581c00 optimize & rewrite gcd
Jaroslav Hajek <highegg@gmail.com>
parents: 10840
diff changeset
45 {
29961
7d6709900da7 eliminate octave:: namespace tags in DEFUN and DEFMETHOD and more
John W. Eaton <jwe@octave.org>
parents: 29958
diff changeset
46 if (! math::isinteger (a) || ! math::isinteger (b))
20650
a6a452346c42 use error and error_with_id directly in libinterp code
John W. Eaton <jwe@octave.org>
parents: 20555
diff changeset
47 error ("gcd: all values must be integers");
11021
0ee74d581c00 optimize & rewrite gcd
Jaroslav Hajek <highegg@gmail.com>
parents: 10840
diff changeset
48
11064
61a6c99d8ff7 gcd.cc: style fixes
John W. Eaton <jwe@octave.org>
parents: 11062
diff changeset
49 double aa = fabs (a);
61a6c99d8ff7 gcd.cc: style fixes
John W. Eaton <jwe@octave.org>
parents: 11062
diff changeset
50 double bb = fabs (b);
61a6c99d8ff7 gcd.cc: style fixes
John W. Eaton <jwe@octave.org>
parents: 11062
diff changeset
51
11021
0ee74d581c00 optimize & rewrite gcd
Jaroslav Hajek <highegg@gmail.com>
parents: 10840
diff changeset
52 while (bb != 0)
0ee74d581c00 optimize & rewrite gcd
Jaroslav Hajek <highegg@gmail.com>
parents: 10840
diff changeset
53 {
0ee74d581c00 optimize & rewrite gcd
Jaroslav Hajek <highegg@gmail.com>
parents: 10840
diff changeset
54 double tt = fmod (aa, bb);
11064
61a6c99d8ff7 gcd.cc: style fixes
John W. Eaton <jwe@octave.org>
parents: 11062
diff changeset
55 aa = bb;
61a6c99d8ff7 gcd.cc: style fixes
John W. Eaton <jwe@octave.org>
parents: 11062
diff changeset
56 bb = tt;
11021
0ee74d581c00 optimize & rewrite gcd
Jaroslav Hajek <highegg@gmail.com>
parents: 10840
diff changeset
57 }
0ee74d581c00 optimize & rewrite gcd
Jaroslav Hajek <highegg@gmail.com>
parents: 10840
diff changeset
58
0ee74d581c00 optimize & rewrite gcd
Jaroslav Hajek <highegg@gmail.com>
parents: 10840
diff changeset
59 return aa;
0ee74d581c00 optimize & rewrite gcd
Jaroslav Hajek <highegg@gmail.com>
parents: 10840
diff changeset
60 }
0ee74d581c00 optimize & rewrite gcd
Jaroslav Hajek <highegg@gmail.com>
parents: 10840
diff changeset
61
11062
88d78ee12ee8 Implement gcd for Gaussian (complex) integers
Jordi Gutiérrez Hermoso <jordigh@gmail.com>
parents: 11061
diff changeset
62 template <typename FP>
88d78ee12ee8 Implement gcd for Gaussian (complex) integers
Jordi Gutiérrez Hermoso <jordigh@gmail.com>
parents: 11061
diff changeset
63 static void
11450
5eb10763069f substitute and use LAPACK_LIBS in mkoctfile script
John W. Eaton <jwe@octave.org>
parents: 11064
diff changeset
64 divide (const std::complex<FP>& a, const std::complex<FP>& b,
5eb10763069f substitute and use LAPACK_LIBS in mkoctfile script
John W. Eaton <jwe@octave.org>
parents: 11064
diff changeset
65 std::complex<FP>& q, std::complex<FP>& r)
11062
88d78ee12ee8 Implement gcd for Gaussian (complex) integers
Jordi Gutiérrez Hermoso <jordigh@gmail.com>
parents: 11061
diff changeset
66 {
21942
aab79a1885cc limit gnulib headers to liboctave/wrappers directory
John W. Eaton <jwe@octave.org>
parents: 21782
diff changeset
67 FP qr = std::floor ((a/b).real () + 0.5);
aab79a1885cc limit gnulib headers to liboctave/wrappers directory
John W. Eaton <jwe@octave.org>
parents: 21782
diff changeset
68 FP qi = std::floor ((a/b).imag () + 0.5);
11064
61a6c99d8ff7 gcd.cc: style fixes
John W. Eaton <jwe@octave.org>
parents: 11062
diff changeset
69
61a6c99d8ff7 gcd.cc: style fixes
John W. Eaton <jwe@octave.org>
parents: 11062
diff changeset
70 q = std::complex<FP> (qr, qi);
61a6c99d8ff7 gcd.cc: style fixes
John W. Eaton <jwe@octave.org>
parents: 11062
diff changeset
71
11062
88d78ee12ee8 Implement gcd for Gaussian (complex) integers
Jordi Gutiérrez Hermoso <jordigh@gmail.com>
parents: 11061
diff changeset
72 r = a - q*b;
88d78ee12ee8 Implement gcd for Gaussian (complex) integers
Jordi Gutiérrez Hermoso <jordigh@gmail.com>
parents: 11061
diff changeset
73 }
88d78ee12ee8 Implement gcd for Gaussian (complex) integers
Jordi Gutiérrez Hermoso <jordigh@gmail.com>
parents: 11061
diff changeset
74
88d78ee12ee8 Implement gcd for Gaussian (complex) integers
Jordi Gutiérrez Hermoso <jordigh@gmail.com>
parents: 11061
diff changeset
75 template <typename FP>
88d78ee12ee8 Implement gcd for Gaussian (complex) integers
Jordi Gutiérrez Hermoso <jordigh@gmail.com>
parents: 11061
diff changeset
76 static std::complex<FP>
88d78ee12ee8 Implement gcd for Gaussian (complex) integers
Jordi Gutiérrez Hermoso <jordigh@gmail.com>
parents: 11061
diff changeset
77 simple_gcd (const std::complex<FP>& a, const std::complex<FP>& b)
88d78ee12ee8 Implement gcd for Gaussian (complex) integers
Jordi Gutiérrez Hermoso <jordigh@gmail.com>
parents: 11061
diff changeset
78 {
29961
7d6709900da7 eliminate octave:: namespace tags in DEFUN and DEFMETHOD and more
John W. Eaton <jwe@octave.org>
parents: 29958
diff changeset
79 if (! math::isinteger (a.real ())
7d6709900da7 eliminate octave:: namespace tags in DEFUN and DEFMETHOD and more
John W. Eaton <jwe@octave.org>
parents: 29958
diff changeset
80 || ! math::isinteger (a.imag ())
7d6709900da7 eliminate octave:: namespace tags in DEFUN and DEFMETHOD and more
John W. Eaton <jwe@octave.org>
parents: 29958
diff changeset
81 || ! math::isinteger (b.real ())
7d6709900da7 eliminate octave:: namespace tags in DEFUN and DEFMETHOD and more
John W. Eaton <jwe@octave.org>
parents: 29958
diff changeset
82 || ! math::isinteger (b.imag ()))
20650
a6a452346c42 use error and error_with_id directly in libinterp code
John W. Eaton <jwe@octave.org>
parents: 20555
diff changeset
83 error ("gcd: all complex parts must be integers");
11062
88d78ee12ee8 Implement gcd for Gaussian (complex) integers
Jordi Gutiérrez Hermoso <jordigh@gmail.com>
parents: 11061
diff changeset
84
11064
61a6c99d8ff7 gcd.cc: style fixes
John W. Eaton <jwe@octave.org>
parents: 11062
diff changeset
85 std::complex<FP> aa = a;
61a6c99d8ff7 gcd.cc: style fixes
John W. Eaton <jwe@octave.org>
parents: 11062
diff changeset
86 std::complex<FP> bb = b;
11062
88d78ee12ee8 Implement gcd for Gaussian (complex) integers
Jordi Gutiérrez Hermoso <jordigh@gmail.com>
parents: 11061
diff changeset
87
11064
61a6c99d8ff7 gcd.cc: style fixes
John W. Eaton <jwe@octave.org>
parents: 11062
diff changeset
88 if (abs (aa) < abs (bb))
61a6c99d8ff7 gcd.cc: style fixes
John W. Eaton <jwe@octave.org>
parents: 11062
diff changeset
89 std::swap (aa, bb);
11062
88d78ee12ee8 Implement gcd for Gaussian (complex) integers
Jordi Gutiérrez Hermoso <jordigh@gmail.com>
parents: 11061
diff changeset
90
11064
61a6c99d8ff7 gcd.cc: style fixes
John W. Eaton <jwe@octave.org>
parents: 11062
diff changeset
91 while (abs (bb) != 0)
11062
88d78ee12ee8 Implement gcd for Gaussian (complex) integers
Jordi Gutiérrez Hermoso <jordigh@gmail.com>
parents: 11061
diff changeset
92 {
88d78ee12ee8 Implement gcd for Gaussian (complex) integers
Jordi Gutiérrez Hermoso <jordigh@gmail.com>
parents: 11061
diff changeset
93 std::complex<FP> qq, rr;
88d78ee12ee8 Implement gcd for Gaussian (complex) integers
Jordi Gutiérrez Hermoso <jordigh@gmail.com>
parents: 11061
diff changeset
94 divide (aa, bb, qq, rr);
88d78ee12ee8 Implement gcd for Gaussian (complex) integers
Jordi Gutiérrez Hermoso <jordigh@gmail.com>
parents: 11061
diff changeset
95 aa = bb;
88d78ee12ee8 Implement gcd for Gaussian (complex) integers
Jordi Gutiérrez Hermoso <jordigh@gmail.com>
parents: 11061
diff changeset
96 bb = rr;
88d78ee12ee8 Implement gcd for Gaussian (complex) integers
Jordi Gutiérrez Hermoso <jordigh@gmail.com>
parents: 11061
diff changeset
97 }
88d78ee12ee8 Implement gcd for Gaussian (complex) integers
Jordi Gutiérrez Hermoso <jordigh@gmail.com>
parents: 11061
diff changeset
98
88d78ee12ee8 Implement gcd for Gaussian (complex) integers
Jordi Gutiérrez Hermoso <jordigh@gmail.com>
parents: 11061
diff changeset
99 return aa;
88d78ee12ee8 Implement gcd for Gaussian (complex) integers
Jordi Gutiérrez Hermoso <jordigh@gmail.com>
parents: 11061
diff changeset
100 }
88d78ee12ee8 Implement gcd for Gaussian (complex) integers
Jordi Gutiérrez Hermoso <jordigh@gmail.com>
parents: 11061
diff changeset
101
21139
538b57866b90 consistently use "typename" intead of "class" in template declarations
John W. Eaton <jwe@octave.org>
parents: 21100
diff changeset
102 template <typename T>
11021
0ee74d581c00 optimize & rewrite gcd
Jaroslav Hajek <highegg@gmail.com>
parents: 10840
diff changeset
103 static octave_int<T>
0ee74d581c00 optimize & rewrite gcd
Jaroslav Hajek <highegg@gmail.com>
parents: 10840
diff changeset
104 simple_gcd (const octave_int<T>& a, const octave_int<T>& b)
0ee74d581c00 optimize & rewrite gcd
Jaroslav Hajek <highegg@gmail.com>
parents: 10840
diff changeset
105 {
11064
61a6c99d8ff7 gcd.cc: style fixes
John W. Eaton <jwe@octave.org>
parents: 11062
diff changeset
106 T aa = a.abs ().value ();
61a6c99d8ff7 gcd.cc: style fixes
John W. Eaton <jwe@octave.org>
parents: 11062
diff changeset
107 T bb = b.abs ().value ();
61a6c99d8ff7 gcd.cc: style fixes
John W. Eaton <jwe@octave.org>
parents: 11062
diff changeset
108
11021
0ee74d581c00 optimize & rewrite gcd
Jaroslav Hajek <highegg@gmail.com>
parents: 10840
diff changeset
109 while (bb != 0)
0ee74d581c00 optimize & rewrite gcd
Jaroslav Hajek <highegg@gmail.com>
parents: 10840
diff changeset
110 {
0ee74d581c00 optimize & rewrite gcd
Jaroslav Hajek <highegg@gmail.com>
parents: 10840
diff changeset
111 T tt = aa % bb;
11064
61a6c99d8ff7 gcd.cc: style fixes
John W. Eaton <jwe@octave.org>
parents: 11062
diff changeset
112 aa = bb;
61a6c99d8ff7 gcd.cc: style fixes
John W. Eaton <jwe@octave.org>
parents: 11062
diff changeset
113 bb = tt;
11021
0ee74d581c00 optimize & rewrite gcd
Jaroslav Hajek <highegg@gmail.com>
parents: 10840
diff changeset
114 }
0ee74d581c00 optimize & rewrite gcd
Jaroslav Hajek <highegg@gmail.com>
parents: 10840
diff changeset
115
0ee74d581c00 optimize & rewrite gcd
Jaroslav Hajek <highegg@gmail.com>
parents: 10840
diff changeset
116 return aa;
0ee74d581c00 optimize & rewrite gcd
Jaroslav Hajek <highegg@gmail.com>
parents: 10840
diff changeset
117 }
0ee74d581c00 optimize & rewrite gcd
Jaroslav Hajek <highegg@gmail.com>
parents: 10840
diff changeset
118
11062
88d78ee12ee8 Implement gcd for Gaussian (complex) integers
Jordi Gutiérrez Hermoso <jordigh@gmail.com>
parents: 11061
diff changeset
119 static double
11021
0ee74d581c00 optimize & rewrite gcd
Jaroslav Hajek <highegg@gmail.com>
parents: 10840
diff changeset
120 extended_gcd (double a, double b, double& x, double& y)
0ee74d581c00 optimize & rewrite gcd
Jaroslav Hajek <highegg@gmail.com>
parents: 10840
diff changeset
121 {
29961
7d6709900da7 eliminate octave:: namespace tags in DEFUN and DEFMETHOD and more
John W. Eaton <jwe@octave.org>
parents: 29958
diff changeset
122 if (! math::isinteger (a) || ! math::isinteger (b))
20650
a6a452346c42 use error and error_with_id directly in libinterp code
John W. Eaton <jwe@octave.org>
parents: 20555
diff changeset
123 error ("gcd: all values must be integers");
11021
0ee74d581c00 optimize & rewrite gcd
Jaroslav Hajek <highegg@gmail.com>
parents: 10840
diff changeset
124
11064
61a6c99d8ff7 gcd.cc: style fixes
John W. Eaton <jwe@octave.org>
parents: 11062
diff changeset
125 double aa = fabs (a);
61a6c99d8ff7 gcd.cc: style fixes
John W. Eaton <jwe@octave.org>
parents: 11062
diff changeset
126 double bb = fabs (b);
61a6c99d8ff7 gcd.cc: style fixes
John W. Eaton <jwe@octave.org>
parents: 11062
diff changeset
127
18100
6a71e5030df5 Follow coding convention of defining and initializing only 1 variable per line in liboctinterp.
Rik <rik@octave.org>
parents: 17787
diff changeset
128 double xx, lx, yy, ly;
6a71e5030df5 Follow coding convention of defining and initializing only 1 variable per line in liboctinterp.
Rik <rik@octave.org>
parents: 17787
diff changeset
129 xx = 0, lx = 1;
6a71e5030df5 Follow coding convention of defining and initializing only 1 variable per line in liboctinterp.
Rik <rik@octave.org>
parents: 17787
diff changeset
130 yy = 1, ly = 0;
11064
61a6c99d8ff7 gcd.cc: style fixes
John W. Eaton <jwe@octave.org>
parents: 11062
diff changeset
131
11021
0ee74d581c00 optimize & rewrite gcd
Jaroslav Hajek <highegg@gmail.com>
parents: 10840
diff changeset
132 while (bb != 0)
0ee74d581c00 optimize & rewrite gcd
Jaroslav Hajek <highegg@gmail.com>
parents: 10840
diff changeset
133 {
21942
aab79a1885cc limit gnulib headers to liboctave/wrappers directory
John W. Eaton <jwe@octave.org>
parents: 21782
diff changeset
134 double qq = std::floor (aa / bb);
11021
0ee74d581c00 optimize & rewrite gcd
Jaroslav Hajek <highegg@gmail.com>
parents: 10840
diff changeset
135 double tt = fmod (aa, bb);
11064
61a6c99d8ff7 gcd.cc: style fixes
John W. Eaton <jwe@octave.org>
parents: 11062
diff changeset
136
61a6c99d8ff7 gcd.cc: style fixes
John W. Eaton <jwe@octave.org>
parents: 11062
diff changeset
137 aa = bb;
61a6c99d8ff7 gcd.cc: style fixes
John W. Eaton <jwe@octave.org>
parents: 11062
diff changeset
138 bb = tt;
11021
0ee74d581c00 optimize & rewrite gcd
Jaroslav Hajek <highegg@gmail.com>
parents: 10840
diff changeset
139
0ee74d581c00 optimize & rewrite gcd
Jaroslav Hajek <highegg@gmail.com>
parents: 10840
diff changeset
140 double tx = lx - qq*xx;
11064
61a6c99d8ff7 gcd.cc: style fixes
John W. Eaton <jwe@octave.org>
parents: 11062
diff changeset
141 lx = xx;
61a6c99d8ff7 gcd.cc: style fixes
John W. Eaton <jwe@octave.org>
parents: 11062
diff changeset
142 xx = tx;
11021
0ee74d581c00 optimize & rewrite gcd
Jaroslav Hajek <highegg@gmail.com>
parents: 10840
diff changeset
143
0ee74d581c00 optimize & rewrite gcd
Jaroslav Hajek <highegg@gmail.com>
parents: 10840
diff changeset
144 double ty = ly - qq*yy;
11064
61a6c99d8ff7 gcd.cc: style fixes
John W. Eaton <jwe@octave.org>
parents: 11062
diff changeset
145 ly = yy;
61a6c99d8ff7 gcd.cc: style fixes
John W. Eaton <jwe@octave.org>
parents: 11062
diff changeset
146 yy = ty;
11021
0ee74d581c00 optimize & rewrite gcd
Jaroslav Hajek <highegg@gmail.com>
parents: 10840
diff changeset
147 }
4864
d49d761c8c93 [project @ 2004-04-16 16:01:11 by jwe]
jwe
parents:
diff changeset
148
23450
855122b993da maint: Wrap tertiary operator in parentheses "(COND ? x : y)".
Rik <rik@octave.org>
parents: 23220
diff changeset
149 x = (a >= 0 ? lx : -lx);
855122b993da maint: Wrap tertiary operator in parentheses "(COND ? x : y)".
Rik <rik@octave.org>
parents: 23220
diff changeset
150 y = (b >= 0 ? ly : -ly);
11021
0ee74d581c00 optimize & rewrite gcd
Jaroslav Hajek <highegg@gmail.com>
parents: 10840
diff changeset
151
0ee74d581c00 optimize & rewrite gcd
Jaroslav Hajek <highegg@gmail.com>
parents: 10840
diff changeset
152 return aa;
0ee74d581c00 optimize & rewrite gcd
Jaroslav Hajek <highegg@gmail.com>
parents: 10840
diff changeset
153 }
0ee74d581c00 optimize & rewrite gcd
Jaroslav Hajek <highegg@gmail.com>
parents: 10840
diff changeset
154
11062
88d78ee12ee8 Implement gcd for Gaussian (complex) integers
Jordi Gutiérrez Hermoso <jordigh@gmail.com>
parents: 11061
diff changeset
155 template <typename FP>
88d78ee12ee8 Implement gcd for Gaussian (complex) integers
Jordi Gutiérrez Hermoso <jordigh@gmail.com>
parents: 11061
diff changeset
156 static std::complex<FP>
88d78ee12ee8 Implement gcd for Gaussian (complex) integers
Jordi Gutiérrez Hermoso <jordigh@gmail.com>
parents: 11061
diff changeset
157 extended_gcd (const std::complex<FP>& a, const std::complex<FP>& b,
88d78ee12ee8 Implement gcd for Gaussian (complex) integers
Jordi Gutiérrez Hermoso <jordigh@gmail.com>
parents: 11061
diff changeset
158 std::complex<FP>& x, std::complex<FP>& y)
88d78ee12ee8 Implement gcd for Gaussian (complex) integers
Jordi Gutiérrez Hermoso <jordigh@gmail.com>
parents: 11061
diff changeset
159 {
29961
7d6709900da7 eliminate octave:: namespace tags in DEFUN and DEFMETHOD and more
John W. Eaton <jwe@octave.org>
parents: 29958
diff changeset
160 if (! math::isinteger (a.real ())
7d6709900da7 eliminate octave:: namespace tags in DEFUN and DEFMETHOD and more
John W. Eaton <jwe@octave.org>
parents: 29958
diff changeset
161 || ! math::isinteger (a.imag ())
7d6709900da7 eliminate octave:: namespace tags in DEFUN and DEFMETHOD and more
John W. Eaton <jwe@octave.org>
parents: 29958
diff changeset
162 || ! math::isinteger (b.real ())
7d6709900da7 eliminate octave:: namespace tags in DEFUN and DEFMETHOD and more
John W. Eaton <jwe@octave.org>
parents: 29958
diff changeset
163 || ! math::isinteger (b.imag ()))
20650
a6a452346c42 use error and error_with_id directly in libinterp code
John W. Eaton <jwe@octave.org>
parents: 20555
diff changeset
164 error ("gcd: all complex parts must be integers");
11062
88d78ee12ee8 Implement gcd for Gaussian (complex) integers
Jordi Gutiérrez Hermoso <jordigh@gmail.com>
parents: 11061
diff changeset
165
18100
6a71e5030df5 Follow coding convention of defining and initializing only 1 variable per line in liboctinterp.
Rik <rik@octave.org>
parents: 17787
diff changeset
166 std::complex<FP> aa = a;
6a71e5030df5 Follow coding convention of defining and initializing only 1 variable per line in liboctinterp.
Rik <rik@octave.org>
parents: 17787
diff changeset
167 std::complex<FP> bb = b;
11062
88d78ee12ee8 Implement gcd for Gaussian (complex) integers
Jordi Gutiérrez Hermoso <jordigh@gmail.com>
parents: 11061
diff changeset
168 bool swapped = false;
11064
61a6c99d8ff7 gcd.cc: style fixes
John W. Eaton <jwe@octave.org>
parents: 11062
diff changeset
169 if (abs (aa) < abs (bb))
11062
88d78ee12ee8 Implement gcd for Gaussian (complex) integers
Jordi Gutiérrez Hermoso <jordigh@gmail.com>
parents: 11061
diff changeset
170 {
11064
61a6c99d8ff7 gcd.cc: style fixes
John W. Eaton <jwe@octave.org>
parents: 11062
diff changeset
171 std::swap (aa, bb);
11062
88d78ee12ee8 Implement gcd for Gaussian (complex) integers
Jordi Gutiérrez Hermoso <jordigh@gmail.com>
parents: 11061
diff changeset
172 swapped = true;
88d78ee12ee8 Implement gcd for Gaussian (complex) integers
Jordi Gutiérrez Hermoso <jordigh@gmail.com>
parents: 11061
diff changeset
173 }
88d78ee12ee8 Implement gcd for Gaussian (complex) integers
Jordi Gutiérrez Hermoso <jordigh@gmail.com>
parents: 11061
diff changeset
174
18100
6a71e5030df5 Follow coding convention of defining and initializing only 1 variable per line in liboctinterp.
Rik <rik@octave.org>
parents: 17787
diff changeset
175 std::complex<FP> xx, lx, yy, ly;
6a71e5030df5 Follow coding convention of defining and initializing only 1 variable per line in liboctinterp.
Rik <rik@octave.org>
parents: 17787
diff changeset
176 xx = 0, lx = 1;
6a71e5030df5 Follow coding convention of defining and initializing only 1 variable per line in liboctinterp.
Rik <rik@octave.org>
parents: 17787
diff changeset
177 yy = 1, ly = 0;
11064
61a6c99d8ff7 gcd.cc: style fixes
John W. Eaton <jwe@octave.org>
parents: 11062
diff changeset
178
61a6c99d8ff7 gcd.cc: style fixes
John W. Eaton <jwe@octave.org>
parents: 11062
diff changeset
179 while (abs(bb) != 0)
11062
88d78ee12ee8 Implement gcd for Gaussian (complex) integers
Jordi Gutiérrez Hermoso <jordigh@gmail.com>
parents: 11061
diff changeset
180 {
88d78ee12ee8 Implement gcd for Gaussian (complex) integers
Jordi Gutiérrez Hermoso <jordigh@gmail.com>
parents: 11061
diff changeset
181 std::complex<FP> qq, rr;
88d78ee12ee8 Implement gcd for Gaussian (complex) integers
Jordi Gutiérrez Hermoso <jordigh@gmail.com>
parents: 11061
diff changeset
182 divide (aa, bb, qq, rr);
11064
61a6c99d8ff7 gcd.cc: style fixes
John W. Eaton <jwe@octave.org>
parents: 11062
diff changeset
183 aa = bb;
61a6c99d8ff7 gcd.cc: style fixes
John W. Eaton <jwe@octave.org>
parents: 11062
diff changeset
184 bb = rr;
11062
88d78ee12ee8 Implement gcd for Gaussian (complex) integers
Jordi Gutiérrez Hermoso <jordigh@gmail.com>
parents: 11061
diff changeset
185
88d78ee12ee8 Implement gcd for Gaussian (complex) integers
Jordi Gutiérrez Hermoso <jordigh@gmail.com>
parents: 11061
diff changeset
186 std::complex<FP> tx = lx - qq*xx;
11064
61a6c99d8ff7 gcd.cc: style fixes
John W. Eaton <jwe@octave.org>
parents: 11062
diff changeset
187 lx = xx;
61a6c99d8ff7 gcd.cc: style fixes
John W. Eaton <jwe@octave.org>
parents: 11062
diff changeset
188 xx = tx;
11062
88d78ee12ee8 Implement gcd for Gaussian (complex) integers
Jordi Gutiérrez Hermoso <jordigh@gmail.com>
parents: 11061
diff changeset
189
88d78ee12ee8 Implement gcd for Gaussian (complex) integers
Jordi Gutiérrez Hermoso <jordigh@gmail.com>
parents: 11061
diff changeset
190 std::complex<FP> ty = ly - qq*yy;
11064
61a6c99d8ff7 gcd.cc: style fixes
John W. Eaton <jwe@octave.org>
parents: 11062
diff changeset
191 ly = yy;
61a6c99d8ff7 gcd.cc: style fixes
John W. Eaton <jwe@octave.org>
parents: 11062
diff changeset
192 yy = ty;
11062
88d78ee12ee8 Implement gcd for Gaussian (complex) integers
Jordi Gutiérrez Hermoso <jordigh@gmail.com>
parents: 11061
diff changeset
193 }
88d78ee12ee8 Implement gcd for Gaussian (complex) integers
Jordi Gutiérrez Hermoso <jordigh@gmail.com>
parents: 11061
diff changeset
194
88d78ee12ee8 Implement gcd for Gaussian (complex) integers
Jordi Gutiérrez Hermoso <jordigh@gmail.com>
parents: 11061
diff changeset
195 x = lx;
88d78ee12ee8 Implement gcd for Gaussian (complex) integers
Jordi Gutiérrez Hermoso <jordigh@gmail.com>
parents: 11061
diff changeset
196 y = ly;
11064
61a6c99d8ff7 gcd.cc: style fixes
John W. Eaton <jwe@octave.org>
parents: 11062
diff changeset
197
11062
88d78ee12ee8 Implement gcd for Gaussian (complex) integers
Jordi Gutiérrez Hermoso <jordigh@gmail.com>
parents: 11061
diff changeset
198 if (swapped)
11064
61a6c99d8ff7 gcd.cc: style fixes
John W. Eaton <jwe@octave.org>
parents: 11062
diff changeset
199 std::swap (x, y);
11062
88d78ee12ee8 Implement gcd for Gaussian (complex) integers
Jordi Gutiérrez Hermoso <jordigh@gmail.com>
parents: 11061
diff changeset
200
88d78ee12ee8 Implement gcd for Gaussian (complex) integers
Jordi Gutiérrez Hermoso <jordigh@gmail.com>
parents: 11061
diff changeset
201 return aa;
88d78ee12ee8 Implement gcd for Gaussian (complex) integers
Jordi Gutiérrez Hermoso <jordigh@gmail.com>
parents: 11061
diff changeset
202 }
88d78ee12ee8 Implement gcd for Gaussian (complex) integers
Jordi Gutiérrez Hermoso <jordigh@gmail.com>
parents: 11061
diff changeset
203
21139
538b57866b90 consistently use "typename" intead of "class" in template declarations
John W. Eaton <jwe@octave.org>
parents: 21100
diff changeset
204 template <typename T>
11021
0ee74d581c00 optimize & rewrite gcd
Jaroslav Hajek <highegg@gmail.com>
parents: 10840
diff changeset
205 static octave_int<T>
0ee74d581c00 optimize & rewrite gcd
Jaroslav Hajek <highegg@gmail.com>
parents: 10840
diff changeset
206 extended_gcd (const octave_int<T>& a, const octave_int<T>& b,
0ee74d581c00 optimize & rewrite gcd
Jaroslav Hajek <highegg@gmail.com>
parents: 10840
diff changeset
207 octave_int<T>& x, octave_int<T>& y)
4864
d49d761c8c93 [project @ 2004-04-16 16:01:11 by jwe]
jwe
parents:
diff changeset
208 {
11064
61a6c99d8ff7 gcd.cc: style fixes
John W. Eaton <jwe@octave.org>
parents: 11062
diff changeset
209 T aa = a.abs ().value ();
61a6c99d8ff7 gcd.cc: style fixes
John W. Eaton <jwe@octave.org>
parents: 11062
diff changeset
210 T bb = b.abs ().value ();
18100
6a71e5030df5 Follow coding convention of defining and initializing only 1 variable per line in liboctinterp.
Rik <rik@octave.org>
parents: 17787
diff changeset
211 T xx, lx, yy, ly;
6a71e5030df5 Follow coding convention of defining and initializing only 1 variable per line in liboctinterp.
Rik <rik@octave.org>
parents: 17787
diff changeset
212 xx = 0, lx = 1;
6a71e5030df5 Follow coding convention of defining and initializing only 1 variable per line in liboctinterp.
Rik <rik@octave.org>
parents: 17787
diff changeset
213 yy = 1, ly = 0;
11064
61a6c99d8ff7 gcd.cc: style fixes
John W. Eaton <jwe@octave.org>
parents: 11062
diff changeset
214
11021
0ee74d581c00 optimize & rewrite gcd
Jaroslav Hajek <highegg@gmail.com>
parents: 10840
diff changeset
215 while (bb != 0)
0ee74d581c00 optimize & rewrite gcd
Jaroslav Hajek <highegg@gmail.com>
parents: 10840
diff changeset
216 {
0ee74d581c00 optimize & rewrite gcd
Jaroslav Hajek <highegg@gmail.com>
parents: 10840
diff changeset
217 T qq = aa / bb;
0ee74d581c00 optimize & rewrite gcd
Jaroslav Hajek <highegg@gmail.com>
parents: 10840
diff changeset
218 T tt = aa % bb;
11064
61a6c99d8ff7 gcd.cc: style fixes
John W. Eaton <jwe@octave.org>
parents: 11062
diff changeset
219 aa = bb;
61a6c99d8ff7 gcd.cc: style fixes
John W. Eaton <jwe@octave.org>
parents: 11062
diff changeset
220 bb = tt;
11021
0ee74d581c00 optimize & rewrite gcd
Jaroslav Hajek <highegg@gmail.com>
parents: 10840
diff changeset
221
0ee74d581c00 optimize & rewrite gcd
Jaroslav Hajek <highegg@gmail.com>
parents: 10840
diff changeset
222 T tx = lx - qq*xx;
11064
61a6c99d8ff7 gcd.cc: style fixes
John W. Eaton <jwe@octave.org>
parents: 11062
diff changeset
223 lx = xx;
61a6c99d8ff7 gcd.cc: style fixes
John W. Eaton <jwe@octave.org>
parents: 11062
diff changeset
224 xx = tx;
11021
0ee74d581c00 optimize & rewrite gcd
Jaroslav Hajek <highegg@gmail.com>
parents: 10840
diff changeset
225
0ee74d581c00 optimize & rewrite gcd
Jaroslav Hajek <highegg@gmail.com>
parents: 10840
diff changeset
226 T ty = ly - qq*yy;
11064
61a6c99d8ff7 gcd.cc: style fixes
John W. Eaton <jwe@octave.org>
parents: 11062
diff changeset
227 ly = yy;
61a6c99d8ff7 gcd.cc: style fixes
John W. Eaton <jwe@octave.org>
parents: 11062
diff changeset
228 yy = ty;
11021
0ee74d581c00 optimize & rewrite gcd
Jaroslav Hajek <highegg@gmail.com>
parents: 10840
diff changeset
229 }
0ee74d581c00 optimize & rewrite gcd
Jaroslav Hajek <highegg@gmail.com>
parents: 10840
diff changeset
230
0ee74d581c00 optimize & rewrite gcd
Jaroslav Hajek <highegg@gmail.com>
parents: 10840
diff changeset
231 x = octave_int<T> (lx) * a.signum ();
0ee74d581c00 optimize & rewrite gcd
Jaroslav Hajek <highegg@gmail.com>
parents: 10840
diff changeset
232 y = octave_int<T> (ly) * b.signum ();
0ee74d581c00 optimize & rewrite gcd
Jaroslav Hajek <highegg@gmail.com>
parents: 10840
diff changeset
233
0ee74d581c00 optimize & rewrite gcd
Jaroslav Hajek <highegg@gmail.com>
parents: 10840
diff changeset
234 return aa;
0ee74d581c00 optimize & rewrite gcd
Jaroslav Hajek <highegg@gmail.com>
parents: 10840
diff changeset
235 }
0ee74d581c00 optimize & rewrite gcd
Jaroslav Hajek <highegg@gmail.com>
parents: 10840
diff changeset
236
21139
538b57866b90 consistently use "typename" intead of "class" in template declarations
John W. Eaton <jwe@octave.org>
parents: 21100
diff changeset
237 template <typename NDA>
11021
0ee74d581c00 optimize & rewrite gcd
Jaroslav Hajek <highegg@gmail.com>
parents: 10840
diff changeset
238 static octave_value
0ee74d581c00 optimize & rewrite gcd
Jaroslav Hajek <highegg@gmail.com>
parents: 10840
diff changeset
239 do_simple_gcd (const octave_value& a, const octave_value& b)
0ee74d581c00 optimize & rewrite gcd
Jaroslav Hajek <highegg@gmail.com>
parents: 10840
diff changeset
240 {
0ee74d581c00 optimize & rewrite gcd
Jaroslav Hajek <highegg@gmail.com>
parents: 10840
diff changeset
241 typedef typename NDA::element_type T;
0ee74d581c00 optimize & rewrite gcd
Jaroslav Hajek <highegg@gmail.com>
parents: 10840
diff changeset
242 octave_value retval;
0ee74d581c00 optimize & rewrite gcd
Jaroslav Hajek <highegg@gmail.com>
parents: 10840
diff changeset
243
0ee74d581c00 optimize & rewrite gcd
Jaroslav Hajek <highegg@gmail.com>
parents: 10840
diff changeset
244 if (a.is_scalar_type () && b.is_scalar_type ())
0ee74d581c00 optimize & rewrite gcd
Jaroslav Hajek <highegg@gmail.com>
parents: 10840
diff changeset
245 {
0ee74d581c00 optimize & rewrite gcd
Jaroslav Hajek <highegg@gmail.com>
parents: 10840
diff changeset
246 // Optimize scalar case.
0ee74d581c00 optimize & rewrite gcd
Jaroslav Hajek <highegg@gmail.com>
parents: 10840
diff changeset
247 T aa = octave_value_extract<T> (a);
0ee74d581c00 optimize & rewrite gcd
Jaroslav Hajek <highegg@gmail.com>
parents: 10840
diff changeset
248 T bb = octave_value_extract<T> (b);
0ee74d581c00 optimize & rewrite gcd
Jaroslav Hajek <highegg@gmail.com>
parents: 10840
diff changeset
249 retval = simple_gcd (aa, bb);
0ee74d581c00 optimize & rewrite gcd
Jaroslav Hajek <highegg@gmail.com>
parents: 10840
diff changeset
250 }
0ee74d581c00 optimize & rewrite gcd
Jaroslav Hajek <highegg@gmail.com>
parents: 10840
diff changeset
251 else
0ee74d581c00 optimize & rewrite gcd
Jaroslav Hajek <highegg@gmail.com>
parents: 10840
diff changeset
252 {
0ee74d581c00 optimize & rewrite gcd
Jaroslav Hajek <highegg@gmail.com>
parents: 10840
diff changeset
253 NDA aa = octave_value_extract<NDA> (a);
0ee74d581c00 optimize & rewrite gcd
Jaroslav Hajek <highegg@gmail.com>
parents: 10840
diff changeset
254 NDA bb = octave_value_extract<NDA> (b);
0ee74d581c00 optimize & rewrite gcd
Jaroslav Hajek <highegg@gmail.com>
parents: 10840
diff changeset
255 retval = binmap<T> (aa, bb, simple_gcd, "gcd");
0ee74d581c00 optimize & rewrite gcd
Jaroslav Hajek <highegg@gmail.com>
parents: 10840
diff changeset
256 }
0ee74d581c00 optimize & rewrite gcd
Jaroslav Hajek <highegg@gmail.com>
parents: 10840
diff changeset
257
0ee74d581c00 optimize & rewrite gcd
Jaroslav Hajek <highegg@gmail.com>
parents: 10840
diff changeset
258 return retval;
4864
d49d761c8c93 [project @ 2004-04-16 16:01:11 by jwe]
jwe
parents:
diff changeset
259 }
d49d761c8c93 [project @ 2004-04-16 16:01:11 by jwe]
jwe
parents:
diff changeset
260
11021
0ee74d581c00 optimize & rewrite gcd
Jaroslav Hajek <highegg@gmail.com>
parents: 10840
diff changeset
261 // Dispatcher
0ee74d581c00 optimize & rewrite gcd
Jaroslav Hajek <highegg@gmail.com>
parents: 10840
diff changeset
262 static octave_value
0ee74d581c00 optimize & rewrite gcd
Jaroslav Hajek <highegg@gmail.com>
parents: 10840
diff changeset
263 do_simple_gcd (const octave_value& a, const octave_value& b)
0ee74d581c00 optimize & rewrite gcd
Jaroslav Hajek <highegg@gmail.com>
parents: 10840
diff changeset
264 {
0ee74d581c00 optimize & rewrite gcd
Jaroslav Hajek <highegg@gmail.com>
parents: 10840
diff changeset
265 octave_value retval;
0ee74d581c00 optimize & rewrite gcd
Jaroslav Hajek <highegg@gmail.com>
parents: 10840
diff changeset
266 builtin_type_t btyp = btyp_mixed_numeric (a.builtin_type (),
0ee74d581c00 optimize & rewrite gcd
Jaroslav Hajek <highegg@gmail.com>
parents: 10840
diff changeset
267 b.builtin_type ());
0ee74d581c00 optimize & rewrite gcd
Jaroslav Hajek <highegg@gmail.com>
parents: 10840
diff changeset
268 switch (btyp)
0ee74d581c00 optimize & rewrite gcd
Jaroslav Hajek <highegg@gmail.com>
parents: 10840
diff changeset
269 {
0ee74d581c00 optimize & rewrite gcd
Jaroslav Hajek <highegg@gmail.com>
parents: 10840
diff changeset
270 case btyp_double:
23583
b7747a2c88b2 maint: Deprecate is_sparse_type and replace with issparse.
Rik <rik@octave.org>
parents: 23450
diff changeset
271 if (a.issparse () && b.issparse ())
11021
0ee74d581c00 optimize & rewrite gcd
Jaroslav Hajek <highegg@gmail.com>
parents: 10840
diff changeset
272 {
0ee74d581c00 optimize & rewrite gcd
Jaroslav Hajek <highegg@gmail.com>
parents: 10840
diff changeset
273 retval = do_simple_gcd<SparseMatrix> (a, b);
0ee74d581c00 optimize & rewrite gcd
Jaroslav Hajek <highegg@gmail.com>
parents: 10840
diff changeset
274 break;
0ee74d581c00 optimize & rewrite gcd
Jaroslav Hajek <highegg@gmail.com>
parents: 10840
diff changeset
275 }
23826
d69021d58a61 avoid fallthrough warnings
John W. Eaton <jwe@octave.org>
parents: 23583
diff changeset
276 OCTAVE_FALLTHROUGH;
11064
61a6c99d8ff7 gcd.cc: style fixes
John W. Eaton <jwe@octave.org>
parents: 11062
diff changeset
277
11021
0ee74d581c00 optimize & rewrite gcd
Jaroslav Hajek <highegg@gmail.com>
parents: 10840
diff changeset
278 case btyp_float:
0ee74d581c00 optimize & rewrite gcd
Jaroslav Hajek <highegg@gmail.com>
parents: 10840
diff changeset
279 retval = do_simple_gcd<NDArray> (a, b);
0ee74d581c00 optimize & rewrite gcd
Jaroslav Hajek <highegg@gmail.com>
parents: 10840
diff changeset
280 break;
11064
61a6c99d8ff7 gcd.cc: style fixes
John W. Eaton <jwe@octave.org>
parents: 11062
diff changeset
281
22407
34ce5be04942 maint: Style check C++ code in libinterp/.
Rik <rik@octave.org>
parents: 22327
diff changeset
282 #define MAKE_INT_BRANCH(X) \
34ce5be04942 maint: Style check C++ code in libinterp/.
Rik <rik@octave.org>
parents: 22327
diff changeset
283 case btyp_ ## X: \
34ce5be04942 maint: Style check C++ code in libinterp/.
Rik <rik@octave.org>
parents: 22327
diff changeset
284 retval = do_simple_gcd<X ## NDArray> (a, b); \
34ce5be04942 maint: Style check C++ code in libinterp/.
Rik <rik@octave.org>
parents: 22327
diff changeset
285 break
11064
61a6c99d8ff7 gcd.cc: style fixes
John W. Eaton <jwe@octave.org>
parents: 11062
diff changeset
286
11021
0ee74d581c00 optimize & rewrite gcd
Jaroslav Hajek <highegg@gmail.com>
parents: 10840
diff changeset
287 MAKE_INT_BRANCH (int8);
0ee74d581c00 optimize & rewrite gcd
Jaroslav Hajek <highegg@gmail.com>
parents: 10840
diff changeset
288 MAKE_INT_BRANCH (int16);
0ee74d581c00 optimize & rewrite gcd
Jaroslav Hajek <highegg@gmail.com>
parents: 10840
diff changeset
289 MAKE_INT_BRANCH (int32);
0ee74d581c00 optimize & rewrite gcd
Jaroslav Hajek <highegg@gmail.com>
parents: 10840
diff changeset
290 MAKE_INT_BRANCH (int64);
0ee74d581c00 optimize & rewrite gcd
Jaroslav Hajek <highegg@gmail.com>
parents: 10840
diff changeset
291 MAKE_INT_BRANCH (uint8);
0ee74d581c00 optimize & rewrite gcd
Jaroslav Hajek <highegg@gmail.com>
parents: 10840
diff changeset
292 MAKE_INT_BRANCH (uint16);
0ee74d581c00 optimize & rewrite gcd
Jaroslav Hajek <highegg@gmail.com>
parents: 10840
diff changeset
293 MAKE_INT_BRANCH (uint32);
0ee74d581c00 optimize & rewrite gcd
Jaroslav Hajek <highegg@gmail.com>
parents: 10840
diff changeset
294 MAKE_INT_BRANCH (uint64);
11064
61a6c99d8ff7 gcd.cc: style fixes
John W. Eaton <jwe@octave.org>
parents: 11062
diff changeset
295
11021
0ee74d581c00 optimize & rewrite gcd
Jaroslav Hajek <highegg@gmail.com>
parents: 10840
diff changeset
296 #undef MAKE_INT_BRANCH
11064
61a6c99d8ff7 gcd.cc: style fixes
John W. Eaton <jwe@octave.org>
parents: 11062
diff changeset
297
11021
0ee74d581c00 optimize & rewrite gcd
Jaroslav Hajek <highegg@gmail.com>
parents: 10840
diff changeset
298 case btyp_complex:
11064
61a6c99d8ff7 gcd.cc: style fixes
John W. Eaton <jwe@octave.org>
parents: 11062
diff changeset
299 retval = do_simple_gcd<ComplexNDArray> (a, b);
11062
88d78ee12ee8 Implement gcd for Gaussian (complex) integers
Jordi Gutiérrez Hermoso <jordigh@gmail.com>
parents: 11061
diff changeset
300 break;
11064
61a6c99d8ff7 gcd.cc: style fixes
John W. Eaton <jwe@octave.org>
parents: 11062
diff changeset
301
11021
0ee74d581c00 optimize & rewrite gcd
Jaroslav Hajek <highegg@gmail.com>
parents: 10840
diff changeset
302 case btyp_float_complex:
11064
61a6c99d8ff7 gcd.cc: style fixes
John W. Eaton <jwe@octave.org>
parents: 11062
diff changeset
303 retval = do_simple_gcd<FloatComplexNDArray> (a, b);
11062
88d78ee12ee8 Implement gcd for Gaussian (complex) integers
Jordi Gutiérrez Hermoso <jordigh@gmail.com>
parents: 11061
diff changeset
304 break;
11064
61a6c99d8ff7 gcd.cc: style fixes
John W. Eaton <jwe@octave.org>
parents: 11062
diff changeset
305
11021
0ee74d581c00 optimize & rewrite gcd
Jaroslav Hajek <highegg@gmail.com>
parents: 10840
diff changeset
306 default:
0ee74d581c00 optimize & rewrite gcd
Jaroslav Hajek <highegg@gmail.com>
parents: 10840
diff changeset
307 error ("gcd: invalid class combination for gcd: %s and %s\n",
0ee74d581c00 optimize & rewrite gcd
Jaroslav Hajek <highegg@gmail.com>
parents: 10840
diff changeset
308 a.class_name ().c_str (), b.class_name ().c_str ());
0ee74d581c00 optimize & rewrite gcd
Jaroslav Hajek <highegg@gmail.com>
parents: 10840
diff changeset
309 }
0ee74d581c00 optimize & rewrite gcd
Jaroslav Hajek <highegg@gmail.com>
parents: 10840
diff changeset
310
0ee74d581c00 optimize & rewrite gcd
Jaroslav Hajek <highegg@gmail.com>
parents: 10840
diff changeset
311 if (btyp == btyp_float)
0ee74d581c00 optimize & rewrite gcd
Jaroslav Hajek <highegg@gmail.com>
parents: 10840
diff changeset
312 retval = retval.float_array_value ();
0ee74d581c00 optimize & rewrite gcd
Jaroslav Hajek <highegg@gmail.com>
parents: 10840
diff changeset
313
0ee74d581c00 optimize & rewrite gcd
Jaroslav Hajek <highegg@gmail.com>
parents: 10840
diff changeset
314 return retval;
0ee74d581c00 optimize & rewrite gcd
Jaroslav Hajek <highegg@gmail.com>
parents: 10840
diff changeset
315 }
0ee74d581c00 optimize & rewrite gcd
Jaroslav Hajek <highegg@gmail.com>
parents: 10840
diff changeset
316
21139
538b57866b90 consistently use "typename" intead of "class" in template declarations
John W. Eaton <jwe@octave.org>
parents: 21100
diff changeset
317 template <typename NDA>
11021
0ee74d581c00 optimize & rewrite gcd
Jaroslav Hajek <highegg@gmail.com>
parents: 10840
diff changeset
318 static octave_value
0ee74d581c00 optimize & rewrite gcd
Jaroslav Hajek <highegg@gmail.com>
parents: 10840
diff changeset
319 do_extended_gcd (const octave_value& a, const octave_value& b,
0ee74d581c00 optimize & rewrite gcd
Jaroslav Hajek <highegg@gmail.com>
parents: 10840
diff changeset
320 octave_value& x, octave_value& y)
7815
a41df65f3f00 Add some single precision test code and fix resulting bugs
David Bateman <dbateman@free.fr>
parents: 7017
diff changeset
321 {
11021
0ee74d581c00 optimize & rewrite gcd
Jaroslav Hajek <highegg@gmail.com>
parents: 10840
diff changeset
322 typedef typename NDA::element_type T;
0ee74d581c00 optimize & rewrite gcd
Jaroslav Hajek <highegg@gmail.com>
parents: 10840
diff changeset
323 octave_value retval;
0ee74d581c00 optimize & rewrite gcd
Jaroslav Hajek <highegg@gmail.com>
parents: 10840
diff changeset
324
0ee74d581c00 optimize & rewrite gcd
Jaroslav Hajek <highegg@gmail.com>
parents: 10840
diff changeset
325 if (a.is_scalar_type () && b.is_scalar_type ())
0ee74d581c00 optimize & rewrite gcd
Jaroslav Hajek <highegg@gmail.com>
parents: 10840
diff changeset
326 {
0ee74d581c00 optimize & rewrite gcd
Jaroslav Hajek <highegg@gmail.com>
parents: 10840
diff changeset
327 // Optimize scalar case.
0ee74d581c00 optimize & rewrite gcd
Jaroslav Hajek <highegg@gmail.com>
parents: 10840
diff changeset
328 T aa = octave_value_extract<T> (a);
0ee74d581c00 optimize & rewrite gcd
Jaroslav Hajek <highegg@gmail.com>
parents: 10840
diff changeset
329 T bb = octave_value_extract<T> (b);
0ee74d581c00 optimize & rewrite gcd
Jaroslav Hajek <highegg@gmail.com>
parents: 10840
diff changeset
330 T xx, yy;
0ee74d581c00 optimize & rewrite gcd
Jaroslav Hajek <highegg@gmail.com>
parents: 10840
diff changeset
331 retval = extended_gcd (aa, bb, xx, yy);
0ee74d581c00 optimize & rewrite gcd
Jaroslav Hajek <highegg@gmail.com>
parents: 10840
diff changeset
332 x = xx;
0ee74d581c00 optimize & rewrite gcd
Jaroslav Hajek <highegg@gmail.com>
parents: 10840
diff changeset
333 y = yy;
0ee74d581c00 optimize & rewrite gcd
Jaroslav Hajek <highegg@gmail.com>
parents: 10840
diff changeset
334 }
0ee74d581c00 optimize & rewrite gcd
Jaroslav Hajek <highegg@gmail.com>
parents: 10840
diff changeset
335 else
0ee74d581c00 optimize & rewrite gcd
Jaroslav Hajek <highegg@gmail.com>
parents: 10840
diff changeset
336 {
0ee74d581c00 optimize & rewrite gcd
Jaroslav Hajek <highegg@gmail.com>
parents: 10840
diff changeset
337 NDA aa = octave_value_extract<NDA> (a);
0ee74d581c00 optimize & rewrite gcd
Jaroslav Hajek <highegg@gmail.com>
parents: 10840
diff changeset
338 NDA bb = octave_value_extract<NDA> (b);
0ee74d581c00 optimize & rewrite gcd
Jaroslav Hajek <highegg@gmail.com>
parents: 10840
diff changeset
339
0ee74d581c00 optimize & rewrite gcd
Jaroslav Hajek <highegg@gmail.com>
parents: 10840
diff changeset
340 dim_vector dv = aa.dims ();
0ee74d581c00 optimize & rewrite gcd
Jaroslav Hajek <highegg@gmail.com>
parents: 10840
diff changeset
341 if (aa.numel () == 1)
0ee74d581c00 optimize & rewrite gcd
Jaroslav Hajek <highegg@gmail.com>
parents: 10840
diff changeset
342 dv = bb.dims ();
0ee74d581c00 optimize & rewrite gcd
Jaroslav Hajek <highegg@gmail.com>
parents: 10840
diff changeset
343 else if (bb.numel () != 1 && bb.dims () != dv)
29961
7d6709900da7 eliminate octave:: namespace tags in DEFUN and DEFMETHOD and more
John W. Eaton <jwe@octave.org>
parents: 29958
diff changeset
344 err_nonconformant ("gcd", a.dims (), b.dims ());
11021
0ee74d581c00 optimize & rewrite gcd
Jaroslav Hajek <highegg@gmail.com>
parents: 10840
diff changeset
345
0ee74d581c00 optimize & rewrite gcd
Jaroslav Hajek <highegg@gmail.com>
parents: 10840
diff changeset
346 NDA gg (dv), xx (dv), yy (dv);
0ee74d581c00 optimize & rewrite gcd
Jaroslav Hajek <highegg@gmail.com>
parents: 10840
diff changeset
347
29931
7faff48840eb prefer data over fortran_vec for read-only access to data
John W. Eaton <jwe@octave.org>
parents: 29359
diff changeset
348 const T *aptr = aa.data ();
7faff48840eb prefer data over fortran_vec for read-only access to data
John W. Eaton <jwe@octave.org>
parents: 29359
diff changeset
349 const T *bptr = bb.data ();
11021
0ee74d581c00 optimize & rewrite gcd
Jaroslav Hajek <highegg@gmail.com>
parents: 10840
diff changeset
350
11064
61a6c99d8ff7 gcd.cc: style fixes
John W. Eaton <jwe@octave.org>
parents: 11062
diff changeset
351 bool inca = aa.numel () != 1;
61a6c99d8ff7 gcd.cc: style fixes
John W. Eaton <jwe@octave.org>
parents: 11062
diff changeset
352 bool incb = bb.numel () != 1;
11021
0ee74d581c00 optimize & rewrite gcd
Jaroslav Hajek <highegg@gmail.com>
parents: 10840
diff changeset
353
0ee74d581c00 optimize & rewrite gcd
Jaroslav Hajek <highegg@gmail.com>
parents: 10840
diff changeset
354 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
355 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
356 T *yptr = yy.fortran_vec ();
11021
0ee74d581c00 optimize & rewrite gcd
Jaroslav Hajek <highegg@gmail.com>
parents: 10840
diff changeset
357
0ee74d581c00 optimize & rewrite gcd
Jaroslav Hajek <highegg@gmail.com>
parents: 10840
diff changeset
358 octave_idx_type n = gg.numel ();
0ee74d581c00 optimize & rewrite gcd
Jaroslav Hajek <highegg@gmail.com>
parents: 10840
diff changeset
359 for (octave_idx_type i = 0; i < n; i++)
0ee74d581c00 optimize & rewrite gcd
Jaroslav Hajek <highegg@gmail.com>
parents: 10840
diff changeset
360 {
0ee74d581c00 optimize & rewrite gcd
Jaroslav Hajek <highegg@gmail.com>
parents: 10840
diff changeset
361 octave_quit ();
0ee74d581c00 optimize & rewrite gcd
Jaroslav Hajek <highegg@gmail.com>
parents: 10840
diff changeset
362
0ee74d581c00 optimize & rewrite gcd
Jaroslav Hajek <highegg@gmail.com>
parents: 10840
diff changeset
363 *gptr++ = extended_gcd (*aptr, *bptr, *xptr++, *yptr++);
11064
61a6c99d8ff7 gcd.cc: style fixes
John W. Eaton <jwe@octave.org>
parents: 11062
diff changeset
364
11021
0ee74d581c00 optimize & rewrite gcd
Jaroslav Hajek <highegg@gmail.com>
parents: 10840
diff changeset
365 aptr += inca;
0ee74d581c00 optimize & rewrite gcd
Jaroslav Hajek <highegg@gmail.com>
parents: 10840
diff changeset
366 bptr += incb;
0ee74d581c00 optimize & rewrite gcd
Jaroslav Hajek <highegg@gmail.com>
parents: 10840
diff changeset
367 }
0ee74d581c00 optimize & rewrite gcd
Jaroslav Hajek <highegg@gmail.com>
parents: 10840
diff changeset
368
0ee74d581c00 optimize & rewrite gcd
Jaroslav Hajek <highegg@gmail.com>
parents: 10840
diff changeset
369 x = xx;
0ee74d581c00 optimize & rewrite gcd
Jaroslav Hajek <highegg@gmail.com>
parents: 10840
diff changeset
370 y = yy;
11064
61a6c99d8ff7 gcd.cc: style fixes
John W. Eaton <jwe@octave.org>
parents: 11062
diff changeset
371
11021
0ee74d581c00 optimize & rewrite gcd
Jaroslav Hajek <highegg@gmail.com>
parents: 10840
diff changeset
372 retval = gg;
0ee74d581c00 optimize & rewrite gcd
Jaroslav Hajek <highegg@gmail.com>
parents: 10840
diff changeset
373 }
0ee74d581c00 optimize & rewrite gcd
Jaroslav Hajek <highegg@gmail.com>
parents: 10840
diff changeset
374
0ee74d581c00 optimize & rewrite gcd
Jaroslav Hajek <highegg@gmail.com>
parents: 10840
diff changeset
375 return retval;
7815
a41df65f3f00 Add some single precision test code and fix resulting bugs
David Bateman <dbateman@free.fr>
parents: 7017
diff changeset
376 }
a41df65f3f00 Add some single precision test code and fix resulting bugs
David Bateman <dbateman@free.fr>
parents: 7017
diff changeset
377
11021
0ee74d581c00 optimize & rewrite gcd
Jaroslav Hajek <highegg@gmail.com>
parents: 10840
diff changeset
378 // Dispatcher
0ee74d581c00 optimize & rewrite gcd
Jaroslav Hajek <highegg@gmail.com>
parents: 10840
diff changeset
379 static octave_value
0ee74d581c00 optimize & rewrite gcd
Jaroslav Hajek <highegg@gmail.com>
parents: 10840
diff changeset
380 do_extended_gcd (const octave_value& a, const octave_value& b,
0ee74d581c00 optimize & rewrite gcd
Jaroslav Hajek <highegg@gmail.com>
parents: 10840
diff changeset
381 octave_value& x, octave_value& y)
0ee74d581c00 optimize & rewrite gcd
Jaroslav Hajek <highegg@gmail.com>
parents: 10840
diff changeset
382 {
0ee74d581c00 optimize & rewrite gcd
Jaroslav Hajek <highegg@gmail.com>
parents: 10840
diff changeset
383 octave_value retval;
11064
61a6c99d8ff7 gcd.cc: style fixes
John W. Eaton <jwe@octave.org>
parents: 11062
diff changeset
384
11021
0ee74d581c00 optimize & rewrite gcd
Jaroslav Hajek <highegg@gmail.com>
parents: 10840
diff changeset
385 builtin_type_t btyp = btyp_mixed_numeric (a.builtin_type (),
0ee74d581c00 optimize & rewrite gcd
Jaroslav Hajek <highegg@gmail.com>
parents: 10840
diff changeset
386 b.builtin_type ());
0ee74d581c00 optimize & rewrite gcd
Jaroslav Hajek <highegg@gmail.com>
parents: 10840
diff changeset
387 switch (btyp)
0ee74d581c00 optimize & rewrite gcd
Jaroslav Hajek <highegg@gmail.com>
parents: 10840
diff changeset
388 {
0ee74d581c00 optimize & rewrite gcd
Jaroslav Hajek <highegg@gmail.com>
parents: 10840
diff changeset
389 case btyp_double:
0ee74d581c00 optimize & rewrite gcd
Jaroslav Hajek <highegg@gmail.com>
parents: 10840
diff changeset
390 case btyp_float:
0ee74d581c00 optimize & rewrite gcd
Jaroslav Hajek <highegg@gmail.com>
parents: 10840
diff changeset
391 retval = do_extended_gcd<NDArray> (a, b, x, y);
0ee74d581c00 optimize & rewrite gcd
Jaroslav Hajek <highegg@gmail.com>
parents: 10840
diff changeset
392 break;
11064
61a6c99d8ff7 gcd.cc: style fixes
John W. Eaton <jwe@octave.org>
parents: 11062
diff changeset
393
22407
34ce5be04942 maint: Style check C++ code in libinterp/.
Rik <rik@octave.org>
parents: 22327
diff changeset
394 #define MAKE_INT_BRANCH(X) \
34ce5be04942 maint: Style check C++ code in libinterp/.
Rik <rik@octave.org>
parents: 22327
diff changeset
395 case btyp_ ## X: \
34ce5be04942 maint: Style check C++ code in libinterp/.
Rik <rik@octave.org>
parents: 22327
diff changeset
396 retval = do_extended_gcd<X ## NDArray> (a, b, x, y); \
34ce5be04942 maint: Style check C++ code in libinterp/.
Rik <rik@octave.org>
parents: 22327
diff changeset
397 break
11064
61a6c99d8ff7 gcd.cc: style fixes
John W. Eaton <jwe@octave.org>
parents: 11062
diff changeset
398
11021
0ee74d581c00 optimize & rewrite gcd
Jaroslav Hajek <highegg@gmail.com>
parents: 10840
diff changeset
399 MAKE_INT_BRANCH (int8);
0ee74d581c00 optimize & rewrite gcd
Jaroslav Hajek <highegg@gmail.com>
parents: 10840
diff changeset
400 MAKE_INT_BRANCH (int16);
0ee74d581c00 optimize & rewrite gcd
Jaroslav Hajek <highegg@gmail.com>
parents: 10840
diff changeset
401 MAKE_INT_BRANCH (int32);
0ee74d581c00 optimize & rewrite gcd
Jaroslav Hajek <highegg@gmail.com>
parents: 10840
diff changeset
402 MAKE_INT_BRANCH (int64);
0ee74d581c00 optimize & rewrite gcd
Jaroslav Hajek <highegg@gmail.com>
parents: 10840
diff changeset
403 MAKE_INT_BRANCH (uint8);
0ee74d581c00 optimize & rewrite gcd
Jaroslav Hajek <highegg@gmail.com>
parents: 10840
diff changeset
404 MAKE_INT_BRANCH (uint16);
0ee74d581c00 optimize & rewrite gcd
Jaroslav Hajek <highegg@gmail.com>
parents: 10840
diff changeset
405 MAKE_INT_BRANCH (uint32);
0ee74d581c00 optimize & rewrite gcd
Jaroslav Hajek <highegg@gmail.com>
parents: 10840
diff changeset
406 MAKE_INT_BRANCH (uint64);
11064
61a6c99d8ff7 gcd.cc: style fixes
John W. Eaton <jwe@octave.org>
parents: 11062
diff changeset
407
11021
0ee74d581c00 optimize & rewrite gcd
Jaroslav Hajek <highegg@gmail.com>
parents: 10840
diff changeset
408 #undef MAKE_INT_BRANCH
11064
61a6c99d8ff7 gcd.cc: style fixes
John W. Eaton <jwe@octave.org>
parents: 11062
diff changeset
409
11021
0ee74d581c00 optimize & rewrite gcd
Jaroslav Hajek <highegg@gmail.com>
parents: 10840
diff changeset
410 case btyp_complex:
11062
88d78ee12ee8 Implement gcd for Gaussian (complex) integers
Jordi Gutiérrez Hermoso <jordigh@gmail.com>
parents: 11061
diff changeset
411 retval = do_extended_gcd<ComplexNDArray> (a, b, x, y);
88d78ee12ee8 Implement gcd for Gaussian (complex) integers
Jordi Gutiérrez Hermoso <jordigh@gmail.com>
parents: 11061
diff changeset
412 break;
11064
61a6c99d8ff7 gcd.cc: style fixes
John W. Eaton <jwe@octave.org>
parents: 11062
diff changeset
413
11021
0ee74d581c00 optimize & rewrite gcd
Jaroslav Hajek <highegg@gmail.com>
parents: 10840
diff changeset
414 case btyp_float_complex:
11062
88d78ee12ee8 Implement gcd for Gaussian (complex) integers
Jordi Gutiérrez Hermoso <jordigh@gmail.com>
parents: 11061
diff changeset
415 retval = do_extended_gcd<FloatComplexNDArray> (a, b, x, y);
88d78ee12ee8 Implement gcd for Gaussian (complex) integers
Jordi Gutiérrez Hermoso <jordigh@gmail.com>
parents: 11061
diff changeset
416 break;
11064
61a6c99d8ff7 gcd.cc: style fixes
John W. Eaton <jwe@octave.org>
parents: 11062
diff changeset
417
11021
0ee74d581c00 optimize & rewrite gcd
Jaroslav Hajek <highegg@gmail.com>
parents: 10840
diff changeset
418 default:
0ee74d581c00 optimize & rewrite gcd
Jaroslav Hajek <highegg@gmail.com>
parents: 10840
diff changeset
419 error ("gcd: invalid class combination for gcd: %s and %s\n",
0ee74d581c00 optimize & rewrite gcd
Jaroslav Hajek <highegg@gmail.com>
parents: 10840
diff changeset
420 a.class_name ().c_str (), b.class_name ().c_str ());
0ee74d581c00 optimize & rewrite gcd
Jaroslav Hajek <highegg@gmail.com>
parents: 10840
diff changeset
421 }
0ee74d581c00 optimize & rewrite gcd
Jaroslav Hajek <highegg@gmail.com>
parents: 10840
diff changeset
422
0ee74d581c00 optimize & rewrite gcd
Jaroslav Hajek <highegg@gmail.com>
parents: 10840
diff changeset
423 // For consistency.
23583
b7747a2c88b2 maint: Deprecate is_sparse_type and replace with issparse.
Rik <rik@octave.org>
parents: 23450
diff changeset
424 if (a.issparse () && b.issparse ())
11021
0ee74d581c00 optimize & rewrite gcd
Jaroslav Hajek <highegg@gmail.com>
parents: 10840
diff changeset
425 {
0ee74d581c00 optimize & rewrite gcd
Jaroslav Hajek <highegg@gmail.com>
parents: 10840
diff changeset
426 retval = retval.sparse_matrix_value ();
0ee74d581c00 optimize & rewrite gcd
Jaroslav Hajek <highegg@gmail.com>
parents: 10840
diff changeset
427 x = x.sparse_matrix_value ();
0ee74d581c00 optimize & rewrite gcd
Jaroslav Hajek <highegg@gmail.com>
parents: 10840
diff changeset
428 y = y.sparse_matrix_value ();
0ee74d581c00 optimize & rewrite gcd
Jaroslav Hajek <highegg@gmail.com>
parents: 10840
diff changeset
429 }
0ee74d581c00 optimize & rewrite gcd
Jaroslav Hajek <highegg@gmail.com>
parents: 10840
diff changeset
430
0ee74d581c00 optimize & rewrite gcd
Jaroslav Hajek <highegg@gmail.com>
parents: 10840
diff changeset
431 if (btyp == btyp_float)
0ee74d581c00 optimize & rewrite gcd
Jaroslav Hajek <highegg@gmail.com>
parents: 10840
diff changeset
432 {
0ee74d581c00 optimize & rewrite gcd
Jaroslav Hajek <highegg@gmail.com>
parents: 10840
diff changeset
433 retval = retval.float_array_value ();
0ee74d581c00 optimize & rewrite gcd
Jaroslav Hajek <highegg@gmail.com>
parents: 10840
diff changeset
434 x = x.float_array_value ();
0ee74d581c00 optimize & rewrite gcd
Jaroslav Hajek <highegg@gmail.com>
parents: 10840
diff changeset
435 y = y.float_array_value ();
0ee74d581c00 optimize & rewrite gcd
Jaroslav Hajek <highegg@gmail.com>
parents: 10840
diff changeset
436 }
0ee74d581c00 optimize & rewrite gcd
Jaroslav Hajek <highegg@gmail.com>
parents: 10840
diff changeset
437
0ee74d581c00 optimize & rewrite gcd
Jaroslav Hajek <highegg@gmail.com>
parents: 10840
diff changeset
438 return retval;
0ee74d581c00 optimize & rewrite gcd
Jaroslav Hajek <highegg@gmail.com>
parents: 10840
diff changeset
439 }
0ee74d581c00 optimize & rewrite gcd
Jaroslav Hajek <highegg@gmail.com>
parents: 10840
diff changeset
440
15039
e753177cde93 maint: Move non-dynamically linked functions from DLD-FUNCTIONS/ to corefcn/ directory
Rik <rik@octave.org>
parents: 14854
diff changeset
441 DEFUN (gcd, args, nargout,
21966
112b20240c87 move docstrings in C++ files out of C strings and into comments
John W. Eaton <jwe@octave.org>
parents: 21942
diff changeset
442 doc: /* -*- texinfo -*-
112b20240c87 move docstrings in C++ files out of C strings and into comments
John W. Eaton <jwe@octave.org>
parents: 21942
diff changeset
443 @deftypefn {} {@var{g} =} gcd (@var{a1}, @var{a2}, @dots{})
112b20240c87 move docstrings in C++ files out of C strings and into comments
John W. Eaton <jwe@octave.org>
parents: 21942
diff changeset
444 @deftypefnx {} {[@var{g}, @var{v1}, @dots{}] =} gcd (@var{a1}, @var{a2}, @dots{})
112b20240c87 move docstrings in C++ files out of C strings and into comments
John W. Eaton <jwe@octave.org>
parents: 21942
diff changeset
445 Compute the greatest common divisor of @var{a1}, @var{a2}, @dots{}.
112b20240c87 move docstrings in C++ files out of C strings and into comments
John W. Eaton <jwe@octave.org>
parents: 21942
diff changeset
446
27954
cbabcd7bb6c3 doc: Clarify documentation of inputs for gcd (bug #57609).
Rik <rik@octave.org>
parents: 27923
diff changeset
447 All arguments must be the same size or scalar. For arrays, the greatest common
cbabcd7bb6c3 doc: Clarify documentation of inputs for gcd (bug #57609).
Rik <rik@octave.org>
parents: 27923
diff changeset
448 divisor is calculated for each element individually. All elements must be
cbabcd7bb6c3 doc: Clarify documentation of inputs for gcd (bug #57609).
Rik <rik@octave.org>
parents: 27923
diff changeset
449 ordinary or Gaussian (complex) integers. Note that for Gaussian integers, the
cbabcd7bb6c3 doc: Clarify documentation of inputs for gcd (bug #57609).
Rik <rik@octave.org>
parents: 27923
diff changeset
450 gcd is only unique up to a phase factor (multiplication by 1, -1, i, or -i), so
cbabcd7bb6c3 doc: Clarify documentation of inputs for gcd (bug #57609).
Rik <rik@octave.org>
parents: 27923
diff changeset
451 an arbitrary greatest common divisor among the four possible is returned.
21966
112b20240c87 move docstrings in C++ files out of C strings and into comments
John W. Eaton <jwe@octave.org>
parents: 21942
diff changeset
452
112b20240c87 move docstrings in C++ files out of C strings and into comments
John W. Eaton <jwe@octave.org>
parents: 21942
diff changeset
453 Optional return arguments @var{v1}, @dots{}, contain integer vectors such
112b20240c87 move docstrings in C++ files out of C strings and into comments
John W. Eaton <jwe@octave.org>
parents: 21942
diff changeset
454 that,
112b20240c87 move docstrings in C++ files out of C strings and into comments
John W. Eaton <jwe@octave.org>
parents: 21942
diff changeset
455
112b20240c87 move docstrings in C++ files out of C strings and into comments
John W. Eaton <jwe@octave.org>
parents: 21942
diff changeset
456 @tex
112b20240c87 move docstrings in C++ files out of C strings and into comments
John W. Eaton <jwe@octave.org>
parents: 21942
diff changeset
457 $g = v_1 a_1 + v_2 a_2 + \cdots$
112b20240c87 move docstrings in C++ files out of C strings and into comments
John W. Eaton <jwe@octave.org>
parents: 21942
diff changeset
458 @end tex
112b20240c87 move docstrings in C++ files out of C strings and into comments
John W. Eaton <jwe@octave.org>
parents: 21942
diff changeset
459 @ifnottex
112b20240c87 move docstrings in C++ files out of C strings and into comments
John W. Eaton <jwe@octave.org>
parents: 21942
diff changeset
460
112b20240c87 move docstrings in C++ files out of C strings and into comments
John W. Eaton <jwe@octave.org>
parents: 21942
diff changeset
461 @example
112b20240c87 move docstrings in C++ files out of C strings and into comments
John W. Eaton <jwe@octave.org>
parents: 21942
diff changeset
462 @var{g} = @var{v1} .* @var{a1} + @var{v2} .* @var{a2} + @dots{}
112b20240c87 move docstrings in C++ files out of C strings and into comments
John W. Eaton <jwe@octave.org>
parents: 21942
diff changeset
463 @end example
112b20240c87 move docstrings in C++ files out of C strings and into comments
John W. Eaton <jwe@octave.org>
parents: 21942
diff changeset
464
112b20240c87 move docstrings in C++ files out of C strings and into comments
John W. Eaton <jwe@octave.org>
parents: 21942
diff changeset
465 @end ifnottex
112b20240c87 move docstrings in C++ files out of C strings and into comments
John W. Eaton <jwe@octave.org>
parents: 21942
diff changeset
466
112b20240c87 move docstrings in C++ files out of C strings and into comments
John W. Eaton <jwe@octave.org>
parents: 21942
diff changeset
467 Example code:
112b20240c87 move docstrings in C++ files out of C strings and into comments
John W. Eaton <jwe@octave.org>
parents: 21942
diff changeset
468
112b20240c87 move docstrings in C++ files out of C strings and into comments
John W. Eaton <jwe@octave.org>
parents: 21942
diff changeset
469 @example
112b20240c87 move docstrings in C++ files out of C strings and into comments
John W. Eaton <jwe@octave.org>
parents: 21942
diff changeset
470 @group
112b20240c87 move docstrings in C++ files out of C strings and into comments
John W. Eaton <jwe@octave.org>
parents: 21942
diff changeset
471 gcd ([15, 9], [20, 18])
112b20240c87 move docstrings in C++ files out of C strings and into comments
John W. Eaton <jwe@octave.org>
parents: 21942
diff changeset
472 @result{} 5 9
112b20240c87 move docstrings in C++ files out of C strings and into comments
John W. Eaton <jwe@octave.org>
parents: 21942
diff changeset
473 @end group
112b20240c87 move docstrings in C++ files out of C strings and into comments
John W. Eaton <jwe@octave.org>
parents: 21942
diff changeset
474 @end example
112b20240c87 move docstrings in C++ files out of C strings and into comments
John W. Eaton <jwe@octave.org>
parents: 21942
diff changeset
475
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))
30346
91c6288781ba maint: Shorten some long lines in libinterp to <= 80 characters (bug #57599)
Rik <rik@octave.org>
parents: 29961
diff changeset
514 %!assert (gcd (uint64 (200), uint64 (300), uint64 (50), uint64 (35)),
91c6288781ba maint: Shorten some long lines in libinterp to <= 80 characters (bug #57599)
Rik <rik@octave.org>
parents: 29961
diff changeset
515 %! uint64 (5))
14501
60e5cf354d80 Update %!tests in DLD-FUNCTIONS/ directory with Octave coding conventions.
Rik <octave@nomad.inbox5.com>
parents: 14360
diff changeset
516 %!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
517
19132
c3bdd640a4dd * gcd.cc: add more tests
Jordi Gutiérrez Hermoso <jordigh@octave.org>
parents: 18100
diff changeset
518 %!test
c3bdd640a4dd * gcd.cc: add more tests
Jordi Gutiérrez Hermoso <jordigh@octave.org>
parents: 18100
diff changeset
519 %! p = [953 967];
c3bdd640a4dd * gcd.cc: add more tests
Jordi Gutiérrez Hermoso <jordigh@octave.org>
parents: 18100
diff changeset
520 %! u = [953 + i*971, 967 + i*977];
c3bdd640a4dd * gcd.cc: add more tests
Jordi Gutiérrez Hermoso <jordigh@octave.org>
parents: 18100
diff changeset
521 %! [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
522 %! [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
523 %! assert (d, 1);
ecce63c99c3f maint: Add semicolons to terminate code in %! blocks.
Rik <rik@octave.org>
parents: 21301
diff changeset
524 %! assert (sum (p.*k), d);
ecce63c99c3f maint: Add semicolons to terminate code in %! blocks.
Rik <rik@octave.org>
parents: 21301
diff changeset
525 %! assert (abs (z), sqrt (2));
ecce63c99c3f maint: Add semicolons to terminate code in %! blocks.
Rik <rik@octave.org>
parents: 21301
diff changeset
526 %! 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
527
21581
6fab85c1538f maint: Follow Octave conventions for use of semicolon in BIST tests.
Rik <rik@octave.org>
parents: 21580
diff changeset
528 %!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
529 %!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
530
14501
60e5cf354d80 Update %!tests in DLD-FUNCTIONS/ directory with Octave coding conventions.
Rik <octave@nomad.inbox5.com>
parents: 14360
diff changeset
531 %!error gcd ()
7815
a41df65f3f00 Add some single precision test code and fix resulting bugs
David Bateman <dbateman@free.fr>
parents: 7017
diff changeset
532
a41df65f3f00 Add some single precision test code and fix resulting bugs
David Bateman <dbateman@free.fr>
parents: 7017
diff changeset
533 %!test
a41df65f3f00 Add some single precision test code and fix resulting bugs
David Bateman <dbateman@free.fr>
parents: 7017
diff changeset
534 %! s.a = 1;
14501
60e5cf354d80 Update %!tests in DLD-FUNCTIONS/ directory with Octave coding conventions.
Rik <octave@nomad.inbox5.com>
parents: 14360
diff changeset
535 %! fail ("gcd (s)");
4864
d49d761c8c93 [project @ 2004-04-16 16:01:11 by jwe]
jwe
parents:
diff changeset
536 */
29958
32c3a5805893 move DEFUN and DEFMETHOD functions inside octave namespace
John W. Eaton <jwe@octave.org>
parents: 29931
diff changeset
537
31605
e88a07dec498 maint: Use macros to begin/end C++ namespaces.
Rik <rik@octave.org>
parents: 30991
diff changeset
538 OCTAVE_END_NAMESPACE(octave)