Added methods for taking real and imaginary parts.
[ginac.git] / ginac / inifcns_trans.cpp
1 /** @file inifcns_trans.cpp
2  *
3  *  Implementation of transcendental (and trigonometric and hyperbolic)
4  *  functions. */
5
6 /*
7  *  GiNaC Copyright (C) 1999-2005 Johannes Gutenberg University Mainz, Germany
8  *
9  *  This program is free software; you can redistribute it and/or modify
10  *  it under the terms of the GNU General Public License as published by
11  *  the Free Software Foundation; either version 2 of the License, or
12  *  (at your option) any later version.
13  *
14  *  This program is distributed in the hope that it will be useful,
15  *  but WITHOUT ANY WARRANTY; without even the implied warranty of
16  *  MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
17  *  GNU General Public License for more details.
18  *
19  *  You should have received a copy of the GNU General Public License
20  *  along with this program; if not, write to the Free Software
21  *  Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA  02110-1301  USA
22  */
23
24 #include <vector>
25 #include <stdexcept>
26
27 #include "inifcns.h"
28 #include "ex.h"
29 #include "constant.h"
30 #include "numeric.h"
31 #include "power.h"
32 #include "operators.h"
33 #include "relational.h"
34 #include "symbol.h"
35 #include "pseries.h"
36 #include "utils.h"
37
38 namespace GiNaC {
39
40 //////////
41 // exponential function
42 //////////
43
44 static ex exp_evalf(const ex & x)
45 {
46         if (is_exactly_a<numeric>(x))
47                 return exp(ex_to<numeric>(x));
48         
49         return exp(x).hold();
50 }
51
52 static ex exp_eval(const ex & x)
53 {
54         // exp(0) -> 1
55         if (x.is_zero()) {
56                 return _ex1;
57         }
58
59         // exp(n*Pi*I/2) -> {+1|+I|-1|-I}
60         const ex TwoExOverPiI=(_ex2*x)/(Pi*I);
61         if (TwoExOverPiI.info(info_flags::integer)) {
62                 const numeric z = mod(ex_to<numeric>(TwoExOverPiI),*_num4_p);
63                 if (z.is_equal(*_num0_p))
64                         return _ex1;
65                 if (z.is_equal(*_num1_p))
66                         return ex(I);
67                 if (z.is_equal(*_num2_p))
68                         return _ex_1;
69                 if (z.is_equal(*_num3_p))
70                         return ex(-I);
71         }
72
73         // exp(log(x)) -> x
74         if (is_ex_the_function(x, log))
75                 return x.op(0);
76         
77         // exp(float) -> float
78         if (x.info(info_flags::numeric) && !x.info(info_flags::crational))
79                 return exp(ex_to<numeric>(x));
80         
81         return exp(x).hold();
82 }
83
84 static ex exp_deriv(const ex & x, unsigned deriv_param)
85 {
86         GINAC_ASSERT(deriv_param==0);
87
88         // d/dx exp(x) -> exp(x)
89         return exp(x);
90 }
91
92 static ex exp_real_part(const ex & x)
93 {
94         return exp(GiNaC::real_part(x))*cos(GiNaC::imag_part(x));
95 }
96
97 static ex exp_imag_part(const ex & x)
98 {
99         return exp(GiNaC::real_part(x))*sin(GiNaC::imag_part(x));
100 }
101
102 REGISTER_FUNCTION(exp, eval_func(exp_eval).
103                        evalf_func(exp_evalf).
104                        derivative_func(exp_deriv).
105                        real_part_func(exp_real_part).
106                        imag_part_func(exp_imag_part).
107                        latex_name("\\exp"));
108
109 //////////
110 // natural logarithm
111 //////////
112
113 static ex log_evalf(const ex & x)
114 {
115         if (is_exactly_a<numeric>(x))
116                 return log(ex_to<numeric>(x));
117         
118         return log(x).hold();
119 }
120
121 static ex log_eval(const ex & x)
122 {
123         if (x.info(info_flags::numeric)) {
124                 if (x.is_zero())         // log(0) -> infinity
125                         throw(pole_error("log_eval(): log(0)",0));
126                 if (x.info(info_flags::rational) && x.info(info_flags::negative))
127                         return (log(-x)+I*Pi);
128                 if (x.is_equal(_ex1))  // log(1) -> 0
129                         return _ex0;
130                 if (x.is_equal(I))       // log(I) -> Pi*I/2
131                         return (Pi*I*_ex1_2);
132                 if (x.is_equal(-I))      // log(-I) -> -Pi*I/2
133                         return (Pi*I*_ex_1_2);
134
135                 // log(float) -> float
136                 if (!x.info(info_flags::crational))
137                         return log(ex_to<numeric>(x));
138         }
139
140         // log(exp(t)) -> t (if -Pi < t.imag() <= Pi):
141         if (is_ex_the_function(x, exp)) {
142                 const ex &t = x.op(0);
143                 if (t.info(info_flags::real))
144                         return t;
145         }
146         
147         return log(x).hold();
148 }
149
150 static ex log_deriv(const ex & x, unsigned deriv_param)
151 {
152         GINAC_ASSERT(deriv_param==0);
153         
154         // d/dx log(x) -> 1/x
155         return power(x, _ex_1);
156 }
157
158 static ex log_series(const ex &arg,
159                      const relational &rel,
160                      int order,
161                      unsigned options)
162 {
163         GINAC_ASSERT(is_a<symbol>(rel.lhs()));
164         ex arg_pt;
165         bool must_expand_arg = false;
166         // maybe substitution of rel into arg fails because of a pole
167         try {
168                 arg_pt = arg.subs(rel, subs_options::no_pattern);
169         } catch (pole_error) {
170                 must_expand_arg = true;
171         }
172         // or we are at the branch point anyways
173         if (arg_pt.is_zero())
174                 must_expand_arg = true;
175         
176         if (must_expand_arg) {
177                 // method:
178                 // This is the branch point: Series expand the argument first, then
179                 // trivially factorize it to isolate that part which has constant
180                 // leading coefficient in this fashion:
181                 //   x^n + x^(n+1) +...+ Order(x^(n+m))  ->  x^n * (1 + x +...+ Order(x^m)).
182                 // Return a plain n*log(x) for the x^n part and series expand the
183                 // other part.  Add them together and reexpand again in order to have
184                 // one unnested pseries object.  All this also works for negative n.
185                 pseries argser;          // series expansion of log's argument
186                 unsigned extra_ord = 0;  // extra expansion order
187                 do {
188                         // oops, the argument expanded to a pure Order(x^something)...
189                         argser = ex_to<pseries>(arg.series(rel, order+extra_ord, options));
190                         ++extra_ord;
191                 } while (!argser.is_terminating() && argser.nops()==1);
192
193                 const symbol &s = ex_to<symbol>(rel.lhs());
194                 const ex &point = rel.rhs();
195                 const int n = argser.ldegree(s);
196                 epvector seq;
197                 // construct what we carelessly called the n*log(x) term above
198                 const ex coeff = argser.coeff(s, n);
199                 // expand the log, but only if coeff is real and > 0, since otherwise
200                 // it would make the branch cut run into the wrong direction
201                 if (coeff.info(info_flags::positive))
202                         seq.push_back(expair(n*log(s-point)+log(coeff), _ex0));
203                 else
204                         seq.push_back(expair(log(coeff*pow(s-point, n)), _ex0));
205
206                 if (!argser.is_terminating() || argser.nops()!=1) {
207                         // in this case n more (or less) terms are needed
208                         // (sadly, to generate them, we have to start from the beginning)
209                         if (n == 0 && coeff == 1) {
210                                 epvector epv;
211                                 ex acc = (new pseries(rel, epv))->setflag(status_flags::dynallocated);
212                                 epv.reserve(2);
213                                 epv.push_back(expair(-1, _ex0));
214                                 epv.push_back(expair(Order(_ex1), order));
215                                 ex rest = pseries(rel, epv).add_series(argser);
216                                 for (int i = order-1; i>0; --i) {
217                                         epvector cterm;
218                                         cterm.reserve(1);
219                                         cterm.push_back(expair(i%2 ? _ex1/i : _ex_1/i, _ex0));
220                                         acc = pseries(rel, cterm).add_series(ex_to<pseries>(acc));
221                                         acc = (ex_to<pseries>(rest)).mul_series(ex_to<pseries>(acc));
222                                 }
223                                 return acc;
224                         }
225                         const ex newarg = ex_to<pseries>((arg/coeff).series(rel, order+n, options)).shift_exponents(-n).convert_to_poly(true);
226                         return pseries(rel, seq).add_series(ex_to<pseries>(log(newarg).series(rel, order, options)));
227                 } else  // it was a monomial
228                         return pseries(rel, seq);
229         }
230         if (!(options & series_options::suppress_branchcut) &&
231              arg_pt.info(info_flags::negative)) {
232                 // method:
233                 // This is the branch cut: assemble the primitive series manually and
234                 // then add the corresponding complex step function.
235                 const symbol &s = ex_to<symbol>(rel.lhs());
236                 const ex &point = rel.rhs();
237                 const symbol foo;
238                 const ex replarg = series(log(arg), s==foo, order).subs(foo==point, subs_options::no_pattern);
239                 epvector seq;
240                 seq.push_back(expair(-I*csgn(arg*I)*Pi, _ex0));
241                 seq.push_back(expair(Order(_ex1), order));
242                 return series(replarg - I*Pi + pseries(rel, seq), rel, order);
243         }
244         throw do_taylor();  // caught by function::series()
245 }
246
247 static ex log_real_part(const ex & x)
248 {
249         return log(abs(x));
250 }
251
252 static ex log_imag_part(const ex & x)
253 {
254         return atan2(GiNaC::imag_part(x), GiNaC::real_part(x));
255 }
256
257 REGISTER_FUNCTION(log, eval_func(log_eval).
258                        evalf_func(log_evalf).
259                        derivative_func(log_deriv).
260                        series_func(log_series).
261                        real_part_func(log_real_part).
262                        imag_part_func(log_imag_part).
263                        latex_name("\\ln"));
264
265 //////////
266 // sine (trigonometric function)
267 //////////
268
269 static ex sin_evalf(const ex & x)
270 {
271         if (is_exactly_a<numeric>(x))
272                 return sin(ex_to<numeric>(x));
273         
274         return sin(x).hold();
275 }
276
277 static ex sin_eval(const ex & x)
278 {
279         // sin(n/d*Pi) -> { all known non-nested radicals }
280         const ex SixtyExOverPi = _ex60*x/Pi;
281         ex sign = _ex1;
282         if (SixtyExOverPi.info(info_flags::integer)) {
283                 numeric z = mod(ex_to<numeric>(SixtyExOverPi),*_num120_p);
284                 if (z>=*_num60_p) {
285                         // wrap to interval [0, Pi)
286                         z -= *_num60_p;
287                         sign = _ex_1;
288                 }
289                 if (z>*_num30_p) {
290                         // wrap to interval [0, Pi/2)
291                         z = *_num60_p-z;
292                 }
293                 if (z.is_equal(*_num0_p))  // sin(0)       -> 0
294                         return _ex0;
295                 if (z.is_equal(*_num5_p))  // sin(Pi/12)   -> sqrt(6)/4*(1-sqrt(3)/3)
296                         return sign*_ex1_4*sqrt(_ex6)*(_ex1+_ex_1_3*sqrt(_ex3));
297                 if (z.is_equal(*_num6_p))  // sin(Pi/10)   -> sqrt(5)/4-1/4
298                         return sign*(_ex1_4*sqrt(_ex5)+_ex_1_4);
299                 if (z.is_equal(*_num10_p)) // sin(Pi/6)    -> 1/2
300                         return sign*_ex1_2;
301                 if (z.is_equal(*_num15_p)) // sin(Pi/4)    -> sqrt(2)/2
302                         return sign*_ex1_2*sqrt(_ex2);
303                 if (z.is_equal(*_num18_p)) // sin(3/10*Pi) -> sqrt(5)/4+1/4
304                         return sign*(_ex1_4*sqrt(_ex5)+_ex1_4);
305                 if (z.is_equal(*_num20_p)) // sin(Pi/3)    -> sqrt(3)/2
306                         return sign*_ex1_2*sqrt(_ex3);
307                 if (z.is_equal(*_num25_p)) // sin(5/12*Pi) -> sqrt(6)/4*(1+sqrt(3)/3)
308                         return sign*_ex1_4*sqrt(_ex6)*(_ex1+_ex1_3*sqrt(_ex3));
309                 if (z.is_equal(*_num30_p)) // sin(Pi/2)    -> 1
310                         return sign;
311         }
312
313         if (is_exactly_a<function>(x)) {
314                 const ex &t = x.op(0);
315
316                 // sin(asin(x)) -> x
317                 if (is_ex_the_function(x, asin))
318                         return t;
319
320                 // sin(acos(x)) -> sqrt(1-x^2)
321                 if (is_ex_the_function(x, acos))
322                         return sqrt(_ex1-power(t,_ex2));
323
324                 // sin(atan(x)) -> x/sqrt(1+x^2)
325                 if (is_ex_the_function(x, atan))
326                         return t*power(_ex1+power(t,_ex2),_ex_1_2);
327         }
328         
329         // sin(float) -> float
330         if (x.info(info_flags::numeric) && !x.info(info_flags::crational))
331                 return sin(ex_to<numeric>(x));
332
333         // sin() is odd
334         if (x.info(info_flags::negative))
335                 return -sin(-x);
336         
337         return sin(x).hold();
338 }
339
340 static ex sin_deriv(const ex & x, unsigned deriv_param)
341 {
342         GINAC_ASSERT(deriv_param==0);
343         
344         // d/dx sin(x) -> cos(x)
345         return cos(x);
346 }
347
348 static ex sin_real_part(const ex & x)
349 {
350         return cosh(GiNaC::imag_part(x))*sin(GiNaC::real_part(x));
351 }
352
353 static ex sin_imag_part(const ex & x)
354 {
355         return sinh(GiNaC::imag_part(x))*cos(GiNaC::real_part(x));
356 }
357
358 REGISTER_FUNCTION(sin, eval_func(sin_eval).
359                        evalf_func(sin_evalf).
360                        derivative_func(sin_deriv).
361                        real_part_func(sin_real_part).
362                        imag_part_func(sin_imag_part).
363                        latex_name("\\sin"));
364
365 //////////
366 // cosine (trigonometric function)
367 //////////
368
369 static ex cos_evalf(const ex & x)
370 {
371         if (is_exactly_a<numeric>(x))
372                 return cos(ex_to<numeric>(x));
373         
374         return cos(x).hold();
375 }
376
377 static ex cos_eval(const ex & x)
378 {
379         // cos(n/d*Pi) -> { all known non-nested radicals }
380         const ex SixtyExOverPi = _ex60*x/Pi;
381         ex sign = _ex1;
382         if (SixtyExOverPi.info(info_flags::integer)) {
383                 numeric z = mod(ex_to<numeric>(SixtyExOverPi),*_num120_p);
384                 if (z>=*_num60_p) {
385                         // wrap to interval [0, Pi)
386                         z = *_num120_p-z;
387                 }
388                 if (z>=*_num30_p) {
389                         // wrap to interval [0, Pi/2)
390                         z = *_num60_p-z;
391                         sign = _ex_1;
392                 }
393                 if (z.is_equal(*_num0_p))  // cos(0)       -> 1
394                         return sign;
395                 if (z.is_equal(*_num5_p))  // cos(Pi/12)   -> sqrt(6)/4*(1+sqrt(3)/3)
396                         return sign*_ex1_4*sqrt(_ex6)*(_ex1+_ex1_3*sqrt(_ex3));
397                 if (z.is_equal(*_num10_p)) // cos(Pi/6)    -> sqrt(3)/2
398                         return sign*_ex1_2*sqrt(_ex3);
399                 if (z.is_equal(*_num12_p)) // cos(Pi/5)    -> sqrt(5)/4+1/4
400                         return sign*(_ex1_4*sqrt(_ex5)+_ex1_4);
401                 if (z.is_equal(*_num15_p)) // cos(Pi/4)    -> sqrt(2)/2
402                         return sign*_ex1_2*sqrt(_ex2);
403                 if (z.is_equal(*_num20_p)) // cos(Pi/3)    -> 1/2
404                         return sign*_ex1_2;
405                 if (z.is_equal(*_num24_p)) // cos(2/5*Pi)  -> sqrt(5)/4-1/4x
406                         return sign*(_ex1_4*sqrt(_ex5)+_ex_1_4);
407                 if (z.is_equal(*_num25_p)) // cos(5/12*Pi) -> sqrt(6)/4*(1-sqrt(3)/3)
408                         return sign*_ex1_4*sqrt(_ex6)*(_ex1+_ex_1_3*sqrt(_ex3));
409                 if (z.is_equal(*_num30_p)) // cos(Pi/2)    -> 0
410                         return _ex0;
411         }
412
413         if (is_exactly_a<function>(x)) {
414                 const ex &t = x.op(0);
415
416                 // cos(acos(x)) -> x
417                 if (is_ex_the_function(x, acos))
418                         return t;
419
420                 // cos(asin(x)) -> sqrt(1-x^2)
421                 if (is_ex_the_function(x, asin))
422                         return sqrt(_ex1-power(t,_ex2));
423
424                 // cos(atan(x)) -> 1/sqrt(1+x^2)
425                 if (is_ex_the_function(x, atan))
426                         return power(_ex1+power(t,_ex2),_ex_1_2);
427         }
428         
429         // cos(float) -> float
430         if (x.info(info_flags::numeric) && !x.info(info_flags::crational))
431                 return cos(ex_to<numeric>(x));
432         
433         // cos() is even
434         if (x.info(info_flags::negative))
435                 return cos(-x);
436         
437         return cos(x).hold();
438 }
439
440 static ex cos_deriv(const ex & x, unsigned deriv_param)
441 {
442         GINAC_ASSERT(deriv_param==0);
443
444         // d/dx cos(x) -> -sin(x)
445         return -sin(x);
446 }
447
448 static ex cos_real_part(const ex & x)
449 {
450         return cosh(GiNaC::imag_part(x))*cos(GiNaC::real_part(x));
451 }
452
453 static ex cos_imag_part(const ex & x)
454 {
455         return -sinh(GiNaC::imag_part(x))*sin(GiNaC::real_part(x));
456 }
457
458 REGISTER_FUNCTION(cos, eval_func(cos_eval).
459                        evalf_func(cos_evalf).
460                        derivative_func(cos_deriv).
461                        real_part_func(cos_real_part).
462                        imag_part_func(cos_imag_part).
463                        latex_name("\\cos"));
464
465 //////////
466 // tangent (trigonometric function)
467 //////////
468
469 static ex tan_evalf(const ex & x)
470 {
471         if (is_exactly_a<numeric>(x))
472                 return tan(ex_to<numeric>(x));
473         
474         return tan(x).hold();
475 }
476
477 static ex tan_eval(const ex & x)
478 {
479         // tan(n/d*Pi) -> { all known non-nested radicals }
480         const ex SixtyExOverPi = _ex60*x/Pi;
481         ex sign = _ex1;
482         if (SixtyExOverPi.info(info_flags::integer)) {
483                 numeric z = mod(ex_to<numeric>(SixtyExOverPi),*_num60_p);
484                 if (z>=*_num60_p) {
485                         // wrap to interval [0, Pi)
486                         z -= *_num60_p;
487                 }
488                 if (z>=*_num30_p) {
489                         // wrap to interval [0, Pi/2)
490                         z = *_num60_p-z;
491                         sign = _ex_1;
492                 }
493                 if (z.is_equal(*_num0_p))  // tan(0)       -> 0
494                         return _ex0;
495                 if (z.is_equal(*_num5_p))  // tan(Pi/12)   -> 2-sqrt(3)
496                         return sign*(_ex2-sqrt(_ex3));
497                 if (z.is_equal(*_num10_p)) // tan(Pi/6)    -> sqrt(3)/3
498                         return sign*_ex1_3*sqrt(_ex3);
499                 if (z.is_equal(*_num15_p)) // tan(Pi/4)    -> 1
500                         return sign;
501                 if (z.is_equal(*_num20_p)) // tan(Pi/3)    -> sqrt(3)
502                         return sign*sqrt(_ex3);
503                 if (z.is_equal(*_num25_p)) // tan(5/12*Pi) -> 2+sqrt(3)
504                         return sign*(sqrt(_ex3)+_ex2);
505                 if (z.is_equal(*_num30_p)) // tan(Pi/2)    -> infinity
506                         throw (pole_error("tan_eval(): simple pole",1));
507         }
508
509         if (is_exactly_a<function>(x)) {
510                 const ex &t = x.op(0);
511
512                 // tan(atan(x)) -> x
513                 if (is_ex_the_function(x, atan))
514                         return t;
515
516                 // tan(asin(x)) -> x/sqrt(1+x^2)
517                 if (is_ex_the_function(x, asin))
518                         return t*power(_ex1-power(t,_ex2),_ex_1_2);
519
520                 // tan(acos(x)) -> sqrt(1-x^2)/x
521                 if (is_ex_the_function(x, acos))
522                         return power(t,_ex_1)*sqrt(_ex1-power(t,_ex2));
523         }
524         
525         // tan(float) -> float
526         if (x.info(info_flags::numeric) && !x.info(info_flags::crational)) {
527                 return tan(ex_to<numeric>(x));
528         }
529         
530         // tan() is odd
531         if (x.info(info_flags::negative))
532                 return -tan(-x);
533         
534         return tan(x).hold();
535 }
536
537 static ex tan_deriv(const ex & x, unsigned deriv_param)
538 {
539         GINAC_ASSERT(deriv_param==0);
540         
541         // d/dx tan(x) -> 1+tan(x)^2;
542         return (_ex1+power(tan(x),_ex2));
543 }
544
545 static ex tan_real_part(const ex & x)
546 {
547         ex a = GiNaC::real_part(x);
548         ex b = GiNaC::imag_part(x);
549         return tan(a)/(1+power(tan(a),2)*power(tan(b),2));
550 }
551
552 static ex tan_imag_part(const ex & x)
553 {
554         ex a = GiNaC::real_part(x);
555         ex b = GiNaC::imag_part(x);
556         return tanh(b)/(1+power(tan(a),2)*power(tan(b),2));
557 }
558
559 static ex tan_series(const ex &x,
560                      const relational &rel,
561                      int order,
562                      unsigned options)
563 {
564         GINAC_ASSERT(is_a<symbol>(rel.lhs()));
565         // method:
566         // Taylor series where there is no pole falls back to tan_deriv.
567         // On a pole simply expand sin(x)/cos(x).
568         const ex x_pt = x.subs(rel, subs_options::no_pattern);
569         if (!(2*x_pt/Pi).info(info_flags::odd))
570                 throw do_taylor();  // caught by function::series()
571         // if we got here we have to care for a simple pole
572         return (sin(x)/cos(x)).series(rel, order, options);
573 }
574
575 REGISTER_FUNCTION(tan, eval_func(tan_eval).
576                        evalf_func(tan_evalf).
577                        derivative_func(tan_deriv).
578                        series_func(tan_series).
579                        real_part_func(tan_real_part).
580                        imag_part_func(tan_imag_part).
581                        latex_name("\\tan"));
582
583 //////////
584 // inverse sine (arc sine)
585 //////////
586
587 static ex asin_evalf(const ex & x)
588 {
589         if (is_exactly_a<numeric>(x))
590                 return asin(ex_to<numeric>(x));
591         
592         return asin(x).hold();
593 }
594
595 static ex asin_eval(const ex & x)
596 {
597         if (x.info(info_flags::numeric)) {
598
599                 // asin(0) -> 0
600                 if (x.is_zero())
601                         return x;
602
603                 // asin(1/2) -> Pi/6
604                 if (x.is_equal(_ex1_2))
605                         return numeric(1,6)*Pi;
606
607                 // asin(1) -> Pi/2
608                 if (x.is_equal(_ex1))
609                         return _ex1_2*Pi;
610
611                 // asin(-1/2) -> -Pi/6
612                 if (x.is_equal(_ex_1_2))
613                         return numeric(-1,6)*Pi;
614
615                 // asin(-1) -> -Pi/2
616                 if (x.is_equal(_ex_1))
617                         return _ex_1_2*Pi;
618
619                 // asin(float) -> float
620                 if (!x.info(info_flags::crational))
621                         return asin(ex_to<numeric>(x));
622
623                 // asin() is odd
624                 if (x.info(info_flags::negative))
625                         return -asin(-x);
626         }
627         
628         return asin(x).hold();
629 }
630
631 static ex asin_deriv(const ex & x, unsigned deriv_param)
632 {
633         GINAC_ASSERT(deriv_param==0);
634         
635         // d/dx asin(x) -> 1/sqrt(1-x^2)
636         return power(1-power(x,_ex2),_ex_1_2);
637 }
638
639 REGISTER_FUNCTION(asin, eval_func(asin_eval).
640                         evalf_func(asin_evalf).
641                         derivative_func(asin_deriv).
642                         latex_name("\\arcsin"));
643
644 //////////
645 // inverse cosine (arc cosine)
646 //////////
647
648 static ex acos_evalf(const ex & x)
649 {
650         if (is_exactly_a<numeric>(x))
651                 return acos(ex_to<numeric>(x));
652         
653         return acos(x).hold();
654 }
655
656 static ex acos_eval(const ex & x)
657 {
658         if (x.info(info_flags::numeric)) {
659
660                 // acos(1) -> 0
661                 if (x.is_equal(_ex1))
662                         return _ex0;
663
664                 // acos(1/2) -> Pi/3
665                 if (x.is_equal(_ex1_2))
666                         return _ex1_3*Pi;
667
668                 // acos(0) -> Pi/2
669                 if (x.is_zero())
670                         return _ex1_2*Pi;
671
672                 // acos(-1/2) -> 2/3*Pi
673                 if (x.is_equal(_ex_1_2))
674                         return numeric(2,3)*Pi;
675
676                 // acos(-1) -> Pi
677                 if (x.is_equal(_ex_1))
678                         return Pi;
679
680                 // acos(float) -> float
681                 if (!x.info(info_flags::crational))
682                         return acos(ex_to<numeric>(x));
683
684                 // acos(-x) -> Pi-acos(x)
685                 if (x.info(info_flags::negative))
686                         return Pi-acos(-x);
687         }
688         
689         return acos(x).hold();
690 }
691
692 static ex acos_deriv(const ex & x, unsigned deriv_param)
693 {
694         GINAC_ASSERT(deriv_param==0);
695         
696         // d/dx acos(x) -> -1/sqrt(1-x^2)
697         return -power(1-power(x,_ex2),_ex_1_2);
698 }
699
700 REGISTER_FUNCTION(acos, eval_func(acos_eval).
701                         evalf_func(acos_evalf).
702                         derivative_func(acos_deriv).
703                         latex_name("\\arccos"));
704
705 //////////
706 // inverse tangent (arc tangent)
707 //////////
708
709 static ex atan_evalf(const ex & x)
710 {
711         if (is_exactly_a<numeric>(x))
712                 return atan(ex_to<numeric>(x));
713         
714         return atan(x).hold();
715 }
716
717 static ex atan_eval(const ex & x)
718 {
719         if (x.info(info_flags::numeric)) {
720
721                 // atan(0) -> 0
722                 if (x.is_zero())
723                         return _ex0;
724
725                 // atan(1) -> Pi/4
726                 if (x.is_equal(_ex1))
727                         return _ex1_4*Pi;
728
729                 // atan(-1) -> -Pi/4
730                 if (x.is_equal(_ex_1))
731                         return _ex_1_4*Pi;
732
733                 if (x.is_equal(I) || x.is_equal(-I))
734                         throw (pole_error("atan_eval(): logarithmic pole",0));
735
736                 // atan(float) -> float
737                 if (!x.info(info_flags::crational))
738                         return atan(ex_to<numeric>(x));
739
740                 // atan() is odd
741                 if (x.info(info_flags::negative))
742                         return -atan(-x);
743         }
744         
745         return atan(x).hold();
746 }
747
748 static ex atan_deriv(const ex & x, unsigned deriv_param)
749 {
750         GINAC_ASSERT(deriv_param==0);
751
752         // d/dx atan(x) -> 1/(1+x^2)
753         return power(_ex1+power(x,_ex2), _ex_1);
754 }
755
756 static ex atan_series(const ex &arg,
757                       const relational &rel,
758                       int order,
759                       unsigned options)
760 {
761         GINAC_ASSERT(is_a<symbol>(rel.lhs()));
762         // method:
763         // Taylor series where there is no pole or cut falls back to atan_deriv.
764         // There are two branch cuts, one runnig from I up the imaginary axis and
765         // one running from -I down the imaginary axis.  The points I and -I are
766         // poles.
767         // On the branch cuts and the poles series expand
768         //     (log(1+I*x)-log(1-I*x))/(2*I)
769         // instead.
770         const ex arg_pt = arg.subs(rel, subs_options::no_pattern);
771         if (!(I*arg_pt).info(info_flags::real))
772                 throw do_taylor();     // Re(x) != 0
773         if ((I*arg_pt).info(info_flags::real) && abs(I*arg_pt)<_ex1)
774                 throw do_taylor();     // Re(x) == 0, but abs(x)<1
775         // care for the poles, using the defining formula for atan()...
776         if (arg_pt.is_equal(I) || arg_pt.is_equal(-I))
777                 return ((log(1+I*arg)-log(1-I*arg))/(2*I)).series(rel, order, options);
778         if (!(options & series_options::suppress_branchcut)) {
779                 // method:
780                 // This is the branch cut: assemble the primitive series manually and
781                 // then add the corresponding complex step function.
782                 const symbol &s = ex_to<symbol>(rel.lhs());
783                 const ex &point = rel.rhs();
784                 const symbol foo;
785                 const ex replarg = series(atan(arg), s==foo, order).subs(foo==point, subs_options::no_pattern);
786                 ex Order0correction = replarg.op(0)+csgn(arg)*Pi*_ex_1_2;
787                 if ((I*arg_pt)<_ex0)
788                         Order0correction += log((I*arg_pt+_ex_1)/(I*arg_pt+_ex1))*I*_ex_1_2;
789                 else
790                         Order0correction += log((I*arg_pt+_ex1)/(I*arg_pt+_ex_1))*I*_ex1_2;
791                 epvector seq;
792                 seq.push_back(expair(Order0correction, _ex0));
793                 seq.push_back(expair(Order(_ex1), order));
794                 return series(replarg - pseries(rel, seq), rel, order);
795         }
796         throw do_taylor();
797 }
798
799 REGISTER_FUNCTION(atan, eval_func(atan_eval).
800                         evalf_func(atan_evalf).
801                         derivative_func(atan_deriv).
802                         series_func(atan_series).
803                         latex_name("\\arctan"));
804
805 //////////
806 // inverse tangent (atan2(y,x))
807 //////////
808
809 static ex atan2_evalf(const ex &y, const ex &x)
810 {
811         if (is_exactly_a<numeric>(y) && is_exactly_a<numeric>(x))
812                 return atan(ex_to<numeric>(y), ex_to<numeric>(x));
813         
814         return atan2(y, x).hold();
815 }
816
817 static ex atan2_eval(const ex & y, const ex & x)
818 {
819         if (y.info(info_flags::numeric) && x.info(info_flags::numeric)) {
820
821                 if (y.is_zero()) {
822
823                         // atan(0, 0) -> 0
824                         if (x.is_zero())
825                                 return _ex0;
826
827                         // atan(0, x), x real and positive -> 0
828                         if (x.info(info_flags::positive))
829                                 return _ex0;
830
831                         // atan(0, x), x real and negative -> -Pi
832                         if (x.info(info_flags::negative))
833                                 return _ex_1*Pi;
834                 }
835
836                 if (x.is_zero()) {
837
838                         // atan(y, 0), y real and positive -> Pi/2
839                         if (y.info(info_flags::positive))
840                                 return _ex1_2*Pi;
841
842                         // atan(y, 0), y real and negative -> -Pi/2
843                         if (y.info(info_flags::negative))
844                                 return _ex_1_2*Pi;
845                 }
846
847                 if (y.is_equal(x)) {
848
849                         // atan(y, y), y real and positive -> Pi/4
850                         if (y.info(info_flags::positive))
851                                 return _ex1_4*Pi;
852
853                         // atan(y, y), y real and negative -> -3/4*Pi
854                         if (y.info(info_flags::negative))
855                                 return numeric(-3, 4)*Pi;
856                 }
857
858                 if (y.is_equal(-x)) {
859
860                         // atan(y, -y), y real and positive -> 3*Pi/4
861                         if (y.info(info_flags::positive))
862                                 return numeric(3, 4)*Pi;
863
864                         // atan(y, -y), y real and negative -> -Pi/4
865                         if (y.info(info_flags::negative))
866                                 return _ex_1_4*Pi;
867                 }
868
869                 // atan(float, float) -> float
870                 if (!y.info(info_flags::crational) && !x.info(info_flags::crational))
871                         return atan(ex_to<numeric>(y), ex_to<numeric>(x));
872
873                 // atan(real, real) -> atan(y/x) +/- Pi
874                 if (y.info(info_flags::real) && x.info(info_flags::real)) {
875                         if (x.info(info_flags::positive))
876                                 return atan(y/x);
877                         else if(y.info(info_flags::positive))
878                                 return atan(y/x)+Pi;
879                         else
880                                 return atan(y/x)-Pi;
881                 }
882         }
883
884         return atan2(y, x).hold();
885 }    
886
887 static ex atan2_deriv(const ex & y, const ex & x, unsigned deriv_param)
888 {
889         GINAC_ASSERT(deriv_param<2);
890         
891         if (deriv_param==0) {
892                 // d/dy atan(y,x)
893                 return x*power(power(x,_ex2)+power(y,_ex2),_ex_1);
894         }
895         // d/dx atan(y,x)
896         return -y*power(power(x,_ex2)+power(y,_ex2),_ex_1);
897 }
898
899 REGISTER_FUNCTION(atan2, eval_func(atan2_eval).
900                          evalf_func(atan2_evalf).
901                          derivative_func(atan2_deriv));
902
903 //////////
904 // hyperbolic sine (trigonometric function)
905 //////////
906
907 static ex sinh_evalf(const ex & x)
908 {
909         if (is_exactly_a<numeric>(x))
910                 return sinh(ex_to<numeric>(x));
911         
912         return sinh(x).hold();
913 }
914
915 static ex sinh_eval(const ex & x)
916 {
917         if (x.info(info_flags::numeric)) {
918
919                 // sinh(0) -> 0
920                 if (x.is_zero())
921                         return _ex0;        
922
923                 // sinh(float) -> float
924                 if (!x.info(info_flags::crational))
925                         return sinh(ex_to<numeric>(x));
926
927                 // sinh() is odd
928                 if (x.info(info_flags::negative))
929                         return -sinh(-x);
930         }
931         
932         if ((x/Pi).info(info_flags::numeric) &&
933                 ex_to<numeric>(x/Pi).real().is_zero())  // sinh(I*x) -> I*sin(x)
934                 return I*sin(x/I);
935         
936         if (is_exactly_a<function>(x)) {
937                 const ex &t = x.op(0);
938
939                 // sinh(asinh(x)) -> x
940                 if (is_ex_the_function(x, asinh))
941                         return t;
942
943                 // sinh(acosh(x)) -> sqrt(x-1) * sqrt(x+1)
944                 if (is_ex_the_function(x, acosh))
945                         return sqrt(t-_ex1)*sqrt(t+_ex1);
946
947                 // sinh(atanh(x)) -> x/sqrt(1-x^2)
948                 if (is_ex_the_function(x, atanh))
949                         return t*power(_ex1-power(t,_ex2),_ex_1_2);
950         }
951         
952         return sinh(x).hold();
953 }
954
955 static ex sinh_deriv(const ex & x, unsigned deriv_param)
956 {
957         GINAC_ASSERT(deriv_param==0);
958         
959         // d/dx sinh(x) -> cosh(x)
960         return cosh(x);
961 }
962
963 static ex sinh_real_part(const ex & x)
964 {
965         return sinh(GiNaC::real_part(x))*cos(GiNaC::imag_part(x));
966 }
967
968 static ex sinh_imag_part(const ex & x)
969 {
970         return cosh(GiNaC::real_part(x))*sin(GiNaC::imag_part(x));
971 }
972
973 REGISTER_FUNCTION(sinh, eval_func(sinh_eval).
974                         evalf_func(sinh_evalf).
975                         derivative_func(sinh_deriv).
976                         real_part_func(sinh_real_part).
977                         imag_part_func(sinh_imag_part).
978                         latex_name("\\sinh"));
979
980 //////////
981 // hyperbolic cosine (trigonometric function)
982 //////////
983
984 static ex cosh_evalf(const ex & x)
985 {
986         if (is_exactly_a<numeric>(x))
987                 return cosh(ex_to<numeric>(x));
988         
989         return cosh(x).hold();
990 }
991
992 static ex cosh_eval(const ex & x)
993 {
994         if (x.info(info_flags::numeric)) {
995
996                 // cosh(0) -> 1
997                 if (x.is_zero())
998                         return _ex1;
999
1000                 // cosh(float) -> float
1001                 if (!x.info(info_flags::crational))
1002                         return cosh(ex_to<numeric>(x));
1003
1004                 // cosh() is even
1005                 if (x.info(info_flags::negative))
1006                         return cosh(-x);
1007         }
1008         
1009         if ((x/Pi).info(info_flags::numeric) &&
1010                 ex_to<numeric>(x/Pi).real().is_zero())  // cosh(I*x) -> cos(x)
1011                 return cos(x/I);
1012         
1013         if (is_exactly_a<function>(x)) {
1014                 const ex &t = x.op(0);
1015
1016                 // cosh(acosh(x)) -> x
1017                 if (is_ex_the_function(x, acosh))
1018                         return t;
1019
1020                 // cosh(asinh(x)) -> sqrt(1+x^2)
1021                 if (is_ex_the_function(x, asinh))
1022                         return sqrt(_ex1+power(t,_ex2));
1023
1024                 // cosh(atanh(x)) -> 1/sqrt(1-x^2)
1025                 if (is_ex_the_function(x, atanh))
1026                         return power(_ex1-power(t,_ex2),_ex_1_2);
1027         }
1028         
1029         return cosh(x).hold();
1030 }
1031
1032 static ex cosh_deriv(const ex & x, unsigned deriv_param)
1033 {
1034         GINAC_ASSERT(deriv_param==0);
1035         
1036         // d/dx cosh(x) -> sinh(x)
1037         return sinh(x);
1038 }
1039
1040 static ex cosh_real_part(const ex & x)
1041 {
1042         return cosh(GiNaC::real_part(x))*cos(GiNaC::imag_part(x));
1043 }
1044
1045 static ex cosh_imag_part(const ex & x)
1046 {
1047         return sinh(GiNaC::real_part(x))*sin(GiNaC::imag_part(x));
1048 }
1049
1050 REGISTER_FUNCTION(cosh, eval_func(cosh_eval).
1051                         evalf_func(cosh_evalf).
1052                         derivative_func(cosh_deriv).
1053                         real_part_func(cosh_real_part).
1054                         imag_part_func(cosh_imag_part).
1055                         latex_name("\\cosh"));
1056
1057 //////////
1058 // hyperbolic tangent (trigonometric function)
1059 //////////
1060
1061 static ex tanh_evalf(const ex & x)
1062 {
1063         if (is_exactly_a<numeric>(x))
1064                 return tanh(ex_to<numeric>(x));
1065         
1066         return tanh(x).hold();
1067 }
1068
1069 static ex tanh_eval(const ex & x)
1070 {
1071         if (x.info(info_flags::numeric)) {
1072
1073                 // tanh(0) -> 0
1074                 if (x.is_zero())
1075                         return _ex0;
1076
1077                 // tanh(float) -> float
1078                 if (!x.info(info_flags::crational))
1079                         return tanh(ex_to<numeric>(x));
1080
1081                 // tanh() is odd
1082                 if (x.info(info_flags::negative))
1083                         return -tanh(-x);
1084         }
1085         
1086         if ((x/Pi).info(info_flags::numeric) &&
1087                 ex_to<numeric>(x/Pi).real().is_zero())  // tanh(I*x) -> I*tan(x);
1088                 return I*tan(x/I);
1089         
1090         if (is_exactly_a<function>(x)) {
1091                 const ex &t = x.op(0);
1092
1093                 // tanh(atanh(x)) -> x
1094                 if (is_ex_the_function(x, atanh))
1095                         return t;
1096
1097                 // tanh(asinh(x)) -> x/sqrt(1+x^2)
1098                 if (is_ex_the_function(x, asinh))
1099                         return t*power(_ex1+power(t,_ex2),_ex_1_2);
1100
1101                 // tanh(acosh(x)) -> sqrt(x-1)*sqrt(x+1)/x
1102                 if (is_ex_the_function(x, acosh))
1103                         return sqrt(t-_ex1)*sqrt(t+_ex1)*power(t,_ex_1);
1104         }
1105         
1106         return tanh(x).hold();
1107 }
1108
1109 static ex tanh_deriv(const ex & x, unsigned deriv_param)
1110 {
1111         GINAC_ASSERT(deriv_param==0);
1112         
1113         // d/dx tanh(x) -> 1-tanh(x)^2
1114         return _ex1-power(tanh(x),_ex2);
1115 }
1116
1117 static ex tanh_series(const ex &x,
1118                       const relational &rel,
1119                       int order,
1120                       unsigned options)
1121 {
1122         GINAC_ASSERT(is_a<symbol>(rel.lhs()));
1123         // method:
1124         // Taylor series where there is no pole falls back to tanh_deriv.
1125         // On a pole simply expand sinh(x)/cosh(x).
1126         const ex x_pt = x.subs(rel, subs_options::no_pattern);
1127         if (!(2*I*x_pt/Pi).info(info_flags::odd))
1128                 throw do_taylor();  // caught by function::series()
1129         // if we got here we have to care for a simple pole
1130         return (sinh(x)/cosh(x)).series(rel, order, options);
1131 }
1132
1133 static ex tanh_real_part(const ex & x)
1134 {
1135         ex a = GiNaC::real_part(x);
1136         ex b = GiNaC::imag_part(x);
1137         return tanh(a)/(1+power(tanh(a),2)*power(tan(b),2));
1138 }
1139
1140 static ex tanh_imag_part(const ex & x)
1141 {
1142         ex a = GiNaC::real_part(x);
1143         ex b = GiNaC::imag_part(x);
1144         return tan(b)/(1+power(tanh(a),2)*power(tan(b),2));
1145 }
1146
1147 REGISTER_FUNCTION(tanh, eval_func(tanh_eval).
1148                         evalf_func(tanh_evalf).
1149                         derivative_func(tanh_deriv).
1150                         series_func(tanh_series).
1151                         real_part_func(tanh_real_part).
1152                         imag_part_func(tanh_imag_part).
1153                         latex_name("\\tanh"));
1154
1155 //////////
1156 // inverse hyperbolic sine (trigonometric function)
1157 //////////
1158
1159 static ex asinh_evalf(const ex & x)
1160 {
1161         if (is_exactly_a<numeric>(x))
1162                 return asinh(ex_to<numeric>(x));
1163         
1164         return asinh(x).hold();
1165 }
1166
1167 static ex asinh_eval(const ex & x)
1168 {
1169         if (x.info(info_flags::numeric)) {
1170
1171                 // asinh(0) -> 0
1172                 if (x.is_zero())
1173                         return _ex0;
1174
1175                 // asinh(float) -> float
1176                 if (!x.info(info_flags::crational))
1177                         return asinh(ex_to<numeric>(x));
1178
1179                 // asinh() is odd
1180                 if (x.info(info_flags::negative))
1181                         return -asinh(-x);
1182         }
1183         
1184         return asinh(x).hold();
1185 }
1186
1187 static ex asinh_deriv(const ex & x, unsigned deriv_param)
1188 {
1189         GINAC_ASSERT(deriv_param==0);
1190         
1191         // d/dx asinh(x) -> 1/sqrt(1+x^2)
1192         return power(_ex1+power(x,_ex2),_ex_1_2);
1193 }
1194
1195 REGISTER_FUNCTION(asinh, eval_func(asinh_eval).
1196                          evalf_func(asinh_evalf).
1197                          derivative_func(asinh_deriv));
1198
1199 //////////
1200 // inverse hyperbolic cosine (trigonometric function)
1201 //////////
1202
1203 static ex acosh_evalf(const ex & x)
1204 {
1205         if (is_exactly_a<numeric>(x))
1206                 return acosh(ex_to<numeric>(x));
1207         
1208         return acosh(x).hold();
1209 }
1210
1211 static ex acosh_eval(const ex & x)
1212 {
1213         if (x.info(info_flags::numeric)) {
1214
1215                 // acosh(0) -> Pi*I/2
1216                 if (x.is_zero())
1217                         return Pi*I*numeric(1,2);
1218
1219                 // acosh(1) -> 0
1220                 if (x.is_equal(_ex1))
1221                         return _ex0;
1222
1223                 // acosh(-1) -> Pi*I
1224                 if (x.is_equal(_ex_1))
1225                         return Pi*I;
1226
1227                 // acosh(float) -> float
1228                 if (!x.info(info_flags::crational))
1229                         return acosh(ex_to<numeric>(x));
1230
1231                 // acosh(-x) -> Pi*I-acosh(x)
1232                 if (x.info(info_flags::negative))
1233                         return Pi*I-acosh(-x);
1234         }
1235         
1236         return acosh(x).hold();
1237 }
1238
1239 static ex acosh_deriv(const ex & x, unsigned deriv_param)
1240 {
1241         GINAC_ASSERT(deriv_param==0);
1242         
1243         // d/dx acosh(x) -> 1/(sqrt(x-1)*sqrt(x+1))
1244         return power(x+_ex_1,_ex_1_2)*power(x+_ex1,_ex_1_2);
1245 }
1246
1247 REGISTER_FUNCTION(acosh, eval_func(acosh_eval).
1248                          evalf_func(acosh_evalf).
1249                          derivative_func(acosh_deriv));
1250
1251 //////////
1252 // inverse hyperbolic tangent (trigonometric function)
1253 //////////
1254
1255 static ex atanh_evalf(const ex & x)
1256 {
1257         if (is_exactly_a<numeric>(x))
1258                 return atanh(ex_to<numeric>(x));
1259         
1260         return atanh(x).hold();
1261 }
1262
1263 static ex atanh_eval(const ex & x)
1264 {
1265         if (x.info(info_flags::numeric)) {
1266
1267                 // atanh(0) -> 0
1268                 if (x.is_zero())
1269                         return _ex0;
1270
1271                 // atanh({+|-}1) -> throw
1272                 if (x.is_equal(_ex1) || x.is_equal(_ex_1))
1273                         throw (pole_error("atanh_eval(): logarithmic pole",0));
1274
1275                 // atanh(float) -> float
1276                 if (!x.info(info_flags::crational))
1277                         return atanh(ex_to<numeric>(x));
1278
1279                 // atanh() is odd
1280                 if (x.info(info_flags::negative))
1281                         return -atanh(-x);
1282         }
1283         
1284         return atanh(x).hold();
1285 }
1286
1287 static ex atanh_deriv(const ex & x, unsigned deriv_param)
1288 {
1289         GINAC_ASSERT(deriv_param==0);
1290         
1291         // d/dx atanh(x) -> 1/(1-x^2)
1292         return power(_ex1-power(x,_ex2),_ex_1);
1293 }
1294
1295 static ex atanh_series(const ex &arg,
1296                        const relational &rel,
1297                        int order,
1298                        unsigned options)
1299 {
1300         GINAC_ASSERT(is_a<symbol>(rel.lhs()));
1301         // method:
1302         // Taylor series where there is no pole or cut falls back to atanh_deriv.
1303         // There are two branch cuts, one runnig from 1 up the real axis and one
1304         // one running from -1 down the real axis.  The points 1 and -1 are poles
1305         // On the branch cuts and the poles series expand
1306         //     (log(1+x)-log(1-x))/2
1307         // instead.
1308         const ex arg_pt = arg.subs(rel, subs_options::no_pattern);
1309         if (!(arg_pt).info(info_flags::real))
1310                 throw do_taylor();     // Im(x) != 0
1311         if ((arg_pt).info(info_flags::real) && abs(arg_pt)<_ex1)
1312                 throw do_taylor();     // Im(x) == 0, but abs(x)<1
1313         // care for the poles, using the defining formula for atanh()...
1314         if (arg_pt.is_equal(_ex1) || arg_pt.is_equal(_ex_1))
1315                 return ((log(_ex1+arg)-log(_ex1-arg))*_ex1_2).series(rel, order, options);
1316         // ...and the branch cuts (the discontinuity at the cut being just I*Pi)
1317         if (!(options & series_options::suppress_branchcut)) {
1318                 // method:
1319                 // This is the branch cut: assemble the primitive series manually and
1320                 // then add the corresponding complex step function.
1321                 const symbol &s = ex_to<symbol>(rel.lhs());
1322                 const ex &point = rel.rhs();
1323                 const symbol foo;
1324                 const ex replarg = series(atanh(arg), s==foo, order).subs(foo==point, subs_options::no_pattern);
1325                 ex Order0correction = replarg.op(0)+csgn(I*arg)*Pi*I*_ex1_2;
1326                 if (arg_pt<_ex0)
1327                         Order0correction += log((arg_pt+_ex_1)/(arg_pt+_ex1))*_ex1_2;
1328                 else
1329                         Order0correction += log((arg_pt+_ex1)/(arg_pt+_ex_1))*_ex_1_2;
1330                 epvector seq;
1331                 seq.push_back(expair(Order0correction, _ex0));
1332                 seq.push_back(expair(Order(_ex1), order));
1333                 return series(replarg - pseries(rel, seq), rel, order);
1334         }
1335         throw do_taylor();
1336 }
1337
1338 REGISTER_FUNCTION(atanh, eval_func(atanh_eval).
1339                          evalf_func(atanh_evalf).
1340                          derivative_func(atanh_deriv).
1341                          series_func(atanh_series));
1342
1343
1344 } // namespace GiNaC