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