def test_special_printers(): class IntervalPrinter(LambdaPrinter): """Use ``lambda`` printer but print numbers as ``mpi`` intervals. """ def _print_Integer(self, expr): return "mpi('%s')" % super(IntervalPrinter, self)._print_Integer(expr) def _print_Rational(self, expr): return "mpi('%s')" % super(IntervalPrinter, self)._print_Rational(expr) def intervalrepr(expr): return IntervalPrinter().doprint(expr) expr = sympy.sqrt(sympy.sqrt(2) + sympy.sqrt(3)) + sympy.S(1) / 2 func0 = lambdify((), expr, modules="mpmath", printer=intervalrepr) func1 = lambdify((), expr, modules="mpmath", printer=IntervalPrinter) func2 = lambdify((), expr, modules="mpmath", printer=IntervalPrinter()) mpi = type(mpmath.mpi(1, 2)) assert isinstance(func0(), mpi) assert isinstance(func1(), mpi) assert isinstance(func2(), mpi)
def arithmeticalOperationX(self, i2, alpha, operation): """ Wrapper class for performing Fuzzy Arithmetic on X-mu Functions using SymPy/MPI. Primarily used as a private class, but could be used publicly. @param i2 the target sympy formula. @param alpha the level on which to perform the operation. @param operation One of the following: +, -, *, /, ** """ res = self.get_xequals().subs(ALPHA, float(alpha)) res2 = i2.get_xequals().subs(ALPHA, float(alpha)) if (not res.is_EmptySet) and (not res2.is_EmptySet): if (type(res) != FiniteSet) and (type(res2) != FiniteSet): result = eval("res.to_mpi() " + operation + " res2.to_mpi()") else: res_mpi = mpmath.mpi(res.inf, res.sup) res2_mpi = mpmath.mpi(res2.inf, res2.sup) result = eval("res_mpi " + operation + " res2_mpi") return BasicXmu(self.u, Interval(float(mpmath.mpf(result.a)), float(mpmath.mpf(result.b)))) else: return None
def test_special_printers(): class IntervalPrinter(LambdaPrinter): """Use ``lambda`` printer but print numbers as ``mpi`` intervals. """ def _print_Integer(self, expr): return "mpi('%s')" % super(IntervalPrinter, self)._print_Integer(expr) def _print_Rational(self, expr): return "mpi('%s')" % super(IntervalPrinter, self)._print_Rational(expr) def intervalrepr(expr): return IntervalPrinter().doprint(expr) expr = sympy.sqrt(sympy.sqrt(2) + sympy.sqrt(3)) + sympy.S(1)/2 func0 = lambdify((), expr, modules="mpmath", printer=intervalrepr) func1 = lambdify((), expr, modules="mpmath", printer=IntervalPrinter) func2 = lambdify((), expr, modules="mpmath", printer=IntervalPrinter()) mpi = type(mpmath.mpi(1, 2)) assert isinstance(func0(), mpi) assert isinstance(func1(), mpi) assert isinstance(func2(), mpi)
def to_mpi(self, prec=53): return mpi(mpf(self.start.evalf(prec)), mpf(self.end.evalf(prec)))
def test_interval_to_mpi(): assert Interval(0, 1).to_mpi() == mpi(0, 1) assert Interval(0, 1, True, False).to_mpi() == mpi(0, 1) assert type(Interval(0, 1).to_mpi()) == type(mpi(0, 1))
def test_D12(): assert (mpi(-4, 2) * x + mpi(1, 3)) ** 2 == mpi(-8, 16)*x**2 + mpi(-24, 12)*x + mpi(1, 9)
def _eval_evalf(self, prec): return mpi(self.start.evalf(prec), self.end.evalf(prec))
def test_D12(): assert (mpi(-4, 2) * x + mpi(1, 3))**2 == mpi(-8, 16) * x**2 + mpi(-24, 12) * x + mpi(1, 9)
def test_interval_evalf(): assert Interval(0, 1).evalf() == mpi(0, 1) assert Interval(0, 1, True, False).evalf() == mpi(0, 1)
def test_interval_to_mpi(): raises(SymPyDeprecationWarning, "Interval(0, 1).to_mpi()") assert Interval(0, 1).to_mpi() == mpi(0, 1) assert Interval(0, 1, True, False).to_mpi() == mpi(0, 1) assert type(Interval(0, 1).to_mpi()) == type(mpi(0, 1))
def _eval_evalf(self, prec): return mpi(mpf(self.start.evalf(prec)), mpf(self.end.evalf(prec)))