[2/2] port factorials to new-style functions
[sympy.git] / sympy / functions / special / error_functions.py
blob637bd12963f3154a5af59378e635f6e134062ee6
2 from sympy.core.basic import Basic, S, cache_it, cache_it_immutable
3 from sympy.core.function import SingleValuedFunction
5 ###############################################################################
6 ################################ ERROR FUNCTION ###############################
7 ###############################################################################
9 class erf(SingleValuedFunction):
11 nofargs = 1
13 def fdiff(self, argindex=1):
14 if argindex == 1:
15 return 2*Basic.exp(-self[0]**2)/Basic.sqrt(S.Pi)
16 else:
17 raise ArgumentIndexError(self, argindex)
19 @classmethod
20 def _eval_apply_subs(cls, *args):
21 return
23 @classmethod
24 def _eval_apply(self, arg):
25 arg = Basic.sympify(arg)
27 if isinstance(arg, Basic.Number):
28 if isinstance(arg, Basic.NaN):
29 return S.NaN
30 elif isinstance(arg, Basic.Infinity):
31 return S.One
32 elif isinstance(arg, Basic.NegativeInfinity):
33 return S.NegativeOne
34 elif isinstance(arg, Basic.Zero):
35 return S.Zero
36 elif arg.is_negative:
37 return -self(-arg)
38 elif isinstance(arg, Basic.Mul):
39 coeff, terms = arg.as_coeff_terms()
41 if coeff.is_negative:
42 return -self(-arg)
44 @classmethod
45 @cache_it_immutable
46 def taylor_term(self, n, x, *previous_terms):
47 if n < 0 or n % 2 == 0:
48 return S.Zero
49 else:
50 x = Basic.sympify(x)
52 k = (n - 1)/2
54 if len(previous_terms) > 2:
55 return -previous_terms[-2] * x**2 * (n-2)/(n*k)
56 else:
57 return 2*(-1)**k * x**n/(n*Basic.Factorial(k)*Basic.sqrt(S.Pi))
59 def _eval_as_leading_term(self, x):
60 arg = self[0].as_leading_term(x)
62 if Basic.Order(1,x).contains(arg):
63 return arg
64 else:
65 return self.func(arg)
67 def _eval_is_real(self):
68 return self[0].is_real
70 @classmethod
71 def _eval_apply_evalf(self, arg):
72 arg = arg.evalf()
74 if isinstance(arg, Basic.Number):
75 # Temporary hack
76 from sympy.core.numbers import Real
77 from sympy.numerics import evalf
78 from sympy.numerics.functions2 import erf
79 e = erf(evalf(arg))
80 return Real(str(e))