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