fixed the doctest bug
[sympy.git] / sympy / functions / elementary / miscellaneous.py
blob3a8a64f228d6f7dfa5b15dc08678e9315b158668
2 from sympy.core.basic import Basic, S, cache_it, cache_it_immutable
3 from sympy.core.function import Lambda, SingleValuedFunction
5 ###############################################################################
6 ############################# SQUARE ROOT FUNCTION ############################
7 ###############################################################################
9 class sqrt(SingleValuedFunction):
11 nofargs = 1
13 def fdiff(self, argindex=1):
14 if argindex == 1:
15 s = Basic.Symbol('x',dummy=True)
16 return Lambda(S.Half * s**(-S.Half),s)
17 else:
18 raise ArgumentIndexError(self, argindex)
20 def inverse(self, argindex=1):
21 s = Basic.Symbol('x', dummy=True)
22 return Lambda(s**2, s)
24 def tostr(self, p=None):
25 return "sqrt(%s)" % self[0]
27 @classmethod
28 def _eval_apply(self, arg):
29 arg = Basic.sympify(arg)
31 if isinstance(arg, Basic.Number):
32 if isinstance(arg, Basic.NaN):
33 return S.NaN
34 if isinstance(arg, Basic.Infinity):
35 return S.Infinity
36 if isinstance(arg, Basic.NegativeInfinity):
37 return S.ImaginaryUnit * S.Infinity
38 if isinstance(arg, Basic.Rational):
39 factors = arg.factors()
40 sqrt_factors = {}
41 eval_factors = {}
42 n = Basic.Integer(1)
43 for k,v in factors.items():
44 n *= Basic.Integer(k) ** (v//2)
45 if v % 2:
46 n *= Basic.Integer(k) ** S.Half
47 return n
48 return arg ** S.Half
49 if arg.is_nonnegative:
50 coeff, terms = arg.as_coeff_terms()
51 if not isinstance(coeff, Basic.One):
52 return self(coeff) * self(Basic.Mul(*terms))
53 base, exp = arg.as_base_exp()
54 if isinstance(exp, Basic.Number):
55 if exp == 2:
56 return Basic.abs(base)
57 return base ** (exp/2)
59 def _eval_apply_power(self, arg, exp):
60 if isinstance(exp, Basic.Number):
61 return arg ** (exp/2)
63 def _eval_apply_subs(self, x, old, new):
64 base, exp = old.as_base_exp()
65 if base==x:
66 return new ** (exp/2)
68 def _eval_is_zero(self):
69 return isinstance(self[0], Basic.Zero)
71 def as_base_exp(self):
72 return self[0], S.Half
74 ###############################################################################
75 ############################# MINIMUM and MAXIMUM #############################
76 ###############################################################################
78 class max_(SingleValuedFunction):
80 nofargs = 2
82 def _eval_apply(self, x, y):
83 if isinstance(x, Basic.Number) and isinstance(y, Basic.Number):
84 return max(x, y)
85 if x.is_positive:
86 if y.is_negative:
87 return x
88 if y.is_positive:
89 if x.is_unbounded:
90 if y.is_unbounded:
91 return
92 return x
93 elif x.is_negative:
94 if y.is_negative:
95 if y.is_unbounded:
96 if x.is_unbounded:
97 return
98 return x
100 class min_(SingleValuedFunction):
102 nofargs = 2
104 def _eval_apply(self, x, y):
105 if isinstance(x, Basic.Number) and isinstance(y, Basic.Number):
106 return min(x, y)