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