Mercurial > octave
annotate liboctave/array/Range.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 | 2989202f92f8 |
children | dfa5d9c3ae72 aac27ad79be6 |
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:
30420
diff
changeset
|
3 // Copyright (C) 1993-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 //////////////////////////////////////////////////////////////////////// |
3 | 25 |
21724
aba2e6293dd8
use "#if ..." consistently instead of "#ifdef" and "#ifndef"
John W. Eaton <jwe@octave.org>
parents:
21321
diff
changeset
|
26 #if defined (HAVE_CONFIG_H) |
21301
40de9f8f23a6
Use '#include "config.h"' rather than <config.h>.
Rik <rik@octave.org>
parents:
21229
diff
changeset
|
27 # include "config.h" |
3 | 28 #endif |
29 | |
23443
3f1bf237908b
maint: Eliminate <cfloat.h> header from liboctave files.
Rik <rik@octave.org>
parents:
23398
diff
changeset
|
30 #include <cmath> |
1367 | 31 |
25438
cb1606f78f6b
prefer <istream>, <ostream>, or <iosfwd> to <iostream> where possible
John W. Eaton <jwe@octave.org>
parents:
25054
diff
changeset
|
32 #include <istream> |
6490 | 33 #include <limits> |
25438
cb1606f78f6b
prefer <istream>, <ostream>, or <iosfwd> to <iostream> where possible
John W. Eaton <jwe@octave.org>
parents:
25054
diff
changeset
|
34 #include <ostream> |
3 | 35 |
23443
3f1bf237908b
maint: Eliminate <cfloat.h> header from liboctave files.
Rik <rik@octave.org>
parents:
23398
diff
changeset
|
36 #include "Array-util.h" |
3 | 37 #include "Range.h" |
7458 | 38 #include "lo-error.h" |
2383 | 39 #include "lo-mappers.h" |
40 #include "lo-utils.h" | |
41 | |
31605
e88a07dec498
maint: Use macros to begin/end C++ namespaces.
Rik <rik@octave.org>
parents:
30835
diff
changeset
|
42 OCTAVE_BEGIN_NAMESPACE(octave) |
e88a07dec498
maint: Use macros to begin/end C++ namespaces.
Rik <rik@octave.org>
parents:
30835
diff
changeset
|
43 |
28635
b6371946f106
new template for range objects
John W. Eaton <jwe@octave.org>
parents:
28628
diff
changeset
|
44 template <typename T> |
b6371946f106
new template for range objects
John W. Eaton <jwe@octave.org>
parents:
28628
diff
changeset
|
45 T xtfloor (T x, T ct) |
b6371946f106
new template for range objects
John W. Eaton <jwe@octave.org>
parents:
28628
diff
changeset
|
46 { |
b6371946f106
new template for range objects
John W. Eaton <jwe@octave.org>
parents:
28628
diff
changeset
|
47 // C---------FLOOR(X) is the largest integer algebraically less than |
b6371946f106
new template for range objects
John W. Eaton <jwe@octave.org>
parents:
28628
diff
changeset
|
48 // C or equal to X; that is, the unfuzzy FLOOR function. |
b6371946f106
new template for range objects
John W. Eaton <jwe@octave.org>
parents:
28628
diff
changeset
|
49 |
b6371946f106
new template for range objects
John W. Eaton <jwe@octave.org>
parents:
28628
diff
changeset
|
50 // DINT (X) = X - DMOD (X, 1.0); |
b6371946f106
new template for range objects
John W. Eaton <jwe@octave.org>
parents:
28628
diff
changeset
|
51 // FLOOR (X) = DINT (X) - DMOD (2.0 + DSIGN (1.0, X), 3.0); |
b6371946f106
new template for range objects
John W. Eaton <jwe@octave.org>
parents:
28628
diff
changeset
|
52 |
b6371946f106
new template for range objects
John W. Eaton <jwe@octave.org>
parents:
28628
diff
changeset
|
53 // C---------Hagerty's FL5 function follows... |
b6371946f106
new template for range objects
John W. Eaton <jwe@octave.org>
parents:
28628
diff
changeset
|
54 |
b6371946f106
new template for range objects
John W. Eaton <jwe@octave.org>
parents:
28628
diff
changeset
|
55 T q = 1; |
b6371946f106
new template for range objects
John W. Eaton <jwe@octave.org>
parents:
28628
diff
changeset
|
56 |
b6371946f106
new template for range objects
John W. Eaton <jwe@octave.org>
parents:
28628
diff
changeset
|
57 if (x < 0) |
b6371946f106
new template for range objects
John W. Eaton <jwe@octave.org>
parents:
28628
diff
changeset
|
58 q = 1 - ct; |
b6371946f106
new template for range objects
John W. Eaton <jwe@octave.org>
parents:
28628
diff
changeset
|
59 |
b6371946f106
new template for range objects
John W. Eaton <jwe@octave.org>
parents:
28628
diff
changeset
|
60 T rmax = q / (2 - ct); |
b6371946f106
new template for range objects
John W. Eaton <jwe@octave.org>
parents:
28628
diff
changeset
|
61 |
b6371946f106
new template for range objects
John W. Eaton <jwe@octave.org>
parents:
28628
diff
changeset
|
62 T t1 = 1 + std::floor (x); |
b6371946f106
new template for range objects
John W. Eaton <jwe@octave.org>
parents:
28628
diff
changeset
|
63 t1 = (ct / q) * (t1 < 0 ? -t1 : t1); |
b6371946f106
new template for range objects
John W. Eaton <jwe@octave.org>
parents:
28628
diff
changeset
|
64 t1 = (rmax < t1 ? rmax : t1); |
b6371946f106
new template for range objects
John W. Eaton <jwe@octave.org>
parents:
28628
diff
changeset
|
65 t1 = (ct > t1 ? ct : t1); |
b6371946f106
new template for range objects
John W. Eaton <jwe@octave.org>
parents:
28628
diff
changeset
|
66 t1 = std::floor (x + t1); |
b6371946f106
new template for range objects
John W. Eaton <jwe@octave.org>
parents:
28628
diff
changeset
|
67 |
b6371946f106
new template for range objects
John W. Eaton <jwe@octave.org>
parents:
28628
diff
changeset
|
68 if (x <= 0 || (t1 - x) < rmax) |
b6371946f106
new template for range objects
John W. Eaton <jwe@octave.org>
parents:
28628
diff
changeset
|
69 return t1; |
b6371946f106
new template for range objects
John W. Eaton <jwe@octave.org>
parents:
28628
diff
changeset
|
70 else |
b6371946f106
new template for range objects
John W. Eaton <jwe@octave.org>
parents:
28628
diff
changeset
|
71 return t1 - 1; |
b6371946f106
new template for range objects
John W. Eaton <jwe@octave.org>
parents:
28628
diff
changeset
|
72 } |
b6371946f106
new template for range objects
John W. Eaton <jwe@octave.org>
parents:
28628
diff
changeset
|
73 |
b6371946f106
new template for range objects
John W. Eaton <jwe@octave.org>
parents:
28628
diff
changeset
|
74 template <typename T> |
b6371946f106
new template for range objects
John W. Eaton <jwe@octave.org>
parents:
28628
diff
changeset
|
75 bool xteq (T u, T v, T ct = 3 * std::numeric_limits<T>::epsilon ()) |
b6371946f106
new template for range objects
John W. Eaton <jwe@octave.org>
parents:
28628
diff
changeset
|
76 { |
b6371946f106
new template for range objects
John W. Eaton <jwe@octave.org>
parents:
28628
diff
changeset
|
77 T tu = std::abs (u); |
b6371946f106
new template for range objects
John W. Eaton <jwe@octave.org>
parents:
28628
diff
changeset
|
78 T tv = std::abs (v); |
b6371946f106
new template for range objects
John W. Eaton <jwe@octave.org>
parents:
28628
diff
changeset
|
79 |
b6371946f106
new template for range objects
John W. Eaton <jwe@octave.org>
parents:
28628
diff
changeset
|
80 return std::abs (u - v) < ((tu > tv ? tu : tv) * ct); |
b6371946f106
new template for range objects
John W. Eaton <jwe@octave.org>
parents:
28628
diff
changeset
|
81 } |
b6371946f106
new template for range objects
John W. Eaton <jwe@octave.org>
parents:
28628
diff
changeset
|
82 |
b6371946f106
new template for range objects
John W. Eaton <jwe@octave.org>
parents:
28628
diff
changeset
|
83 template <typename T> |
b6371946f106
new template for range objects
John W. Eaton <jwe@octave.org>
parents:
28628
diff
changeset
|
84 octave_idx_type xnumel_internal (T base, T limit, T inc) |
b6371946f106
new template for range objects
John W. Eaton <jwe@octave.org>
parents:
28628
diff
changeset
|
85 { |
b6371946f106
new template for range objects
John W. Eaton <jwe@octave.org>
parents:
28628
diff
changeset
|
86 octave_idx_type retval = -1; |
28857
43ad651cf5a0
eliminate unnecessary uses of octave:: namespace qualifier
John W. Eaton <jwe@octave.org>
parents:
28636
diff
changeset
|
87 if (! math::isfinite (base) || ! math::isfinite (inc) |
43ad651cf5a0
eliminate unnecessary uses of octave:: namespace qualifier
John W. Eaton <jwe@octave.org>
parents:
28636
diff
changeset
|
88 || math::isnan (limit)) |
28635
b6371946f106
new template for range objects
John W. Eaton <jwe@octave.org>
parents:
28628
diff
changeset
|
89 retval = -2; |
28857
43ad651cf5a0
eliminate unnecessary uses of octave:: namespace qualifier
John W. Eaton <jwe@octave.org>
parents:
28636
diff
changeset
|
90 else if (math::isinf (limit) |
28635
b6371946f106
new template for range objects
John W. Eaton <jwe@octave.org>
parents:
28628
diff
changeset
|
91 && ((inc > 0 && limit > 0) |
b6371946f106
new template for range objects
John W. Eaton <jwe@octave.org>
parents:
28628
diff
changeset
|
92 || (inc < 0 && limit < 0))) |
b6371946f106
new template for range objects
John W. Eaton <jwe@octave.org>
parents:
28628
diff
changeset
|
93 retval = std::numeric_limits<octave_idx_type>::max () - 1; |
b6371946f106
new template for range objects
John W. Eaton <jwe@octave.org>
parents:
28628
diff
changeset
|
94 else if (inc == 0 |
b6371946f106
new template for range objects
John W. Eaton <jwe@octave.org>
parents:
28628
diff
changeset
|
95 || (limit > base && inc < 0) |
b6371946f106
new template for range objects
John W. Eaton <jwe@octave.org>
parents:
28628
diff
changeset
|
96 || (limit < base && inc > 0)) |
b6371946f106
new template for range objects
John W. Eaton <jwe@octave.org>
parents:
28628
diff
changeset
|
97 { |
b6371946f106
new template for range objects
John W. Eaton <jwe@octave.org>
parents:
28628
diff
changeset
|
98 retval = 0; |
b6371946f106
new template for range objects
John W. Eaton <jwe@octave.org>
parents:
28628
diff
changeset
|
99 } |
b6371946f106
new template for range objects
John W. Eaton <jwe@octave.org>
parents:
28628
diff
changeset
|
100 else |
b6371946f106
new template for range objects
John W. Eaton <jwe@octave.org>
parents:
28628
diff
changeset
|
101 { |
b6371946f106
new template for range objects
John W. Eaton <jwe@octave.org>
parents:
28628
diff
changeset
|
102 T ct = 3 * std::numeric_limits<T>::epsilon (); |
b6371946f106
new template for range objects
John W. Eaton <jwe@octave.org>
parents:
28628
diff
changeset
|
103 |
b6371946f106
new template for range objects
John W. Eaton <jwe@octave.org>
parents:
28628
diff
changeset
|
104 T tmp = xtfloor ((limit - base + inc) / inc, ct); |
b6371946f106
new template for range objects
John W. Eaton <jwe@octave.org>
parents:
28628
diff
changeset
|
105 |
b6371946f106
new template for range objects
John W. Eaton <jwe@octave.org>
parents:
28628
diff
changeset
|
106 octave_idx_type n_elt |
b6371946f106
new template for range objects
John W. Eaton <jwe@octave.org>
parents:
28628
diff
changeset
|
107 = (tmp > 0 ? static_cast<octave_idx_type> (tmp) : 0); |
b6371946f106
new template for range objects
John W. Eaton <jwe@octave.org>
parents:
28628
diff
changeset
|
108 |
b6371946f106
new template for range objects
John W. Eaton <jwe@octave.org>
parents:
28628
diff
changeset
|
109 // If the final element that we would compute for the range is |
b6371946f106
new template for range objects
John W. Eaton <jwe@octave.org>
parents:
28628
diff
changeset
|
110 // equal to the limit of the range, or is an adjacent floating |
b6371946f106
new template for range objects
John W. Eaton <jwe@octave.org>
parents:
28628
diff
changeset
|
111 // point number, accept it. Otherwise, try a range with one |
b6371946f106
new template for range objects
John W. Eaton <jwe@octave.org>
parents:
28628
diff
changeset
|
112 // fewer element. If that fails, try again with one more |
b6371946f106
new template for range objects
John W. Eaton <jwe@octave.org>
parents:
28628
diff
changeset
|
113 // element. |
b6371946f106
new template for range objects
John W. Eaton <jwe@octave.org>
parents:
28628
diff
changeset
|
114 // |
b6371946f106
new template for range objects
John W. Eaton <jwe@octave.org>
parents:
28628
diff
changeset
|
115 // I'm not sure this is very good, but it seems to work better |
b6371946f106
new template for range objects
John W. Eaton <jwe@octave.org>
parents:
28628
diff
changeset
|
116 // than just using tfloor as above. For example, without it, |
b6371946f106
new template for range objects
John W. Eaton <jwe@octave.org>
parents:
28628
diff
changeset
|
117 // the expression 1.8:0.05:1.9 fails to produce the expected |
b6371946f106
new template for range objects
John W. Eaton <jwe@octave.org>
parents:
28628
diff
changeset
|
118 // result of [1.8, 1.85, 1.9]. |
b6371946f106
new template for range objects
John W. Eaton <jwe@octave.org>
parents:
28628
diff
changeset
|
119 |
b6371946f106
new template for range objects
John W. Eaton <jwe@octave.org>
parents:
28628
diff
changeset
|
120 if (! xteq (base + (n_elt - 1) * inc, limit)) |
b6371946f106
new template for range objects
John W. Eaton <jwe@octave.org>
parents:
28628
diff
changeset
|
121 { |
b6371946f106
new template for range objects
John W. Eaton <jwe@octave.org>
parents:
28628
diff
changeset
|
122 if (xteq (base + (n_elt - 2) * inc, limit)) |
b6371946f106
new template for range objects
John W. Eaton <jwe@octave.org>
parents:
28628
diff
changeset
|
123 n_elt--; |
b6371946f106
new template for range objects
John W. Eaton <jwe@octave.org>
parents:
28628
diff
changeset
|
124 else if (xteq (base + n_elt * inc, limit)) |
b6371946f106
new template for range objects
John W. Eaton <jwe@octave.org>
parents:
28628
diff
changeset
|
125 n_elt++; |
b6371946f106
new template for range objects
John W. Eaton <jwe@octave.org>
parents:
28628
diff
changeset
|
126 } |
b6371946f106
new template for range objects
John W. Eaton <jwe@octave.org>
parents:
28628
diff
changeset
|
127 |
b6371946f106
new template for range objects
John W. Eaton <jwe@octave.org>
parents:
28628
diff
changeset
|
128 retval = (n_elt < std::numeric_limits<octave_idx_type>::max () - 1 |
b6371946f106
new template for range objects
John W. Eaton <jwe@octave.org>
parents:
28628
diff
changeset
|
129 ? n_elt : -1); |
b6371946f106
new template for range objects
John W. Eaton <jwe@octave.org>
parents:
28628
diff
changeset
|
130 } |
b6371946f106
new template for range objects
John W. Eaton <jwe@octave.org>
parents:
28628
diff
changeset
|
131 |
b6371946f106
new template for range objects
John W. Eaton <jwe@octave.org>
parents:
28628
diff
changeset
|
132 return retval; |
b6371946f106
new template for range objects
John W. Eaton <jwe@octave.org>
parents:
28628
diff
changeset
|
133 } |
b6371946f106
new template for range objects
John W. Eaton <jwe@octave.org>
parents:
28628
diff
changeset
|
134 |
b6371946f106
new template for range objects
John W. Eaton <jwe@octave.org>
parents:
28628
diff
changeset
|
135 template <typename T> |
29353
715344f405f0
improve handling of nan, infinite value, and empty ranges (bug #59229)
John W. Eaton <jwe@octave.org>
parents:
29352
diff
changeset
|
136 bool xall_elements_are_ints (T base, T inc, T final_val, octave_idx_type nel) |
28635
b6371946f106
new template for range objects
John W. Eaton <jwe@octave.org>
parents:
28628
diff
changeset
|
137 { |
29353
715344f405f0
improve handling of nan, infinite value, and empty ranges (bug #59229)
John W. Eaton <jwe@octave.org>
parents:
29352
diff
changeset
|
138 // If the range is empty or NaN then there are no elements so there |
715344f405f0
improve handling of nan, infinite value, and empty ranges (bug #59229)
John W. Eaton <jwe@octave.org>
parents:
29352
diff
changeset
|
139 // can be no int elements. |
715344f405f0
improve handling of nan, infinite value, and empty ranges (bug #59229)
John W. Eaton <jwe@octave.org>
parents:
29352
diff
changeset
|
140 if (nel == 0 || math::isnan (final_val)) |
715344f405f0
improve handling of nan, infinite value, and empty ranges (bug #59229)
John W. Eaton <jwe@octave.org>
parents:
29352
diff
changeset
|
141 return false; |
28635
b6371946f106
new template for range objects
John W. Eaton <jwe@octave.org>
parents:
28628
diff
changeset
|
142 |
29353
715344f405f0
improve handling of nan, infinite value, and empty ranges (bug #59229)
John W. Eaton <jwe@octave.org>
parents:
29352
diff
changeset
|
143 // If the base and increment are ints, all elements will be |
715344f405f0
improve handling of nan, infinite value, and empty ranges (bug #59229)
John W. Eaton <jwe@octave.org>
parents:
29352
diff
changeset
|
144 // integers. |
715344f405f0
improve handling of nan, infinite value, and empty ranges (bug #59229)
John W. Eaton <jwe@octave.org>
parents:
29352
diff
changeset
|
145 if (math::nint_big (base) == base && math::nint_big (inc) == inc) |
715344f405f0
improve handling of nan, infinite value, and empty ranges (bug #59229)
John W. Eaton <jwe@octave.org>
parents:
29352
diff
changeset
|
146 return true; |
715344f405f0
improve handling of nan, infinite value, and empty ranges (bug #59229)
John W. Eaton <jwe@octave.org>
parents:
29352
diff
changeset
|
147 |
715344f405f0
improve handling of nan, infinite value, and empty ranges (bug #59229)
John W. Eaton <jwe@octave.org>
parents:
29352
diff
changeset
|
148 // If the range has only one element, then the base needs to be an |
715344f405f0
improve handling of nan, infinite value, and empty ranges (bug #59229)
John W. Eaton <jwe@octave.org>
parents:
29352
diff
changeset
|
149 // integer. |
715344f405f0
improve handling of nan, infinite value, and empty ranges (bug #59229)
John W. Eaton <jwe@octave.org>
parents:
29352
diff
changeset
|
150 if (nel == 1 && math::nint_big (base)) |
715344f405f0
improve handling of nan, infinite value, and empty ranges (bug #59229)
John W. Eaton <jwe@octave.org>
parents:
29352
diff
changeset
|
151 return true; |
715344f405f0
improve handling of nan, infinite value, and empty ranges (bug #59229)
John W. Eaton <jwe@octave.org>
parents:
29352
diff
changeset
|
152 |
715344f405f0
improve handling of nan, infinite value, and empty ranges (bug #59229)
John W. Eaton <jwe@octave.org>
parents:
29352
diff
changeset
|
153 return false; |
28635
b6371946f106
new template for range objects
John W. Eaton <jwe@octave.org>
parents:
28628
diff
changeset
|
154 } |
b6371946f106
new template for range objects
John W. Eaton <jwe@octave.org>
parents:
28628
diff
changeset
|
155 |
b6371946f106
new template for range objects
John W. Eaton <jwe@octave.org>
parents:
28628
diff
changeset
|
156 template <typename T> |
b6371946f106
new template for range objects
John W. Eaton <jwe@octave.org>
parents:
28628
diff
changeset
|
157 T |
b6371946f106
new template for range objects
John W. Eaton <jwe@octave.org>
parents:
28628
diff
changeset
|
158 xfinal_value (T base, T limit, T inc, octave_idx_type nel) |
b6371946f106
new template for range objects
John W. Eaton <jwe@octave.org>
parents:
28628
diff
changeset
|
159 { |
b6371946f106
new template for range objects
John W. Eaton <jwe@octave.org>
parents:
28628
diff
changeset
|
160 T retval = T (0); |
b6371946f106
new template for range objects
John W. Eaton <jwe@octave.org>
parents:
28628
diff
changeset
|
161 |
b6371946f106
new template for range objects
John W. Eaton <jwe@octave.org>
parents:
28628
diff
changeset
|
162 if (nel <= 1) |
b6371946f106
new template for range objects
John W. Eaton <jwe@octave.org>
parents:
28628
diff
changeset
|
163 return base; |
b6371946f106
new template for range objects
John W. Eaton <jwe@octave.org>
parents:
28628
diff
changeset
|
164 |
b6371946f106
new template for range objects
John W. Eaton <jwe@octave.org>
parents:
28628
diff
changeset
|
165 // If increment is 0, then numel should also be zero. |
b6371946f106
new template for range objects
John W. Eaton <jwe@octave.org>
parents:
28628
diff
changeset
|
166 |
b6371946f106
new template for range objects
John W. Eaton <jwe@octave.org>
parents:
28628
diff
changeset
|
167 retval = base + (nel - 1) * inc; |
b6371946f106
new template for range objects
John W. Eaton <jwe@octave.org>
parents:
28628
diff
changeset
|
168 |
b6371946f106
new template for range objects
John W. Eaton <jwe@octave.org>
parents:
28628
diff
changeset
|
169 // On some machines (x86 with extended precision floating point |
b6371946f106
new template for range objects
John W. Eaton <jwe@octave.org>
parents:
28628
diff
changeset
|
170 // arithmetic, for example) it is possible that we can overshoot |
b6371946f106
new template for range objects
John W. Eaton <jwe@octave.org>
parents:
28628
diff
changeset
|
171 // the limit by approximately the machine precision even though |
b6371946f106
new template for range objects
John W. Eaton <jwe@octave.org>
parents:
28628
diff
changeset
|
172 // we were very careful in our calculation of the number of |
b6371946f106
new template for range objects
John W. Eaton <jwe@octave.org>
parents:
28628
diff
changeset
|
173 // elements. Therefore, we clip the result to the limit if it |
b6371946f106
new template for range objects
John W. Eaton <jwe@octave.org>
parents:
28628
diff
changeset
|
174 // overshoots. |
b6371946f106
new template for range objects
John W. Eaton <jwe@octave.org>
parents:
28628
diff
changeset
|
175 |
b6371946f106
new template for range objects
John W. Eaton <jwe@octave.org>
parents:
28628
diff
changeset
|
176 // NOTE: The test also includes equality (>= limit) to have |
b6371946f106
new template for range objects
John W. Eaton <jwe@octave.org>
parents:
28628
diff
changeset
|
177 // expressions such as -5:1:-0 result in a -0 endpoint. |
b6371946f106
new template for range objects
John W. Eaton <jwe@octave.org>
parents:
28628
diff
changeset
|
178 |
b6371946f106
new template for range objects
John W. Eaton <jwe@octave.org>
parents:
28628
diff
changeset
|
179 if ((inc > T (0) && retval >= limit) || (inc < T (0) && retval <= limit)) |
b6371946f106
new template for range objects
John W. Eaton <jwe@octave.org>
parents:
28628
diff
changeset
|
180 retval = limit; |
b6371946f106
new template for range objects
John W. Eaton <jwe@octave.org>
parents:
28628
diff
changeset
|
181 |
b6371946f106
new template for range objects
John W. Eaton <jwe@octave.org>
parents:
28628
diff
changeset
|
182 // If all elements are integers, then ensure the final value is. |
29353
715344f405f0
improve handling of nan, infinite value, and empty ranges (bug #59229)
John W. Eaton <jwe@octave.org>
parents:
29352
diff
changeset
|
183 // Note that we pass the preliminary computed final value to |
715344f405f0
improve handling of nan, infinite value, and empty ranges (bug #59229)
John W. Eaton <jwe@octave.org>
parents:
29352
diff
changeset
|
184 // xall_elements_are_ints, but it only checks whether that value is |
715344f405f0
improve handling of nan, infinite value, and empty ranges (bug #59229)
John W. Eaton <jwe@octave.org>
parents:
29352
diff
changeset
|
185 // NaN. |
28635
b6371946f106
new template for range objects
John W. Eaton <jwe@octave.org>
parents:
28628
diff
changeset
|
186 |
29353
715344f405f0
improve handling of nan, infinite value, and empty ranges (bug #59229)
John W. Eaton <jwe@octave.org>
parents:
29352
diff
changeset
|
187 if (xall_elements_are_ints (base, inc, retval, nel)) |
28635
b6371946f106
new template for range objects
John W. Eaton <jwe@octave.org>
parents:
28628
diff
changeset
|
188 retval = std::round (retval); |
b6371946f106
new template for range objects
John W. Eaton <jwe@octave.org>
parents:
28628
diff
changeset
|
189 |
b6371946f106
new template for range objects
John W. Eaton <jwe@octave.org>
parents:
28628
diff
changeset
|
190 return retval; |
b6371946f106
new template for range objects
John W. Eaton <jwe@octave.org>
parents:
28628
diff
changeset
|
191 } |
b6371946f106
new template for range objects
John W. Eaton <jwe@octave.org>
parents:
28628
diff
changeset
|
192 |
29352
03c283f73b9a
specialize range<T>::init instead of defining get_numel and get_final_value
John W. Eaton <jwe@octave.org>
parents:
29351
diff
changeset
|
193 template <typename T> |
03c283f73b9a
specialize range<T>::init instead of defining get_numel and get_final_value
John W. Eaton <jwe@octave.org>
parents:
29351
diff
changeset
|
194 void |
30420
366aa563dd2e
style fixes for Range.h and Range.cc
John W. Eaton <jwe@octave.org>
parents:
30416
diff
changeset
|
195 xinit (T base, T limit, T inc, bool reverse, T& final_val, |
30416
4736bc8e9804
Allow descending ranges of all integer types (bug #61132).
Markus Mützel <markus.muetzel@gmx.de>
parents:
30407
diff
changeset
|
196 octave_idx_type& nel) |
29352
03c283f73b9a
specialize range<T>::init instead of defining get_numel and get_final_value
John W. Eaton <jwe@octave.org>
parents:
29351
diff
changeset
|
197 { |
29353
715344f405f0
improve handling of nan, infinite value, and empty ranges (bug #59229)
John W. Eaton <jwe@octave.org>
parents:
29352
diff
changeset
|
198 // Catch obvious NaN ranges. |
715344f405f0
improve handling of nan, infinite value, and empty ranges (bug #59229)
John W. Eaton <jwe@octave.org>
parents:
29352
diff
changeset
|
199 if (math::isnan (base) || math::isnan (limit) || math::isnan (inc)) |
715344f405f0
improve handling of nan, infinite value, and empty ranges (bug #59229)
John W. Eaton <jwe@octave.org>
parents:
29352
diff
changeset
|
200 { |
715344f405f0
improve handling of nan, infinite value, and empty ranges (bug #59229)
John W. Eaton <jwe@octave.org>
parents:
29352
diff
changeset
|
201 final_val = numeric_limits<T>::NaN (); |
715344f405f0
improve handling of nan, infinite value, and empty ranges (bug #59229)
John W. Eaton <jwe@octave.org>
parents:
29352
diff
changeset
|
202 nel = 1; |
715344f405f0
improve handling of nan, infinite value, and empty ranges (bug #59229)
John W. Eaton <jwe@octave.org>
parents:
29352
diff
changeset
|
203 return; |
715344f405f0
improve handling of nan, infinite value, and empty ranges (bug #59229)
John W. Eaton <jwe@octave.org>
parents:
29352
diff
changeset
|
204 } |
715344f405f0
improve handling of nan, infinite value, and empty ranges (bug #59229)
John W. Eaton <jwe@octave.org>
parents:
29352
diff
changeset
|
205 |
30416
4736bc8e9804
Allow descending ranges of all integer types (bug #61132).
Markus Mützel <markus.muetzel@gmx.de>
parents:
30407
diff
changeset
|
206 // Floating point numbers are always signed |
4736bc8e9804
Allow descending ranges of all integer types (bug #61132).
Markus Mützel <markus.muetzel@gmx.de>
parents:
30407
diff
changeset
|
207 if (reverse) |
4736bc8e9804
Allow descending ranges of all integer types (bug #61132).
Markus Mützel <markus.muetzel@gmx.de>
parents:
30407
diff
changeset
|
208 inc = -inc; |
4736bc8e9804
Allow descending ranges of all integer types (bug #61132).
Markus Mützel <markus.muetzel@gmx.de>
parents:
30407
diff
changeset
|
209 |
29353
715344f405f0
improve handling of nan, infinite value, and empty ranges (bug #59229)
John W. Eaton <jwe@octave.org>
parents:
29352
diff
changeset
|
210 // Catch empty ranges. |
715344f405f0
improve handling of nan, infinite value, and empty ranges (bug #59229)
John W. Eaton <jwe@octave.org>
parents:
29352
diff
changeset
|
211 if (inc == 0 |
715344f405f0
improve handling of nan, infinite value, and empty ranges (bug #59229)
John W. Eaton <jwe@octave.org>
parents:
29352
diff
changeset
|
212 || (limit < base && inc > 0) |
715344f405f0
improve handling of nan, infinite value, and empty ranges (bug #59229)
John W. Eaton <jwe@octave.org>
parents:
29352
diff
changeset
|
213 || (limit > base && inc < 0)) |
715344f405f0
improve handling of nan, infinite value, and empty ranges (bug #59229)
John W. Eaton <jwe@octave.org>
parents:
29352
diff
changeset
|
214 { |
715344f405f0
improve handling of nan, infinite value, and empty ranges (bug #59229)
John W. Eaton <jwe@octave.org>
parents:
29352
diff
changeset
|
215 nel = 0; |
715344f405f0
improve handling of nan, infinite value, and empty ranges (bug #59229)
John W. Eaton <jwe@octave.org>
parents:
29352
diff
changeset
|
216 return; |
715344f405f0
improve handling of nan, infinite value, and empty ranges (bug #59229)
John W. Eaton <jwe@octave.org>
parents:
29352
diff
changeset
|
217 } |
715344f405f0
improve handling of nan, infinite value, and empty ranges (bug #59229)
John W. Eaton <jwe@octave.org>
parents:
29352
diff
changeset
|
218 |
715344f405f0
improve handling of nan, infinite value, and empty ranges (bug #59229)
John W. Eaton <jwe@octave.org>
parents:
29352
diff
changeset
|
219 // The following case also catches Inf values for increment when |
715344f405f0
improve handling of nan, infinite value, and empty ranges (bug #59229)
John W. Eaton <jwe@octave.org>
parents:
29352
diff
changeset
|
220 // there will be only one element. |
715344f405f0
improve handling of nan, infinite value, and empty ranges (bug #59229)
John W. Eaton <jwe@octave.org>
parents:
29352
diff
changeset
|
221 |
715344f405f0
improve handling of nan, infinite value, and empty ranges (bug #59229)
John W. Eaton <jwe@octave.org>
parents:
29352
diff
changeset
|
222 if ((limit <= base && base + inc < limit) |
715344f405f0
improve handling of nan, infinite value, and empty ranges (bug #59229)
John W. Eaton <jwe@octave.org>
parents:
29352
diff
changeset
|
223 || (limit >= base && base + inc > limit)) |
715344f405f0
improve handling of nan, infinite value, and empty ranges (bug #59229)
John W. Eaton <jwe@octave.org>
parents:
29352
diff
changeset
|
224 { |
715344f405f0
improve handling of nan, infinite value, and empty ranges (bug #59229)
John W. Eaton <jwe@octave.org>
parents:
29352
diff
changeset
|
225 final_val = base; |
715344f405f0
improve handling of nan, infinite value, and empty ranges (bug #59229)
John W. Eaton <jwe@octave.org>
parents:
29352
diff
changeset
|
226 nel = 1; |
715344f405f0
improve handling of nan, infinite value, and empty ranges (bug #59229)
John W. Eaton <jwe@octave.org>
parents:
29352
diff
changeset
|
227 return; |
715344f405f0
improve handling of nan, infinite value, and empty ranges (bug #59229)
John W. Eaton <jwe@octave.org>
parents:
29352
diff
changeset
|
228 } |
715344f405f0
improve handling of nan, infinite value, and empty ranges (bug #59229)
John W. Eaton <jwe@octave.org>
parents:
29352
diff
changeset
|
229 |
715344f405f0
improve handling of nan, infinite value, and empty ranges (bug #59229)
John W. Eaton <jwe@octave.org>
parents:
29352
diff
changeset
|
230 // Any other calculations with Inf will give us either a NaN range |
715344f405f0
improve handling of nan, infinite value, and empty ranges (bug #59229)
John W. Eaton <jwe@octave.org>
parents:
29352
diff
changeset
|
231 // or an infinite nember of elements. |
715344f405f0
improve handling of nan, infinite value, and empty ranges (bug #59229)
John W. Eaton <jwe@octave.org>
parents:
29352
diff
changeset
|
232 |
715344f405f0
improve handling of nan, infinite value, and empty ranges (bug #59229)
John W. Eaton <jwe@octave.org>
parents:
29352
diff
changeset
|
233 T dnel = (limit - base) / inc; |
715344f405f0
improve handling of nan, infinite value, and empty ranges (bug #59229)
John W. Eaton <jwe@octave.org>
parents:
29352
diff
changeset
|
234 if (math::isnan (dnel)) |
715344f405f0
improve handling of nan, infinite value, and empty ranges (bug #59229)
John W. Eaton <jwe@octave.org>
parents:
29352
diff
changeset
|
235 { |
715344f405f0
improve handling of nan, infinite value, and empty ranges (bug #59229)
John W. Eaton <jwe@octave.org>
parents:
29352
diff
changeset
|
236 nel = 1; |
715344f405f0
improve handling of nan, infinite value, and empty ranges (bug #59229)
John W. Eaton <jwe@octave.org>
parents:
29352
diff
changeset
|
237 final_val = numeric_limits<T>::NaN (); |
715344f405f0
improve handling of nan, infinite value, and empty ranges (bug #59229)
John W. Eaton <jwe@octave.org>
parents:
29352
diff
changeset
|
238 return; |
715344f405f0
improve handling of nan, infinite value, and empty ranges (bug #59229)
John W. Eaton <jwe@octave.org>
parents:
29352
diff
changeset
|
239 } |
715344f405f0
improve handling of nan, infinite value, and empty ranges (bug #59229)
John W. Eaton <jwe@octave.org>
parents:
29352
diff
changeset
|
240 |
715344f405f0
improve handling of nan, infinite value, and empty ranges (bug #59229)
John W. Eaton <jwe@octave.org>
parents:
29352
diff
changeset
|
241 if (dnel > 0 && math::isinf (dnel)) |
715344f405f0
improve handling of nan, infinite value, and empty ranges (bug #59229)
John W. Eaton <jwe@octave.org>
parents:
29352
diff
changeset
|
242 { |
715344f405f0
improve handling of nan, infinite value, and empty ranges (bug #59229)
John W. Eaton <jwe@octave.org>
parents:
29352
diff
changeset
|
243 // FIXME: Should this be an immediate error? |
715344f405f0
improve handling of nan, infinite value, and empty ranges (bug #59229)
John W. Eaton <jwe@octave.org>
parents:
29352
diff
changeset
|
244 nel = std::numeric_limits<octave_idx_type>::max (); |
715344f405f0
improve handling of nan, infinite value, and empty ranges (bug #59229)
John W. Eaton <jwe@octave.org>
parents:
29352
diff
changeset
|
245 |
715344f405f0
improve handling of nan, infinite value, and empty ranges (bug #59229)
John W. Eaton <jwe@octave.org>
parents:
29352
diff
changeset
|
246 // FIXME: Will this do the right thing in all cases? |
715344f405f0
improve handling of nan, infinite value, and empty ranges (bug #59229)
John W. Eaton <jwe@octave.org>
parents:
29352
diff
changeset
|
247 final_val = xfinal_value (base, limit, inc, nel); |
715344f405f0
improve handling of nan, infinite value, and empty ranges (bug #59229)
John W. Eaton <jwe@octave.org>
parents:
29352
diff
changeset
|
248 |
715344f405f0
improve handling of nan, infinite value, and empty ranges (bug #59229)
John W. Eaton <jwe@octave.org>
parents:
29352
diff
changeset
|
249 return; |
715344f405f0
improve handling of nan, infinite value, and empty ranges (bug #59229)
John W. Eaton <jwe@octave.org>
parents:
29352
diff
changeset
|
250 } |
715344f405f0
improve handling of nan, infinite value, and empty ranges (bug #59229)
John W. Eaton <jwe@octave.org>
parents:
29352
diff
changeset
|
251 |
715344f405f0
improve handling of nan, infinite value, and empty ranges (bug #59229)
John W. Eaton <jwe@octave.org>
parents:
29352
diff
changeset
|
252 // Now that we have handled all the special cases, we can compute |
715344f405f0
improve handling of nan, infinite value, and empty ranges (bug #59229)
John W. Eaton <jwe@octave.org>
parents:
29352
diff
changeset
|
253 // the number of elements and the final value in a way that attempts |
715344f405f0
improve handling of nan, infinite value, and empty ranges (bug #59229)
John W. Eaton <jwe@octave.org>
parents:
29352
diff
changeset
|
254 // to avoid rounding errors as much as possible. |
715344f405f0
improve handling of nan, infinite value, and empty ranges (bug #59229)
John W. Eaton <jwe@octave.org>
parents:
29352
diff
changeset
|
255 |
29352
03c283f73b9a
specialize range<T>::init instead of defining get_numel and get_final_value
John W. Eaton <jwe@octave.org>
parents:
29351
diff
changeset
|
256 nel = xnumel_internal (base, limit, inc); |
03c283f73b9a
specialize range<T>::init instead of defining get_numel and get_final_value
John W. Eaton <jwe@octave.org>
parents:
29351
diff
changeset
|
257 final_val = xfinal_value (base, limit, inc, nel); |
03c283f73b9a
specialize range<T>::init instead of defining get_numel and get_final_value
John W. Eaton <jwe@octave.org>
parents:
29351
diff
changeset
|
258 } |
03c283f73b9a
specialize range<T>::init instead of defining get_numel and get_final_value
John W. Eaton <jwe@octave.org>
parents:
29351
diff
changeset
|
259 |
29353
715344f405f0
improve handling of nan, infinite value, and empty ranges (bug #59229)
John W. Eaton <jwe@octave.org>
parents:
29352
diff
changeset
|
260 template <typename T> |
30405
20cefb3b0da6
range: Use specialization of init function for octave_int<T> types (bug #61300).
Markus Mützel <markus.muetzel@gmx.de>
parents:
30394
diff
changeset
|
261 void |
30420
366aa563dd2e
style fixes for Range.h and Range.cc
John W. Eaton <jwe@octave.org>
parents:
30416
diff
changeset
|
262 xinit (const octave_int<T>& base, const octave_int<T>& limit, |
366aa563dd2e
style fixes for Range.h and Range.cc
John W. Eaton <jwe@octave.org>
parents:
30416
diff
changeset
|
263 const octave_int<T>& inc, bool reverse, |
30416
4736bc8e9804
Allow descending ranges of all integer types (bug #61132).
Markus Mützel <markus.muetzel@gmx.de>
parents:
30407
diff
changeset
|
264 octave_int<T>& final_val, octave_idx_type& nel) |
30405
20cefb3b0da6
range: Use specialization of init function for octave_int<T> types (bug #61300).
Markus Mützel <markus.muetzel@gmx.de>
parents:
30394
diff
changeset
|
265 { |
30407
597275db9c7f
don't return void; pass objects by const ref instead of value
John W. Eaton <jwe@octave.org>
parents:
30405
diff
changeset
|
266 // We need an integer division that is truncating decimals instead |
597275db9c7f
don't return void; pass objects by const ref instead of value
John W. Eaton <jwe@octave.org>
parents:
30405
diff
changeset
|
267 // of rounding. So, use underlying C++ types instead of |
597275db9c7f
don't return void; pass objects by const ref instead of value
John W. Eaton <jwe@octave.org>
parents:
30405
diff
changeset
|
268 // octave_int<T>. |
597275db9c7f
don't return void; pass objects by const ref instead of value
John W. Eaton <jwe@octave.org>
parents:
30405
diff
changeset
|
269 |
597275db9c7f
don't return void; pass objects by const ref instead of value
John W. Eaton <jwe@octave.org>
parents:
30405
diff
changeset
|
270 // FIXME: The numerator might underflow or overflow. Add checks for |
597275db9c7f
don't return void; pass objects by const ref instead of value
John W. Eaton <jwe@octave.org>
parents:
30405
diff
changeset
|
271 // that. |
30416
4736bc8e9804
Allow descending ranges of all integer types (bug #61132).
Markus Mützel <markus.muetzel@gmx.de>
parents:
30407
diff
changeset
|
272 if (reverse) |
4736bc8e9804
Allow descending ranges of all integer types (bug #61132).
Markus Mützel <markus.muetzel@gmx.de>
parents:
30407
diff
changeset
|
273 { |
4736bc8e9804
Allow descending ranges of all integer types (bug #61132).
Markus Mützel <markus.muetzel@gmx.de>
parents:
30407
diff
changeset
|
274 nel = ((inc == octave_int<T> (0) |
4736bc8e9804
Allow descending ranges of all integer types (bug #61132).
Markus Mützel <markus.muetzel@gmx.de>
parents:
30407
diff
changeset
|
275 || (limit > base && inc > octave_int<T> (0)) |
4736bc8e9804
Allow descending ranges of all integer types (bug #61132).
Markus Mützel <markus.muetzel@gmx.de>
parents:
30407
diff
changeset
|
276 || (limit < base && inc < octave_int<T> (0))) |
4736bc8e9804
Allow descending ranges of all integer types (bug #61132).
Markus Mützel <markus.muetzel@gmx.de>
parents:
30407
diff
changeset
|
277 ? 0 |
4736bc8e9804
Allow descending ranges of all integer types (bug #61132).
Markus Mützel <markus.muetzel@gmx.de>
parents:
30407
diff
changeset
|
278 : (base.value () - limit.value () + inc.value ()) |
4736bc8e9804
Allow descending ranges of all integer types (bug #61132).
Markus Mützel <markus.muetzel@gmx.de>
parents:
30407
diff
changeset
|
279 / inc.value ()); |
30405
20cefb3b0da6
range: Use specialization of init function for octave_int<T> types (bug #61300).
Markus Mützel <markus.muetzel@gmx.de>
parents:
30394
diff
changeset
|
280 |
30416
4736bc8e9804
Allow descending ranges of all integer types (bug #61132).
Markus Mützel <markus.muetzel@gmx.de>
parents:
30407
diff
changeset
|
281 final_val = base - (nel - 1) * inc; |
4736bc8e9804
Allow descending ranges of all integer types (bug #61132).
Markus Mützel <markus.muetzel@gmx.de>
parents:
30407
diff
changeset
|
282 } |
4736bc8e9804
Allow descending ranges of all integer types (bug #61132).
Markus Mützel <markus.muetzel@gmx.de>
parents:
30407
diff
changeset
|
283 else |
4736bc8e9804
Allow descending ranges of all integer types (bug #61132).
Markus Mützel <markus.muetzel@gmx.de>
parents:
30407
diff
changeset
|
284 { |
4736bc8e9804
Allow descending ranges of all integer types (bug #61132).
Markus Mützel <markus.muetzel@gmx.de>
parents:
30407
diff
changeset
|
285 nel = ((inc == octave_int<T> (0) |
4736bc8e9804
Allow descending ranges of all integer types (bug #61132).
Markus Mützel <markus.muetzel@gmx.de>
parents:
30407
diff
changeset
|
286 || (limit > base && inc < octave_int<T> (0)) |
4736bc8e9804
Allow descending ranges of all integer types (bug #61132).
Markus Mützel <markus.muetzel@gmx.de>
parents:
30407
diff
changeset
|
287 || (limit < base && inc > octave_int<T> (0))) |
4736bc8e9804
Allow descending ranges of all integer types (bug #61132).
Markus Mützel <markus.muetzel@gmx.de>
parents:
30407
diff
changeset
|
288 ? 0 |
4736bc8e9804
Allow descending ranges of all integer types (bug #61132).
Markus Mützel <markus.muetzel@gmx.de>
parents:
30407
diff
changeset
|
289 : (limit.value () - base.value () + inc.value ()) |
4736bc8e9804
Allow descending ranges of all integer types (bug #61132).
Markus Mützel <markus.muetzel@gmx.de>
parents:
30407
diff
changeset
|
290 / inc.value ()); |
30407
597275db9c7f
don't return void; pass objects by const ref instead of value
John W. Eaton <jwe@octave.org>
parents:
30405
diff
changeset
|
291 |
30416
4736bc8e9804
Allow descending ranges of all integer types (bug #61132).
Markus Mützel <markus.muetzel@gmx.de>
parents:
30407
diff
changeset
|
292 final_val = base + (nel - 1) * inc; |
4736bc8e9804
Allow descending ranges of all integer types (bug #61132).
Markus Mützel <markus.muetzel@gmx.de>
parents:
30407
diff
changeset
|
293 } |
30407
597275db9c7f
don't return void; pass objects by const ref instead of value
John W. Eaton <jwe@octave.org>
parents:
30405
diff
changeset
|
294 } |
30405
20cefb3b0da6
range: Use specialization of init function for octave_int<T> types (bug #61300).
Markus Mützel <markus.muetzel@gmx.de>
parents:
30394
diff
changeset
|
295 |
20cefb3b0da6
range: Use specialization of init function for octave_int<T> types (bug #61300).
Markus Mützel <markus.muetzel@gmx.de>
parents:
30394
diff
changeset
|
296 template <typename T> |
29353
715344f405f0
improve handling of nan, infinite value, and empty ranges (bug #59229)
John W. Eaton <jwe@octave.org>
parents:
29352
diff
changeset
|
297 bool |
715344f405f0
improve handling of nan, infinite value, and empty ranges (bug #59229)
John W. Eaton <jwe@octave.org>
parents:
29352
diff
changeset
|
298 xis_storable (T base, T limit, octave_idx_type nel) |
715344f405f0
improve handling of nan, infinite value, and empty ranges (bug #59229)
John W. Eaton <jwe@octave.org>
parents:
29352
diff
changeset
|
299 { |
715344f405f0
improve handling of nan, infinite value, and empty ranges (bug #59229)
John W. Eaton <jwe@octave.org>
parents:
29352
diff
changeset
|
300 return ! (nel > 1 && (math::isinf (base) || math::isinf (limit))); |
715344f405f0
improve handling of nan, infinite value, and empty ranges (bug #59229)
John W. Eaton <jwe@octave.org>
parents:
29352
diff
changeset
|
301 } |
715344f405f0
improve handling of nan, infinite value, and empty ranges (bug #59229)
John W. Eaton <jwe@octave.org>
parents:
29352
diff
changeset
|
302 |
28635
b6371946f106
new template for range objects
John W. Eaton <jwe@octave.org>
parents:
28628
diff
changeset
|
303 template <> |
b6371946f106
new template for range objects
John W. Eaton <jwe@octave.org>
parents:
28628
diff
changeset
|
304 bool |
b6371946f106
new template for range objects
John W. Eaton <jwe@octave.org>
parents:
28628
diff
changeset
|
305 range<double>::all_elements_are_ints (void) const |
b6371946f106
new template for range objects
John W. Eaton <jwe@octave.org>
parents:
28628
diff
changeset
|
306 { |
29353
715344f405f0
improve handling of nan, infinite value, and empty ranges (bug #59229)
John W. Eaton <jwe@octave.org>
parents:
29352
diff
changeset
|
307 return xall_elements_are_ints (m_base, m_increment, m_final, m_numel); |
28635
b6371946f106
new template for range objects
John W. Eaton <jwe@octave.org>
parents:
28628
diff
changeset
|
308 } |
b6371946f106
new template for range objects
John W. Eaton <jwe@octave.org>
parents:
28628
diff
changeset
|
309 |
b6371946f106
new template for range objects
John W. Eaton <jwe@octave.org>
parents:
28628
diff
changeset
|
310 template <> |
b6371946f106
new template for range objects
John W. Eaton <jwe@octave.org>
parents:
28628
diff
changeset
|
311 bool |
b6371946f106
new template for range objects
John W. Eaton <jwe@octave.org>
parents:
28628
diff
changeset
|
312 range<float>::all_elements_are_ints (void) const |
b6371946f106
new template for range objects
John W. Eaton <jwe@octave.org>
parents:
28628
diff
changeset
|
313 { |
29353
715344f405f0
improve handling of nan, infinite value, and empty ranges (bug #59229)
John W. Eaton <jwe@octave.org>
parents:
29352
diff
changeset
|
314 return xall_elements_are_ints (m_base, m_increment, m_final, m_numel); |
28635
b6371946f106
new template for range objects
John W. Eaton <jwe@octave.org>
parents:
28628
diff
changeset
|
315 } |
b6371946f106
new template for range objects
John W. Eaton <jwe@octave.org>
parents:
28628
diff
changeset
|
316 |
b6371946f106
new template for range objects
John W. Eaton <jwe@octave.org>
parents:
28628
diff
changeset
|
317 template <> |
29352
03c283f73b9a
specialize range<T>::init instead of defining get_numel and get_final_value
John W. Eaton <jwe@octave.org>
parents:
29351
diff
changeset
|
318 void |
03c283f73b9a
specialize range<T>::init instead of defining get_numel and get_final_value
John W. Eaton <jwe@octave.org>
parents:
29351
diff
changeset
|
319 range<double>::init (void) |
28635
b6371946f106
new template for range objects
John W. Eaton <jwe@octave.org>
parents:
28628
diff
changeset
|
320 { |
30416
4736bc8e9804
Allow descending ranges of all integer types (bug #61132).
Markus Mützel <markus.muetzel@gmx.de>
parents:
30407
diff
changeset
|
321 xinit (m_base, m_limit, m_increment, m_reverse, m_final, m_numel); |
28635
b6371946f106
new template for range objects
John W. Eaton <jwe@octave.org>
parents:
28628
diff
changeset
|
322 } |
b6371946f106
new template for range objects
John W. Eaton <jwe@octave.org>
parents:
28628
diff
changeset
|
323 |
b6371946f106
new template for range objects
John W. Eaton <jwe@octave.org>
parents:
28628
diff
changeset
|
324 template <> |
29352
03c283f73b9a
specialize range<T>::init instead of defining get_numel and get_final_value
John W. Eaton <jwe@octave.org>
parents:
29351
diff
changeset
|
325 void |
03c283f73b9a
specialize range<T>::init instead of defining get_numel and get_final_value
John W. Eaton <jwe@octave.org>
parents:
29351
diff
changeset
|
326 range<float>::init (void) |
28635
b6371946f106
new template for range objects
John W. Eaton <jwe@octave.org>
parents:
28628
diff
changeset
|
327 { |
30416
4736bc8e9804
Allow descending ranges of all integer types (bug #61132).
Markus Mützel <markus.muetzel@gmx.de>
parents:
30407
diff
changeset
|
328 xinit (m_base, m_limit, m_increment, m_reverse, m_final, m_numel); |
28635
b6371946f106
new template for range objects
John W. Eaton <jwe@octave.org>
parents:
28628
diff
changeset
|
329 } |
b6371946f106
new template for range objects
John W. Eaton <jwe@octave.org>
parents:
28628
diff
changeset
|
330 |
30835
2989202f92f8
only allow range<FLOAT> to be instantiated
John W. Eaton <jwe@octave.org>
parents:
30564
diff
changeset
|
331 // For now, only define for float and double. |
2989202f92f8
only allow range<FLOAT> to be instantiated
John W. Eaton <jwe@octave.org>
parents:
30564
diff
changeset
|
332 |
2989202f92f8
only allow range<FLOAT> to be instantiated
John W. Eaton <jwe@octave.org>
parents:
30564
diff
changeset
|
333 #if 0 |
2989202f92f8
only allow range<FLOAT> to be instantiated
John W. Eaton <jwe@octave.org>
parents:
30564
diff
changeset
|
334 |
29353
715344f405f0
improve handling of nan, infinite value, and empty ranges (bug #59229)
John W. Eaton <jwe@octave.org>
parents:
29352
diff
changeset
|
335 template <> |
30405
20cefb3b0da6
range: Use specialization of init function for octave_int<T> types (bug #61300).
Markus Mützel <markus.muetzel@gmx.de>
parents:
30394
diff
changeset
|
336 void |
20cefb3b0da6
range: Use specialization of init function for octave_int<T> types (bug #61300).
Markus Mützel <markus.muetzel@gmx.de>
parents:
30394
diff
changeset
|
337 range<octave_int8>::init (void) |
20cefb3b0da6
range: Use specialization of init function for octave_int<T> types (bug #61300).
Markus Mützel <markus.muetzel@gmx.de>
parents:
30394
diff
changeset
|
338 { |
30416
4736bc8e9804
Allow descending ranges of all integer types (bug #61132).
Markus Mützel <markus.muetzel@gmx.de>
parents:
30407
diff
changeset
|
339 xinit (m_base, m_limit, m_increment, m_reverse, m_final, m_numel); |
30405
20cefb3b0da6
range: Use specialization of init function for octave_int<T> types (bug #61300).
Markus Mützel <markus.muetzel@gmx.de>
parents:
30394
diff
changeset
|
340 } |
20cefb3b0da6
range: Use specialization of init function for octave_int<T> types (bug #61300).
Markus Mützel <markus.muetzel@gmx.de>
parents:
30394
diff
changeset
|
341 |
20cefb3b0da6
range: Use specialization of init function for octave_int<T> types (bug #61300).
Markus Mützel <markus.muetzel@gmx.de>
parents:
30394
diff
changeset
|
342 template <> |
20cefb3b0da6
range: Use specialization of init function for octave_int<T> types (bug #61300).
Markus Mützel <markus.muetzel@gmx.de>
parents:
30394
diff
changeset
|
343 void |
20cefb3b0da6
range: Use specialization of init function for octave_int<T> types (bug #61300).
Markus Mützel <markus.muetzel@gmx.de>
parents:
30394
diff
changeset
|
344 range<octave_int16>::init (void) |
20cefb3b0da6
range: Use specialization of init function for octave_int<T> types (bug #61300).
Markus Mützel <markus.muetzel@gmx.de>
parents:
30394
diff
changeset
|
345 { |
30416
4736bc8e9804
Allow descending ranges of all integer types (bug #61132).
Markus Mützel <markus.muetzel@gmx.de>
parents:
30407
diff
changeset
|
346 xinit (m_base, m_limit, m_increment, m_reverse, m_final, m_numel); |
30405
20cefb3b0da6
range: Use specialization of init function for octave_int<T> types (bug #61300).
Markus Mützel <markus.muetzel@gmx.de>
parents:
30394
diff
changeset
|
347 } |
20cefb3b0da6
range: Use specialization of init function for octave_int<T> types (bug #61300).
Markus Mützel <markus.muetzel@gmx.de>
parents:
30394
diff
changeset
|
348 |
20cefb3b0da6
range: Use specialization of init function for octave_int<T> types (bug #61300).
Markus Mützel <markus.muetzel@gmx.de>
parents:
30394
diff
changeset
|
349 template <> |
20cefb3b0da6
range: Use specialization of init function for octave_int<T> types (bug #61300).
Markus Mützel <markus.muetzel@gmx.de>
parents:
30394
diff
changeset
|
350 void |
20cefb3b0da6
range: Use specialization of init function for octave_int<T> types (bug #61300).
Markus Mützel <markus.muetzel@gmx.de>
parents:
30394
diff
changeset
|
351 range<octave_int32>::init (void) |
20cefb3b0da6
range: Use specialization of init function for octave_int<T> types (bug #61300).
Markus Mützel <markus.muetzel@gmx.de>
parents:
30394
diff
changeset
|
352 { |
30416
4736bc8e9804
Allow descending ranges of all integer types (bug #61132).
Markus Mützel <markus.muetzel@gmx.de>
parents:
30407
diff
changeset
|
353 xinit (m_base, m_limit, m_increment, m_reverse, m_final, m_numel); |
30405
20cefb3b0da6
range: Use specialization of init function for octave_int<T> types (bug #61300).
Markus Mützel <markus.muetzel@gmx.de>
parents:
30394
diff
changeset
|
354 } |
20cefb3b0da6
range: Use specialization of init function for octave_int<T> types (bug #61300).
Markus Mützel <markus.muetzel@gmx.de>
parents:
30394
diff
changeset
|
355 |
20cefb3b0da6
range: Use specialization of init function for octave_int<T> types (bug #61300).
Markus Mützel <markus.muetzel@gmx.de>
parents:
30394
diff
changeset
|
356 template <> |
20cefb3b0da6
range: Use specialization of init function for octave_int<T> types (bug #61300).
Markus Mützel <markus.muetzel@gmx.de>
parents:
30394
diff
changeset
|
357 void |
20cefb3b0da6
range: Use specialization of init function for octave_int<T> types (bug #61300).
Markus Mützel <markus.muetzel@gmx.de>
parents:
30394
diff
changeset
|
358 range<octave_int64>::init (void) |
20cefb3b0da6
range: Use specialization of init function for octave_int<T> types (bug #61300).
Markus Mützel <markus.muetzel@gmx.de>
parents:
30394
diff
changeset
|
359 { |
30416
4736bc8e9804
Allow descending ranges of all integer types (bug #61132).
Markus Mützel <markus.muetzel@gmx.de>
parents:
30407
diff
changeset
|
360 xinit (m_base, m_limit, m_increment, m_reverse, m_final, m_numel); |
30405
20cefb3b0da6
range: Use specialization of init function for octave_int<T> types (bug #61300).
Markus Mützel <markus.muetzel@gmx.de>
parents:
30394
diff
changeset
|
361 } |
20cefb3b0da6
range: Use specialization of init function for octave_int<T> types (bug #61300).
Markus Mützel <markus.muetzel@gmx.de>
parents:
30394
diff
changeset
|
362 |
20cefb3b0da6
range: Use specialization of init function for octave_int<T> types (bug #61300).
Markus Mützel <markus.muetzel@gmx.de>
parents:
30394
diff
changeset
|
363 template <> |
20cefb3b0da6
range: Use specialization of init function for octave_int<T> types (bug #61300).
Markus Mützel <markus.muetzel@gmx.de>
parents:
30394
diff
changeset
|
364 void |
20cefb3b0da6
range: Use specialization of init function for octave_int<T> types (bug #61300).
Markus Mützel <markus.muetzel@gmx.de>
parents:
30394
diff
changeset
|
365 range<octave_uint8>::init (void) |
20cefb3b0da6
range: Use specialization of init function for octave_int<T> types (bug #61300).
Markus Mützel <markus.muetzel@gmx.de>
parents:
30394
diff
changeset
|
366 { |
30416
4736bc8e9804
Allow descending ranges of all integer types (bug #61132).
Markus Mützel <markus.muetzel@gmx.de>
parents:
30407
diff
changeset
|
367 xinit (m_base, m_limit, m_increment, m_reverse, m_final, m_numel); |
30405
20cefb3b0da6
range: Use specialization of init function for octave_int<T> types (bug #61300).
Markus Mützel <markus.muetzel@gmx.de>
parents:
30394
diff
changeset
|
368 } |
20cefb3b0da6
range: Use specialization of init function for octave_int<T> types (bug #61300).
Markus Mützel <markus.muetzel@gmx.de>
parents:
30394
diff
changeset
|
369 |
20cefb3b0da6
range: Use specialization of init function for octave_int<T> types (bug #61300).
Markus Mützel <markus.muetzel@gmx.de>
parents:
30394
diff
changeset
|
370 template <> |
20cefb3b0da6
range: Use specialization of init function for octave_int<T> types (bug #61300).
Markus Mützel <markus.muetzel@gmx.de>
parents:
30394
diff
changeset
|
371 void |
20cefb3b0da6
range: Use specialization of init function for octave_int<T> types (bug #61300).
Markus Mützel <markus.muetzel@gmx.de>
parents:
30394
diff
changeset
|
372 range<octave_uint16>::init (void) |
20cefb3b0da6
range: Use specialization of init function for octave_int<T> types (bug #61300).
Markus Mützel <markus.muetzel@gmx.de>
parents:
30394
diff
changeset
|
373 { |
30416
4736bc8e9804
Allow descending ranges of all integer types (bug #61132).
Markus Mützel <markus.muetzel@gmx.de>
parents:
30407
diff
changeset
|
374 xinit (m_base, m_limit, m_increment, m_reverse, m_final, m_numel); |
30405
20cefb3b0da6
range: Use specialization of init function for octave_int<T> types (bug #61300).
Markus Mützel <markus.muetzel@gmx.de>
parents:
30394
diff
changeset
|
375 } |
20cefb3b0da6
range: Use specialization of init function for octave_int<T> types (bug #61300).
Markus Mützel <markus.muetzel@gmx.de>
parents:
30394
diff
changeset
|
376 |
20cefb3b0da6
range: Use specialization of init function for octave_int<T> types (bug #61300).
Markus Mützel <markus.muetzel@gmx.de>
parents:
30394
diff
changeset
|
377 template <> |
20cefb3b0da6
range: Use specialization of init function for octave_int<T> types (bug #61300).
Markus Mützel <markus.muetzel@gmx.de>
parents:
30394
diff
changeset
|
378 void |
20cefb3b0da6
range: Use specialization of init function for octave_int<T> types (bug #61300).
Markus Mützel <markus.muetzel@gmx.de>
parents:
30394
diff
changeset
|
379 range<octave_uint32>::init (void) |
20cefb3b0da6
range: Use specialization of init function for octave_int<T> types (bug #61300).
Markus Mützel <markus.muetzel@gmx.de>
parents:
30394
diff
changeset
|
380 { |
30416
4736bc8e9804
Allow descending ranges of all integer types (bug #61132).
Markus Mützel <markus.muetzel@gmx.de>
parents:
30407
diff
changeset
|
381 xinit (m_base, m_limit, m_increment, m_reverse, m_final, m_numel); |
30405
20cefb3b0da6
range: Use specialization of init function for octave_int<T> types (bug #61300).
Markus Mützel <markus.muetzel@gmx.de>
parents:
30394
diff
changeset
|
382 } |
20cefb3b0da6
range: Use specialization of init function for octave_int<T> types (bug #61300).
Markus Mützel <markus.muetzel@gmx.de>
parents:
30394
diff
changeset
|
383 |
20cefb3b0da6
range: Use specialization of init function for octave_int<T> types (bug #61300).
Markus Mützel <markus.muetzel@gmx.de>
parents:
30394
diff
changeset
|
384 template <> |
20cefb3b0da6
range: Use specialization of init function for octave_int<T> types (bug #61300).
Markus Mützel <markus.muetzel@gmx.de>
parents:
30394
diff
changeset
|
385 void |
20cefb3b0da6
range: Use specialization of init function for octave_int<T> types (bug #61300).
Markus Mützel <markus.muetzel@gmx.de>
parents:
30394
diff
changeset
|
386 range<octave_uint64>::init (void) |
20cefb3b0da6
range: Use specialization of init function for octave_int<T> types (bug #61300).
Markus Mützel <markus.muetzel@gmx.de>
parents:
30394
diff
changeset
|
387 { |
30416
4736bc8e9804
Allow descending ranges of all integer types (bug #61132).
Markus Mützel <markus.muetzel@gmx.de>
parents:
30407
diff
changeset
|
388 xinit (m_base, m_limit, m_increment, m_reverse, m_final, m_numel); |
30405
20cefb3b0da6
range: Use specialization of init function for octave_int<T> types (bug #61300).
Markus Mützel <markus.muetzel@gmx.de>
parents:
30394
diff
changeset
|
389 } |
20cefb3b0da6
range: Use specialization of init function for octave_int<T> types (bug #61300).
Markus Mützel <markus.muetzel@gmx.de>
parents:
30394
diff
changeset
|
390 |
30835
2989202f92f8
only allow range<FLOAT> to be instantiated
John W. Eaton <jwe@octave.org>
parents:
30564
diff
changeset
|
391 #endif |
2989202f92f8
only allow range<FLOAT> to be instantiated
John W. Eaton <jwe@octave.org>
parents:
30564
diff
changeset
|
392 |
30405
20cefb3b0da6
range: Use specialization of init function for octave_int<T> types (bug #61300).
Markus Mützel <markus.muetzel@gmx.de>
parents:
30394
diff
changeset
|
393 template <> |
29353
715344f405f0
improve handling of nan, infinite value, and empty ranges (bug #59229)
John W. Eaton <jwe@octave.org>
parents:
29352
diff
changeset
|
394 bool |
715344f405f0
improve handling of nan, infinite value, and empty ranges (bug #59229)
John W. Eaton <jwe@octave.org>
parents:
29352
diff
changeset
|
395 range<double>::is_storable (void) const |
715344f405f0
improve handling of nan, infinite value, and empty ranges (bug #59229)
John W. Eaton <jwe@octave.org>
parents:
29352
diff
changeset
|
396 { |
715344f405f0
improve handling of nan, infinite value, and empty ranges (bug #59229)
John W. Eaton <jwe@octave.org>
parents:
29352
diff
changeset
|
397 return xis_storable (m_base, m_limit, m_numel); |
715344f405f0
improve handling of nan, infinite value, and empty ranges (bug #59229)
John W. Eaton <jwe@octave.org>
parents:
29352
diff
changeset
|
398 } |
715344f405f0
improve handling of nan, infinite value, and empty ranges (bug #59229)
John W. Eaton <jwe@octave.org>
parents:
29352
diff
changeset
|
399 |
715344f405f0
improve handling of nan, infinite value, and empty ranges (bug #59229)
John W. Eaton <jwe@octave.org>
parents:
29352
diff
changeset
|
400 template <> |
715344f405f0
improve handling of nan, infinite value, and empty ranges (bug #59229)
John W. Eaton <jwe@octave.org>
parents:
29352
diff
changeset
|
401 bool |
715344f405f0
improve handling of nan, infinite value, and empty ranges (bug #59229)
John W. Eaton <jwe@octave.org>
parents:
29352
diff
changeset
|
402 range<float>::is_storable (void) const |
715344f405f0
improve handling of nan, infinite value, and empty ranges (bug #59229)
John W. Eaton <jwe@octave.org>
parents:
29352
diff
changeset
|
403 { |
715344f405f0
improve handling of nan, infinite value, and empty ranges (bug #59229)
John W. Eaton <jwe@octave.org>
parents:
29352
diff
changeset
|
404 return xis_storable (m_base, m_limit, m_numel); |
715344f405f0
improve handling of nan, infinite value, and empty ranges (bug #59229)
John W. Eaton <jwe@octave.org>
parents:
29352
diff
changeset
|
405 } |
715344f405f0
improve handling of nan, infinite value, and empty ranges (bug #59229)
John W. Eaton <jwe@octave.org>
parents:
29352
diff
changeset
|
406 |
29351
326a7ca62c43
also define range<float>::nnz specialization
John W. Eaton <jwe@octave.org>
parents:
28857
diff
changeset
|
407 template <typename T> |
326a7ca62c43
also define range<float>::nnz specialization
John W. Eaton <jwe@octave.org>
parents:
28857
diff
changeset
|
408 octave_idx_type |
326a7ca62c43
also define range<float>::nnz specialization
John W. Eaton <jwe@octave.org>
parents:
28857
diff
changeset
|
409 xnnz (T base, T limit, T inc, T final_val, octave_idx_type nel) |
326a7ca62c43
also define range<float>::nnz specialization
John W. Eaton <jwe@octave.org>
parents:
28857
diff
changeset
|
410 { |
326a7ca62c43
also define range<float>::nnz specialization
John W. Eaton <jwe@octave.org>
parents:
28857
diff
changeset
|
411 // Note that the order of the following checks matters. |
326a7ca62c43
also define range<float>::nnz specialization
John W. Eaton <jwe@octave.org>
parents:
28857
diff
changeset
|
412 |
326a7ca62c43
also define range<float>::nnz specialization
John W. Eaton <jwe@octave.org>
parents:
28857
diff
changeset
|
413 // If there are no elements, there can be no non-zero elements. |
326a7ca62c43
also define range<float>::nnz specialization
John W. Eaton <jwe@octave.org>
parents:
28857
diff
changeset
|
414 if (nel == 0) |
326a7ca62c43
also define range<float>::nnz specialization
John W. Eaton <jwe@octave.org>
parents:
28857
diff
changeset
|
415 return 0; |
326a7ca62c43
also define range<float>::nnz specialization
John W. Eaton <jwe@octave.org>
parents:
28857
diff
changeset
|
416 |
326a7ca62c43
also define range<float>::nnz specialization
John W. Eaton <jwe@octave.org>
parents:
28857
diff
changeset
|
417 // All elements have the same sign, hence there are no zeros. |
326a7ca62c43
also define range<float>::nnz specialization
John W. Eaton <jwe@octave.org>
parents:
28857
diff
changeset
|
418 if ((base > 0 && limit > 0) || (base < 0 && limit < 0)) |
326a7ca62c43
also define range<float>::nnz specialization
John W. Eaton <jwe@octave.org>
parents:
28857
diff
changeset
|
419 return nel; |
326a7ca62c43
also define range<float>::nnz specialization
John W. Eaton <jwe@octave.org>
parents:
28857
diff
changeset
|
420 |
326a7ca62c43
also define range<float>::nnz specialization
John W. Eaton <jwe@octave.org>
parents:
28857
diff
changeset
|
421 // All elements are equal (inc = 0) but we know from the previous |
326a7ca62c43
also define range<float>::nnz specialization
John W. Eaton <jwe@octave.org>
parents:
28857
diff
changeset
|
422 // condition that they are not positive or negative, therefore all |
326a7ca62c43
also define range<float>::nnz specialization
John W. Eaton <jwe@octave.org>
parents:
28857
diff
changeset
|
423 // elements are zero. |
326a7ca62c43
also define range<float>::nnz specialization
John W. Eaton <jwe@octave.org>
parents:
28857
diff
changeset
|
424 if (inc == 0) |
326a7ca62c43
also define range<float>::nnz specialization
John W. Eaton <jwe@octave.org>
parents:
28857
diff
changeset
|
425 return 0; |
326a7ca62c43
also define range<float>::nnz specialization
John W. Eaton <jwe@octave.org>
parents:
28857
diff
changeset
|
426 |
326a7ca62c43
also define range<float>::nnz specialization
John W. Eaton <jwe@octave.org>
parents:
28857
diff
changeset
|
427 // Exactly one zero at beginning or end of range. |
326a7ca62c43
also define range<float>::nnz specialization
John W. Eaton <jwe@octave.org>
parents:
28857
diff
changeset
|
428 if (base == 0 || final_val == 0) |
326a7ca62c43
also define range<float>::nnz specialization
John W. Eaton <jwe@octave.org>
parents:
28857
diff
changeset
|
429 return nel - 1; |
326a7ca62c43
also define range<float>::nnz specialization
John W. Eaton <jwe@octave.org>
parents:
28857
diff
changeset
|
430 |
326a7ca62c43
also define range<float>::nnz specialization
John W. Eaton <jwe@octave.org>
parents:
28857
diff
changeset
|
431 // Range crosses negative/positive without hitting zero. |
326a7ca62c43
also define range<float>::nnz specialization
John W. Eaton <jwe@octave.org>
parents:
28857
diff
changeset
|
432 // FIXME: Is this test sufficiently tolerant or do we need to be |
326a7ca62c43
also define range<float>::nnz specialization
John W. Eaton <jwe@octave.org>
parents:
28857
diff
changeset
|
433 // more careful? |
326a7ca62c43
also define range<float>::nnz specialization
John W. Eaton <jwe@octave.org>
parents:
28857
diff
changeset
|
434 if (math::mod (-base, inc) != 0) |
326a7ca62c43
also define range<float>::nnz specialization
John W. Eaton <jwe@octave.org>
parents:
28857
diff
changeset
|
435 return nel; |
326a7ca62c43
also define range<float>::nnz specialization
John W. Eaton <jwe@octave.org>
parents:
28857
diff
changeset
|
436 |
326a7ca62c43
also define range<float>::nnz specialization
John W. Eaton <jwe@octave.org>
parents:
28857
diff
changeset
|
437 // Range crosses negative/positive and hits zero. |
326a7ca62c43
also define range<float>::nnz specialization
John W. Eaton <jwe@octave.org>
parents:
28857
diff
changeset
|
438 return nel - 1; |
326a7ca62c43
also define range<float>::nnz specialization
John W. Eaton <jwe@octave.org>
parents:
28857
diff
changeset
|
439 } |
326a7ca62c43
also define range<float>::nnz specialization
John W. Eaton <jwe@octave.org>
parents:
28857
diff
changeset
|
440 |
28635
b6371946f106
new template for range objects
John W. Eaton <jwe@octave.org>
parents:
28628
diff
changeset
|
441 template <> |
b6371946f106
new template for range objects
John W. Eaton <jwe@octave.org>
parents:
28628
diff
changeset
|
442 octave_idx_type |
b6371946f106
new template for range objects
John W. Eaton <jwe@octave.org>
parents:
28628
diff
changeset
|
443 range<double>::nnz (void) const |
b6371946f106
new template for range objects
John W. Eaton <jwe@octave.org>
parents:
28628
diff
changeset
|
444 { |
29351
326a7ca62c43
also define range<float>::nnz specialization
John W. Eaton <jwe@octave.org>
parents:
28857
diff
changeset
|
445 return xnnz (m_base, m_limit, m_increment, m_final, m_numel); |
326a7ca62c43
also define range<float>::nnz specialization
John W. Eaton <jwe@octave.org>
parents:
28857
diff
changeset
|
446 } |
28635
b6371946f106
new template for range objects
John W. Eaton <jwe@octave.org>
parents:
28628
diff
changeset
|
447 |
29351
326a7ca62c43
also define range<float>::nnz specialization
John W. Eaton <jwe@octave.org>
parents:
28857
diff
changeset
|
448 template <> |
326a7ca62c43
also define range<float>::nnz specialization
John W. Eaton <jwe@octave.org>
parents:
28857
diff
changeset
|
449 octave_idx_type |
326a7ca62c43
also define range<float>::nnz specialization
John W. Eaton <jwe@octave.org>
parents:
28857
diff
changeset
|
450 range<float>::nnz (void) const |
326a7ca62c43
also define range<float>::nnz specialization
John W. Eaton <jwe@octave.org>
parents:
28857
diff
changeset
|
451 { |
326a7ca62c43
also define range<float>::nnz specialization
John W. Eaton <jwe@octave.org>
parents:
28857
diff
changeset
|
452 return xnnz (m_base, m_limit, m_increment, m_final, m_numel); |
28635
b6371946f106
new template for range objects
John W. Eaton <jwe@octave.org>
parents:
28628
diff
changeset
|
453 } |
31605
e88a07dec498
maint: Use macros to begin/end C++ namespaces.
Rik <rik@octave.org>
parents:
30835
diff
changeset
|
454 |
e88a07dec498
maint: Use macros to begin/end C++ namespaces.
Rik <rik@octave.org>
parents:
30835
diff
changeset
|
455 OCTAVE_END_NAMESPACE(octave) |
28635
b6371946f106
new template for range objects
John W. Eaton <jwe@octave.org>
parents:
28628
diff
changeset
|
456 |
2383 | 457 bool |
458 Range::all_elements_are_ints (void) const | |
459 { | |
25812
fc74c8d2a584
Clean up liboctave Range implementation.
Rik <rik@octave.org>
parents:
25808
diff
changeset
|
460 // If the base and increment are ints, the final value in the range will also |
fc74c8d2a584
Clean up liboctave Range implementation.
Rik <rik@octave.org>
parents:
25808
diff
changeset
|
461 // be an integer, even if the limit is not. If there is one or fewer |
fc74c8d2a584
Clean up liboctave Range implementation.
Rik <rik@octave.org>
parents:
25808
diff
changeset
|
462 // elements only the base needs to be an integer. |
2383 | 463 |
28402
09a3c0e91e6e
style update for Range class
John W. Eaton <jwe@octave.org>
parents:
28004
diff
changeset
|
464 return (! (octave::math::isnan (m_base) || octave::math::isnan (m_inc)) |
09a3c0e91e6e
style update for Range class
John W. Eaton <jwe@octave.org>
parents:
28004
diff
changeset
|
465 && (octave::math::nint_big (m_base) == m_base || m_numel < 1) |
09a3c0e91e6e
style update for Range class
John W. Eaton <jwe@octave.org>
parents:
28004
diff
changeset
|
466 && (octave::math::nint_big (m_inc) == m_inc || m_numel <= 1)); |
2383 | 467 } |
645 | 468 |
24859
00ecff875f8a
Add nnz implementation for Range type (bug #53185).
maorshutman <maorus12@gmail.com>
parents:
24799
diff
changeset
|
469 octave_idx_type |
00ecff875f8a
Add nnz implementation for Range type (bug #53185).
maorshutman <maorus12@gmail.com>
parents:
24799
diff
changeset
|
470 Range::nnz (void) const |
00ecff875f8a
Add nnz implementation for Range type (bug #53185).
maorshutman <maorus12@gmail.com>
parents:
24799
diff
changeset
|
471 { |
00ecff875f8a
Add nnz implementation for Range type (bug #53185).
maorshutman <maorus12@gmail.com>
parents:
24799
diff
changeset
|
472 octave_idx_type retval = 0; |
00ecff875f8a
Add nnz implementation for Range type (bug #53185).
maorshutman <maorus12@gmail.com>
parents:
24799
diff
changeset
|
473 |
00ecff875f8a
Add nnz implementation for Range type (bug #53185).
maorshutman <maorus12@gmail.com>
parents:
24799
diff
changeset
|
474 if (! isempty ()) |
00ecff875f8a
Add nnz implementation for Range type (bug #53185).
maorshutman <maorus12@gmail.com>
parents:
24799
diff
changeset
|
475 { |
28402
09a3c0e91e6e
style update for Range class
John W. Eaton <jwe@octave.org>
parents:
28004
diff
changeset
|
476 if ((m_base > 0.0 && m_limit > 0.0) || (m_base < 0.0 && m_limit < 0.0)) |
24859
00ecff875f8a
Add nnz implementation for Range type (bug #53185).
maorshutman <maorus12@gmail.com>
parents:
24799
diff
changeset
|
477 { |
00ecff875f8a
Add nnz implementation for Range type (bug #53185).
maorshutman <maorus12@gmail.com>
parents:
24799
diff
changeset
|
478 // All elements have the same sign, hence there are no zeros. |
28402
09a3c0e91e6e
style update for Range class
John W. Eaton <jwe@octave.org>
parents:
28004
diff
changeset
|
479 retval = m_numel; |
24859
00ecff875f8a
Add nnz implementation for Range type (bug #53185).
maorshutman <maorus12@gmail.com>
parents:
24799
diff
changeset
|
480 } |
28402
09a3c0e91e6e
style update for Range class
John W. Eaton <jwe@octave.org>
parents:
28004
diff
changeset
|
481 else if (m_inc != 0.0) |
24859
00ecff875f8a
Add nnz implementation for Range type (bug #53185).
maorshutman <maorus12@gmail.com>
parents:
24799
diff
changeset
|
482 { |
28402
09a3c0e91e6e
style update for Range class
John W. Eaton <jwe@octave.org>
parents:
28004
diff
changeset
|
483 if (m_base == 0.0 || m_limit == 0.0) |
25812
fc74c8d2a584
Clean up liboctave Range implementation.
Rik <rik@octave.org>
parents:
25808
diff
changeset
|
484 // Exactly one zero at beginning or end of range. |
28402
09a3c0e91e6e
style update for Range class
John W. Eaton <jwe@octave.org>
parents:
28004
diff
changeset
|
485 retval = m_numel - 1; |
09a3c0e91e6e
style update for Range class
John W. Eaton <jwe@octave.org>
parents:
28004
diff
changeset
|
486 else if ((m_base / m_inc) != std::floor (m_base / m_inc)) |
25812
fc74c8d2a584
Clean up liboctave Range implementation.
Rik <rik@octave.org>
parents:
25808
diff
changeset
|
487 // Range crosses negative/positive without hitting zero. |
28402
09a3c0e91e6e
style update for Range class
John W. Eaton <jwe@octave.org>
parents:
28004
diff
changeset
|
488 retval = m_numel; |
24859
00ecff875f8a
Add nnz implementation for Range type (bug #53185).
maorshutman <maorus12@gmail.com>
parents:
24799
diff
changeset
|
489 else |
25812
fc74c8d2a584
Clean up liboctave Range implementation.
Rik <rik@octave.org>
parents:
25808
diff
changeset
|
490 // Range crosses negative/positive and hits zero. |
28402
09a3c0e91e6e
style update for Range class
John W. Eaton <jwe@octave.org>
parents:
28004
diff
changeset
|
491 retval = m_numel - 1; |
24859
00ecff875f8a
Add nnz implementation for Range type (bug #53185).
maorshutman <maorus12@gmail.com>
parents:
24799
diff
changeset
|
492 } |
00ecff875f8a
Add nnz implementation for Range type (bug #53185).
maorshutman <maorus12@gmail.com>
parents:
24799
diff
changeset
|
493 else |
00ecff875f8a
Add nnz implementation for Range type (bug #53185).
maorshutman <maorus12@gmail.com>
parents:
24799
diff
changeset
|
494 { |
28402
09a3c0e91e6e
style update for Range class
John W. Eaton <jwe@octave.org>
parents:
28004
diff
changeset
|
495 // All elements are equal (m_inc = 0) but not positive or negative, |
24859
00ecff875f8a
Add nnz implementation for Range type (bug #53185).
maorshutman <maorus12@gmail.com>
parents:
24799
diff
changeset
|
496 // therefore all elements are zero. |
00ecff875f8a
Add nnz implementation for Range type (bug #53185).
maorshutman <maorus12@gmail.com>
parents:
24799
diff
changeset
|
497 retval = 0; |
00ecff875f8a
Add nnz implementation for Range type (bug #53185).
maorshutman <maorus12@gmail.com>
parents:
24799
diff
changeset
|
498 } |
00ecff875f8a
Add nnz implementation for Range type (bug #53185).
maorshutman <maorus12@gmail.com>
parents:
24799
diff
changeset
|
499 } |
00ecff875f8a
Add nnz implementation for Range type (bug #53185).
maorshutman <maorus12@gmail.com>
parents:
24799
diff
changeset
|
500 |
00ecff875f8a
Add nnz implementation for Range type (bug #53185).
maorshutman <maorus12@gmail.com>
parents:
24799
diff
changeset
|
501 return retval; |
00ecff875f8a
Add nnz implementation for Range type (bug #53185).
maorshutman <maorus12@gmail.com>
parents:
24799
diff
changeset
|
502 } |
00ecff875f8a
Add nnz implementation for Range type (bug #53185).
maorshutman <maorus12@gmail.com>
parents:
24799
diff
changeset
|
503 |
645 | 504 Matrix |
505 Range::matrix_value (void) const | |
506 { | |
28524
455fe4a6f22c
deprecate arithmetic operators for ranges; eliminate use in Octave
John W. Eaton <jwe@octave.org>
parents:
28402
diff
changeset
|
507 Matrix retval (1, m_numel); |
20531
dcfbf4c1c3c8
eliminate trailing whitespace and tabs from sources
John W. Eaton <jwe@octave.org>
parents:
20513
diff
changeset
|
508 |
28628
83172e1c77f2
refactor octave_read_* and octave_write_* functions
John W. Eaton <jwe@octave.org>
parents:
28592
diff
changeset
|
509 if (m_numel > 0) |
645 | 510 { |
20513 | 511 // The first element must always be *exactly* the base. |
512 // E.g, -0 would otherwise become +0 in the loop (-0 + 0*increment). | |
28628
83172e1c77f2
refactor octave_read_* and octave_write_* functions
John W. Eaton <jwe@octave.org>
parents:
28592
diff
changeset
|
513 retval(0) = m_base; |
20513 | 514 |
28628
83172e1c77f2
refactor octave_read_* and octave_write_* functions
John W. Eaton <jwe@octave.org>
parents:
28592
diff
changeset
|
515 double b = m_base; |
83172e1c77f2
refactor octave_read_* and octave_write_* functions
John W. Eaton <jwe@octave.org>
parents:
28592
diff
changeset
|
516 double increment = m_inc; |
83172e1c77f2
refactor octave_read_* and octave_write_* functions
John W. Eaton <jwe@octave.org>
parents:
28592
diff
changeset
|
517 for (octave_idx_type i = 1; i < m_numel - 1; i++) |
83172e1c77f2
refactor octave_read_* and octave_write_* functions
John W. Eaton <jwe@octave.org>
parents:
28592
diff
changeset
|
518 retval.xelem (i) = b + i * increment; |
4791 | 519 |
28628
83172e1c77f2
refactor octave_read_* and octave_write_* functions
John W. Eaton <jwe@octave.org>
parents:
28592
diff
changeset
|
520 retval.xelem (m_numel - 1) = m_limit; |
645 | 521 } |
522 | |
28524
455fe4a6f22c
deprecate arithmetic operators for ranges; eliminate use in Octave
John W. Eaton <jwe@octave.org>
parents:
28402
diff
changeset
|
523 return retval; |
645 | 524 } |
3 | 525 |
9986
672e1b49e01e
optimize indexing of ranges by single subscripts
Jaroslav Hajek <highegg@gmail.com>
parents:
8971
diff
changeset
|
526 double |
672e1b49e01e
optimize indexing of ranges by single subscripts
Jaroslav Hajek <highegg@gmail.com>
parents:
8971
diff
changeset
|
527 Range::checkelem (octave_idx_type i) const |
672e1b49e01e
optimize indexing of ranges by single subscripts
Jaroslav Hajek <highegg@gmail.com>
parents:
8971
diff
changeset
|
528 { |
28402
09a3c0e91e6e
style update for Range class
John W. Eaton <jwe@octave.org>
parents:
28004
diff
changeset
|
529 if (i < 0 || i >= m_numel) |
09a3c0e91e6e
style update for Range class
John W. Eaton <jwe@octave.org>
parents:
28004
diff
changeset
|
530 octave::err_index_out_of_range (2, 2, i+1, m_numel, dims ()); |
9986
672e1b49e01e
optimize indexing of ranges by single subscripts
Jaroslav Hajek <highegg@gmail.com>
parents:
8971
diff
changeset
|
531 |
16169
0303fda3e929
Fix range behavior with -0 endpoints (bug #38423)
Rik <rik@octave.org>
parents:
15271
diff
changeset
|
532 if (i == 0) |
28402
09a3c0e91e6e
style update for Range class
John W. Eaton <jwe@octave.org>
parents:
28004
diff
changeset
|
533 return m_base; |
09a3c0e91e6e
style update for Range class
John W. Eaton <jwe@octave.org>
parents:
28004
diff
changeset
|
534 else if (i < m_numel - 1) |
09a3c0e91e6e
style update for Range class
John W. Eaton <jwe@octave.org>
parents:
28004
diff
changeset
|
535 return m_base + i * m_inc; |
16169
0303fda3e929
Fix range behavior with -0 endpoints (bug #38423)
Rik <rik@octave.org>
parents:
15271
diff
changeset
|
536 else |
28402
09a3c0e91e6e
style update for Range class
John W. Eaton <jwe@octave.org>
parents:
28004
diff
changeset
|
537 return m_limit; |
9986
672e1b49e01e
optimize indexing of ranges by single subscripts
Jaroslav Hajek <highegg@gmail.com>
parents:
8971
diff
changeset
|
538 } |
672e1b49e01e
optimize indexing of ranges by single subscripts
Jaroslav Hajek <highegg@gmail.com>
parents:
8971
diff
changeset
|
539 |
16169
0303fda3e929
Fix range behavior with -0 endpoints (bug #38423)
Rik <rik@octave.org>
parents:
15271
diff
changeset
|
540 double |
24780
0d21e2a1cdfc
new element access operators for Range objects
John W. Eaton <jwe@octave.org>
parents:
24534
diff
changeset
|
541 Range::checkelem (octave_idx_type i, octave_idx_type j) const |
0d21e2a1cdfc
new element access operators for Range objects
John W. Eaton <jwe@octave.org>
parents:
24534
diff
changeset
|
542 { |
25812
fc74c8d2a584
Clean up liboctave Range implementation.
Rik <rik@octave.org>
parents:
25808
diff
changeset
|
543 // Ranges are *always* row vectors. |
24780
0d21e2a1cdfc
new element access operators for Range objects
John W. Eaton <jwe@octave.org>
parents:
24534
diff
changeset
|
544 if (i != 0) |
28402
09a3c0e91e6e
style update for Range class
John W. Eaton <jwe@octave.org>
parents:
28004
diff
changeset
|
545 octave::err_index_out_of_range (1, 1, i+1, m_numel, dims ()); |
24780
0d21e2a1cdfc
new element access operators for Range objects
John W. Eaton <jwe@octave.org>
parents:
24534
diff
changeset
|
546 |
0d21e2a1cdfc
new element access operators for Range objects
John W. Eaton <jwe@octave.org>
parents:
24534
diff
changeset
|
547 return checkelem (j); |
0d21e2a1cdfc
new element access operators for Range objects
John W. Eaton <jwe@octave.org>
parents:
24534
diff
changeset
|
548 } |
0d21e2a1cdfc
new element access operators for Range objects
John W. Eaton <jwe@octave.org>
parents:
24534
diff
changeset
|
549 |
0d21e2a1cdfc
new element access operators for Range objects
John W. Eaton <jwe@octave.org>
parents:
24534
diff
changeset
|
550 double |
16169
0303fda3e929
Fix range behavior with -0 endpoints (bug #38423)
Rik <rik@octave.org>
parents:
15271
diff
changeset
|
551 Range::elem (octave_idx_type i) const |
0303fda3e929
Fix range behavior with -0 endpoints (bug #38423)
Rik <rik@octave.org>
parents:
15271
diff
changeset
|
552 { |
0303fda3e929
Fix range behavior with -0 endpoints (bug #38423)
Rik <rik@octave.org>
parents:
15271
diff
changeset
|
553 if (i == 0) |
28402
09a3c0e91e6e
style update for Range class
John W. Eaton <jwe@octave.org>
parents:
28004
diff
changeset
|
554 return m_base; |
09a3c0e91e6e
style update for Range class
John W. Eaton <jwe@octave.org>
parents:
28004
diff
changeset
|
555 else if (i < m_numel - 1) |
09a3c0e91e6e
style update for Range class
John W. Eaton <jwe@octave.org>
parents:
28004
diff
changeset
|
556 return m_base + i * m_inc; |
16169
0303fda3e929
Fix range behavior with -0 endpoints (bug #38423)
Rik <rik@octave.org>
parents:
15271
diff
changeset
|
557 else |
28402
09a3c0e91e6e
style update for Range class
John W. Eaton <jwe@octave.org>
parents:
28004
diff
changeset
|
558 return m_limit; |
16169
0303fda3e929
Fix range behavior with -0 endpoints (bug #38423)
Rik <rik@octave.org>
parents:
15271
diff
changeset
|
559 } |
0303fda3e929
Fix range behavior with -0 endpoints (bug #38423)
Rik <rik@octave.org>
parents:
15271
diff
changeset
|
560 |
11586
12df7854fa7c
strip trailing whitespace from source files
John W. Eaton <jwe@octave.org>
parents:
11523
diff
changeset
|
561 Array<double> |
30188
e424d966dca7
replace rangeidx helper classes with lambda expressions
John W. Eaton <jwe@octave.org>
parents:
30159
diff
changeset
|
562 Range::index (const octave::idx_vector& idx) const |
9986
672e1b49e01e
optimize indexing of ranges by single subscripts
Jaroslav Hajek <highegg@gmail.com>
parents:
8971
diff
changeset
|
563 { |
672e1b49e01e
optimize indexing of ranges by single subscripts
Jaroslav Hajek <highegg@gmail.com>
parents:
8971
diff
changeset
|
564 Array<double> retval; |
672e1b49e01e
optimize indexing of ranges by single subscripts
Jaroslav Hajek <highegg@gmail.com>
parents:
8971
diff
changeset
|
565 |
28402
09a3c0e91e6e
style update for Range class
John W. Eaton <jwe@octave.org>
parents:
28004
diff
changeset
|
566 octave_idx_type n = m_numel; |
9986
672e1b49e01e
optimize indexing of ranges by single subscripts
Jaroslav Hajek <highegg@gmail.com>
parents:
8971
diff
changeset
|
567 |
30188
e424d966dca7
replace rangeidx helper classes with lambda expressions
John W. Eaton <jwe@octave.org>
parents:
30159
diff
changeset
|
568 if (idx.is_colon ()) |
9986
672e1b49e01e
optimize indexing of ranges by single subscripts
Jaroslav Hajek <highegg@gmail.com>
parents:
8971
diff
changeset
|
569 { |
28402
09a3c0e91e6e
style update for Range class
John W. Eaton <jwe@octave.org>
parents:
28004
diff
changeset
|
570 retval = matrix_value ().reshape (dim_vector (m_numel, 1)); |
9986
672e1b49e01e
optimize indexing of ranges by single subscripts
Jaroslav Hajek <highegg@gmail.com>
parents:
8971
diff
changeset
|
571 } |
672e1b49e01e
optimize indexing of ranges by single subscripts
Jaroslav Hajek <highegg@gmail.com>
parents:
8971
diff
changeset
|
572 else |
672e1b49e01e
optimize indexing of ranges by single subscripts
Jaroslav Hajek <highegg@gmail.com>
parents:
8971
diff
changeset
|
573 { |
30188
e424d966dca7
replace rangeidx helper classes with lambda expressions
John W. Eaton <jwe@octave.org>
parents:
30159
diff
changeset
|
574 if (idx.extent (n) != n) |
e424d966dca7
replace rangeidx helper classes with lambda expressions
John W. Eaton <jwe@octave.org>
parents:
30159
diff
changeset
|
575 octave::err_index_out_of_range (1, 1, idx.extent (n), n, dims ()); // throws |
10366
e5ae13b8b2c2
improve Array indexing error messages
Jaroslav Hajek <highegg@gmail.com>
parents:
10314
diff
changeset
|
576 |
30188
e424d966dca7
replace rangeidx helper classes with lambda expressions
John W. Eaton <jwe@octave.org>
parents:
30159
diff
changeset
|
577 dim_vector idx_dims = idx.orig_dimensions (); |
e424d966dca7
replace rangeidx helper classes with lambda expressions
John W. Eaton <jwe@octave.org>
parents:
30159
diff
changeset
|
578 octave_idx_type idx_len = idx.length (n); |
9986
672e1b49e01e
optimize indexing of ranges by single subscripts
Jaroslav Hajek <highegg@gmail.com>
parents:
8971
diff
changeset
|
579 |
672e1b49e01e
optimize indexing of ranges by single subscripts
Jaroslav Hajek <highegg@gmail.com>
parents:
8971
diff
changeset
|
580 // taken from Array.cc. |
30188
e424d966dca7
replace rangeidx helper classes with lambda expressions
John W. Eaton <jwe@octave.org>
parents:
30159
diff
changeset
|
581 if (n != 1 && idx_dims.isvector ()) |
e424d966dca7
replace rangeidx helper classes with lambda expressions
John W. Eaton <jwe@octave.org>
parents:
30159
diff
changeset
|
582 idx_dims = dim_vector (1, idx_len); |
9986
672e1b49e01e
optimize indexing of ranges by single subscripts
Jaroslav Hajek <highegg@gmail.com>
parents:
8971
diff
changeset
|
583 |
30188
e424d966dca7
replace rangeidx helper classes with lambda expressions
John W. Eaton <jwe@octave.org>
parents:
30159
diff
changeset
|
584 retval.clear (idx_dims); |
9986
672e1b49e01e
optimize indexing of ranges by single subscripts
Jaroslav Hajek <highegg@gmail.com>
parents:
8971
diff
changeset
|
585 |
30189
41eecdf76372
* Range.h, Range.cc: Fix comments missed in previous change.
John W. Eaton <jwe@octave.org>
parents:
30188
diff
changeset
|
586 // Loop over all values in IDX, executing the lambda expression |
41eecdf76372
* Range.h, Range.cc: Fix comments missed in previous change.
John W. Eaton <jwe@octave.org>
parents:
30188
diff
changeset
|
587 // for each index value. |
30188
e424d966dca7
replace rangeidx helper classes with lambda expressions
John W. Eaton <jwe@octave.org>
parents:
30159
diff
changeset
|
588 |
e424d966dca7
replace rangeidx helper classes with lambda expressions
John W. Eaton <jwe@octave.org>
parents:
30159
diff
changeset
|
589 double *array = retval.fortran_vec (); |
e424d966dca7
replace rangeidx helper classes with lambda expressions
John W. Eaton <jwe@octave.org>
parents:
30159
diff
changeset
|
590 |
30394
f3f3e3793fb5
maint: style check C++ files in liboctave/ ahead of 7.1 release.
Rik <rik@octave.org>
parents:
30189
diff
changeset
|
591 idx.loop (n, [=, &array] (idx_vector i) |
f3f3e3793fb5
maint: style check C++ files in liboctave/ ahead of 7.1 release.
Rik <rik@octave.org>
parents:
30189
diff
changeset
|
592 { |
30188
e424d966dca7
replace rangeidx helper classes with lambda expressions
John W. Eaton <jwe@octave.org>
parents:
30159
diff
changeset
|
593 if (i == 0) |
e424d966dca7
replace rangeidx helper classes with lambda expressions
John W. Eaton <jwe@octave.org>
parents:
30159
diff
changeset
|
594 *array++ = m_base; |
e424d966dca7
replace rangeidx helper classes with lambda expressions
John W. Eaton <jwe@octave.org>
parents:
30159
diff
changeset
|
595 else if (i < m_numel - 1) |
e424d966dca7
replace rangeidx helper classes with lambda expressions
John W. Eaton <jwe@octave.org>
parents:
30159
diff
changeset
|
596 *array++ = m_base + i * m_inc; |
e424d966dca7
replace rangeidx helper classes with lambda expressions
John W. Eaton <jwe@octave.org>
parents:
30159
diff
changeset
|
597 else |
e424d966dca7
replace rangeidx helper classes with lambda expressions
John W. Eaton <jwe@octave.org>
parents:
30159
diff
changeset
|
598 *array++ = m_limit; |
e424d966dca7
replace rangeidx helper classes with lambda expressions
John W. Eaton <jwe@octave.org>
parents:
30159
diff
changeset
|
599 }); |
9986
672e1b49e01e
optimize indexing of ranges by single subscripts
Jaroslav Hajek <highegg@gmail.com>
parents:
8971
diff
changeset
|
600 } |
672e1b49e01e
optimize indexing of ranges by single subscripts
Jaroslav Hajek <highegg@gmail.com>
parents:
8971
diff
changeset
|
601 |
672e1b49e01e
optimize indexing of ranges by single subscripts
Jaroslav Hajek <highegg@gmail.com>
parents:
8971
diff
changeset
|
602 return retval; |
672e1b49e01e
optimize indexing of ranges by single subscripts
Jaroslav Hajek <highegg@gmail.com>
parents:
8971
diff
changeset
|
603 } |
4810 | 604 |
20228
00cf2847355d
Deprecate Array::nelem() and Range::nelem() in favour of ::numel().
Carnë Draug <carandraug@octave.org>
parents:
19697
diff
changeset
|
605 // NOTE: max and min only return useful values if numel > 0. |
00cf2847355d
Deprecate Array::nelem() and Range::nelem() in favour of ::numel().
Carnë Draug <carandraug@octave.org>
parents:
19697
diff
changeset
|
606 // do_minmax_body() in max.cc avoids calling Range::min/max if numel == 0. |
208 | 607 |
608 double | |
609 Range::min (void) const | |
610 { | |
611 double retval = 0.0; | |
28402
09a3c0e91e6e
style update for Range class
John W. Eaton <jwe@octave.org>
parents:
28004
diff
changeset
|
612 if (m_numel > 0) |
208 | 613 { |
28402
09a3c0e91e6e
style update for Range class
John W. Eaton <jwe@octave.org>
parents:
28004
diff
changeset
|
614 if (m_inc > 0) |
09a3c0e91e6e
style update for Range class
John W. Eaton <jwe@octave.org>
parents:
28004
diff
changeset
|
615 retval = m_base; |
208 | 616 else |
10314
07ebe522dac2
untabify liboctave C++ sources
John W. Eaton <jwe@octave.org>
parents:
10158
diff
changeset
|
617 { |
28402
09a3c0e91e6e
style update for Range class
John W. Eaton <jwe@octave.org>
parents:
28004
diff
changeset
|
618 retval = m_base + (m_numel - 1) * m_inc; |
4791 | 619 |
20513 | 620 // Require '<=' test. See note in max (). |
28402
09a3c0e91e6e
style update for Range class
John W. Eaton <jwe@octave.org>
parents:
28004
diff
changeset
|
621 if (retval <= m_limit) |
09a3c0e91e6e
style update for Range class
John W. Eaton <jwe@octave.org>
parents:
28004
diff
changeset
|
622 retval = m_limit; |
10314
07ebe522dac2
untabify liboctave C++ sources
John W. Eaton <jwe@octave.org>
parents:
10158
diff
changeset
|
623 } |
4791 | 624 |
208 | 625 } |
626 return retval; | |
627 } | |
628 | |
629 double | |
630 Range::max (void) const | |
631 { | |
632 double retval = 0.0; | |
28402
09a3c0e91e6e
style update for Range class
John W. Eaton <jwe@octave.org>
parents:
28004
diff
changeset
|
633 if (m_numel > 0) |
208 | 634 { |
28402
09a3c0e91e6e
style update for Range class
John W. Eaton <jwe@octave.org>
parents:
28004
diff
changeset
|
635 if (m_inc > 0) |
10314
07ebe522dac2
untabify liboctave C++ sources
John W. Eaton <jwe@octave.org>
parents:
10158
diff
changeset
|
636 { |
28402
09a3c0e91e6e
style update for Range class
John W. Eaton <jwe@octave.org>
parents:
28004
diff
changeset
|
637 retval = m_base + (m_numel - 1) * m_inc; |
4791 | 638 |
20513 | 639 // On some machines (x86 with extended precision floating point |
640 // arithmetic, for example) it is possible that we can overshoot the | |
641 // limit by approximately the machine precision even though we were | |
642 // very careful in our calculation of the number of elements. | |
643 // Therefore, we clip the result to the limit if it overshoots. | |
28402
09a3c0e91e6e
style update for Range class
John W. Eaton <jwe@octave.org>
parents:
28004
diff
changeset
|
644 // The test also includes equality (>= m_limit) to have expressions |
20513 | 645 // such as -5:1:-0 result in a -0 endpoint. |
28402
09a3c0e91e6e
style update for Range class
John W. Eaton <jwe@octave.org>
parents:
28004
diff
changeset
|
646 if (retval >= m_limit) |
09a3c0e91e6e
style update for Range class
John W. Eaton <jwe@octave.org>
parents:
28004
diff
changeset
|
647 retval = m_limit; |
10314
07ebe522dac2
untabify liboctave C++ sources
John W. Eaton <jwe@octave.org>
parents:
10158
diff
changeset
|
648 } |
208 | 649 else |
28402
09a3c0e91e6e
style update for Range class
John W. Eaton <jwe@octave.org>
parents:
28004
diff
changeset
|
650 retval = m_base; |
208 | 651 } |
652 return retval; | |
653 } | |
654 | |
655 void | |
7458 | 656 Range::sort_internal (bool ascending) |
208 | 657 { |
28402
09a3c0e91e6e
style update for Range class
John W. Eaton <jwe@octave.org>
parents:
28004
diff
changeset
|
658 if ((ascending && m_base > m_limit && m_inc < 0.0) |
09a3c0e91e6e
style update for Range class
John W. Eaton <jwe@octave.org>
parents:
28004
diff
changeset
|
659 || (! ascending && m_base < m_limit && m_inc > 0.0)) |
208 | 660 { |
28402
09a3c0e91e6e
style update for Range class
John W. Eaton <jwe@octave.org>
parents:
28004
diff
changeset
|
661 std::swap (m_base, m_limit); |
09a3c0e91e6e
style update for Range class
John W. Eaton <jwe@octave.org>
parents:
28004
diff
changeset
|
662 m_inc = -m_inc; |
7458 | 663 } |
664 } | |
665 | |
666 void | |
667 Range::sort_internal (Array<octave_idx_type>& sidx, bool ascending) | |
668 { | |
20228
00cf2847355d
Deprecate Array::nelem() and Range::nelem() in favour of ::numel().
Carnë Draug <carandraug@octave.org>
parents:
19697
diff
changeset
|
669 octave_idx_type nel = numel (); |
7458 | 670 |
671 sidx.resize (dim_vector (1, nel)); | |
672 | |
673 octave_idx_type *psidx = sidx.fortran_vec (); | |
674 | |
675 bool reverse = false; | |
676 | |
28402
09a3c0e91e6e
style update for Range class
John W. Eaton <jwe@octave.org>
parents:
28004
diff
changeset
|
677 if ((ascending && m_base > m_limit && m_inc < 0.0) |
09a3c0e91e6e
style update for Range class
John W. Eaton <jwe@octave.org>
parents:
28004
diff
changeset
|
678 || (! ascending && m_base < m_limit && m_inc > 0.0)) |
7458 | 679 { |
28402
09a3c0e91e6e
style update for Range class
John W. Eaton <jwe@octave.org>
parents:
28004
diff
changeset
|
680 std::swap (m_base, m_limit); |
09a3c0e91e6e
style update for Range class
John W. Eaton <jwe@octave.org>
parents:
28004
diff
changeset
|
681 m_inc = -m_inc; |
7458 | 682 reverse = true; |
683 } | |
684 | |
23450
855122b993da
maint: Wrap tertiary operator in parentheses "(COND ? x : y)".
Rik <rik@octave.org>
parents:
23443
diff
changeset
|
685 octave_idx_type tmp = (reverse ? nel - 1 : 0); |
855122b993da
maint: Wrap tertiary operator in parentheses "(COND ? x : y)".
Rik <rik@octave.org>
parents:
23443
diff
changeset
|
686 octave_idx_type stp = (reverse ? -1 : 1); |
7458 | 687 |
7467 | 688 for (octave_idx_type i = 0; i < nel; i++, tmp += stp) |
7458 | 689 psidx[i] = tmp; |
690 } | |
691 | |
11586
12df7854fa7c
strip trailing whitespace from source files
John W. Eaton <jwe@octave.org>
parents:
11523
diff
changeset
|
692 Matrix |
7620
36594d5bbe13
Move diag function into the octave_value class
David Bateman <dbateman@free.fr>
parents:
7467
diff
changeset
|
693 Range::diag (octave_idx_type k) const |
36594d5bbe13
Move diag function into the octave_value class
David Bateman <dbateman@free.fr>
parents:
7467
diff
changeset
|
694 { |
8971 | 695 return matrix_value ().diag (k); |
7620
36594d5bbe13
Move diag function into the octave_value class
David Bateman <dbateman@free.fr>
parents:
7467
diff
changeset
|
696 } |
36594d5bbe13
Move diag function into the octave_value class
David Bateman <dbateman@free.fr>
parents:
7467
diff
changeset
|
697 |
7458 | 698 Range |
699 Range::sort (octave_idx_type dim, sortmode mode) const | |
700 { | |
701 Range retval = *this; | |
702 | |
703 if (dim == 1) | |
704 { | |
7463
2467639bd8c0
eliminate UNDEFINED sort mode
John W. Eaton <jwe@octave.org>
parents:
7458
diff
changeset
|
705 if (mode == ASCENDING) |
10314
07ebe522dac2
untabify liboctave C++ sources
John W. Eaton <jwe@octave.org>
parents:
10158
diff
changeset
|
706 retval.sort_internal (true); |
7463
2467639bd8c0
eliminate UNDEFINED sort mode
John W. Eaton <jwe@octave.org>
parents:
7458
diff
changeset
|
707 else if (mode == DESCENDING) |
10314
07ebe522dac2
untabify liboctave C++ sources
John W. Eaton <jwe@octave.org>
parents:
10158
diff
changeset
|
708 retval.sort_internal (false); |
7458 | 709 } |
710 else if (dim != 0) | |
711 (*current_liboctave_error_handler) ("Range::sort: invalid dimension"); | |
712 | |
713 return retval; | |
714 } | |
715 | |
716 Range | |
717 Range::sort (Array<octave_idx_type>& sidx, octave_idx_type dim, | |
10314
07ebe522dac2
untabify liboctave C++ sources
John W. Eaton <jwe@octave.org>
parents:
10158
diff
changeset
|
718 sortmode mode) const |
7458 | 719 { |
720 Range retval = *this; | |
721 | |
722 if (dim == 1) | |
723 { | |
7463
2467639bd8c0
eliminate UNDEFINED sort mode
John W. Eaton <jwe@octave.org>
parents:
7458
diff
changeset
|
724 if (mode == ASCENDING) |
16169
0303fda3e929
Fix range behavior with -0 endpoints (bug #38423)
Rik <rik@octave.org>
parents:
15271
diff
changeset
|
725 retval.sort_internal (sidx, true); |
7463
2467639bd8c0
eliminate UNDEFINED sort mode
John W. Eaton <jwe@octave.org>
parents:
7458
diff
changeset
|
726 else if (mode == DESCENDING) |
10314
07ebe522dac2
untabify liboctave C++ sources
John W. Eaton <jwe@octave.org>
parents:
10158
diff
changeset
|
727 retval.sort_internal (sidx, false); |
7458 | 728 } |
729 else if (dim != 0) | |
730 (*current_liboctave_error_handler) ("Range::sort: invalid dimension"); | |
731 | |
732 return retval; | |
208 | 733 } |
734 | |
8721
e9cb742df9eb
imported patch sort3.diff
Jaroslav Hajek <highegg@gmail.com>
parents:
8589
diff
changeset
|
735 sortmode |
23588
0549061d35b9
maint: Deprecate is_sorted and replace with issorted.
Rik <rik@octave.org>
parents:
23577
diff
changeset
|
736 Range::issorted (sortmode mode) const |
8721
e9cb742df9eb
imported patch sort3.diff
Jaroslav Hajek <highegg@gmail.com>
parents:
8589
diff
changeset
|
737 { |
28402
09a3c0e91e6e
style update for Range class
John W. Eaton <jwe@octave.org>
parents:
28004
diff
changeset
|
738 if (m_numel > 1 && m_inc > 0) |
16169
0303fda3e929
Fix range behavior with -0 endpoints (bug #38423)
Rik <rik@octave.org>
parents:
15271
diff
changeset
|
739 mode = (mode == DESCENDING) ? UNSORTED : ASCENDING; |
28402
09a3c0e91e6e
style update for Range class
John W. Eaton <jwe@octave.org>
parents:
28004
diff
changeset
|
740 else if (m_numel > 1 && m_inc < 0) |
8721
e9cb742df9eb
imported patch sort3.diff
Jaroslav Hajek <highegg@gmail.com>
parents:
8589
diff
changeset
|
741 mode = (mode == ASCENDING) ? UNSORTED : DESCENDING; |
e9cb742df9eb
imported patch sort3.diff
Jaroslav Hajek <highegg@gmail.com>
parents:
8589
diff
changeset
|
742 else |
26444
5c91daa02f87
Fix static analyzer detected issues with UNSORTED enum (bug #55347).
Rik <rik@octave.org>
parents:
26376
diff
changeset
|
743 mode = (mode == UNSORTED) ? ASCENDING : mode; |
8742
d2b06871afac
add missing return statement
Jaroslav Hajek <highegg@gmail.com>
parents:
8721
diff
changeset
|
744 |
d2b06871afac
add missing return statement
Jaroslav Hajek <highegg@gmail.com>
parents:
8721
diff
changeset
|
745 return mode; |
8721
e9cb742df9eb
imported patch sort3.diff
Jaroslav Hajek <highegg@gmail.com>
parents:
8589
diff
changeset
|
746 } |
e9cb742df9eb
imported patch sort3.diff
Jaroslav Hajek <highegg@gmail.com>
parents:
8589
diff
changeset
|
747 |
20513 | 748 void |
749 Range::set_base (double b) | |
750 { | |
28402
09a3c0e91e6e
style update for Range class
John W. Eaton <jwe@octave.org>
parents:
28004
diff
changeset
|
751 if (m_base != b) |
20513 | 752 { |
28402
09a3c0e91e6e
style update for Range class
John W. Eaton <jwe@octave.org>
parents:
28004
diff
changeset
|
753 m_base = b; |
20513 | 754 |
21321
79a51b7e00b6
eliminate some copy and paste code fragments in the Range class
John W. Eaton <jwe@octave.org>
parents:
21301
diff
changeset
|
755 init (); |
20513 | 756 } |
757 } | |
758 | |
759 void | |
760 Range::set_limit (double l) | |
761 { | |
28402
09a3c0e91e6e
style update for Range class
John W. Eaton <jwe@octave.org>
parents:
28004
diff
changeset
|
762 if (m_limit != l) |
20513 | 763 { |
28402
09a3c0e91e6e
style update for Range class
John W. Eaton <jwe@octave.org>
parents:
28004
diff
changeset
|
764 m_limit = l; |
20513 | 765 |
21321
79a51b7e00b6
eliminate some copy and paste code fragments in the Range class
John W. Eaton <jwe@octave.org>
parents:
21301
diff
changeset
|
766 init (); |
20513 | 767 } |
768 } | |
769 | |
770 void | |
771 Range::set_inc (double i) | |
772 { | |
28402
09a3c0e91e6e
style update for Range class
John W. Eaton <jwe@octave.org>
parents:
28004
diff
changeset
|
773 if (m_inc != i) |
20513 | 774 { |
28402
09a3c0e91e6e
style update for Range class
John W. Eaton <jwe@octave.org>
parents:
28004
diff
changeset
|
775 m_inc = i; |
20513 | 776 |
21321
79a51b7e00b6
eliminate some copy and paste code fragments in the Range class
John W. Eaton <jwe@octave.org>
parents:
21301
diff
changeset
|
777 init (); |
20513 | 778 } |
779 } | |
780 | |
3504 | 781 std::ostream& |
782 operator << (std::ostream& os, const Range& a) | |
3 | 783 { |
784 double b = a.base (); | |
28636
a3db48e66ef8
use Range::increment instead of Range::inc
John W. Eaton <jwe@octave.org>
parents:
28635
diff
changeset
|
785 double increment = a.increment (); |
25812
fc74c8d2a584
Clean up liboctave Range implementation.
Rik <rik@octave.org>
parents:
25808
diff
changeset
|
786 octave_idx_type nel = a.numel (); |
3 | 787 |
25812
fc74c8d2a584
Clean up liboctave Range implementation.
Rik <rik@octave.org>
parents:
25808
diff
changeset
|
788 if (nel > 1) |
16169
0303fda3e929
Fix range behavior with -0 endpoints (bug #38423)
Rik <rik@octave.org>
parents:
15271
diff
changeset
|
789 { |
25812
fc74c8d2a584
Clean up liboctave Range implementation.
Rik <rik@octave.org>
parents:
25808
diff
changeset
|
790 // First element must be the base *exactly* (e.g., -0). |
23807
336f89b6208b
Use character literals 'c' rather than string literals "c" when possible.
Rik <rik@octave.org>
parents:
23662
diff
changeset
|
791 os << b << ' '; |
25812
fc74c8d2a584
Clean up liboctave Range implementation.
Rik <rik@octave.org>
parents:
25808
diff
changeset
|
792 for (octave_idx_type i = 1; i < nel-1; i++) |
23807
336f89b6208b
Use character literals 'c' rather than string literals "c" when possible.
Rik <rik@octave.org>
parents:
23662
diff
changeset
|
793 os << b + i * increment << ' '; |
16169
0303fda3e929
Fix range behavior with -0 endpoints (bug #38423)
Rik <rik@octave.org>
parents:
15271
diff
changeset
|
794 } |
3 | 795 |
25812
fc74c8d2a584
Clean up liboctave Range implementation.
Rik <rik@octave.org>
parents:
25808
diff
changeset
|
796 // Print out the last element exactly, rather than a calculated last element. |
28402
09a3c0e91e6e
style update for Range class
John W. Eaton <jwe@octave.org>
parents:
28004
diff
changeset
|
797 os << a.m_limit << "\n"; |
3 | 798 |
799 return os; | |
800 } | |
801 | |
3504 | 802 std::istream& |
803 operator >> (std::istream& is, Range& a) | |
3 | 804 { |
28402
09a3c0e91e6e
style update for Range class
John W. Eaton <jwe@octave.org>
parents:
28004
diff
changeset
|
805 is >> a.m_base; |
3 | 806 if (is) |
807 { | |
28402
09a3c0e91e6e
style update for Range class
John W. Eaton <jwe@octave.org>
parents:
28004
diff
changeset
|
808 double tmp_limit; |
09a3c0e91e6e
style update for Range class
John W. Eaton <jwe@octave.org>
parents:
28004
diff
changeset
|
809 is >> tmp_limit; |
20513 | 810 |
3 | 811 if (is) |
28402
09a3c0e91e6e
style update for Range class
John W. Eaton <jwe@octave.org>
parents:
28004
diff
changeset
|
812 is >> a.m_inc; |
20513 | 813 |
28402
09a3c0e91e6e
style update for Range class
John W. Eaton <jwe@octave.org>
parents:
28004
diff
changeset
|
814 // Clip the m_limit to the true limit, rebuild numel, clear cache |
09a3c0e91e6e
style update for Range class
John W. Eaton <jwe@octave.org>
parents:
28004
diff
changeset
|
815 a.set_limit (tmp_limit); |
3 | 816 } |
817 | |
818 return is; | |
819 } | |
820 | |
29954
4c88a452519c
rename OCTAVE_USE_DEPRECATED_FUNCTIONS macro and attempt to make it work
John W. Eaton <jwe@octave.org>
parents:
29569
diff
changeset
|
821 // DEPRECATED in Octave 7. |
4c88a452519c
rename OCTAVE_USE_DEPRECATED_FUNCTIONS macro and attempt to make it work
John W. Eaton <jwe@octave.org>
parents:
29569
diff
changeset
|
822 Range operator - (const Range& r) |
2599 | 823 { |
28636
a3db48e66ef8
use Range::increment instead of Range::inc
John W. Eaton <jwe@octave.org>
parents:
28635
diff
changeset
|
824 return Range (-r.base (), -r.limit (), -r.increment (), r.numel ()); |
2599 | 825 } |
826 | |
29954
4c88a452519c
rename OCTAVE_USE_DEPRECATED_FUNCTIONS macro and attempt to make it work
John W. Eaton <jwe@octave.org>
parents:
29569
diff
changeset
|
827 // DEPRECATED in Octave 7. |
8553
c7ff200e45f5
optimize range-scalar ops
Jaroslav Hajek <highegg@gmail.com>
parents:
7620
diff
changeset
|
828 Range operator + (double x, const Range& r) |
c7ff200e45f5
optimize range-scalar ops
Jaroslav Hajek <highegg@gmail.com>
parents:
7620
diff
changeset
|
829 { |
28636
a3db48e66ef8
use Range::increment instead of Range::inc
John W. Eaton <jwe@octave.org>
parents:
28635
diff
changeset
|
830 return Range (x + r.base (), x + r.limit (), r.increment (), r.numel ()); |
8553
c7ff200e45f5
optimize range-scalar ops
Jaroslav Hajek <highegg@gmail.com>
parents:
7620
diff
changeset
|
831 } |
c7ff200e45f5
optimize range-scalar ops
Jaroslav Hajek <highegg@gmail.com>
parents:
7620
diff
changeset
|
832 |
29954
4c88a452519c
rename OCTAVE_USE_DEPRECATED_FUNCTIONS macro and attempt to make it work
John W. Eaton <jwe@octave.org>
parents:
29569
diff
changeset
|
833 // DEPRECATED in Octave 7. |
8553
c7ff200e45f5
optimize range-scalar ops
Jaroslav Hajek <highegg@gmail.com>
parents:
7620
diff
changeset
|
834 Range operator + (const Range& r, double x) |
c7ff200e45f5
optimize range-scalar ops
Jaroslav Hajek <highegg@gmail.com>
parents:
7620
diff
changeset
|
835 { |
28636
a3db48e66ef8
use Range::increment instead of Range::inc
John W. Eaton <jwe@octave.org>
parents:
28635
diff
changeset
|
836 return Range (r.base () + x, r.limit () + x, r.increment (), r.numel ()); |
8553
c7ff200e45f5
optimize range-scalar ops
Jaroslav Hajek <highegg@gmail.com>
parents:
7620
diff
changeset
|
837 } |
c7ff200e45f5
optimize range-scalar ops
Jaroslav Hajek <highegg@gmail.com>
parents:
7620
diff
changeset
|
838 |
29954
4c88a452519c
rename OCTAVE_USE_DEPRECATED_FUNCTIONS macro and attempt to make it work
John W. Eaton <jwe@octave.org>
parents:
29569
diff
changeset
|
839 // DEPRECATED in Octave 7. |
8553
c7ff200e45f5
optimize range-scalar ops
Jaroslav Hajek <highegg@gmail.com>
parents:
7620
diff
changeset
|
840 Range operator - (double x, const Range& r) |
c7ff200e45f5
optimize range-scalar ops
Jaroslav Hajek <highegg@gmail.com>
parents:
7620
diff
changeset
|
841 { |
28636
a3db48e66ef8
use Range::increment instead of Range::inc
John W. Eaton <jwe@octave.org>
parents:
28635
diff
changeset
|
842 return Range (x - r.base (), x - r.limit (), -r.increment (), r.numel ()); |
8553
c7ff200e45f5
optimize range-scalar ops
Jaroslav Hajek <highegg@gmail.com>
parents:
7620
diff
changeset
|
843 } |
c7ff200e45f5
optimize range-scalar ops
Jaroslav Hajek <highegg@gmail.com>
parents:
7620
diff
changeset
|
844 |
29954
4c88a452519c
rename OCTAVE_USE_DEPRECATED_FUNCTIONS macro and attempt to make it work
John W. Eaton <jwe@octave.org>
parents:
29569
diff
changeset
|
845 // DEPRECATED in Octave 7. |
8553
c7ff200e45f5
optimize range-scalar ops
Jaroslav Hajek <highegg@gmail.com>
parents:
7620
diff
changeset
|
846 Range operator - (const Range& r, double x) |
c7ff200e45f5
optimize range-scalar ops
Jaroslav Hajek <highegg@gmail.com>
parents:
7620
diff
changeset
|
847 { |
28636
a3db48e66ef8
use Range::increment instead of Range::inc
John W. Eaton <jwe@octave.org>
parents:
28635
diff
changeset
|
848 return Range (r.base () - x, r.limit () - x, r.increment (), r.numel ()); |
8553
c7ff200e45f5
optimize range-scalar ops
Jaroslav Hajek <highegg@gmail.com>
parents:
7620
diff
changeset
|
849 } |
c7ff200e45f5
optimize range-scalar ops
Jaroslav Hajek <highegg@gmail.com>
parents:
7620
diff
changeset
|
850 |
29954
4c88a452519c
rename OCTAVE_USE_DEPRECATED_FUNCTIONS macro and attempt to make it work
John W. Eaton <jwe@octave.org>
parents:
29569
diff
changeset
|
851 // DEPRECATED in Octave 7. |
8553
c7ff200e45f5
optimize range-scalar ops
Jaroslav Hajek <highegg@gmail.com>
parents:
7620
diff
changeset
|
852 Range operator * (double x, const Range& r) |
c7ff200e45f5
optimize range-scalar ops
Jaroslav Hajek <highegg@gmail.com>
parents:
7620
diff
changeset
|
853 { |
28636
a3db48e66ef8
use Range::increment instead of Range::inc
John W. Eaton <jwe@octave.org>
parents:
28635
diff
changeset
|
854 return Range (x * r.base (), x * r.limit (), x * r.increment (), r.numel ()); |
8553
c7ff200e45f5
optimize range-scalar ops
Jaroslav Hajek <highegg@gmail.com>
parents:
7620
diff
changeset
|
855 } |
c7ff200e45f5
optimize range-scalar ops
Jaroslav Hajek <highegg@gmail.com>
parents:
7620
diff
changeset
|
856 |
29954
4c88a452519c
rename OCTAVE_USE_DEPRECATED_FUNCTIONS macro and attempt to make it work
John W. Eaton <jwe@octave.org>
parents:
29569
diff
changeset
|
857 // DEPRECATED in Octave 7. |
8553
c7ff200e45f5
optimize range-scalar ops
Jaroslav Hajek <highegg@gmail.com>
parents:
7620
diff
changeset
|
858 Range operator * (const Range& r, double x) |
c7ff200e45f5
optimize range-scalar ops
Jaroslav Hajek <highegg@gmail.com>
parents:
7620
diff
changeset
|
859 { |
28636
a3db48e66ef8
use Range::increment instead of Range::inc
John W. Eaton <jwe@octave.org>
parents:
28635
diff
changeset
|
860 return Range (r.base () * x, r.limit () * x, r.increment () * x, r.numel ()); |
8553
c7ff200e45f5
optimize range-scalar ops
Jaroslav Hajek <highegg@gmail.com>
parents:
7620
diff
changeset
|
861 } |
c7ff200e45f5
optimize range-scalar ops
Jaroslav Hajek <highegg@gmail.com>
parents:
7620
diff
changeset
|
862 |
1546 | 863 // C See Knuth, Art Of Computer Programming, Vol. 1, Problem 1.2.4-5. |
864 // C | |
865 // C===Tolerant FLOOR function. | |
866 // C | |
867 // C X - is given as a Double Precision argument to be operated on. | |
868 // C It is assumed that X is represented with M mantissa bits. | |
869 // C CT - is given as a Comparison Tolerance such that | |
870 // C 0.LT.CT.LE.3-SQRT(5)/2. If the relative difference between | |
871 // C X and A whole number is less than CT, then TFLOOR is | |
872 // C returned as this whole number. By treating the | |
873 // C floating-point numbers as a finite ordered set note that | |
874 // C the heuristic EPS=2.**(-(M-1)) and CT=3*EPS causes | |
875 // C arguments of TFLOOR/TCEIL to be treated as whole numbers | |
876 // C if they are exactly whole numbers or are immediately | |
877 // C adjacent to whole number representations. Since EPS, the | |
878 // C "distance" between floating-point numbers on the unit | |
879 // C interval, and M, the number of bits in X'S mantissa, exist | |
880 // C on every floating-point computer, TFLOOR/TCEIL are | |
881 // C consistently definable on every floating-point computer. | |
882 // C | |
883 // C For more information see the following references: | |
884 // C (1) P. E. Hagerty, "More On Fuzzy Floor And Ceiling," APL QUOTE | |
885 // C QUAD 8(4):20-24, June 1978. Note that TFLOOR=FL5. | |
886 // C (2) L. M. Breed, "Definitions For Fuzzy Floor And Ceiling", APL | |
887 // C QUOTE QUAD 8(3):16-23, March 1978. This paper cites FL1 through | |
888 // C FL5, the history of five years of evolutionary development of | |
889 // C FL5 - the seven lines of code below - by open collaboration | |
890 // C and corroboration of the mathematical-computing community. | |
891 // C | |
892 // C Penn State University Center for Academic Computing | |
893 // C H. D. Knoble - August, 1978. | |
894 | |
895 static inline double | |
896 tfloor (double x, double ct) | |
897 { | |
898 // C---------FLOOR(X) is the largest integer algebraically less than | |
899 // C or equal to X; that is, the unfuzzy FLOOR function. | |
900 | |
901 // DINT (X) = X - DMOD (X, 1.0); | |
902 // FLOOR (X) = DINT (X) - DMOD (2.0 + DSIGN (1.0, X), 3.0); | |
903 | |
904 // C---------Hagerty's FL5 function follows... | |
905 | |
906 double q = 1.0; | |
907 | |
908 if (x < 0.0) | |
909 q = 1.0 - ct; | |
910 | |
911 double rmax = q / (2.0 - ct); | |
912 | |
21942
aab79a1885cc
limit gnulib headers to liboctave/wrappers directory
John W. Eaton <jwe@octave.org>
parents:
21782
diff
changeset
|
913 double t1 = 1.0 + std::floor (x); |
1546 | 914 t1 = (ct / q) * (t1 < 0.0 ? -t1 : t1); |
23450
855122b993da
maint: Wrap tertiary operator in parentheses "(COND ? x : y)".
Rik <rik@octave.org>
parents:
23443
diff
changeset
|
915 t1 = (rmax < t1 ? rmax : t1); |
855122b993da
maint: Wrap tertiary operator in parentheses "(COND ? x : y)".
Rik <rik@octave.org>
parents:
23443
diff
changeset
|
916 t1 = (ct > t1 ? ct : t1); |
21942
aab79a1885cc
limit gnulib headers to liboctave/wrappers directory
John W. Eaton <jwe@octave.org>
parents:
21782
diff
changeset
|
917 t1 = std::floor (x + t1); |
1546 | 918 |
1555 | 919 if (x <= 0.0 || (t1 - x) < rmax) |
1546 | 920 return t1; |
921 else | |
922 return t1 - 1.0; | |
923 } | |
924 | |
3753 | 925 static inline bool |
17769
49a5a4be04a1
maint: Use GNU style coding conventions for code in liboctave/
Rik <rik@octave.org>
parents:
17744
diff
changeset
|
926 teq (double u, double v, |
49a5a4be04a1
maint: Use GNU style coding conventions for code in liboctave/
Rik <rik@octave.org>
parents:
17744
diff
changeset
|
927 double ct = 3.0 * std::numeric_limits<double>::epsilon ()) |
3753 | 928 { |
25812
fc74c8d2a584
Clean up liboctave Range implementation.
Rik <rik@octave.org>
parents:
25808
diff
changeset
|
929 double tu = std::abs (u); |
fc74c8d2a584
Clean up liboctave Range implementation.
Rik <rik@octave.org>
parents:
25808
diff
changeset
|
930 double tv = std::abs (v); |
3753 | 931 |
25812
fc74c8d2a584
Clean up liboctave Range implementation.
Rik <rik@octave.org>
parents:
25808
diff
changeset
|
932 return std::abs (u - v) < ((tu > tv ? tu : tv) * ct); |
3753 | 933 } |
934 | |
5275 | 935 octave_idx_type |
20228
00cf2847355d
Deprecate Array::nelem() and Range::nelem() in favour of ::numel().
Carnë Draug <carandraug@octave.org>
parents:
19697
diff
changeset
|
936 Range::numel_internal (void) const |
1360 | 937 { |
5275 | 938 octave_idx_type retval = -1; |
3 | 939 |
28592
e70c859c4bff
allow infinite limits on for loop ranges (bug #45143)
John W. Eaton <jwe@octave.org>
parents:
28524
diff
changeset
|
940 if (! octave::math::isfinite (m_base) || ! octave::math::isfinite (m_inc) |
e70c859c4bff
allow infinite limits on for loop ranges (bug #45143)
John W. Eaton <jwe@octave.org>
parents:
28524
diff
changeset
|
941 || octave::math::isnan (m_limit)) |
e70c859c4bff
allow infinite limits on for loop ranges (bug #45143)
John W. Eaton <jwe@octave.org>
parents:
28524
diff
changeset
|
942 retval = -2; |
e70c859c4bff
allow infinite limits on for loop ranges (bug #45143)
John W. Eaton <jwe@octave.org>
parents:
28524
diff
changeset
|
943 else if (octave::math::isinf (m_limit) |
e70c859c4bff
allow infinite limits on for loop ranges (bug #45143)
John W. Eaton <jwe@octave.org>
parents:
28524
diff
changeset
|
944 && ((m_inc > 0 && m_limit > 0) |
e70c859c4bff
allow infinite limits on for loop ranges (bug #45143)
John W. Eaton <jwe@octave.org>
parents:
28524
diff
changeset
|
945 || (m_inc < 0 && m_limit < 0))) |
e70c859c4bff
allow infinite limits on for loop ranges (bug #45143)
John W. Eaton <jwe@octave.org>
parents:
28524
diff
changeset
|
946 retval = std::numeric_limits<octave_idx_type>::max () - 1; |
e70c859c4bff
allow infinite limits on for loop ranges (bug #45143)
John W. Eaton <jwe@octave.org>
parents:
28524
diff
changeset
|
947 else if (m_inc == 0 |
e70c859c4bff
allow infinite limits on for loop ranges (bug #45143)
John W. Eaton <jwe@octave.org>
parents:
28524
diff
changeset
|
948 || (m_limit > m_base && m_inc < 0) |
e70c859c4bff
allow infinite limits on for loop ranges (bug #45143)
John W. Eaton <jwe@octave.org>
parents:
28524
diff
changeset
|
949 || (m_limit < m_base && m_inc > 0)) |
3858 | 950 { |
951 retval = 0; | |
952 } | |
953 else | |
954 { | |
15220
61822c866ba1
use std::numeric_limits<T>::epsilon in C++ code
John W. Eaton <jwe@octave.org>
parents:
14138
diff
changeset
|
955 double ct = 3.0 * std::numeric_limits<double>::epsilon (); |
3 | 956 |
28402
09a3c0e91e6e
style update for Range class
John W. Eaton <jwe@octave.org>
parents:
28004
diff
changeset
|
957 double tmp = tfloor ((m_limit - m_base + m_inc) / m_inc, ct); |
3858 | 958 |
28592
e70c859c4bff
allow infinite limits on for loop ranges (bug #45143)
John W. Eaton <jwe@octave.org>
parents:
28524
diff
changeset
|
959 octave_idx_type n_elt = (tmp > 0.0 |
e70c859c4bff
allow infinite limits on for loop ranges (bug #45143)
John W. Eaton <jwe@octave.org>
parents:
28524
diff
changeset
|
960 ? static_cast<octave_idx_type> (tmp) : 0); |
398 | 961 |
25812
fc74c8d2a584
Clean up liboctave Range implementation.
Rik <rik@octave.org>
parents:
25808
diff
changeset
|
962 // If the final element that we would compute for the range is equal to |
fc74c8d2a584
Clean up liboctave Range implementation.
Rik <rik@octave.org>
parents:
25808
diff
changeset
|
963 // the limit of the range, or is an adjacent floating point number, |
fc74c8d2a584
Clean up liboctave Range implementation.
Rik <rik@octave.org>
parents:
25808
diff
changeset
|
964 // accept it. Otherwise, try a range with one fewer element. If that |
fc74c8d2a584
Clean up liboctave Range implementation.
Rik <rik@octave.org>
parents:
25808
diff
changeset
|
965 // fails, try again with one more element. |
3858 | 966 // |
25812
fc74c8d2a584
Clean up liboctave Range implementation.
Rik <rik@octave.org>
parents:
25808
diff
changeset
|
967 // I'm not sure this is very good, but it seems to work better than just |
fc74c8d2a584
Clean up liboctave Range implementation.
Rik <rik@octave.org>
parents:
25808
diff
changeset
|
968 // using tfloor as above. For example, without it, the expression |
fc74c8d2a584
Clean up liboctave Range implementation.
Rik <rik@octave.org>
parents:
25808
diff
changeset
|
969 // 1.8:0.05:1.9 fails to produce the expected result of [1.8, 1.85, 1.9]. |
3753 | 970 |
28402
09a3c0e91e6e
style update for Range class
John W. Eaton <jwe@octave.org>
parents:
28004
diff
changeset
|
971 if (! teq (m_base + (n_elt - 1) * m_inc, m_limit)) |
10314
07ebe522dac2
untabify liboctave C++ sources
John W. Eaton <jwe@octave.org>
parents:
10158
diff
changeset
|
972 { |
28402
09a3c0e91e6e
style update for Range class
John W. Eaton <jwe@octave.org>
parents:
28004
diff
changeset
|
973 if (teq (m_base + (n_elt - 2) * m_inc, m_limit)) |
10314
07ebe522dac2
untabify liboctave C++ sources
John W. Eaton <jwe@octave.org>
parents:
10158
diff
changeset
|
974 n_elt--; |
28402
09a3c0e91e6e
style update for Range class
John W. Eaton <jwe@octave.org>
parents:
28004
diff
changeset
|
975 else if (teq (m_base + n_elt * m_inc, m_limit)) |
10314
07ebe522dac2
untabify liboctave C++ sources
John W. Eaton <jwe@octave.org>
parents:
10158
diff
changeset
|
976 n_elt++; |
07ebe522dac2
untabify liboctave C++ sources
John W. Eaton <jwe@octave.org>
parents:
10158
diff
changeset
|
977 } |
3858 | 978 |
28592
e70c859c4bff
allow infinite limits on for loop ranges (bug #45143)
John W. Eaton <jwe@octave.org>
parents:
28524
diff
changeset
|
979 retval = ((n_elt < std::numeric_limits<octave_idx_type>::max ()) |
e70c859c4bff
allow infinite limits on for loop ranges (bug #45143)
John W. Eaton <jwe@octave.org>
parents:
28524
diff
changeset
|
980 ? n_elt : -1); |
3753 | 981 } |
982 | |
3858 | 983 return retval; |
3 | 984 } |
21321
79a51b7e00b6
eliminate some copy and paste code fragments in the Range class
John W. Eaton <jwe@octave.org>
parents:
21301
diff
changeset
|
985 |
79a51b7e00b6
eliminate some copy and paste code fragments in the Range class
John W. Eaton <jwe@octave.org>
parents:
21301
diff
changeset
|
986 double |
79a51b7e00b6
eliminate some copy and paste code fragments in the Range class
John W. Eaton <jwe@octave.org>
parents:
21301
diff
changeset
|
987 Range::limit_internal (void) const |
79a51b7e00b6
eliminate some copy and paste code fragments in the Range class
John W. Eaton <jwe@octave.org>
parents:
21301
diff
changeset
|
988 { |
28592
e70c859c4bff
allow infinite limits on for loop ranges (bug #45143)
John W. Eaton <jwe@octave.org>
parents:
28524
diff
changeset
|
989 double new_limit = m_inc > 0 ? max () : min (); |
21321
79a51b7e00b6
eliminate some copy and paste code fragments in the Range class
John W. Eaton <jwe@octave.org>
parents:
21301
diff
changeset
|
990 |
25812
fc74c8d2a584
Clean up liboctave Range implementation.
Rik <rik@octave.org>
parents:
25808
diff
changeset
|
991 // If result must be an integer then force the new_limit to be one. |
25808
dc47c9e48801
Guarantee an integer for max of Range when base and increment are ints (bug #46859).
Rik <rik@octave.org>
parents:
25438
diff
changeset
|
992 if (all_elements_are_ints ()) |
25812
fc74c8d2a584
Clean up liboctave Range implementation.
Rik <rik@octave.org>
parents:
25808
diff
changeset
|
993 new_limit = std::round (new_limit); |
25808
dc47c9e48801
Guarantee an integer for max of Range when base and increment are ints (bug #46859).
Rik <rik@octave.org>
parents:
25438
diff
changeset
|
994 |
25812
fc74c8d2a584
Clean up liboctave Range implementation.
Rik <rik@octave.org>
parents:
25808
diff
changeset
|
995 return new_limit; |
21321
79a51b7e00b6
eliminate some copy and paste code fragments in the Range class
John W. Eaton <jwe@octave.org>
parents:
21301
diff
changeset
|
996 } |
79a51b7e00b6
eliminate some copy and paste code fragments in the Range class
John W. Eaton <jwe@octave.org>
parents:
21301
diff
changeset
|
997 |
79a51b7e00b6
eliminate some copy and paste code fragments in the Range class
John W. Eaton <jwe@octave.org>
parents:
21301
diff
changeset
|
998 void |
79a51b7e00b6
eliminate some copy and paste code fragments in the Range class
John W. Eaton <jwe@octave.org>
parents:
21301
diff
changeset
|
999 Range::init (void) |
79a51b7e00b6
eliminate some copy and paste code fragments in the Range class
John W. Eaton <jwe@octave.org>
parents:
21301
diff
changeset
|
1000 { |
28402
09a3c0e91e6e
style update for Range class
John W. Eaton <jwe@octave.org>
parents:
28004
diff
changeset
|
1001 m_numel = numel_internal (); |
21321
79a51b7e00b6
eliminate some copy and paste code fragments in the Range class
John W. Eaton <jwe@octave.org>
parents:
21301
diff
changeset
|
1002 |
28592
e70c859c4bff
allow infinite limits on for loop ranges (bug #45143)
John W. Eaton <jwe@octave.org>
parents:
28524
diff
changeset
|
1003 if (! octave::math::isinf (m_limit)) |
e70c859c4bff
allow infinite limits on for loop ranges (bug #45143)
John W. Eaton <jwe@octave.org>
parents:
28524
diff
changeset
|
1004 m_limit = limit_internal (); |
21321
79a51b7e00b6
eliminate some copy and paste code fragments in the Range class
John W. Eaton <jwe@octave.org>
parents:
21301
diff
changeset
|
1005 } |