Ejemplo n.º 1
0
    def apply_N(self, precision, evaluation):
        'N[E, precision_]'

        try:
            d = get_precision(precision, evaluation)
        except PrecisionValueError:
            return

        if d is None:
            return MachineReal(math.e)
        else:
            return PrecisionReal(sympy.E.n(d))
Ejemplo n.º 2
0
    def get_constant(self, precision, evaluation, have_mpmath=False):
        try:
            d = get_precision(precision, evaluation)
        except PrecisionValueError:
            return

        sympy_fn = self.to_sympy()
        if d is None:
            result = self.get_mpmath_function() if have_mpmath else sympy_fn()
            return MachineReal(result)
        else:
            return PrecisionReal(sympy_fn.n(d))
Ejemplo n.º 3
0
    def apply_N(self, precision, evaluation):
        "N[Pi, precision_]"

        try:
            d = get_precision(precision, evaluation)
        except PrecisionValueError:
            return

        if d is None:
            return MachineReal(math.pi)
        else:
            return PrecisionReal(sympy.pi.n(d))
Ejemplo n.º 4
0
    def apply_N(self, precision, evaluation):
        "N[Catalan, precision_]"

        try:
            d = get_precision(precision, evaluation)
        except PrecisionValueError:
            return

        if d is None:
            return MachineReal(mpmath.catalan)
        else:
            return PrecisionReal(sympy.Catalan.n(d))
Ejemplo n.º 5
0
    def get_constant(self, precision, evaluation, preference=None):
        # first, determine the precision
        machine_d = int(0.30103 * machine_precision)
        d = None
        if precision:
            try:
                d = get_precision(precision, evaluation)
            except PrecisionValueError:
                pass

        if d is None:
            d = machine_d

        # If preference not especified, determine it
        # from the precision.
        if preference is None:
            if d <= machine_d:
                preference = "numpy"
            else:
                preference = "mpmath"
        # If preference is not valid, send a message and return.
        if not (preference in ("sympy", "numpy", "mpmath")):
            evaluation.message(
                f'{preference} not in ("sympy", "numpy", "mpmath")')
            return
        # Try to determine the numeric value
        value = None
        if preference == "mpmath" and not hasattr(self, "mpmath_name"):
            preference = "numpy"
        elif preference == "sympy" and not hasattr(self, "sympy_name"):
            preference = "numpy"

        if preference == "numpy" and not hasattr(self, "numpy_name"):
            if hasattr(self, "sympy_name"):
                preference = "sympy"
            elif hasattr(self, "mpmath_name"):
                preference = "mpmath"
            else:
                preference = ""
        if preference == "numpy":
            value = numpy_constant(self.numpy_name)
            if d == machine_d:
                return MachineReal(value)
        if preference == "sympy":
            value = sympy_constant(self.sympy_name, d + 2)
        if preference == "mpmath":
            value = mp_constant(self.mpmath_name, d * 2)
        if value:
            return PrecisionReal(sympy.Float(str(value), d))
        # If the value is not available, return none
        # and keep it unevaluated.
        return
Ejemplo n.º 6
0
    def apply_N(self, precision, evaluation):
        "N[Degree, precision_]"
        try:
            d = get_precision(precision, evaluation)
        except PrecisionValueError:
            return

        # FIXME: There are all sorts of interactions between in the trig functions,
        # that are expected to work out right. Until we have convertion between
        # mpmath and sympy worked out so that values can be made the to the same
        # precision and compared. we have to not use mpmath right now.
        # return self.get_constant(precision, evaluation, preference="mpmath")

        if d is None:
            return MachineReal(math.pi / 180)
        else:
            return PrecisionReal((sympy.pi / 180).n(d))
Ejemplo n.º 7
0
    def get_constant(self, precision, evaluation, preference="mpmath"):
        try:
            d = get_precision(precision, evaluation)
        except PrecisionValueError:
            return

        mpmath_name = self.mpmath_name
        if d is None:
            return MachineReal(mp_fn(mpmath_name))
        elif preference == "mpmath":
            result = mp_fn(mpmath_name, d)
        else:
            sympy_fn = self.to_sympy()
            try:
                result = sympy_fn.n(d)
            except:
                from trepan.api import debug; debug()
                pass
        return PrecisionReal(result)
Ejemplo n.º 8
0
    def get_constant(self, precision, evaluation, preference="sympy"):
        try:
            d = get_precision(precision, evaluation)
        except PrecisionValueError:
            return

        sympy_fn = self.to_sympy()
        if d is None:
            if hasattr(self, "mpmath_name"):
                # Prefer mpmath when precision is not given.
                result = mp_fn(self.mpmath_name)
            else:
                result = sympy_fn()
            return MachineReal(result)

        if preference == "sympy":
            result = sympy_fn_n(d)
        elif hasattr(self, "mpmath_name"):
            result = mp_fn(self.mpmath_name, d)

        return PrecisionReal(result)