Ticket #12074: trac_12074-nth_root.patch

File trac_12074-nth_root.patch, 2.6 KB (added by burcin, 10 years ago)
  • sage/functions/all.py

    # HG changeset patch
    # User Burcin Erocal <burcin@erocal.org>
    # Date 1322135515 -3600
    # Node ID 5a8d249d660dea4d8d1b1cd08b30eb6b4d11f4fa
    # Parent  962a20a747e5fcde890a2784a9080e621cb5b296
    trac 12074: add symbolic nth_root() function.
    
    diff --git a/sage/functions/all.py b/sage/functions/all.py
    a b  
    1616
    1717
    1818from other import ( ceil, floor, gamma, psi, factorial,
    19                     abs_symbolic, erf, sqrt,
     19                    abs_symbolic, erf, sqrt, nth_root,
    2020                    gamma_inc, incomplete_gamma,
    2121                    real_part, real,
    2222                    imag_part, imag, imaginary, conjugate)
  • sage/functions/other.py

    diff --git a/sage/functions/other.py b/sage/functions/other.py
    a b  
    13131313        {'__call__': staticmethod(sqrt),
    13141314            '__setstate__': lambda x, y: None})
    13151315
     1316
     1317class RealNthRoot(BuiltinFunction):
     1318    def __init__(self):
     1319        BuiltinFunction.__init__(self, "real_nth_root", nargs=2)
     1320
     1321    def _eval_(self, base, exp):
     1322        if (not isinstance(base, Expression) and is_inexact(base)) or \
     1323                (not isinstance(exp, Expression) and is_inexact(exp)):
     1324            self._evalf_(base, exp, parent=parent(base))
     1325
     1326    def _evalf_(self, base, exp, parent=None):
     1327        if isinstance(base, float):
     1328            return RR(base).nth_root(exp)
     1329        try:
     1330            return base.nth_root(exp)
     1331        except AttributeError:
     1332            return base**(1/exp)
     1333        return parent(base)**parent(exp)
     1334
     1335    def _power_(self, base, exp, power_param=None):
     1336        """
     1337        EXAMPLES::
     1338
     1339            sage: v = nth_root(x,3)
     1340            sage: v^5
     1341            real_nth_root(x, 3/5)
     1342        """
     1343        return self(base, exp/power_param)
     1344
     1345    def _tderivative_(self, base, exp, diff_param=None):
     1346        """
     1347        Return the derivative of this function in the variable given in
     1348        diff_param.
     1349
     1350        We cannot use _derivative_() here since the chain rule applied by
     1351        pynac does not know that the exponent is actually 1/exp.
     1352
     1353        EXAMPLES::
     1354
     1355            sage: v = nth_root(x,3)
     1356            sage: v.diff(x)
     1357            1/3*real_nth_root(x, -3/2)
     1358        """
     1359        if isinstance(base, Expression) and base.has(diff_param) and \
     1360                not (isinstance(exp, Expression) and exp.has(diff_param)):
     1361            return 1/exp*self(base, exp/(1-exp))*base.diff(diff_param)
     1362        raise NotImplementedError
     1363
     1364nth_root = RealNthRoot()
     1365
     1366
    13161367############################
    13171368# Real and Imaginary Parts #
    13181369############################