1 /** @file inifcns_trans.cpp
3 * Implementation of transcendental (and trigonometric and hyperbolic)
7 * GiNaC Copyright (C) 1999-2001 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 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)) {
126 if (t.info(info_flags::numeric)) {
127 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 static ex log_series(const ex &arg,
145 const relational &rel,
149 GINAC_ASSERT(is_exactly_a<symbol>(rel.lhs()));
151 bool must_expand_arg = false;
152 // maybe substitution of rel into arg fails because of a pole
154 arg_pt = arg.subs(rel);
155 } catch (pole_error) {
156 must_expand_arg = true;
158 // or we are at the branch point anyways
159 if (arg_pt.is_zero())
160 must_expand_arg = true;
162 if (must_expand_arg) {
164 // This is the branch point: Series expand the argument first, then
165 // trivially factorize it to isolate that part which has constant
166 // leading coefficient in this fashion:
167 // x^n + x^(n+1) +...+ Order(x^(n+m)) -> x^n * (1 + x +...+ Order(x^m)).
168 // Return a plain n*log(x) for the x^n part and series expand the
169 // other part. Add them together and reexpand again in order to have
170 // one unnested pseries object. All this also works for negative n.
171 pseries argser; // series expansion of log's argument
172 unsigned extra_ord = 0; // extra expansion order
174 // oops, the argument expanded to a pure Order(x^something)...
175 argser = ex_to<pseries>(arg.series(rel, order+extra_ord, options));
177 } while (!argser.is_terminating() && argser.nops()==1);
179 const symbol &s = ex_to<symbol>(rel.lhs());
180 const ex point = rel.rhs();
181 const int n = argser.ldegree(s);
183 // construct what we carelessly called the n*log(x) term above
184 const ex coeff = argser.coeff(s, n);
185 // expand the log, but only if coeff is real and > 0, since otherwise
186 // it would make the branch cut run into the wrong direction
187 if (coeff.info(info_flags::positive))
188 seq.push_back(expair(n*log(s-point)+log(coeff), _ex0));
190 seq.push_back(expair(log(coeff*pow(s-point, n)), _ex0));
192 if (!argser.is_terminating() || argser.nops()!=1) {
193 // in this case n more (or less) terms are needed
194 // (sadly, to generate them, we have to start from the beginning)
195 const ex newarg = ex_to<pseries>((arg/coeff).series(rel, order+n, options)).shift_exponents(-n).convert_to_poly(true);
196 return pseries(rel, seq).add_series(ex_to<pseries>(log(newarg).series(rel, order, options)));
197 } else // it was a monomial
198 return pseries(rel, seq);
200 if (!(options & series_options::suppress_branchcut) &&
201 arg_pt.info(info_flags::negative)) {
203 // This is the branch cut: assemble the primitive series manually and
204 // then add the corresponding complex step function.
205 const symbol &s = ex_to<symbol>(rel.lhs());
206 const ex point = rel.rhs();
208 const ex replarg = series(log(arg), s==foo, order).subs(foo==point);
210 seq.push_back(expair(-I*csgn(arg*I)*Pi, _ex0));
211 seq.push_back(expair(Order(_ex1), order));
212 return series(replarg - I*Pi + pseries(rel, seq), rel, order);
214 throw do_taylor(); // caught by function::series()
217 REGISTER_FUNCTION(log, eval_func(log_eval).
218 evalf_func(log_evalf).
219 derivative_func(log_deriv).
220 series_func(log_series).
224 // sine (trigonometric function)
227 static ex sin_evalf(const ex & x)
229 if (is_exactly_a<numeric>(x))
230 return sin(ex_to<numeric>(x));
232 return sin(x).hold();
235 static ex sin_eval(const ex & x)
237 // sin(n/d*Pi) -> { all known non-nested radicals }
238 const ex SixtyExOverPi = _ex60*x/Pi;
240 if (SixtyExOverPi.info(info_flags::integer)) {
241 numeric z = mod(ex_to<numeric>(SixtyExOverPi),_num120);
243 // wrap to interval [0, Pi)
248 // wrap to interval [0, Pi/2)
251 if (z.is_equal(_num0)) // sin(0) -> 0
253 if (z.is_equal(_num5)) // sin(Pi/12) -> sqrt(6)/4*(1-sqrt(3)/3)
254 return sign*_ex1_4*sqrt(_ex6)*(_ex1+_ex_1_3*sqrt(_ex3));
255 if (z.is_equal(_num6)) // sin(Pi/10) -> sqrt(5)/4-1/4
256 return sign*(_ex1_4*sqrt(_ex5)+_ex_1_4);
257 if (z.is_equal(_num10)) // sin(Pi/6) -> 1/2
259 if (z.is_equal(_num15)) // sin(Pi/4) -> sqrt(2)/2
260 return sign*_ex1_2*sqrt(_ex2);
261 if (z.is_equal(_num18)) // sin(3/10*Pi) -> sqrt(5)/4+1/4
262 return sign*(_ex1_4*sqrt(_ex5)+_ex1_4);
263 if (z.is_equal(_num20)) // sin(Pi/3) -> sqrt(3)/2
264 return sign*_ex1_2*sqrt(_ex3);
265 if (z.is_equal(_num25)) // sin(5/12*Pi) -> sqrt(6)/4*(1+sqrt(3)/3)
266 return sign*_ex1_4*sqrt(_ex6)*(_ex1+_ex1_3*sqrt(_ex3));
267 if (z.is_equal(_num30)) // sin(Pi/2) -> 1
271 if (is_exactly_a<function>(x)) {
274 if (is_ex_the_function(x, asin))
276 // sin(acos(x)) -> sqrt(1-x^2)
277 if (is_ex_the_function(x, acos))
278 return sqrt(_ex1-power(t,_ex2));
279 // sin(atan(x)) -> x/sqrt(1+x^2)
280 if (is_ex_the_function(x, atan))
281 return t*power(_ex1+power(t,_ex2),_ex_1_2);
284 // sin(float) -> float
285 if (x.info(info_flags::numeric) && !x.info(info_flags::crational))
286 return sin(ex_to<numeric>(x));
288 return sin(x).hold();
291 static ex sin_deriv(const ex & x, unsigned deriv_param)
293 GINAC_ASSERT(deriv_param==0);
295 // d/dx sin(x) -> cos(x)
299 REGISTER_FUNCTION(sin, eval_func(sin_eval).
300 evalf_func(sin_evalf).
301 derivative_func(sin_deriv).
302 latex_name("\\sin"));
305 // cosine (trigonometric function)
308 static ex cos_evalf(const ex & x)
310 if (is_exactly_a<numeric>(x))
311 return cos(ex_to<numeric>(x));
313 return cos(x).hold();
316 static ex cos_eval(const ex & x)
318 // cos(n/d*Pi) -> { all known non-nested radicals }
319 const ex SixtyExOverPi = _ex60*x/Pi;
321 if (SixtyExOverPi.info(info_flags::integer)) {
322 numeric z = mod(ex_to<numeric>(SixtyExOverPi),_num120);
324 // wrap to interval [0, Pi)
328 // wrap to interval [0, Pi/2)
332 if (z.is_equal(_num0)) // cos(0) -> 1
334 if (z.is_equal(_num5)) // cos(Pi/12) -> sqrt(6)/4*(1+sqrt(3)/3)
335 return sign*_ex1_4*sqrt(_ex6)*(_ex1+_ex1_3*sqrt(_ex3));
336 if (z.is_equal(_num10)) // cos(Pi/6) -> sqrt(3)/2
337 return sign*_ex1_2*sqrt(_ex3);
338 if (z.is_equal(_num12)) // cos(Pi/5) -> sqrt(5)/4+1/4
339 return sign*(_ex1_4*sqrt(_ex5)+_ex1_4);
340 if (z.is_equal(_num15)) // cos(Pi/4) -> sqrt(2)/2
341 return sign*_ex1_2*sqrt(_ex2);
342 if (z.is_equal(_num20)) // cos(Pi/3) -> 1/2
344 if (z.is_equal(_num24)) // cos(2/5*Pi) -> sqrt(5)/4-1/4x
345 return sign*(_ex1_4*sqrt(_ex5)+_ex_1_4);
346 if (z.is_equal(_num25)) // cos(5/12*Pi) -> sqrt(6)/4*(1-sqrt(3)/3)
347 return sign*_ex1_4*sqrt(_ex6)*(_ex1+_ex_1_3*sqrt(_ex3));
348 if (z.is_equal(_num30)) // cos(Pi/2) -> 0
352 if (is_exactly_a<function>(x)) {
355 if (is_ex_the_function(x, acos))
357 // cos(asin(x)) -> sqrt(1-x^2)
358 if (is_ex_the_function(x, asin))
359 return sqrt(_ex1-power(t,_ex2));
360 // cos(atan(x)) -> 1/sqrt(1+x^2)
361 if (is_ex_the_function(x, atan))
362 return power(_ex1+power(t,_ex2),_ex_1_2);
365 // cos(float) -> float
366 if (x.info(info_flags::numeric) && !x.info(info_flags::crational))
367 return cos(ex_to<numeric>(x));
369 return cos(x).hold();
372 static ex cos_deriv(const ex & x, unsigned deriv_param)
374 GINAC_ASSERT(deriv_param==0);
376 // d/dx cos(x) -> -sin(x)
380 REGISTER_FUNCTION(cos, eval_func(cos_eval).
381 evalf_func(cos_evalf).
382 derivative_func(cos_deriv).
383 latex_name("\\cos"));
386 // tangent (trigonometric function)
389 static ex tan_evalf(const ex & x)
391 if (is_exactly_a<numeric>(x))
392 return tan(ex_to<numeric>(x));
394 return tan(x).hold();
397 static ex tan_eval(const ex & x)
399 // tan(n/d*Pi) -> { all known non-nested radicals }
400 const ex SixtyExOverPi = _ex60*x/Pi;
402 if (SixtyExOverPi.info(info_flags::integer)) {
403 numeric z = mod(ex_to<numeric>(SixtyExOverPi),_num60);
405 // wrap to interval [0, Pi)
409 // wrap to interval [0, Pi/2)
413 if (z.is_equal(_num0)) // tan(0) -> 0
415 if (z.is_equal(_num5)) // tan(Pi/12) -> 2-sqrt(3)
416 return sign*(_ex2-sqrt(_ex3));
417 if (z.is_equal(_num10)) // tan(Pi/6) -> sqrt(3)/3
418 return sign*_ex1_3*sqrt(_ex3);
419 if (z.is_equal(_num15)) // tan(Pi/4) -> 1
421 if (z.is_equal(_num20)) // tan(Pi/3) -> sqrt(3)
422 return sign*sqrt(_ex3);
423 if (z.is_equal(_num25)) // tan(5/12*Pi) -> 2+sqrt(3)
424 return sign*(sqrt(_ex3)+_ex2);
425 if (z.is_equal(_num30)) // tan(Pi/2) -> infinity
426 throw (pole_error("tan_eval(): simple pole",1));
429 if (is_exactly_a<function>(x)) {
432 if (is_ex_the_function(x, atan))
434 // tan(asin(x)) -> x/sqrt(1+x^2)
435 if (is_ex_the_function(x, asin))
436 return t*power(_ex1-power(t,_ex2),_ex_1_2);
437 // tan(acos(x)) -> sqrt(1-x^2)/x
438 if (is_ex_the_function(x, acos))
439 return power(t,_ex_1)*sqrt(_ex1-power(t,_ex2));
442 // tan(float) -> float
443 if (x.info(info_flags::numeric) && !x.info(info_flags::crational)) {
444 return tan(ex_to<numeric>(x));
447 return tan(x).hold();
450 static ex tan_deriv(const ex & x, unsigned deriv_param)
452 GINAC_ASSERT(deriv_param==0);
454 // d/dx tan(x) -> 1+tan(x)^2;
455 return (_ex1+power(tan(x),_ex2));
458 static ex tan_series(const ex &x,
459 const relational &rel,
463 GINAC_ASSERT(is_exactly_a<symbol>(rel.lhs()));
465 // Taylor series where there is no pole falls back to tan_deriv.
466 // On a pole simply expand sin(x)/cos(x).
467 const ex x_pt = x.subs(rel);
468 if (!(2*x_pt/Pi).info(info_flags::odd))
469 throw do_taylor(); // caught by function::series()
470 // if we got here we have to care for a simple pole
471 return (sin(x)/cos(x)).series(rel, order+2, options);
474 REGISTER_FUNCTION(tan, eval_func(tan_eval).
475 evalf_func(tan_evalf).
476 derivative_func(tan_deriv).
477 series_func(tan_series).
478 latex_name("\\tan"));
481 // inverse sine (arc sine)
484 static ex asin_evalf(const ex & x)
486 if (is_exactly_a<numeric>(x))
487 return asin(ex_to<numeric>(x));
489 return asin(x).hold();
492 static ex asin_eval(const ex & x)
494 if (x.info(info_flags::numeric)) {
499 if (x.is_equal(_ex1_2))
500 return numeric(1,6)*Pi;
502 if (x.is_equal(_ex1))
504 // asin(-1/2) -> -Pi/6
505 if (x.is_equal(_ex_1_2))
506 return numeric(-1,6)*Pi;
508 if (x.is_equal(_ex_1))
510 // asin(float) -> float
511 if (!x.info(info_flags::crational))
512 return asin(ex_to<numeric>(x));
515 return asin(x).hold();
518 static ex asin_deriv(const ex & x, unsigned deriv_param)
520 GINAC_ASSERT(deriv_param==0);
522 // d/dx asin(x) -> 1/sqrt(1-x^2)
523 return power(1-power(x,_ex2),_ex_1_2);
526 REGISTER_FUNCTION(asin, eval_func(asin_eval).
527 evalf_func(asin_evalf).
528 derivative_func(asin_deriv).
529 latex_name("\\arcsin"));
532 // inverse cosine (arc cosine)
535 static ex acos_evalf(const ex & x)
537 if (is_exactly_a<numeric>(x))
538 return acos(ex_to<numeric>(x));
540 return acos(x).hold();
543 static ex acos_eval(const ex & x)
545 if (x.info(info_flags::numeric)) {
547 if (x.is_equal(_ex1))
550 if (x.is_equal(_ex1_2))
555 // acos(-1/2) -> 2/3*Pi
556 if (x.is_equal(_ex_1_2))
557 return numeric(2,3)*Pi;
559 if (x.is_equal(_ex_1))
561 // acos(float) -> float
562 if (!x.info(info_flags::crational))
563 return acos(ex_to<numeric>(x));
566 return acos(x).hold();
569 static ex acos_deriv(const ex & x, unsigned deriv_param)
571 GINAC_ASSERT(deriv_param==0);
573 // d/dx acos(x) -> -1/sqrt(1-x^2)
574 return -power(1-power(x,_ex2),_ex_1_2);
577 REGISTER_FUNCTION(acos, eval_func(acos_eval).
578 evalf_func(acos_evalf).
579 derivative_func(acos_deriv).
580 latex_name("\\arccos"));
583 // inverse tangent (arc tangent)
586 static ex atan_evalf(const ex & x)
588 if (is_exactly_a<numeric>(x))
589 return atan(ex_to<numeric>(x));
591 return atan(x).hold();
594 static ex atan_eval(const ex & x)
596 if (x.info(info_flags::numeric)) {
601 if (x.is_equal(_ex1))
604 if (x.is_equal(_ex_1))
606 if (x.is_equal(I) || x.is_equal(-I))
607 throw (pole_error("atan_eval(): logarithmic pole",0));
608 // atan(float) -> float
609 if (!x.info(info_flags::crational))
610 return atan(ex_to<numeric>(x));
613 return atan(x).hold();
616 static ex atan_deriv(const ex & x, unsigned deriv_param)
618 GINAC_ASSERT(deriv_param==0);
620 // d/dx atan(x) -> 1/(1+x^2)
621 return power(_ex1+power(x,_ex2), _ex_1);
624 static ex atan_series(const ex &arg,
625 const relational &rel,
629 GINAC_ASSERT(is_exactly_a<symbol>(rel.lhs()));
631 // Taylor series where there is no pole or cut falls back to atan_deriv.
632 // There are two branch cuts, one runnig from I up the imaginary axis and
633 // one running from -I down the imaginary axis. The points I and -I are
635 // On the branch cuts and the poles series expand
636 // (log(1+I*x)-log(1-I*x))/(2*I)
638 const ex arg_pt = arg.subs(rel);
639 if (!(I*arg_pt).info(info_flags::real))
640 throw do_taylor(); // Re(x) != 0
641 if ((I*arg_pt).info(info_flags::real) && abs(I*arg_pt)<_ex1)
642 throw do_taylor(); // Re(x) == 0, but abs(x)<1
643 // care for the poles, using the defining formula for atan()...
644 if (arg_pt.is_equal(I) || arg_pt.is_equal(-I))
645 return ((log(1+I*arg)-log(1-I*arg))/(2*I)).series(rel, order, options);
646 if (!(options & series_options::suppress_branchcut)) {
648 // This is the branch cut: assemble the primitive series manually and
649 // then add the corresponding complex step function.
650 const symbol &s = ex_to<symbol>(rel.lhs());
651 const ex point = rel.rhs();
653 const ex replarg = series(atan(arg), s==foo, order).subs(foo==point);
654 ex Order0correction = replarg.op(0)+csgn(arg)*Pi*_ex_1_2;
656 Order0correction += log((I*arg_pt+_ex_1)/(I*arg_pt+_ex1))*I*_ex_1_2;
658 Order0correction += log((I*arg_pt+_ex1)/(I*arg_pt+_ex_1))*I*_ex1_2;
660 seq.push_back(expair(Order0correction, _ex0));
661 seq.push_back(expair(Order(_ex1), order));
662 return series(replarg - pseries(rel, seq), rel, order);
667 REGISTER_FUNCTION(atan, eval_func(atan_eval).
668 evalf_func(atan_evalf).
669 derivative_func(atan_deriv).
670 series_func(atan_series).
671 latex_name("\\arctan"));
674 // inverse tangent (atan2(y,x))
677 static ex atan2_evalf(const ex &y, const ex &x)
679 if (is_exactly_a<numeric>(y) && is_exactly_a<numeric>(x))
680 return atan2(ex_to<numeric>(y), ex_to<numeric>(x));
682 return atan2(y, x).hold();
685 static ex atan2_eval(const ex & y, const ex & x)
687 if (y.info(info_flags::numeric) && !y.info(info_flags::crational) &&
688 x.info(info_flags::numeric) && !x.info(info_flags::crational)) {
689 return atan2_evalf(y,x);
692 return atan2(y,x).hold();
695 static ex atan2_deriv(const ex & y, const ex & x, unsigned deriv_param)
697 GINAC_ASSERT(deriv_param<2);
699 if (deriv_param==0) {
701 return x*power(power(x,_ex2)+power(y,_ex2),_ex_1);
704 return -y*power(power(x,_ex2)+power(y,_ex2),_ex_1);
707 REGISTER_FUNCTION(atan2, eval_func(atan2_eval).
708 evalf_func(atan2_evalf).
709 derivative_func(atan2_deriv));
712 // hyperbolic sine (trigonometric function)
715 static ex sinh_evalf(const ex & x)
717 if (is_exactly_a<numeric>(x))
718 return sinh(ex_to<numeric>(x));
720 return sinh(x).hold();
723 static ex sinh_eval(const ex & x)
725 if (x.info(info_flags::numeric)) {
726 if (x.is_zero()) // sinh(0) -> 0
728 if (!x.info(info_flags::crational)) // sinh(float) -> float
729 return sinh(ex_to<numeric>(x));
732 if ((x/Pi).info(info_flags::numeric) &&
733 ex_to<numeric>(x/Pi).real().is_zero()) // sinh(I*x) -> I*sin(x)
736 if (is_exactly_a<function>(x)) {
738 // sinh(asinh(x)) -> x
739 if (is_ex_the_function(x, asinh))
741 // sinh(acosh(x)) -> sqrt(x-1) * sqrt(x+1)
742 if (is_ex_the_function(x, acosh))
743 return sqrt(t-_ex1)*sqrt(t+_ex1);
744 // sinh(atanh(x)) -> x/sqrt(1-x^2)
745 if (is_ex_the_function(x, atanh))
746 return t*power(_ex1-power(t,_ex2),_ex_1_2);
749 return sinh(x).hold();
752 static ex sinh_deriv(const ex & x, unsigned deriv_param)
754 GINAC_ASSERT(deriv_param==0);
756 // d/dx sinh(x) -> cosh(x)
760 REGISTER_FUNCTION(sinh, eval_func(sinh_eval).
761 evalf_func(sinh_evalf).
762 derivative_func(sinh_deriv).
763 latex_name("\\sinh"));
766 // hyperbolic cosine (trigonometric function)
769 static ex cosh_evalf(const ex & x)
771 if (is_exactly_a<numeric>(x))
772 return cosh(ex_to<numeric>(x));
774 return cosh(x).hold();
777 static ex cosh_eval(const ex & x)
779 if (x.info(info_flags::numeric)) {
780 if (x.is_zero()) // cosh(0) -> 1
782 if (!x.info(info_flags::crational)) // cosh(float) -> float
783 return cosh(ex_to<numeric>(x));
786 if ((x/Pi).info(info_flags::numeric) &&
787 ex_to<numeric>(x/Pi).real().is_zero()) // cosh(I*x) -> cos(x)
790 if (is_exactly_a<function>(x)) {
792 // cosh(acosh(x)) -> x
793 if (is_ex_the_function(x, acosh))
795 // cosh(asinh(x)) -> sqrt(1+x^2)
796 if (is_ex_the_function(x, asinh))
797 return sqrt(_ex1+power(t,_ex2));
798 // cosh(atanh(x)) -> 1/sqrt(1-x^2)
799 if (is_ex_the_function(x, atanh))
800 return power(_ex1-power(t,_ex2),_ex_1_2);
803 return cosh(x).hold();
806 static ex cosh_deriv(const ex & x, unsigned deriv_param)
808 GINAC_ASSERT(deriv_param==0);
810 // d/dx cosh(x) -> sinh(x)
814 REGISTER_FUNCTION(cosh, eval_func(cosh_eval).
815 evalf_func(cosh_evalf).
816 derivative_func(cosh_deriv).
817 latex_name("\\cosh"));
820 // hyperbolic tangent (trigonometric function)
823 static ex tanh_evalf(const ex & x)
825 if (is_exactly_a<numeric>(x))
826 return tanh(ex_to<numeric>(x));
828 return tanh(x).hold();
831 static ex tanh_eval(const ex & x)
833 if (x.info(info_flags::numeric)) {
834 if (x.is_zero()) // tanh(0) -> 0
836 if (!x.info(info_flags::crational)) // tanh(float) -> float
837 return tanh(ex_to<numeric>(x));
840 if ((x/Pi).info(info_flags::numeric) &&
841 ex_to<numeric>(x/Pi).real().is_zero()) // tanh(I*x) -> I*tan(x);
844 if (is_exactly_a<function>(x)) {
846 // tanh(atanh(x)) -> x
847 if (is_ex_the_function(x, atanh))
849 // tanh(asinh(x)) -> x/sqrt(1+x^2)
850 if (is_ex_the_function(x, asinh))
851 return t*power(_ex1+power(t,_ex2),_ex_1_2);
852 // tanh(acosh(x)) -> sqrt(x-1)*sqrt(x+1)/x
853 if (is_ex_the_function(x, acosh))
854 return sqrt(t-_ex1)*sqrt(t+_ex1)*power(t,_ex_1);
857 return tanh(x).hold();
860 static ex tanh_deriv(const ex & x, unsigned deriv_param)
862 GINAC_ASSERT(deriv_param==0);
864 // d/dx tanh(x) -> 1-tanh(x)^2
865 return _ex1-power(tanh(x),_ex2);
868 static ex tanh_series(const ex &x,
869 const relational &rel,
873 GINAC_ASSERT(is_exactly_a<symbol>(rel.lhs()));
875 // Taylor series where there is no pole falls back to tanh_deriv.
876 // On a pole simply expand sinh(x)/cosh(x).
877 const ex x_pt = x.subs(rel);
878 if (!(2*I*x_pt/Pi).info(info_flags::odd))
879 throw do_taylor(); // caught by function::series()
880 // if we got here we have to care for a simple pole
881 return (sinh(x)/cosh(x)).series(rel, order+2, options);
884 REGISTER_FUNCTION(tanh, eval_func(tanh_eval).
885 evalf_func(tanh_evalf).
886 derivative_func(tanh_deriv).
887 series_func(tanh_series).
888 latex_name("\\tanh"));
891 // inverse hyperbolic sine (trigonometric function)
894 static ex asinh_evalf(const ex & x)
896 if (is_exactly_a<numeric>(x))
897 return asinh(ex_to<numeric>(x));
899 return asinh(x).hold();
902 static ex asinh_eval(const ex & x)
904 if (x.info(info_flags::numeric)) {
908 // asinh(float) -> float
909 if (!x.info(info_flags::crational))
910 return asinh(ex_to<numeric>(x));
913 return asinh(x).hold();
916 static ex asinh_deriv(const ex & x, unsigned deriv_param)
918 GINAC_ASSERT(deriv_param==0);
920 // d/dx asinh(x) -> 1/sqrt(1+x^2)
921 return power(_ex1+power(x,_ex2),_ex_1_2);
924 REGISTER_FUNCTION(asinh, eval_func(asinh_eval).
925 evalf_func(asinh_evalf).
926 derivative_func(asinh_deriv));
929 // inverse hyperbolic cosine (trigonometric function)
932 static ex acosh_evalf(const ex & x)
934 if (is_exactly_a<numeric>(x))
935 return acosh(ex_to<numeric>(x));
937 return acosh(x).hold();
940 static ex acosh_eval(const ex & x)
942 if (x.info(info_flags::numeric)) {
943 // acosh(0) -> Pi*I/2
945 return Pi*I*numeric(1,2);
947 if (x.is_equal(_ex1))
950 if (x.is_equal(_ex_1))
952 // acosh(float) -> float
953 if (!x.info(info_flags::crational))
954 return acosh(ex_to<numeric>(x));
957 return acosh(x).hold();
960 static ex acosh_deriv(const ex & x, unsigned deriv_param)
962 GINAC_ASSERT(deriv_param==0);
964 // d/dx acosh(x) -> 1/(sqrt(x-1)*sqrt(x+1))
965 return power(x+_ex_1,_ex_1_2)*power(x+_ex1,_ex_1_2);
968 REGISTER_FUNCTION(acosh, eval_func(acosh_eval).
969 evalf_func(acosh_evalf).
970 derivative_func(acosh_deriv));
973 // inverse hyperbolic tangent (trigonometric function)
976 static ex atanh_evalf(const ex & x)
978 if (is_exactly_a<numeric>(x))
979 return atanh(ex_to<numeric>(x));
981 return atanh(x).hold();
984 static ex atanh_eval(const ex & x)
986 if (x.info(info_flags::numeric)) {
990 // atanh({+|-}1) -> throw
991 if (x.is_equal(_ex1) || x.is_equal(_ex_1))
992 throw (pole_error("atanh_eval(): logarithmic pole",0));
993 // atanh(float) -> float
994 if (!x.info(info_flags::crational))
995 return atanh(ex_to<numeric>(x));
998 return atanh(x).hold();
1001 static ex atanh_deriv(const ex & x, unsigned deriv_param)
1003 GINAC_ASSERT(deriv_param==0);
1005 // d/dx atanh(x) -> 1/(1-x^2)
1006 return power(_ex1-power(x,_ex2),_ex_1);
1009 static ex atanh_series(const ex &arg,
1010 const relational &rel,
1014 GINAC_ASSERT(is_exactly_a<symbol>(rel.lhs()));
1016 // Taylor series where there is no pole or cut falls back to atanh_deriv.
1017 // There are two branch cuts, one runnig from 1 up the real axis and one
1018 // one running from -1 down the real axis. The points 1 and -1 are poles
1019 // On the branch cuts and the poles series expand
1020 // (log(1+x)-log(1-x))/2
1022 const ex arg_pt = arg.subs(rel);
1023 if (!(arg_pt).info(info_flags::real))
1024 throw do_taylor(); // Im(x) != 0
1025 if ((arg_pt).info(info_flags::real) && abs(arg_pt)<_ex1)
1026 throw do_taylor(); // Im(x) == 0, but abs(x)<1
1027 // care for the poles, using the defining formula for atanh()...
1028 if (arg_pt.is_equal(_ex1) || arg_pt.is_equal(_ex_1))
1029 return ((log(_ex1+arg)-log(_ex1-arg))*_ex1_2).series(rel, order, options);
1030 // ...and the branch cuts (the discontinuity at the cut being just I*Pi)
1031 if (!(options & series_options::suppress_branchcut)) {
1033 // This is the branch cut: assemble the primitive series manually and
1034 // then add the corresponding complex step function.
1035 const symbol &s = ex_to<symbol>(rel.lhs());
1036 const ex point = rel.rhs();
1038 const ex replarg = series(atanh(arg), s==foo, order).subs(foo==point);
1039 ex Order0correction = replarg.op(0)+csgn(I*arg)*Pi*I*_ex1_2;
1041 Order0correction += log((arg_pt+_ex_1)/(arg_pt+_ex1))*_ex1_2;
1043 Order0correction += log((arg_pt+_ex1)/(arg_pt+_ex_1))*_ex_1_2;
1045 seq.push_back(expair(Order0correction, _ex0));
1046 seq.push_back(expair(Order(_ex1), order));
1047 return series(replarg - pseries(rel, seq), rel, order);
1052 REGISTER_FUNCTION(atanh, eval_func(atanh_eval).
1053 evalf_func(atanh_evalf).
1054 derivative_func(atanh_deriv).
1055 series_func(atanh_series));
1058 } // namespace GiNaC