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