/usr/share/octave/packages/interval-3.1.0/midrad.m is in octave-interval 3.1.0-5.
This file is owned by root:root, with mode 0o644.
The actual contents of the file can be viewed below.
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 113 114 115 116 117 118 119 120 121 122 123 124 125 126 127 128 129 130 131 132 133 134 135 136 137 138 139 140 141 142 143 144 145 146 147 148 149 150 151 152 153 154 155 156 157 158 159 160 161 162 163 164 165 166 167 168 169 170 171 172 173 174 175 176 177 | ## Copyright 2015-2016 Oliver Heimlich
## Copyright 2017 Joel Dahne
##
## This program is free software; you can redistribute it and/or modify
## it under the terms of the GNU General Public License as published by
## the Free Software Foundation; either version 3 of the License, or
## (at your option) any later version.
##
## This program is distributed in the hope that it will be useful,
## but WITHOUT ANY WARRANTY; without even the implied warranty of
## MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
## GNU General Public License for more details.
##
## You should have received a copy of the GNU General Public License
## along with this program; if not, see <http://www.gnu.org/licenses/>.
## -*- texinfo -*-
## @documentencoding UTF-8
## @deftypefun {@var{I} =} midrad (@var{M}, @var{R})
## @deftypefunx {@var{I} =} midrad (@var{M})
## @deftypefunx {@var{I} =} midrad ()
## @deftypefunx {[@var{M}, @var{R}] =} midrad (@var{I})
##
## Create an interval enclosure @var{I} for [@var{M}-@var{R}, @var{M}+@var{R}].
##
## With two output arguments, compute a rigorous midpoint @var{M} and
## radius @var{R} for interval @var{I}.
##
## Without input parameters, return the empty interval. With only one input
## parameter, the radius @var{R} defaults to zero.
##
## Parameters can be simple numbers, intervals or interval literals as strings.
## If needed, broadcasting is performed.
##
## The result is not guaranteed to be tightest if parameters are given as
## strings. This is due to intermediate results. The infsupdec constructor
## with interval literals in uncertain form @code{m?ruE} can instead be used to
## create tight enclosures of decimal numbers with a radius.
##
## Accuracy (with one output argument): The result is an accurate enclosure.
## The result is tightest if @var{M} and @var{R} are floating-point numbers or
## intervals.
##
## Accuracy (with two output arguments): @var{M} is the interval's midpoint
## in binary64 precision, rounded to nearest and ties to even. The returned
## radius @var{R} will make a tight enclosure of the interval together with
## @var{M}. That is, @var{R} is the smallest binary64 number, which will make
## [@var{M}-@var{R}, @var{M}+@var{R}] enclose the interval @var{I}.
##
## @example
## @group
## midrad (42, 3)
## @result{} ans = [39, 45]_com
## midrad (0, inf)
## @result{} ans = [Entire]_dac
## midrad ("1.1", "0.1")
## @result{} ans ⊂ [0.99999, 1.2001]_com
## midrad ("25", "3/7")
## @result{} ans ⊂ [24.571, 25.429]_com
## @end group
## @end example
## @seealso{@@infsupdec/infsupdec, hull, @@infsupdec/mid, @@infsupdec/rad}
## @end deftypefun
## Author: Oliver Heimlich
## Keywords: interval
## Created: 2015-03-06
function [m, r] = midrad (m, r)
if (nargout > 1)
warning ("off", "id=interval:ImplicitPromote", "local");
endif
switch nargin
case 0
i = infsupdec ();
case 1
if (nargout == 2 && isa (m, "infsup"))
i = m;
else
i = infsupdec (m);
endif
case 2
if (isfloat (m) && isreal (m) && ...
isfloat (r) && isreal (r))
## Simple case: m and r are binary64 numbers
l = mpfr_function_d ('minus', -inf, m, r);
u = mpfr_function_d ('plus', +inf, m, r);
i = infsupdec (l, u);
else
## Complicated case: m and r are strings or other types
m = infsupdec (m);
if (not (isa (r, "infsup")))
## [-inf, r] should make a valid interval, unless r == -inf
## Intersection with non-negative numbers ensures that we
## return [Empty] if r < 0.
if (isfloat (r))
r(r == -inf) = nan;
endif
r = intersect (infsupdec (-inf, r), infsupdec (0, inf));
## Fix decoration, since intersect would return “trv” at best.
legal_radius = not (isempty (r));
r(legal_radius) = newdec (intervalpart (r(legal_radius)));
endif
if (isa (r, "infsupdec"))
dec_r = decorationpart (r);
else
dec_r = {"com"};
endif
sup_r = sup (r);
sup_r(sup_r < 0) = -inf;
r = infsupdec (-sup_r, sup_r, dec_r);
i = m + r;
endif
otherwise
print_usage ();
endswitch
switch nargout
case {0, 1}
m = i;
case 2
m = mid (i);
## The midpoint is rounded to nearest and the radius
## must cover both boundaries
r1 = mpfr_function_d ('minus', +inf, m, inf (i));
r2 = mpfr_function_d ('minus', +inf, sup (i), m);
r = max (r1, r2);
otherwise
print_usage ();
endswitch
endfunction
%!assert (isempty (midrad ()));
%!warning id=interval:UndefinedOperation
%! assert (isnai (midrad (0, -inf)));
%!warning id=interval:UndefinedOperation
%! assert (isnai (midrad (0, -.1)));
%!warning id=interval:UndefinedOperation
%! assert (isnai (midrad (0, "-.1")));
%!warning id=interval:UndefinedOperation
%! assert (isnai (midrad (0, infsup("-.1"))));
%!assert (isequal (midrad ("pi"), infsupdec ("pi")));
%!warning id=interval:ImplicitPromote
%! assert (isequal (midrad (infsup (2), 2), infsupdec (0, 4)));
%!assert (isequal (midrad (2, infsup (2)), infsupdec (0, 4)));
%!warning id=interval:ImplicitPromote
%! assert (isequal (midrad (infsup (2), infsup (2)), infsupdec (0, 4)));
%!assert (isequal (midrad (2, infsupdec (2)), infsupdec (0, 4)));
%!assert (isequal (midrad (infsupdec (2), 2), infsupdec (0, 4)));
%!warning id=interval:ImplicitPromote
%! assert (isequal (midrad (infsup (2), infsupdec (2)), infsupdec (0, 4)));
%!assert (isequal (midrad (infsupdec (2), infsup (2)), infsupdec (0, 4)));
%!assert (isequal (midrad (infsupdec (2), infsupdec (2)), infsupdec (0, 4)));
%!assert (isequal (midrad (1, magic (3)), infsupdec ([-7, 0, -5; -2, -4, -6; -3, -8, -1], [9, 2, 7; 4, 6, 8; 5, 10, 3])));
%!assert (isequal (midrad (magic (3), 1), infsupdec ([7, 0, 5; 2, 4, 6; 3, 8, 1], [9, 2, 7; 4, 6, 8; 5, 10, 3])));
%!# from the documentation string
%!assert (isequal (midrad (42, 3), infsupdec (39, 45)));
%!assert (isequal (midrad (0, inf), entire ()));
%!assert (isequal (midrad ("1.1", "0.1"), infsupdec (1 - eps, "1.2")));
%!# N-dimensional arrays
%!assert (isequal (midrad (zeros (2, 2, 2), ones (2, 2, 2)), infsupdec (-ones (2, 2, 2), ones (2, 2, 2))));
%!assert (isequal (midrad (zeros (2, 2, 2), 1), infsupdec (-ones (2, 2, 2), ones (2, 2, 2))));
%!assert (isequal (midrad (0, ones (2, 2, 2)), infsupdec (-ones (2, 2, 2), ones (2, 2, 2))));
%!test
%! [M, R] = midrad (infsupdec (-ones (2, 2, 2), ones (2, 2, 2)));
%! assert (M, zeros (2, 2, 2));
%! assert (R, ones (2, 2, 2));
|