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
):
13 def fdiff(self
, argindex
=1):
15 s
= Basic
.Symbol('x',dummy
=True)
16 return Lambda(S
.Half
* s
**(-S
.Half
),s
)
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]
28 def _eval_apply(self
, arg
):
29 arg
= Basic
.sympify(arg
)
31 if isinstance(arg
, Basic
.Number
):
32 if isinstance(arg
, Basic
.NaN
):
34 if isinstance(arg
, Basic
.Infinity
):
36 if isinstance(arg
, Basic
.NegativeInfinity
):
37 return S
.ImaginaryUnit
* S
.Infinity
38 if isinstance(arg
, Basic
.Rational
):
39 factors
= arg
.factors()
43 for k
,v
in factors
.items():
44 n
*= Basic
.Integer(k
) ** (v
//2)
46 n
*= Basic
.Integer(k
) ** 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
):
56 return Basic
.abs(base
)
57 return base
** (exp
/2)
59 def _eval_apply_power(self
, arg
, exp
):
60 if isinstance(exp
, Basic
.Number
):
63 def _eval_apply_subs(self
, x
, old
, new
):
64 base
, exp
= old
.as_base_exp()
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
):
82 def _eval_apply(self
, x
, y
):
83 if isinstance(x
, Basic
.Number
) and isinstance(y
, Basic
.Number
):
100 class min_(SingleValuedFunction
):
104 def _eval_apply(self
, x
, y
):
105 if isinstance(x
, Basic
.Number
) and isinstance(y
, Basic
.Number
):