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 "relational.h"
40 // exponential function
43 static ex exp_evalf(const ex & x)
45 if (is_exactly_a<numeric>(x))
46 return exp(ex_to<numeric>(x));
51 static ex exp_eval(const ex & x)
57 // exp(n*Pi*I/2) -> {+1|+I|-1|-I}
58 const ex TwoExOverPiI=(_ex2*x)/(Pi*I);
59 if (TwoExOverPiI.info(info_flags::integer)) {
60 const numeric z = mod(ex_to<numeric>(TwoExOverPiI),_num4);
61 if (z.is_equal(_num0))
63 if (z.is_equal(_num1))
65 if (z.is_equal(_num2))
67 if (z.is_equal(_num3))
71 if (is_ex_the_function(x, log))
75 if (x.info(info_flags::numeric) && !x.info(info_flags::crational))
76 return exp(ex_to<numeric>(x));
81 static ex exp_deriv(const ex & x, unsigned deriv_param)
83 GINAC_ASSERT(deriv_param==0);
85 // d/dx exp(x) -> exp(x)
89 REGISTER_FUNCTION(exp, eval_func(exp_eval).
90 evalf_func(exp_evalf).
91 derivative_func(exp_deriv).
98 static ex log_evalf(const ex & x)
100 if (is_exactly_a<numeric>(x))
101 return log(ex_to<numeric>(x));
103 return log(x).hold();
106 static ex log_eval(const ex & x)
108 if (x.info(info_flags::numeric)) {
109 if (x.is_zero()) // log(0) -> infinity
110 throw(pole_error("log_eval(): log(0)",0));
111 if (x.info(info_flags::real) && x.info(info_flags::negative))
112 return (log(-x)+I*Pi);
113 if (x.is_equal(_ex1)) // log(1) -> 0
115 if (x.is_equal(I)) // log(I) -> Pi*I/2
116 return (Pi*I*_num1_2);
117 if (x.is_equal(-I)) // log(-I) -> -Pi*I/2
118 return (Pi*I*_num_1_2);
120 if (!x.info(info_flags::crational))
121 return log(ex_to<numeric>(x));
123 // log(exp(t)) -> t (if -Pi < t.imag() <= Pi):
124 if (is_ex_the_function(x, exp)) {
125 const ex &t = x.op(0);
126 if (t.info(info_flags::numeric)) {
127 const numeric &nt = ex_to<numeric>(t);
133 return log(x).hold();
136 static ex log_deriv(const ex & x, unsigned deriv_param)
138 GINAC_ASSERT(deriv_param==0);
140 // d/dx log(x) -> 1/x
141 return power(x, _ex_1);
144 // This is a strange workaround for a compiliation problem with the try statement
145 // below. With -O1 the exception is not caucht properly as of GCC-2.95.2, at
146 // least on i386. Version 2.95.4 seems to have fixed this silly problem, though.
147 // Funnily, with a simple extern declaration here it mysteriously works again.
148 #if defined(__GNUC__) && (__GNUC__==2)
149 extern "C" int putchar(int);
152 static ex log_series(const ex &arg,
153 const relational &rel,
157 GINAC_ASSERT(is_a<symbol>(rel.lhs()));
159 bool must_expand_arg = false;
160 // maybe substitution of rel into arg fails because of a pole
162 arg_pt = arg.subs(rel);
163 } catch (pole_error) {
164 must_expand_arg = true;
166 // or we are at the branch point anyways
167 if (arg_pt.is_zero())
168 must_expand_arg = true;
170 if (must_expand_arg) {
172 // This is the branch point: Series expand the argument first, then
173 // trivially factorize it to isolate that part which has constant
174 // leading coefficient in this fashion:
175 // x^n + x^(n+1) +...+ Order(x^(n+m)) -> x^n * (1 + x +...+ Order(x^m)).
176 // Return a plain n*log(x) for the x^n part and series expand the
177 // other part. Add them together and reexpand again in order to have
178 // one unnested pseries object. All this also works for negative n.
179 pseries argser; // series expansion of log's argument
180 unsigned extra_ord = 0; // extra expansion order
182 // oops, the argument expanded to a pure Order(x^something)...
183 argser = ex_to<pseries>(arg.series(rel, order+extra_ord, options));
185 } while (!argser.is_terminating() && argser.nops()==1);
187 const symbol &s = ex_to<symbol>(rel.lhs());
188 const ex &point = rel.rhs();
189 const int n = argser.ldegree(s);
191 // construct what we carelessly called the n*log(x) term above
192 const ex coeff = argser.coeff(s, n);
193 // expand the log, but only if coeff is real and > 0, since otherwise
194 // it would make the branch cut run into the wrong direction
195 if (coeff.info(info_flags::positive))
196 seq.push_back(expair(n*log(s-point)+log(coeff), _ex0));
198 seq.push_back(expair(log(coeff*pow(s-point, n)), _ex0));
200 if (!argser.is_terminating() || argser.nops()!=1) {
201 // in this case n more (or less) terms are needed
202 // (sadly, to generate them, we have to start from the beginning)
203 const ex newarg = ex_to<pseries>((arg/coeff).series(rel, order+n, options)).shift_exponents(-n).convert_to_poly(true);
204 return pseries(rel, seq).add_series(ex_to<pseries>(log(newarg).series(rel, order, options)));
205 } else // it was a monomial
206 return pseries(rel, seq);
208 if (!(options & series_options::suppress_branchcut) &&
209 arg_pt.info(info_flags::negative)) {
211 // This is the branch cut: assemble the primitive series manually and
212 // then add the corresponding complex step function.
213 const symbol &s = ex_to<symbol>(rel.lhs());
214 const ex &point = rel.rhs();
216 const ex replarg = series(log(arg), s==foo, order).subs(foo==point);
218 seq.push_back(expair(-I*csgn(arg*I)*Pi, _ex0));
219 seq.push_back(expair(Order(_ex1), order));
220 return series(replarg - I*Pi + pseries(rel, seq), rel, order);
222 throw do_taylor(); // caught by function::series()
225 REGISTER_FUNCTION(log, eval_func(log_eval).
226 evalf_func(log_evalf).
227 derivative_func(log_deriv).
228 series_func(log_series).
232 // sine (trigonometric function)
235 static ex sin_evalf(const ex & x)
237 if (is_exactly_a<numeric>(x))
238 return sin(ex_to<numeric>(x));
240 return sin(x).hold();
243 static ex sin_eval(const ex & x)
245 // sin(n/d*Pi) -> { all known non-nested radicals }
246 const ex SixtyExOverPi = _ex60*x/Pi;
248 if (SixtyExOverPi.info(info_flags::integer)) {
249 numeric z = mod(ex_to<numeric>(SixtyExOverPi),_num120);
251 // wrap to interval [0, Pi)
256 // wrap to interval [0, Pi/2)
259 if (z.is_equal(_num0)) // sin(0) -> 0
261 if (z.is_equal(_num5)) // sin(Pi/12) -> sqrt(6)/4*(1-sqrt(3)/3)
262 return sign*_ex1_4*sqrt(_ex6)*(_ex1+_ex_1_3*sqrt(_ex3));
263 if (z.is_equal(_num6)) // sin(Pi/10) -> sqrt(5)/4-1/4
264 return sign*(_ex1_4*sqrt(_ex5)+_ex_1_4);
265 if (z.is_equal(_num10)) // sin(Pi/6) -> 1/2
267 if (z.is_equal(_num15)) // sin(Pi/4) -> sqrt(2)/2
268 return sign*_ex1_2*sqrt(_ex2);
269 if (z.is_equal(_num18)) // sin(3/10*Pi) -> sqrt(5)/4+1/4
270 return sign*(_ex1_4*sqrt(_ex5)+_ex1_4);
271 if (z.is_equal(_num20)) // sin(Pi/3) -> sqrt(3)/2
272 return sign*_ex1_2*sqrt(_ex3);
273 if (z.is_equal(_num25)) // sin(5/12*Pi) -> sqrt(6)/4*(1+sqrt(3)/3)
274 return sign*_ex1_4*sqrt(_ex6)*(_ex1+_ex1_3*sqrt(_ex3));
275 if (z.is_equal(_num30)) // sin(Pi/2) -> 1
279 if (is_exactly_a<function>(x)) {
280 const ex &t = x.op(0);
282 if (is_ex_the_function(x, asin))
284 // sin(acos(x)) -> sqrt(1-x^2)
285 if (is_ex_the_function(x, acos))
286 return sqrt(_ex1-power(t,_ex2));
287 // sin(atan(x)) -> x/sqrt(1+x^2)
288 if (is_ex_the_function(x, atan))
289 return t*power(_ex1+power(t,_ex2),_ex_1_2);
292 // sin(float) -> float
293 if (x.info(info_flags::numeric) && !x.info(info_flags::crational))
294 return sin(ex_to<numeric>(x));
296 return sin(x).hold();
299 static ex sin_deriv(const ex & x, unsigned deriv_param)
301 GINAC_ASSERT(deriv_param==0);
303 // d/dx sin(x) -> cos(x)
307 REGISTER_FUNCTION(sin, eval_func(sin_eval).
308 evalf_func(sin_evalf).
309 derivative_func(sin_deriv).
310 latex_name("\\sin"));
313 // cosine (trigonometric function)
316 static ex cos_evalf(const ex & x)
318 if (is_exactly_a<numeric>(x))
319 return cos(ex_to<numeric>(x));
321 return cos(x).hold();
324 static ex cos_eval(const ex & x)
326 // cos(n/d*Pi) -> { all known non-nested radicals }
327 const ex SixtyExOverPi = _ex60*x/Pi;
329 if (SixtyExOverPi.info(info_flags::integer)) {
330 numeric z = mod(ex_to<numeric>(SixtyExOverPi),_num120);
332 // wrap to interval [0, Pi)
336 // wrap to interval [0, Pi/2)
340 if (z.is_equal(_num0)) // cos(0) -> 1
342 if (z.is_equal(_num5)) // cos(Pi/12) -> sqrt(6)/4*(1+sqrt(3)/3)
343 return sign*_ex1_4*sqrt(_ex6)*(_ex1+_ex1_3*sqrt(_ex3));
344 if (z.is_equal(_num10)) // cos(Pi/6) -> sqrt(3)/2
345 return sign*_ex1_2*sqrt(_ex3);
346 if (z.is_equal(_num12)) // cos(Pi/5) -> sqrt(5)/4+1/4
347 return sign*(_ex1_4*sqrt(_ex5)+_ex1_4);
348 if (z.is_equal(_num15)) // cos(Pi/4) -> sqrt(2)/2
349 return sign*_ex1_2*sqrt(_ex2);
350 if (z.is_equal(_num20)) // cos(Pi/3) -> 1/2
352 if (z.is_equal(_num24)) // cos(2/5*Pi) -> sqrt(5)/4-1/4x
353 return sign*(_ex1_4*sqrt(_ex5)+_ex_1_4);
354 if (z.is_equal(_num25)) // cos(5/12*Pi) -> sqrt(6)/4*(1-sqrt(3)/3)
355 return sign*_ex1_4*sqrt(_ex6)*(_ex1+_ex_1_3*sqrt(_ex3));
356 if (z.is_equal(_num30)) // cos(Pi/2) -> 0
360 if (is_exactly_a<function>(x)) {
361 const ex &t = x.op(0);
363 if (is_ex_the_function(x, acos))
365 // cos(asin(x)) -> sqrt(1-x^2)
366 if (is_ex_the_function(x, asin))
367 return sqrt(_ex1-power(t,_ex2));
368 // cos(atan(x)) -> 1/sqrt(1+x^2)
369 if (is_ex_the_function(x, atan))
370 return power(_ex1+power(t,_ex2),_ex_1_2);
373 // cos(float) -> float
374 if (x.info(info_flags::numeric) && !x.info(info_flags::crational))
375 return cos(ex_to<numeric>(x));
377 return cos(x).hold();
380 static ex cos_deriv(const ex & x, unsigned deriv_param)
382 GINAC_ASSERT(deriv_param==0);
384 // d/dx cos(x) -> -sin(x)
388 REGISTER_FUNCTION(cos, eval_func(cos_eval).
389 evalf_func(cos_evalf).
390 derivative_func(cos_deriv).
391 latex_name("\\cos"));
394 // tangent (trigonometric function)
397 static ex tan_evalf(const ex & x)
399 if (is_exactly_a<numeric>(x))
400 return tan(ex_to<numeric>(x));
402 return tan(x).hold();
405 static ex tan_eval(const ex & x)
407 // tan(n/d*Pi) -> { all known non-nested radicals }
408 const ex SixtyExOverPi = _ex60*x/Pi;
410 if (SixtyExOverPi.info(info_flags::integer)) {
411 numeric z = mod(ex_to<numeric>(SixtyExOverPi),_num60);
413 // wrap to interval [0, Pi)
417 // wrap to interval [0, Pi/2)
421 if (z.is_equal(_num0)) // tan(0) -> 0
423 if (z.is_equal(_num5)) // tan(Pi/12) -> 2-sqrt(3)
424 return sign*(_ex2-sqrt(_ex3));
425 if (z.is_equal(_num10)) // tan(Pi/6) -> sqrt(3)/3
426 return sign*_ex1_3*sqrt(_ex3);
427 if (z.is_equal(_num15)) // tan(Pi/4) -> 1
429 if (z.is_equal(_num20)) // tan(Pi/3) -> sqrt(3)
430 return sign*sqrt(_ex3);
431 if (z.is_equal(_num25)) // tan(5/12*Pi) -> 2+sqrt(3)
432 return sign*(sqrt(_ex3)+_ex2);
433 if (z.is_equal(_num30)) // tan(Pi/2) -> infinity
434 throw (pole_error("tan_eval(): simple pole",1));
437 if (is_exactly_a<function>(x)) {
438 const ex &t = x.op(0);
440 if (is_ex_the_function(x, atan))
442 // tan(asin(x)) -> x/sqrt(1+x^2)
443 if (is_ex_the_function(x, asin))
444 return t*power(_ex1-power(t,_ex2),_ex_1_2);
445 // tan(acos(x)) -> sqrt(1-x^2)/x
446 if (is_ex_the_function(x, acos))
447 return power(t,_ex_1)*sqrt(_ex1-power(t,_ex2));
450 // tan(float) -> float
451 if (x.info(info_flags::numeric) && !x.info(info_flags::crational)) {
452 return tan(ex_to<numeric>(x));
455 return tan(x).hold();
458 static ex tan_deriv(const ex & x, unsigned deriv_param)
460 GINAC_ASSERT(deriv_param==0);
462 // d/dx tan(x) -> 1+tan(x)^2;
463 return (_ex1+power(tan(x),_ex2));
466 static ex tan_series(const ex &x,
467 const relational &rel,
471 GINAC_ASSERT(is_a<symbol>(rel.lhs()));
473 // Taylor series where there is no pole falls back to tan_deriv.
474 // On a pole simply expand sin(x)/cos(x).
475 const ex x_pt = x.subs(rel);
476 if (!(2*x_pt/Pi).info(info_flags::odd))
477 throw do_taylor(); // caught by function::series()
478 // if we got here we have to care for a simple pole
479 return (sin(x)/cos(x)).series(rel, order+2, options);
482 REGISTER_FUNCTION(tan, eval_func(tan_eval).
483 evalf_func(tan_evalf).
484 derivative_func(tan_deriv).
485 series_func(tan_series).
486 latex_name("\\tan"));
489 // inverse sine (arc sine)
492 static ex asin_evalf(const ex & x)
494 if (is_exactly_a<numeric>(x))
495 return asin(ex_to<numeric>(x));
497 return asin(x).hold();
500 static ex asin_eval(const ex & x)
502 if (x.info(info_flags::numeric)) {
507 if (x.is_equal(_ex1_2))
508 return numeric(1,6)*Pi;
510 if (x.is_equal(_ex1))
512 // asin(-1/2) -> -Pi/6
513 if (x.is_equal(_ex_1_2))
514 return numeric(-1,6)*Pi;
516 if (x.is_equal(_ex_1))
518 // asin(float) -> float
519 if (!x.info(info_flags::crational))
520 return asin(ex_to<numeric>(x));
523 return asin(x).hold();
526 static ex asin_deriv(const ex & x, unsigned deriv_param)
528 GINAC_ASSERT(deriv_param==0);
530 // d/dx asin(x) -> 1/sqrt(1-x^2)
531 return power(1-power(x,_ex2),_ex_1_2);
534 REGISTER_FUNCTION(asin, eval_func(asin_eval).
535 evalf_func(asin_evalf).
536 derivative_func(asin_deriv).
537 latex_name("\\arcsin"));
540 // inverse cosine (arc cosine)
543 static ex acos_evalf(const ex & x)
545 if (is_exactly_a<numeric>(x))
546 return acos(ex_to<numeric>(x));
548 return acos(x).hold();
551 static ex acos_eval(const ex & x)
553 if (x.info(info_flags::numeric)) {
555 if (x.is_equal(_ex1))
558 if (x.is_equal(_ex1_2))
563 // acos(-1/2) -> 2/3*Pi
564 if (x.is_equal(_ex_1_2))
565 return numeric(2,3)*Pi;
567 if (x.is_equal(_ex_1))
569 // acos(float) -> float
570 if (!x.info(info_flags::crational))
571 return acos(ex_to<numeric>(x));
574 return acos(x).hold();
577 static ex acos_deriv(const ex & x, unsigned deriv_param)
579 GINAC_ASSERT(deriv_param==0);
581 // d/dx acos(x) -> -1/sqrt(1-x^2)
582 return -power(1-power(x,_ex2),_ex_1_2);
585 REGISTER_FUNCTION(acos, eval_func(acos_eval).
586 evalf_func(acos_evalf).
587 derivative_func(acos_deriv).
588 latex_name("\\arccos"));
591 // inverse tangent (arc tangent)
594 static ex atan_evalf(const ex & x)
596 if (is_exactly_a<numeric>(x))
597 return atan(ex_to<numeric>(x));
599 return atan(x).hold();
602 static ex atan_eval(const ex & x)
604 if (x.info(info_flags::numeric)) {
609 if (x.is_equal(_ex1))
612 if (x.is_equal(_ex_1))
614 if (x.is_equal(I) || x.is_equal(-I))
615 throw (pole_error("atan_eval(): logarithmic pole",0));
616 // atan(float) -> float
617 if (!x.info(info_flags::crational))
618 return atan(ex_to<numeric>(x));
621 return atan(x).hold();
624 static ex atan_deriv(const ex & x, unsigned deriv_param)
626 GINAC_ASSERT(deriv_param==0);
628 // d/dx atan(x) -> 1/(1+x^2)
629 return power(_ex1+power(x,_ex2), _ex_1);
632 static ex atan_series(const ex &arg,
633 const relational &rel,
637 GINAC_ASSERT(is_a<symbol>(rel.lhs()));
639 // Taylor series where there is no pole or cut falls back to atan_deriv.
640 // There are two branch cuts, one runnig from I up the imaginary axis and
641 // one running from -I down the imaginary axis. The points I and -I are
643 // On the branch cuts and the poles series expand
644 // (log(1+I*x)-log(1-I*x))/(2*I)
646 const ex arg_pt = arg.subs(rel);
647 if (!(I*arg_pt).info(info_flags::real))
648 throw do_taylor(); // Re(x) != 0
649 if ((I*arg_pt).info(info_flags::real) && abs(I*arg_pt)<_ex1)
650 throw do_taylor(); // Re(x) == 0, but abs(x)<1
651 // care for the poles, using the defining formula for atan()...
652 if (arg_pt.is_equal(I) || arg_pt.is_equal(-I))
653 return ((log(1+I*arg)-log(1-I*arg))/(2*I)).series(rel, order, options);
654 if (!(options & series_options::suppress_branchcut)) {
656 // This is the branch cut: assemble the primitive series manually and
657 // then add the corresponding complex step function.
658 const symbol &s = ex_to<symbol>(rel.lhs());
659 const ex &point = rel.rhs();
661 const ex replarg = series(atan(arg), s==foo, order).subs(foo==point);
662 ex Order0correction = replarg.op(0)+csgn(arg)*Pi*_ex_1_2;
664 Order0correction += log((I*arg_pt+_ex_1)/(I*arg_pt+_ex1))*I*_ex_1_2;
666 Order0correction += log((I*arg_pt+_ex1)/(I*arg_pt+_ex_1))*I*_ex1_2;
668 seq.push_back(expair(Order0correction, _ex0));
669 seq.push_back(expair(Order(_ex1), order));
670 return series(replarg - pseries(rel, seq), rel, order);
675 REGISTER_FUNCTION(atan, eval_func(atan_eval).
676 evalf_func(atan_evalf).
677 derivative_func(atan_deriv).
678 series_func(atan_series).
679 latex_name("\\arctan"));
682 // inverse tangent (atan2(y,x))
685 static ex atan2_evalf(const ex &y, const ex &x)
687 if (is_exactly_a<numeric>(y) && is_exactly_a<numeric>(x))
688 return atan2(ex_to<numeric>(y), ex_to<numeric>(x));
690 return atan2(y, x).hold();
693 static ex atan2_eval(const ex & y, const ex & x)
695 if (y.info(info_flags::numeric) && !y.info(info_flags::crational) &&
696 x.info(info_flags::numeric) && !x.info(info_flags::crational)) {
697 return atan2_evalf(y,x);
700 return atan2(y,x).hold();
703 static ex atan2_deriv(const ex & y, const ex & x, unsigned deriv_param)
705 GINAC_ASSERT(deriv_param<2);
707 if (deriv_param==0) {
709 return x*power(power(x,_ex2)+power(y,_ex2),_ex_1);
712 return -y*power(power(x,_ex2)+power(y,_ex2),_ex_1);
715 REGISTER_FUNCTION(atan2, eval_func(atan2_eval).
716 evalf_func(atan2_evalf).
717 derivative_func(atan2_deriv));
720 // hyperbolic sine (trigonometric function)
723 static ex sinh_evalf(const ex & x)
725 if (is_exactly_a<numeric>(x))
726 return sinh(ex_to<numeric>(x));
728 return sinh(x).hold();
731 static ex sinh_eval(const ex & x)
733 if (x.info(info_flags::numeric)) {
734 if (x.is_zero()) // sinh(0) -> 0
736 if (!x.info(info_flags::crational)) // sinh(float) -> float
737 return sinh(ex_to<numeric>(x));
740 if ((x/Pi).info(info_flags::numeric) &&
741 ex_to<numeric>(x/Pi).real().is_zero()) // sinh(I*x) -> I*sin(x)
744 if (is_exactly_a<function>(x)) {
745 const ex &t = x.op(0);
746 // sinh(asinh(x)) -> x
747 if (is_ex_the_function(x, asinh))
749 // sinh(acosh(x)) -> sqrt(x-1) * sqrt(x+1)
750 if (is_ex_the_function(x, acosh))
751 return sqrt(t-_ex1)*sqrt(t+_ex1);
752 // sinh(atanh(x)) -> x/sqrt(1-x^2)
753 if (is_ex_the_function(x, atanh))
754 return t*power(_ex1-power(t,_ex2),_ex_1_2);
757 return sinh(x).hold();
760 static ex sinh_deriv(const ex & x, unsigned deriv_param)
762 GINAC_ASSERT(deriv_param==0);
764 // d/dx sinh(x) -> cosh(x)
768 REGISTER_FUNCTION(sinh, eval_func(sinh_eval).
769 evalf_func(sinh_evalf).
770 derivative_func(sinh_deriv).
771 latex_name("\\sinh"));
774 // hyperbolic cosine (trigonometric function)
777 static ex cosh_evalf(const ex & x)
779 if (is_exactly_a<numeric>(x))
780 return cosh(ex_to<numeric>(x));
782 return cosh(x).hold();
785 static ex cosh_eval(const ex & x)
787 if (x.info(info_flags::numeric)) {
788 if (x.is_zero()) // cosh(0) -> 1
790 if (!x.info(info_flags::crational)) // cosh(float) -> float
791 return cosh(ex_to<numeric>(x));
794 if ((x/Pi).info(info_flags::numeric) &&
795 ex_to<numeric>(x/Pi).real().is_zero()) // cosh(I*x) -> cos(x)
798 if (is_exactly_a<function>(x)) {
799 const ex &t = x.op(0);
800 // cosh(acosh(x)) -> x
801 if (is_ex_the_function(x, acosh))
803 // cosh(asinh(x)) -> sqrt(1+x^2)
804 if (is_ex_the_function(x, asinh))
805 return sqrt(_ex1+power(t,_ex2));
806 // cosh(atanh(x)) -> 1/sqrt(1-x^2)
807 if (is_ex_the_function(x, atanh))
808 return power(_ex1-power(t,_ex2),_ex_1_2);
811 return cosh(x).hold();
814 static ex cosh_deriv(const ex & x, unsigned deriv_param)
816 GINAC_ASSERT(deriv_param==0);
818 // d/dx cosh(x) -> sinh(x)
822 REGISTER_FUNCTION(cosh, eval_func(cosh_eval).
823 evalf_func(cosh_evalf).
824 derivative_func(cosh_deriv).
825 latex_name("\\cosh"));
828 // hyperbolic tangent (trigonometric function)
831 static ex tanh_evalf(const ex & x)
833 if (is_exactly_a<numeric>(x))
834 return tanh(ex_to<numeric>(x));
836 return tanh(x).hold();
839 static ex tanh_eval(const ex & x)
841 if (x.info(info_flags::numeric)) {
842 if (x.is_zero()) // tanh(0) -> 0
844 if (!x.info(info_flags::crational)) // tanh(float) -> float
845 return tanh(ex_to<numeric>(x));
848 if ((x/Pi).info(info_flags::numeric) &&
849 ex_to<numeric>(x/Pi).real().is_zero()) // tanh(I*x) -> I*tan(x);
852 if (is_exactly_a<function>(x)) {
853 const ex &t = x.op(0);
854 // tanh(atanh(x)) -> x
855 if (is_ex_the_function(x, atanh))
857 // tanh(asinh(x)) -> x/sqrt(1+x^2)
858 if (is_ex_the_function(x, asinh))
859 return t*power(_ex1+power(t,_ex2),_ex_1_2);
860 // tanh(acosh(x)) -> sqrt(x-1)*sqrt(x+1)/x
861 if (is_ex_the_function(x, acosh))
862 return sqrt(t-_ex1)*sqrt(t+_ex1)*power(t,_ex_1);
865 return tanh(x).hold();
868 static ex tanh_deriv(const ex & x, unsigned deriv_param)
870 GINAC_ASSERT(deriv_param==0);
872 // d/dx tanh(x) -> 1-tanh(x)^2
873 return _ex1-power(tanh(x),_ex2);
876 static ex tanh_series(const ex &x,
877 const relational &rel,
881 GINAC_ASSERT(is_a<symbol>(rel.lhs()));
883 // Taylor series where there is no pole falls back to tanh_deriv.
884 // On a pole simply expand sinh(x)/cosh(x).
885 const ex x_pt = x.subs(rel);
886 if (!(2*I*x_pt/Pi).info(info_flags::odd))
887 throw do_taylor(); // caught by function::series()
888 // if we got here we have to care for a simple pole
889 return (sinh(x)/cosh(x)).series(rel, order+2, options);
892 REGISTER_FUNCTION(tanh, eval_func(tanh_eval).
893 evalf_func(tanh_evalf).
894 derivative_func(tanh_deriv).
895 series_func(tanh_series).
896 latex_name("\\tanh"));
899 // inverse hyperbolic sine (trigonometric function)
902 static ex asinh_evalf(const ex & x)
904 if (is_exactly_a<numeric>(x))
905 return asinh(ex_to<numeric>(x));
907 return asinh(x).hold();
910 static ex asinh_eval(const ex & x)
912 if (x.info(info_flags::numeric)) {
916 // asinh(float) -> float
917 if (!x.info(info_flags::crational))
918 return asinh(ex_to<numeric>(x));
921 return asinh(x).hold();
924 static ex asinh_deriv(const ex & x, unsigned deriv_param)
926 GINAC_ASSERT(deriv_param==0);
928 // d/dx asinh(x) -> 1/sqrt(1+x^2)
929 return power(_ex1+power(x,_ex2),_ex_1_2);
932 REGISTER_FUNCTION(asinh, eval_func(asinh_eval).
933 evalf_func(asinh_evalf).
934 derivative_func(asinh_deriv));
937 // inverse hyperbolic cosine (trigonometric function)
940 static ex acosh_evalf(const ex & x)
942 if (is_exactly_a<numeric>(x))
943 return acosh(ex_to<numeric>(x));
945 return acosh(x).hold();
948 static ex acosh_eval(const ex & x)
950 if (x.info(info_flags::numeric)) {
951 // acosh(0) -> Pi*I/2
953 return Pi*I*numeric(1,2);
955 if (x.is_equal(_ex1))
958 if (x.is_equal(_ex_1))
960 // acosh(float) -> float
961 if (!x.info(info_flags::crational))
962 return acosh(ex_to<numeric>(x));
965 return acosh(x).hold();
968 static ex acosh_deriv(const ex & x, unsigned deriv_param)
970 GINAC_ASSERT(deriv_param==0);
972 // d/dx acosh(x) -> 1/(sqrt(x-1)*sqrt(x+1))
973 return power(x+_ex_1,_ex_1_2)*power(x+_ex1,_ex_1_2);
976 REGISTER_FUNCTION(acosh, eval_func(acosh_eval).
977 evalf_func(acosh_evalf).
978 derivative_func(acosh_deriv));
981 // inverse hyperbolic tangent (trigonometric function)
984 static ex atanh_evalf(const ex & x)
986 if (is_exactly_a<numeric>(x))
987 return atanh(ex_to<numeric>(x));
989 return atanh(x).hold();
992 static ex atanh_eval(const ex & x)
994 if (x.info(info_flags::numeric)) {
998 // atanh({+|-}1) -> throw
999 if (x.is_equal(_ex1) || x.is_equal(_ex_1))
1000 throw (pole_error("atanh_eval(): logarithmic pole",0));
1001 // atanh(float) -> float
1002 if (!x.info(info_flags::crational))
1003 return atanh(ex_to<numeric>(x));
1006 return atanh(x).hold();
1009 static ex atanh_deriv(const ex & x, unsigned deriv_param)
1011 GINAC_ASSERT(deriv_param==0);
1013 // d/dx atanh(x) -> 1/(1-x^2)
1014 return power(_ex1-power(x,_ex2),_ex_1);
1017 static ex atanh_series(const ex &arg,
1018 const relational &rel,
1022 GINAC_ASSERT(is_a<symbol>(rel.lhs()));
1024 // Taylor series where there is no pole or cut falls back to atanh_deriv.
1025 // There are two branch cuts, one runnig from 1 up the real axis and one
1026 // one running from -1 down the real axis. The points 1 and -1 are poles
1027 // On the branch cuts and the poles series expand
1028 // (log(1+x)-log(1-x))/2
1030 const ex arg_pt = arg.subs(rel);
1031 if (!(arg_pt).info(info_flags::real))
1032 throw do_taylor(); // Im(x) != 0
1033 if ((arg_pt).info(info_flags::real) && abs(arg_pt)<_ex1)
1034 throw do_taylor(); // Im(x) == 0, but abs(x)<1
1035 // care for the poles, using the defining formula for atanh()...
1036 if (arg_pt.is_equal(_ex1) || arg_pt.is_equal(_ex_1))
1037 return ((log(_ex1+arg)-log(_ex1-arg))*_ex1_2).series(rel, order, options);
1038 // ...and the branch cuts (the discontinuity at the cut being just I*Pi)
1039 if (!(options & series_options::suppress_branchcut)) {
1041 // This is the branch cut: assemble the primitive series manually and
1042 // then add the corresponding complex step function.
1043 const symbol &s = ex_to<symbol>(rel.lhs());
1044 const ex &point = rel.rhs();
1046 const ex replarg = series(atanh(arg), s==foo, order).subs(foo==point);
1047 ex Order0correction = replarg.op(0)+csgn(I*arg)*Pi*I*_ex1_2;
1049 Order0correction += log((arg_pt+_ex_1)/(arg_pt+_ex1))*_ex1_2;
1051 Order0correction += log((arg_pt+_ex1)/(arg_pt+_ex_1))*_ex_1_2;
1053 seq.push_back(expair(Order0correction, _ex0));
1054 seq.push_back(expair(Order(_ex1), order));
1055 return series(replarg - pseries(rel, seq), rel, order);
1060 REGISTER_FUNCTION(atanh, eval_func(atanh_eval).
1061 evalf_func(atanh_evalf).
1062 derivative_func(atanh_deriv).
1063 series_func(atanh_series));
1066 } // namespace GiNaC