/usr/share/octave/packages/interval-2.1.0/@infsup/nthroot.m is in octave-interval 2.1.0-2.
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 178 179 180 181 182 183 184 185 186 187 188 189 190 191 192 193 194 195 196 197 198 199 200 201 202 203 204 | ## Copyright 2015-2016 Oliver Heimlich
##
## 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
## @defmethod {@@infsup} nthroot (@var{X}, @var{N})
##
## Compute the real n-th root of @var{X}.
##
## Accuracy: The result is a valid enclosure. The result is a tight
## enclosure for @var{n} ≥ -2. The result also is a tight enclosure if the
## reciprocal of @var{n} can be computed exactly in double-precision.
## @seealso{@@infsup/pown, @@infsup/pownrev, @@infsup/realsqrt, @@infsup/cbrt}
## @end defmethod
## Author: Oliver Heimlich
## Keywords: interval
## Created: 2015-02-20
function x = nthroot (x, n)
if (nargin ~= 2)
print_usage ();
return
endif
if (not (isa (x, "infsup")))
x = infsup (x);
endif
if (not (isnumeric (n)) || fix (n) ~= n)
error ("interval:InvalidOperand", "nthroot: degree is not an integer");
endif
even = mod (n, 2) == 0;
if (even)
x = intersect (x, infsup (0, inf));
endif
switch sign (n)
case +1
emptyresult = isempty (x);
l = mpfr_function_d ('nthroot', -inf, x.inf, n);
u = mpfr_function_d ('nthroot', +inf, x.sup, n);
l(emptyresult) = inf;
u(emptyresult) = -inf;
l(l == 0) = -0;
x.inf = l;
x.sup = u;
case -1
emptyresult = isempty (x) ...
| (x.sup <= 0 & even) | (x.inf == 0 & x.sup == 0);
if (even)
l = zeros (size (x.inf));
u = inf (size (x.inf));
select = x.inf > 0 & isfinite (x.inf);
if (any (select(:)))
u(select) = invrootrounded (x.inf(select), -n, +inf);
endif
select = x.sup > 0 & isfinite (x.sup);
if (any (select(:)))
l(select) = invrootrounded (x.sup(select), -n, -inf);
endif
l(emptyresult) = inf;
u(emptyresult) = -inf;
l(l == 0) = -0;
x.inf = l;
x.sup = u;
else # uneven
l = zeros (size (x.inf));
u = inf (size (x.inf));
select = x.inf > 0 & isfinite (x.inf);
if (any (select(:)))
u(select) = invrootrounded (x.inf(select), -n, +inf);
endif
select = x.sup > 0 & isfinite (x.sup);
if (any (select(:)))
l(select) = invrootrounded (x.sup(select), -n, -inf);
endif
notpositive = x.sup <= 0;
l(emptyresult | notpositive) = inf;
u(emptyresult | notpositive) = -inf;
l(l == 0) = -0;
# this is only the positive part
pos = x;
pos.inf = l;
pos.sup = u;
l = zeros (size (x.inf));
u = inf (size (x.inf));
select = x.sup < 0 & isfinite (x.sup);
if (any (select(:)))
u(select) = invrootrounded (-x.sup(select), -n, +inf);
endif
select = x.inf < 0 & isfinite (x.inf);
if (any (select(:)))
l(select) = invrootrounded (-x.inf(select), -n, -inf);
endif
notnegative = x.inf >= 0;
l(emptyresult | notnegative) = inf;
u(emptyresult | notnegative) = -inf;
u(u == 0) = +0;
neg = x;
neg.inf = -u;
neg.sup = -l;
x = union (pos, neg);
endif
otherwise
error ("interval:InvalidOperand", "nthroot: degree must not be zero");
endswitch
endfunction
function x = invrootrounded (z, n, direction)
## We cannot compute the inverse of the n-th root of z in a single step.
## Thus, we use three different ways for computation, each of which has an
## intermediate result with possible rounding errors and can't guarantee to
## produce a correctly rounded result.
## When we finally merge the 3 results, it is still not guaranteed to be
## correctly rounded. However, chances are good that one of the three ways
## produced a “relatively good” result.
##
## x1: z ^ (- 1 / n)
## x2: 1 / root (z, n)
## x3: root (1 / z, n)
inv_n = 1 ./ infsup (n);
if (direction > 0)
x1 = z;
select = z > 1;
x1(select) = mpfr_function_d ('pow', direction, z(select), -inv_n.inf);
select = z < 1;
x1(select) = mpfr_function_d ('pow', direction, z(select), -inv_n.sup);
else
x1 = z;
select = z > 1;
x1(select) = mpfr_function_d ('pow', direction, z(select), -inv_n.sup);
select = z < 1;
x1(select) = mpfr_function_d ('pow', direction, z(select), -inv_n.inf);
endif
if (issingleton (inv_n))
## We are lucky: The result is correctly rounded
x = x1;
return
endif
x2 = mpfr_function_d ('rdivide', direction, 1, ...
mpfr_function_d ('nthroot', -direction, z, n));
x3 = mpfr_function_d ('nthroot', direction, ...
mpfr_function_d ('rdivide', direction, 1, z), n);
## Choose the most accurate result
if (direction > 0)
x = min (min (x1, x2), x3);
else
x = max (max (x1, x2), x3);
endif
endfunction
%!assert (nthroot (infsup (25, 36), 2) == infsup (5, 6));
%!# correct use of signed zeros
%!test
%! x = nthroot (infsup (0), 2);
%! assert (signbit (inf (x)));
%! assert (not (signbit (sup (x))));
%!test
%! x = nthroot (infsup (0, inf), -2);
%! assert (signbit (inf (x)));
%!test
%! x = nthroot (infsup (0, inf), -3);
%! assert (signbit (inf (x)));
|