1 /** @file inifcns_trans.cpp
3 * Implementation of transcendental (and trigonometric and hyperbolic)
7 * GiNaC Copyright (C) 1999-2004 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)
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))
65 if (z.is_equal(_num1))
67 if (z.is_equal(_num2))
69 if (z.is_equal(_num3))
74 if (is_ex_the_function(x, log))
77 // exp(float) -> float
78 if (x.info(info_flags::numeric) && !x.info(info_flags::crational))
79 return exp(ex_to<numeric>(x));
84 static ex exp_deriv(const ex & x, unsigned deriv_param)
86 GINAC_ASSERT(deriv_param==0);
88 // d/dx exp(x) -> exp(x)
92 REGISTER_FUNCTION(exp, eval_func(exp_eval).
93 evalf_func(exp_evalf).
94 derivative_func(exp_deriv).
101 static ex log_evalf(const ex & x)
103 if (is_exactly_a<numeric>(x))
104 return log(ex_to<numeric>(x));
106 return log(x).hold();
109 static ex log_eval(const ex & x)
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 return (log(-x)+I*Pi);
116 if (x.is_equal(_ex1)) // log(1) -> 0
118 if (x.is_equal(I)) // log(I) -> Pi*I/2
119 return (Pi*I*_num1_2);
120 if (x.is_equal(-I)) // log(-I) -> -Pi*I/2
121 return (Pi*I*_num_1_2);
123 // log(float) -> float
124 if (!x.info(info_flags::crational))
125 return log(ex_to<numeric>(x));
128 // log(exp(t)) -> t (if -Pi < t.imag() <= Pi):
129 if (is_ex_the_function(x, exp)) {
130 const ex &t = x.op(0);
131 if (is_a<symbol>(t) && t.info(info_flags::real)) {
134 if (t.info(info_flags::numeric)) {
135 const numeric &nt = ex_to<numeric>(t);
141 return log(x).hold();
144 static ex log_deriv(const ex & x, unsigned deriv_param)
146 GINAC_ASSERT(deriv_param==0);
148 // d/dx log(x) -> 1/x
149 return power(x, _ex_1);
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, subs_options::no_pattern);
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, subs_options::no_pattern);
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);
283 if (is_ex_the_function(x, asin))
286 // sin(acos(x)) -> sqrt(1-x^2)
287 if (is_ex_the_function(x, acos))
288 return sqrt(_ex1-power(t,_ex2));
290 // sin(atan(x)) -> x/sqrt(1+x^2)
291 if (is_ex_the_function(x, atan))
292 return t*power(_ex1+power(t,_ex2),_ex_1_2);
295 // sin(float) -> float
296 if (x.info(info_flags::numeric) && !x.info(info_flags::crational))
297 return sin(ex_to<numeric>(x));
300 if (x.info(info_flags::negative))
303 return sin(x).hold();
306 static ex sin_deriv(const ex & x, unsigned deriv_param)
308 GINAC_ASSERT(deriv_param==0);
310 // d/dx sin(x) -> cos(x)
314 REGISTER_FUNCTION(sin, eval_func(sin_eval).
315 evalf_func(sin_evalf).
316 derivative_func(sin_deriv).
317 latex_name("\\sin"));
320 // cosine (trigonometric function)
323 static ex cos_evalf(const ex & x)
325 if (is_exactly_a<numeric>(x))
326 return cos(ex_to<numeric>(x));
328 return cos(x).hold();
331 static ex cos_eval(const ex & x)
333 // cos(n/d*Pi) -> { all known non-nested radicals }
334 const ex SixtyExOverPi = _ex60*x/Pi;
336 if (SixtyExOverPi.info(info_flags::integer)) {
337 numeric z = mod(ex_to<numeric>(SixtyExOverPi),_num120);
339 // wrap to interval [0, Pi)
343 // wrap to interval [0, Pi/2)
347 if (z.is_equal(_num0)) // cos(0) -> 1
349 if (z.is_equal(_num5)) // cos(Pi/12) -> sqrt(6)/4*(1+sqrt(3)/3)
350 return sign*_ex1_4*sqrt(_ex6)*(_ex1+_ex1_3*sqrt(_ex3));
351 if (z.is_equal(_num10)) // cos(Pi/6) -> sqrt(3)/2
352 return sign*_ex1_2*sqrt(_ex3);
353 if (z.is_equal(_num12)) // cos(Pi/5) -> sqrt(5)/4+1/4
354 return sign*(_ex1_4*sqrt(_ex5)+_ex1_4);
355 if (z.is_equal(_num15)) // cos(Pi/4) -> sqrt(2)/2
356 return sign*_ex1_2*sqrt(_ex2);
357 if (z.is_equal(_num20)) // cos(Pi/3) -> 1/2
359 if (z.is_equal(_num24)) // cos(2/5*Pi) -> sqrt(5)/4-1/4x
360 return sign*(_ex1_4*sqrt(_ex5)+_ex_1_4);
361 if (z.is_equal(_num25)) // cos(5/12*Pi) -> sqrt(6)/4*(1-sqrt(3)/3)
362 return sign*_ex1_4*sqrt(_ex6)*(_ex1+_ex_1_3*sqrt(_ex3));
363 if (z.is_equal(_num30)) // cos(Pi/2) -> 0
367 if (is_exactly_a<function>(x)) {
368 const ex &t = x.op(0);
371 if (is_ex_the_function(x, acos))
374 // cos(asin(x)) -> sqrt(1-x^2)
375 if (is_ex_the_function(x, asin))
376 return sqrt(_ex1-power(t,_ex2));
378 // cos(atan(x)) -> 1/sqrt(1+x^2)
379 if (is_ex_the_function(x, atan))
380 return power(_ex1+power(t,_ex2),_ex_1_2);
383 // cos(float) -> float
384 if (x.info(info_flags::numeric) && !x.info(info_flags::crational))
385 return cos(ex_to<numeric>(x));
388 if (x.info(info_flags::negative))
391 return cos(x).hold();
394 static ex cos_deriv(const ex & x, unsigned deriv_param)
396 GINAC_ASSERT(deriv_param==0);
398 // d/dx cos(x) -> -sin(x)
402 REGISTER_FUNCTION(cos, eval_func(cos_eval).
403 evalf_func(cos_evalf).
404 derivative_func(cos_deriv).
405 latex_name("\\cos"));
408 // tangent (trigonometric function)
411 static ex tan_evalf(const ex & x)
413 if (is_exactly_a<numeric>(x))
414 return tan(ex_to<numeric>(x));
416 return tan(x).hold();
419 static ex tan_eval(const ex & x)
421 // tan(n/d*Pi) -> { all known non-nested radicals }
422 const ex SixtyExOverPi = _ex60*x/Pi;
424 if (SixtyExOverPi.info(info_flags::integer)) {
425 numeric z = mod(ex_to<numeric>(SixtyExOverPi),_num60);
427 // wrap to interval [0, Pi)
431 // wrap to interval [0, Pi/2)
435 if (z.is_equal(_num0)) // tan(0) -> 0
437 if (z.is_equal(_num5)) // tan(Pi/12) -> 2-sqrt(3)
438 return sign*(_ex2-sqrt(_ex3));
439 if (z.is_equal(_num10)) // tan(Pi/6) -> sqrt(3)/3
440 return sign*_ex1_3*sqrt(_ex3);
441 if (z.is_equal(_num15)) // tan(Pi/4) -> 1
443 if (z.is_equal(_num20)) // tan(Pi/3) -> sqrt(3)
444 return sign*sqrt(_ex3);
445 if (z.is_equal(_num25)) // tan(5/12*Pi) -> 2+sqrt(3)
446 return sign*(sqrt(_ex3)+_ex2);
447 if (z.is_equal(_num30)) // tan(Pi/2) -> infinity
448 throw (pole_error("tan_eval(): simple pole",1));
451 if (is_exactly_a<function>(x)) {
452 const ex &t = x.op(0);
455 if (is_ex_the_function(x, atan))
458 // tan(asin(x)) -> x/sqrt(1+x^2)
459 if (is_ex_the_function(x, asin))
460 return t*power(_ex1-power(t,_ex2),_ex_1_2);
462 // tan(acos(x)) -> sqrt(1-x^2)/x
463 if (is_ex_the_function(x, acos))
464 return power(t,_ex_1)*sqrt(_ex1-power(t,_ex2));
467 // tan(float) -> float
468 if (x.info(info_flags::numeric) && !x.info(info_flags::crational)) {
469 return tan(ex_to<numeric>(x));
473 if (x.info(info_flags::negative))
476 return tan(x).hold();
479 static ex tan_deriv(const ex & x, unsigned deriv_param)
481 GINAC_ASSERT(deriv_param==0);
483 // d/dx tan(x) -> 1+tan(x)^2;
484 return (_ex1+power(tan(x),_ex2));
487 static ex tan_series(const ex &x,
488 const relational &rel,
492 GINAC_ASSERT(is_a<symbol>(rel.lhs()));
494 // Taylor series where there is no pole falls back to tan_deriv.
495 // On a pole simply expand sin(x)/cos(x).
496 const ex x_pt = x.subs(rel, subs_options::no_pattern);
497 if (!(2*x_pt/Pi).info(info_flags::odd))
498 throw do_taylor(); // caught by function::series()
499 // if we got here we have to care for a simple pole
500 return (sin(x)/cos(x)).series(rel, order, options);
503 REGISTER_FUNCTION(tan, eval_func(tan_eval).
504 evalf_func(tan_evalf).
505 derivative_func(tan_deriv).
506 series_func(tan_series).
507 latex_name("\\tan"));
510 // inverse sine (arc sine)
513 static ex asin_evalf(const ex & x)
515 if (is_exactly_a<numeric>(x))
516 return asin(ex_to<numeric>(x));
518 return asin(x).hold();
521 static ex asin_eval(const ex & x)
523 if (x.info(info_flags::numeric)) {
530 if (x.is_equal(_ex1_2))
531 return numeric(1,6)*Pi;
534 if (x.is_equal(_ex1))
537 // asin(-1/2) -> -Pi/6
538 if (x.is_equal(_ex_1_2))
539 return numeric(-1,6)*Pi;
542 if (x.is_equal(_ex_1))
545 // asin(float) -> float
546 if (!x.info(info_flags::crational))
547 return asin(ex_to<numeric>(x));
550 if (x.info(info_flags::negative))
554 return asin(x).hold();
557 static ex asin_deriv(const ex & x, unsigned deriv_param)
559 GINAC_ASSERT(deriv_param==0);
561 // d/dx asin(x) -> 1/sqrt(1-x^2)
562 return power(1-power(x,_ex2),_ex_1_2);
565 REGISTER_FUNCTION(asin, eval_func(asin_eval).
566 evalf_func(asin_evalf).
567 derivative_func(asin_deriv).
568 latex_name("\\arcsin"));
571 // inverse cosine (arc cosine)
574 static ex acos_evalf(const ex & x)
576 if (is_exactly_a<numeric>(x))
577 return acos(ex_to<numeric>(x));
579 return acos(x).hold();
582 static ex acos_eval(const ex & x)
584 if (x.info(info_flags::numeric)) {
587 if (x.is_equal(_ex1))
591 if (x.is_equal(_ex1_2))
598 // acos(-1/2) -> 2/3*Pi
599 if (x.is_equal(_ex_1_2))
600 return numeric(2,3)*Pi;
603 if (x.is_equal(_ex_1))
606 // acos(float) -> float
607 if (!x.info(info_flags::crational))
608 return acos(ex_to<numeric>(x));
610 // acos(-x) -> Pi-acos(x)
611 if (x.info(info_flags::negative))
615 return acos(x).hold();
618 static ex acos_deriv(const ex & x, unsigned deriv_param)
620 GINAC_ASSERT(deriv_param==0);
622 // d/dx acos(x) -> -1/sqrt(1-x^2)
623 return -power(1-power(x,_ex2),_ex_1_2);
626 REGISTER_FUNCTION(acos, eval_func(acos_eval).
627 evalf_func(acos_evalf).
628 derivative_func(acos_deriv).
629 latex_name("\\arccos"));
632 // inverse tangent (arc tangent)
635 static ex atan_evalf(const ex & x)
637 if (is_exactly_a<numeric>(x))
638 return atan(ex_to<numeric>(x));
640 return atan(x).hold();
643 static ex atan_eval(const ex & x)
645 if (x.info(info_flags::numeric)) {
652 if (x.is_equal(_ex1))
656 if (x.is_equal(_ex_1))
659 if (x.is_equal(I) || x.is_equal(-I))
660 throw (pole_error("atan_eval(): logarithmic pole",0));
662 // atan(float) -> float
663 if (!x.info(info_flags::crational))
664 return atan(ex_to<numeric>(x));
667 if (x.info(info_flags::negative))
671 return atan(x).hold();
674 static ex atan_deriv(const ex & x, unsigned deriv_param)
676 GINAC_ASSERT(deriv_param==0);
678 // d/dx atan(x) -> 1/(1+x^2)
679 return power(_ex1+power(x,_ex2), _ex_1);
682 static ex atan_series(const ex &arg,
683 const relational &rel,
687 GINAC_ASSERT(is_a<symbol>(rel.lhs()));
689 // Taylor series where there is no pole or cut falls back to atan_deriv.
690 // There are two branch cuts, one runnig from I up the imaginary axis and
691 // one running from -I down the imaginary axis. The points I and -I are
693 // On the branch cuts and the poles series expand
694 // (log(1+I*x)-log(1-I*x))/(2*I)
696 const ex arg_pt = arg.subs(rel, subs_options::no_pattern);
697 if (!(I*arg_pt).info(info_flags::real))
698 throw do_taylor(); // Re(x) != 0
699 if ((I*arg_pt).info(info_flags::real) && abs(I*arg_pt)<_ex1)
700 throw do_taylor(); // Re(x) == 0, but abs(x)<1
701 // care for the poles, using the defining formula for atan()...
702 if (arg_pt.is_equal(I) || arg_pt.is_equal(-I))
703 return ((log(1+I*arg)-log(1-I*arg))/(2*I)).series(rel, order, options);
704 if (!(options & series_options::suppress_branchcut)) {
706 // This is the branch cut: assemble the primitive series manually and
707 // then add the corresponding complex step function.
708 const symbol &s = ex_to<symbol>(rel.lhs());
709 const ex &point = rel.rhs();
711 const ex replarg = series(atan(arg), s==foo, order).subs(foo==point, subs_options::no_pattern);
712 ex Order0correction = replarg.op(0)+csgn(arg)*Pi*_ex_1_2;
714 Order0correction += log((I*arg_pt+_ex_1)/(I*arg_pt+_ex1))*I*_ex_1_2;
716 Order0correction += log((I*arg_pt+_ex1)/(I*arg_pt+_ex_1))*I*_ex1_2;
718 seq.push_back(expair(Order0correction, _ex0));
719 seq.push_back(expair(Order(_ex1), order));
720 return series(replarg - pseries(rel, seq), rel, order);
725 REGISTER_FUNCTION(atan, eval_func(atan_eval).
726 evalf_func(atan_evalf).
727 derivative_func(atan_deriv).
728 series_func(atan_series).
729 latex_name("\\arctan"));
732 // inverse tangent (atan2(y,x))
735 static ex atan2_evalf(const ex &y, const ex &x)
737 if (is_exactly_a<numeric>(y) && is_exactly_a<numeric>(x))
738 return atan(ex_to<numeric>(y), ex_to<numeric>(x));
740 return atan2(y, x).hold();
743 static ex atan2_eval(const ex & y, const ex & x)
745 if (y.info(info_flags::numeric) && x.info(info_flags::numeric)) {
753 // atan(0, x), x real and positive -> 0
754 if (x.info(info_flags::positive))
757 // atan(0, x), x real and negative -> -Pi
758 if (x.info(info_flags::negative))
764 // atan(y, 0), y real and positive -> Pi/2
765 if (y.info(info_flags::positive))
768 // atan(y, 0), y real and negative -> -Pi/2
769 if (y.info(info_flags::negative))
775 // atan(y, y), y real and positive -> Pi/4
776 if (y.info(info_flags::positive))
779 // atan(y, y), y real and negative -> -3/4*Pi
780 if (y.info(info_flags::negative))
781 return numeric(-3, 4)*Pi;
784 if (y.is_equal(-x)) {
786 // atan(y, -y), y real and positive -> 3*Pi/4
787 if (y.info(info_flags::positive))
788 return numeric(3, 4)*Pi;
790 // atan(y, -y), y real and negative -> -Pi/4
791 if (y.info(info_flags::negative))
795 // atan(float, float) -> float
796 if (!y.info(info_flags::crational) && !x.info(info_flags::crational))
797 return atan(ex_to<numeric>(y), ex_to<numeric>(x));
799 // atan(real, real) -> atan(y/x) +/- Pi
800 if (y.info(info_flags::real) && x.info(info_flags::real)) {
801 if (x.info(info_flags::positive))
803 else if(y.info(info_flags::positive))
810 return atan2(y, x).hold();
813 static ex atan2_deriv(const ex & y, const ex & x, unsigned deriv_param)
815 GINAC_ASSERT(deriv_param<2);
817 if (deriv_param==0) {
819 return x*power(power(x,_ex2)+power(y,_ex2),_ex_1);
822 return -y*power(power(x,_ex2)+power(y,_ex2),_ex_1);
825 REGISTER_FUNCTION(atan2, eval_func(atan2_eval).
826 evalf_func(atan2_evalf).
827 derivative_func(atan2_deriv));
830 // hyperbolic sine (trigonometric function)
833 static ex sinh_evalf(const ex & x)
835 if (is_exactly_a<numeric>(x))
836 return sinh(ex_to<numeric>(x));
838 return sinh(x).hold();
841 static ex sinh_eval(const ex & x)
843 if (x.info(info_flags::numeric)) {
849 // sinh(float) -> float
850 if (!x.info(info_flags::crational))
851 return sinh(ex_to<numeric>(x));
854 if (x.info(info_flags::negative))
858 if ((x/Pi).info(info_flags::numeric) &&
859 ex_to<numeric>(x/Pi).real().is_zero()) // sinh(I*x) -> I*sin(x)
862 if (is_exactly_a<function>(x)) {
863 const ex &t = x.op(0);
865 // sinh(asinh(x)) -> x
866 if (is_ex_the_function(x, asinh))
869 // sinh(acosh(x)) -> sqrt(x-1) * sqrt(x+1)
870 if (is_ex_the_function(x, acosh))
871 return sqrt(t-_ex1)*sqrt(t+_ex1);
873 // sinh(atanh(x)) -> x/sqrt(1-x^2)
874 if (is_ex_the_function(x, atanh))
875 return t*power(_ex1-power(t,_ex2),_ex_1_2);
878 return sinh(x).hold();
881 static ex sinh_deriv(const ex & x, unsigned deriv_param)
883 GINAC_ASSERT(deriv_param==0);
885 // d/dx sinh(x) -> cosh(x)
889 REGISTER_FUNCTION(sinh, eval_func(sinh_eval).
890 evalf_func(sinh_evalf).
891 derivative_func(sinh_deriv).
892 latex_name("\\sinh"));
895 // hyperbolic cosine (trigonometric function)
898 static ex cosh_evalf(const ex & x)
900 if (is_exactly_a<numeric>(x))
901 return cosh(ex_to<numeric>(x));
903 return cosh(x).hold();
906 static ex cosh_eval(const ex & x)
908 if (x.info(info_flags::numeric)) {
914 // cosh(float) -> float
915 if (!x.info(info_flags::crational))
916 return cosh(ex_to<numeric>(x));
919 if (x.info(info_flags::negative))
923 if ((x/Pi).info(info_flags::numeric) &&
924 ex_to<numeric>(x/Pi).real().is_zero()) // cosh(I*x) -> cos(x)
927 if (is_exactly_a<function>(x)) {
928 const ex &t = x.op(0);
930 // cosh(acosh(x)) -> x
931 if (is_ex_the_function(x, acosh))
934 // cosh(asinh(x)) -> sqrt(1+x^2)
935 if (is_ex_the_function(x, asinh))
936 return sqrt(_ex1+power(t,_ex2));
938 // cosh(atanh(x)) -> 1/sqrt(1-x^2)
939 if (is_ex_the_function(x, atanh))
940 return power(_ex1-power(t,_ex2),_ex_1_2);
943 return cosh(x).hold();
946 static ex cosh_deriv(const ex & x, unsigned deriv_param)
948 GINAC_ASSERT(deriv_param==0);
950 // d/dx cosh(x) -> sinh(x)
954 REGISTER_FUNCTION(cosh, eval_func(cosh_eval).
955 evalf_func(cosh_evalf).
956 derivative_func(cosh_deriv).
957 latex_name("\\cosh"));
960 // hyperbolic tangent (trigonometric function)
963 static ex tanh_evalf(const ex & x)
965 if (is_exactly_a<numeric>(x))
966 return tanh(ex_to<numeric>(x));
968 return tanh(x).hold();
971 static ex tanh_eval(const ex & x)
973 if (x.info(info_flags::numeric)) {
979 // tanh(float) -> float
980 if (!x.info(info_flags::crational))
981 return tanh(ex_to<numeric>(x));
984 if (x.info(info_flags::negative))
988 if ((x/Pi).info(info_flags::numeric) &&
989 ex_to<numeric>(x/Pi).real().is_zero()) // tanh(I*x) -> I*tan(x);
992 if (is_exactly_a<function>(x)) {
993 const ex &t = x.op(0);
995 // tanh(atanh(x)) -> x
996 if (is_ex_the_function(x, atanh))
999 // tanh(asinh(x)) -> x/sqrt(1+x^2)
1000 if (is_ex_the_function(x, asinh))
1001 return t*power(_ex1+power(t,_ex2),_ex_1_2);
1003 // tanh(acosh(x)) -> sqrt(x-1)*sqrt(x+1)/x
1004 if (is_ex_the_function(x, acosh))
1005 return sqrt(t-_ex1)*sqrt(t+_ex1)*power(t,_ex_1);
1008 return tanh(x).hold();
1011 static ex tanh_deriv(const ex & x, unsigned deriv_param)
1013 GINAC_ASSERT(deriv_param==0);
1015 // d/dx tanh(x) -> 1-tanh(x)^2
1016 return _ex1-power(tanh(x),_ex2);
1019 static ex tanh_series(const ex &x,
1020 const relational &rel,
1024 GINAC_ASSERT(is_a<symbol>(rel.lhs()));
1026 // Taylor series where there is no pole falls back to tanh_deriv.
1027 // On a pole simply expand sinh(x)/cosh(x).
1028 const ex x_pt = x.subs(rel, subs_options::no_pattern);
1029 if (!(2*I*x_pt/Pi).info(info_flags::odd))
1030 throw do_taylor(); // caught by function::series()
1031 // if we got here we have to care for a simple pole
1032 return (sinh(x)/cosh(x)).series(rel, order, options);
1035 REGISTER_FUNCTION(tanh, eval_func(tanh_eval).
1036 evalf_func(tanh_evalf).
1037 derivative_func(tanh_deriv).
1038 series_func(tanh_series).
1039 latex_name("\\tanh"));
1042 // inverse hyperbolic sine (trigonometric function)
1045 static ex asinh_evalf(const ex & x)
1047 if (is_exactly_a<numeric>(x))
1048 return asinh(ex_to<numeric>(x));
1050 return asinh(x).hold();
1053 static ex asinh_eval(const ex & x)
1055 if (x.info(info_flags::numeric)) {
1061 // asinh(float) -> float
1062 if (!x.info(info_flags::crational))
1063 return asinh(ex_to<numeric>(x));
1066 if (x.info(info_flags::negative))
1070 return asinh(x).hold();
1073 static ex asinh_deriv(const ex & x, unsigned deriv_param)
1075 GINAC_ASSERT(deriv_param==0);
1077 // d/dx asinh(x) -> 1/sqrt(1+x^2)
1078 return power(_ex1+power(x,_ex2),_ex_1_2);
1081 REGISTER_FUNCTION(asinh, eval_func(asinh_eval).
1082 evalf_func(asinh_evalf).
1083 derivative_func(asinh_deriv));
1086 // inverse hyperbolic cosine (trigonometric function)
1089 static ex acosh_evalf(const ex & x)
1091 if (is_exactly_a<numeric>(x))
1092 return acosh(ex_to<numeric>(x));
1094 return acosh(x).hold();
1097 static ex acosh_eval(const ex & x)
1099 if (x.info(info_flags::numeric)) {
1101 // acosh(0) -> Pi*I/2
1103 return Pi*I*numeric(1,2);
1106 if (x.is_equal(_ex1))
1109 // acosh(-1) -> Pi*I
1110 if (x.is_equal(_ex_1))
1113 // acosh(float) -> float
1114 if (!x.info(info_flags::crational))
1115 return acosh(ex_to<numeric>(x));
1117 // acosh(-x) -> Pi*I-acosh(x)
1118 if (x.info(info_flags::negative))
1119 return Pi*I-acosh(-x);
1122 return acosh(x).hold();
1125 static ex acosh_deriv(const ex & x, unsigned deriv_param)
1127 GINAC_ASSERT(deriv_param==0);
1129 // d/dx acosh(x) -> 1/(sqrt(x-1)*sqrt(x+1))
1130 return power(x+_ex_1,_ex_1_2)*power(x+_ex1,_ex_1_2);
1133 REGISTER_FUNCTION(acosh, eval_func(acosh_eval).
1134 evalf_func(acosh_evalf).
1135 derivative_func(acosh_deriv));
1138 // inverse hyperbolic tangent (trigonometric function)
1141 static ex atanh_evalf(const ex & x)
1143 if (is_exactly_a<numeric>(x))
1144 return atanh(ex_to<numeric>(x));
1146 return atanh(x).hold();
1149 static ex atanh_eval(const ex & x)
1151 if (x.info(info_flags::numeric)) {
1157 // atanh({+|-}1) -> throw
1158 if (x.is_equal(_ex1) || x.is_equal(_ex_1))
1159 throw (pole_error("atanh_eval(): logarithmic pole",0));
1161 // atanh(float) -> float
1162 if (!x.info(info_flags::crational))
1163 return atanh(ex_to<numeric>(x));
1166 if (x.info(info_flags::negative))
1170 return atanh(x).hold();
1173 static ex atanh_deriv(const ex & x, unsigned deriv_param)
1175 GINAC_ASSERT(deriv_param==0);
1177 // d/dx atanh(x) -> 1/(1-x^2)
1178 return power(_ex1-power(x,_ex2),_ex_1);
1181 static ex atanh_series(const ex &arg,
1182 const relational &rel,
1186 GINAC_ASSERT(is_a<symbol>(rel.lhs()));
1188 // Taylor series where there is no pole or cut falls back to atanh_deriv.
1189 // There are two branch cuts, one runnig from 1 up the real axis and one
1190 // one running from -1 down the real axis. The points 1 and -1 are poles
1191 // On the branch cuts and the poles series expand
1192 // (log(1+x)-log(1-x))/2
1194 const ex arg_pt = arg.subs(rel, subs_options::no_pattern);
1195 if (!(arg_pt).info(info_flags::real))
1196 throw do_taylor(); // Im(x) != 0
1197 if ((arg_pt).info(info_flags::real) && abs(arg_pt)<_ex1)
1198 throw do_taylor(); // Im(x) == 0, but abs(x)<1
1199 // care for the poles, using the defining formula for atanh()...
1200 if (arg_pt.is_equal(_ex1) || arg_pt.is_equal(_ex_1))
1201 return ((log(_ex1+arg)-log(_ex1-arg))*_ex1_2).series(rel, order, options);
1202 // ...and the branch cuts (the discontinuity at the cut being just I*Pi)
1203 if (!(options & series_options::suppress_branchcut)) {
1205 // This is the branch cut: assemble the primitive series manually and
1206 // then add the corresponding complex step function.
1207 const symbol &s = ex_to<symbol>(rel.lhs());
1208 const ex &point = rel.rhs();
1210 const ex replarg = series(atanh(arg), s==foo, order).subs(foo==point, subs_options::no_pattern);
1211 ex Order0correction = replarg.op(0)+csgn(I*arg)*Pi*I*_ex1_2;
1213 Order0correction += log((arg_pt+_ex_1)/(arg_pt+_ex1))*_ex1_2;
1215 Order0correction += log((arg_pt+_ex1)/(arg_pt+_ex_1))*_ex_1_2;
1217 seq.push_back(expair(Order0correction, _ex0));
1218 seq.push_back(expair(Order(_ex1), order));
1219 return series(replarg - pseries(rel, seq), rel, order);
1224 REGISTER_FUNCTION(atanh, eval_func(atanh_eval).
1225 evalf_func(atanh_evalf).
1226 derivative_func(atanh_deriv).
1227 series_func(atanh_series));
1230 } // namespace GiNaC