1 /** @file inifcns_trans.cpp
3 * Implementation of transcendental (and trigonometric and hyperbolic)
7 * GiNaC Copyright (C) 1999-2003 Johannes Gutenberg University Mainz, Germany
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.
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.
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
32 #include "operators.h"
33 #include "relational.h"
41 // exponential function
44 static ex exp_evalf(const ex & x)
46 if (is_exactly_a<numeric>(x))
47 return exp(ex_to<numeric>(x));
52 static ex exp_eval(const ex & x)
58 // exp(n*Pi*I/2) -> {+1|+I|-1|-I}
59 const ex TwoExOverPiI=(_ex2*x)/(Pi*I);
60 if (TwoExOverPiI.info(info_flags::integer)) {
61 const numeric z = mod(ex_to<numeric>(TwoExOverPiI),_num4);
62 if (z.is_equal(_num0))
64 if (z.is_equal(_num1))
66 if (z.is_equal(_num2))
68 if (z.is_equal(_num3))
72 if (is_ex_the_function(x, log))
76 if (x.info(info_flags::numeric) && !x.info(info_flags::crational))
77 return exp(ex_to<numeric>(x));
82 static ex exp_deriv(const ex & x, unsigned deriv_param)
84 GINAC_ASSERT(deriv_param==0);
86 // d/dx exp(x) -> exp(x)
90 REGISTER_FUNCTION(exp, eval_func(exp_eval).
91 evalf_func(exp_evalf).
92 derivative_func(exp_deriv).
99 static ex log_evalf(const ex & x)
101 if (is_exactly_a<numeric>(x))
102 return log(ex_to<numeric>(x));
104 return log(x).hold();
107 static ex log_eval(const ex & x)
109 if (x.info(info_flags::numeric)) {
110 if (x.is_zero()) // log(0) -> infinity
111 throw(pole_error("log_eval(): log(0)",0));
112 if (x.info(info_flags::real) && x.info(info_flags::negative))
113 return (log(-x)+I*Pi);
114 if (x.is_equal(_ex1)) // log(1) -> 0
116 if (x.is_equal(I)) // log(I) -> Pi*I/2
117 return (Pi*I*_num1_2);
118 if (x.is_equal(-I)) // log(-I) -> -Pi*I/2
119 return (Pi*I*_num_1_2);
121 if (!x.info(info_flags::crational))
122 return log(ex_to<numeric>(x));
124 // log(exp(t)) -> t (if -Pi < t.imag() <= Pi):
125 if (is_ex_the_function(x, exp)) {
126 const ex &t = x.op(0);
127 if (is_a<symbol>(t) && (ex_to<symbol>(t).get_domain() == symbol_options::real)) {
130 if (t.info(info_flags::numeric)) {
131 const numeric &nt = ex_to<numeric>(t);
137 return log(x).hold();
140 static ex log_deriv(const ex & x, unsigned deriv_param)
142 GINAC_ASSERT(deriv_param==0);
144 // d/dx log(x) -> 1/x
145 return power(x, _ex_1);
148 static ex log_series(const ex &arg,
149 const relational &rel,
153 GINAC_ASSERT(is_a<symbol>(rel.lhs()));
155 bool must_expand_arg = false;
156 // maybe substitution of rel into arg fails because of a pole
158 arg_pt = arg.subs(rel, subs_options::no_pattern);
159 } catch (pole_error) {
160 must_expand_arg = true;
162 // or we are at the branch point anyways
163 if (arg_pt.is_zero())
164 must_expand_arg = true;
166 if (must_expand_arg) {
168 // This is the branch point: Series expand the argument first, then
169 // trivially factorize it to isolate that part which has constant
170 // leading coefficient in this fashion:
171 // x^n + x^(n+1) +...+ Order(x^(n+m)) -> x^n * (1 + x +...+ Order(x^m)).
172 // Return a plain n*log(x) for the x^n part and series expand the
173 // other part. Add them together and reexpand again in order to have
174 // one unnested pseries object. All this also works for negative n.
175 pseries argser; // series expansion of log's argument
176 unsigned extra_ord = 0; // extra expansion order
178 // oops, the argument expanded to a pure Order(x^something)...
179 argser = ex_to<pseries>(arg.series(rel, order+extra_ord, options));
181 } while (!argser.is_terminating() && argser.nops()==1);
183 const symbol &s = ex_to<symbol>(rel.lhs());
184 const ex &point = rel.rhs();
185 const int n = argser.ldegree(s);
187 // construct what we carelessly called the n*log(x) term above
188 const ex coeff = argser.coeff(s, n);
189 // expand the log, but only if coeff is real and > 0, since otherwise
190 // it would make the branch cut run into the wrong direction
191 if (coeff.info(info_flags::positive))
192 seq.push_back(expair(n*log(s-point)+log(coeff), _ex0));
194 seq.push_back(expair(log(coeff*pow(s-point, n)), _ex0));
196 if (!argser.is_terminating() || argser.nops()!=1) {
197 // in this case n more (or less) terms are needed
198 // (sadly, to generate them, we have to start from the beginning)
199 const ex newarg = ex_to<pseries>((arg/coeff).series(rel, order+n, options)).shift_exponents(-n).convert_to_poly(true);
200 return pseries(rel, seq).add_series(ex_to<pseries>(log(newarg).series(rel, order, options)));
201 } else // it was a monomial
202 return pseries(rel, seq);
204 if (!(options & series_options::suppress_branchcut) &&
205 arg_pt.info(info_flags::negative)) {
207 // This is the branch cut: assemble the primitive series manually and
208 // then add the corresponding complex step function.
209 const symbol &s = ex_to<symbol>(rel.lhs());
210 const ex &point = rel.rhs();
212 const ex replarg = series(log(arg), s==foo, order).subs(foo==point, subs_options::no_pattern);
214 seq.push_back(expair(-I*csgn(arg*I)*Pi, _ex0));
215 seq.push_back(expair(Order(_ex1), order));
216 return series(replarg - I*Pi + pseries(rel, seq), rel, order);
218 throw do_taylor(); // caught by function::series()
221 REGISTER_FUNCTION(log, eval_func(log_eval).
222 evalf_func(log_evalf).
223 derivative_func(log_deriv).
224 series_func(log_series).
228 // sine (trigonometric function)
231 static ex sin_evalf(const ex & x)
233 if (is_exactly_a<numeric>(x))
234 return sin(ex_to<numeric>(x));
236 return sin(x).hold();
239 static ex sin_eval(const ex & x)
241 // sin(n/d*Pi) -> { all known non-nested radicals }
242 const ex SixtyExOverPi = _ex60*x/Pi;
244 if (SixtyExOverPi.info(info_flags::integer)) {
245 numeric z = mod(ex_to<numeric>(SixtyExOverPi),_num120);
247 // wrap to interval [0, Pi)
252 // wrap to interval [0, Pi/2)
255 if (z.is_equal(_num0)) // sin(0) -> 0
257 if (z.is_equal(_num5)) // sin(Pi/12) -> sqrt(6)/4*(1-sqrt(3)/3)
258 return sign*_ex1_4*sqrt(_ex6)*(_ex1+_ex_1_3*sqrt(_ex3));
259 if (z.is_equal(_num6)) // sin(Pi/10) -> sqrt(5)/4-1/4
260 return sign*(_ex1_4*sqrt(_ex5)+_ex_1_4);
261 if (z.is_equal(_num10)) // sin(Pi/6) -> 1/2
263 if (z.is_equal(_num15)) // sin(Pi/4) -> sqrt(2)/2
264 return sign*_ex1_2*sqrt(_ex2);
265 if (z.is_equal(_num18)) // sin(3/10*Pi) -> sqrt(5)/4+1/4
266 return sign*(_ex1_4*sqrt(_ex5)+_ex1_4);
267 if (z.is_equal(_num20)) // sin(Pi/3) -> sqrt(3)/2
268 return sign*_ex1_2*sqrt(_ex3);
269 if (z.is_equal(_num25)) // sin(5/12*Pi) -> sqrt(6)/4*(1+sqrt(3)/3)
270 return sign*_ex1_4*sqrt(_ex6)*(_ex1+_ex1_3*sqrt(_ex3));
271 if (z.is_equal(_num30)) // sin(Pi/2) -> 1
275 if (is_exactly_a<function>(x)) {
276 const ex &t = x.op(0);
278 if (is_ex_the_function(x, asin))
280 // sin(acos(x)) -> sqrt(1-x^2)
281 if (is_ex_the_function(x, acos))
282 return sqrt(_ex1-power(t,_ex2));
283 // sin(atan(x)) -> x/sqrt(1+x^2)
284 if (is_ex_the_function(x, atan))
285 return t*power(_ex1+power(t,_ex2),_ex_1_2);
288 // sin(float) -> float
289 if (x.info(info_flags::numeric) && !x.info(info_flags::crational))
290 return sin(ex_to<numeric>(x));
292 return sin(x).hold();
295 static ex sin_deriv(const ex & x, unsigned deriv_param)
297 GINAC_ASSERT(deriv_param==0);
299 // d/dx sin(x) -> cos(x)
303 REGISTER_FUNCTION(sin, eval_func(sin_eval).
304 evalf_func(sin_evalf).
305 derivative_func(sin_deriv).
306 latex_name("\\sin"));
309 // cosine (trigonometric function)
312 static ex cos_evalf(const ex & x)
314 if (is_exactly_a<numeric>(x))
315 return cos(ex_to<numeric>(x));
317 return cos(x).hold();
320 static ex cos_eval(const ex & x)
322 // cos(n/d*Pi) -> { all known non-nested radicals }
323 const ex SixtyExOverPi = _ex60*x/Pi;
325 if (SixtyExOverPi.info(info_flags::integer)) {
326 numeric z = mod(ex_to<numeric>(SixtyExOverPi),_num120);
328 // wrap to interval [0, Pi)
332 // wrap to interval [0, Pi/2)
336 if (z.is_equal(_num0)) // cos(0) -> 1
338 if (z.is_equal(_num5)) // cos(Pi/12) -> sqrt(6)/4*(1+sqrt(3)/3)
339 return sign*_ex1_4*sqrt(_ex6)*(_ex1+_ex1_3*sqrt(_ex3));
340 if (z.is_equal(_num10)) // cos(Pi/6) -> sqrt(3)/2
341 return sign*_ex1_2*sqrt(_ex3);
342 if (z.is_equal(_num12)) // cos(Pi/5) -> sqrt(5)/4+1/4
343 return sign*(_ex1_4*sqrt(_ex5)+_ex1_4);
344 if (z.is_equal(_num15)) // cos(Pi/4) -> sqrt(2)/2
345 return sign*_ex1_2*sqrt(_ex2);
346 if (z.is_equal(_num20)) // cos(Pi/3) -> 1/2
348 if (z.is_equal(_num24)) // cos(2/5*Pi) -> sqrt(5)/4-1/4x
349 return sign*(_ex1_4*sqrt(_ex5)+_ex_1_4);
350 if (z.is_equal(_num25)) // cos(5/12*Pi) -> sqrt(6)/4*(1-sqrt(3)/3)
351 return sign*_ex1_4*sqrt(_ex6)*(_ex1+_ex_1_3*sqrt(_ex3));
352 if (z.is_equal(_num30)) // cos(Pi/2) -> 0
356 if (is_exactly_a<function>(x)) {
357 const ex &t = x.op(0);
359 if (is_ex_the_function(x, acos))
361 // cos(asin(x)) -> sqrt(1-x^2)
362 if (is_ex_the_function(x, asin))
363 return sqrt(_ex1-power(t,_ex2));
364 // cos(atan(x)) -> 1/sqrt(1+x^2)
365 if (is_ex_the_function(x, atan))
366 return power(_ex1+power(t,_ex2),_ex_1_2);
369 // cos(float) -> float
370 if (x.info(info_flags::numeric) && !x.info(info_flags::crational))
371 return cos(ex_to<numeric>(x));
373 return cos(x).hold();
376 static ex cos_deriv(const ex & x, unsigned deriv_param)
378 GINAC_ASSERT(deriv_param==0);
380 // d/dx cos(x) -> -sin(x)
384 REGISTER_FUNCTION(cos, eval_func(cos_eval).
385 evalf_func(cos_evalf).
386 derivative_func(cos_deriv).
387 latex_name("\\cos"));
390 // tangent (trigonometric function)
393 static ex tan_evalf(const ex & x)
395 if (is_exactly_a<numeric>(x))
396 return tan(ex_to<numeric>(x));
398 return tan(x).hold();
401 static ex tan_eval(const ex & x)
403 // tan(n/d*Pi) -> { all known non-nested radicals }
404 const ex SixtyExOverPi = _ex60*x/Pi;
406 if (SixtyExOverPi.info(info_flags::integer)) {
407 numeric z = mod(ex_to<numeric>(SixtyExOverPi),_num60);
409 // wrap to interval [0, Pi)
413 // wrap to interval [0, Pi/2)
417 if (z.is_equal(_num0)) // tan(0) -> 0
419 if (z.is_equal(_num5)) // tan(Pi/12) -> 2-sqrt(3)
420 return sign*(_ex2-sqrt(_ex3));
421 if (z.is_equal(_num10)) // tan(Pi/6) -> sqrt(3)/3
422 return sign*_ex1_3*sqrt(_ex3);
423 if (z.is_equal(_num15)) // tan(Pi/4) -> 1
425 if (z.is_equal(_num20)) // tan(Pi/3) -> sqrt(3)
426 return sign*sqrt(_ex3);
427 if (z.is_equal(_num25)) // tan(5/12*Pi) -> 2+sqrt(3)
428 return sign*(sqrt(_ex3)+_ex2);
429 if (z.is_equal(_num30)) // tan(Pi/2) -> infinity
430 throw (pole_error("tan_eval(): simple pole",1));
433 if (is_exactly_a<function>(x)) {
434 const ex &t = x.op(0);
436 if (is_ex_the_function(x, atan))
438 // tan(asin(x)) -> x/sqrt(1+x^2)
439 if (is_ex_the_function(x, asin))
440 return t*power(_ex1-power(t,_ex2),_ex_1_2);
441 // tan(acos(x)) -> sqrt(1-x^2)/x
442 if (is_ex_the_function(x, acos))
443 return power(t,_ex_1)*sqrt(_ex1-power(t,_ex2));
446 // tan(float) -> float
447 if (x.info(info_flags::numeric) && !x.info(info_flags::crational)) {
448 return tan(ex_to<numeric>(x));
451 return tan(x).hold();
454 static ex tan_deriv(const ex & x, unsigned deriv_param)
456 GINAC_ASSERT(deriv_param==0);
458 // d/dx tan(x) -> 1+tan(x)^2;
459 return (_ex1+power(tan(x),_ex2));
462 static ex tan_series(const ex &x,
463 const relational &rel,
467 GINAC_ASSERT(is_a<symbol>(rel.lhs()));
469 // Taylor series where there is no pole falls back to tan_deriv.
470 // On a pole simply expand sin(x)/cos(x).
471 const ex x_pt = x.subs(rel, subs_options::no_pattern);
472 if (!(2*x_pt/Pi).info(info_flags::odd))
473 throw do_taylor(); // caught by function::series()
474 // if we got here we have to care for a simple pole
475 return (sin(x)/cos(x)).series(rel, order+2, options);
478 REGISTER_FUNCTION(tan, eval_func(tan_eval).
479 evalf_func(tan_evalf).
480 derivative_func(tan_deriv).
481 series_func(tan_series).
482 latex_name("\\tan"));
485 // inverse sine (arc sine)
488 static ex asin_evalf(const ex & x)
490 if (is_exactly_a<numeric>(x))
491 return asin(ex_to<numeric>(x));
493 return asin(x).hold();
496 static ex asin_eval(const ex & x)
498 if (x.info(info_flags::numeric)) {
503 if (x.is_equal(_ex1_2))
504 return numeric(1,6)*Pi;
506 if (x.is_equal(_ex1))
508 // asin(-1/2) -> -Pi/6
509 if (x.is_equal(_ex_1_2))
510 return numeric(-1,6)*Pi;
512 if (x.is_equal(_ex_1))
514 // asin(float) -> float
515 if (!x.info(info_flags::crational))
516 return asin(ex_to<numeric>(x));
519 return asin(x).hold();
522 static ex asin_deriv(const ex & x, unsigned deriv_param)
524 GINAC_ASSERT(deriv_param==0);
526 // d/dx asin(x) -> 1/sqrt(1-x^2)
527 return power(1-power(x,_ex2),_ex_1_2);
530 REGISTER_FUNCTION(asin, eval_func(asin_eval).
531 evalf_func(asin_evalf).
532 derivative_func(asin_deriv).
533 latex_name("\\arcsin"));
536 // inverse cosine (arc cosine)
539 static ex acos_evalf(const ex & x)
541 if (is_exactly_a<numeric>(x))
542 return acos(ex_to<numeric>(x));
544 return acos(x).hold();
547 static ex acos_eval(const ex & x)
549 if (x.info(info_flags::numeric)) {
551 if (x.is_equal(_ex1))
554 if (x.is_equal(_ex1_2))
559 // acos(-1/2) -> 2/3*Pi
560 if (x.is_equal(_ex_1_2))
561 return numeric(2,3)*Pi;
563 if (x.is_equal(_ex_1))
565 // acos(float) -> float
566 if (!x.info(info_flags::crational))
567 return acos(ex_to<numeric>(x));
570 return acos(x).hold();
573 static ex acos_deriv(const ex & x, unsigned deriv_param)
575 GINAC_ASSERT(deriv_param==0);
577 // d/dx acos(x) -> -1/sqrt(1-x^2)
578 return -power(1-power(x,_ex2),_ex_1_2);
581 REGISTER_FUNCTION(acos, eval_func(acos_eval).
582 evalf_func(acos_evalf).
583 derivative_func(acos_deriv).
584 latex_name("\\arccos"));
587 // inverse tangent (arc tangent)
590 static ex atan_evalf(const ex & x)
592 if (is_exactly_a<numeric>(x))
593 return atan(ex_to<numeric>(x));
595 return atan(x).hold();
598 static ex atan_eval(const ex & x)
600 if (x.info(info_flags::numeric)) {
605 if (x.is_equal(_ex1))
608 if (x.is_equal(_ex_1))
610 if (x.is_equal(I) || x.is_equal(-I))
611 throw (pole_error("atan_eval(): logarithmic pole",0));
612 // atan(float) -> float
613 if (!x.info(info_flags::crational))
614 return atan(ex_to<numeric>(x));
617 return atan(x).hold();
620 static ex atan_deriv(const ex & x, unsigned deriv_param)
622 GINAC_ASSERT(deriv_param==0);
624 // d/dx atan(x) -> 1/(1+x^2)
625 return power(_ex1+power(x,_ex2), _ex_1);
628 static ex atan_series(const ex &arg,
629 const relational &rel,
633 GINAC_ASSERT(is_a<symbol>(rel.lhs()));
635 // Taylor series where there is no pole or cut falls back to atan_deriv.
636 // There are two branch cuts, one runnig from I up the imaginary axis and
637 // one running from -I down the imaginary axis. The points I and -I are
639 // On the branch cuts and the poles series expand
640 // (log(1+I*x)-log(1-I*x))/(2*I)
642 const ex arg_pt = arg.subs(rel, subs_options::no_pattern);
643 if (!(I*arg_pt).info(info_flags::real))
644 throw do_taylor(); // Re(x) != 0
645 if ((I*arg_pt).info(info_flags::real) && abs(I*arg_pt)<_ex1)
646 throw do_taylor(); // Re(x) == 0, but abs(x)<1
647 // care for the poles, using the defining formula for atan()...
648 if (arg_pt.is_equal(I) || arg_pt.is_equal(-I))
649 return ((log(1+I*arg)-log(1-I*arg))/(2*I)).series(rel, order, options);
650 if (!(options & series_options::suppress_branchcut)) {
652 // This is the branch cut: assemble the primitive series manually and
653 // then add the corresponding complex step function.
654 const symbol &s = ex_to<symbol>(rel.lhs());
655 const ex &point = rel.rhs();
657 const ex replarg = series(atan(arg), s==foo, order).subs(foo==point, subs_options::no_pattern);
658 ex Order0correction = replarg.op(0)+csgn(arg)*Pi*_ex_1_2;
660 Order0correction += log((I*arg_pt+_ex_1)/(I*arg_pt+_ex1))*I*_ex_1_2;
662 Order0correction += log((I*arg_pt+_ex1)/(I*arg_pt+_ex_1))*I*_ex1_2;
664 seq.push_back(expair(Order0correction, _ex0));
665 seq.push_back(expair(Order(_ex1), order));
666 return series(replarg - pseries(rel, seq), rel, order);
671 REGISTER_FUNCTION(atan, eval_func(atan_eval).
672 evalf_func(atan_evalf).
673 derivative_func(atan_deriv).
674 series_func(atan_series).
675 latex_name("\\arctan"));
678 // inverse tangent (atan2(y,x))
681 static ex atan2_evalf(const ex &y, const ex &x)
683 if (is_exactly_a<numeric>(y) && is_exactly_a<numeric>(x))
684 return atan2(ex_to<numeric>(y), ex_to<numeric>(x));
686 return atan2(y, x).hold();
689 static ex atan2_eval(const ex & y, const ex & x)
691 if (y.info(info_flags::numeric) && !y.info(info_flags::crational) &&
692 x.info(info_flags::numeric) && !x.info(info_flags::crational)) {
693 return atan2_evalf(y,x);
696 return atan2(y,x).hold();
699 static ex atan2_deriv(const ex & y, const ex & x, unsigned deriv_param)
701 GINAC_ASSERT(deriv_param<2);
703 if (deriv_param==0) {
705 return x*power(power(x,_ex2)+power(y,_ex2),_ex_1);
708 return -y*power(power(x,_ex2)+power(y,_ex2),_ex_1);
711 REGISTER_FUNCTION(atan2, eval_func(atan2_eval).
712 evalf_func(atan2_evalf).
713 derivative_func(atan2_deriv));
716 // hyperbolic sine (trigonometric function)
719 static ex sinh_evalf(const ex & x)
721 if (is_exactly_a<numeric>(x))
722 return sinh(ex_to<numeric>(x));
724 return sinh(x).hold();
727 static ex sinh_eval(const ex & x)
729 if (x.info(info_flags::numeric)) {
730 if (x.is_zero()) // sinh(0) -> 0
732 if (!x.info(info_flags::crational)) // sinh(float) -> float
733 return sinh(ex_to<numeric>(x));
736 if ((x/Pi).info(info_flags::numeric) &&
737 ex_to<numeric>(x/Pi).real().is_zero()) // sinh(I*x) -> I*sin(x)
740 if (is_exactly_a<function>(x)) {
741 const ex &t = x.op(0);
742 // sinh(asinh(x)) -> x
743 if (is_ex_the_function(x, asinh))
745 // sinh(acosh(x)) -> sqrt(x-1) * sqrt(x+1)
746 if (is_ex_the_function(x, acosh))
747 return sqrt(t-_ex1)*sqrt(t+_ex1);
748 // sinh(atanh(x)) -> x/sqrt(1-x^2)
749 if (is_ex_the_function(x, atanh))
750 return t*power(_ex1-power(t,_ex2),_ex_1_2);
753 return sinh(x).hold();
756 static ex sinh_deriv(const ex & x, unsigned deriv_param)
758 GINAC_ASSERT(deriv_param==0);
760 // d/dx sinh(x) -> cosh(x)
764 REGISTER_FUNCTION(sinh, eval_func(sinh_eval).
765 evalf_func(sinh_evalf).
766 derivative_func(sinh_deriv).
767 latex_name("\\sinh"));
770 // hyperbolic cosine (trigonometric function)
773 static ex cosh_evalf(const ex & x)
775 if (is_exactly_a<numeric>(x))
776 return cosh(ex_to<numeric>(x));
778 return cosh(x).hold();
781 static ex cosh_eval(const ex & x)
783 if (x.info(info_flags::numeric)) {
784 if (x.is_zero()) // cosh(0) -> 1
786 if (!x.info(info_flags::crational)) // cosh(float) -> float
787 return cosh(ex_to<numeric>(x));
790 if ((x/Pi).info(info_flags::numeric) &&
791 ex_to<numeric>(x/Pi).real().is_zero()) // cosh(I*x) -> cos(x)
794 if (is_exactly_a<function>(x)) {
795 const ex &t = x.op(0);
796 // cosh(acosh(x)) -> x
797 if (is_ex_the_function(x, acosh))
799 // cosh(asinh(x)) -> sqrt(1+x^2)
800 if (is_ex_the_function(x, asinh))
801 return sqrt(_ex1+power(t,_ex2));
802 // cosh(atanh(x)) -> 1/sqrt(1-x^2)
803 if (is_ex_the_function(x, atanh))
804 return power(_ex1-power(t,_ex2),_ex_1_2);
807 return cosh(x).hold();
810 static ex cosh_deriv(const ex & x, unsigned deriv_param)
812 GINAC_ASSERT(deriv_param==0);
814 // d/dx cosh(x) -> sinh(x)
818 REGISTER_FUNCTION(cosh, eval_func(cosh_eval).
819 evalf_func(cosh_evalf).
820 derivative_func(cosh_deriv).
821 latex_name("\\cosh"));
824 // hyperbolic tangent (trigonometric function)
827 static ex tanh_evalf(const ex & x)
829 if (is_exactly_a<numeric>(x))
830 return tanh(ex_to<numeric>(x));
832 return tanh(x).hold();
835 static ex tanh_eval(const ex & x)
837 if (x.info(info_flags::numeric)) {
838 if (x.is_zero()) // tanh(0) -> 0
840 if (!x.info(info_flags::crational)) // tanh(float) -> float
841 return tanh(ex_to<numeric>(x));
844 if ((x/Pi).info(info_flags::numeric) &&
845 ex_to<numeric>(x/Pi).real().is_zero()) // tanh(I*x) -> I*tan(x);
848 if (is_exactly_a<function>(x)) {
849 const ex &t = x.op(0);
850 // tanh(atanh(x)) -> x
851 if (is_ex_the_function(x, atanh))
853 // tanh(asinh(x)) -> x/sqrt(1+x^2)
854 if (is_ex_the_function(x, asinh))
855 return t*power(_ex1+power(t,_ex2),_ex_1_2);
856 // tanh(acosh(x)) -> sqrt(x-1)*sqrt(x+1)/x
857 if (is_ex_the_function(x, acosh))
858 return sqrt(t-_ex1)*sqrt(t+_ex1)*power(t,_ex_1);
861 return tanh(x).hold();
864 static ex tanh_deriv(const ex & x, unsigned deriv_param)
866 GINAC_ASSERT(deriv_param==0);
868 // d/dx tanh(x) -> 1-tanh(x)^2
869 return _ex1-power(tanh(x),_ex2);
872 static ex tanh_series(const ex &x,
873 const relational &rel,
877 GINAC_ASSERT(is_a<symbol>(rel.lhs()));
879 // Taylor series where there is no pole falls back to tanh_deriv.
880 // On a pole simply expand sinh(x)/cosh(x).
881 const ex x_pt = x.subs(rel, subs_options::no_pattern);
882 if (!(2*I*x_pt/Pi).info(info_flags::odd))
883 throw do_taylor(); // caught by function::series()
884 // if we got here we have to care for a simple pole
885 return (sinh(x)/cosh(x)).series(rel, order+2, options);
888 REGISTER_FUNCTION(tanh, eval_func(tanh_eval).
889 evalf_func(tanh_evalf).
890 derivative_func(tanh_deriv).
891 series_func(tanh_series).
892 latex_name("\\tanh"));
895 // inverse hyperbolic sine (trigonometric function)
898 static ex asinh_evalf(const ex & x)
900 if (is_exactly_a<numeric>(x))
901 return asinh(ex_to<numeric>(x));
903 return asinh(x).hold();
906 static ex asinh_eval(const ex & x)
908 if (x.info(info_flags::numeric)) {
912 // asinh(float) -> float
913 if (!x.info(info_flags::crational))
914 return asinh(ex_to<numeric>(x));
917 return asinh(x).hold();
920 static ex asinh_deriv(const ex & x, unsigned deriv_param)
922 GINAC_ASSERT(deriv_param==0);
924 // d/dx asinh(x) -> 1/sqrt(1+x^2)
925 return power(_ex1+power(x,_ex2),_ex_1_2);
928 REGISTER_FUNCTION(asinh, eval_func(asinh_eval).
929 evalf_func(asinh_evalf).
930 derivative_func(asinh_deriv));
933 // inverse hyperbolic cosine (trigonometric function)
936 static ex acosh_evalf(const ex & x)
938 if (is_exactly_a<numeric>(x))
939 return acosh(ex_to<numeric>(x));
941 return acosh(x).hold();
944 static ex acosh_eval(const ex & x)
946 if (x.info(info_flags::numeric)) {
947 // acosh(0) -> Pi*I/2
949 return Pi*I*numeric(1,2);
951 if (x.is_equal(_ex1))
954 if (x.is_equal(_ex_1))
956 // acosh(float) -> float
957 if (!x.info(info_flags::crational))
958 return acosh(ex_to<numeric>(x));
961 return acosh(x).hold();
964 static ex acosh_deriv(const ex & x, unsigned deriv_param)
966 GINAC_ASSERT(deriv_param==0);
968 // d/dx acosh(x) -> 1/(sqrt(x-1)*sqrt(x+1))
969 return power(x+_ex_1,_ex_1_2)*power(x+_ex1,_ex_1_2);
972 REGISTER_FUNCTION(acosh, eval_func(acosh_eval).
973 evalf_func(acosh_evalf).
974 derivative_func(acosh_deriv));
977 // inverse hyperbolic tangent (trigonometric function)
980 static ex atanh_evalf(const ex & x)
982 if (is_exactly_a<numeric>(x))
983 return atanh(ex_to<numeric>(x));
985 return atanh(x).hold();
988 static ex atanh_eval(const ex & x)
990 if (x.info(info_flags::numeric)) {
994 // atanh({+|-}1) -> throw
995 if (x.is_equal(_ex1) || x.is_equal(_ex_1))
996 throw (pole_error("atanh_eval(): logarithmic pole",0));
997 // atanh(float) -> float
998 if (!x.info(info_flags::crational))
999 return atanh(ex_to<numeric>(x));
1002 return atanh(x).hold();
1005 static ex atanh_deriv(const ex & x, unsigned deriv_param)
1007 GINAC_ASSERT(deriv_param==0);
1009 // d/dx atanh(x) -> 1/(1-x^2)
1010 return power(_ex1-power(x,_ex2),_ex_1);
1013 static ex atanh_series(const ex &arg,
1014 const relational &rel,
1018 GINAC_ASSERT(is_a<symbol>(rel.lhs()));
1020 // Taylor series where there is no pole or cut falls back to atanh_deriv.
1021 // There are two branch cuts, one runnig from 1 up the real axis and one
1022 // one running from -1 down the real axis. The points 1 and -1 are poles
1023 // On the branch cuts and the poles series expand
1024 // (log(1+x)-log(1-x))/2
1026 const ex arg_pt = arg.subs(rel, subs_options::no_pattern);
1027 if (!(arg_pt).info(info_flags::real))
1028 throw do_taylor(); // Im(x) != 0
1029 if ((arg_pt).info(info_flags::real) && abs(arg_pt)<_ex1)
1030 throw do_taylor(); // Im(x) == 0, but abs(x)<1
1031 // care for the poles, using the defining formula for atanh()...
1032 if (arg_pt.is_equal(_ex1) || arg_pt.is_equal(_ex_1))
1033 return ((log(_ex1+arg)-log(_ex1-arg))*_ex1_2).series(rel, order, options);
1034 // ...and the branch cuts (the discontinuity at the cut being just I*Pi)
1035 if (!(options & series_options::suppress_branchcut)) {
1037 // This is the branch cut: assemble the primitive series manually and
1038 // then add the corresponding complex step function.
1039 const symbol &s = ex_to<symbol>(rel.lhs());
1040 const ex &point = rel.rhs();
1042 const ex replarg = series(atanh(arg), s==foo, order).subs(foo==point, subs_options::no_pattern);
1043 ex Order0correction = replarg.op(0)+csgn(I*arg)*Pi*I*_ex1_2;
1045 Order0correction += log((arg_pt+_ex_1)/(arg_pt+_ex1))*_ex1_2;
1047 Order0correction += log((arg_pt+_ex1)/(arg_pt+_ex_1))*_ex_1_2;
1049 seq.push_back(expair(Order0correction, _ex0));
1050 seq.push_back(expair(Order(_ex1), order));
1051 return series(replarg - pseries(rel, seq), rel, order);
1056 REGISTER_FUNCTION(atanh, eval_func(atanh_eval).
1057 evalf_func(atanh_evalf).
1058 derivative_func(atanh_deriv).
1059 series_func(atanh_series));
1062 } // namespace GiNaC